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