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