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