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