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