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