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