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