This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
missed a file
[perl5.git] / op.c
CommitLineData
a0d0e21e 1/* op.c
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e
LW
8 */
9
10/*
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
79072805
LW
16 */
17
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_OP_C
79072805
LW
20#include "perl.h"
21
76e3520e 22#ifdef PERL_OBJECT
22c35a8c 23#define CHECKCALL this->*PL_check
76e3520e 24#else
22c35a8c 25#define CHECKCALL *PL_check
b7dc083c
NIS
26#endif
27
28/* #define PL_OP_SLAB_ALLOC */
29
30#ifdef PL_OP_SLAB_ALLOC
31#define SLAB_SIZE 8192
32static char *PL_OpPtr = NULL;
33static int PL_OpSpace = 0;
34#define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
35 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
36 else \
37 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
38 } while (0)
39
864dbfa3
GS
40STATIC void *
41Slab_Alloc(pTHX_ int m, size_t sz)
b7dc083c
NIS
42{
43 Newz(m,PL_OpPtr,SLAB_SIZE,char);
44 PL_OpSpace = SLAB_SIZE - sz;
45 return PL_OpPtr += PL_OpSpace;
46}
76e3520e 47
b7dc083c
NIS
48#else
49#define NewOp(m, var, c, type) Newz(m, var, c, type)
50#endif
e50aee73 51/*
5dc0d613 52 * In the following definition, the ", Nullop" is just to make the compiler
a5f75d66 53 * think the expression is of the right type: croak actually does a Siglongjmp.
e50aee73 54 */
11343788 55#define CHECKOP(type,o) \
3280af22 56 ((PL_op_mask && PL_op_mask[type]) \
5dc0d613 57 ? ( op_free((OP*)o), \
22c35a8c 58 croak("%s trapped by operation mask", PL_op_desc[type]), \
28757baa 59 Nullop ) \
76e3520e 60 : (CHECKCALL[type])((OP*)o))
e50aee73 61
c53d7c7d
HS
62#define PAD_MAX 999999999
63
76e3520e 64STATIC char*
864dbfa3 65gv_ename(pTHX_ GV *gv)
4633a7c4 66{
2d8e6c8d 67 STRLEN n_a;
4633a7c4 68 SV* tmpsv = sv_newmortal();
46fc3d4c 69 gv_efullname3(tmpsv, gv, Nullch);
2d8e6c8d 70 return SvPV(tmpsv,n_a);
4633a7c4
LW
71}
72
76e3520e 73STATIC OP *
864dbfa3 74no_fh_allowed(pTHX_ OP *o)
79072805 75{
46fc3d4c 76 yyerror(form("Missing comma after first argument to %s function",
22c35a8c 77 PL_op_desc[o->op_type]));
11343788 78 return o;
79072805
LW
79}
80
76e3520e 81STATIC OP *
864dbfa3 82too_few_arguments(pTHX_ OP *o, char *name)
79072805 83{
46fc3d4c 84 yyerror(form("Not enough arguments for %s", name));
11343788 85 return o;
79072805
LW
86}
87
76e3520e 88STATIC OP *
864dbfa3 89too_many_arguments(pTHX_ OP *o, char *name)
79072805 90{
46fc3d4c 91 yyerror(form("Too many arguments for %s", name));
11343788 92 return o;
79072805
LW
93}
94
76e3520e 95STATIC void
864dbfa3 96bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
8990e307 97{
46fc3d4c 98 yyerror(form("Type of arg %d to %s must be %s (not %s)",
22c35a8c 99 (int)n, name, t, PL_op_desc[kid->op_type]));
8990e307
LW
100}
101
7a52d87a 102STATIC void
864dbfa3 103no_bareword_allowed(pTHX_ OP *o)
7a52d87a
GS
104{
105 warn("Bareword \"%s\" not allowed while \"strict subs\" in use",
106 SvPV_nolen(cSVOPo->op_sv));
107 ++PL_error_count;
108}
109
a0d0e21e 110void
864dbfa3 111Perl_assertref(pTHX_ OP *o)
a0d0e21e 112{
11343788 113 int type = o->op_type;
136254bc 114 if (type != OP_AELEM && type != OP_HELEM && type != OP_GELEM) {
22c35a8c 115 yyerror(form("Can't use subscript on %s", PL_op_desc[type]));
bde90e66 116 if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) {
9c82454c 117 dTHR;
bde90e66
HS
118 SV *msg = sv_2mortal(
119 newSVpvf("(Did you mean $ or @ instead of %c?)\n",
120 type == OP_ENTERSUB ? '&' : '%'));
faef0170 121 if (PL_in_eval & EVAL_WARNONLY)
bde90e66 122 warn("%_", msg);
3280af22
NIS
123 else if (PL_in_eval)
124 sv_catsv(GvSV(PL_errgv), msg);
bde90e66
HS
125 else
126 PerlIO_write(PerlIO_stderr(), SvPVX(msg), SvCUR(msg));
127 }
a0d0e21e
LW
128 }
129}
130
79072805
LW
131/* "register" allocation */
132
133PADOFFSET
864dbfa3 134Perl_pad_allocmy(pTHX_ char *name)
93a17b20 135{
11343788 136 dTHR;
a0d0e21e
LW
137 PADOFFSET off;
138 SV *sv;
139
834a4ddd
LW
140 if (!(
141 isALPHA(name[1]) ||
142 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
143 name[1] == '_' && (int)strlen(name) > 2))
144 {
46fc3d4c 145 if (!isPRINT(name[1])) {
2b92dfce
GS
146 /* 1999-02-27 mjd@plover.com */
147 char *p;
148 p = strchr(name, '\0');
149 /* The next block assumes the buffer is at least 205 chars
150 long. At present, it's always at least 256 chars. */
151 if (p-name > 200) {
152 strcpy(name+200, "...");
153 p = name+199;
154 }
155 else {
156 p[1] = '\0';
157 }
158 /* Move everything else down one character */
159 for (; p-name > 2; p--)
160 *p = *(p-1);
46fc3d4c 161 name[2] = toCTRL(name[1]);
162 name[1] = '^';
163 }
dce40276 164 yyerror(form("Can't use global %s in \"my\"",name));
a0d0e21e 165 }
599cee73 166 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
3280af22
NIS
167 SV **svp = AvARRAY(PL_comppad_name);
168 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
b1cb66bf 169 if ((sv = svp[off])
3280af22 170 && sv != &PL_sv_undef
c53d7c7d 171 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
b1cb66bf 172 && strEQ(name, SvPVX(sv)))
173 {
599cee73 174 warner(WARN_UNSAFE,
9fbbe825 175 "\"my\" variable %s masks earlier declaration in same %s",
c53d7c7d 176 name, (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
b1cb66bf 177 break;
178 }
179 }
180 }
a0d0e21e
LW
181 off = pad_alloc(OP_PADSV, SVs_PADMY);
182 sv = NEWSV(1102,0);
93a17b20
LW
183 sv_upgrade(sv, SVt_PVNV);
184 sv_setpv(sv, name);
3280af22 185 if (PL_in_my_stash) {
c750a3ec 186 if (*name != '$')
dce40276
GS
187 yyerror(form("Can't declare class for non-scalar %s in \"my\"",
188 name));
c750a3ec
MB
189 SvOBJECT_on(sv);
190 (void)SvUPGRADE(sv, SVt_PVMG);
3280af22
NIS
191 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
192 PL_sv_objcount++;
c750a3ec 193 }
3280af22 194 av_store(PL_comppad_name, off, sv);
c53d7c7d 195 SvNVX(sv) = (double)PAD_MAX;
8990e307 196 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
3280af22
NIS
197 if (!PL_min_intro_pending)
198 PL_min_intro_pending = off;
199 PL_max_intro_pending = off;
93a17b20 200 if (*name == '@')
3280af22 201 av_store(PL_comppad, off, (SV*)newAV());
93a17b20 202 else if (*name == '%')
3280af22
NIS
203 av_store(PL_comppad, off, (SV*)newHV());
204 SvPADMY_on(PL_curpad[off]);
93a17b20
LW
205 return off;
206}
207
2680586e
GS
208#define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
209
76e3520e 210STATIC PADOFFSET
864dbfa3
GS
211pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
212 I32 cx_ix, I32 saweval, U32 flags)
93a17b20 213{
11343788 214 dTHR;
748a9306 215 CV *cv;
93a17b20
LW
216 I32 off;
217 SV *sv;
93a17b20 218 register I32 i;
c09156bb 219 register PERL_CONTEXT *cx;
93a17b20 220
748a9306 221 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
4fdae800 222 AV *curlist = CvPADLIST(cv);
223 SV **svp = av_fetch(curlist, 0, FALSE);
748a9306 224 AV *curname;
4fdae800 225
3280af22 226 if (!svp || *svp == &PL_sv_undef)
4633a7c4 227 continue;
748a9306
LW
228 curname = (AV*)*svp;
229 svp = AvARRAY(curname);
93965878 230 for (off = AvFILLp(curname); off > 0; off--) {
748a9306 231 if ((sv = svp[off]) &&
3280af22 232 sv != &PL_sv_undef &&
748a9306 233 seq <= SvIVX(sv) &&
13826f2c 234 seq > I_32(SvNVX(sv)) &&
748a9306
LW
235 strEQ(SvPVX(sv), name))
236 {
5f05dabc 237 I32 depth;
238 AV *oldpad;
239 SV *oldsv;
240
241 depth = CvDEPTH(cv);
242 if (!depth) {
9607fc9c 243 if (newoff) {
244 if (SvFAKE(sv))
245 continue;
4fdae800 246 return 0; /* don't clone from inactive stack frame */
9607fc9c 247 }
5f05dabc 248 depth = 1;
249 }
250 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
251 oldsv = *av_fetch(oldpad, off, TRUE);
748a9306 252 if (!newoff) { /* Not a mere clone operation. */
9607fc9c 253 SV *namesv = NEWSV(1103,0);
748a9306 254 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
9607fc9c 255 sv_upgrade(namesv, SVt_PVNV);
256 sv_setpv(namesv, name);
3280af22
NIS
257 av_store(PL_comppad_name, newoff, namesv);
258 SvNVX(namesv) = (double)PL_curcop->cop_seq;
c53d7c7d 259 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
9607fc9c 260 SvFAKE_on(namesv); /* A ref, not a real var */
87671ffc 261 if (SvOBJECT(sv)) { /* A typed var */
3d93dc8b
GS
262 SvOBJECT_on(namesv);
263 (void)SvUPGRADE(namesv, SVt_PVMG);
87671ffc 264 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
3d93dc8b
GS
265 PL_sv_objcount++;
266 }
3280af22 267 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
28757baa 268 /* "It's closures all the way down." */
3280af22 269 CvCLONE_on(PL_compcv);
54310121 270 if (cv == startcv) {
3280af22 271 if (CvANON(PL_compcv))
54310121 272 oldsv = Nullsv; /* no need to keep ref */
273 }
274 else {
28757baa 275 CV *bcv;
276 for (bcv = startcv;
277 bcv && bcv != cv && !CvCLONE(bcv);
6b35e009
GS
278 bcv = CvOUTSIDE(bcv))
279 {
28757baa 280 if (CvANON(bcv))
281 CvCLONE_on(bcv);
282 else {
6b35e009
GS
283 if (ckWARN(WARN_CLOSURE)
284 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
285 {
599cee73 286 warner(WARN_CLOSURE,
44a8e56a 287 "Variable \"%s\" may be unavailable",
28757baa 288 name);
6b35e009 289 }
28757baa 290 break;
291 }
292 }
293 }
294 }
3280af22 295 else if (!CvUNIQUE(PL_compcv)) {
599cee73
PM
296 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
297 warner(WARN_CLOSURE,
298 "Variable \"%s\" will not stay shared", name);
5f05dabc 299 }
748a9306 300 }
3280af22 301 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
748a9306
LW
302 return newoff;
303 }
93a17b20
LW
304 }
305 }
306
2680586e
GS
307 if (flags & FINDLEX_NOSEARCH)
308 return 0;
309
93a17b20
LW
310 /* Nothing in current lexical context--try eval's context, if any.
311 * This is necessary to let the perldb get at lexically scoped variables.
312 * XXX This will also probably interact badly with eval tree caching.
313 */
314
748a9306 315 for (i = cx_ix; i >= 0; i--) {
93a17b20 316 cx = &cxstack[i];
6b35e009 317 switch (CxTYPE(cx)) {
93a17b20 318 default:
748a9306
LW
319 if (i == 0 && saweval) {
320 seq = cxstack[saweval].blk_oldcop->cop_seq;
2680586e 321 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
748a9306 322 }
93a17b20
LW
323 break;
324 case CXt_EVAL:
44a8e56a 325 switch (cx->blk_eval.old_op_type) {
326 case OP_ENTEREVAL:
6b35e009
GS
327 if (CxREALEVAL(cx))
328 saweval = i;
44a8e56a 329 break;
330 case OP_REQUIRE:
331 /* require must have its own scope */
332 return 0;
333 }
93a17b20
LW
334 break;
335 case CXt_SUB:
336 if (!saweval)
337 return 0;
338 cv = cx->blk_sub.cv;
3280af22 339 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
748a9306 340 saweval = i; /* so we know where we were called from */
93a17b20 341 continue;
93a17b20 342 }
748a9306 343 seq = cxstack[saweval].blk_oldcop->cop_seq;
2680586e 344 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
93a17b20
LW
345 }
346 }
347
748a9306
LW
348 return 0;
349}
a0d0e21e 350
748a9306 351PADOFFSET
864dbfa3 352Perl_pad_findmy(pTHX_ char *name)
748a9306 353{
11343788 354 dTHR;
748a9306 355 I32 off;
54310121 356 I32 pendoff = 0;
748a9306 357 SV *sv;
3280af22
NIS
358 SV **svp = AvARRAY(PL_comppad_name);
359 U32 seq = PL_cop_seqmax;
6b35e009 360 PERL_CONTEXT *cx;
33b8ce05 361 CV *outside;
748a9306 362
11343788
MB
363#ifdef USE_THREADS
364 /*
365 * Special case to get lexical (and hence per-thread) @_.
366 * XXX I need to find out how to tell at parse-time whether use
367 * of @_ should refer to a lexical (from a sub) or defgv (global
368 * scope and maybe weird sub-ish things like formats). See
369 * startsub in perly.y. It's possible that @_ could be lexical
370 * (at least from subs) even in non-threaded perl.
371 */
372 if (strEQ(name, "@_"))
373 return 0; /* success. (NOT_IN_PAD indicates failure) */
374#endif /* USE_THREADS */
375
748a9306 376 /* The one we're looking for is probably just before comppad_name_fill. */
3280af22 377 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
a0d0e21e 378 if ((sv = svp[off]) &&
3280af22 379 sv != &PL_sv_undef &&
54310121 380 (!SvIVX(sv) ||
381 (seq <= SvIVX(sv) &&
382 seq > I_32(SvNVX(sv)))) &&
a0d0e21e
LW
383 strEQ(SvPVX(sv), name))
384 {
54310121 385 if (SvIVX(sv))
386 return (PADOFFSET)off;
387 pendoff = off; /* this pending def. will override import */
a0d0e21e
LW
388 }
389 }
748a9306 390
33b8ce05
GS
391 outside = CvOUTSIDE(PL_compcv);
392
393 /* Check if if we're compiling an eval'', and adjust seq to be the
394 * eval's seq number. This depends on eval'' having a non-null
395 * CvOUTSIDE() while it is being compiled. The eval'' itself is
1aff0e91
GS
396 * identified by CvEVAL being true and CvGV being null. */
397 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
6b35e009
GS
398 cx = &cxstack[cxstack_ix];
399 if (CxREALEVAL(cx))
400 seq = cx->blk_oldcop->cop_seq;
401 }
402
748a9306 403 /* See if it's in a nested scope */
2680586e 404 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
54310121 405 if (off) {
406 /* If there is a pending local definition, this new alias must die */
407 if (pendoff)
3280af22 408 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
11343788 409 return off; /* pad_findlex returns 0 for failure...*/
54310121 410 }
11343788 411 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
93a17b20
LW
412}
413
414void
864dbfa3 415Perl_pad_leavemy(pTHX_ I32 fill)
93a17b20
LW
416{
417 I32 off;
3280af22 418 SV **svp = AvARRAY(PL_comppad_name);
93a17b20 419 SV *sv;
3280af22
NIS
420 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
421 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
422 if ((sv = svp[off]) && sv != &PL_sv_undef)
8990e307
LW
423 warn("%s never introduced", SvPVX(sv));
424 }
425 }
426 /* "Deintroduce" my variables that are leaving with this scope. */
3280af22 427 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
c53d7c7d 428 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
3280af22 429 SvIVX(sv) = PL_cop_seqmax;
93a17b20
LW
430 }
431}
432
433PADOFFSET
864dbfa3 434Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
79072805 435{
11343788 436 dTHR;
79072805
LW
437 SV *sv;
438 I32 retval;
439
3280af22 440 if (AvARRAY(PL_comppad) != PL_curpad)
463ee0b2 441 croak("panic: pad_alloc");
3280af22 442 if (PL_pad_reset_pending)
a0d0e21e 443 pad_reset();
ed6116ce 444 if (tmptype & SVs_PADMY) {
79072805 445 do {
3280af22 446 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
ed6116ce 447 } while (SvPADBUSY(sv)); /* need a fresh one */
3280af22 448 retval = AvFILLp(PL_comppad);
79072805
LW
449 }
450 else {
3280af22
NIS
451 SV **names = AvARRAY(PL_comppad_name);
452 SSize_t names_fill = AvFILLp(PL_comppad_name);
bbce6d69 453 for (;;) {
454 /*
455 * "foreach" index vars temporarily become aliases to non-"my"
456 * values. Thus we must skip, not just pad values that are
457 * marked as current pad values, but also those with names.
458 */
3280af22
NIS
459 if (++PL_padix <= names_fill &&
460 (sv = names[PL_padix]) && sv != &PL_sv_undef)
bbce6d69 461 continue;
3280af22 462 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
bbce6d69 463 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
464 break;
465 }
3280af22 466 retval = PL_padix;
79072805 467 }
8990e307 468 SvFLAGS(sv) |= tmptype;
3280af22 469 PL_curpad = AvARRAY(PL_comppad);
11343788 470#ifdef USE_THREADS
5dc0d613 471 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
533c011a 472 (unsigned long) thr, (unsigned long) PL_curpad,
22c35a8c 473 (long) retval, PL_op_name[optype]));
11343788 474#else
d9bb4600 475 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
3280af22 476 (unsigned long) PL_curpad,
22c35a8c 477 (long) retval, PL_op_name[optype]));
11343788 478#endif /* USE_THREADS */
79072805
LW
479 return (PADOFFSET)retval;
480}
481
482SV *
864dbfa3 483Perl_pad_sv(pTHX_ PADOFFSET po)
79072805 484{
11343788
MB
485 dTHR;
486#ifdef USE_THREADS
5dc0d613 487 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
533c011a 488 (unsigned long) thr, (unsigned long) PL_curpad, po));
11343788 489#else
79072805 490 if (!po)
463ee0b2 491 croak("panic: pad_sv po");
d9bb4600 492 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
3280af22 493 (unsigned long) PL_curpad, po));
11343788 494#endif /* USE_THREADS */
3280af22 495 return PL_curpad[po]; /* eventually we'll turn this into a macro */
79072805
LW
496}
497
498void
864dbfa3 499Perl_pad_free(pTHX_ PADOFFSET po)
79072805 500{
11343788 501 dTHR;
3280af22 502 if (!PL_curpad)
a0d0e21e 503 return;
3280af22 504 if (AvARRAY(PL_comppad) != PL_curpad)
463ee0b2 505 croak("panic: pad_free curpad");
79072805 506 if (!po)
463ee0b2 507 croak("panic: pad_free po");
11343788 508#ifdef USE_THREADS
5dc0d613 509 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
533c011a 510 (unsigned long) thr, (unsigned long) PL_curpad, po));
11343788 511#else
d9bb4600 512 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
3280af22 513 (unsigned long) PL_curpad, po));
11343788 514#endif /* USE_THREADS */
3280af22
NIS
515 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
516 SvPADTMP_off(PL_curpad[po]);
517 if ((I32)po < PL_padix)
518 PL_padix = po - 1;
79072805
LW
519}
520
521void
864dbfa3 522Perl_pad_swipe(pTHX_ PADOFFSET po)
79072805 523{
11343788 524 dTHR;
3280af22 525 if (AvARRAY(PL_comppad) != PL_curpad)
463ee0b2 526 croak("panic: pad_swipe curpad");
79072805 527 if (!po)
463ee0b2 528 croak("panic: pad_swipe po");
11343788 529#ifdef USE_THREADS
5dc0d613 530 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
533c011a 531 (unsigned long) thr, (unsigned long) PL_curpad, po));
11343788 532#else
d9bb4600 533 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
3280af22 534 (unsigned long) PL_curpad, po));
11343788 535#endif /* USE_THREADS */
3280af22
NIS
536 SvPADTMP_off(PL_curpad[po]);
537 PL_curpad[po] = NEWSV(1107,0);
538 SvPADTMP_on(PL_curpad[po]);
539 if ((I32)po < PL_padix)
540 PL_padix = po - 1;
79072805
LW
541}
542
d9bb4600
GS
543/* XXX pad_reset() is currently disabled because it results in serious bugs.
544 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
545 * on the stack by OPs that use them, there are several ways to get an alias
546 * to a shared TARG. Such an alias will change randomly and unpredictably.
547 * We avoid doing this until we can think of a Better Way.
548 * GSAR 97-10-29 */
79072805 549void
864dbfa3 550Perl_pad_reset(pTHX)
79072805 551{
d9bb4600 552#ifdef USE_BROKEN_PAD_RESET
11343788 553 dTHR;
79072805
LW
554 register I32 po;
555
6b88bc9c 556 if (AvARRAY(PL_comppad) != PL_curpad)
463ee0b2 557 croak("panic: pad_reset curpad");
11343788 558#ifdef USE_THREADS
5dc0d613 559 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
6b88bc9c 560 (unsigned long) thr, (unsigned long) PL_curpad));
11343788 561#else
d9bb4600 562 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
6b88bc9c 563 (unsigned long) PL_curpad));
11343788 564#endif /* USE_THREADS */
6b88bc9c
GS
565 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
566 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
567 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
568 SvPADTMP_off(PL_curpad[po]);
748a9306 569 }
6b88bc9c 570 PL_padix = PL_padix_floor;
79072805 571 }
d9bb4600 572#endif
3280af22 573 PL_pad_reset_pending = FALSE;
79072805
LW
574}
575
a863c7d1 576#ifdef USE_THREADS
54b9620d 577/* find_threadsv is not reentrant */
a863c7d1 578PADOFFSET
864dbfa3 579Perl_find_threadsv(pTHX_ const char *name)
a863c7d1
MB
580{
581 dTHR;
582 char *p;
583 PADOFFSET key;
554b3eca 584 SV **svp;
54b9620d 585 /* We currently only handle names of a single character */
533c011a 586 p = strchr(PL_threadsv_names, *name);
a863c7d1
MB
587 if (!p)
588 return NOT_IN_PAD;
533c011a 589 key = p - PL_threadsv_names;
2d8e6c8d 590 MUTEX_LOCK(&thr->mutex);
54b9620d 591 svp = av_fetch(thr->threadsv, key, FALSE);
2d8e6c8d
GS
592 if (svp)
593 MUTEX_UNLOCK(&thr->mutex);
594 else {
554b3eca 595 SV *sv = NEWSV(0, 0);
54b9620d 596 av_store(thr->threadsv, key, sv);
940cb80d 597 thr->threadsvp = AvARRAY(thr->threadsv);
2d8e6c8d 598 MUTEX_UNLOCK(&thr->mutex);
554b3eca
MB
599 /*
600 * Some magic variables used to be automagically initialised
601 * in gv_fetchpv. Those which are now per-thread magicals get
602 * initialised here instead.
603 */
604 switch (*name) {
54b9620d
MB
605 case '_':
606 break;
554b3eca
MB
607 case ';':
608 sv_setpv(sv, "\034");
54b9620d 609 sv_magic(sv, 0, 0, name, 1);
554b3eca 610 break;
c277df42
IZ
611 case '&':
612 case '`':
613 case '\'':
533c011a 614 PL_sawampersand = TRUE;
a3f914c5
GS
615 /* FALL THROUGH */
616 case '1':
617 case '2':
618 case '3':
619 case '4':
620 case '5':
621 case '6':
622 case '7':
623 case '8':
624 case '9':
c277df42 625 SvREADONLY_on(sv);
d8b5173a 626 /* FALL THROUGH */
067391ea
GS
627
628 /* XXX %! tied to Errno.pm needs to be added here.
629 * See gv_fetchpv(). */
630 /* case '!': */
631
54b9620d
MB
632 default:
633 sv_magic(sv, 0, 0, name, 1);
554b3eca 634 }
8b73bbec 635 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
54b9620d 636 "find_threadsv: new SV %p for $%s%c\n",
554b3eca
MB
637 sv, (*name < 32) ? "^" : "",
638 (*name < 32) ? toCTRL(*name) : *name));
a863c7d1
MB
639 }
640 return key;
641}
642#endif /* USE_THREADS */
643
79072805
LW
644/* Destructor */
645
646void
864dbfa3 647Perl_op_free(pTHX_ OP *o)
79072805 648{
85e6fe83 649 register OP *kid, *nextkid;
79072805 650
5dc0d613 651 if (!o || o->op_seq == (U16)-1)
79072805
LW
652 return;
653
11343788
MB
654 if (o->op_flags & OPf_KIDS) {
655 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
85e6fe83 656 nextkid = kid->op_sibling; /* Get before next freeing kid */
79072805 657 op_free(kid);
85e6fe83 658 }
79072805
LW
659 }
660
11343788 661 switch (o->op_type) {
8990e307 662 case OP_NULL:
11343788 663 o->op_targ = 0; /* Was holding old type, if any. */
8990e307 664 break;
a0d0e21e 665 case OP_ENTEREVAL:
11343788 666 o->op_targ = 0; /* Was holding hints. */
a0d0e21e 667 break;
554b3eca 668#ifdef USE_THREADS
8dd3ba40
SM
669 case OP_ENTERITER:
670 if (!(o->op_flags & OPf_SPECIAL))
671 break;
672 /* FALL THROUGH */
2faa37cc 673 case OP_THREADSV:
54b9620d 674 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
554b3eca
MB
675 break;
676#endif /* USE_THREADS */
a6006777 677 default:
ac4c12e7 678 if (!(o->op_flags & OPf_REF)
22c35a8c 679 || (PL_check[o->op_type] != FUNC_NAME_TO_PTR(ck_ftst)))
a6006777 680 break;
681 /* FALL THROUGH */
463ee0b2 682 case OP_GVSV:
79072805 683 case OP_GV:
a6006777 684 case OP_AELEMFAST:
11343788 685 SvREFCNT_dec(cGVOPo->op_gv);
8990e307
LW
686 break;
687 case OP_NEXTSTATE:
688 case OP_DBSTATE:
5196be3e 689 Safefree(cCOPo->cop_label);
11343788 690 SvREFCNT_dec(cCOPo->cop_filegv);
599cee73
PM
691 if (cCOPo->cop_warnings != WARN_NONE && cCOPo->cop_warnings != WARN_ALL)
692 SvREFCNT_dec(cCOPo->cop_warnings);
79072805
LW
693 break;
694 case OP_CONST:
11343788 695 SvREFCNT_dec(cSVOPo->op_sv);
79072805 696 break;
748a9306
LW
697 case OP_GOTO:
698 case OP_NEXT:
699 case OP_LAST:
700 case OP_REDO:
11343788 701 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
748a9306
LW
702 break;
703 /* FALL THROUGH */
a0d0e21e 704 case OP_TRANS:
a0ed51b3
LW
705 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF))
706 SvREFCNT_dec(cSVOPo->op_sv);
707 else
708 Safefree(cPVOPo->op_pv);
a0d0e21e
LW
709 break;
710 case OP_SUBST:
11343788 711 op_free(cPMOPo->op_pmreplroot);
a0d0e21e 712 /* FALL THROUGH */
748a9306 713 case OP_PUSHRE:
a0d0e21e 714 case OP_MATCH:
8782bef2 715 case OP_QR:
c277df42 716 ReREFCNT_dec(cPMOPo->op_pmregexp);
a0d0e21e 717 break;
79072805
LW
718 }
719
11343788
MB
720 if (o->op_targ > 0)
721 pad_free(o->op_targ);
8990e307 722
b7dc083c
NIS
723#ifdef PL_OP_SLAB_ALLOC
724 if ((char *) o == PL_OpPtr)
725 {
726 }
727#else
11343788 728 Safefree(o);
b7dc083c 729#endif
79072805
LW
730}
731
76e3520e 732STATIC void
864dbfa3 733null(pTHX_ OP *o)
8990e307 734{
54b9620d 735 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
11343788
MB
736 pad_free(o->op_targ);
737 o->op_targ = o->op_type;
738 o->op_type = OP_NULL;
22c35a8c 739 o->op_ppaddr = PL_ppaddr[OP_NULL];
8990e307
LW
740}
741
79072805
LW
742/* Contextualizers */
743
463ee0b2 744#define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
79072805
LW
745
746OP *
864dbfa3 747Perl_linklist(pTHX_ OP *o)
79072805
LW
748{
749 register OP *kid;
750
11343788
MB
751 if (o->op_next)
752 return o->op_next;
79072805
LW
753
754 /* establish postfix order */
11343788
MB
755 if (cUNOPo->op_first) {
756 o->op_next = LINKLIST(cUNOPo->op_first);
757 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
758 if (kid->op_sibling)
759 kid->op_next = LINKLIST(kid->op_sibling);
760 else
11343788 761 kid->op_next = o;
79072805
LW
762 }
763 }
764 else
11343788 765 o->op_next = o;
79072805 766
11343788 767 return o->op_next;
79072805
LW
768}
769
770OP *
864dbfa3 771Perl_scalarkids(pTHX_ OP *o)
79072805
LW
772{
773 OP *kid;
11343788
MB
774 if (o && o->op_flags & OPf_KIDS) {
775 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
776 scalar(kid);
777 }
11343788 778 return o;
79072805
LW
779}
780
76e3520e 781STATIC OP *
864dbfa3 782scalarboolean(pTHX_ OP *o)
8990e307 783{
d008e5eb 784 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
0f15f207 785 dTHR;
d008e5eb
GS
786 if (ckWARN(WARN_SYNTAX)) {
787 line_t oldline = PL_curcop->cop_line;
a0d0e21e 788
d008e5eb
GS
789 if (PL_copline != NOLINE)
790 PL_curcop->cop_line = PL_copline;
791 warner(WARN_SYNTAX, "Found = in conditional, should be ==");
792 PL_curcop->cop_line = oldline;
793 }
a0d0e21e 794 }
11343788 795 return scalar(o);
8990e307
LW
796}
797
798OP *
864dbfa3 799Perl_scalar(pTHX_ OP *o)
79072805
LW
800{
801 OP *kid;
802
a0d0e21e 803 /* assumes no premature commitment */
3280af22 804 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 805 || o->op_type == OP_RETURN)
11343788 806 return o;
79072805 807
5dc0d613 808 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
79072805 809
11343788 810 switch (o->op_type) {
79072805 811 case OP_REPEAT:
11343788
MB
812 if (o->op_private & OPpREPEAT_DOLIST)
813 null(((LISTOP*)cBINOPo->op_first)->op_first);
814 scalar(cBINOPo->op_first);
8990e307 815 break;
79072805
LW
816 case OP_OR:
817 case OP_AND:
818 case OP_COND_EXPR:
11343788 819 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
8990e307 820 scalar(kid);
79072805 821 break;
a0d0e21e 822 case OP_SPLIT:
11343788 823 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
824 if (!kPMOP->op_pmreplroot)
825 deprecate("implicit split to @_");
826 }
827 /* FALL THROUGH */
79072805 828 case OP_MATCH:
8782bef2 829 case OP_QR:
79072805
LW
830 case OP_SUBST:
831 case OP_NULL:
8990e307 832 default:
11343788
MB
833 if (o->op_flags & OPf_KIDS) {
834 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
8990e307
LW
835 scalar(kid);
836 }
79072805
LW
837 break;
838 case OP_LEAVE:
839 case OP_LEAVETRY:
5dc0d613 840 kid = cLISTOPo->op_first;
54310121 841 scalar(kid);
842 while (kid = kid->op_sibling) {
843 if (kid->op_sibling)
844 scalarvoid(kid);
845 else
846 scalar(kid);
847 }
3280af22 848 WITH_THR(PL_curcop = &PL_compiling);
54310121 849 break;
748a9306 850 case OP_SCOPE:
79072805 851 case OP_LINESEQ:
8990e307 852 case OP_LIST:
11343788 853 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
854 if (kid->op_sibling)
855 scalarvoid(kid);
856 else
857 scalar(kid);
858 }
3280af22 859 WITH_THR(PL_curcop = &PL_compiling);
79072805
LW
860 break;
861 }
11343788 862 return o;
79072805
LW
863}
864
865OP *
864dbfa3 866Perl_scalarvoid(pTHX_ OP *o)
79072805
LW
867{
868 OP *kid;
8990e307
LW
869 char* useless = 0;
870 SV* sv;
2ebea0a1
GS
871 U8 want;
872
873 if (o->op_type == OP_NEXTSTATE || o->op_type == OP_DBSTATE ||
874 (o->op_type == OP_NULL &&
875 (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)))
876 {
877 dTHR;
878 PL_curcop = (COP*)o; /* for warning below */
879 }
79072805 880
54310121 881 /* assumes no premature commitment */
2ebea0a1
GS
882 want = o->op_flags & OPf_WANT;
883 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
5dc0d613 884 || o->op_type == OP_RETURN)
11343788 885 return o;
79072805 886
5dc0d613 887 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
79072805 888
11343788 889 switch (o->op_type) {
79072805 890 default:
22c35a8c 891 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
8990e307 892 break;
36477c24 893 /* FALL THROUGH */
894 case OP_REPEAT:
11343788 895 if (o->op_flags & OPf_STACKED)
8990e307 896 break;
5d82c453
GA
897 goto func_ops;
898 case OP_SUBSTR:
899 if (o->op_private == 4)
900 break;
8990e307
LW
901 /* FALL THROUGH */
902 case OP_GVSV:
903 case OP_WANTARRAY:
904 case OP_GV:
905 case OP_PADSV:
906 case OP_PADAV:
907 case OP_PADHV:
908 case OP_PADANY:
909 case OP_AV2ARYLEN:
8990e307 910 case OP_REF:
a0d0e21e
LW
911 case OP_REFGEN:
912 case OP_SREFGEN:
8990e307
LW
913 case OP_DEFINED:
914 case OP_HEX:
915 case OP_OCT:
916 case OP_LENGTH:
8990e307
LW
917 case OP_VEC:
918 case OP_INDEX:
919 case OP_RINDEX:
920 case OP_SPRINTF:
921 case OP_AELEM:
922 case OP_AELEMFAST:
923 case OP_ASLICE:
8990e307
LW
924 case OP_HELEM:
925 case OP_HSLICE:
926 case OP_UNPACK:
927 case OP_PACK:
8990e307
LW
928 case OP_JOIN:
929 case OP_LSLICE:
930 case OP_ANONLIST:
931 case OP_ANONHASH:
932 case OP_SORT:
933 case OP_REVERSE:
934 case OP_RANGE:
935 case OP_FLIP:
936 case OP_FLOP:
937 case OP_CALLER:
938 case OP_FILENO:
939 case OP_EOF:
940 case OP_TELL:
941 case OP_GETSOCKNAME:
942 case OP_GETPEERNAME:
943 case OP_READLINK:
944 case OP_TELLDIR:
945 case OP_GETPPID:
946 case OP_GETPGRP:
947 case OP_GETPRIORITY:
948 case OP_TIME:
949 case OP_TMS:
950 case OP_LOCALTIME:
951 case OP_GMTIME:
952 case OP_GHBYNAME:
953 case OP_GHBYADDR:
954 case OP_GHOSTENT:
955 case OP_GNBYNAME:
956 case OP_GNBYADDR:
957 case OP_GNETENT:
958 case OP_GPBYNAME:
959 case OP_GPBYNUMBER:
960 case OP_GPROTOENT:
961 case OP_GSBYNAME:
962 case OP_GSBYPORT:
963 case OP_GSERVENT:
964 case OP_GPWNAM:
965 case OP_GPWUID:
966 case OP_GGRNAM:
967 case OP_GGRGID:
968 case OP_GETLOGIN:
5d82c453 969 func_ops:
11343788 970 if (!(o->op_private & OPpLVAL_INTRO))
22c35a8c 971 useless = PL_op_desc[o->op_type];
8990e307
LW
972 break;
973
974 case OP_RV2GV:
975 case OP_RV2SV:
976 case OP_RV2AV:
977 case OP_RV2HV:
11343788
MB
978 if (!(o->op_private & OPpLVAL_INTRO) &&
979 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
8990e307
LW
980 useless = "a variable";
981 break;
79072805
LW
982
983 case OP_CONST:
11343788 984 sv = cSVOPo->op_sv;
7a52d87a
GS
985 if (cSVOPo->op_private & OPpCONST_STRICT)
986 no_bareword_allowed(o);
987 else {
d008e5eb
GS
988 dTHR;
989 if (ckWARN(WARN_VOID)) {
990 useless = "a constant";
991 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
992 useless = 0;
993 else if (SvPOK(sv)) {
994 if (strnEQ(SvPVX(sv), "di", 2) ||
995 strnEQ(SvPVX(sv), "ds", 2) ||
996 strnEQ(SvPVX(sv), "ig", 2))
997 useless = 0;
998 }
8990e307
LW
999 }
1000 }
11343788 1001 null(o); /* don't execute a constant */
8990e307 1002 SvREFCNT_dec(sv); /* don't even remember it */
79072805
LW
1003 break;
1004
1005 case OP_POSTINC:
11343788 1006 o->op_type = OP_PREINC; /* pre-increment is faster */
22c35a8c 1007 o->op_ppaddr = PL_ppaddr[OP_PREINC];
79072805
LW
1008 break;
1009
1010 case OP_POSTDEC:
11343788 1011 o->op_type = OP_PREDEC; /* pre-decrement is faster */
22c35a8c 1012 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
79072805
LW
1013 break;
1014
79072805
LW
1015 case OP_OR:
1016 case OP_AND:
1017 case OP_COND_EXPR:
11343788 1018 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1019 scalarvoid(kid);
1020 break;
5aabfad6 1021
a0d0e21e 1022 case OP_NULL:
11343788 1023 if (o->op_flags & OPf_STACKED)
a0d0e21e 1024 break;
5aabfad6 1025 /* FALL THROUGH */
2ebea0a1
GS
1026 case OP_NEXTSTATE:
1027 case OP_DBSTATE:
79072805
LW
1028 case OP_ENTERTRY:
1029 case OP_ENTER:
1030 case OP_SCALAR:
11343788 1031 if (!(o->op_flags & OPf_KIDS))
79072805 1032 break;
54310121 1033 /* FALL THROUGH */
463ee0b2 1034 case OP_SCOPE:
79072805
LW
1035 case OP_LEAVE:
1036 case OP_LEAVETRY:
a0d0e21e 1037 case OP_LEAVELOOP:
79072805 1038 case OP_LINESEQ:
79072805 1039 case OP_LIST:
11343788 1040 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1041 scalarvoid(kid);
1042 break;
c90c0ff4 1043 case OP_ENTEREVAL:
5196be3e 1044 scalarkids(o);
c90c0ff4 1045 break;
5aabfad6 1046 case OP_REQUIRE:
c90c0ff4 1047 /* all requires must return a boolean value */
5196be3e
MB
1048 o->op_flags &= ~OPf_WANT;
1049 return scalar(o);
a0d0e21e 1050 case OP_SPLIT:
11343788 1051 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
1052 if (!kPMOP->op_pmreplroot)
1053 deprecate("implicit split to @_");
1054 }
1055 break;
79072805 1056 }
d008e5eb
GS
1057 if (useless) {
1058 dTHR;
1059 if (ckWARN(WARN_VOID))
1060 warner(WARN_VOID, "Useless use of %s in void context", useless);
1061 }
11343788 1062 return o;
79072805
LW
1063}
1064
1065OP *
864dbfa3 1066Perl_listkids(pTHX_ OP *o)
79072805
LW
1067{
1068 OP *kid;
11343788
MB
1069 if (o && o->op_flags & OPf_KIDS) {
1070 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1071 list(kid);
1072 }
11343788 1073 return o;
79072805
LW
1074}
1075
1076OP *
864dbfa3 1077Perl_list(pTHX_ OP *o)
79072805
LW
1078{
1079 OP *kid;
1080
a0d0e21e 1081 /* assumes no premature commitment */
3280af22 1082 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 1083 || o->op_type == OP_RETURN)
11343788 1084 return o;
79072805 1085
5dc0d613 1086 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
79072805 1087
11343788 1088 switch (o->op_type) {
79072805
LW
1089 case OP_FLOP:
1090 case OP_REPEAT:
11343788 1091 list(cBINOPo->op_first);
79072805
LW
1092 break;
1093 case OP_OR:
1094 case OP_AND:
1095 case OP_COND_EXPR:
11343788 1096 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1097 list(kid);
1098 break;
1099 default:
1100 case OP_MATCH:
8782bef2 1101 case OP_QR:
79072805
LW
1102 case OP_SUBST:
1103 case OP_NULL:
11343788 1104 if (!(o->op_flags & OPf_KIDS))
79072805 1105 break;
11343788
MB
1106 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1107 list(cBINOPo->op_first);
1108 return gen_constant_list(o);
79072805
LW
1109 }
1110 case OP_LIST:
11343788 1111 listkids(o);
79072805
LW
1112 break;
1113 case OP_LEAVE:
1114 case OP_LEAVETRY:
5dc0d613 1115 kid = cLISTOPo->op_first;
54310121 1116 list(kid);
1117 while (kid = kid->op_sibling) {
1118 if (kid->op_sibling)
1119 scalarvoid(kid);
1120 else
1121 list(kid);
1122 }
3280af22 1123 WITH_THR(PL_curcop = &PL_compiling);
54310121 1124 break;
748a9306 1125 case OP_SCOPE:
79072805 1126 case OP_LINESEQ:
11343788 1127 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
1128 if (kid->op_sibling)
1129 scalarvoid(kid);
1130 else
1131 list(kid);
1132 }
3280af22 1133 WITH_THR(PL_curcop = &PL_compiling);
79072805 1134 break;
c90c0ff4 1135 case OP_REQUIRE:
1136 /* all requires must return a boolean value */
5196be3e
MB
1137 o->op_flags &= ~OPf_WANT;
1138 return scalar(o);
79072805 1139 }
11343788 1140 return o;
79072805
LW
1141}
1142
1143OP *
864dbfa3 1144Perl_scalarseq(pTHX_ OP *o)
79072805
LW
1145{
1146 OP *kid;
1147
11343788
MB
1148 if (o) {
1149 if (o->op_type == OP_LINESEQ ||
1150 o->op_type == OP_SCOPE ||
1151 o->op_type == OP_LEAVE ||
1152 o->op_type == OP_LEAVETRY)
463ee0b2 1153 {
0f15f207 1154 dTHR;
11343788 1155 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
ed6116ce 1156 if (kid->op_sibling) {
463ee0b2 1157 scalarvoid(kid);
ed6116ce 1158 }
463ee0b2 1159 }
3280af22 1160 PL_curcop = &PL_compiling;
79072805 1161 }
11343788 1162 o->op_flags &= ~OPf_PARENS;
3280af22 1163 if (PL_hints & HINT_BLOCK_SCOPE)
11343788 1164 o->op_flags |= OPf_PARENS;
79072805 1165 }
8990e307 1166 else
11343788
MB
1167 o = newOP(OP_STUB, 0);
1168 return o;
79072805
LW
1169}
1170
76e3520e 1171STATIC OP *
864dbfa3 1172modkids(pTHX_ OP *o, I32 type)
79072805
LW
1173{
1174 OP *kid;
11343788
MB
1175 if (o && o->op_flags & OPf_KIDS) {
1176 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2 1177 mod(kid, type);
79072805 1178 }
11343788 1179 return o;
79072805
LW
1180}
1181
79072805 1182OP *
864dbfa3 1183Perl_mod(pTHX_ OP *o, I32 type)
79072805 1184{
11343788 1185 dTHR;
79072805
LW
1186 OP *kid;
1187 SV *sv;
2d8e6c8d 1188 STRLEN n_a;
79072805 1189
3280af22 1190 if (!o || PL_error_count)
11343788 1191 return o;
79072805 1192
11343788 1193 switch (o->op_type) {
68dc0745 1194 case OP_UNDEF:
3280af22 1195 PL_modcount++;
5dc0d613 1196 return o;
a0d0e21e 1197 case OP_CONST:
11343788 1198 if (!(o->op_private & (OPpCONST_ARYBASE)))
a0d0e21e 1199 goto nomod;
3280af22
NIS
1200 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1201 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1202 PL_eval_start = 0;
a0d0e21e
LW
1203 }
1204 else if (!type) {
3280af22
NIS
1205 SAVEI32(PL_compiling.cop_arybase);
1206 PL_compiling.cop_arybase = 0;
a0d0e21e
LW
1207 }
1208 else if (type == OP_REFGEN)
1209 goto nomod;
1210 else
1211 croak("That use of $[ is unsupported");
1212 break;
5f05dabc 1213 case OP_STUB:
5196be3e 1214 if (o->op_flags & OPf_PARENS)
5f05dabc 1215 break;
1216 goto nomod;
a0d0e21e
LW
1217 case OP_ENTERSUB:
1218 if ((type == OP_UNDEF || type == OP_REFGEN) &&
11343788
MB
1219 !(o->op_flags & OPf_STACKED)) {
1220 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1221 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1222 assert(cUNOPo->op_first->op_type == OP_NULL);
1223 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
79072805
LW
1224 break;
1225 }
1226 /* FALL THROUGH */
1227 default:
a0d0e21e
LW
1228 nomod:
1229 /* grep, foreach, subcalls, refgen */
1230 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1231 break;
46fc3d4c 1232 yyerror(form("Can't modify %s in %s",
638bc118 1233 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
22c35a8c
GS
1234 ? "do block" : PL_op_desc[o->op_type]),
1235 type ? PL_op_desc[type] : "local"));
11343788 1236 return o;
79072805 1237
a0d0e21e
LW
1238 case OP_PREINC:
1239 case OP_PREDEC:
1240 case OP_POW:
1241 case OP_MULTIPLY:
1242 case OP_DIVIDE:
1243 case OP_MODULO:
1244 case OP_REPEAT:
1245 case OP_ADD:
1246 case OP_SUBTRACT:
1247 case OP_CONCAT:
1248 case OP_LEFT_SHIFT:
1249 case OP_RIGHT_SHIFT:
1250 case OP_BIT_AND:
1251 case OP_BIT_XOR:
1252 case OP_BIT_OR:
1253 case OP_I_MULTIPLY:
1254 case OP_I_DIVIDE:
1255 case OP_I_MODULO:
1256 case OP_I_ADD:
1257 case OP_I_SUBTRACT:
11343788 1258 if (!(o->op_flags & OPf_STACKED))
a0d0e21e 1259 goto nomod;
3280af22 1260 PL_modcount++;
a0d0e21e
LW
1261 break;
1262
79072805 1263 case OP_COND_EXPR:
11343788 1264 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2 1265 mod(kid, type);
79072805
LW
1266 break;
1267
1268 case OP_RV2AV:
1269 case OP_RV2HV:
93af7a87 1270 if (!type && cUNOPo->op_first->op_type != OP_GV)
706a304b 1271 croak("Can't localize through a reference");
11343788 1272 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3280af22 1273 PL_modcount = 10000;
11343788 1274 return o; /* Treat \(@foo) like ordinary list. */
748a9306
LW
1275 }
1276 /* FALL THROUGH */
79072805 1277 case OP_RV2GV:
5dc0d613 1278 if (scalar_mod_type(o, type))
3fe9a6f1 1279 goto nomod;
11343788 1280 ref(cUNOPo->op_first, o->op_type);
79072805
LW
1281 /* FALL THROUGH */
1282 case OP_AASSIGN:
1283 case OP_ASLICE:
1284 case OP_HSLICE:
93a17b20
LW
1285 case OP_NEXTSTATE:
1286 case OP_DBSTATE:
a0d0e21e
LW
1287 case OP_REFGEN:
1288 case OP_CHOMP:
3280af22 1289 PL_modcount = 10000;
79072805 1290 break;
463ee0b2 1291 case OP_RV2SV:
11343788 1292 if (!type && cUNOPo->op_first->op_type != OP_GV)
706a304b 1293 croak("Can't localize through a reference");
aeea060c 1294 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1295 /* FALL THROUGH */
79072805 1296 case OP_GV:
463ee0b2 1297 case OP_AV2ARYLEN:
3280af22 1298 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1299 case OP_SASSIGN:
bf4b1e52
GS
1300 case OP_ANDASSIGN:
1301 case OP_ORASSIGN:
8990e307 1302 case OP_AELEMFAST:
3280af22 1303 PL_modcount++;
8990e307
LW
1304 break;
1305
748a9306
LW
1306 case OP_PADAV:
1307 case OP_PADHV:
3280af22 1308 PL_modcount = 10000;
5196be3e
MB
1309 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1310 return o; /* Treat \(@foo) like ordinary list. */
1311 if (scalar_mod_type(o, type))
3fe9a6f1 1312 goto nomod;
748a9306
LW
1313 /* FALL THROUGH */
1314 case OP_PADSV:
3280af22 1315 PL_modcount++;
748a9306
LW
1316 if (!type)
1317 croak("Can't localize lexical variable %s",
2d8e6c8d 1318 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
463ee0b2
LW
1319 break;
1320
554b3eca 1321#ifdef USE_THREADS
2faa37cc 1322 case OP_THREADSV:
533c011a 1323 PL_modcount++; /* XXX ??? */
554b3eca
MB
1324 break;
1325#endif /* USE_THREADS */
1326
748a9306
LW
1327 case OP_PUSHMARK:
1328 break;
a0d0e21e 1329
69969c6f
SB
1330 case OP_KEYS:
1331 if (type != OP_SASSIGN)
1332 goto nomod;
5d82c453
GA
1333 goto lvalue_func;
1334 case OP_SUBSTR:
1335 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1336 goto nomod;
5f05dabc 1337 /* FALL THROUGH */
a0d0e21e 1338 case OP_POS:
463ee0b2 1339 case OP_VEC:
5d82c453 1340 lvalue_func:
11343788
MB
1341 pad_free(o->op_targ);
1342 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
5dc0d613 1343 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
11343788
MB
1344 if (o->op_flags & OPf_KIDS)
1345 mod(cBINOPo->op_first->op_sibling, type);
463ee0b2 1346 break;
a0d0e21e 1347
463ee0b2
LW
1348 case OP_AELEM:
1349 case OP_HELEM:
11343788 1350 ref(cBINOPo->op_first, o->op_type);
68dc0745 1351 if (type == OP_ENTERSUB &&
5dc0d613
MB
1352 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1353 o->op_private |= OPpLVAL_DEFER;
3280af22 1354 PL_modcount++;
463ee0b2
LW
1355 break;
1356
1357 case OP_SCOPE:
1358 case OP_LEAVE:
1359 case OP_ENTER:
11343788
MB
1360 if (o->op_flags & OPf_KIDS)
1361 mod(cLISTOPo->op_last, type);
a0d0e21e
LW
1362 break;
1363
1364 case OP_NULL:
638bc118
GS
1365 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1366 goto nomod;
1367 else if (!(o->op_flags & OPf_KIDS))
463ee0b2 1368 break;
11343788
MB
1369 if (o->op_targ != OP_LIST) {
1370 mod(cBINOPo->op_first, type);
a0d0e21e
LW
1371 break;
1372 }
1373 /* FALL THROUGH */
463ee0b2 1374 case OP_LIST:
11343788 1375 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1376 mod(kid, type);
1377 break;
1378 }
11343788 1379 o->op_flags |= OPf_MOD;
a0d0e21e
LW
1380
1381 if (type == OP_AASSIGN || type == OP_SASSIGN)
11343788 1382 o->op_flags |= OPf_SPECIAL|OPf_REF;
a0d0e21e 1383 else if (!type) {
11343788
MB
1384 o->op_private |= OPpLVAL_INTRO;
1385 o->op_flags &= ~OPf_SPECIAL;
3280af22 1386 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1387 }
a0d0e21e 1388 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
11343788
MB
1389 o->op_flags |= OPf_REF;
1390 return o;
463ee0b2
LW
1391}
1392
864dbfa3
GS
1393STATIC bool
1394scalar_mod_type(pTHX_ OP *o, I32 type)
3fe9a6f1 1395{
1396 switch (type) {
1397 case OP_SASSIGN:
5196be3e 1398 if (o->op_type == OP_RV2GV)
3fe9a6f1 1399 return FALSE;
1400 /* FALL THROUGH */
1401 case OP_PREINC:
1402 case OP_PREDEC:
1403 case OP_POSTINC:
1404 case OP_POSTDEC:
1405 case OP_I_PREINC:
1406 case OP_I_PREDEC:
1407 case OP_I_POSTINC:
1408 case OP_I_POSTDEC:
1409 case OP_POW:
1410 case OP_MULTIPLY:
1411 case OP_DIVIDE:
1412 case OP_MODULO:
1413 case OP_REPEAT:
1414 case OP_ADD:
1415 case OP_SUBTRACT:
1416 case OP_I_MULTIPLY:
1417 case OP_I_DIVIDE:
1418 case OP_I_MODULO:
1419 case OP_I_ADD:
1420 case OP_I_SUBTRACT:
1421 case OP_LEFT_SHIFT:
1422 case OP_RIGHT_SHIFT:
1423 case OP_BIT_AND:
1424 case OP_BIT_XOR:
1425 case OP_BIT_OR:
1426 case OP_CONCAT:
1427 case OP_SUBST:
1428 case OP_TRANS:
49e9fbe6
GS
1429 case OP_READ:
1430 case OP_SYSREAD:
1431 case OP_RECV:
bf4b1e52
GS
1432 case OP_ANDASSIGN:
1433 case OP_ORASSIGN:
3fe9a6f1 1434 return TRUE;
1435 default:
1436 return FALSE;
1437 }
1438}
1439
35cd451c 1440STATIC bool
864dbfa3 1441is_handle_constructor(pTHX_ OP *o, I32 argnum)
35cd451c
GS
1442{
1443 switch (o->op_type) {
1444 case OP_PIPE_OP:
1445 case OP_SOCKPAIR:
1446 if (argnum == 2)
1447 return TRUE;
1448 /* FALL THROUGH */
1449 case OP_SYSOPEN:
1450 case OP_OPEN:
ded8aa31 1451 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
35cd451c
GS
1452 case OP_SOCKET:
1453 case OP_OPEN_DIR:
1454 case OP_ACCEPT:
1455 if (argnum == 1)
1456 return TRUE;
1457 /* FALL THROUGH */
1458 default:
1459 return FALSE;
1460 }
1461}
1462
463ee0b2 1463OP *
864dbfa3 1464Perl_refkids(pTHX_ OP *o, I32 type)
463ee0b2
LW
1465{
1466 OP *kid;
11343788
MB
1467 if (o && o->op_flags & OPf_KIDS) {
1468 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1469 ref(kid, type);
1470 }
11343788 1471 return o;
463ee0b2
LW
1472}
1473
1474OP *
864dbfa3 1475Perl_ref(pTHX_ OP *o, I32 type)
463ee0b2
LW
1476{
1477 OP *kid;
463ee0b2 1478
3280af22 1479 if (!o || PL_error_count)
11343788 1480 return o;
463ee0b2 1481
11343788 1482 switch (o->op_type) {
a0d0e21e 1483 case OP_ENTERSUB:
e55aaa0e 1484 if ((type == OP_DEFINED || type == OP_LOCK) &&
11343788
MB
1485 !(o->op_flags & OPf_STACKED)) {
1486 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1487 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1488 assert(cUNOPo->op_first->op_type == OP_NULL);
1489 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1490 o->op_flags |= OPf_SPECIAL;
8990e307
LW
1491 }
1492 break;
aeea060c 1493
463ee0b2 1494 case OP_COND_EXPR:
11343788 1495 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2
LW
1496 ref(kid, type);
1497 break;
8990e307 1498 case OP_RV2SV:
35cd451c
GS
1499 if (type == OP_DEFINED)
1500 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1501 ref(cUNOPo->op_first, o->op_type);
4633a7c4
LW
1502 /* FALL THROUGH */
1503 case OP_PADSV:
5f05dabc 1504 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1505 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1506 : type == OP_RV2HV ? OPpDEREF_HV
1507 : OPpDEREF_SV);
11343788 1508 o->op_flags |= OPf_MOD;
a0d0e21e 1509 }
8990e307
LW
1510 break;
1511
2faa37cc 1512 case OP_THREADSV:
a863c7d1
MB
1513 o->op_flags |= OPf_MOD; /* XXX ??? */
1514 break;
1515
463ee0b2
LW
1516 case OP_RV2AV:
1517 case OP_RV2HV:
aeea060c 1518 o->op_flags |= OPf_REF;
8990e307 1519 /* FALL THROUGH */
463ee0b2 1520 case OP_RV2GV:
35cd451c
GS
1521 if (type == OP_DEFINED)
1522 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1523 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1524 break;
8990e307 1525
463ee0b2
LW
1526 case OP_PADAV:
1527 case OP_PADHV:
aeea060c 1528 o->op_flags |= OPf_REF;
79072805 1529 break;
aeea060c 1530
8990e307 1531 case OP_SCALAR:
79072805 1532 case OP_NULL:
11343788 1533 if (!(o->op_flags & OPf_KIDS))
463ee0b2 1534 break;
11343788 1535 ref(cBINOPo->op_first, type);
79072805
LW
1536 break;
1537 case OP_AELEM:
1538 case OP_HELEM:
11343788 1539 ref(cBINOPo->op_first, o->op_type);
5f05dabc 1540 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1541 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1542 : type == OP_RV2HV ? OPpDEREF_HV
1543 : OPpDEREF_SV);
11343788 1544 o->op_flags |= OPf_MOD;
8990e307 1545 }
79072805
LW
1546 break;
1547
463ee0b2 1548 case OP_SCOPE:
79072805
LW
1549 case OP_LEAVE:
1550 case OP_ENTER:
8990e307 1551 case OP_LIST:
11343788 1552 if (!(o->op_flags & OPf_KIDS))
79072805 1553 break;
11343788 1554 ref(cLISTOPo->op_last, type);
79072805 1555 break;
a0d0e21e
LW
1556 default:
1557 break;
79072805 1558 }
11343788 1559 return scalar(o);
8990e307 1560
79072805
LW
1561}
1562
1563OP *
864dbfa3 1564Perl_my(pTHX_ OP *o)
93a17b20
LW
1565{
1566 OP *kid;
93a17b20
LW
1567 I32 type;
1568
3280af22 1569 if (!o || PL_error_count)
11343788 1570 return o;
93a17b20 1571
11343788 1572 type = o->op_type;
93a17b20 1573 if (type == OP_LIST) {
11343788 1574 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
93a17b20 1575 my(kid);
dab48698 1576 } else if (type == OP_UNDEF) {
7766148a 1577 return o;
dab48698 1578 } else if (type != OP_PADSV &&
93a17b20
LW
1579 type != OP_PADAV &&
1580 type != OP_PADHV &&
1581 type != OP_PUSHMARK)
1582 {
22c35a8c 1583 yyerror(form("Can't declare %s in my", PL_op_desc[o->op_type]));
11343788 1584 return o;
93a17b20 1585 }
11343788
MB
1586 o->op_flags |= OPf_MOD;
1587 o->op_private |= OPpLVAL_INTRO;
1588 return o;
93a17b20
LW
1589}
1590
1591OP *
864dbfa3 1592Perl_sawparens(pTHX_ OP *o)
79072805
LW
1593{
1594 if (o)
1595 o->op_flags |= OPf_PARENS;
1596 return o;
1597}
1598
1599OP *
864dbfa3 1600Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
79072805 1601{
d008e5eb 1602 dTHR;
11343788 1603 OP *o;
79072805 1604
599cee73
PM
1605 if (ckWARN(WARN_UNSAFE) &&
1606 (left->op_type == OP_RV2AV ||
1607 left->op_type == OP_RV2HV ||
1608 left->op_type == OP_PADAV ||
1609 left->op_type == OP_PADHV)) {
22c35a8c 1610 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
599cee73
PM
1611 right->op_type == OP_TRANS)
1612 ? right->op_type : OP_MATCH];
1613 char *sample = ((left->op_type == OP_RV2AV ||
1614 left->op_type == OP_PADAV)
1615 ? "@array" : "%hash");
1616 warner(WARN_UNSAFE,
1617 "Applying %s to %s will act on scalar(%s)",
1618 desc, sample, sample);
2ae324a7 1619 }
1620
79072805
LW
1621 if (right->op_type == OP_MATCH ||
1622 right->op_type == OP_SUBST ||
1623 right->op_type == OP_TRANS) {
1624 right->op_flags |= OPf_STACKED;
1625 if (right->op_type != OP_MATCH)
463ee0b2 1626 left = mod(left, right->op_type);
79072805 1627 if (right->op_type == OP_TRANS)
11343788 1628 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
79072805 1629 else
11343788 1630 o = prepend_elem(right->op_type, scalar(left), right);
79072805 1631 if (type == OP_NOT)
11343788
MB
1632 return newUNOP(OP_NOT, 0, scalar(o));
1633 return o;
79072805
LW
1634 }
1635 else
1636 return bind_match(type, left,
1637 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1638}
1639
1640OP *
864dbfa3 1641Perl_invert(pTHX_ OP *o)
79072805 1642{
11343788
MB
1643 if (!o)
1644 return o;
79072805 1645 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
11343788 1646 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
79072805
LW
1647}
1648
1649OP *
864dbfa3 1650Perl_scope(pTHX_ OP *o)
79072805
LW
1651{
1652 if (o) {
3280af22 1653 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
463ee0b2
LW
1654 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1655 o->op_type = OP_LEAVE;
22c35a8c 1656 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
463ee0b2
LW
1657 }
1658 else {
1659 if (o->op_type == OP_LINESEQ) {
1660 OP *kid;
1661 o->op_type = OP_SCOPE;
22c35a8c 1662 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
463ee0b2 1663 kid = ((LISTOP*)o)->op_first;
748a9306
LW
1664 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1665 SvREFCNT_dec(((COP*)kid)->cop_filegv);
8990e307 1666 null(kid);
748a9306 1667 }
463ee0b2
LW
1668 }
1669 else
748a9306 1670 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
463ee0b2 1671 }
79072805
LW
1672 }
1673 return o;
1674}
1675
b3ac6de7 1676void
864dbfa3 1677Perl_save_hints(pTHX)
b3ac6de7 1678{
3280af22
NIS
1679 SAVEI32(PL_hints);
1680 SAVESPTR(GvHV(PL_hintgv));
1681 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1682 SAVEFREESV(GvHV(PL_hintgv));
b3ac6de7
IZ
1683}
1684
a0d0e21e 1685int
864dbfa3 1686Perl_block_start(pTHX_ int full)
79072805 1687{
11343788 1688 dTHR;
3280af22 1689 int retval = PL_savestack_ix;
b3ac6de7 1690
3280af22 1691 SAVEI32(PL_comppad_name_floor);
55497cff 1692 if (full) {
3280af22
NIS
1693 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1694 PL_comppad_name_floor = PL_comppad_name_fill;
55497cff 1695 else
3280af22
NIS
1696 PL_comppad_name_floor = 0;
1697 }
1698 SAVEI32(PL_min_intro_pending);
1699 SAVEI32(PL_max_intro_pending);
1700 PL_min_intro_pending = 0;
1701 SAVEI32(PL_comppad_name_fill);
1702 SAVEI32(PL_padix_floor);
1703 PL_padix_floor = PL_padix;
1704 PL_pad_reset_pending = FALSE;
b3ac6de7 1705 SAVEHINTS();
3280af22 1706 PL_hints &= ~HINT_BLOCK_SCOPE;
e24b16f9 1707 SAVEPPTR(PL_compiling.cop_warnings);
599cee73
PM
1708 if (PL_compiling.cop_warnings != WARN_ALL &&
1709 PL_compiling.cop_warnings != WARN_NONE) {
1710 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1711 SAVEFREESV(PL_compiling.cop_warnings) ;
1712 }
1713
1714
a0d0e21e
LW
1715 return retval;
1716}
1717
1718OP*
864dbfa3 1719Perl_block_end(pTHX_ I32 floor, OP *seq)
a0d0e21e 1720{
11343788 1721 dTHR;
3280af22 1722 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
a0d0e21e 1723 OP* retval = scalarseq(seq);
a0d0e21e 1724 LEAVE_SCOPE(floor);
3280af22 1725 PL_pad_reset_pending = FALSE;
e24b16f9 1726 PL_compiling.op_private = PL_hints;
a0d0e21e 1727 if (needblockscope)
3280af22
NIS
1728 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1729 pad_leavemy(PL_comppad_name_fill);
1730 PL_cop_seqmax++;
a0d0e21e
LW
1731 return retval;
1732}
1733
76e3520e 1734STATIC OP *
864dbfa3 1735newDEFSVOP(pTHX)
54b9620d
MB
1736{
1737#ifdef USE_THREADS
1738 OP *o = newOP(OP_THREADSV, 0);
1739 o->op_targ = find_threadsv("_");
1740 return o;
1741#else
3280af22 1742 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
54b9620d
MB
1743#endif /* USE_THREADS */
1744}
1745
a0d0e21e 1746void
864dbfa3 1747Perl_newPROG(pTHX_ OP *o)
a0d0e21e 1748{
11343788 1749 dTHR;
3280af22 1750 if (PL_in_eval) {
b295d113
TH
1751 if (PL_eval_root)
1752 return;
faef0170
HS
1753 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1754 ((PL_in_eval & EVAL_KEEPERR)
1755 ? OPf_SPECIAL : 0), o);
3280af22
NIS
1756 PL_eval_start = linklist(PL_eval_root);
1757 PL_eval_root->op_next = 0;
1758 peep(PL_eval_start);
a0d0e21e
LW
1759 }
1760 else {
5dc0d613 1761 if (!o)
a0d0e21e 1762 return;
3280af22
NIS
1763 PL_main_root = scope(sawparens(scalarvoid(o)));
1764 PL_curcop = &PL_compiling;
1765 PL_main_start = LINKLIST(PL_main_root);
1766 PL_main_root->op_next = 0;
1767 peep(PL_main_start);
1768 PL_compcv = 0;
3841441e 1769
4fdae800 1770 /* Register with debugger */
84902520 1771 if (PERLDB_INTER) {
864dbfa3 1772 CV *cv = get_cv("DB::postponed", FALSE);
3841441e
CS
1773 if (cv) {
1774 dSP;
924508f0 1775 PUSHMARK(SP);
3280af22 1776 XPUSHs((SV*)PL_compiling.cop_filegv);
3841441e 1777 PUTBACK;
864dbfa3 1778 call_sv((SV*)cv, G_DISCARD);
3841441e
CS
1779 }
1780 }
79072805 1781 }
79072805
LW
1782}
1783
1784OP *
864dbfa3 1785Perl_localize(pTHX_ OP *o, I32 lex)
79072805
LW
1786{
1787 if (o->op_flags & OPf_PARENS)
1788 list(o);
8990e307 1789 else {
d008e5eb 1790 dTHR;
599cee73 1791 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
8990e307 1792 char *s;
834a4ddd 1793 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
a0d0e21e 1794 if (*s == ';' || *s == '=')
752ebe84 1795 warner(WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
599cee73 1796 lex ? "my" : "local");
8990e307
LW
1797 }
1798 }
3280af22
NIS
1799 PL_in_my = FALSE;
1800 PL_in_my_stash = Nullhv;
93a17b20
LW
1801 if (lex)
1802 return my(o);
1803 else
463ee0b2 1804 return mod(o, OP_NULL); /* a bit kludgey */
79072805
LW
1805}
1806
1807OP *
864dbfa3 1808Perl_jmaybe(pTHX_ OP *o)
79072805
LW
1809{
1810 if (o->op_type == OP_LIST) {
554b3eca
MB
1811 OP *o2;
1812#ifdef USE_THREADS
2faa37cc 1813 o2 = newOP(OP_THREADSV, 0);
54b9620d 1814 o2->op_targ = find_threadsv(";");
554b3eca
MB
1815#else
1816 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1817#endif /* USE_THREADS */
1818 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
79072805
LW
1819 }
1820 return o;
1821}
1822
1823OP *
864dbfa3 1824Perl_fold_constants(pTHX_ register OP *o)
79072805 1825{
11343788 1826 dTHR;
79072805
LW
1827 register OP *curop;
1828 I32 type = o->op_type;
748a9306 1829 SV *sv;
79072805 1830
22c35a8c 1831 if (PL_opargs[type] & OA_RETSCALAR)
79072805 1832 scalar(o);
22c35a8c 1833 if (PL_opargs[type] & OA_TARGET)
ed6116ce 1834 o->op_targ = pad_alloc(type, SVs_PADTMP);
79072805 1835
22c35a8c
GS
1836 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
1837 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
85e6fe83 1838
22c35a8c 1839 if (!(PL_opargs[type] & OA_FOLDCONST))
79072805
LW
1840 goto nope;
1841
de939608 1842 switch (type) {
7a52d87a
GS
1843 case OP_NEGATE:
1844 /* XXX might want a ck_negate() for this */
1845 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
1846 break;
de939608
CS
1847 case OP_SPRINTF:
1848 case OP_UCFIRST:
1849 case OP_LCFIRST:
1850 case OP_UC:
1851 case OP_LC:
69dcf70c
MB
1852 case OP_SLT:
1853 case OP_SGT:
1854 case OP_SLE:
1855 case OP_SGE:
1856 case OP_SCMP:
1857
de939608
CS
1858 if (o->op_private & OPpLOCALE)
1859 goto nope;
1860 }
1861
3280af22 1862 if (PL_error_count)
a0d0e21e
LW
1863 goto nope; /* Don't try to run w/ errors */
1864
79072805 1865 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
93a17b20 1866 if (curop->op_type != OP_CONST &&
7a52d87a
GS
1867 curop->op_type != OP_LIST &&
1868 curop->op_type != OP_SCALAR &&
1869 curop->op_type != OP_NULL &&
1870 curop->op_type != OP_PUSHMARK)
1871 {
79072805
LW
1872 goto nope;
1873 }
1874 }
1875
1876 curop = LINKLIST(o);
1877 o->op_next = 0;
533c011a 1878 PL_op = curop;
76e3520e 1879 CALLRUNOPS();
3280af22 1880 sv = *(PL_stack_sp--);
748a9306 1881 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
79072805 1882 pad_swipe(o->op_targ);
748a9306
LW
1883 else if (SvTEMP(sv)) { /* grab mortal temp? */
1884 (void)SvREFCNT_inc(sv);
1885 SvTEMP_off(sv);
85e6fe83 1886 }
79072805
LW
1887 op_free(o);
1888 if (type == OP_RV2GV)
b1cb66bf 1889 return newGVOP(OP_GV, 0, (GV*)sv);
748a9306 1890 else {
ee580363
GS
1891 /* try to smush double to int, but don't smush -2.0 to -2 */
1892 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
1893 type != OP_NEGATE)
1894 {
748a9306 1895 IV iv = SvIV(sv);
ee580363 1896 if ((double)iv == SvNV(sv)) {
748a9306
LW
1897 SvREFCNT_dec(sv);
1898 sv = newSViv(iv);
1899 }
b1cb66bf 1900 else
1901 SvIOK_off(sv); /* undo SvIV() damage */
748a9306
LW
1902 }
1903 return newSVOP(OP_CONST, 0, sv);
1904 }
aeea060c 1905
79072805 1906 nope:
22c35a8c 1907 if (!(PL_opargs[type] & OA_OTHERINT))
79072805 1908 return o;
79072805 1909
3280af22 1910 if (!(PL_hints & HINT_INTEGER)) {
a0d0e21e 1911 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
85e6fe83
LW
1912 return o;
1913
1914 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1915 if (curop->op_type == OP_CONST) {
b1cb66bf 1916 if (SvIOK(((SVOP*)curop)->op_sv))
85e6fe83
LW
1917 continue;
1918 return o;
1919 }
22c35a8c 1920 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
79072805
LW
1921 continue;
1922 return o;
1923 }
22c35a8c 1924 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
79072805
LW
1925 }
1926
79072805
LW
1927 return o;
1928}
1929
1930OP *
864dbfa3 1931Perl_gen_constant_list(pTHX_ register OP *o)
79072805 1932{
11343788 1933 dTHR;
79072805 1934 register OP *curop;
3280af22 1935 I32 oldtmps_floor = PL_tmps_floor;
79072805 1936
a0d0e21e 1937 list(o);
3280af22 1938 if (PL_error_count)
a0d0e21e
LW
1939 return o; /* Don't attempt to run with errors */
1940
533c011a 1941 PL_op = curop = LINKLIST(o);
a0d0e21e 1942 o->op_next = 0;
11343788 1943 pp_pushmark(ARGS);
76e3520e 1944 CALLRUNOPS();
533c011a 1945 PL_op = curop;
11343788 1946 pp_anonlist(ARGS);
3280af22 1947 PL_tmps_floor = oldtmps_floor;
79072805
LW
1948
1949 o->op_type = OP_RV2AV;
22c35a8c 1950 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
79072805 1951 curop = ((UNOP*)o)->op_first;
3280af22 1952 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
79072805 1953 op_free(curop);
79072805
LW
1954 linklist(o);
1955 return list(o);
1956}
1957
1958OP *
864dbfa3 1959Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
79072805
LW
1960{
1961 OP *kid;
a0d0e21e 1962 OP *last = 0;
79072805 1963
11343788
MB
1964 if (!o || o->op_type != OP_LIST)
1965 o = newLISTOP(OP_LIST, 0, o, Nullop);
748a9306 1966 else
5dc0d613 1967 o->op_flags &= ~OPf_WANT;
79072805 1968
22c35a8c 1969 if (!(PL_opargs[type] & OA_MARK))
11343788 1970 null(cLISTOPo->op_first);
8990e307 1971
11343788 1972 o->op_type = type;
22c35a8c 1973 o->op_ppaddr = PL_ppaddr[type];
11343788 1974 o->op_flags |= flags;
79072805 1975
11343788
MB
1976 o = CHECKOP(type, o);
1977 if (o->op_type != type)
1978 return o;
79072805 1979
11343788 1980 if (cLISTOPo->op_children < 7) {
79072805 1981 /* XXX do we really need to do this if we're done appending?? */
11343788 1982 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805 1983 last = kid;
11343788 1984 cLISTOPo->op_last = last; /* in case check substituted last arg */
79072805
LW
1985 }
1986
11343788 1987 return fold_constants(o);
79072805
LW
1988}
1989
1990/* List constructors */
1991
1992OP *
864dbfa3 1993Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
1994{
1995 if (!first)
1996 return last;
8990e307
LW
1997
1998 if (!last)
79072805 1999 return first;
8990e307 2000
a0d0e21e
LW
2001 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2002 return newLISTOP(type, 0, first, last);
79072805 2003
a0d0e21e
LW
2004 if (first->op_flags & OPf_KIDS)
2005 ((LISTOP*)first)->op_last->op_sibling = last;
2006 else {
2007 first->op_flags |= OPf_KIDS;
2008 ((LISTOP*)first)->op_first = last;
2009 }
2010 ((LISTOP*)first)->op_last = last;
2011 ((LISTOP*)first)->op_children++;
2012 return first;
79072805
LW
2013}
2014
2015OP *
864dbfa3 2016Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
79072805
LW
2017{
2018 if (!first)
2019 return (OP*)last;
8990e307
LW
2020
2021 if (!last)
79072805 2022 return (OP*)first;
8990e307
LW
2023
2024 if (first->op_type != type)
79072805 2025 return prepend_elem(type, (OP*)first, (OP*)last);
8990e307
LW
2026
2027 if (last->op_type != type)
79072805
LW
2028 return append_elem(type, (OP*)first, (OP*)last);
2029
2030 first->op_last->op_sibling = last->op_first;
2031 first->op_last = last->op_last;
2032 first->op_children += last->op_children;
2033 if (first->op_children)
acb74605 2034 first->op_flags |= OPf_KIDS;
b7dc083c
NIS
2035
2036#ifdef PL_OP_SLAB_ALLOC
2037#else
2038 Safefree(last);
2039#endif
79072805
LW
2040 return (OP*)first;
2041}
2042
2043OP *
864dbfa3 2044Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2045{
2046 if (!first)
2047 return last;
8990e307
LW
2048
2049 if (!last)
79072805 2050 return first;
8990e307
LW
2051
2052 if (last->op_type == type) {
2053 if (type == OP_LIST) { /* already a PUSHMARK there */
2054 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2055 ((LISTOP*)last)->op_first->op_sibling = first;
2056 }
2057 else {
2058 if (!(last->op_flags & OPf_KIDS)) {
2059 ((LISTOP*)last)->op_last = first;
2060 last->op_flags |= OPf_KIDS;
2061 }
2062 first->op_sibling = ((LISTOP*)last)->op_first;
2063 ((LISTOP*)last)->op_first = first;
79072805 2064 }
79072805
LW
2065 ((LISTOP*)last)->op_children++;
2066 return last;
2067 }
2068
2069 return newLISTOP(type, 0, first, last);
2070}
2071
2072/* Constructors */
2073
2074OP *
864dbfa3 2075Perl_newNULLLIST(pTHX)
79072805 2076{
8990e307
LW
2077 return newOP(OP_STUB, 0);
2078}
2079
2080OP *
864dbfa3 2081Perl_force_list(pTHX_ OP *o)
8990e307 2082{
11343788
MB
2083 if (!o || o->op_type != OP_LIST)
2084 o = newLISTOP(OP_LIST, 0, o, Nullop);
2085 null(o);
2086 return o;
79072805
LW
2087}
2088
2089OP *
864dbfa3 2090Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2091{
2092 LISTOP *listop;
2093
b7dc083c 2094 NewOp(1101, listop, 1, LISTOP);
79072805
LW
2095
2096 listop->op_type = type;
22c35a8c 2097 listop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2098 listop->op_children = (first != 0) + (last != 0);
2099 listop->op_flags = flags;
79072805
LW
2100
2101 if (!last && first)
2102 last = first;
2103 else if (!first && last)
2104 first = last;
8990e307
LW
2105 else if (first)
2106 first->op_sibling = last;
79072805
LW
2107 listop->op_first = first;
2108 listop->op_last = last;
8990e307
LW
2109 if (type == OP_LIST) {
2110 OP* pushop;
2111 pushop = newOP(OP_PUSHMARK, 0);
2112 pushop->op_sibling = first;
2113 listop->op_first = pushop;
2114 listop->op_flags |= OPf_KIDS;
2115 if (!last)
2116 listop->op_last = pushop;
2117 }
2118 else if (listop->op_children)
2119 listop->op_flags |= OPf_KIDS;
79072805
LW
2120
2121 return (OP*)listop;
2122}
2123
2124OP *
864dbfa3 2125Perl_newOP(pTHX_ I32 type, I32 flags)
79072805 2126{
11343788 2127 OP *o;
b7dc083c 2128 NewOp(1101, o, 1, OP);
11343788 2129 o->op_type = type;
22c35a8c 2130 o->op_ppaddr = PL_ppaddr[type];
11343788 2131 o->op_flags = flags;
79072805 2132
11343788
MB
2133 o->op_next = o;
2134 o->op_private = 0 + (flags >> 8);
22c35a8c 2135 if (PL_opargs[type] & OA_RETSCALAR)
11343788 2136 scalar(o);
22c35a8c 2137 if (PL_opargs[type] & OA_TARGET)
11343788
MB
2138 o->op_targ = pad_alloc(type, SVs_PADTMP);
2139 return CHECKOP(type, o);
79072805
LW
2140}
2141
2142OP *
864dbfa3 2143Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
79072805
LW
2144{
2145 UNOP *unop;
2146
93a17b20 2147 if (!first)
aeea060c 2148 first = newOP(OP_STUB, 0);
22c35a8c 2149 if (PL_opargs[type] & OA_MARK)
8990e307 2150 first = force_list(first);
93a17b20 2151
b7dc083c 2152 NewOp(1101, unop, 1, UNOP);
79072805 2153 unop->op_type = type;
22c35a8c 2154 unop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2155 unop->op_first = first;
2156 unop->op_flags = flags | OPf_KIDS;
c07a80fd 2157 unop->op_private = 1 | (flags >> 8);
e50aee73 2158 unop = (UNOP*) CHECKOP(type, unop);
79072805
LW
2159 if (unop->op_next)
2160 return (OP*)unop;
2161
a0d0e21e 2162 return fold_constants((OP *) unop);
79072805
LW
2163}
2164
2165OP *
864dbfa3 2166Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2167{
2168 BINOP *binop;
b7dc083c 2169 NewOp(1101, binop, 1, BINOP);
79072805
LW
2170
2171 if (!first)
2172 first = newOP(OP_NULL, 0);
2173
2174 binop->op_type = type;
22c35a8c 2175 binop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2176 binop->op_first = first;
2177 binop->op_flags = flags | OPf_KIDS;
2178 if (!last) {
2179 last = first;
c07a80fd 2180 binop->op_private = 1 | (flags >> 8);
79072805
LW
2181 }
2182 else {
c07a80fd 2183 binop->op_private = 2 | (flags >> 8);
79072805
LW
2184 first->op_sibling = last;
2185 }
2186
e50aee73 2187 binop = (BINOP*)CHECKOP(type, binop);
79072805
LW
2188 if (binop->op_next)
2189 return (OP*)binop;
2190
7284ab6f 2191 binop->op_last = binop->op_first->op_sibling;
79072805 2192
a0d0e21e 2193 return fold_constants((OP *)binop);
79072805
LW
2194}
2195
a0ed51b3
LW
2196static int
2197utf8compare(const void *a, const void *b)
2198{
2199 int i;
2200 for (i = 0; i < 10; i++) {
2201 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2202 return -1;
2203 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2204 return 1;
2205 }
2206 return 0;
2207}
2208
79072805 2209OP *
864dbfa3 2210Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2211{
79072805
LW
2212 SV *tstr = ((SVOP*)expr)->op_sv;
2213 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
2214 STRLEN tlen;
2215 STRLEN rlen;
ec49126f 2216 register U8 *t = (U8*)SvPV(tstr, tlen);
2217 register U8 *r = (U8*)SvPV(rstr, rlen);
79072805
LW
2218 register I32 i;
2219 register I32 j;
a0ed51b3 2220 I32 del;
79072805 2221 I32 complement;
5d06d08e 2222 I32 squash;
79072805
LW
2223 register short *tbl;
2224
11343788 2225 complement = o->op_private & OPpTRANS_COMPLEMENT;
a0ed51b3 2226 del = o->op_private & OPpTRANS_DELETE;
5d06d08e 2227 squash = o->op_private & OPpTRANS_SQUASH;
79072805 2228
a0ed51b3 2229 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
79cb57f6 2230 SV* listsv = newSVpvn("# comment\n",10);
a0ed51b3
LW
2231 SV* transv = 0;
2232 U8* tend = t + tlen;
2233 U8* rend = r + rlen;
2234 I32 ulen;
2235 U32 tfirst = 1;
2236 U32 tlast = 0;
2237 I32 tdiff;
2238 U32 rfirst = 1;
2239 U32 rlast = 0;
2240 I32 rdiff;
2241 I32 diff;
2242 I32 none = 0;
2243 U32 max = 0;
2244 I32 bits;
2245 I32 grows = 0;
2246 I32 havefinal = 0;
2247 U32 final;
2248 HV *hv;
2249 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2250 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2251
2252 if (complement) {
2253 U8 tmpbuf[10];
2254 U8** cp;
2255 UV nextmin = 0;
2256 New(1109, cp, tlen, U8*);
2257 i = 0;
79cb57f6 2258 transv = newSVpvn("",0);
a0ed51b3
LW
2259 while (t < tend) {
2260 cp[i++] = t;
2261 t += UTF8SKIP(t);
2262 if (*t == 0xff) {
2263 t++;
2264 t += UTF8SKIP(t);
2265 }
2266 }
2267 qsort(cp, i, sizeof(U8*), utf8compare);
2268 for (j = 0; j < i; j++) {
2269 U8 *s = cp[j];
2270 UV val = utf8_to_uv(s, &ulen);
2271 s += ulen;
2272 diff = val - nextmin;
2273 if (diff > 0) {
2274 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2275 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2276 if (diff > 1) {
2277 t = uv_to_utf8(tmpbuf, val - 1);
2278 sv_catpvn(transv, "\377", 1);
dfe13c55 2279 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2280 }
2281 }
2282 if (*s == 0xff)
2283 val = utf8_to_uv(s+1, &ulen);
2284 if (val >= nextmin)
2285 nextmin = val + 1;
2286 }
2287 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2288 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2289 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2290 sv_catpvn(transv, "\377", 1);
dfe13c55
GS
2291 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2292 t = (U8*)SvPVX(transv);
a0ed51b3
LW
2293 tlen = SvCUR(transv);
2294 tend = t + tlen;
2295 }
2296 else if (!rlen && !del) {
2297 r = t; rlen = tlen; rend = tend;
4757a243
LW
2298 }
2299 if (!squash) {
2300 if (to_utf && from_utf) { /* only counting characters */
2301 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2302 o->op_private |= OPpTRANS_IDENTICAL;
2303 }
2304 else { /* straight latin-1 translation */
2305 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2306 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2307 o->op_private |= OPpTRANS_IDENTICAL;
2308 }
a0ed51b3
LW
2309 }
2310
2311 while (t < tend || tfirst <= tlast) {
2312 /* see if we need more "t" chars */
2313 if (tfirst > tlast) {
2314 tfirst = (I32)utf8_to_uv(t, &ulen);
2315 t += ulen;
2316 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2317 tlast = (I32)utf8_to_uv(++t, &ulen);
2318 t += ulen;
2319 }
2320 else
2321 tlast = tfirst;
2322 }
2323
2324 /* now see if we need more "r" chars */
2325 if (rfirst > rlast) {
2326 if (r < rend) {
2327 rfirst = (I32)utf8_to_uv(r, &ulen);
2328 r += ulen;
2329 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2330 rlast = (I32)utf8_to_uv(++r, &ulen);
2331 r += ulen;
2332 }
2333 else
2334 rlast = rfirst;
2335 }
2336 else {
2337 if (!havefinal++)
2338 final = rlast;
2339 rfirst = rlast = 0xffffffff;
2340 }
2341 }
2342
2343 /* now see which range will peter our first, if either. */
2344 tdiff = tlast - tfirst;
2345 rdiff = rlast - rfirst;
2346
2347 if (tdiff <= rdiff)
2348 diff = tdiff;
2349 else
2350 diff = rdiff;
2351
2352 if (rfirst == 0xffffffff) {
2353 diff = tdiff; /* oops, pretend rdiff is infinite */
2354 if (diff > 0)
2355 sv_catpvf(listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
2356 else
2357 sv_catpvf(listsv, "%04x\t\tXXXX\n", tfirst);
2358 }
2359 else {
2360 if (diff > 0)
2361 sv_catpvf(listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
2362 else
2363 sv_catpvf(listsv, "%04x\t\t%04x\n", tfirst, rfirst);
2364
2365 if (rfirst + diff > max)
2366 max = rfirst + diff;
2367 rfirst += diff + 1;
2368 if (!grows) {
2369 if (rfirst <= 0x80)
2370 ;
2371 else if (rfirst <= 0x800)
2372 grows |= (tfirst < 0x80);
2373 else if (rfirst <= 0x10000)
2374 grows |= (tfirst < 0x800);
2375 else if (rfirst <= 0x200000)
2376 grows |= (tfirst < 0x10000);
2377 else if (rfirst <= 0x4000000)
2378 grows |= (tfirst < 0x200000);
2379 else if (rfirst <= 0x80000000)
2380 grows |= (tfirst < 0x4000000);
2381 }
2382 }
2383 tfirst += diff + 1;
2384 }
2385
2386 none = ++max;
2387 if (del)
2388 del = ++max;
2389
2390 if (max > 0xffff)
2391 bits = 32;
2392 else if (max > 0xff)
2393 bits = 16;
2394 else
2395 bits = 8;
2396
2397 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2398 SvREFCNT_dec(listsv);
2399 if (transv)
2400 SvREFCNT_dec(transv);
2401
2402 if (!del && havefinal)
2403 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2404
2405 if (grows && to_utf)
2406 o->op_private |= OPpTRANS_GROWS;
2407
2408 op_free(expr);
2409 op_free(repl);
2410 return o;
2411 }
2412
2413 tbl = (short*)cPVOPo->op_pv;
79072805
LW
2414 if (complement) {
2415 Zero(tbl, 256, short);
2416 for (i = 0; i < tlen; i++)
ec49126f 2417 tbl[t[i]] = -1;
79072805
LW
2418 for (i = 0, j = 0; i < 256; i++) {
2419 if (!tbl[i]) {
2420 if (j >= rlen) {
a0ed51b3 2421 if (del)
79072805
LW
2422 tbl[i] = -2;
2423 else if (rlen)
ec49126f 2424 tbl[i] = r[j-1];
79072805
LW
2425 else
2426 tbl[i] = i;
2427 }
2428 else
ec49126f 2429 tbl[i] = r[j++];
79072805
LW
2430 }
2431 }
2432 }
2433 else {
a0ed51b3 2434 if (!rlen && !del) {
79072805 2435 r = t; rlen = tlen;
5d06d08e 2436 if (!squash)
4757a243 2437 o->op_private |= OPpTRANS_IDENTICAL;
79072805
LW
2438 }
2439 for (i = 0; i < 256; i++)
2440 tbl[i] = -1;
2441 for (i = 0, j = 0; i < tlen; i++,j++) {
2442 if (j >= rlen) {
a0ed51b3 2443 if (del) {
ec49126f 2444 if (tbl[t[i]] == -1)
2445 tbl[t[i]] = -2;
79072805
LW
2446 continue;
2447 }
2448 --j;
2449 }
ec49126f 2450 if (tbl[t[i]] == -1)
2451 tbl[t[i]] = r[j];
79072805
LW
2452 }
2453 }
2454 op_free(expr);
2455 op_free(repl);
2456
11343788 2457 return o;
79072805
LW
2458}
2459
2460OP *
864dbfa3 2461Perl_newPMOP(pTHX_ I32 type, I32 flags)
79072805 2462{
11343788 2463 dTHR;
79072805
LW
2464 PMOP *pmop;
2465
b7dc083c 2466 NewOp(1101, pmop, 1, PMOP);
79072805 2467 pmop->op_type = type;
22c35a8c 2468 pmop->op_ppaddr = PL_ppaddr[type];
79072805 2469 pmop->op_flags = flags;
c07a80fd 2470 pmop->op_private = 0 | (flags >> 8);
79072805 2471
3280af22 2472 if (PL_hints & HINT_RE_TAINT)
b3eb6a9b 2473 pmop->op_pmpermflags |= PMf_RETAINT;
3280af22 2474 if (PL_hints & HINT_LOCALE)
b3eb6a9b
GS
2475 pmop->op_pmpermflags |= PMf_LOCALE;
2476 pmop->op_pmflags = pmop->op_pmpermflags;
36477c24 2477
79072805 2478 /* link into pm list */
3280af22
NIS
2479 if (type != OP_TRANS && PL_curstash) {
2480 pmop->op_pmnext = HvPMROOT(PL_curstash);
2481 HvPMROOT(PL_curstash) = pmop;
79072805
LW
2482 }
2483
2484 return (OP*)pmop;
2485}
2486
2487OP *
864dbfa3 2488Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2489{
5c0ca799 2490 dTHR;
79072805
LW
2491 PMOP *pm;
2492 LOGOP *rcop;
ce862d02 2493 I32 repl_has_vars = 0;
79072805 2494
11343788
MB
2495 if (o->op_type == OP_TRANS)
2496 return pmtrans(o, expr, repl);
79072805 2497
3280af22 2498 PL_hints |= HINT_BLOCK_SCOPE;
11343788 2499 pm = (PMOP*)o;
79072805
LW
2500
2501 if (expr->op_type == OP_CONST) {
463ee0b2 2502 STRLEN plen;
79072805 2503 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 2504 char *p = SvPV(pat, plen);
11343788 2505 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 2506 sv_setpvn(pat, "\\s+", 3);
463ee0b2 2507 p = SvPV(pat, plen);
79072805
LW
2508 pm->op_pmflags |= PMf_SKIPWHITE;
2509 }
15e52e56 2510 pm->op_pmregexp = CALLREGCOMP(p, p + plen, pm);
aeea060c 2511 if (strEQ("\\s+", pm->op_pmregexp->precomp))
85e6fe83 2512 pm->op_pmflags |= PMf_WHITE;
79072805
LW
2513 op_free(expr);
2514 }
2515 else {
3280af22
NIS
2516 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2517 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2518 ? OP_REGCRESET
2519 : OP_REGCMAYBE),0,expr);
463ee0b2 2520
b7dc083c 2521 NewOp(1101, rcop, 1, LOGOP);
79072805 2522 rcop->op_type = OP_REGCOMP;
22c35a8c 2523 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
79072805 2524 rcop->op_first = scalar(expr);
3280af22 2525 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2526 ? (OPf_SPECIAL | OPf_KIDS)
2527 : OPf_KIDS);
79072805 2528 rcop->op_private = 1;
11343788 2529 rcop->op_other = o;
79072805
LW
2530
2531 /* establish postfix order */
3280af22 2532 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
463ee0b2
LW
2533 LINKLIST(expr);
2534 rcop->op_next = expr;
2535 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2536 }
2537 else {
2538 rcop->op_next = LINKLIST(expr);
2539 expr->op_next = (OP*)rcop;
2540 }
79072805 2541
11343788 2542 prepend_elem(o->op_type, scalar((OP*)rcop), o);
79072805
LW
2543 }
2544
2545 if (repl) {
748a9306 2546 OP *curop;
0244c3a4 2547 if (pm->op_pmflags & PMf_EVAL) {
748a9306 2548 curop = 0;
0244c3a4
GS
2549 if (PL_curcop->cop_line < PL_multi_end)
2550 PL_curcop->cop_line = PL_multi_end;
2551 }
554b3eca 2552#ifdef USE_THREADS
2faa37cc 2553 else if (repl->op_type == OP_THREADSV
554b3eca 2554 && strchr("&`'123456789+",
533c011a 2555 PL_threadsv_names[repl->op_targ]))
554b3eca
MB
2556 {
2557 curop = 0;
2558 }
2559#endif /* USE_THREADS */
748a9306
LW
2560 else if (repl->op_type == OP_CONST)
2561 curop = repl;
79072805 2562 else {
79072805
LW
2563 OP *lastop = 0;
2564 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
22c35a8c 2565 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
554b3eca 2566#ifdef USE_THREADS
ce862d02
IZ
2567 if (curop->op_type == OP_THREADSV) {
2568 repl_has_vars = 1;
be949f6f 2569 if (strchr("&`'123456789+", curop->op_private))
ce862d02 2570 break;
554b3eca
MB
2571 }
2572#else
79072805
LW
2573 if (curop->op_type == OP_GV) {
2574 GV *gv = ((GVOP*)curop)->op_gv;
ce862d02 2575 repl_has_vars = 1;
93a17b20 2576 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
2577 break;
2578 }
554b3eca 2579#endif /* USE_THREADS */
79072805
LW
2580 else if (curop->op_type == OP_RV2CV)
2581 break;
2582 else if (curop->op_type == OP_RV2SV ||
2583 curop->op_type == OP_RV2AV ||
2584 curop->op_type == OP_RV2HV ||
2585 curop->op_type == OP_RV2GV) {
2586 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2587 break;
2588 }
748a9306
LW
2589 else if (curop->op_type == OP_PADSV ||
2590 curop->op_type == OP_PADAV ||
2591 curop->op_type == OP_PADHV ||
554b3eca 2592 curop->op_type == OP_PADANY) {
ce862d02 2593 repl_has_vars = 1;
748a9306 2594 }
1167e5da
SM
2595 else if (curop->op_type == OP_PUSHRE)
2596 ; /* Okay here, dangerous in newASSIGNOP */
79072805
LW
2597 else
2598 break;
2599 }
2600 lastop = curop;
2601 }
748a9306 2602 }
ce862d02
IZ
2603 if (curop == repl
2604 && !(repl_has_vars
2605 && (!pm->op_pmregexp
2606 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
748a9306 2607 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4633a7c4 2608 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
11343788 2609 prepend_elem(o->op_type, scalar(repl), o);
748a9306
LW
2610 }
2611 else {
ce862d02
IZ
2612 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2613 pm->op_pmflags |= PMf_MAYBE_CONST;
2614 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2615 }
b7dc083c 2616 NewOp(1101, rcop, 1, LOGOP);
748a9306 2617 rcop->op_type = OP_SUBSTCONT;
22c35a8c 2618 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
748a9306
LW
2619 rcop->op_first = scalar(repl);
2620 rcop->op_flags |= OPf_KIDS;
2621 rcop->op_private = 1;
11343788 2622 rcop->op_other = o;
748a9306
LW
2623
2624 /* establish postfix order */
2625 rcop->op_next = LINKLIST(repl);
2626 repl->op_next = (OP*)rcop;
2627
2628 pm->op_pmreplroot = scalar((OP*)rcop);
2629 pm->op_pmreplstart = LINKLIST(rcop);
2630 rcop->op_next = 0;
79072805
LW
2631 }
2632 }
2633
2634 return (OP*)pm;
2635}
2636
2637OP *
864dbfa3 2638Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
79072805
LW
2639{
2640 SVOP *svop;
b7dc083c 2641 NewOp(1101, svop, 1, SVOP);
79072805 2642 svop->op_type = type;
22c35a8c 2643 svop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2644 svop->op_sv = sv;
2645 svop->op_next = (OP*)svop;
2646 svop->op_flags = flags;
22c35a8c 2647 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2648 scalar((OP*)svop);
22c35a8c 2649 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2650 svop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2651 return CHECKOP(type, svop);
79072805
LW
2652}
2653
2654OP *
864dbfa3 2655Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
79072805 2656{
11343788 2657 dTHR;
79072805 2658 GVOP *gvop;
b7dc083c 2659 NewOp(1101, gvop, 1, GVOP);
79072805 2660 gvop->op_type = type;
22c35a8c 2661 gvop->op_ppaddr = PL_ppaddr[type];
8990e307 2662 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
79072805
LW
2663 gvop->op_next = (OP*)gvop;
2664 gvop->op_flags = flags;
22c35a8c 2665 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2666 scalar((OP*)gvop);
22c35a8c 2667 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2668 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2669 return CHECKOP(type, gvop);
79072805
LW
2670}
2671
2672OP *
864dbfa3 2673Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
79072805
LW
2674{
2675 PVOP *pvop;
b7dc083c 2676 NewOp(1101, pvop, 1, PVOP);
79072805 2677 pvop->op_type = type;
22c35a8c 2678 pvop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2679 pvop->op_pv = pv;
2680 pvop->op_next = (OP*)pvop;
2681 pvop->op_flags = flags;
22c35a8c 2682 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2683 scalar((OP*)pvop);
22c35a8c 2684 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2685 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2686 return CHECKOP(type, pvop);
79072805
LW
2687}
2688
79072805 2689void
864dbfa3 2690Perl_package(pTHX_ OP *o)
79072805 2691{
11343788 2692 dTHR;
93a17b20 2693 SV *sv;
79072805 2694
3280af22
NIS
2695 save_hptr(&PL_curstash);
2696 save_item(PL_curstname);
11343788 2697 if (o) {
463ee0b2
LW
2698 STRLEN len;
2699 char *name;
11343788 2700 sv = cSVOPo->op_sv;
463ee0b2 2701 name = SvPV(sv, len);
3280af22
NIS
2702 PL_curstash = gv_stashpvn(name,len,TRUE);
2703 sv_setpvn(PL_curstname, name, len);
11343788 2704 op_free(o);
93a17b20
LW
2705 }
2706 else {
3280af22
NIS
2707 sv_setpv(PL_curstname,"<none>");
2708 PL_curstash = Nullhv;
93a17b20 2709 }
7ad382f4 2710 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
2711 PL_copline = NOLINE;
2712 PL_expect = XSTATE;
79072805
LW
2713}
2714
85e6fe83 2715void
864dbfa3 2716Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
85e6fe83 2717{
a0d0e21e
LW
2718 OP *pack;
2719 OP *meth;
2720 OP *rqop;
2721 OP *imop;
b1cb66bf 2722 OP *veop;
78ca652e 2723 GV *gv;
85e6fe83 2724
a0d0e21e
LW
2725 if (id->op_type != OP_CONST)
2726 croak("Module name must be constant");
85e6fe83 2727
b1cb66bf 2728 veop = Nullop;
2729
2730 if(version != Nullop) {
2731 SV *vesv = ((SVOP*)version)->op_sv;
2732
2733 if (arg == Nullop && !SvNIOK(vesv)) {
2734 arg = version;
2735 }
2736 else {
2737 OP *pack;
2738 OP *meth;
2739
2740 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2741 croak("Version number must be constant number");
2742
2743 /* Make copy of id so we don't free it twice */
2744 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2745
2746 /* Fake up a method call to VERSION */
79cb57f6 2747 meth = newSVOP(OP_CONST, 0, newSVpvn("VERSION", 7));
b1cb66bf 2748 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2749 append_elem(OP_LIST,
2750 prepend_elem(OP_LIST, pack, list(version)),
2751 newUNOP(OP_METHOD, 0, meth)));
2752 }
2753 }
aeea060c 2754
a0d0e21e 2755 /* Fake up an import/unimport */
4633a7c4
LW
2756 if (arg && arg->op_type == OP_STUB)
2757 imop = arg; /* no import on explicit () */
b1cb66bf 2758 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2759 imop = Nullop; /* use 5.0; */
2760 }
4633a7c4
LW
2761 else {
2762 /* Make copy of id so we don't free it twice */
2763 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
4633a7c4
LW
2764 meth = newSVOP(OP_CONST, 0,
2765 aver
79cb57f6
GS
2766 ? newSVpvn("import", 6)
2767 : newSVpvn("unimport", 8)
4633a7c4
LW
2768 );
2769 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
a0d0e21e
LW
2770 append_elem(OP_LIST,
2771 prepend_elem(OP_LIST, pack, list(arg)),
2772 newUNOP(OP_METHOD, 0, meth)));
4633a7c4
LW
2773 }
2774
78ca652e
GS
2775 /* Fake up a require, handle override, if any */
2776 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2777 if (!(gv && GvIMPORTED_CV(gv)))
2778 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2779
2780 if (gv && GvIMPORTED_CV(gv)) {
2781 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2782 append_elem(OP_LIST, id,
2783 scalar(newUNOP(OP_RV2CV, 0,
2784 newGVOP(OP_GV, 0,
2785 gv))))));
2786 }
2787 else {
2788 rqop = newUNOP(OP_REQUIRE, 0, id);
2789 }
a0d0e21e
LW
2790
2791 /* Fake up the BEGIN {}, which does its thing immediately. */
c07a80fd 2792 newSUB(floor,
79cb57f6 2793 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
4633a7c4 2794 Nullop,
a0d0e21e 2795 append_elem(OP_LINESEQ,
b1cb66bf 2796 append_elem(OP_LINESEQ,
2797 newSTATEOP(0, Nullch, rqop),
2798 newSTATEOP(0, Nullch, veop)),
a0d0e21e 2799 newSTATEOP(0, Nullch, imop) ));
85e6fe83 2800
3280af22
NIS
2801 PL_copline = NOLINE;
2802 PL_expect = XSTATE;
85e6fe83
LW
2803}
2804
79072805 2805OP *
864dbfa3 2806Perl_dofile(pTHX_ OP *term)
78ca652e
GS
2807{
2808 OP *doop;
2809 GV *gv;
2810
2811 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
2812 if (!(gv && GvIMPORTED_CV(gv)))
2813 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
2814
2815 if (gv && GvIMPORTED_CV(gv)) {
2816 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2817 append_elem(OP_LIST, term,
2818 scalar(newUNOP(OP_RV2CV, 0,
2819 newGVOP(OP_GV, 0,
2820 gv))))));
2821 }
2822 else {
2823 doop = newUNOP(OP_DOFILE, 0, scalar(term));
2824 }
2825 return doop;
2826}
2827
2828OP *
864dbfa3 2829Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
79072805
LW
2830{
2831 return newBINOP(OP_LSLICE, flags,
8990e307
LW
2832 list(force_list(subscript)),
2833 list(force_list(listval)) );
79072805
LW
2834}
2835
76e3520e 2836STATIC I32
864dbfa3 2837list_assignment(pTHX_ register OP *o)
79072805 2838{
11343788 2839 if (!o)
79072805
LW
2840 return TRUE;
2841
11343788
MB
2842 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2843 o = cUNOPo->op_first;
79072805 2844
11343788
MB
2845 if (o->op_type == OP_COND_EXPR) {
2846 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2847 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
79072805
LW
2848
2849 if (t && f)
2850 return TRUE;
2851 if (t || f)
2852 yyerror("Assignment to both a list and a scalar");
2853 return FALSE;
2854 }
2855
11343788
MB
2856 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2857 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2858 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
79072805
LW
2859 return TRUE;
2860
11343788 2861 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
93a17b20
LW
2862 return TRUE;
2863
11343788 2864 if (o->op_type == OP_RV2SV)
79072805
LW
2865 return FALSE;
2866
2867 return FALSE;
2868}
2869
2870OP *
864dbfa3 2871Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
79072805 2872{
11343788 2873 OP *o;
79072805 2874
a0d0e21e
LW
2875 if (optype) {
2876 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2877 return newLOGOP(optype, 0,
2878 mod(scalar(left), optype),
2879 newUNOP(OP_SASSIGN, 0, scalar(right)));
2880 }
2881 else {
2882 return newBINOP(optype, OPf_STACKED,
2883 mod(scalar(left), optype), scalar(right));
2884 }
2885 }
2886
79072805 2887 if (list_assignment(left)) {
6ee623d5 2888 dTHR;
3280af22
NIS
2889 PL_modcount = 0;
2890 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
463ee0b2 2891 left = mod(left, OP_AASSIGN);
3280af22
NIS
2892 if (PL_eval_start)
2893 PL_eval_start = 0;
748a9306 2894 else {
a0d0e21e
LW
2895 op_free(left);
2896 op_free(right);
2897 return Nullop;
2898 }
11343788 2899 o = newBINOP(OP_AASSIGN, flags,
8990e307
LW
2900 list(force_list(right)),
2901 list(force_list(left)) );
11343788 2902 o->op_private = 0 | (flags >> 8);
a0d0e21e 2903 if (!(left->op_private & OPpLVAL_INTRO)) {
79072805 2904 OP *curop;
11343788 2905 OP *lastop = o;
3280af22 2906 PL_generation++;
11343788 2907 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
22c35a8c 2908 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
79072805
LW
2909 if (curop->op_type == OP_GV) {
2910 GV *gv = ((GVOP*)curop)->op_gv;
3280af22 2911 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
79072805 2912 break;
3280af22 2913 SvCUR(gv) = PL_generation;
79072805 2914 }
748a9306
LW
2915 else if (curop->op_type == OP_PADSV ||
2916 curop->op_type == OP_PADAV ||
2917 curop->op_type == OP_PADHV ||
2918 curop->op_type == OP_PADANY) {
3280af22 2919 SV **svp = AvARRAY(PL_comppad_name);
8e07c86e 2920 SV *sv = svp[curop->op_targ];
3280af22 2921 if (SvCUR(sv) == PL_generation)
748a9306 2922 break;
3280af22 2923 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
748a9306 2924 }
79072805
LW
2925 else if (curop->op_type == OP_RV2CV)
2926 break;
2927 else if (curop->op_type == OP_RV2SV ||
2928 curop->op_type == OP_RV2AV ||
2929 curop->op_type == OP_RV2HV ||
2930 curop->op_type == OP_RV2GV) {
2931 if (lastop->op_type != OP_GV) /* funny deref? */
2932 break;
2933 }
1167e5da
SM
2934 else if (curop->op_type == OP_PUSHRE) {
2935 if (((PMOP*)curop)->op_pmreplroot) {
2936 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3280af22 2937 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
1167e5da 2938 break;
3280af22 2939 SvCUR(gv) = PL_generation;
1167e5da
SM
2940 }
2941 }
79072805
LW
2942 else
2943 break;
2944 }
2945 lastop = curop;
2946 }
11343788
MB
2947 if (curop != o)
2948 o->op_private = OPpASSIGN_COMMON;
79072805 2949 }
c07a80fd 2950 if (right && right->op_type == OP_SPLIT) {
2951 OP* tmpop;
2952 if ((tmpop = ((LISTOP*)right)->op_first) &&
2953 tmpop->op_type == OP_PUSHRE)
2954 {
2955 PMOP *pm = (PMOP*)tmpop;
2956 if (left->op_type == OP_RV2AV &&
2957 !(left->op_private & OPpLVAL_INTRO) &&
11343788 2958 !(o->op_private & OPpASSIGN_COMMON) )
c07a80fd 2959 {
2960 tmpop = ((UNOP*)left)->op_first;
2961 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2962 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2963 pm->op_pmflags |= PMf_ONCE;
11343788 2964 tmpop = cUNOPo->op_first; /* to list (nulled) */
c07a80fd 2965 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2966 tmpop->op_sibling = Nullop; /* don't free split */
2967 right->op_next = tmpop->op_next; /* fix starting loc */
11343788 2968 op_free(o); /* blow off assign */
54310121 2969 right->op_flags &= ~OPf_WANT;
a5f75d66 2970 /* "I don't know and I don't care." */
c07a80fd 2971 return right;
2972 }
2973 }
2974 else {
3280af22 2975 if (PL_modcount < 10000 &&
c07a80fd 2976 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2977 {
2978 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2979 if (SvIVX(sv) == 0)
3280af22 2980 sv_setiv(sv, PL_modcount+1);
c07a80fd 2981 }
2982 }
2983 }
2984 }
11343788 2985 return o;
79072805
LW
2986 }
2987 if (!right)
2988 right = newOP(OP_UNDEF, 0);
2989 if (right->op_type == OP_READLINE) {
2990 right->op_flags |= OPf_STACKED;
463ee0b2 2991 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805 2992 }
a0d0e21e 2993 else {
3280af22 2994 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
11343788 2995 o = newBINOP(OP_SASSIGN, flags,
463ee0b2 2996 scalar(right), mod(scalar(left), OP_SASSIGN) );
3280af22
NIS
2997 if (PL_eval_start)
2998 PL_eval_start = 0;
748a9306 2999 else {
11343788 3000 op_free(o);
a0d0e21e
LW
3001 return Nullop;
3002 }
3003 }
11343788 3004 return o;
79072805
LW
3005}
3006
3007OP *
864dbfa3 3008Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
79072805 3009{
11343788 3010 dTHR;
bbce6d69 3011 U32 seq = intro_my();
79072805
LW
3012 register COP *cop;
3013
b7dc083c 3014 NewOp(1101, cop, 1, COP);
3280af22 3015 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
8990e307 3016 cop->op_type = OP_DBSTATE;
22c35a8c 3017 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
8990e307
LW
3018 }
3019 else {
3020 cop->op_type = OP_NEXTSTATE;
22c35a8c 3021 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
8990e307 3022 }
79072805 3023 cop->op_flags = flags;
a0ed51b3 3024 cop->op_private = (PL_hints & HINT_UTF8);
ff0cee69 3025#ifdef NATIVE_HINTS
3026 cop->op_private |= NATIVE_HINTS;
3027#endif
e24b16f9 3028 PL_compiling.op_private = cop->op_private;
79072805
LW
3029 cop->op_next = (OP*)cop;
3030
463ee0b2
LW
3031 if (label) {
3032 cop->cop_label = label;
3280af22 3033 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 3034 }
bbce6d69 3035 cop->cop_seq = seq;
3280af22 3036 cop->cop_arybase = PL_curcop->cop_arybase;
599cee73
PM
3037 if (PL_curcop->cop_warnings == WARN_NONE
3038 || PL_curcop->cop_warnings == WARN_ALL)
3039 cop->cop_warnings = PL_curcop->cop_warnings ;
3040 else
3041 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3042
79072805 3043
3280af22
NIS
3044 if (PL_copline == NOLINE)
3045 cop->cop_line = PL_curcop->cop_line;
79072805 3046 else {
3280af22
NIS
3047 cop->cop_line = PL_copline;
3048 PL_copline = NOLINE;
79072805 3049 }
3280af22
NIS
3050 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
3051 cop->cop_stash = PL_curstash;
79072805 3052
3280af22
NIS
3053 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3054 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
3055 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
a0d0e21e 3056 (void)SvIOK_on(*svp);
a5f75d66 3057 SvIVX(*svp) = 1;
93a17b20
LW
3058 SvSTASH(*svp) = (HV*)cop;
3059 }
3060 }
3061
11343788 3062 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
79072805
LW
3063}
3064
bbce6d69 3065/* "Introduce" my variables to visible status. */
3066U32
864dbfa3 3067Perl_intro_my(pTHX)
bbce6d69 3068{
3069 SV **svp;
3070 SV *sv;
3071 I32 i;
3072
3280af22
NIS
3073 if (! PL_min_intro_pending)
3074 return PL_cop_seqmax;
bbce6d69 3075
3280af22
NIS
3076 svp = AvARRAY(PL_comppad_name);
3077 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3078 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
c53d7c7d 3079 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3280af22 3080 SvNVX(sv) = (double)PL_cop_seqmax;
bbce6d69 3081 }
3082 }
3280af22
NIS
3083 PL_min_intro_pending = 0;
3084 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3085 return PL_cop_seqmax++;
bbce6d69 3086}
3087
79072805 3088OP *
864dbfa3 3089Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
79072805 3090{
883ffac3
CS
3091 return new_logop(type, flags, &first, &other);
3092}
3093
3bd495df 3094STATIC OP *
864dbfa3 3095new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
883ffac3 3096{
11343788 3097 dTHR;
79072805 3098 LOGOP *logop;
11343788 3099 OP *o;
883ffac3
CS
3100 OP *first = *firstp;
3101 OP *other = *otherp;
79072805 3102
a0d0e21e
LW
3103 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3104 return newBINOP(type, flags, scalar(first), scalar(other));
3105
8990e307 3106 scalarboolean(first);
79072805
LW
3107 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3108 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3109 if (type == OP_AND || type == OP_OR) {
3110 if (type == OP_AND)
3111 type = OP_OR;
3112 else
3113 type = OP_AND;
11343788 3114 o = first;
883ffac3 3115 first = *firstp = cUNOPo->op_first;
11343788
MB
3116 if (o->op_next)
3117 first->op_next = o->op_next;
3118 cUNOPo->op_first = Nullop;
3119 op_free(o);
79072805
LW
3120 }
3121 }
3122 if (first->op_type == OP_CONST) {
599cee73
PM
3123 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
3124 warner(WARN_PRECEDENCE, "Probable precedence problem on %s",
22c35a8c 3125 PL_op_desc[type]);
79072805
LW
3126 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3127 op_free(first);
883ffac3 3128 *firstp = Nullop;
79072805
LW
3129 return other;
3130 }
3131 else {
3132 op_free(other);
883ffac3 3133 *otherp = Nullop;
79072805
LW
3134 return first;
3135 }
3136 }
3137 else if (first->op_type == OP_WANTARRAY) {
3138 if (type == OP_AND)
3139 list(other);
3140 else
3141 scalar(other);
3142 }
599cee73 3143 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
a6006777 3144 OP *k1 = ((UNOP*)first)->op_first;
3145 OP *k2 = k1->op_sibling;
3146 OPCODE warnop = 0;
3147 switch (first->op_type)
3148 {
3149 case OP_NULL:
3150 if (k2 && k2->op_type == OP_READLINE
3151 && (k2->op_flags & OPf_STACKED)
55d729e4 3152 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
a6006777 3153 warnop = k2->op_type;
3154 break;
3155
3156 case OP_SASSIGN:
68dc0745 3157 if (k1->op_type == OP_READDIR
3158 || k1->op_type == OP_GLOB
3159 || k1->op_type == OP_EACH)
a6006777 3160 warnop = k1->op_type;
3161 break;
3162 }
8ebc5c01 3163 if (warnop) {
3280af22
NIS
3164 line_t oldline = PL_curcop->cop_line;
3165 PL_curcop->cop_line = PL_copline;
599cee73
PM
3166 warner(WARN_UNSAFE,
3167 "Value of %s%s can be \"0\"; test with defined()",
22c35a8c 3168 PL_op_desc[warnop],
68dc0745 3169 ((warnop == OP_READLINE || warnop == OP_GLOB)
3170 ? " construct" : "() operator"));
3280af22 3171 PL_curcop->cop_line = oldline;
8ebc5c01 3172 }
a6006777 3173 }
79072805
LW
3174
3175 if (!other)
3176 return first;
3177
a0d0e21e
LW
3178 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3179 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3180
b7dc083c 3181 NewOp(1101, logop, 1, LOGOP);
79072805
LW
3182
3183 logop->op_type = type;
22c35a8c 3184 logop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3185 logop->op_first = first;
3186 logop->op_flags = flags | OPf_KIDS;
3187 logop->op_other = LINKLIST(other);
c07a80fd 3188 logop->op_private = 1 | (flags >> 8);
79072805
LW
3189
3190 /* establish postfix order */
3191 logop->op_next = LINKLIST(first);
3192 first->op_next = (OP*)logop;
3193 first->op_sibling = other;
3194
11343788
MB
3195 o = newUNOP(OP_NULL, 0, (OP*)logop);
3196 other->op_next = o;
79072805 3197
11343788 3198 return o;
79072805
LW
3199}
3200
3201OP *
864dbfa3 3202Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
79072805 3203{
11343788 3204 dTHR;
79072805 3205 CONDOP *condop;
11343788 3206 OP *o;
79072805 3207
b1cb66bf 3208 if (!falseop)
3209 return newLOGOP(OP_AND, 0, first, trueop);
3210 if (!trueop)
3211 return newLOGOP(OP_OR, 0, first, falseop);
79072805 3212
8990e307 3213 scalarboolean(first);
79072805
LW
3214 if (first->op_type == OP_CONST) {
3215 if (SvTRUE(((SVOP*)first)->op_sv)) {
3216 op_free(first);
b1cb66bf 3217 op_free(falseop);
3218 return trueop;
79072805
LW
3219 }
3220 else {
3221 op_free(first);
b1cb66bf 3222 op_free(trueop);
3223 return falseop;
79072805
LW
3224 }
3225 }
3226 else if (first->op_type == OP_WANTARRAY) {
b1cb66bf 3227 list(trueop);
3228 scalar(falseop);
79072805 3229 }
b7dc083c 3230 NewOp(1101, condop, 1, CONDOP);
79072805
LW
3231
3232 condop->op_type = OP_COND_EXPR;
22c35a8c 3233 condop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
79072805
LW
3234 condop->op_first = first;
3235 condop->op_flags = flags | OPf_KIDS;
b1cb66bf 3236 condop->op_true = LINKLIST(trueop);
3237 condop->op_false = LINKLIST(falseop);
c07a80fd 3238 condop->op_private = 1 | (flags >> 8);
79072805
LW
3239
3240 /* establish postfix order */
3241 condop->op_next = LINKLIST(first);
3242 first->op_next = (OP*)condop;
3243
b1cb66bf 3244 first->op_sibling = trueop;
3245 trueop->op_sibling = falseop;
11343788 3246 o = newUNOP(OP_NULL, 0, (OP*)condop);
79072805 3247
5dc0d613
MB
3248 trueop->op_next = o;
3249 falseop->op_next = o;
79072805 3250
11343788 3251 return o;
79072805
LW
3252}
3253
3254OP *
864dbfa3 3255Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
79072805 3256{
b35b2403 3257 dTHR;
79072805
LW
3258 CONDOP *condop;
3259 OP *flip;
3260 OP *flop;
11343788 3261 OP *o;
79072805 3262
b7dc083c 3263 NewOp(1101, condop, 1, CONDOP);
79072805
LW
3264
3265 condop->op_type = OP_RANGE;
22c35a8c 3266 condop->op_ppaddr = PL_ppaddr[OP_RANGE];
79072805
LW
3267 condop->op_first = left;
3268 condop->op_flags = OPf_KIDS;
3269 condop->op_true = LINKLIST(left);
3270 condop->op_false = LINKLIST(right);
c07a80fd 3271 condop->op_private = 1 | (flags >> 8);
79072805
LW
3272
3273 left->op_sibling = right;
3274
3275 condop->op_next = (OP*)condop;
3276 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
3277 flop = newUNOP(OP_FLOP, 0, flip);
11343788 3278 o = newUNOP(OP_NULL, 0, flop);
79072805
LW
3279 linklist(flop);
3280
3281 left->op_next = flip;
3282 right->op_next = flop;
3283
ed6116ce 3284 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805 3285 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
ed6116ce 3286 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805
LW
3287 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3288
3289 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3290 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3291
11343788 3292 flip->op_next = o;
79072805 3293 if (!flip->op_private || !flop->op_private)
11343788 3294 linklist(o); /* blow off optimizer unless constant */
79072805 3295
11343788 3296 return o;
79072805
LW
3297}
3298
3299OP *
864dbfa3 3300Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
79072805 3301{
11343788 3302 dTHR;
463ee0b2 3303 OP* listop;
11343788 3304 OP* o;
463ee0b2 3305 int once = block && block->op_flags & OPf_SPECIAL &&
a0d0e21e 3306 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
93a17b20 3307
463ee0b2
LW
3308 if (expr) {
3309 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3310 return block; /* do {} while 0 does once */
fb73857a 3311 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3312 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
774d564b 3313 expr = newUNOP(OP_DEFINED, 0,
54b9620d 3314 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
55d729e4
GS
3315 } else if (expr->op_flags & OPf_KIDS) {
3316 OP *k1 = ((UNOP*)expr)->op_first;
3317 OP *k2 = (k1) ? k1->op_sibling : NULL;
3318 switch (expr->op_type) {
3319 case OP_NULL:
3320 if (k2 && k2->op_type == OP_READLINE
3321 && (k2->op_flags & OPf_STACKED)
3322 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3323 expr = newUNOP(OP_DEFINED, 0, expr);
3324 break;
3325
3326 case OP_SASSIGN:
3327 if (k1->op_type == OP_READDIR
3328 || k1->op_type == OP_GLOB
3329 || k1->op_type == OP_EACH)
3330 expr = newUNOP(OP_DEFINED, 0, expr);
3331 break;
3332 }
774d564b 3333 }
463ee0b2 3334 }
93a17b20 3335
8990e307 3336 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
883ffac3 3337 o = new_logop(OP_AND, 0, &expr, &listop);
463ee0b2 3338
883ffac3
CS
3339 if (listop)
3340 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
79072805 3341
11343788
MB
3342 if (once && o != listop)
3343 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
79072805 3344
11343788
MB
3345 if (o == listop)
3346 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
748a9306 3347
11343788
MB
3348 o->op_flags |= flags;
3349 o = scope(o);
3350 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3351 return o;
79072805
LW
3352}
3353
3354OP *
864dbfa3 3355Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
79072805 3356{
11343788 3357 dTHR;
79072805
LW
3358 OP *redo;
3359 OP *next = 0;
3360 OP *listop;
11343788 3361 OP *o;
79072805
LW
3362 OP *condop;
3363
fb73857a 3364 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3365 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
748a9306 3366 expr = newUNOP(OP_DEFINED, 0,
54b9620d 3367 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
55d729e4
GS
3368 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3369 OP *k1 = ((UNOP*)expr)->op_first;
3370 OP *k2 = (k1) ? k1->op_sibling : NULL;
3371 switch (expr->op_type) {
3372 case OP_NULL:
3373 if (k2 && k2->op_type == OP_READLINE
3374 && (k2->op_flags & OPf_STACKED)
3375 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3376 expr = newUNOP(OP_DEFINED, 0, expr);
3377 break;
3378
3379 case OP_SASSIGN:
3380 if (k1->op_type == OP_READDIR
3381 || k1->op_type == OP_GLOB
3382 || k1->op_type == OP_EACH)
3383 expr = newUNOP(OP_DEFINED, 0, expr);
3384 break;
3385 }
748a9306 3386 }
79072805
LW
3387
3388 if (!block)
3389 block = newOP(OP_NULL, 0);
3390
3391 if (cont)
3392 next = LINKLIST(cont);
fb73857a 3393 if (expr) {
79072805 3394 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
fb73857a 3395 if ((line_t)whileline != NOLINE) {
3280af22 3396 PL_copline = whileline;
fb73857a 3397 cont = append_elem(OP_LINESEQ, cont,
3398 newSTATEOP(0, Nullch, Nullop));
3399 }
3400 }
79072805 3401
463ee0b2 3402 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
79072805
LW
3403 redo = LINKLIST(listop);
3404
3405 if (expr) {
3280af22 3406 PL_copline = whileline;
883ffac3
CS
3407 scalar(listop);
3408 o = new_logop(OP_AND, 0, &expr, &listop);
11343788 3409 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
85e6fe83 3410 op_free(expr); /* oops, it's a while (0) */
463ee0b2 3411 op_free((OP*)loop);
883ffac3 3412 return Nullop; /* listop already freed by new_logop */
463ee0b2 3413 }
883ffac3
CS
3414 if (listop)
3415 ((LISTOP*)listop)->op_last->op_next = condop =
3416 (o == listop ? redo : LINKLIST(o));
79072805
LW
3417 if (!next)
3418 next = condop;
3419 }
3420 else
11343788 3421 o = listop;
79072805
LW
3422
3423 if (!loop) {
b7dc083c 3424 NewOp(1101,loop,1,LOOP);
79072805 3425 loop->op_type = OP_ENTERLOOP;
22c35a8c 3426 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
79072805
LW
3427 loop->op_private = 0;
3428 loop->op_next = (OP*)loop;
3429 }
3430
11343788 3431 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
79072805
LW
3432
3433 loop->op_redoop = redo;
11343788 3434 loop->op_lastop = o;
79072805
LW
3435
3436 if (next)
3437 loop->op_nextop = next;
3438 else
11343788 3439 loop->op_nextop = o;
79072805 3440
11343788
MB
3441 o->op_flags |= flags;
3442 o->op_private |= (flags >> 8);
3443 return o;
79072805
LW
3444}
3445
3446OP *
864dbfa3 3447Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
79072805
LW
3448{
3449 LOOP *loop;
b7dc083c 3450 LOOP *tmp;
fb73857a 3451 OP *wop;
85e6fe83 3452 int padoff = 0;
4633a7c4 3453 I32 iterflags = 0;
79072805 3454
79072805 3455 if (sv) {
85e6fe83 3456 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
748a9306 3457 sv->op_type = OP_RV2GV;
22c35a8c 3458 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
79072805 3459 }
85e6fe83
LW
3460 else if (sv->op_type == OP_PADSV) { /* private variable */
3461 padoff = sv->op_targ;
3462 op_free(sv);
3463 sv = Nullop;
3464 }
54b9620d
MB
3465 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3466 padoff = sv->op_targ;
3467 iterflags |= OPf_SPECIAL;
3468 op_free(sv);
3469 sv = Nullop;
3470 }
79072805 3471 else
22c35a8c 3472 croak("Can't use %s for loop variable", PL_op_desc[sv->op_type]);
79072805
LW
3473 }
3474 else {
54b9620d
MB
3475#ifdef USE_THREADS
3476 padoff = find_threadsv("_");
3477 iterflags |= OPf_SPECIAL;
3478#else
3280af22 3479 sv = newGVOP(OP_GV, 0, PL_defgv);
54b9620d 3480#endif
79072805 3481 }
5f05dabc 3482 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
89ea2908 3483 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
4633a7c4
LW
3484 iterflags |= OPf_STACKED;
3485 }
89ea2908
GA
3486 else if (expr->op_type == OP_NULL &&
3487 (expr->op_flags & OPf_KIDS) &&
3488 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3489 {
3490 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3491 * set the STACKED flag to indicate that these values are to be
3492 * treated as min/max values by 'pp_iterinit'.
3493 */
3494 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3495 CONDOP* range = (CONDOP*) flip->op_first;
3496 OP* left = range->op_first;
3497 OP* right = left->op_sibling;
5152d7c7 3498 LISTOP* listop;
89ea2908
GA
3499
3500 range->op_flags &= ~OPf_KIDS;
3501 range->op_first = Nullop;
3502
5152d7c7
GS
3503 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3504 listop->op_first->op_next = range->op_true;
89ea2908 3505 left->op_next = range->op_false;
5152d7c7
GS
3506 right->op_next = (OP*)listop;
3507 listop->op_next = listop->op_first;
89ea2908
GA
3508
3509 op_free(expr);
5152d7c7 3510 expr = (OP*)(listop);
89ea2908
GA
3511 null(expr);
3512 iterflags |= OPf_STACKED;
3513 }
3514 else {
3515 expr = mod(force_list(expr), OP_GREPSTART);
3516 }
3517
3518
4633a7c4 3519 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
89ea2908 3520 append_elem(OP_LIST, expr, scalar(sv))));
85e6fe83 3521 assert(!loop->op_next);
b7dc083c
NIS
3522#ifdef PL_OP_SLAB_ALLOC
3523 NewOp(1234,tmp,1,LOOP);
3524 Copy(loop,tmp,1,LOOP);
3525 loop = tmp;
3526#else
85e6fe83 3527 Renew(loop, 1, LOOP);
b7dc083c