This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
portability fix for IO::File and FileHandle
[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 {
46fc3d4c 145 if (!isPRINT(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 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 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 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 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 237 I32 depth;
238 AV *oldpad;
239 SV *oldsv;
240
241 depth = CvDEPTH(cv);
242 if (!depth) {
9607fc9c 243 if (newoff) {
244 if (SvFAKE(sv))
245 continue;
4fdae800 246 return 0; /* don't clone from inactive stack frame */
9607fc9c 247 }
5f05dabc 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 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 272 oldsv = Nullsv; /* no need to keep ref */
273 }
274 else {
28757baa 275 CV *bcv;
276 for (bcv = startcv;
277 bcv && bcv != cv && !CvCLONE(bcv);
6b35e009
GS
278 bcv = CvOUTSIDE(bcv))
279 {
28757baa 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 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 325 switch (cx->blk_eval.old_op_type) {
326 case OP_ENTEREVAL:
6b35e009
GS
327 if (CxREALEVAL(cx))
328 saweval = i;
44a8e56a 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 380 (!SvIVX(sv) ||
381 (seq <= SvIVX(sv) &&
382 seq > I_32(SvNVX(sv)))) &&
a0d0e21e
LW
383 strEQ(SvPVX(sv), name))
384 {
54310121 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 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 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 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 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:
5196be3e 689 Safefree(cCOPo->cop_label);
11343788 690 SvREFCNT_dec(cCOPo->cop_filegv);
599cee73
PM
691 if (cCOPo->cop_warnings != WARN_NONE && cCOPo->cop_warnings != WARN_ALL)
692 SvREFCNT_dec(cCOPo->cop_warnings);
79072805
LW
693 break;
694 case OP_CONST:
11343788 695 SvREFCNT_dec(cSVOPo->op_sv);
79072805 696 break;
748a9306
LW
697 case OP_GOTO:
698 case OP_NEXT:
699 case OP_LAST:
700 case OP_REDO:
11343788 701 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
748a9306
LW
702 break;
703 /* FALL THROUGH */
a0d0e21e 704 case OP_TRANS:
a0ed51b3
LW
705 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF))
706 SvREFCNT_dec(cSVOPo->op_sv);
707 else
708 Safefree(cPVOPo->op_pv);
a0d0e21e
LW
709 break;
710 case OP_SUBST:
11343788 711 op_free(cPMOPo->op_pmreplroot);
a0d0e21e 712 /* FALL THROUGH */
748a9306 713 case OP_PUSHRE:
a0d0e21e 714 case OP_MATCH:
8782bef2 715 case OP_QR:
c277df42 716 ReREFCNT_dec(cPMOPo->op_pmregexp);
a0d0e21e 717 break;
79072805
LW
718 }
719
11343788
MB
720 if (o->op_targ > 0)
721 pad_free(o->op_targ);
8990e307 722
b7dc083c
NIS
723#ifdef PL_OP_SLAB_ALLOC
724 if ((char *) o == PL_OpPtr)
725 {
726 }
727#else
11343788 728 Safefree(o);
b7dc083c 729#endif
79072805
LW
730}
731
76e3520e 732STATIC void
cea2e8a9 733S_null(pTHX_ OP *o)
8990e307 734{
54b9620d 735 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
11343788
MB
736 pad_free(o->op_targ);
737 o->op_targ = o->op_type;
738 o->op_type = OP_NULL;
22c35a8c 739 o->op_ppaddr = PL_ppaddr[OP_NULL];
8990e307
LW
740}
741
79072805
LW
742/* Contextualizers */
743
463ee0b2 744#define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
79072805
LW
745
746OP *
864dbfa3 747Perl_linklist(pTHX_ OP *o)
79072805
LW
748{
749 register OP *kid;
750
11343788
MB
751 if (o->op_next)
752 return o->op_next;
79072805
LW
753
754 /* establish postfix order */
11343788
MB
755 if (cUNOPo->op_first) {
756 o->op_next = LINKLIST(cUNOPo->op_first);
757 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
758 if (kid->op_sibling)
759 kid->op_next = LINKLIST(kid->op_sibling);
760 else
11343788 761 kid->op_next = o;
79072805
LW
762 }
763 }
764 else
11343788 765 o->op_next = o;
79072805 766
11343788 767 return o->op_next;
79072805
LW
768}
769
770OP *
864dbfa3 771Perl_scalarkids(pTHX_ OP *o)
79072805
LW
772{
773 OP *kid;
11343788
MB
774 if (o && o->op_flags & OPf_KIDS) {
775 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
776 scalar(kid);
777 }
11343788 778 return o;
79072805
LW
779}
780
76e3520e 781STATIC OP *
cea2e8a9 782S_scalarboolean(pTHX_ OP *o)
8990e307 783{
d008e5eb 784 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
0f15f207 785 dTHR;
d008e5eb
GS
786 if (ckWARN(WARN_SYNTAX)) {
787 line_t oldline = PL_curcop->cop_line;
a0d0e21e 788
d008e5eb
GS
789 if (PL_copline != NOLINE)
790 PL_curcop->cop_line = PL_copline;
cea2e8a9 791 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
d008e5eb
GS
792 PL_curcop->cop_line = oldline;
793 }
a0d0e21e 794 }
11343788 795 return scalar(o);
8990e307
LW
796}
797
798OP *
864dbfa3 799Perl_scalar(pTHX_ OP *o)
79072805
LW
800{
801 OP *kid;
802
a0d0e21e 803 /* assumes no premature commitment */
3280af22 804 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 805 || o->op_type == OP_RETURN)
11343788 806 return o;
79072805 807
5dc0d613 808 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
79072805 809
11343788 810 switch (o->op_type) {
79072805 811 case OP_REPEAT:
11343788
MB
812 if (o->op_private & OPpREPEAT_DOLIST)
813 null(((LISTOP*)cBINOPo->op_first)->op_first);
814 scalar(cBINOPo->op_first);
8990e307 815 break;
79072805
LW
816 case OP_OR:
817 case OP_AND:
818 case OP_COND_EXPR:
11343788 819 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
8990e307 820 scalar(kid);
79072805 821 break;
a0d0e21e 822 case OP_SPLIT:
11343788 823 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
824 if (!kPMOP->op_pmreplroot)
825 deprecate("implicit split to @_");
826 }
827 /* FALL THROUGH */
79072805 828 case OP_MATCH:
8782bef2 829 case OP_QR:
79072805
LW
830 case OP_SUBST:
831 case OP_NULL:
8990e307 832 default:
11343788
MB
833 if (o->op_flags & OPf_KIDS) {
834 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
8990e307
LW
835 scalar(kid);
836 }
79072805
LW
837 break;
838 case OP_LEAVE:
839 case OP_LEAVETRY:
5dc0d613 840 kid = cLISTOPo->op_first;
54310121 841 scalar(kid);
842 while (kid = kid->op_sibling) {
843 if (kid->op_sibling)
844 scalarvoid(kid);
845 else
846 scalar(kid);
847 }
3280af22 848 WITH_THR(PL_curcop = &PL_compiling);
54310121 849 break;
748a9306 850 case OP_SCOPE:
79072805 851 case OP_LINESEQ:
8990e307 852 case OP_LIST:
11343788 853 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
854 if (kid->op_sibling)
855 scalarvoid(kid);
856 else
857 scalar(kid);
858 }
3280af22 859 WITH_THR(PL_curcop = &PL_compiling);
79072805
LW
860 break;
861 }
11343788 862 return o;
79072805
LW
863}
864
865OP *
864dbfa3 866Perl_scalarvoid(pTHX_ OP *o)
79072805
LW
867{
868 OP *kid;
8990e307
LW
869 char* useless = 0;
870 SV* sv;
2ebea0a1
GS
871 U8 want;
872
873 if (o->op_type == OP_NEXTSTATE || o->op_type == OP_DBSTATE ||
874 (o->op_type == OP_NULL &&
875 (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)))
876 {
877 dTHR;
878 PL_curcop = (COP*)o; /* for warning below */
879 }
79072805 880
54310121 881 /* assumes no premature commitment */
2ebea0a1
GS
882 want = o->op_flags & OPf_WANT;
883 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
5dc0d613 884 || o->op_type == OP_RETURN)
11343788 885 return o;
79072805 886
5dc0d613 887 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
79072805 888
11343788 889 switch (o->op_type) {
79072805 890 default:
22c35a8c 891 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
8990e307 892 break;
36477c24 893 /* FALL THROUGH */
894 case OP_REPEAT:
11343788 895 if (o->op_flags & OPf_STACKED)
8990e307 896 break;
5d82c453
GA
897 goto func_ops;
898 case OP_SUBSTR:
899 if (o->op_private == 4)
900 break;
8990e307
LW
901 /* FALL THROUGH */
902 case OP_GVSV:
903 case OP_WANTARRAY:
904 case OP_GV:
905 case OP_PADSV:
906 case OP_PADAV:
907 case OP_PADHV:
908 case OP_PADANY:
909 case OP_AV2ARYLEN:
8990e307 910 case OP_REF:
a0d0e21e
LW
911 case OP_REFGEN:
912 case OP_SREFGEN:
8990e307
LW
913 case OP_DEFINED:
914 case OP_HEX:
915 case OP_OCT:
916 case OP_LENGTH:
8990e307
LW
917 case OP_VEC:
918 case OP_INDEX:
919 case OP_RINDEX:
920 case OP_SPRINTF:
921 case OP_AELEM:
922 case OP_AELEMFAST:
923 case OP_ASLICE:
8990e307
LW
924 case OP_HELEM:
925 case OP_HSLICE:
926 case OP_UNPACK:
927 case OP_PACK:
8990e307
LW
928 case OP_JOIN:
929 case OP_LSLICE:
930 case OP_ANONLIST:
931 case OP_ANONHASH:
932 case OP_SORT:
933 case OP_REVERSE:
934 case OP_RANGE:
935 case OP_FLIP:
936 case OP_FLOP:
937 case OP_CALLER:
938 case OP_FILENO:
939 case OP_EOF:
940 case OP_TELL:
941 case OP_GETSOCKNAME:
942 case OP_GETPEERNAME:
943 case OP_READLINK:
944 case OP_TELLDIR:
945 case OP_GETPPID:
946 case OP_GETPGRP:
947 case OP_GETPRIORITY:
948 case OP_TIME:
949 case OP_TMS:
950 case OP_LOCALTIME:
951 case OP_GMTIME:
952 case OP_GHBYNAME:
953 case OP_GHBYADDR:
954 case OP_GHOSTENT:
955 case OP_GNBYNAME:
956 case OP_GNBYADDR:
957 case OP_GNETENT:
958 case OP_GPBYNAME:
959 case OP_GPBYNUMBER:
960 case OP_GPROTOENT:
961 case OP_GSBYNAME:
962 case OP_GSBYPORT:
963 case OP_GSERVENT:
964 case OP_GPWNAM:
965 case OP_GPWUID:
966 case OP_GGRNAM:
967 case OP_GGRGID:
968 case OP_GETLOGIN:
5d82c453 969 func_ops:
11343788 970 if (!(o->op_private & OPpLVAL_INTRO))
22c35a8c 971 useless = PL_op_desc[o->op_type];
8990e307
LW
972 break;
973
974 case OP_RV2GV:
975 case OP_RV2SV:
976 case OP_RV2AV:
977 case OP_RV2HV:
11343788
MB
978 if (!(o->op_private & OPpLVAL_INTRO) &&
979 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
8990e307
LW
980 useless = "a variable";
981 break;
79072805
LW
982
983 case OP_CONST:
11343788 984 sv = cSVOPo->op_sv;
7a52d87a
GS
985 if (cSVOPo->op_private & OPpCONST_STRICT)
986 no_bareword_allowed(o);
987 else {
d008e5eb
GS
988 dTHR;
989 if (ckWARN(WARN_VOID)) {
990 useless = "a constant";
991 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
992 useless = 0;
993 else if (SvPOK(sv)) {
994 if (strnEQ(SvPVX(sv), "di", 2) ||
995 strnEQ(SvPVX(sv), "ds", 2) ||
996 strnEQ(SvPVX(sv), "ig", 2))
997 useless = 0;
998 }
8990e307
LW
999 }
1000 }
11343788 1001 null(o); /* don't execute a constant */
8990e307 1002 SvREFCNT_dec(sv); /* don't even remember it */
79072805
LW
1003 break;
1004
1005 case OP_POSTINC:
11343788 1006 o->op_type = OP_PREINC; /* pre-increment is faster */
22c35a8c 1007 o->op_ppaddr = PL_ppaddr[OP_PREINC];
79072805
LW
1008 break;
1009
1010 case OP_POSTDEC:
11343788 1011 o->op_type = OP_PREDEC; /* pre-decrement is faster */
22c35a8c 1012 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
79072805
LW
1013 break;
1014
79072805
LW
1015 case OP_OR:
1016 case OP_AND:
1017 case OP_COND_EXPR:
11343788 1018 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1019 scalarvoid(kid);
1020 break;
5aabfad6 1021
a0d0e21e 1022 case OP_NULL:
11343788 1023 if (o->op_flags & OPf_STACKED)
a0d0e21e 1024 break;
5aabfad6 1025 /* FALL THROUGH */
2ebea0a1
GS
1026 case OP_NEXTSTATE:
1027 case OP_DBSTATE:
79072805
LW
1028 case OP_ENTERTRY:
1029 case OP_ENTER:
1030 case OP_SCALAR:
11343788 1031 if (!(o->op_flags & OPf_KIDS))
79072805 1032 break;
54310121 1033 /* FALL THROUGH */
463ee0b2 1034 case OP_SCOPE:
79072805
LW
1035 case OP_LEAVE:
1036 case OP_LEAVETRY:
a0d0e21e 1037 case OP_LEAVELOOP:
79072805 1038 case OP_LINESEQ:
79072805 1039 case OP_LIST:
11343788 1040 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1041 scalarvoid(kid);
1042 break;
c90c0ff4 1043 case OP_ENTEREVAL:
5196be3e 1044 scalarkids(o);
c90c0ff4 1045 break;
5aabfad6 1046 case OP_REQUIRE:
c90c0ff4 1047 /* all requires must return a boolean value */
5196be3e
MB
1048 o->op_flags &= ~OPf_WANT;
1049 return scalar(o);
a0d0e21e 1050 case OP_SPLIT:
11343788 1051 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
1052 if (!kPMOP->op_pmreplroot)
1053 deprecate("implicit split to @_");
1054 }
1055 break;
79072805 1056 }
d008e5eb
GS
1057 if (useless) {
1058 dTHR;
1059 if (ckWARN(WARN_VOID))
cea2e8a9 1060 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
d008e5eb 1061 }
11343788 1062 return o;
79072805
LW
1063}
1064
1065OP *
864dbfa3 1066Perl_listkids(pTHX_ OP *o)
79072805
LW
1067{
1068 OP *kid;
11343788
MB
1069 if (o && o->op_flags & OPf_KIDS) {
1070 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1071 list(kid);
1072 }
11343788 1073 return o;
79072805
LW
1074}
1075
1076OP *
864dbfa3 1077Perl_list(pTHX_ OP *o)
79072805
LW
1078{
1079 OP *kid;
1080
a0d0e21e 1081 /* assumes no premature commitment */
3280af22 1082 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 1083 || o->op_type == OP_RETURN)
11343788 1084 return o;
79072805 1085
5dc0d613 1086 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
79072805 1087
11343788 1088 switch (o->op_type) {
79072805
LW
1089 case OP_FLOP:
1090 case OP_REPEAT:
11343788 1091 list(cBINOPo->op_first);
79072805
LW
1092 break;
1093 case OP_OR:
1094 case OP_AND:
1095 case OP_COND_EXPR:
11343788 1096 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1097 list(kid);
1098 break;
1099 default:
1100 case OP_MATCH:
8782bef2 1101 case OP_QR:
79072805
LW
1102 case OP_SUBST:
1103 case OP_NULL:
11343788 1104 if (!(o->op_flags & OPf_KIDS))
79072805 1105 break;
11343788
MB
1106 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1107 list(cBINOPo->op_first);
1108 return gen_constant_list(o);
79072805
LW
1109 }
1110 case OP_LIST:
11343788 1111 listkids(o);
79072805
LW
1112 break;
1113 case OP_LEAVE:
1114 case OP_LEAVETRY:
5dc0d613 1115 kid = cLISTOPo->op_first;
54310121 1116 list(kid);
1117 while (kid = kid->op_sibling) {
1118 if (kid->op_sibling)
1119 scalarvoid(kid);
1120 else
1121 list(kid);
1122 }
3280af22 1123 WITH_THR(PL_curcop = &PL_compiling);
54310121 1124 break;
748a9306 1125 case OP_SCOPE:
79072805 1126 case OP_LINESEQ:
11343788 1127 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
1128 if (kid->op_sibling)
1129 scalarvoid(kid);
1130 else
1131 list(kid);
1132 }
3280af22 1133 WITH_THR(PL_curcop = &PL_compiling);
79072805 1134 break;
c90c0ff4 1135 case OP_REQUIRE:
1136 /* all requires must return a boolean value */
5196be3e
MB
1137 o->op_flags &= ~OPf_WANT;
1138 return scalar(o);
79072805 1139 }
11343788 1140 return o;
79072805
LW
1141}
1142
1143OP *
864dbfa3 1144Perl_scalarseq(pTHX_ OP *o)
79072805
LW
1145{
1146 OP *kid;
1147
11343788
MB
1148 if (o) {
1149 if (o->op_type == OP_LINESEQ ||
1150 o->op_type == OP_SCOPE ||
1151 o->op_type == OP_LEAVE ||
1152 o->op_type == OP_LEAVETRY)
463ee0b2 1153 {
0f15f207 1154 dTHR;
11343788 1155 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
ed6116ce 1156 if (kid->op_sibling) {
463ee0b2 1157 scalarvoid(kid);
ed6116ce 1158 }
463ee0b2 1159 }
3280af22 1160 PL_curcop = &PL_compiling;
79072805 1161 }
11343788 1162 o->op_flags &= ~OPf_PARENS;
3280af22 1163 if (PL_hints & HINT_BLOCK_SCOPE)
11343788 1164 o->op_flags |= OPf_PARENS;
79072805 1165 }
8990e307 1166 else
11343788
MB
1167 o = newOP(OP_STUB, 0);
1168 return o;
79072805
LW
1169}
1170
76e3520e 1171STATIC OP *
cea2e8a9 1172S_modkids(pTHX_ OP *o, I32 type)
79072805
LW
1173{
1174 OP *kid;
11343788
MB
1175 if (o && o->op_flags & OPf_KIDS) {
1176 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2 1177 mod(kid, type);
79072805 1178 }
11343788 1179 return o;
79072805
LW
1180}
1181
79072805 1182OP *
864dbfa3 1183Perl_mod(pTHX_ OP *o, I32 type)
79072805 1184{
11343788 1185 dTHR;
79072805
LW
1186 OP *kid;
1187 SV *sv;
2d8e6c8d 1188 STRLEN n_a;
79072805 1189
3280af22 1190 if (!o || PL_error_count)
11343788 1191 return o;
79072805 1192
11343788 1193 switch (o->op_type) {
68dc0745 1194 case OP_UNDEF:
3280af22 1195 PL_modcount++;
5dc0d613 1196 return o;
a0d0e21e 1197 case OP_CONST:
11343788 1198 if (!(o->op_private & (OPpCONST_ARYBASE)))
a0d0e21e 1199 goto nomod;
3280af22
NIS
1200 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1201 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1202 PL_eval_start = 0;
a0d0e21e
LW
1203 }
1204 else if (!type) {
3280af22
NIS
1205 SAVEI32(PL_compiling.cop_arybase);
1206 PL_compiling.cop_arybase = 0;
a0d0e21e
LW
1207 }
1208 else if (type == OP_REFGEN)
1209 goto nomod;
1210 else
cea2e8a9 1211 Perl_croak(aTHX_ "That use of $[ is unsupported");
a0d0e21e 1212 break;
5f05dabc 1213 case OP_STUB:
5196be3e 1214 if (o->op_flags & OPf_PARENS)
5f05dabc 1215 break;
1216 goto nomod;
a0d0e21e
LW
1217 case OP_ENTERSUB:
1218 if ((type == OP_UNDEF || type == OP_REFGEN) &&
11343788
MB
1219 !(o->op_flags & OPf_STACKED)) {
1220 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1221 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1222 assert(cUNOPo->op_first->op_type == OP_NULL);
1223 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
79072805
LW
1224 break;
1225 }
1226 /* FALL THROUGH */
1227 default:
a0d0e21e
LW
1228 nomod:
1229 /* grep, foreach, subcalls, refgen */
1230 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1231 break;
cea2e8a9 1232 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
638bc118 1233 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
22c35a8c
GS
1234 ? "do block" : PL_op_desc[o->op_type]),
1235 type ? PL_op_desc[type] : "local"));
11343788 1236 return o;
79072805 1237
a0d0e21e
LW
1238 case OP_PREINC:
1239 case OP_PREDEC:
1240 case OP_POW:
1241 case OP_MULTIPLY:
1242 case OP_DIVIDE:
1243 case OP_MODULO:
1244 case OP_REPEAT:
1245 case OP_ADD:
1246 case OP_SUBTRACT:
1247 case OP_CONCAT:
1248 case OP_LEFT_SHIFT:
1249 case OP_RIGHT_SHIFT:
1250 case OP_BIT_AND:
1251 case OP_BIT_XOR:
1252 case OP_BIT_OR:
1253 case OP_I_MULTIPLY:
1254 case OP_I_DIVIDE:
1255 case OP_I_MODULO:
1256 case OP_I_ADD:
1257 case OP_I_SUBTRACT:
11343788 1258 if (!(o->op_flags & OPf_STACKED))
a0d0e21e 1259 goto nomod;
3280af22 1260 PL_modcount++;
a0d0e21e
LW
1261 break;
1262
79072805 1263 case OP_COND_EXPR:
11343788 1264 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2 1265 mod(kid, type);
79072805
LW
1266 break;
1267
1268 case OP_RV2AV:
1269 case OP_RV2HV:
93af7a87 1270 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1271 Perl_croak(aTHX_ "Can't localize through a reference");
11343788 1272 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3280af22 1273 PL_modcount = 10000;
11343788 1274 return o; /* Treat \(@foo) like ordinary list. */
748a9306
LW
1275 }
1276 /* FALL THROUGH */
79072805 1277 case OP_RV2GV:
5dc0d613 1278 if (scalar_mod_type(o, type))
3fe9a6f1 1279 goto nomod;
11343788 1280 ref(cUNOPo->op_first, o->op_type);
79072805
LW
1281 /* FALL THROUGH */
1282 case OP_AASSIGN:
1283 case OP_ASLICE:
1284 case OP_HSLICE:
93a17b20
LW
1285 case OP_NEXTSTATE:
1286 case OP_DBSTATE:
a0d0e21e
LW
1287 case OP_REFGEN:
1288 case OP_CHOMP:
3280af22 1289 PL_modcount = 10000;
79072805 1290 break;
463ee0b2 1291 case OP_RV2SV:
11343788 1292 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1293 Perl_croak(aTHX_ "Can't localize through a reference");
aeea060c 1294 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1295 /* FALL THROUGH */
79072805 1296 case OP_GV:
463ee0b2 1297 case OP_AV2ARYLEN:
3280af22 1298 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1299 case OP_SASSIGN:
bf4b1e52
GS
1300 case OP_ANDASSIGN:
1301 case OP_ORASSIGN:
8990e307 1302 case OP_AELEMFAST:
3280af22 1303 PL_modcount++;
8990e307
LW
1304 break;
1305
748a9306
LW
1306 case OP_PADAV:
1307 case OP_PADHV:
3280af22 1308 PL_modcount = 10000;
5196be3e
MB
1309 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1310 return o; /* Treat \(@foo) like ordinary list. */
1311 if (scalar_mod_type(o, type))
3fe9a6f1 1312 goto nomod;
748a9306
LW
1313 /* FALL THROUGH */
1314 case OP_PADSV:
3280af22 1315 PL_modcount++;
748a9306 1316 if (!type)
cea2e8a9 1317 Perl_croak(aTHX_ "Can't localize lexical variable %s",
2d8e6c8d 1318 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
463ee0b2
LW
1319 break;
1320
554b3eca 1321#ifdef USE_THREADS
2faa37cc 1322 case OP_THREADSV:
533c011a 1323 PL_modcount++; /* XXX ??? */
554b3eca
MB
1324 break;
1325#endif /* USE_THREADS */
1326
748a9306
LW
1327 case OP_PUSHMARK:
1328 break;
a0d0e21e 1329
69969c6f
SB
1330 case OP_KEYS:
1331 if (type != OP_SASSIGN)
1332 goto nomod;
5d82c453
GA
1333 goto lvalue_func;
1334 case OP_SUBSTR:
1335 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1336 goto nomod;
5f05dabc 1337 /* FALL THROUGH */
a0d0e21e 1338 case OP_POS:
463ee0b2 1339 case OP_VEC:
5d82c453 1340 lvalue_func:
11343788
MB
1341 pad_free(o->op_targ);
1342 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
5dc0d613 1343 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
11343788
MB
1344 if (o->op_flags & OPf_KIDS)
1345 mod(cBINOPo->op_first->op_sibling, type);
463ee0b2 1346 break;
a0d0e21e 1347
463ee0b2
LW
1348 case OP_AELEM:
1349 case OP_HELEM:
11343788 1350 ref(cBINOPo->op_first, o->op_type);
68dc0745 1351 if (type == OP_ENTERSUB &&
5dc0d613
MB
1352 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1353 o->op_private |= OPpLVAL_DEFER;
3280af22 1354 PL_modcount++;
463ee0b2
LW
1355 break;
1356
1357 case OP_SCOPE:
1358 case OP_LEAVE:
1359 case OP_ENTER:
11343788
MB
1360 if (o->op_flags & OPf_KIDS)
1361 mod(cLISTOPo->op_last, type);
a0d0e21e
LW
1362 break;
1363
1364 case OP_NULL:
638bc118
GS
1365 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1366 goto nomod;
1367 else if (!(o->op_flags & OPf_KIDS))
463ee0b2 1368 break;
11343788
MB
1369 if (o->op_targ != OP_LIST) {
1370 mod(cBINOPo->op_first, type);
a0d0e21e
LW
1371 break;
1372 }
1373 /* FALL THROUGH */
463ee0b2 1374 case OP_LIST:
11343788 1375 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1376 mod(kid, type);
1377 break;
1378 }
11343788 1379 o->op_flags |= OPf_MOD;
a0d0e21e
LW
1380
1381 if (type == OP_AASSIGN || type == OP_SASSIGN)
11343788 1382 o->op_flags |= OPf_SPECIAL|OPf_REF;
a0d0e21e 1383 else if (!type) {
11343788
MB
1384 o->op_private |= OPpLVAL_INTRO;
1385 o->op_flags &= ~OPf_SPECIAL;
3280af22 1386 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1387 }
a0d0e21e 1388 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
11343788
MB
1389 o->op_flags |= OPf_REF;
1390 return o;
463ee0b2
LW
1391}
1392
864dbfa3 1393STATIC bool
cea2e8a9 1394S_scalar_mod_type(pTHX_ OP *o, I32 type)
3fe9a6f1 1395{
1396 switch (type) {
1397 case OP_SASSIGN:
5196be3e 1398 if (o->op_type == OP_RV2GV)
3fe9a6f1 1399 return FALSE;
1400 /* FALL THROUGH */
1401 case OP_PREINC:
1402 case OP_PREDEC:
1403 case OP_POSTINC:
1404 case OP_POSTDEC:
1405 case OP_I_PREINC:
1406 case OP_I_PREDEC:
1407 case OP_I_POSTINC:
1408 case OP_I_POSTDEC:
1409 case OP_POW:
1410 case OP_MULTIPLY:
1411 case OP_DIVIDE:
1412 case OP_MODULO:
1413 case OP_REPEAT:
1414 case OP_ADD:
1415 case OP_SUBTRACT:
1416 case OP_I_MULTIPLY:
1417 case OP_I_DIVIDE:
1418 case OP_I_MODULO:
1419 case OP_I_ADD:
1420 case OP_I_SUBTRACT:
1421 case OP_LEFT_SHIFT:
1422 case OP_RIGHT_SHIFT:
1423 case OP_BIT_AND:
1424 case OP_BIT_XOR:
1425 case OP_BIT_OR:
1426 case OP_CONCAT:
1427 case OP_SUBST:
1428 case OP_TRANS:
49e9fbe6
GS
1429 case OP_READ:
1430 case OP_SYSREAD:
1431 case OP_RECV:
bf4b1e52
GS
1432 case OP_ANDASSIGN:
1433 case OP_ORASSIGN:
3fe9a6f1 1434 return TRUE;
1435 default:
1436 return FALSE;
1437 }
1438}
1439
35cd451c 1440STATIC bool
cea2e8a9 1441S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
35cd451c
GS
1442{
1443 switch (o->op_type) {
1444 case OP_PIPE_OP:
1445 case OP_SOCKPAIR:
1446 if (argnum == 2)
1447 return TRUE;
1448 /* FALL THROUGH */
1449 case OP_SYSOPEN:
1450 case OP_OPEN:
ded8aa31 1451 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
35cd451c
GS
1452 case OP_SOCKET:
1453 case OP_OPEN_DIR:
1454 case OP_ACCEPT:
1455 if (argnum == 1)
1456 return TRUE;
1457 /* FALL THROUGH */
1458 default:
1459 return FALSE;
1460 }
1461}
1462
463ee0b2 1463OP *
864dbfa3 1464Perl_refkids(pTHX_ OP *o, I32 type)
463ee0b2
LW
1465{
1466 OP *kid;
11343788
MB
1467 if (o && o->op_flags & OPf_KIDS) {
1468 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1469 ref(kid, type);
1470 }
11343788 1471 return o;
463ee0b2
LW
1472}
1473
1474OP *
864dbfa3 1475Perl_ref(pTHX_ OP *o, I32 type)
463ee0b2
LW
1476{
1477 OP *kid;
463ee0b2 1478
3280af22 1479 if (!o || PL_error_count)
11343788 1480 return o;
463ee0b2 1481
11343788 1482 switch (o->op_type) {
a0d0e21e 1483 case OP_ENTERSUB:
e55aaa0e 1484 if ((type == OP_DEFINED || type == OP_LOCK) &&
11343788
MB
1485 !(o->op_flags & OPf_STACKED)) {
1486 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1487 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1488 assert(cUNOPo->op_first->op_type == OP_NULL);
1489 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1490 o->op_flags |= OPf_SPECIAL;
8990e307
LW
1491 }
1492 break;
aeea060c 1493
463ee0b2 1494 case OP_COND_EXPR:
11343788 1495 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2
LW
1496 ref(kid, type);
1497 break;
8990e307 1498 case OP_RV2SV:
35cd451c
GS
1499 if (type == OP_DEFINED)
1500 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1501 ref(cUNOPo->op_first, o->op_type);
4633a7c4
LW
1502 /* FALL THROUGH */
1503 case OP_PADSV:
5f05dabc 1504 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1505 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1506 : type == OP_RV2HV ? OPpDEREF_HV
1507 : OPpDEREF_SV);
11343788 1508 o->op_flags |= OPf_MOD;
a0d0e21e 1509 }
8990e307
LW
1510 break;
1511
2faa37cc 1512 case OP_THREADSV:
a863c7d1
MB
1513 o->op_flags |= OPf_MOD; /* XXX ??? */
1514 break;
1515
463ee0b2
LW
1516 case OP_RV2AV:
1517 case OP_RV2HV:
aeea060c 1518 o->op_flags |= OPf_REF;
8990e307 1519 /* FALL THROUGH */
463ee0b2 1520 case OP_RV2GV:
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);
463ee0b2 1524 break;
8990e307 1525
463ee0b2
LW
1526 case OP_PADAV:
1527 case OP_PADHV:
aeea060c 1528 o->op_flags |= OPf_REF;
79072805 1529 break;
aeea060c 1530
8990e307 1531 case OP_SCALAR:
79072805 1532 case OP_NULL:
11343788 1533 if (!(o->op_flags & OPf_KIDS))
463ee0b2 1534 break;
11343788 1535 ref(cBINOPo->op_first, type);
79072805
LW
1536 break;
1537 case OP_AELEM:
1538 case OP_HELEM:
11343788 1539 ref(cBINOPo->op_first, o->op_type);
5f05dabc 1540 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1541 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1542 : type == OP_RV2HV ? OPpDEREF_HV
1543 : OPpDEREF_SV);
11343788 1544 o->op_flags |= OPf_MOD;
8990e307 1545 }
79072805
LW
1546 break;
1547
463ee0b2 1548 case OP_SCOPE:
79072805
LW
1549 case OP_LEAVE:
1550 case OP_ENTER:
8990e307 1551 case OP_LIST:
11343788 1552 if (!(o->op_flags & OPf_KIDS))
79072805 1553 break;
11343788 1554 ref(cLISTOPo->op_last, type);
79072805 1555 break;
a0d0e21e
LW
1556 default:
1557 break;
79072805 1558 }
11343788 1559 return scalar(o);
8990e307 1560
79072805
LW
1561}
1562
1563OP *
864dbfa3 1564Perl_my(pTHX_ OP *o)
93a17b20
LW
1565{
1566 OP *kid;
93a17b20
LW
1567 I32 type;
1568
3280af22 1569 if (!o || PL_error_count)
11343788 1570 return o;
93a17b20 1571
11343788 1572 type = o->op_type;
93a17b20 1573 if (type == OP_LIST) {
11343788 1574 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
93a17b20 1575 my(kid);
dab48698 1576 } else if (type == OP_UNDEF) {
7766148a 1577 return o;
dab48698 1578 } else if (type != OP_PADSV &&
93a17b20
LW
1579 type != OP_PADAV &&
1580 type != OP_PADHV &&
1581 type != OP_PUSHMARK)
1582 {
cea2e8a9 1583 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
11343788 1584 return o;
93a17b20 1585 }
11343788
MB
1586 o->op_flags |= OPf_MOD;
1587 o->op_private |= OPpLVAL_INTRO;
1588 return o;
93a17b20
LW
1589}
1590
1591OP *
864dbfa3 1592Perl_sawparens(pTHX_ OP *o)
79072805
LW
1593{
1594 if (o)
1595 o->op_flags |= OPf_PARENS;
1596 return o;
1597}
1598
1599OP *
864dbfa3 1600Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
79072805 1601{
d008e5eb 1602 dTHR;
11343788 1603 OP *o;
79072805 1604
599cee73
PM
1605 if (ckWARN(WARN_UNSAFE) &&
1606 (left->op_type == OP_RV2AV ||
1607 left->op_type == OP_RV2HV ||
1608 left->op_type == OP_PADAV ||
1609 left->op_type == OP_PADHV)) {
22c35a8c 1610 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
599cee73
PM
1611 right->op_type == OP_TRANS)
1612 ? right->op_type : OP_MATCH];
1613 char *sample = ((left->op_type == OP_RV2AV ||
1614 left->op_type == OP_PADAV)
1615 ? "@array" : "%hash");
cea2e8a9 1616 Perl_warner(aTHX_ WARN_UNSAFE,
599cee73
PM
1617 "Applying %s to %s will act on scalar(%s)",
1618 desc, sample, sample);
2ae324a7 1619 }
1620
79072805
LW
1621 if (right->op_type == OP_MATCH ||
1622 right->op_type == OP_SUBST ||
1623 right->op_type == OP_TRANS) {
1624 right->op_flags |= OPf_STACKED;
1625 if (right->op_type != OP_MATCH)
463ee0b2 1626 left = mod(left, right->op_type);
79072805 1627 if (right->op_type == OP_TRANS)
11343788 1628 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
79072805 1629 else
11343788 1630 o = prepend_elem(right->op_type, scalar(left), right);
79072805 1631 if (type == OP_NOT)
11343788
MB
1632 return newUNOP(OP_NOT, 0, scalar(o));
1633 return o;
79072805
LW
1634 }
1635 else
1636 return bind_match(type, left,
1637 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1638}
1639
1640OP *
864dbfa3 1641Perl_invert(pTHX_ OP *o)
79072805 1642{
11343788
MB
1643 if (!o)
1644 return o;
79072805 1645 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
11343788 1646 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
79072805
LW
1647}
1648
1649OP *
864dbfa3 1650Perl_scope(pTHX_ OP *o)
79072805
LW
1651{
1652 if (o) {
3280af22 1653 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
463ee0b2
LW
1654 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1655 o->op_type = OP_LEAVE;
22c35a8c 1656 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
463ee0b2
LW
1657 }
1658 else {
1659 if (o->op_type == OP_LINESEQ) {
1660 OP *kid;
1661 o->op_type = OP_SCOPE;
22c35a8c 1662 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
463ee0b2 1663 kid = ((LISTOP*)o)->op_first;
748a9306
LW
1664 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1665 SvREFCNT_dec(((COP*)kid)->cop_filegv);
8990e307 1666 null(kid);
748a9306 1667 }
463ee0b2
LW
1668 }
1669 else
748a9306 1670 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
463ee0b2 1671 }
79072805
LW
1672 }
1673 return o;
1674}
1675
b3ac6de7 1676void
864dbfa3 1677Perl_save_hints(pTHX)
b3ac6de7 1678{
3280af22
NIS
1679 SAVEI32(PL_hints);
1680 SAVESPTR(GvHV(PL_hintgv));
1681 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1682 SAVEFREESV(GvHV(PL_hintgv));
b3ac6de7
IZ
1683}
1684
a0d0e21e 1685int
864dbfa3 1686Perl_block_start(pTHX_ int full)
79072805 1687{
11343788 1688 dTHR;
3280af22 1689 int retval = PL_savestack_ix;
b3ac6de7 1690
3280af22 1691 SAVEI32(PL_comppad_name_floor);
55497cff 1692 if (full) {
3280af22
NIS
1693 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1694 PL_comppad_name_floor = PL_comppad_name_fill;
55497cff 1695 else
3280af22
NIS
1696 PL_comppad_name_floor = 0;
1697 }
1698 SAVEI32(PL_min_intro_pending);
1699 SAVEI32(PL_max_intro_pending);
1700 PL_min_intro_pending = 0;
1701 SAVEI32(PL_comppad_name_fill);
1702 SAVEI32(PL_padix_floor);
1703 PL_padix_floor = PL_padix;
1704 PL_pad_reset_pending = FALSE;
b3ac6de7 1705 SAVEHINTS();
3280af22 1706 PL_hints &= ~HINT_BLOCK_SCOPE;
e24b16f9 1707 SAVEPPTR(PL_compiling.cop_warnings);
599cee73
PM
1708 if (PL_compiling.cop_warnings != WARN_ALL &&
1709 PL_compiling.cop_warnings != WARN_NONE) {
1710 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1711 SAVEFREESV(PL_compiling.cop_warnings) ;
1712 }
1713
1714
a0d0e21e
LW
1715 return retval;
1716}
1717
1718OP*
864dbfa3 1719Perl_block_end(pTHX_ I32 floor, OP *seq)
a0d0e21e 1720{
11343788 1721 dTHR;
3280af22 1722 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
a0d0e21e 1723 OP* retval = scalarseq(seq);
a0d0e21e 1724 LEAVE_SCOPE(floor);
3280af22 1725 PL_pad_reset_pending = FALSE;
e24b16f9 1726 PL_compiling.op_private = PL_hints;
a0d0e21e 1727 if (needblockscope)
3280af22
NIS
1728 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1729 pad_leavemy(PL_comppad_name_fill);
1730 PL_cop_seqmax++;
a0d0e21e
LW
1731 return retval;
1732}
1733
76e3520e 1734STATIC OP *
cea2e8a9 1735S_newDEFSVOP(pTHX)
54b9620d
MB
1736{
1737#ifdef USE_THREADS
1738 OP *o = newOP(OP_THREADSV, 0);
1739 o->op_targ = find_threadsv("_");
1740 return o;
1741#else
3280af22 1742 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
54b9620d
MB
1743#endif /* USE_THREADS */
1744}
1745
a0d0e21e 1746void
864dbfa3 1747Perl_newPROG(pTHX_ OP *o)
a0d0e21e 1748{
11343788 1749 dTHR;
3280af22 1750 if (PL_in_eval) {
b295d113
TH
1751 if (PL_eval_root)
1752 return;
faef0170
HS
1753 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1754 ((PL_in_eval & EVAL_KEEPERR)
1755 ? OPf_SPECIAL : 0), o);
3280af22
NIS
1756 PL_eval_start = linklist(PL_eval_root);
1757 PL_eval_root->op_next = 0;
1758 peep(PL_eval_start);
a0d0e21e
LW
1759 }
1760 else {
5dc0d613 1761 if (!o)
a0d0e21e 1762 return;
3280af22
NIS
1763 PL_main_root = scope(sawparens(scalarvoid(o)));
1764 PL_curcop = &PL_compiling;
1765 PL_main_start = LINKLIST(PL_main_root);
1766 PL_main_root->op_next = 0;
1767 peep(PL_main_start);
1768 PL_compcv = 0;
3841441e 1769
4fdae800 1770 /* Register with debugger */
84902520 1771 if (PERLDB_INTER) {
864dbfa3 1772 CV *cv = get_cv("DB::postponed", FALSE);
3841441e
CS
1773 if (cv) {
1774 dSP;
924508f0 1775 PUSHMARK(SP);
3280af22 1776 XPUSHs((SV*)PL_compiling.cop_filegv);
3841441e 1777 PUTBACK;
864dbfa3 1778 call_sv((SV*)cv, G_DISCARD);
3841441e
CS
1779 }
1780 }
79072805 1781 }
79072805
LW
1782}
1783
1784OP *
864dbfa3 1785Perl_localize(pTHX_ OP *o, I32 lex)
79072805
LW
1786{
1787 if (o->op_flags & OPf_PARENS)
1788 list(o);
8990e307 1789 else {
d008e5eb 1790 dTHR;
599cee73 1791 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
8990e307 1792 char *s;
834a4ddd 1793 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
a0d0e21e 1794 if (*s == ';' || *s == '=')
cea2e8a9 1795 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
599cee73 1796 lex ? "my" : "local");
8990e307
LW
1797 }
1798 }
3280af22
NIS
1799 PL_in_my = FALSE;
1800 PL_in_my_stash = Nullhv;
93a17b20
LW
1801 if (lex)
1802 return my(o);
1803 else
463ee0b2 1804 return mod(o, OP_NULL); /* a bit kludgey */
79072805
LW
1805}
1806
1807OP *
864dbfa3 1808Perl_jmaybe(pTHX_ OP *o)
79072805
LW
1809{
1810 if (o->op_type == OP_LIST) {
554b3eca
MB
1811 OP *o2;
1812#ifdef USE_THREADS
2faa37cc 1813 o2 = newOP(OP_THREADSV, 0);
54b9620d 1814 o2->op_targ = find_threadsv(";");
554b3eca
MB
1815#else
1816 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1817#endif /* USE_THREADS */
1818 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
79072805
LW
1819 }
1820 return o;
1821}
1822
1823OP *
864dbfa3 1824Perl_fold_constants(pTHX_ register OP *o)
79072805 1825{
11343788 1826 dTHR;
79072805
LW
1827 register OP *curop;
1828 I32 type = o->op_type;
748a9306 1829 SV *sv;
79072805 1830
22c35a8c 1831 if (PL_opargs[type] & OA_RETSCALAR)
79072805 1832 scalar(o);
22c35a8c 1833 if (PL_opargs[type] & OA_TARGET)
ed6116ce 1834 o->op_targ = pad_alloc(type, SVs_PADTMP);
79072805 1835
eac055e9
GS
1836 /* integerize op, unless it happens to be C<-foo>.
1837 * XXX should pp_i_negate() do magic string negation instead? */
1838 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
1839 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
1840 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
1841 {
22c35a8c 1842 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
eac055e9 1843 }
85e6fe83 1844
22c35a8c 1845 if (!(PL_opargs[type] & OA_FOLDCONST))
79072805
LW
1846 goto nope;
1847
de939608 1848 switch (type) {
7a52d87a
GS
1849 case OP_NEGATE:
1850 /* XXX might want a ck_negate() for this */
1851 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
1852 break;
de939608
CS
1853 case OP_SPRINTF:
1854 case OP_UCFIRST:
1855 case OP_LCFIRST:
1856 case OP_UC:
1857 case OP_LC:
69dcf70c
MB
1858 case OP_SLT:
1859 case OP_SGT:
1860 case OP_SLE:
1861 case OP_SGE:
1862 case OP_SCMP:
1863
de939608
CS
1864 if (o->op_private & OPpLOCALE)
1865 goto nope;
1866 }
1867
3280af22 1868 if (PL_error_count)
a0d0e21e
LW
1869 goto nope; /* Don't try to run w/ errors */
1870
79072805 1871 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
93a17b20 1872 if (curop->op_type != OP_CONST &&
7a52d87a
GS
1873 curop->op_type != OP_LIST &&
1874 curop->op_type != OP_SCALAR &&
1875 curop->op_type != OP_NULL &&
1876 curop->op_type != OP_PUSHMARK)
1877 {
79072805
LW
1878 goto nope;
1879 }
1880 }
1881
1882 curop = LINKLIST(o);
1883 o->op_next = 0;
533c011a 1884 PL_op = curop;
cea2e8a9 1885 CALLRUNOPS(aTHX);
3280af22 1886 sv = *(PL_stack_sp--);
748a9306 1887 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
79072805 1888 pad_swipe(o->op_targ);
748a9306
LW
1889 else if (SvTEMP(sv)) { /* grab mortal temp? */
1890 (void)SvREFCNT_inc(sv);
1891 SvTEMP_off(sv);
85e6fe83 1892 }
79072805
LW
1893 op_free(o);
1894 if (type == OP_RV2GV)
b1cb66bf 1895 return newGVOP(OP_GV, 0, (GV*)sv);
748a9306 1896 else {
ee580363
GS
1897 /* try to smush double to int, but don't smush -2.0 to -2 */
1898 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
1899 type != OP_NEGATE)
1900 {
748a9306 1901 IV iv = SvIV(sv);
65202027 1902 if ((NV)iv == SvNV(sv)) {
748a9306
LW
1903 SvREFCNT_dec(sv);
1904 sv = newSViv(iv);
1905 }
b1cb66bf 1906 else
1907 SvIOK_off(sv); /* undo SvIV() damage */
748a9306
LW
1908 }
1909 return newSVOP(OP_CONST, 0, sv);
1910 }
aeea060c 1911
79072805 1912 nope:
22c35a8c 1913 if (!(PL_opargs[type] & OA_OTHERINT))
79072805 1914 return o;
79072805 1915
3280af22 1916 if (!(PL_hints & HINT_INTEGER)) {
a0d0e21e 1917 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
85e6fe83
LW
1918 return o;
1919
1920 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1921 if (curop->op_type == OP_CONST) {
b1cb66bf 1922 if (SvIOK(((SVOP*)curop)->op_sv))
85e6fe83
LW
1923 continue;
1924 return o;
1925 }
22c35a8c 1926 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
79072805
LW
1927 continue;
1928 return o;
1929 }
22c35a8c 1930 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
79072805
LW
1931 }
1932
79072805
LW
1933 return o;
1934}
1935
1936OP *
864dbfa3 1937Perl_gen_constant_list(pTHX_ register OP *o)
79072805 1938{
11343788 1939 dTHR;
79072805 1940 register OP *curop;
3280af22 1941 I32 oldtmps_floor = PL_tmps_floor;
79072805 1942
a0d0e21e 1943 list(o);
3280af22 1944 if (PL_error_count)
a0d0e21e
LW
1945 return o; /* Don't attempt to run with errors */
1946
533c011a 1947 PL_op = curop = LINKLIST(o);
a0d0e21e 1948 o->op_next = 0;
cea2e8a9
GS
1949 pp_pushmark();
1950 CALLRUNOPS(aTHX);
533c011a 1951 PL_op = curop;
cea2e8a9 1952 pp_anonlist();
3280af22 1953 PL_tmps_floor = oldtmps_floor;
79072805
LW
1954
1955 o->op_type = OP_RV2AV;
22c35a8c 1956 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
79072805 1957 curop = ((UNOP*)o)->op_first;
3280af22 1958 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
79072805 1959 op_free(curop);
79072805
LW
1960 linklist(o);
1961 return list(o);
1962}
1963
1964OP *
864dbfa3 1965Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
79072805
LW
1966{
1967 OP *kid;
a0d0e21e 1968 OP *last = 0;
79072805 1969
11343788
MB
1970 if (!o || o->op_type != OP_LIST)
1971 o = newLISTOP(OP_LIST, 0, o, Nullop);
748a9306 1972 else
5dc0d613 1973 o->op_flags &= ~OPf_WANT;
79072805 1974
22c35a8c 1975 if (!(PL_opargs[type] & OA_MARK))
11343788 1976 null(cLISTOPo->op_first);
8990e307 1977
11343788 1978 o->op_type = type;
22c35a8c 1979 o->op_ppaddr = PL_ppaddr[type];
11343788 1980 o->op_flags |= flags;
79072805 1981
11343788
MB
1982 o = CHECKOP(type, o);
1983 if (o->op_type != type)
1984 return o;
79072805 1985
11343788 1986 if (cLISTOPo->op_children < 7) {
79072805 1987 /* XXX do we really need to do this if we're done appending?? */
11343788 1988 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805 1989 last = kid;
11343788 1990 cLISTOPo->op_last = last; /* in case check substituted last arg */
79072805
LW
1991 }
1992
11343788 1993 return fold_constants(o);
79072805
LW
1994}
1995
1996/* List constructors */
1997
1998OP *
864dbfa3 1999Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2000{
2001 if (!first)
2002 return last;
8990e307
LW
2003
2004 if (!last)
79072805 2005 return first;
8990e307 2006
a0d0e21e
LW
2007 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2008 return newLISTOP(type, 0, first, last);
79072805 2009
a0d0e21e
LW
2010 if (first->op_flags & OPf_KIDS)
2011 ((LISTOP*)first)->op_last->op_sibling = last;
2012 else {
2013 first->op_flags |= OPf_KIDS;
2014 ((LISTOP*)first)->op_first = last;
2015 }
2016 ((LISTOP*)first)->op_last = last;
2017 ((LISTOP*)first)->op_children++;
2018 return first;
79072805
LW
2019}
2020
2021OP *
864dbfa3 2022Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
79072805
LW
2023{
2024 if (!first)
2025 return (OP*)last;
8990e307
LW
2026
2027 if (!last)
79072805 2028 return (OP*)first;
8990e307
LW
2029
2030 if (first->op_type != type)
79072805 2031 return prepend_elem(type, (OP*)first, (OP*)last);
8990e307
LW
2032
2033 if (last->op_type != type)
79072805
LW
2034 return append_elem(type, (OP*)first, (OP*)last);
2035
2036 first->op_last->op_sibling = last->op_first;
2037 first->op_last = last->op_last;
2038 first->op_children += last->op_children;
2039 if (first->op_children)
acb74605 2040 first->op_flags |= OPf_KIDS;
b7dc083c
NIS
2041
2042#ifdef PL_OP_SLAB_ALLOC
2043#else
2044 Safefree(last);
2045#endif
79072805
LW
2046 return (OP*)first;
2047}
2048
2049OP *
864dbfa3 2050Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2051{
2052 if (!first)
2053 return last;
8990e307
LW
2054
2055 if (!last)
79072805 2056 return first;
8990e307
LW
2057
2058 if (last->op_type == type) {
2059 if (type == OP_LIST) { /* already a PUSHMARK there */
2060 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2061 ((LISTOP*)last)->op_first->op_sibling = first;
2062 }
2063 else {
2064 if (!(last->op_flags & OPf_KIDS)) {
2065 ((LISTOP*)last)->op_last = first;
2066 last->op_flags |= OPf_KIDS;
2067 }
2068 first->op_sibling = ((LISTOP*)last)->op_first;
2069 ((LISTOP*)last)->op_first = first;
79072805 2070 }
79072805
LW
2071 ((LISTOP*)last)->op_children++;
2072 return last;
2073 }
2074
2075 return newLISTOP(type, 0, first, last);
2076}
2077
2078/* Constructors */
2079
2080OP *
864dbfa3 2081Perl_newNULLLIST(pTHX)
79072805 2082{
8990e307
LW
2083 return newOP(OP_STUB, 0);
2084}
2085
2086OP *
864dbfa3 2087Perl_force_list(pTHX_ OP *o)
8990e307 2088{
11343788
MB
2089 if (!o || o->op_type != OP_LIST)
2090 o = newLISTOP(OP_LIST, 0, o, Nullop);
2091 null(o);
2092 return o;
79072805
LW
2093}
2094
2095OP *
864dbfa3 2096Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2097{
2098 LISTOP *listop;
2099
b7dc083c 2100 NewOp(1101, listop, 1, LISTOP);
79072805
LW
2101
2102 listop->op_type = type;
22c35a8c 2103 listop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2104 listop->op_children = (first != 0) + (last != 0);
2105 listop->op_flags = flags;
79072805
LW
2106
2107 if (!last && first)
2108 last = first;
2109 else if (!first && last)
2110 first = last;
8990e307
LW
2111 else if (first)
2112 first->op_sibling = last;
79072805
LW
2113 listop->op_first = first;
2114 listop->op_last = last;
8990e307
LW
2115 if (type == OP_LIST) {
2116 OP* pushop;
2117 pushop = newOP(OP_PUSHMARK, 0);
2118 pushop->op_sibling = first;
2119 listop->op_first = pushop;
2120 listop->op_flags |= OPf_KIDS;
2121 if (!last)
2122 listop->op_last = pushop;
2123 }
2124 else if (listop->op_children)
2125 listop->op_flags |= OPf_KIDS;
79072805
LW
2126
2127 return (OP*)listop;
2128}
2129
2130OP *
864dbfa3 2131Perl_newOP(pTHX_ I32 type, I32 flags)
79072805 2132{
11343788 2133 OP *o;
b7dc083c 2134 NewOp(1101, o, 1, OP);
11343788 2135 o->op_type = type;
22c35a8c 2136 o->op_ppaddr = PL_ppaddr[type];
11343788 2137 o->op_flags = flags;
79072805 2138
11343788
MB
2139 o->op_next = o;
2140 o->op_private = 0 + (flags >> 8);
22c35a8c 2141 if (PL_opargs[type] & OA_RETSCALAR)
11343788 2142 scalar(o);
22c35a8c 2143 if (PL_opargs[type] & OA_TARGET)
11343788
MB
2144 o->op_targ = pad_alloc(type, SVs_PADTMP);
2145 return CHECKOP(type, o);
79072805
LW
2146}
2147
2148OP *
864dbfa3 2149Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
79072805
LW
2150{
2151 UNOP *unop;
2152
93a17b20 2153 if (!first)
aeea060c 2154 first = newOP(OP_STUB, 0);
22c35a8c 2155 if (PL_opargs[type] & OA_MARK)
8990e307 2156 first = force_list(first);
93a17b20 2157
b7dc083c 2158 NewOp(1101, unop, 1, UNOP);
79072805 2159 unop->op_type = type;
22c35a8c 2160 unop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2161 unop->op_first = first;
2162 unop->op_flags = flags | OPf_KIDS;
c07a80fd 2163 unop->op_private = 1 | (flags >> 8);
e50aee73 2164 unop = (UNOP*) CHECKOP(type, unop);
79072805
LW
2165 if (unop->op_next)
2166 return (OP*)unop;
2167
a0d0e21e 2168 return fold_constants((OP *) unop);
79072805
LW
2169}
2170
2171OP *
864dbfa3 2172Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2173{
2174 BINOP *binop;
b7dc083c 2175 NewOp(1101, binop, 1, BINOP);
79072805
LW
2176
2177 if (!first)
2178 first = newOP(OP_NULL, 0);
2179
2180 binop->op_type = type;
22c35a8c 2181 binop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2182 binop->op_first = first;
2183 binop->op_flags = flags | OPf_KIDS;
2184 if (!last) {
2185 last = first;
c07a80fd 2186 binop->op_private = 1 | (flags >> 8);
79072805
LW
2187 }
2188 else {
c07a80fd 2189 binop->op_private = 2 | (flags >> 8);
79072805
LW
2190 first->op_sibling = last;
2191 }
2192
e50aee73 2193 binop = (BINOP*)CHECKOP(type, binop);
79072805
LW
2194 if (binop->op_next)
2195 return (OP*)binop;
2196
7284ab6f 2197 binop->op_last = binop->op_first->op_sibling;
79072805 2198
a0d0e21e 2199 return fold_constants((OP *)binop);
79072805
LW
2200}
2201
a0ed51b3
LW
2202static int
2203utf8compare(const void *a, const void *b)
2204{
2205 int i;
2206 for (i = 0; i < 10; i++) {
2207 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2208 return -1;
2209 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2210 return 1;
2211 }
2212 return 0;
2213}
2214
79072805 2215OP *
864dbfa3 2216Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2217{
79072805
LW
2218 SV *tstr = ((SVOP*)expr)->op_sv;
2219 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
2220 STRLEN tlen;
2221 STRLEN rlen;
ec49126f 2222 register U8 *t = (U8*)SvPV(tstr, tlen);
2223 register U8 *r = (U8*)SvPV(rstr, rlen);
79072805
LW
2224 register I32 i;
2225 register I32 j;
a0ed51b3 2226 I32 del;
79072805 2227 I32 complement;
5d06d08e 2228 I32 squash;
79072805
LW
2229 register short *tbl;
2230
11343788 2231 complement = o->op_private & OPpTRANS_COMPLEMENT;
a0ed51b3 2232 del = o->op_private & OPpTRANS_DELETE;
5d06d08e 2233 squash = o->op_private & OPpTRANS_SQUASH;
79072805 2234
a0ed51b3 2235 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
79cb57f6 2236 SV* listsv = newSVpvn("# comment\n",10);
a0ed51b3
LW
2237 SV* transv = 0;
2238 U8* tend = t + tlen;
2239 U8* rend = r + rlen;
2240 I32 ulen;
2241 U32 tfirst = 1;
2242 U32 tlast = 0;
2243 I32 tdiff;
2244 U32 rfirst = 1;
2245 U32 rlast = 0;
2246 I32 rdiff;
2247 I32 diff;
2248 I32 none = 0;
2249 U32 max = 0;
2250 I32 bits;
2251 I32 grows = 0;
2252 I32 havefinal = 0;
2253 U32 final;
2254 HV *hv;
2255 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2256 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2257
2258 if (complement) {
2259 U8 tmpbuf[10];
2260 U8** cp;
2261 UV nextmin = 0;
2262 New(1109, cp, tlen, U8*);
2263 i = 0;
79cb57f6 2264 transv = newSVpvn("",0);
a0ed51b3
LW
2265 while (t < tend) {
2266 cp[i++] = t;
2267 t += UTF8SKIP(t);
2268 if (*t == 0xff) {
2269 t++;
2270 t += UTF8SKIP(t);
2271 }
2272 }
2273 qsort(cp, i, sizeof(U8*), utf8compare);
2274 for (j = 0; j < i; j++) {
2275 U8 *s = cp[j];
2276 UV val = utf8_to_uv(s, &ulen);
2277 s += ulen;
2278 diff = val - nextmin;
2279 if (diff > 0) {
2280 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2281 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2282 if (diff > 1) {
2283 t = uv_to_utf8(tmpbuf, val - 1);
2284 sv_catpvn(transv, "\377", 1);
dfe13c55 2285 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2286 }
2287 }
2288 if (*s == 0xff)
2289 val = utf8_to_uv(s+1, &ulen);
2290 if (val >= nextmin)
2291 nextmin = val + 1;
2292 }
2293 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2294 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2295 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2296 sv_catpvn(transv, "\377", 1);
dfe13c55
GS
2297 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2298 t = (U8*)SvPVX(transv);
a0ed51b3
LW
2299 tlen = SvCUR(transv);
2300 tend = t + tlen;
2301 }
2302 else if (!rlen && !del) {
2303 r = t; rlen = tlen; rend = tend;
4757a243
LW
2304 }
2305 if (!squash) {
2306 if (to_utf && from_utf) { /* only counting characters */
2307 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2308 o->op_private |= OPpTRANS_IDENTICAL;
2309 }
2310 else { /* straight latin-1 translation */
2311 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2312 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2313 o->op_private |= OPpTRANS_IDENTICAL;
2314 }
a0ed51b3
LW
2315 }
2316
2317 while (t < tend || tfirst <= tlast) {
2318 /* see if we need more "t" chars */
2319 if (tfirst > tlast) {
2320 tfirst = (I32)utf8_to_uv(t, &ulen);
2321 t += ulen;
2322 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2323 tlast = (I32)utf8_to_uv(++t, &ulen);
2324 t += ulen;
2325 }
2326 else
2327 tlast = tfirst;
2328 }
2329
2330 /* now see if we need more "r" chars */
2331 if (rfirst > rlast) {
2332 if (r < rend) {
2333 rfirst = (I32)utf8_to_uv(r, &ulen);
2334 r += ulen;
2335 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2336 rlast = (I32)utf8_to_uv(++r, &ulen);
2337 r += ulen;
2338 }
2339 else
2340 rlast = rfirst;
2341 }
2342 else {
2343 if (!havefinal++)
2344 final = rlast;
2345 rfirst = rlast = 0xffffffff;
2346 }
2347 }
2348
2349 /* now see which range will peter our first, if either. */
2350 tdiff = tlast - tfirst;
2351 rdiff = rlast - rfirst;
2352
2353 if (tdiff <= rdiff)
2354 diff = tdiff;
2355 else
2356 diff = rdiff;
2357
2358 if (rfirst == 0xffffffff) {
2359 diff = tdiff; /* oops, pretend rdiff is infinite */
2360 if (diff > 0)
cea2e8a9 2361 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
a0ed51b3 2362 else
cea2e8a9 2363 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
a0ed51b3
LW
2364 }
2365 else {
2366 if (diff > 0)
cea2e8a9 2367 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
a0ed51b3 2368 else
cea2e8a9 2369 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
a0ed51b3
LW
2370
2371 if (rfirst + diff > max)
2372 max = rfirst + diff;
2373 rfirst += diff + 1;
2374 if (!grows) {
2375 if (rfirst <= 0x80)
2376 ;
2377 else if (rfirst <= 0x800)
2378 grows |= (tfirst < 0x80);
2379 else if (rfirst <= 0x10000)
2380 grows |= (tfirst < 0x800);
2381 else if (rfirst <= 0x200000)
2382 grows |= (tfirst < 0x10000);
2383 else if (rfirst <= 0x4000000)
2384 grows |= (tfirst < 0x200000);
2385 else if (rfirst <= 0x80000000)
2386 grows |= (tfirst < 0x4000000);
2387 }
2388 }
2389 tfirst += diff + 1;
2390 }
2391
2392 none = ++max;
2393 if (del)
2394 del = ++max;
2395
2396 if (max > 0xffff)
2397 bits = 32;
2398 else if (max > 0xff)
2399 bits = 16;
2400 else
2401 bits = 8;
2402
2403 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2404 SvREFCNT_dec(listsv);
2405 if (transv)
2406 SvREFCNT_dec(transv);
2407
2408 if (!del && havefinal)
2409 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2410
2411 if (grows && to_utf)
2412 o->op_private |= OPpTRANS_GROWS;
2413
2414 op_free(expr);
2415 op_free(repl);
2416 return o;
2417 }
2418
2419 tbl = (short*)cPVOPo->op_pv;
79072805
LW
2420 if (complement) {
2421 Zero(tbl, 256, short);
2422 for (i = 0; i < tlen; i++)
ec49126f 2423 tbl[t[i]] = -1;
79072805
LW
2424 for (i = 0, j = 0; i < 256; i++) {
2425 if (!tbl[i]) {
2426 if (j >= rlen) {
a0ed51b3 2427 if (del)
79072805
LW
2428 tbl[i] = -2;
2429 else if (rlen)
ec49126f 2430 tbl[i] = r[j-1];
79072805
LW
2431 else
2432 tbl[i] = i;
2433 }
2434 else
ec49126f 2435 tbl[i] = r[j++];
79072805
LW
2436 }
2437 }
2438 }
2439 else {
a0ed51b3 2440 if (!rlen && !del) {
79072805 2441 r = t; rlen = tlen;
5d06d08e 2442 if (!squash)
4757a243 2443 o->op_private |= OPpTRANS_IDENTICAL;
79072805
LW
2444 }
2445 for (i = 0; i < 256; i++)
2446 tbl[i] = -1;
2447 for (i = 0, j = 0; i < tlen; i++,j++) {
2448 if (j >= rlen) {
a0ed51b3 2449 if (del) {
ec49126f 2450 if (tbl[t[i]] == -1)
2451 tbl[t[i]] = -2;
79072805
LW
2452 continue;
2453 }
2454 --j;
2455 }
ec49126f 2456 if (tbl[t[i]] == -1)
2457 tbl[t[i]] = r[j];
79072805
LW
2458 }
2459 }
2460 op_free(expr);
2461 op_free(repl);
2462
11343788 2463 return o;
79072805
LW
2464}
2465
2466OP *
864dbfa3 2467Perl_newPMOP(pTHX_ I32 type, I32 flags)
79072805 2468{
11343788 2469 dTHR;
79072805
LW
2470 PMOP *pmop;
2471
b7dc083c 2472 NewOp(1101, pmop, 1, PMOP);
79072805 2473 pmop->op_type = type;
22c35a8c 2474 pmop->op_ppaddr = PL_ppaddr[type];
79072805 2475 pmop->op_flags = flags;
c07a80fd 2476 pmop->op_private = 0 | (flags >> 8);
79072805 2477
3280af22 2478 if (PL_hints & HINT_RE_TAINT)
b3eb6a9b 2479 pmop->op_pmpermflags |= PMf_RETAINT;
3280af22 2480 if (PL_hints & HINT_LOCALE)
b3eb6a9b
GS
2481 pmop->op_pmpermflags |= PMf_LOCALE;
2482 pmop->op_pmflags = pmop->op_pmpermflags;
36477c24 2483
79072805 2484 /* link into pm list */
3280af22
NIS
2485 if (type != OP_TRANS && PL_curstash) {
2486 pmop->op_pmnext = HvPMROOT(PL_curstash);
2487 HvPMROOT(PL_curstash) = pmop;
79072805
LW
2488 }
2489
2490 return (OP*)pmop;
2491}
2492
2493OP *
864dbfa3 2494Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2495{
5c0ca799 2496 dTHR;
79072805
LW
2497 PMOP *pm;
2498 LOGOP *rcop;
ce862d02 2499 I32 repl_has_vars = 0;
79072805 2500
11343788
MB
2501 if (o->op_type == OP_TRANS)
2502 return pmtrans(o, expr, repl);
79072805 2503
3280af22 2504 PL_hints |= HINT_BLOCK_SCOPE;
11343788 2505 pm = (PMOP*)o;
79072805
LW
2506
2507 if (expr->op_type == OP_CONST) {
463ee0b2 2508 STRLEN plen;
79072805 2509 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 2510 char *p = SvPV(pat, plen);
11343788 2511 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 2512 sv_setpvn(pat, "\\s+", 3);
463ee0b2 2513 p = SvPV(pat, plen);
79072805
LW
2514 pm->op_pmflags |= PMf_SKIPWHITE;
2515 }
cea2e8a9 2516 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
aeea060c 2517 if (strEQ("\\s+", pm->op_pmregexp->precomp))
85e6fe83 2518 pm->op_pmflags |= PMf_WHITE;
79072805
LW
2519 op_free(expr);
2520 }
2521 else {
3280af22
NIS
2522 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2523 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2524 ? OP_REGCRESET
2525 : OP_REGCMAYBE),0,expr);
463ee0b2 2526
b7dc083c 2527 NewOp(1101, rcop, 1, LOGOP);
79072805 2528 rcop->op_type = OP_REGCOMP;
22c35a8c 2529 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
79072805 2530 rcop->op_first = scalar(expr);
3280af22 2531 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2532 ? (OPf_SPECIAL | OPf_KIDS)
2533 : OPf_KIDS);
79072805 2534 rcop->op_private = 1;
11343788 2535 rcop->op_other = o;
79072805
LW
2536
2537 /* establish postfix order */
3280af22 2538 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
463ee0b2
LW
2539 LINKLIST(expr);
2540 rcop->op_next = expr;
2541 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2542 }
2543 else {
2544 rcop->op_next = LINKLIST(expr);
2545 expr->op_next = (OP*)rcop;
2546 }
79072805 2547
11343788 2548 prepend_elem(o->op_type, scalar((OP*)rcop), o);
79072805
LW
2549 }
2550
2551 if (repl) {
748a9306 2552 OP *curop;
0244c3a4 2553 if (pm->op_pmflags & PMf_EVAL) {
748a9306 2554 curop = 0;
0244c3a4
GS
2555 if (PL_curcop->cop_line < PL_multi_end)
2556 PL_curcop->cop_line = PL_multi_end;
2557 }
554b3eca 2558#ifdef USE_THREADS
2faa37cc 2559 else if (repl->op_type == OP_THREADSV
554b3eca 2560 && strchr("&`'123456789+",
533c011a 2561 PL_threadsv_names[repl->op_targ]))
554b3eca
MB
2562 {
2563 curop = 0;
2564 }
2565#endif /* USE_THREADS */
748a9306
LW
2566 else if (repl->op_type == OP_CONST)
2567 curop = repl;
79072805 2568 else {
79072805
LW
2569 OP *lastop = 0;
2570 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
22c35a8c 2571 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
554b3eca 2572#ifdef USE_THREADS
ce862d02
IZ
2573 if (curop->op_type == OP_THREADSV) {
2574 repl_has_vars = 1;
be949f6f 2575 if (strchr("&`'123456789+", curop->op_private))
ce862d02 2576 break;
554b3eca
MB
2577 }
2578#else
79072805
LW
2579 if (curop->op_type == OP_GV) {
2580 GV *gv = ((GVOP*)curop)->op_gv;
ce862d02 2581 repl_has_vars = 1;
93a17b20 2582 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
2583 break;
2584 }
554b3eca 2585#endif /* USE_THREADS */
79072805
LW
2586 else if (curop->op_type == OP_RV2CV)
2587 break;
2588 else if (curop->op_type == OP_RV2SV ||
2589 curop->op_type == OP_RV2AV ||
2590 curop->op_type == OP_RV2HV ||
2591 curop->op_type == OP_RV2GV) {
2592 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2593 break;
2594 }
748a9306
LW
2595 else if (curop->op_type == OP_PADSV ||
2596 curop->op_type == OP_PADAV ||
2597 curop->op_type == OP_PADHV ||
554b3eca 2598 curop->op_type == OP_PADANY) {
ce862d02 2599 repl_has_vars = 1;
748a9306 2600 }
1167e5da
SM
2601 else if (curop->op_type == OP_PUSHRE)
2602 ; /* Okay here, dangerous in newASSIGNOP */
79072805
LW
2603 else
2604 break;
2605 }
2606 lastop = curop;
2607 }
748a9306 2608 }
ce862d02
IZ
2609 if (curop == repl
2610 && !(repl_has_vars
2611 && (!pm->op_pmregexp
2612 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
748a9306 2613 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4633a7c4 2614 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
11343788 2615 prepend_elem(o->op_type, scalar(repl), o);
748a9306
LW
2616 }
2617 else {
ce862d02
IZ
2618 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2619 pm->op_pmflags |= PMf_MAYBE_CONST;
2620 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2621 }
b7dc083c 2622 NewOp(1101, rcop, 1, LOGOP);
748a9306 2623 rcop->op_type = OP_SUBSTCONT;
22c35a8c 2624 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
748a9306
LW
2625 rcop->op_first = scalar(repl);
2626 rcop->op_flags |= OPf_KIDS;
2627 rcop->op_private = 1;
11343788 2628 rcop->op_other = o;
748a9306
LW
2629
2630 /* establish postfix order */
2631 rcop->op_next = LINKLIST(repl);
2632 repl->op_next = (OP*)rcop;
2633
2634 pm->op_pmreplroot = scalar((OP*)rcop);
2635 pm->op_pmreplstart = LINKLIST(rcop);
2636 rcop->op_next = 0;
79072805
LW
2637 }
2638 }
2639
2640 return (OP*)pm;
2641}
2642
2643OP *
864dbfa3 2644Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
79072805
LW
2645{
2646 SVOP *svop;
b7dc083c 2647 NewOp(1101, svop, 1, SVOP);
79072805 2648 svop->op_type = type;
22c35a8c 2649 svop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2650 svop->op_sv = sv;
2651 svop->op_next = (OP*)svop;
2652 svop->op_flags = flags;
22c35a8c 2653 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2654 scalar((OP*)svop);
22c35a8c 2655 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2656 svop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2657 return CHECKOP(type, svop);
79072805
LW
2658}
2659
2660OP *
864dbfa3 2661Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
79072805 2662{
11343788 2663 dTHR;
79072805 2664 GVOP *gvop;
b7dc083c 2665 NewOp(1101, gvop, 1, GVOP);
79072805 2666 gvop->op_type = type;
22c35a8c 2667 gvop->op_ppaddr = PL_ppaddr[type];
8990e307 2668 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
79072805
LW
2669 gvop->op_next = (OP*)gvop;
2670 gvop->op_flags = flags;
22c35a8c 2671 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2672 scalar((OP*)gvop);
22c35a8c 2673 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2674 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2675 return CHECKOP(type, gvop);
79072805
LW
2676}
2677
2678OP *
864dbfa3 2679Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
79072805
LW
2680{
2681 PVOP *pvop;
b7dc083c 2682 NewOp(1101, pvop, 1, PVOP);
79072805 2683 pvop->op_type = type;
22c35a8c 2684 pvop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2685 pvop->op_pv = pv;
2686 pvop->op_next = (OP*)pvop;
2687 pvop->op_flags = flags;
22c35a8c 2688 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2689 scalar((OP*)pvop);
22c35a8c 2690 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2691 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2692 return CHECKOP(type, pvop);
79072805
LW
2693}
2694
79072805 2695void
864dbfa3 2696Perl_package(pTHX_ OP *o)
79072805 2697{
11343788 2698 dTHR;
93a17b20 2699 SV *sv;
79072805 2700
3280af22
NIS
2701 save_hptr(&PL_curstash);
2702 save_item(PL_curstname);
11343788 2703 if (o) {
463ee0b2
LW
2704 STRLEN len;
2705 char *name;
11343788 2706 sv = cSVOPo->op_sv;
463ee0b2 2707 name = SvPV(sv, len);
3280af22
NIS
2708 PL_curstash = gv_stashpvn(name,len,TRUE);
2709 sv_setpvn(PL_curstname, name, len);
11343788 2710 op_free(o);
93a17b20
LW
2711 }
2712 else {
3280af22
NIS
2713 sv_setpv(PL_curstname,"<none>");
2714 PL_curstash = Nullhv;
93a17b20 2715 }
7ad382f4 2716 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
2717 PL_copline = NOLINE;
2718 PL_expect = XSTATE;
79072805
LW
2719}
2720
85e6fe83 2721void
864dbfa3 2722Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
85e6fe83 2723{
a0d0e21e
LW
2724 OP *pack;
2725 OP *meth;
2726 OP *rqop;
2727 OP *imop;
b1cb66bf 2728 OP *veop;
78ca652e 2729 GV *gv;
85e6fe83 2730
a0d0e21e 2731 if (id->op_type != OP_CONST)
cea2e8a9 2732 Perl_croak(aTHX_ "Module name must be constant");
85e6fe83 2733
b1cb66bf 2734 veop = Nullop;
2735
2736 if(version != Nullop) {
2737 SV *vesv = ((SVOP*)version)->op_sv;
2738
2739 if (arg == Nullop && !SvNIOK(vesv)) {
2740 arg = version;
2741 }
2742 else {
2743 OP *pack;
2744 OP *meth;
2745
2746 if (version->op_type != OP_CONST || !SvNIOK(vesv))
cea2e8a9 2747 Perl_croak(aTHX_ "Version number must be constant number");
b1cb66bf 2748
2749 /* Make copy of id so we don't free it twice */
2750 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2751
2752 /* Fake up a method call to VERSION */
79cb57f6 2753 meth = newSVOP(OP_CONST, 0, newSVpvn("VERSION", 7));
b1cb66bf 2754 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2755 append_elem(OP_LIST,
2756 prepend_elem(OP_LIST, pack, list(version)),
2757 newUNOP(OP_METHOD, 0, meth)));
2758 }
2759 }
aeea060c 2760
a0d0e21e 2761 /* Fake up an import/unimport */
4633a7c4
LW
2762 if (arg && arg->op_type == OP_STUB)
2763 imop = arg; /* no import on explicit () */
b1cb66bf 2764 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2765 imop = Nullop; /* use 5.0; */
2766 }
4633a7c4
LW
2767 else {
2768 /* Make copy of id so we don't free it twice */
2769 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
4633a7c4
LW
2770 meth = newSVOP(OP_CONST, 0,
2771 aver
79cb57f6
GS
2772 ? newSVpvn("import", 6)
2773 : newSVpvn("unimport", 8)
4633a7c4
LW
2774 );
2775 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
a0d0e21e
LW
2776 append_elem(OP_LIST,
2777 prepend_elem(OP_LIST, pack, list(arg)),
2778 newUNOP(OP_METHOD, 0, meth)));
4633a7c4
LW
2779 }
2780
78ca652e
GS
2781 /* Fake up a require, handle override, if any */
2782 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
2783 if (!(gv && GvIMPORTED_CV(gv)))
2784 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
2785
2786 if (gv && GvIMPORTED_CV(gv)) {
2787 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2788 append_elem(OP_LIST, id,
2789 scalar(newUNOP(OP_RV2CV, 0,
2790 newGVOP(OP_GV, 0,
2791 gv))))));
2792 }
2793 else {
2794 rqop = newUNOP(OP_REQUIRE, 0, id);
2795 }
a0d0e21e
LW
2796
2797 /* Fake up the BEGIN {}, which does its thing immediately. */
c07a80fd 2798 newSUB(floor,
79cb57f6 2799 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
4633a7c4 2800 Nullop,
a0d0e21e 2801 append_elem(OP_LINESEQ,
b1cb66bf 2802 append_elem(OP_LINESEQ,
2803 newSTATEOP(0, Nullch, rqop),
2804 newSTATEOP(0, Nullch, veop)),
a0d0e21e 2805 newSTATEOP(0, Nullch, imop) ));
85e6fe83 2806
3280af22
NIS
2807 PL_copline = NOLINE;
2808 PL_expect = XSTATE;
85e6fe83
LW
2809}
2810
79072805 2811OP *
864dbfa3 2812Perl_dofile(pTHX_ OP *term)
78ca652e
GS
2813{
2814 OP *doop;
2815 GV *gv;
2816
2817 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
2818 if (!(gv && GvIMPORTED_CV(gv)))
2819 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
2820
2821 if (gv && GvIMPORTED_CV(gv)) {
2822 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
2823 append_elem(OP_LIST, term,
2824 scalar(newUNOP(OP_RV2CV, 0,
2825 newGVOP(OP_GV, 0,
2826 gv))))));
2827 }
2828 else {
2829 doop = newUNOP(OP_DOFILE, 0, scalar(term));
2830 }
2831 return doop;
2832}
2833
2834OP *
864dbfa3 2835Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
79072805
LW
2836{
2837 return newBINOP(OP_LSLICE, flags,
8990e307
LW
2838 list(force_list(subscript)),
2839 list(force_list(listval)) );
79072805
LW
2840}
2841
76e3520e 2842STATIC I32
cea2e8a9 2843S_list_assignment(pTHX_ register OP *o)
79072805 2844{
11343788 2845 if (!o)
79072805
LW
2846 return TRUE;
2847
11343788
MB
2848 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2849 o = cUNOPo->op_first;
79072805 2850
11343788
MB
2851 if (o->op_type == OP_COND_EXPR) {
2852 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2853 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
79072805
LW
2854
2855 if (t && f)
2856 return TRUE;
2857 if (t || f)
2858 yyerror("Assignment to both a list and a scalar");
2859 return FALSE;
2860 }
2861
11343788
MB
2862 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2863 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2864 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
79072805
LW
2865 return TRUE;
2866
11343788 2867 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
93a17b20
LW
2868 return TRUE;
2869
11343788 2870 if (o->op_type == OP_RV2SV)
79072805
LW
2871 return FALSE;
2872
2873 return FALSE;
2874}
2875
2876OP *
864dbfa3 2877Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
79072805 2878{
11343788 2879 OP *o;
79072805 2880
a0d0e21e
LW
2881 if (optype) {
2882 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2883 return newLOGOP(optype, 0,
2884 mod(scalar(left), optype),
2885 newUNOP(OP_SASSIGN, 0, scalar(right)));
2886 }
2887 else {
2888 return newBINOP(optype, OPf_STACKED,
2889 mod(scalar(left), optype), scalar(right));
2890 }
2891 }
2892
79072805 2893 if (list_assignment(left)) {
6ee623d5 2894 dTHR;
3280af22
NIS
2895 PL_modcount = 0;
2896 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
463ee0b2 2897 left = mod(left, OP_AASSIGN);
3280af22
NIS
2898 if (PL_eval_start)
2899 PL_eval_start = 0;
748a9306 2900 else {
a0d0e21e
LW
2901 op_free(left);
2902 op_free(right);
2903 return Nullop;
2904 }
11343788 2905 o = newBINOP(OP_AASSIGN, flags,
8990e307
LW
2906 list(force_list(right)),
2907 list(force_list(left)) );
11343788 2908 o->op_private = 0 | (flags >> 8);
a0d0e21e 2909 if (!(left->op_private & OPpLVAL_INTRO)) {
79072805 2910 OP *curop;
11343788 2911 OP *lastop = o;
3280af22 2912 PL_generation++;
11343788 2913 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
22c35a8c 2914 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
79072805
LW
2915 if (curop->op_type == OP_GV) {
2916 GV *gv = ((GVOP*)curop)->op_gv;
3280af22 2917 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
79072805 2918 break;
3280af22 2919 SvCUR(gv) = PL_generation;
79072805 2920 }
748a9306
LW
2921 else if (curop->op_type == OP_PADSV ||
2922 curop->op_type == OP_PADAV ||
2923 curop->op_type == OP_PADHV ||
2924 curop->op_type == OP_PADANY) {
3280af22 2925 SV **svp = AvARRAY(PL_comppad_name);
8e07c86e 2926 SV *sv = svp[curop->op_targ];
3280af22 2927 if (SvCUR(sv) == PL_generation)
748a9306 2928 break;
3280af22 2929 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
748a9306 2930 }
79072805
LW
2931 else if (curop->op_type == OP_RV2CV)
2932 break;
2933 else if (curop->op_type == OP_RV2SV ||
2934 curop->op_type == OP_RV2AV ||
2935 curop->op_type == OP_RV2HV ||
2936 curop->op_type == OP_RV2GV) {
2937 if (lastop->op_type != OP_GV) /* funny deref? */
2938 break;
2939 }
1167e5da
SM
2940 else if (curop->op_type == OP_PUSHRE) {
2941 if (((PMOP*)curop)->op_pmreplroot) {
2942 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3280af22 2943 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
1167e5da 2944 break;
3280af22 2945 SvCUR(gv) = PL_generation;
1167e5da
SM
2946 }
2947 }
79072805
LW
2948 else
2949 break;
2950 }
2951 lastop = curop;
2952 }
11343788
MB
2953 if (curop != o)
2954 o->op_private = OPpASSIGN_COMMON;
79072805 2955 }
c07a80fd 2956 if (right && right->op_type == OP_SPLIT) {
2957 OP* tmpop;
2958 if ((tmpop = ((LISTOP*)right)->op_first) &&
2959 tmpop->op_type == OP_PUSHRE)
2960 {
2961 PMOP *pm = (PMOP*)tmpop;
2962 if (left->op_type == OP_RV2AV &&
2963 !(left->op_private & OPpLVAL_INTRO) &&
11343788 2964 !(o->op_private & OPpASSIGN_COMMON) )
c07a80fd 2965 {
2966 tmpop = ((UNOP*)left)->op_first;
2967 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2968 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2969 pm->op_pmflags |= PMf_ONCE;
11343788 2970 tmpop = cUNOPo->op_first; /* to list (nulled) */
c07a80fd 2971 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2972 tmpop->op_sibling = Nullop; /* don't free split */
2973 right->op_next = tmpop->op_next; /* fix starting loc */
11343788 2974 op_free(o); /* blow off assign */
54310121 2975 right->op_flags &= ~OPf_WANT;
a5f75d66 2976 /* "I don't know and I don't care." */
c07a80fd 2977 return right;
2978 }
2979 }
2980 else {
3280af22 2981 if (PL_modcount < 10000 &&
c07a80fd 2982 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2983 {
2984 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2985 if (SvIVX(sv) == 0)
3280af22 2986 sv_setiv(sv, PL_modcount+1);
c07a80fd 2987 }
2988 }
2989 }
2990 }
11343788 2991 return o;
79072805
LW
2992 }
2993 if (!right)
2994 right = newOP(OP_UNDEF, 0);
2995 if (right->op_type == OP_READLINE) {
2996 right->op_flags |= OPf_STACKED;
463ee0b2 2997 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805 2998 }
a0d0e21e 2999 else {
3280af22 3000 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
11343788 3001 o = newBINOP(OP_SASSIGN, flags,
463ee0b2 3002 scalar(right), mod(scalar(left), OP_SASSIGN) );
3280af22
NIS
3003 if (PL_eval_start)
3004 PL_eval_start = 0;
748a9306 3005 else {
11343788 3006 op_free(o);
a0d0e21e
LW
3007 return Nullop;
3008 }
3009 }
11343788 3010 return o;
79072805
LW
3011}
3012
3013OP *
864dbfa3 3014Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
79072805 3015{
11343788 3016 dTHR;
bbce6d69 3017 U32 seq = intro_my();
79072805
LW
3018 register COP *cop;
3019
b7dc083c 3020 NewOp(1101, cop, 1, COP);
3280af22 3021 if (PERLDB_LINE && PL_curcop->cop_line && PL_curstash != PL_debstash) {
8990e307 3022 cop->op_type = OP_DBSTATE;
22c35a8c 3023 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
8990e307
LW
3024 }
3025 else {
3026 cop->op_type = OP_NEXTSTATE;
22c35a8c 3027 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
8990e307 3028 }
79072805 3029 cop->op_flags = flags;
a0ed51b3 3030 cop->op_private = (PL_hints & HINT_UTF8);
ff0cee69 3031#ifdef NATIVE_HINTS
3032 cop->op_private |= NATIVE_HINTS;
3033#endif
e24b16f9 3034 PL_compiling.op_private = cop->op_private;
79072805
LW
3035 cop->op_next = (OP*)cop;
3036
463ee0b2
LW
3037 if (label) {
3038 cop->cop_label = label;
3280af22 3039 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 3040 }
bbce6d69 3041 cop->cop_seq = seq;
3280af22 3042 cop->cop_arybase = PL_curcop->cop_arybase;
599cee73
PM
3043 if (PL_curcop->cop_warnings == WARN_NONE
3044 || PL_curcop->cop_warnings == WARN_ALL)
3045 cop->cop_warnings = PL_curcop->cop_warnings ;
3046 else
3047 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3048
79072805 3049
3280af22
NIS
3050 if (PL_copline == NOLINE)
3051 cop->cop_line = PL_curcop->cop_line;
79072805 3052 else {
3280af22
NIS
3053 cop->cop_line = PL_copline;
3054 PL_copline = NOLINE;
79072805 3055 }
3280af22
NIS
3056 cop->cop_filegv = (GV*)SvREFCNT_inc(PL_curcop->cop_filegv);
3057 cop->cop_stash = PL_curstash;
79072805 3058
3280af22
NIS
3059 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3060 SV **svp = av_fetch(GvAV(PL_curcop->cop_filegv),(I32)cop->cop_line, FALSE);
3061 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
a0d0e21e 3062 (void)SvIOK_on(*svp);
a5f75d66 3063 SvIVX(*svp) = 1;
93a17b20
LW
3064 SvSTASH(*svp) = (HV*)cop;
3065 }
3066 }
3067
11343788 3068 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
79072805
LW
3069}
3070
bbce6d69 3071/* "Introduce" my variables to visible status. */
3072U32
864dbfa3 3073Perl_intro_my(pTHX)
bbce6d69 3074{
3075 SV **svp;
3076 SV *sv;
3077 I32 i;
3078
3280af22
NIS
3079 if (! PL_min_intro_pending)
3080 return PL_cop_seqmax;
bbce6d69 3081
3280af22
NIS
3082 svp = AvARRAY(PL_comppad_name);
3083 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3084 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
c53d7c7d 3085 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
65202027 3086 SvNVX(sv) = (NV)PL_cop_seqmax;
bbce6d69 3087 }
3088 }
3280af22
NIS
3089 PL_min_intro_pending = 0;
3090 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3091 return PL_cop_seqmax++;
bbce6d69 3092}
3093
79072805 3094OP *
864dbfa3 3095Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
79072805 3096{
883ffac3
CS
3097 return new_logop(type, flags, &first, &other);
3098}
3099
3bd495df 3100STATIC OP *
cea2e8a9 3101S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
883ffac3 3102{
11343788 3103 dTHR;
79072805 3104 LOGOP *logop;
11343788 3105 OP *o;
883ffac3
CS
3106 OP *first = *firstp;
3107 OP *other = *otherp;
79072805 3108
a0d0e21e
LW
3109 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3110 return newBINOP(type, flags, scalar(first), scalar(other));
3111
8990e307 3112 scalarboolean(first);
79072805
LW
3113 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3114 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3115 if (type == OP_AND || type == OP_OR) {
3116 if (type == OP_AND)
3117 type = OP_OR;
3118 else
3119 type = OP_AND;
11343788 3120 o = first;
883ffac3 3121 first = *firstp = cUNOPo->op_first;
11343788
MB
3122 if (o->op_next)
3123 first->op_next = o->op_next;
3124 cUNOPo->op_first = Nullop;
3125 op_free(o);
79072805
LW
3126 }
3127 }
3128 if (first->op_type == OP_CONST) {
599cee73 3129 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
cea2e8a9 3130 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
22c35a8c 3131 PL_op_desc[type]);
79072805
LW
3132 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3133 op_free(first);
883ffac3 3134 *firstp = Nullop;
79072805
LW
3135 return other;
3136 }
3137 else {
3138 op_free(other);
883ffac3 3139 *otherp = Nullop;
79072805
LW
3140 return first;
3141 }
3142 }
3143 else if (first->op_type == OP_WANTARRAY) {
3144 if (type == OP_AND)
3145 list(other);
3146 else
3147 scalar(other);
3148 }
599cee73 3149 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
a6006777 3150 OP *k1 = ((UNOP*)first)->op_first;
3151 OP *k2 = k1->op_sibling;
3152 OPCODE warnop = 0;
3153 switch (first->op_type)
3154 {
3155 case OP_NULL:
3156 if (k2 && k2->op_type == OP_READLINE
3157 && (k2->op_flags & OPf_STACKED)
55d729e4 3158 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
a6006777 3159 warnop = k2->op_type;
3160 break;
3161
3162 case OP_SASSIGN:
68dc0745 3163 if (k1->op_type == OP_READDIR
3164 || k1->op_type == OP_GLOB
3165 || k1->op_type == OP_EACH)
a6006777 3166 warnop = k1->op_type;
3167 break;
3168 }
8ebc5c01 3169 if (warnop) {
3280af22
NIS
3170 line_t oldline = PL_curcop->cop_line;
3171 PL_curcop->cop_line = PL_copline;
cea2e8a9 3172 Perl_warner(aTHX_ WARN_UNSAFE,
599cee73 3173 "Value of %s%s can be \"0\"; test with defined()",
22c35a8c 3174 PL_op_desc[warnop],
68dc0745 3175 ((warnop == OP_READLINE || warnop == OP_GLOB)
3176 ? " construct" : "() operator"));
3280af22 3177 PL_curcop->cop_line = oldline;
8ebc5c01 3178 }
a6006777 3179 }
79072805
LW
3180
3181 if (!other)
3182 return first;
3183
a0d0e21e
LW
3184 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3185 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3186
b7dc083c 3187 NewOp(1101, logop, 1, LOGOP);
79072805
LW
3188
3189 logop->op_type = type;
22c35a8c 3190 logop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3191 logop->op_first = first;
3192 logop->op_flags = flags | OPf_KIDS;
3193 logop->op_other = LINKLIST(other);
c07a80fd 3194 logop->op_private = 1 | (flags >> 8);
79072805
LW
3195
3196 /* establish postfix order */
3197 logop->op_next = LINKLIST(first);
3198 first->op_next = (OP*)logop;
3199 first->op_sibling = other;
3200
11343788
MB
3201 o = newUNOP(OP_NULL, 0, (OP*)logop);
3202 other->op_next = o;
79072805 3203
11343788 3204 return o;
79072805
LW
3205}
3206
3207OP *
864dbfa3 3208Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
79072805 3209{
11343788 3210 dTHR;
79072805 3211 CONDOP *condop;
11343788 3212 OP *o;
79072805 3213
b1cb66bf 3214 if (!falseop)
3215 return newLOGOP(OP_AND, 0, first, trueop);
3216 if (!trueop)
3217 return newLOGOP(OP_OR, 0, first, falseop);
79072805 3218
8990e307 3219 scalarboolean(first);
79072805
LW
3220 if (first->op_type == OP_CONST) {
3221 if (SvTRUE(((SVOP*)first)->op_sv)) {
3222 op_free(first);
b1cb66bf 3223 op_free(falseop);
3224 return trueop;
79072805
LW
3225 }
3226 else {
3227 op_free(first);
b1cb66bf 3228 op_free(trueop);
3229 return falseop;
79072805
LW
3230 }
3231 }
3232 else if (first->op_type == OP_WANTARRAY) {
b1cb66bf 3233 list(trueop);
3234 scalar(falseop);
79072805 3235 }
b7dc083c 3236 NewOp(1101, condop, 1, CONDOP);
79072805
LW
3237
3238 condop->op_type = OP_COND_EXPR;
22c35a8c 3239 condop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
79072805
LW
3240 condop->op_first = first;
3241 condop->op_flags = flags | OPf_KIDS;
b1cb66bf 3242 condop->op_true = LINKLIST(trueop);
3243 condop->op_false = LINKLIST(falseop);
c07a80fd 3244 condop->op_private = 1 | (flags >> 8);
79072805
LW
3245
3246 /* establish postfix order */
3247 condop->op_next = LINKLIST(first);
3248 first->op_next = (OP*)condop;
3249
b1cb66bf 3250 first->op_sibling = trueop;
3251 trueop->op_sibling = falseop;
11343788 3252 o = newUNOP(OP_NULL, 0, (OP*)condop);
79072805 3253
5dc0d613
MB
3254 trueop->op_next = o;
3255 falseop->op_next = o;
79072805 3256
11343788 3257 return o;
79072805
LW
3258}
3259
3260OP *
864dbfa3 3261Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
79072805 3262{
b35b2403 3263 dTHR;
79072805
LW
3264 CONDOP *condop;
3265 OP *flip;
3266 OP *flop;
11343788 3267 OP *o;
79072805 3268
b7dc083c 3269 NewOp(1101, condop, 1, CONDOP);
79072805
LW
3270
3271 condop->op_type = OP_RANGE;
22c35a8c 3272 condop->op_ppaddr = PL_ppaddr[OP_RANGE];
79072805
LW
3273 condop->op_first = left;
3274 condop->op_flags = OPf_KIDS;
3275 condop->op_true = LINKLIST(left);
3276 condop->op_false = LINKLIST(right);
c07a80fd 3277 condop->op_private = 1 | (flags >> 8);
79072805
LW
3278
3279 left->op_sibling = right;
3280
3281 condop->op_next = (OP*)condop;
3282 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
3283 flop = newUNOP(OP_FLOP, 0, flip);
11343788 3284 o = newUNOP(OP_NULL, 0, flop);
79072805
LW
3285 linklist(flop);
3286
3287 left->op_next = flip;
3288 right->op_next = flop;
3289
ed6116ce 3290 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805 3291 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
ed6116ce 3292 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805
LW
3293 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3294
3295 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3296 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3297
11343788 3298 flip->op_next = o;
79072805 3299 if (!flip->op_private || !flop->op_private)
11343788 3300 linklist(o); /* blow off optimizer unless constant */
79072805 3301
11343788 3302 return o;
79072805
LW
3303}
3304
3305OP *
864dbfa3 3306Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
79072805 3307{
11343788 3308 dTHR;
463ee0b2 3309 OP* listop;
11343788 3310 OP* o;
463ee0b2 3311 int once = block && block->op_flags & OPf_SPECIAL &&
a0d0e21e 3312 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
93a17b20 3313
463ee0b2
LW
3314 if (expr) {
3315 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3316 return block; /* do {} while 0 does once */
fb73857a 3317 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3318 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
774d564b 3319 expr = newUNOP(OP_DEFINED, 0,
54b9620d 3320 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
55d729e4
GS
3321 } else if (expr->op_flags & OPf_KIDS) {
3322 OP *k1 = ((UNOP*)expr)->op_first;
3323 OP *k2 = (k1) ? k1->op_sibling : NULL;
3324 switch (expr->op_type) {
3325 case OP_NULL:
3326 if (k2 && k2->op_type == OP_READLINE
3327 && (k2->op_flags & OPf_STACKED)
3328 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3329 expr = newUNOP(OP_DEFINED, 0, expr);
3330 break;
3331
3332 case OP_SASSIGN:
3333 if (k1->op_type == OP_READDIR
3334 || k1->op_type == OP_GLOB
3335 || k1->op_type == OP_EACH)
3336 expr = newUNOP(OP_DEFINED, 0, expr);
3337 break;
3338 }
774d564b 3339 }
463ee0b2 3340 }
93a17b20 3341
8990e307 3342 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
883ffac3 3343 o = new_logop(OP_AND, 0, &expr, &listop);
463ee0b2 3344
883ffac3
CS
3345 if (listop)
3346 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
79072805 3347
11343788
MB
3348 if (once && o != listop)
3349 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
79072805 3350
11343788
MB
3351 if (o == listop)
3352 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
748a9306 3353
11343788
MB
3354 o->op_flags |= flags;
3355 o = scope(o);
3356 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3357 return o;
79072805
LW
3358}
3359
3360OP *
864dbfa3 3361Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
79072805 3362{
11343788 3363 dTHR;
79072805
LW
3364 OP *redo;
3365 OP *next = 0;
3366 OP *listop;
11343788 3367 OP *o;
79072805
LW
3368 OP *condop;
3369
fb73857a 3370 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3371 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
748a9306 3372 expr = newUNOP(OP_DEFINED, 0,
54b9620d 3373 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
55d729e4
GS
3374 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3375 OP *k1 = ((UNOP*)expr)->op_first;
3376 OP *k2 = (k1) ? k1->op_sibling : NULL;
3377 switch (expr->op_type) {
3378 case OP_NULL:
3379 if (k2 && k2->op_type == OP_READLINE
3380 && (k2->op_flags & OPf_STACKED)
3381 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3382 expr = newUNOP(OP_DEFINED, 0, expr);
3383 break;
3384
3385 case OP_SASSIGN:
3386 if (k1->op_type == OP_READDIR
3387 || k1->op_type == OP_GLOB
3388 || k1->op_type == OP_EACH)
3389 expr = newUNOP(OP_DEFINED, 0, expr);
3390 break;
3391 }
748a9306 3392 }
79072805
LW
3393
3394 if (!block)
3395 block = newOP(OP_NULL, 0);
3396
3397 if (cont)
3398 next = LINKLIST(cont);
fb73857a 3399 if (expr) {
79072805 3400 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
fb73857a 3401 if ((line_t)whileline != NOLINE) {
3280af22 3402 PL_copline = whileline;
fb73857a 3403 cont = append_elem(OP_LINESEQ, cont,
3404 newSTATEOP(0, Nullch, Nullop));
3405 }
3406 }
79072805 3407
463ee0b2 3408 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
79072805
LW
3409 redo = LINKLIST(listop);
3410
3411 if (expr) {
3280af22 3412 PL_copline = whileline;
883ffac3
CS
3413 scalar(listop);
3414 o = new_logop(OP_AND, 0, &expr, &listop);
11343788 3415 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
85e6fe83 3416 op_free(expr); /* oops, it's a while (0) */
463ee0b2 3417 op_free((OP*)loop);
883ffac3 3418 return Nullop; /* listop already freed by new_logop */
463ee0b2 3419 }
883ffac3
CS
3420 if (listop)
3421 ((LISTOP*)listop)->op_last->op_next = condop =
3422 (o == listop ? redo : LINKLIST(o));
79072805
LW
3423 if (!next)
3424 next = condop;
3425 }
3426 else
11343788 3427 o = listop;
79072805
LW
3428
3429 if (!loop) {
b7dc083c 3430 NewOp(1101,loop,1,LOOP);
79072805 3431 loop->op_type = OP_ENTERLOOP;
22c35a8c 3432 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
79072805
LW
3433 loop->op_private = 0;
3434 loop->op_next = (OP*)loop;
3435 }
3436
11343788 3437 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
79072805
LW
3438
3439 loop->op_redoop = redo;
11343788 3440 loop->op_lastop = o;
79072805
LW
3441
3442 if (next)
3443 loop->op_nextop = next;
3444 else
11343788 3445 loop->op_nextop = o;
79072805 3446
11343788
MB
3447 o->op_flags |= flags;
3448 o->op_private |= (flags >> 8);
3449 return o;
79072805
LW
3450}
3451
3452OP *
864dbfa3 3453Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
79072805
LW
3454{
3455 LOOP *loop;
b7dc083c 3456 LOOP *tmp;
fb73857a 3457 OP *wop;
85e6fe83 3458 int padoff = 0;
4633a7c4 3459 I32 iterflags = 0;
79072805 3460
79072805 3461 if (sv) {
85e6fe83 3462 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
748a9306 3463 sv->op_type = OP_RV2GV;
22c35a8c 3464 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
79072805 3465 }
85e6fe83
LW
3466 else if (sv->op_type == OP_PADSV) { /* private variable */
3467 padoff = sv->op_targ;
3468 op_free(sv);
3469 sv = Nullop;
3470 }
54b9620d
MB
3471 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3472 padoff = sv->op_targ;
3473 iterflags |= OPf_SPECIAL;
3474 op_free(sv);
3475 sv = Nullop;
3476 }
79072805 3477 else
cea2e8a9 3478 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
79072805
LW
3479 }
3480 else {
54b9620d
MB
3481#ifdef USE_THREADS
3482 padoff = find_threadsv("_");
3483 iterflags |= OPf_SPECIAL;
3484#else
3280af22 3485 sv = newGVOP(OP_GV, 0, PL_defgv);
54b9620d 3486#endif
79072805 3487 }
5f05dabc 3488 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
89ea2908 3489 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
4633a7c4
LW
3490 iterflags |= OPf_STACKED;
3491 }
89ea2908
GA
3492 else if (expr->op_type == OP_NULL &&
3493 (expr->op_flags & OPf_KIDS) &&
3494 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3495 {
3496 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3497 * set the STACKED flag to indicate that these values are to be
3498 * treated as min/max values by 'pp_iterinit'.
3499 */
3500 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3501 CONDOP* range = (CONDOP*) flip->op_first;
3502 OP* left = range->op_first;
3503 OP* right = left->op_sibling;
5152d7c7 3504 LISTOP* listop;
89ea2908
GA
3505
3506 range->op_flags &= ~OPf_KIDS;