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