3 * Copyright (c) 1991-2000, Larry Wall
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.
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
23 /* #define PL_OP_SLAB_ALLOC */
25 #ifdef PL_OP_SLAB_ALLOC
26 #define SLAB_SIZE 8192
27 static char *PL_OpPtr = NULL;
28 static int PL_OpSpace = 0;
29 #define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
30 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
32 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
36 S_Slab_Alloc(pTHX_ int m, size_t sz)
38 Newz(m,PL_OpPtr,SLAB_SIZE,char);
39 PL_OpSpace = SLAB_SIZE - sz;
40 return PL_OpPtr += PL_OpSpace;
44 #define NewOp(m, var, c, type) Newz(m, var, c, type)
47 * In the following definition, the ", Nullop" is just to make the compiler
48 * think the expression is of the right type: croak actually does a Siglongjmp.
50 #define CHECKOP(type,o) \
51 ((PL_op_mask && PL_op_mask[type]) \
52 ? ( op_free((OP*)o), \
53 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
55 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
57 #define PAD_MAX 999999999
60 S_gv_ename(pTHX_ GV *gv)
63 SV* tmpsv = sv_newmortal();
64 gv_efullname3(tmpsv, gv, Nullch);
65 return SvPV(tmpsv,n_a);
69 S_no_fh_allowed(pTHX_ OP *o)
71 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
72 PL_op_desc[o->op_type]));
77 S_too_few_arguments(pTHX_ OP *o, char *name)
79 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
84 S_too_many_arguments(pTHX_ OP *o, char *name)
86 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
91 S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
93 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
94 (int)n, name, t, PL_op_desc[kid->op_type]));
98 S_no_bareword_allowed(pTHX_ OP *o)
100 qerror(Perl_mess(aTHX_
101 "Bareword \"%s\" not allowed while \"strict subs\" in use",
102 SvPV_nolen(cSVOPo_sv)));
105 /* "register" allocation */
108 Perl_pad_allocmy(pTHX_ char *name)
114 if (!(PL_in_my == KEY_our ||
116 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
117 (name[1] == '_' && (int)strlen(name) > 2)))
119 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
120 /* 1999-02-27 mjd@plover.com */
122 p = strchr(name, '\0');
123 /* The next block assumes the buffer is at least 205 chars
124 long. At present, it's always at least 256 chars. */
126 strcpy(name+200, "...");
132 /* Move everything else down one character */
133 for (; p-name > 2; p--)
135 name[2] = toCTRL(name[1]);
138 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
140 if (ckWARN(WARN_MISC) && AvFILLp(PL_comppad_name) >= 0) {
141 SV **svp = AvARRAY(PL_comppad_name);
142 HV *ourstash = (PL_curstash ? PL_curstash : PL_defstash);
143 PADOFFSET top = AvFILLp(PL_comppad_name);
144 for (off = top; off > PL_comppad_name_floor; off--) {
146 && sv != &PL_sv_undef
147 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
148 && (PL_in_my != KEY_our
149 || ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash))
150 && strEQ(name, SvPVX(sv)))
152 Perl_warner(aTHX_ WARN_MISC,
153 "\"%s\" variable %s masks earlier declaration in same %s",
154 (PL_in_my == KEY_our ? "our" : "my"),
156 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
161 if (PL_in_my == KEY_our) {
164 && sv != &PL_sv_undef
165 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
166 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
167 && strEQ(name, SvPVX(sv)))
169 Perl_warner(aTHX_ WARN_MISC,
170 "\"our\" variable %s redeclared", name);
171 Perl_warner(aTHX_ WARN_MISC,
172 "\t(Did you mean \"local\" instead of \"our\"?)\n");
175 } while ( off-- > 0 );
178 off = pad_alloc(OP_PADSV, SVs_PADMY);
180 sv_upgrade(sv, SVt_PVNV);
182 if (PL_in_my_stash) {
184 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"%s\"",
185 name, PL_in_my == KEY_our ? "our" : "my"));
187 (void)SvUPGRADE(sv, SVt_PVMG);
188 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
191 if (PL_in_my == KEY_our) {
192 (void)SvUPGRADE(sv, SVt_PVGV);
193 GvSTASH(sv) = (HV*)SvREFCNT_inc(PL_curstash ? (SV*)PL_curstash : (SV*)PL_defstash);
194 SvFLAGS(sv) |= SVpad_OUR;
196 av_store(PL_comppad_name, off, sv);
197 SvNVX(sv) = (NV)PAD_MAX;
198 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
199 if (!PL_min_intro_pending)
200 PL_min_intro_pending = off;
201 PL_max_intro_pending = off;
203 av_store(PL_comppad, off, (SV*)newAV());
204 else if (*name == '%')
205 av_store(PL_comppad, off, (SV*)newHV());
206 SvPADMY_on(PL_curpad[off]);
211 S_pad_addlex(pTHX_ SV *proto_namesv)
213 SV *namesv = NEWSV(1103,0);
214 PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY);
215 sv_upgrade(namesv, SVt_PVNV);
216 sv_setpv(namesv, SvPVX(proto_namesv));
217 av_store(PL_comppad_name, newoff, namesv);
218 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
219 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
220 SvFAKE_on(namesv); /* A ref, not a real var */
221 if (SvFLAGS(proto_namesv) & SVpad_OUR) { /* An "our" variable */
222 SvFLAGS(namesv) |= SVpad_OUR;
223 (void)SvUPGRADE(namesv, SVt_PVGV);
224 GvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)GvSTASH(proto_namesv));
226 if (SvOBJECT(proto_namesv)) { /* A typed var */
228 (void)SvUPGRADE(namesv, SVt_PVMG);
229 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(proto_namesv));
235 #define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
238 S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
239 I32 cx_ix, I32 saweval, U32 flags)
246 register PERL_CONTEXT *cx;
248 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
249 AV *curlist = CvPADLIST(cv);
250 SV **svp = av_fetch(curlist, 0, FALSE);
253 if (!svp || *svp == &PL_sv_undef)
256 svp = AvARRAY(curname);
257 for (off = AvFILLp(curname); off > 0; off--) {
258 if ((sv = svp[off]) &&
259 sv != &PL_sv_undef &&
261 seq > I_32(SvNVX(sv)) &&
262 strEQ(SvPVX(sv), name))
273 return 0; /* don't clone from inactive stack frame */
277 oldpad = (AV*)AvARRAY(curlist)[depth];
278 oldsv = *av_fetch(oldpad, off, TRUE);
279 if (!newoff) { /* Not a mere clone operation. */
280 newoff = pad_addlex(sv);
281 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
282 /* "It's closures all the way down." */
283 CvCLONE_on(PL_compcv);
285 if (CvANON(PL_compcv))
286 oldsv = Nullsv; /* no need to keep ref */
291 bcv && bcv != cv && !CvCLONE(bcv);
292 bcv = CvOUTSIDE(bcv))
295 /* install the missing pad entry in intervening
296 * nested subs and mark them cloneable.
297 * XXX fix pad_foo() to not use globals */
298 AV *ocomppad_name = PL_comppad_name;
299 AV *ocomppad = PL_comppad;
300 SV **ocurpad = PL_curpad;
301 AV *padlist = CvPADLIST(bcv);
302 PL_comppad_name = (AV*)AvARRAY(padlist)[0];
303 PL_comppad = (AV*)AvARRAY(padlist)[1];
304 PL_curpad = AvARRAY(PL_comppad);
306 PL_comppad_name = ocomppad_name;
307 PL_comppad = ocomppad;
312 if (ckWARN(WARN_CLOSURE)
313 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
315 Perl_warner(aTHX_ WARN_CLOSURE,
316 "Variable \"%s\" may be unavailable",
324 else if (!CvUNIQUE(PL_compcv)) {
325 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv)
326 && !(SvFLAGS(sv) & SVpad_OUR))
328 Perl_warner(aTHX_ WARN_CLOSURE,
329 "Variable \"%s\" will not stay shared", name);
333 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
339 if (flags & FINDLEX_NOSEARCH)
342 /* Nothing in current lexical context--try eval's context, if any.
343 * This is necessary to let the perldb get at lexically scoped variables.
344 * XXX This will also probably interact badly with eval tree caching.
347 for (i = cx_ix; i >= 0; i--) {
349 switch (CxTYPE(cx)) {
351 if (i == 0 && saweval) {
352 seq = cxstack[saweval].blk_oldcop->cop_seq;
353 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
357 switch (cx->blk_eval.old_op_type) {
364 /* require/do must have their own scope */
373 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
374 saweval = i; /* so we know where we were called from */
377 seq = cxstack[saweval].blk_oldcop->cop_seq;
378 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
386 Perl_pad_findmy(pTHX_ char *name)
392 SV **svp = AvARRAY(PL_comppad_name);
393 U32 seq = PL_cop_seqmax;
399 * Special case to get lexical (and hence per-thread) @_.
400 * XXX I need to find out how to tell at parse-time whether use
401 * of @_ should refer to a lexical (from a sub) or defgv (global
402 * scope and maybe weird sub-ish things like formats). See
403 * startsub in perly.y. It's possible that @_ could be lexical
404 * (at least from subs) even in non-threaded perl.
406 if (strEQ(name, "@_"))
407 return 0; /* success. (NOT_IN_PAD indicates failure) */
408 #endif /* USE_THREADS */
410 /* The one we're looking for is probably just before comppad_name_fill. */
411 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
412 if ((sv = svp[off]) &&
413 sv != &PL_sv_undef &&
416 seq > I_32(SvNVX(sv)))) &&
417 strEQ(SvPVX(sv), name))
419 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
420 return (PADOFFSET)off;
421 pendoff = off; /* this pending def. will override import */
425 outside = CvOUTSIDE(PL_compcv);
427 /* Check if if we're compiling an eval'', and adjust seq to be the
428 * eval's seq number. This depends on eval'' having a non-null
429 * CvOUTSIDE() while it is being compiled. The eval'' itself is
430 * identified by CvEVAL being true and CvGV being null. */
431 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
432 cx = &cxstack[cxstack_ix];
434 seq = cx->blk_oldcop->cop_seq;
437 /* See if it's in a nested scope */
438 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
440 /* If there is a pending local definition, this new alias must die */
442 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
443 return off; /* pad_findlex returns 0 for failure...*/
445 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
449 Perl_pad_leavemy(pTHX_ I32 fill)
453 SV **svp = AvARRAY(PL_comppad_name);
455 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
456 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
457 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
458 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
461 /* "Deintroduce" my variables that are leaving with this scope. */
462 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
463 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
464 SvIVX(sv) = PL_cop_seqmax;
469 Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
475 if (AvARRAY(PL_comppad) != PL_curpad)
476 Perl_croak(aTHX_ "panic: pad_alloc");
477 if (PL_pad_reset_pending)
479 if (tmptype & SVs_PADMY) {
481 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
482 } while (SvPADBUSY(sv)); /* need a fresh one */
483 retval = AvFILLp(PL_comppad);
486 SV **names = AvARRAY(PL_comppad_name);
487 SSize_t names_fill = AvFILLp(PL_comppad_name);
490 * "foreach" index vars temporarily become aliases to non-"my"
491 * values. Thus we must skip, not just pad values that are
492 * marked as current pad values, but also those with names.
494 if (++PL_padix <= names_fill &&
495 (sv = names[PL_padix]) && sv != &PL_sv_undef)
497 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
498 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) && !IS_PADGV(sv))
503 SvFLAGS(sv) |= tmptype;
504 PL_curpad = AvARRAY(PL_comppad);
506 DEBUG_X(PerlIO_printf(Perl_debug_log,
507 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
508 PTR2UV(thr), PTR2UV(PL_curpad),
509 (long) retval, PL_op_name[optype]));
511 DEBUG_X(PerlIO_printf(Perl_debug_log,
512 "Pad 0x%"UVxf" alloc %ld for %s\n",
514 (long) retval, PL_op_name[optype]));
515 #endif /* USE_THREADS */
516 return (PADOFFSET)retval;
520 Perl_pad_sv(pTHX_ PADOFFSET po)
524 DEBUG_X(PerlIO_printf(Perl_debug_log,
525 "0x%"UVxf" Pad 0x%"UVxf" sv %"IVdf"\n",
526 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
529 Perl_croak(aTHX_ "panic: pad_sv po");
530 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %"IVdf"\n",
531 PTR2UV(PL_curpad), (IV)po));
532 #endif /* USE_THREADS */
533 return PL_curpad[po]; /* eventually we'll turn this into a macro */
537 Perl_pad_free(pTHX_ PADOFFSET po)
542 if (AvARRAY(PL_comppad) != PL_curpad)
543 Perl_croak(aTHX_ "panic: pad_free curpad");
545 Perl_croak(aTHX_ "panic: pad_free po");
547 DEBUG_X(PerlIO_printf(Perl_debug_log,
548 "0x%"UVxf" Pad 0x%"UVxf" free %"IVdf"\n",
549 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
551 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %"IVdf"\n",
552 PTR2UV(PL_curpad), (IV)po));
553 #endif /* USE_THREADS */
554 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef) {
555 SvPADTMP_off(PL_curpad[po]);
557 SvREADONLY_off(PL_curpad[po]); /* could be a freed constant */
560 if ((I32)po < PL_padix)
565 Perl_pad_swipe(pTHX_ PADOFFSET po)
568 if (AvARRAY(PL_comppad) != PL_curpad)
569 Perl_croak(aTHX_ "panic: pad_swipe curpad");
571 Perl_croak(aTHX_ "panic: pad_swipe po");
573 DEBUG_X(PerlIO_printf(Perl_debug_log,
574 "0x%"UVxf" Pad 0x%"UVxf" swipe %"IVdf"\n",
575 PTR2UV(thr), PTR2UV(PL_curpad), (IV)po));
577 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %"IVdf"\n",
578 PTR2UV(PL_curpad), (IV)po));
579 #endif /* USE_THREADS */
580 SvPADTMP_off(PL_curpad[po]);
581 PL_curpad[po] = NEWSV(1107,0);
582 SvPADTMP_on(PL_curpad[po]);
583 if ((I32)po < PL_padix)
587 /* XXX pad_reset() is currently disabled because it results in serious bugs.
588 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
589 * on the stack by OPs that use them, there are several ways to get an alias
590 * to a shared TARG. Such an alias will change randomly and unpredictably.
591 * We avoid doing this until we can think of a Better Way.
596 #ifdef USE_BROKEN_PAD_RESET
600 if (AvARRAY(PL_comppad) != PL_curpad)
601 Perl_croak(aTHX_ "panic: pad_reset curpad");
603 DEBUG_X(PerlIO_printf(Perl_debug_log,
604 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
605 PTR2UV(thr), PTR2UV(PL_curpad)));
607 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
609 #endif /* USE_THREADS */
610 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
611 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
612 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
613 SvPADTMP_off(PL_curpad[po]);
615 PL_padix = PL_padix_floor;
618 PL_pad_reset_pending = FALSE;
622 /* find_threadsv is not reentrant */
624 Perl_find_threadsv(pTHX_ const char *name)
630 /* We currently only handle names of a single character */
631 p = strchr(PL_threadsv_names, *name);
634 key = p - PL_threadsv_names;
635 MUTEX_LOCK(&thr->mutex);
636 svp = av_fetch(thr->threadsv, key, FALSE);
638 MUTEX_UNLOCK(&thr->mutex);
640 SV *sv = NEWSV(0, 0);
641 av_store(thr->threadsv, key, sv);
642 thr->threadsvp = AvARRAY(thr->threadsv);
643 MUTEX_UNLOCK(&thr->mutex);
645 * Some magic variables used to be automagically initialised
646 * in gv_fetchpv. Those which are now per-thread magicals get
647 * initialised here instead.
653 sv_setpv(sv, "\034");
654 sv_magic(sv, 0, 0, name, 1);
659 PL_sawampersand = TRUE;
673 /* XXX %! tied to Errno.pm needs to be added here.
674 * See gv_fetchpv(). */
678 sv_magic(sv, 0, 0, name, 1);
680 DEBUG_S(PerlIO_printf(Perl_error_log,
681 "find_threadsv: new SV %p for $%s%c\n",
682 sv, (*name < 32) ? "^" : "",
683 (*name < 32) ? toCTRL(*name) : *name));
687 #endif /* USE_THREADS */
692 Perl_op_free(pTHX_ OP *o)
694 register OP *kid, *nextkid;
697 if (!o || o->op_seq == (U16)-1)
700 if (o->op_private & OPpREFCOUNTED) {
701 switch (o->op_type) {
709 if (OpREFCNT_dec(o)) {
720 if (o->op_flags & OPf_KIDS) {
721 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
722 nextkid = kid->op_sibling; /* Get before next freeing kid */
730 /* COP* is not cleared by op_clear() so that we may track line
731 * numbers etc even after null() */
732 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
737 #ifdef PL_OP_SLAB_ALLOC
738 if ((char *) o == PL_OpPtr)
747 S_op_clear(pTHX_ OP *o)
749 switch (o->op_type) {
750 case OP_NULL: /* Was holding old type, if any. */
751 case OP_ENTEREVAL: /* Was holding hints. */
753 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
759 if (!(o->op_flags & OPf_SPECIAL))
762 #endif /* USE_THREADS */
764 if (!(o->op_flags & OPf_REF)
765 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
772 if (cPADOPo->op_padix > 0) {
775 pad_swipe(cPADOPo->op_padix);
776 /* No GvIN_PAD_off(gv) here, because other references may still
777 * exist on the pad */
780 cPADOPo->op_padix = 0;
783 SvREFCNT_dec(cSVOPo->op_sv);
784 cSVOPo->op_sv = Nullsv;
787 case OP_METHOD_NAMED:
789 SvREFCNT_dec(cSVOPo->op_sv);
790 cSVOPo->op_sv = Nullsv;
796 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
800 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
801 SvREFCNT_dec(cSVOPo->op_sv);
802 cSVOPo->op_sv = Nullsv;
805 Safefree(cPVOPo->op_pv);
806 cPVOPo->op_pv = Nullch;
810 op_free(cPMOPo->op_pmreplroot);
814 if ((PADOFFSET)cPMOPo->op_pmreplroot) {
816 GV *gv = (GV*)PL_curpad[(PADOFFSET)cPMOPo->op_pmreplroot];
817 pad_swipe((PADOFFSET)cPMOPo->op_pmreplroot);
818 /* No GvIN_PAD_off(gv) here, because other references may still
819 * exist on the pad */
824 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
830 cPMOPo->op_pmreplroot = Nullop;
831 ReREFCNT_dec(cPMOPo->op_pmregexp);
832 cPMOPo->op_pmregexp = (REGEXP*)NULL;
836 if (o->op_targ > 0) {
837 pad_free(o->op_targ);
843 S_cop_free(pTHX_ COP* cop)
845 Safefree(cop->cop_label);
847 Safefree(CopFILE(cop)); /* XXX share in a pvtable? */
848 Safefree(CopSTASHPV(cop)); /* XXX share in a pvtable? */
850 /* NOTE: COP.cop_stash is not refcounted */
851 SvREFCNT_dec(CopFILEGV(cop));
853 if (! specialWARN(cop->cop_warnings))
854 SvREFCNT_dec(cop->cop_warnings);
860 if (o->op_type == OP_NULL)
863 o->op_targ = o->op_type;
864 o->op_type = OP_NULL;
865 o->op_ppaddr = PL_ppaddr[OP_NULL];
868 /* Contextualizers */
870 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
873 Perl_linklist(pTHX_ OP *o)
880 /* establish postfix order */
881 if (cUNOPo->op_first) {
882 o->op_next = LINKLIST(cUNOPo->op_first);
883 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
885 kid->op_next = LINKLIST(kid->op_sibling);
897 Perl_scalarkids(pTHX_ OP *o)
900 if (o && o->op_flags & OPf_KIDS) {
901 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
908 S_scalarboolean(pTHX_ OP *o)
910 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
912 if (ckWARN(WARN_SYNTAX)) {
913 line_t oldline = CopLINE(PL_curcop);
915 if (PL_copline != NOLINE)
916 CopLINE_set(PL_curcop, PL_copline);
917 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
918 CopLINE_set(PL_curcop, oldline);
925 Perl_scalar(pTHX_ OP *o)
929 /* assumes no premature commitment */
930 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
931 || o->op_type == OP_RETURN)
936 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
938 switch (o->op_type) {
940 if (o->op_private & OPpREPEAT_DOLIST)
941 null(((LISTOP*)cBINOPo->op_first)->op_first);
942 scalar(cBINOPo->op_first);
947 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
951 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
952 if (!kPMOP->op_pmreplroot)
953 deprecate("implicit split to @_");
961 if (o->op_flags & OPf_KIDS) {
962 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
968 kid = cLISTOPo->op_first;
970 while ((kid = kid->op_sibling)) {
976 WITH_THR(PL_curcop = &PL_compiling);
981 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
987 WITH_THR(PL_curcop = &PL_compiling);
994 Perl_scalarvoid(pTHX_ OP *o)
1001 if (o->op_type == OP_NEXTSTATE
1002 || o->op_type == OP_SETSTATE
1003 || o->op_type == OP_DBSTATE
1004 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1005 || o->op_targ == OP_SETSTATE
1006 || o->op_targ == OP_DBSTATE)))
1009 PL_curcop = (COP*)o; /* for warning below */
1012 /* assumes no premature commitment */
1013 want = o->op_flags & OPf_WANT;
1014 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
1015 || o->op_type == OP_RETURN)
1020 if ((o->op_private & OPpTARGET_MY)
1021 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1023 return scalar(o); /* As if inside SASSIGN */
1026 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1028 switch (o->op_type) {
1030 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1034 if (o->op_flags & OPf_STACKED)
1038 if (o->op_private == 4)
1080 case OP_GETSOCKNAME:
1081 case OP_GETPEERNAME:
1086 case OP_GETPRIORITY:
1109 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1110 useless = PL_op_desc[o->op_type];
1117 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1118 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1119 useless = "a variable";
1124 if (cSVOPo->op_private & OPpCONST_STRICT)
1125 no_bareword_allowed(o);
1128 if (ckWARN(WARN_VOID)) {
1129 useless = "a constant";
1130 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1132 else if (SvPOK(sv)) {
1133 if (strnEQ(SvPVX(sv), "di", 2) ||
1134 strnEQ(SvPVX(sv), "ds", 2) ||
1135 strnEQ(SvPVX(sv), "ig", 2))
1140 null(o); /* don't execute or even remember it */
1144 o->op_type = OP_PREINC; /* pre-increment is faster */
1145 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1149 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1150 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1156 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1161 if (o->op_flags & OPf_STACKED)
1168 if (!(o->op_flags & OPf_KIDS))
1177 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1184 /* all requires must return a boolean value */
1185 o->op_flags &= ~OPf_WANT;
1190 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
1191 if (!kPMOP->op_pmreplroot)
1192 deprecate("implicit split to @_");
1198 if (ckWARN(WARN_VOID))
1199 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
1205 Perl_listkids(pTHX_ OP *o)
1208 if (o && o->op_flags & OPf_KIDS) {
1209 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1216 Perl_list(pTHX_ OP *o)
1220 /* assumes no premature commitment */
1221 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
1222 || o->op_type == OP_RETURN)
1227 if ((o->op_private & OPpTARGET_MY)
1228 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1230 return o; /* As if inside SASSIGN */
1233 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1235 switch (o->op_type) {
1238 list(cBINOPo->op_first);
1243 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1251 if (!(o->op_flags & OPf_KIDS))
1253 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1254 list(cBINOPo->op_first);
1255 return gen_constant_list(o);
1262 kid = cLISTOPo->op_first;
1264 while ((kid = kid->op_sibling)) {
1265 if (kid->op_sibling)
1270 WITH_THR(PL_curcop = &PL_compiling);
1274 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1275 if (kid->op_sibling)
1280 WITH_THR(PL_curcop = &PL_compiling);
1283 /* all requires must return a boolean value */
1284 o->op_flags &= ~OPf_WANT;
1291 Perl_scalarseq(pTHX_ OP *o)
1296 if (o->op_type == OP_LINESEQ ||
1297 o->op_type == OP_SCOPE ||
1298 o->op_type == OP_LEAVE ||
1299 o->op_type == OP_LEAVETRY)
1302 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1303 if (kid->op_sibling) {
1307 PL_curcop = &PL_compiling;
1309 o->op_flags &= ~OPf_PARENS;
1310 if (PL_hints & HINT_BLOCK_SCOPE)
1311 o->op_flags |= OPf_PARENS;
1314 o = newOP(OP_STUB, 0);
1319 S_modkids(pTHX_ OP *o, I32 type)
1322 if (o && o->op_flags & OPf_KIDS) {
1323 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1330 Perl_mod(pTHX_ OP *o, I32 type)
1336 if (!o || PL_error_count)
1339 if ((o->op_private & OPpTARGET_MY)
1340 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1345 switch (o->op_type) {
1350 if (!(o->op_private & (OPpCONST_ARYBASE)))
1352 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1353 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1357 SAVEI32(PL_compiling.cop_arybase);
1358 PL_compiling.cop_arybase = 0;
1360 else if (type == OP_REFGEN)
1363 Perl_croak(aTHX_ "That use of $[ is unsupported");
1366 if (o->op_flags & OPf_PARENS)
1370 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1371 !(o->op_flags & OPf_STACKED)) {
1372 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1373 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1374 assert(cUNOPo->op_first->op_type == OP_NULL);
1375 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1378 else { /* lvalue subroutine call */
1379 o->op_private |= OPpLVAL_INTRO;
1380 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1381 /* Backward compatibility mode: */
1382 o->op_private |= OPpENTERSUB_INARGS;
1385 else { /* Compile-time error message: */
1386 OP *kid = cUNOPo->op_first;
1390 if (kid->op_type == OP_PUSHMARK)
1392 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1394 "panic: unexpected lvalue entersub "
1395 "args: type/targ %ld:%ld",
1396 (long)kid->op_type,kid->op_targ);
1397 kid = kLISTOP->op_first;
1399 while (kid->op_sibling)
1400 kid = kid->op_sibling;
1401 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1403 if (kid->op_type == OP_METHOD_NAMED
1404 || kid->op_type == OP_METHOD)
1408 if (kid->op_sibling || kid->op_next != kid) {
1409 yyerror("panic: unexpected optree near method call");
1413 NewOp(1101, newop, 1, UNOP);
1414 newop->op_type = OP_RV2CV;
1415 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1416 newop->op_first = Nullop;
1417 newop->op_next = (OP*)newop;
1418 kid->op_sibling = (OP*)newop;
1419 newop->op_private |= OPpLVAL_INTRO;
1423 if (kid->op_type != OP_RV2CV)
1425 "panic: unexpected lvalue entersub "
1426 "entry via type/targ %ld:%ld",
1427 (long)kid->op_type,kid->op_targ);
1428 kid->op_private |= OPpLVAL_INTRO;
1429 break; /* Postpone until runtime */
1433 kid = kUNOP->op_first;
1434 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1435 kid = kUNOP->op_first;
1436 if (kid->op_type == OP_NULL)
1438 "Unexpected constant lvalue entersub "
1439 "entry via type/targ %ld:%ld",
1440 (long)kid->op_type,kid->op_targ);
1441 if (kid->op_type != OP_GV) {
1442 /* Restore RV2CV to check lvalueness */
1444 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1445 okid->op_next = kid->op_next;
1446 kid->op_next = okid;
1449 okid->op_next = Nullop;
1450 okid->op_type = OP_RV2CV;
1452 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1453 okid->op_private |= OPpLVAL_INTRO;
1457 cv = GvCV(kGVOP_gv);
1467 /* grep, foreach, subcalls, refgen */
1468 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1470 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1471 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1473 : (o->op_type == OP_ENTERSUB
1474 ? "non-lvalue subroutine call"
1475 : PL_op_desc[o->op_type])),
1476 type ? PL_op_desc[type] : "local"));
1490 case OP_RIGHT_SHIFT:
1499 if (!(o->op_flags & OPf_STACKED))
1505 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1511 if (!type && cUNOPo->op_first->op_type != OP_GV)
1512 Perl_croak(aTHX_ "Can't localize through a reference");
1513 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1514 PL_modcount = 10000;
1515 return o; /* Treat \(@foo) like ordinary list. */
1519 if (scalar_mod_type(o, type))
1521 ref(cUNOPo->op_first, o->op_type);
1530 PL_modcount = 10000;
1533 if (!type && cUNOPo->op_first->op_type != OP_GV)
1534 Perl_croak(aTHX_ "Can't localize through a reference");
1535 ref(cUNOPo->op_first, o->op_type);
1539 PL_hints |= HINT_BLOCK_SCOPE;
1549 PL_modcount = 10000;
1550 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1551 return o; /* Treat \(@foo) like ordinary list. */
1552 if (scalar_mod_type(o, type))
1558 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1559 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
1564 PL_modcount++; /* XXX ??? */
1566 #endif /* USE_THREADS */
1572 if (type != OP_SASSIGN)
1576 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1582 pad_free(o->op_targ);
1583 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1584 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1585 if (o->op_flags & OPf_KIDS)
1586 mod(cBINOPo->op_first->op_sibling, type);
1591 ref(cBINOPo->op_first, o->op_type);
1592 if (type == OP_ENTERSUB &&
1593 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1594 o->op_private |= OPpLVAL_DEFER;
1601 if (o->op_flags & OPf_KIDS)
1602 mod(cLISTOPo->op_last, type);
1606 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1608 else if (!(o->op_flags & OPf_KIDS))
1610 if (o->op_targ != OP_LIST) {
1611 mod(cBINOPo->op_first, type);
1616 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1620 o->op_flags |= OPf_MOD;
1622 if (type == OP_AASSIGN || type == OP_SASSIGN)
1623 o->op_flags |= OPf_SPECIAL|OPf_REF;
1625 o->op_private |= OPpLVAL_INTRO;
1626 o->op_flags &= ~OPf_SPECIAL;
1627 PL_hints |= HINT_BLOCK_SCOPE;
1629 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1630 o->op_flags |= OPf_REF;
1635 S_scalar_mod_type(pTHX_ OP *o, I32 type)
1639 if (o->op_type == OP_RV2GV)
1663 case OP_RIGHT_SHIFT:
1682 S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
1684 switch (o->op_type) {
1692 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1705 Perl_refkids(pTHX_ OP *o, I32 type)
1708 if (o && o->op_flags & OPf_KIDS) {
1709 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1716 Perl_ref(pTHX_ OP *o, I32 type)
1720 if (!o || PL_error_count)
1723 switch (o->op_type) {
1725 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1726 !(o->op_flags & OPf_STACKED)) {
1727 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1728 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1729 assert(cUNOPo->op_first->op_type == OP_NULL);
1730 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1731 o->op_flags |= OPf_SPECIAL;
1736 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1740 if (type == OP_DEFINED)
1741 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1742 ref(cUNOPo->op_first, o->op_type);
1745 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1746 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1747 : type == OP_RV2HV ? OPpDEREF_HV
1749 o->op_flags |= OPf_MOD;
1754 o->op_flags |= OPf_MOD; /* XXX ??? */
1759 o->op_flags |= OPf_REF;
1762 if (type == OP_DEFINED)
1763 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1764 ref(cUNOPo->op_first, o->op_type);
1769 o->op_flags |= OPf_REF;
1774 if (!(o->op_flags & OPf_KIDS))
1776 ref(cBINOPo->op_first, type);
1780 ref(cBINOPo->op_first, o->op_type);
1781 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1782 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1783 : type == OP_RV2HV ? OPpDEREF_HV
1785 o->op_flags |= OPf_MOD;
1793 if (!(o->op_flags & OPf_KIDS))
1795 ref(cLISTOPo->op_last, type);
1805 S_dup_attrlist(pTHX_ OP *o)
1809 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1810 * where the first kid is OP_PUSHMARK and the remaining ones
1811 * are OP_CONST. We need to push the OP_CONST values.
1813 if (o->op_type == OP_CONST)
1814 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1816 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1817 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1818 if (o->op_type == OP_CONST)
1819 rop = append_elem(OP_LIST, rop,
1820 newSVOP(OP_CONST, o->op_flags,
1821 SvREFCNT_inc(cSVOPo->op_sv)));
1828 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1832 /* fake up C<use attributes $pkg,$rv,@attrs> */
1833 ENTER; /* need to protect against side-effects of 'use' */
1835 if (stash && HvNAME(stash))
1836 stashsv = newSVpv(HvNAME(stash), 0);
1838 stashsv = &PL_sv_no;
1840 #define ATTRSMODULE "attributes"
1842 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1843 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1845 prepend_elem(OP_LIST,
1846 newSVOP(OP_CONST, 0, stashsv),
1847 prepend_elem(OP_LIST,
1848 newSVOP(OP_CONST, 0,
1850 dup_attrlist(attrs))));
1855 S_my_kid(pTHX_ OP *o, OP *attrs)
1860 if (!o || PL_error_count)
1864 if (type == OP_LIST) {
1865 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1867 } else if (type == OP_UNDEF) {
1869 } else if (type == OP_RV2SV || /* "our" declaration */
1871 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1872 o->op_private |= OPpOUR_INTRO;
1874 } else if (type != OP_PADSV &&
1877 type != OP_PUSHMARK)
1879 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1880 PL_op_desc[o->op_type],
1881 PL_in_my == KEY_our ? "our" : "my"));
1884 else if (attrs && type != OP_PUSHMARK) {
1890 PL_in_my_stash = Nullhv;
1892 /* check for C<my Dog $spot> when deciding package */
1893 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1894 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1895 stash = SvSTASH(*namesvp);
1897 stash = PL_curstash;
1898 padsv = PAD_SV(o->op_targ);
1899 apply_attrs(stash, padsv, attrs);
1901 o->op_flags |= OPf_MOD;
1902 o->op_private |= OPpLVAL_INTRO;
1907 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1909 if (o->op_flags & OPf_PARENS)
1913 o = my_kid(o, attrs);
1915 PL_in_my_stash = Nullhv;
1920 Perl_my(pTHX_ OP *o)
1922 return my_kid(o, Nullop);
1926 Perl_sawparens(pTHX_ OP *o)
1929 o->op_flags |= OPf_PARENS;
1934 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1939 if (ckWARN(WARN_MISC) &&
1940 (left->op_type == OP_RV2AV ||
1941 left->op_type == OP_RV2HV ||
1942 left->op_type == OP_PADAV ||
1943 left->op_type == OP_PADHV)) {
1944 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
1945 right->op_type == OP_TRANS)
1946 ? right->op_type : OP_MATCH];
1947 const char *sample = ((left->op_type == OP_RV2AV ||
1948 left->op_type == OP_PADAV)
1949 ? "@array" : "%hash");
1950 Perl_warner(aTHX_ WARN_MISC,
1951 "Applying %s to %s will act on scalar(%s)",
1952 desc, sample, sample);
1955 if (right->op_type == OP_MATCH ||
1956 right->op_type == OP_SUBST ||
1957 right->op_type == OP_TRANS) {
1958 right->op_flags |= OPf_STACKED;
1959 if (right->op_type != OP_MATCH)
1960 left = mod(left, right->op_type);
1961 if (right->op_type == OP_TRANS)
1962 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1964 o = prepend_elem(right->op_type, scalar(left), right);
1966 return newUNOP(OP_NOT, 0, scalar(o));
1970 return bind_match(type, left,
1971 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1975 Perl_invert(pTHX_ OP *o)
1979 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1980 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1984 Perl_scope(pTHX_ OP *o)
1987 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1988 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1989 o->op_type = OP_LEAVE;
1990 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1993 if (o->op_type == OP_LINESEQ) {
1995 o->op_type = OP_SCOPE;
1996 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1997 kid = ((LISTOP*)o)->op_first;
1998 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
2002 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
2009 Perl_save_hints(pTHX)
2012 SAVESPTR(GvHV(PL_hintgv));
2013 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
2014 SAVEFREESV(GvHV(PL_hintgv));
2018 Perl_block_start(pTHX_ int full)
2021 int retval = PL_savestack_ix;
2023 SAVEI32(PL_comppad_name_floor);
2024 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
2026 PL_comppad_name_fill = PL_comppad_name_floor;
2027 if (PL_comppad_name_floor < 0)
2028 PL_comppad_name_floor = 0;
2029 SAVEI32(PL_min_intro_pending);
2030 SAVEI32(PL_max_intro_pending);
2031 PL_min_intro_pending = 0;
2032 SAVEI32(PL_comppad_name_fill);
2033 SAVEI32(PL_padix_floor);
2034 PL_padix_floor = PL_padix;
2035 PL_pad_reset_pending = FALSE;
2037 PL_hints &= ~HINT_BLOCK_SCOPE;
2038 SAVESPTR(PL_compiling.cop_warnings);
2039 if (! specialWARN(PL_compiling.cop_warnings)) {
2040 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
2041 SAVEFREESV(PL_compiling.cop_warnings) ;
2047 Perl_block_end(pTHX_ I32 floor, OP *seq)
2050 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2051 OP* retval = scalarseq(seq);
2053 PL_pad_reset_pending = FALSE;
2054 PL_compiling.op_private = PL_hints;
2056 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2057 pad_leavemy(PL_comppad_name_fill);
2066 OP *o = newOP(OP_THREADSV, 0);
2067 o->op_targ = find_threadsv("_");
2070 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2071 #endif /* USE_THREADS */
2075 Perl_newPROG(pTHX_ OP *o)
2081 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2082 ((PL_in_eval & EVAL_KEEPERR)
2083 ? OPf_SPECIAL : 0), o);
2084 PL_eval_start = linklist(PL_eval_root);
2085 PL_eval_root->op_private |= OPpREFCOUNTED;
2086 OpREFCNT_set(PL_eval_root, 1);
2087 PL_eval_root->op_next = 0;
2088 peep(PL_eval_start);
2093 PL_main_root = scope(sawparens(scalarvoid(o)));
2094 PL_curcop = &PL_compiling;
2095 PL_main_start = LINKLIST(PL_main_root);
2096 PL_main_root->op_private |= OPpREFCOUNTED;
2097 OpREFCNT_set(PL_main_root, 1);
2098 PL_main_root->op_next = 0;
2099 peep(PL_main_start);
2102 /* Register with debugger */
2104 CV *cv = get_cv("DB::postponed", FALSE);
2108 XPUSHs((SV*)CopFILEGV(&PL_compiling));
2110 call_sv((SV*)cv, G_DISCARD);
2117 Perl_localize(pTHX_ OP *o, I32 lex)
2119 if (o->op_flags & OPf_PARENS)
2123 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
2125 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
2126 if (*s == ';' || *s == '=')
2127 Perl_warner(aTHX_ WARN_PARENTHESIS,
2128 "Parentheses missing around \"%s\" list",
2129 lex ? (PL_in_my == KEY_our ? "our" : "my") : "local");
2135 o = mod(o, OP_NULL); /* a bit kludgey */
2137 PL_in_my_stash = Nullhv;
2142 Perl_jmaybe(pTHX_ OP *o)
2144 if (o->op_type == OP_LIST) {
2147 o2 = newOP(OP_THREADSV, 0);
2148 o2->op_targ = find_threadsv(";");
2150 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2151 #endif /* USE_THREADS */
2152 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2158 Perl_fold_constants(pTHX_ register OP *o)
2162 I32 type = o->op_type;
2165 if (PL_opargs[type] & OA_RETSCALAR)
2167 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2168 o->op_targ = pad_alloc(type, SVs_PADTMP);
2170 /* integerize op, unless it happens to be C<-foo>.
2171 * XXX should pp_i_negate() do magic string negation instead? */
2172 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2173 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2174 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2176 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2179 if (!(PL_opargs[type] & OA_FOLDCONST))
2184 /* XXX might want a ck_negate() for this */
2185 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2198 if (o->op_private & OPpLOCALE)
2203 goto nope; /* Don't try to run w/ errors */
2205 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2206 if ((curop->op_type != OP_CONST ||
2207 (curop->op_private & OPpCONST_BARE)) &&
2208 curop->op_type != OP_LIST &&
2209 curop->op_type != OP_SCALAR &&
2210 curop->op_type != OP_NULL &&
2211 curop->op_type != OP_PUSHMARK)
2217 curop = LINKLIST(o);
2221 sv = *(PL_stack_sp--);
2222 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2223 pad_swipe(o->op_targ);
2224 else if (SvTEMP(sv)) { /* grab mortal temp? */
2225 (void)SvREFCNT_inc(sv);
2229 if (type == OP_RV2GV)
2230 return newGVOP(OP_GV, 0, (GV*)sv);
2232 /* try to smush double to int, but don't smush -2.0 to -2 */
2233 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2237 if ((NV)iv == SvNV(sv)) {
2242 SvIOK_off(sv); /* undo SvIV() damage */
2244 return newSVOP(OP_CONST, 0, sv);
2248 if (!(PL_opargs[type] & OA_OTHERINT))
2251 if (!(PL_hints & HINT_INTEGER)) {
2252 if (type == OP_MODULO
2253 || type == OP_DIVIDE
2254 || !(o->op_flags & OPf_KIDS))
2259 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2260 if (curop->op_type == OP_CONST) {
2261 if (SvIOK(((SVOP*)curop)->op_sv))
2265 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
2269 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
2276 Perl_gen_constant_list(pTHX_ register OP *o)
2280 I32 oldtmps_floor = PL_tmps_floor;
2284 return o; /* Don't attempt to run with errors */
2286 PL_op = curop = LINKLIST(o);
2293 PL_tmps_floor = oldtmps_floor;
2295 o->op_type = OP_RV2AV;
2296 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2297 curop = ((UNOP*)o)->op_first;
2298 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2305 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2310 if (!o || o->op_type != OP_LIST)
2311 o = newLISTOP(OP_LIST, 0, o, Nullop);
2313 o->op_flags &= ~OPf_WANT;
2315 if (!(PL_opargs[type] & OA_MARK))
2316 null(cLISTOPo->op_first);
2319 o->op_ppaddr = PL_ppaddr[type];
2320 o->op_flags |= flags;
2322 o = CHECKOP(type, o);
2323 if (o->op_type != type)
2326 if (cLISTOPo->op_children < 7) {
2327 /* XXX do we really need to do this if we're done appending?? */
2328 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2330 cLISTOPo->op_last = last; /* in case check substituted last arg */
2333 return fold_constants(o);
2336 /* List constructors */
2339 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2347 if (first->op_type != type
2348 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2350 return newLISTOP(type, 0, first, last);
2353 if (first->op_flags & OPf_KIDS)
2354 ((LISTOP*)first)->op_last->op_sibling = last;
2356 first->op_flags |= OPf_KIDS;
2357 ((LISTOP*)first)->op_first = last;
2359 ((LISTOP*)first)->op_last = last;
2360 ((LISTOP*)first)->op_children++;
2365 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2373 if (first->op_type != type)
2374 return prepend_elem(type, (OP*)first, (OP*)last);
2376 if (last->op_type != type)
2377 return append_elem(type, (OP*)first, (OP*)last);
2379 first->op_last->op_sibling = last->op_first;
2380 first->op_last = last->op_last;
2381 first->op_children += last->op_children;
2382 if (first->op_children)
2383 first->op_flags |= OPf_KIDS;
2385 #ifdef PL_OP_SLAB_ALLOC
2393 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2401 if (last->op_type == type) {
2402 if (type == OP_LIST) { /* already a PUSHMARK there */
2403 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2404 ((LISTOP*)last)->op_first->op_sibling = first;
2407 if (!(last->op_flags & OPf_KIDS)) {
2408 ((LISTOP*)last)->op_last = first;
2409 last->op_flags |= OPf_KIDS;
2411 first->op_sibling = ((LISTOP*)last)->op_first;
2412 ((LISTOP*)last)->op_first = first;
2414 ((LISTOP*)last)->op_children++;
2418 return newLISTOP(type, 0, first, last);
2424 Perl_newNULLLIST(pTHX)
2426 return newOP(OP_STUB, 0);
2430 Perl_force_list(pTHX_ OP *o)
2432 if (!o || o->op_type != OP_LIST)
2433 o = newLISTOP(OP_LIST, 0, o, Nullop);
2439 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2443 NewOp(1101, listop, 1, LISTOP);
2445 listop->op_type = type;
2446 listop->op_ppaddr = PL_ppaddr[type];
2447 listop->op_children = (first != 0) + (last != 0);
2448 listop->op_flags = flags;
2452 else if (!first && last)
2455 first->op_sibling = last;
2456 listop->op_first = first;
2457 listop->op_last = last;
2458 if (type == OP_LIST) {
2460 pushop = newOP(OP_PUSHMARK, 0);
2461 pushop->op_sibling = first;
2462 listop->op_first = pushop;
2463 listop->op_flags |= OPf_KIDS;
2465 listop->op_last = pushop;
2467 else if (listop->op_children)
2468 listop->op_flags |= OPf_KIDS;
2474 Perl_newOP(pTHX_ I32 type, I32 flags)
2477 NewOp(1101, o, 1, OP);
2479 o->op_ppaddr = PL_ppaddr[type];
2480 o->op_flags = flags;
2483 o->op_private = 0 + (flags >> 8);
2484 if (PL_opargs[type] & OA_RETSCALAR)
2486 if (PL_opargs[type] & OA_TARGET)
2487 o->op_targ = pad_alloc(type, SVs_PADTMP);
2488 return CHECKOP(type, o);
2492 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2497 first = newOP(OP_STUB, 0);
2498 if (PL_opargs[type] & OA_MARK)
2499 first = force_list(first);
2501 NewOp(1101, unop, 1, UNOP);
2502 unop->op_type = type;
2503 unop->op_ppaddr = PL_ppaddr[type];
2504 unop->op_first = first;
2505 unop->op_flags = flags | OPf_KIDS;
2506 unop->op_private = 1 | (flags >> 8);
2507 unop = (UNOP*) CHECKOP(type, unop);
2511 return fold_constants((OP *) unop);
2515 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2518 NewOp(1101, binop, 1, BINOP);
2521 first = newOP(OP_NULL, 0);
2523 binop->op_type = type;
2524 binop->op_ppaddr = PL_ppaddr[type];
2525 binop->op_first = first;
2526 binop->op_flags = flags | OPf_KIDS;
2529 binop->op_private = 1 | (flags >> 8);
2532 binop->op_private = 2 | (flags >> 8);
2533 first->op_sibling = last;
2536 binop = (BINOP*)CHECKOP(type, binop);
2537 if (binop->op_next || binop->op_type != type)
2540 binop->op_last = binop->op_first->op_sibling;
2542 return fold_constants((OP *)binop);
2546 utf8compare(const void *a, const void *b)
2549 for (i = 0; i < 10; i++) {
2550 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2552 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2559 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2561 SV *tstr = ((SVOP*)expr)->op_sv;
2562 SV *rstr = ((SVOP*)repl)->op_sv;
2565 register U8 *t = (U8*)SvPV(tstr, tlen);
2566 register U8 *r = (U8*)SvPV(rstr, rlen);
2572 register short *tbl;
2574 complement = o->op_private & OPpTRANS_COMPLEMENT;
2575 del = o->op_private & OPpTRANS_DELETE;
2576 squash = o->op_private & OPpTRANS_SQUASH;
2579 o->op_private |= OPpTRANS_FROM_UTF;
2582 o->op_private |= OPpTRANS_TO_UTF;
2584 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2585 SV* listsv = newSVpvn("# comment\n",10);
2587 U8* tend = t + tlen;
2588 U8* rend = r + rlen;
2603 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2604 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2607 U8 tmpbuf[UTF8_MAXLEN];
2610 New(1109, cp, tlen, U8*);
2612 transv = newSVpvn("",0);
2621 qsort(cp, i, sizeof(U8*), utf8compare);
2622 for (j = 0; j < i; j++) {
2624 UV val = utf8_to_uv(s, &ulen);
2626 diff = val - nextmin;
2628 t = uv_to_utf8(tmpbuf,nextmin);
2629 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2631 t = uv_to_utf8(tmpbuf, val - 1);
2632 sv_catpvn(transv, "\377", 1);
2633 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2637 val = utf8_to_uv(s+1, &ulen);
2641 t = uv_to_utf8(tmpbuf,nextmin);
2642 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2643 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2644 sv_catpvn(transv, "\377", 1);
2645 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2646 t = (U8*)SvPVX(transv);
2647 tlen = SvCUR(transv);
2650 else if (!rlen && !del) {
2651 r = t; rlen = tlen; rend = tend;
2655 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2656 o->op_private |= OPpTRANS_IDENTICAL;
2659 while (t < tend || tfirst <= tlast) {
2660 /* see if we need more "t" chars */
2661 if (tfirst > tlast) {
2662 tfirst = (I32)utf8_to_uv(t, &ulen);
2664 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2665 tlast = (I32)utf8_to_uv(++t, &ulen);
2672 /* now see if we need more "r" chars */
2673 if (rfirst > rlast) {
2675 rfirst = (I32)utf8_to_uv(r, &ulen);
2677 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2678 rlast = (I32)utf8_to_uv(++r, &ulen);
2687 rfirst = rlast = 0xffffffff;
2691 /* now see which range will peter our first, if either. */
2692 tdiff = tlast - tfirst;
2693 rdiff = rlast - rfirst;
2700 if (rfirst == 0xffffffff) {
2701 diff = tdiff; /* oops, pretend rdiff is infinite */
2703 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2704 (long)tfirst, (long)tlast);
2706 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2710 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2711 (long)tfirst, (long)(tfirst + diff),
2714 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2715 (long)tfirst, (long)rfirst);
2717 if (rfirst + diff > max)
2718 max = rfirst + diff;
2723 else if (rfirst <= 0x800)
2724 grows |= (tfirst < 0x80);
2725 else if (rfirst <= 0x10000)
2726 grows |= (tfirst < 0x800);
2727 else if (rfirst <= 0x200000)
2728 grows |= (tfirst < 0x10000);
2729 else if (rfirst <= 0x4000000)
2730 grows |= (tfirst < 0x200000);
2731 else if (rfirst <= 0x80000000)
2732 grows |= (tfirst < 0x4000000);
2744 else if (max > 0xff)
2749 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2750 SvREFCNT_dec(listsv);
2752 SvREFCNT_dec(transv);
2754 if (!del && havefinal)
2755 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2756 newSVuv((UV)final), 0);
2758 if (grows && to_utf)
2759 o->op_private |= OPpTRANS_GROWS;
2766 tbl = (short*)cPVOPo->op_pv;
2768 Zero(tbl, 256, short);
2769 for (i = 0; i < tlen; i++)
2771 for (i = 0, j = 0; i < 256; i++) {
2787 if (!rlen && !del) {
2790 o->op_private |= OPpTRANS_IDENTICAL;
2792 for (i = 0; i < 256; i++)
2794 for (i = 0, j = 0; i < tlen; i++,j++) {
2797 if (tbl[t[i]] == -1)
2803 if (tbl[t[i]] == -1)
2814 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2819 NewOp(1101, pmop, 1, PMOP);
2820 pmop->op_type = type;
2821 pmop->op_ppaddr = PL_ppaddr[type];
2822 pmop->op_flags = flags;
2823 pmop->op_private = 0 | (flags >> 8);
2825 if (PL_hints & HINT_RE_TAINT)
2826 pmop->op_pmpermflags |= PMf_RETAINT;
2827 if (PL_hints & HINT_LOCALE)
2828 pmop->op_pmpermflags |= PMf_LOCALE;
2829 pmop->op_pmflags = pmop->op_pmpermflags;
2831 /* link into pm list */
2832 if (type != OP_TRANS && PL_curstash) {
2833 pmop->op_pmnext = HvPMROOT(PL_curstash);
2834 HvPMROOT(PL_curstash) = pmop;
2841 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2846 I32 repl_has_vars = 0;
2848 if (o->op_type == OP_TRANS)
2849 return pmtrans(o, expr, repl);
2851 PL_hints |= HINT_BLOCK_SCOPE;
2854 if (expr->op_type == OP_CONST) {
2856 SV *pat = ((SVOP*)expr)->op_sv;
2857 char *p = SvPV(pat, plen);
2858 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2859 sv_setpvn(pat, "\\s+", 3);
2860 p = SvPV(pat, plen);
2861 pm->op_pmflags |= PMf_SKIPWHITE;
2863 if ((PL_hints & HINT_UTF8) || (SvUTF8(pat) && !(PL_hints & HINT_BYTE)))
2864 pm->op_pmdynflags |= PMdf_UTF8;
2865 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2866 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2867 pm->op_pmflags |= PMf_WHITE;
2871 if (PL_hints & HINT_UTF8)
2872 pm->op_pmdynflags |= PMdf_UTF8;
2873 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2874 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2876 : OP_REGCMAYBE),0,expr);
2878 NewOp(1101, rcop, 1, LOGOP);
2879 rcop->op_type = OP_REGCOMP;
2880 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2881 rcop->op_first = scalar(expr);
2882 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2883 ? (OPf_SPECIAL | OPf_KIDS)
2885 rcop->op_private = 1;
2888 /* establish postfix order */
2889 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2891 rcop->op_next = expr;
2892 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2895 rcop->op_next = LINKLIST(expr);
2896 expr->op_next = (OP*)rcop;
2899 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2904 if (pm->op_pmflags & PMf_EVAL) {
2906 if (CopLINE(PL_curcop) < PL_multi_end)
2907 CopLINE_set(PL_curcop, PL_multi_end);
2910 else if (repl->op_type == OP_THREADSV
2911 && strchr("&`'123456789+",
2912 PL_threadsv_names[repl->op_targ]))
2916 #endif /* USE_THREADS */
2917 else if (repl->op_type == OP_CONST)
2921 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2922 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2924 if (curop->op_type == OP_THREADSV) {
2926 if (strchr("&`'123456789+", curop->op_private))
2930 if (curop->op_type == OP_GV) {
2931 GV *gv = cGVOPx_gv(curop);
2933 if (strchr("&`'123456789+", *GvENAME(gv)))
2936 #endif /* USE_THREADS */
2937 else if (curop->op_type == OP_RV2CV)
2939 else if (curop->op_type == OP_RV2SV ||
2940 curop->op_type == OP_RV2AV ||
2941 curop->op_type == OP_RV2HV ||
2942 curop->op_type == OP_RV2GV) {
2943 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2946 else if (curop->op_type == OP_PADSV ||
2947 curop->op_type == OP_PADAV ||
2948 curop->op_type == OP_PADHV ||
2949 curop->op_type == OP_PADANY) {
2952 else if (curop->op_type == OP_PUSHRE)
2953 ; /* Okay here, dangerous in newASSIGNOP */
2962 && (!pm->op_pmregexp
2963 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2964 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2965 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2966 prepend_elem(o->op_type, scalar(repl), o);
2969 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2970 pm->op_pmflags |= PMf_MAYBE_CONST;
2971 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2973 NewOp(1101, rcop, 1, LOGOP);
2974 rcop->op_type = OP_SUBSTCONT;
2975 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2976 rcop->op_first = scalar(repl);
2977 rcop->op_flags |= OPf_KIDS;
2978 rcop->op_private = 1;
2981 /* establish postfix order */
2982 rcop->op_next = LINKLIST(repl);
2983 repl->op_next = (OP*)rcop;
2985 pm->op_pmreplroot = scalar((OP*)rcop);
2986 pm->op_pmreplstart = LINKLIST(rcop);
2995 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2998 NewOp(1101, svop, 1, SVOP);
2999 svop->op_type = type;
3000 svop->op_ppaddr = PL_ppaddr[type];
3002 svop->op_next = (OP*)svop;
3003 svop->op_flags = flags;
3004 if (PL_opargs[type] & OA_RETSCALAR)
3006 if (PL_opargs[type] & OA_TARGET)
3007 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3008 return CHECKOP(type, svop);
3012 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3015 NewOp(1101, padop, 1, PADOP);
3016 padop->op_type = type;
3017 padop->op_ppaddr = PL_ppaddr[type];
3018 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3019 SvREFCNT_dec(PL_curpad[padop->op_padix]);
3020 PL_curpad[padop->op_padix] = sv;
3022 padop->op_next = (OP*)padop;
3023 padop->op_flags = flags;
3024 if (PL_opargs[type] & OA_RETSCALAR)
3026 if (PL_opargs[type] & OA_TARGET)
3027 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3028 return CHECKOP(type, padop);
3032 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3037 return newPADOP(type, flags, SvREFCNT_inc(gv));
3039 return newSVOP(type, flags, SvREFCNT_inc(gv));
3044 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3047 NewOp(1101, pvop, 1, PVOP);
3048 pvop->op_type = type;
3049 pvop->op_ppaddr = PL_ppaddr[type];
3051 pvop->op_next = (OP*)pvop;
3052 pvop->op_flags = flags;
3053 if (PL_opargs[type] & OA_RETSCALAR)
3055 if (PL_opargs[type] & OA_TARGET)
3056 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3057 return CHECKOP(type, pvop);
3061 Perl_package(pTHX_ OP *o)
3066 save_hptr(&PL_curstash);
3067 save_item(PL_curstname);
3072 name = SvPV(sv, len);
3073 PL_curstash = gv_stashpvn(name,len,TRUE);
3074 sv_setpvn(PL_curstname, name, len);
3078 sv_setpv(PL_curstname,"<none>");
3079 PL_curstash = Nullhv;
3081 PL_hints |= HINT_BLOCK_SCOPE;
3082 PL_copline = NOLINE;
3087 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
3095 if (id->op_type != OP_CONST)
3096 Perl_croak(aTHX_ "Module name must be constant");
3100 if (version != Nullop) {
3101 SV *vesv = ((SVOP*)version)->op_sv;
3103 if (arg == Nullop && !SvNIOKp(vesv)) {
3110 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3111 Perl_croak(aTHX_ "Version number must be constant number");
3113 /* Make copy of id so we don't free it twice */
3114 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3116 /* Fake up a method call to VERSION */
3117 meth = newSVpvn("VERSION",7);
3118 sv_upgrade(meth, SVt_PVIV);
3119 (void)SvIOK_on(meth);
3120 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3121 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3122 append_elem(OP_LIST,
3123 prepend_elem(OP_LIST, pack, list(version)),
3124 newSVOP(OP_METHOD_NAMED, 0, meth)));
3128 /* Fake up an import/unimport */
3129 if (arg && arg->op_type == OP_STUB)
3130 imop = arg; /* no import on explicit () */
3131 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
3132 imop = Nullop; /* use 5.0; */
3137 /* Make copy of id so we don't free it twice */
3138 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3140 /* Fake up a method call to import/unimport */
3141 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);;
3142 sv_upgrade(meth, SVt_PVIV);
3143 (void)SvIOK_on(meth);
3144 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3145 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3146 append_elem(OP_LIST,
3147 prepend_elem(OP_LIST, pack, list(arg)),
3148 newSVOP(OP_METHOD_NAMED, 0, meth)));
3151 /* Fake up a require, handle override, if any */
3152 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3153 if (!(gv && GvIMPORTED_CV(gv)))
3154 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3156 if (gv && GvIMPORTED_CV(gv)) {
3157 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3158 append_elem(OP_LIST, id,
3159 scalar(newUNOP(OP_RV2CV, 0,
3164 rqop = newUNOP(OP_REQUIRE, 0, id);
3167 /* Fake up the BEGIN {}, which does its thing immediately. */
3169 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3172 append_elem(OP_LINESEQ,
3173 append_elem(OP_LINESEQ,
3174 newSTATEOP(0, Nullch, rqop),
3175 newSTATEOP(0, Nullch, veop)),
3176 newSTATEOP(0, Nullch, imop) ));
3178 PL_hints |= HINT_BLOCK_SCOPE;
3179 PL_copline = NOLINE;
3184 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3187 va_start(args, ver);
3188 vload_module(flags, name, ver, &args);
3192 #ifdef PERL_IMPLICIT_CONTEXT
3194 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3198 va_start(args, ver);
3199 vload_module(flags, name, ver, &args);
3205 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3207 OP *modname, *veop, *imop;
3209 modname = newSVOP(OP_CONST, 0, name);
3210 modname->op_private |= OPpCONST_BARE;
3212 veop = newSVOP(OP_CONST, 0, ver);
3216 if (flags & PERL_LOADMOD_NOIMPORT) {
3217 imop = sawparens(newNULLLIST());
3219 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3220 imop = va_arg(*args, OP*);
3225 sv = va_arg(*args, SV*);
3227 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3228 sv = va_arg(*args, SV*);
3232 line_t ocopline = PL_copline;
3233 int oexpect = PL_expect;
3235 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3236 veop, modname, imop);
3237 PL_expect = oexpect;
3238 PL_copline = ocopline;
3243 Perl_dofile(pTHX_ OP *term)
3248 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3249 if (!(gv && GvIMPORTED_CV(gv)))
3250 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3252 if (gv && GvIMPORTED_CV(gv)) {
3253 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3254 append_elem(OP_LIST, term,
3255 scalar(newUNOP(OP_RV2CV, 0,
3260 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3266 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3268 return newBINOP(OP_LSLICE, flags,
3269 list(force_list(subscript)),
3270 list(force_list(listval)) );
3274 S_list_assignment(pTHX_ register OP *o)
3279 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3280 o = cUNOPo->op_first;
3282 if (o->op_type == OP_COND_EXPR) {
3283 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3284 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3289 yyerror("Assignment to both a list and a scalar");
3293 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3294 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3295 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3298 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3301 if (o->op_type == OP_RV2SV)
3308 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3313 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3314 return newLOGOP(optype, 0,
3315 mod(scalar(left), optype),
3316 newUNOP(OP_SASSIGN, 0, scalar(right)));
3319 return newBINOP(optype, OPf_STACKED,
3320 mod(scalar(left), optype), scalar(right));
3324 if (list_assignment(left)) {
3329 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3330 left = mod(left, OP_AASSIGN);
3338 curop = list(force_list(left));
3339 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3340 o->op_private = 0 | (flags >> 8);
3341 for (curop = ((LISTOP*)curop)->op_first;
3342 curop; curop = curop->op_sibling)
3344 if (curop->op_type == OP_RV2HV &&
3345 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3346 o->op_private |= OPpASSIGN_HASH;
3350 if (!(left->op_private & OPpLVAL_INTRO)) {
3353 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3354 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3355 if (curop->op_type == OP_GV) {
3356 GV *gv = cGVOPx_gv(curop);
3357 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3359 SvCUR(gv) = PL_generation;
3361 else if (curop->op_type == OP_PADSV ||
3362 curop->op_type == OP_PADAV ||
3363 curop->op_type == OP_PADHV ||
3364 curop->op_type == OP_PADANY) {
3365 SV **svp = AvARRAY(PL_comppad_name);
3366 SV *sv = svp[curop->op_targ];
3367 if (SvCUR(sv) == PL_generation)
3369 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3371 else if (curop->op_type == OP_RV2CV)
3373 else if (curop->op_type == OP_RV2SV ||
3374 curop->op_type == OP_RV2AV ||
3375 curop->op_type == OP_RV2HV ||
3376 curop->op_type == OP_RV2GV) {
3377 if (lastop->op_type != OP_GV) /* funny deref? */
3380 else if (curop->op_type == OP_PUSHRE) {
3381 if (((PMOP*)curop)->op_pmreplroot) {
3382 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3383 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3385 SvCUR(gv) = PL_generation;
3394 o->op_private |= OPpASSIGN_COMMON;
3396 if (right && right->op_type == OP_SPLIT) {
3398 if ((tmpop = ((LISTOP*)right)->op_first) &&
3399 tmpop->op_type == OP_PUSHRE)
3401 PMOP *pm = (PMOP*)tmpop;
3402 if (left->op_type == OP_RV2AV &&
3403 !(left->op_private & OPpLVAL_INTRO) &&
3404 !(o->op_private & OPpASSIGN_COMMON) )
3406 tmpop = ((UNOP*)left)->op_first;
3407 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3409 pm->op_pmreplroot = (OP*)cPADOPx(tmpop)->op_padix;
3410 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3412 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3413 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3415 pm->op_pmflags |= PMf_ONCE;
3416 tmpop = cUNOPo->op_first; /* to list (nulled) */
3417 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3418 tmpop->op_sibling = Nullop; /* don't free split */
3419 right->op_next = tmpop->op_next; /* fix starting loc */
3420 op_free(o); /* blow off assign */
3421 right->op_flags &= ~OPf_WANT;
3422 /* "I don't know and I don't care." */
3427 if (PL_modcount < 10000 &&
3428 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3430 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3432 sv_setiv(sv, PL_modcount+1);
3440 right = newOP(OP_UNDEF, 0);
3441 if (right->op_type == OP_READLINE) {
3442 right->op_flags |= OPf_STACKED;
3443 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3446 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3447 o = newBINOP(OP_SASSIGN, flags,
3448 scalar(right), mod(scalar(left), OP_SASSIGN) );
3460 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3463 U32 seq = intro_my();
3466 NewOp(1101, cop, 1, COP);
3467 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3468 cop->op_type = OP_DBSTATE;
3469 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3472 cop->op_type = OP_NEXTSTATE;
3473 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3475 cop->op_flags = flags;
3476 cop->op_private = (PL_hints & HINT_BYTE);
3478 cop->op_private |= NATIVE_HINTS;
3480 PL_compiling.op_private = cop->op_private;
3481 cop->op_next = (OP*)cop;
3484 cop->cop_label = label;
3485 PL_hints |= HINT_BLOCK_SCOPE;
3488 cop->cop_arybase = PL_curcop->cop_arybase;
3489 if (specialWARN(PL_curcop->cop_warnings))
3490 cop->cop_warnings = PL_curcop->cop_warnings ;
3492 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3495 if (PL_copline == NOLINE)
3496 CopLINE_set(cop, CopLINE(PL_curcop));
3498 CopLINE_set(cop, PL_copline);
3499 PL_copline = NOLINE;
3502 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3504 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3506 CopSTASH_set(cop, PL_curstash);
3508 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3509 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3510 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3511 (void)SvIOK_on(*svp);
3512 SvIVX(*svp) = PTR2IV(cop);
3516 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3519 /* "Introduce" my variables to visible status. */
3527 if (! PL_min_intro_pending)
3528 return PL_cop_seqmax;
3530 svp = AvARRAY(PL_comppad_name);
3531 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3532 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3533 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3534 SvNVX(sv) = (NV)PL_cop_seqmax;
3537 PL_min_intro_pending = 0;
3538 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3539 return PL_cop_seqmax++;
3543 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3545 return new_logop(type, flags, &first, &other);
3549 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3554 OP *first = *firstp;
3555 OP *other = *otherp;
3557 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3558 return newBINOP(type, flags, scalar(first), scalar(other));
3560 scalarboolean(first);
3561 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3562 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3563 if (type == OP_AND || type == OP_OR) {
3569 first = *firstp = cUNOPo->op_first;
3571 first->op_next = o->op_next;
3572 cUNOPo->op_first = Nullop;
3576 if (first->op_type == OP_CONST) {
3577 if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE))
3578 Perl_warner(aTHX_ WARN_BAREWORD, "Bareword found in conditional");
3579 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3590 else if (first->op_type == OP_WANTARRAY) {
3596 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
3597 OP *k1 = ((UNOP*)first)->op_first;
3598 OP *k2 = k1->op_sibling;
3600 switch (first->op_type)
3603 if (k2 && k2->op_type == OP_READLINE
3604 && (k2->op_flags & OPf_STACKED)
3605 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3607 warnop = k2->op_type;
3612 if (k1->op_type == OP_READDIR
3613 || k1->op_type == OP_GLOB
3614 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3615 || k1->op_type == OP_EACH)
3617 warnop = ((k1->op_type == OP_NULL)
3618 ? k1->op_targ : k1->op_type);
3623 line_t oldline = CopLINE(PL_curcop);
3624 CopLINE_set(PL_curcop, PL_copline);
3625 Perl_warner(aTHX_ WARN_MISC,
3626 "Value of %s%s can be \"0\"; test with defined()",
3628 ((warnop == OP_READLINE || warnop == OP_GLOB)
3629 ? " construct" : "() operator"));
3630 CopLINE_set(PL_curcop, oldline);
3637 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3638 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3640 NewOp(1101, logop, 1, LOGOP);
3642 logop->op_type = type;
3643 logop->op_ppaddr = PL_ppaddr[type];
3644 logop->op_first = first;
3645 logop->op_flags = flags | OPf_KIDS;
3646 logop->op_other = LINKLIST(other);
3647 logop->op_private = 1 | (flags >> 8);
3649 /* establish postfix order */
3650 logop->op_next = LINKLIST(first);
3651 first->op_next = (OP*)logop;
3652 first->op_sibling = other;
3654 o = newUNOP(OP_NULL, 0, (OP*)logop);
3661 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3669 return newLOGOP(OP_AND, 0, first, trueop);
3671 return newLOGOP(OP_OR, 0, first, falseop);
3673 scalarboolean(first);
3674 if (first->op_type == OP_CONST) {
3675 if (SvTRUE(((SVOP*)first)->op_sv)) {
3686 else if (first->op_type == OP_WANTARRAY) {
3690 NewOp(1101, logop, 1, LOGOP);
3691 logop->op_type = OP_COND_EXPR;
3692 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3693 logop->op_first = first;
3694 logop->op_flags = flags | OPf_KIDS;
3695 logop->op_private = 1 | (flags >> 8);
3696 logop->op_other = LINKLIST(trueop);
3697 logop->op_next = LINKLIST(falseop);
3700 /* establish postfix order */
3701 start = LINKLIST(first);
3702 first->op_next = (OP*)logop;
3704 first->op_sibling = trueop;
3705 trueop->op_sibling = falseop;
3706 o = newUNOP(OP_NULL, 0, (OP*)logop);
3708 trueop->op_next = falseop->op_next = o;
3715 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3724 NewOp(1101, range, 1, LOGOP);
3726 range->op_type = OP_RANGE;
3727 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3728 range->op_first = left;
3729 range->op_flags = OPf_KIDS;
3730 leftstart = LINKLIST(left);
3731 range->op_other = LINKLIST(right);
3732 range->op_private = 1 | (flags >> 8);
3734 left->op_sibling = right;
3736 range->op_next = (OP*)range;
3737 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3738 flop = newUNOP(OP_FLOP, 0, flip);
3739 o = newUNOP(OP_NULL, 0, flop);
3741 range->op_next = leftstart;
3743 left->op_next = flip;
3744 right->op_next = flop;
3746 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3747 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3748 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3749 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3751 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3752 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3755 if (!flip->op_private || !flop->op_private)
3756 linklist(o); /* blow off optimizer unless constant */
3762 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3767 int once = block && block->op_flags & OPf_SPECIAL &&
3768 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3771 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3772 return block; /* do {} while 0 does once */
3773 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3774 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3775 expr = newUNOP(OP_DEFINED, 0,
3776 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3777 } else if (expr->op_flags & OPf_KIDS) {
3778 OP *k1 = ((UNOP*)expr)->op_first;
3779 OP *k2 = (k1) ? k1->op_sibling : NULL;
3780 switch (expr->op_type) {
3782 if (k2 && k2->op_type == OP_READLINE
3783 && (k2->op_flags & OPf_STACKED)
3784 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3785 expr = newUNOP(OP_DEFINED, 0, expr);
3789 if (k1->op_type == OP_READDIR
3790 || k1->op_type == OP_GLOB
3791 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3792 || k1->op_type == OP_EACH)
3793 expr = newUNOP(OP_DEFINED, 0, expr);
3799 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3800 o = new_logop(OP_AND, 0, &expr, &listop);
3803 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3805 if (once && o != listop)
3806 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3809 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3811 o->op_flags |= flags;
3813 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3818 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3828 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3829 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3830 expr = newUNOP(OP_DEFINED, 0,
3831 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3832 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3833 OP *k1 = ((UNOP*)expr)->op_first;
3834 OP *k2 = (k1) ? k1->op_sibling : NULL;
3835 switch (expr->op_type) {
3837 if (k2 && k2->op_type == OP_READLINE
3838 && (k2->op_flags & OPf_STACKED)
3839 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3840 expr = newUNOP(OP_DEFINED, 0, expr);
3844 if (k1->op_type == OP_READDIR
3845 || k1->op_type == OP_GLOB
3846 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3847 || k1->op_type == OP_EACH)
3848 expr = newUNOP(OP_DEFINED, 0, expr);
3854 block = newOP(OP_NULL, 0);
3856 block = scope(block);
3860 next = LINKLIST(cont);
3861 loopflags |= OPpLOOP_CONTINUE;
3864 OP *unstack = newOP(OP_UNSTACK, 0);
3867 cont = append_elem(OP_LINESEQ, cont, unstack);
3868 if ((line_t)whileline != NOLINE) {
3869 PL_copline = whileline;
3870 cont = append_elem(OP_LINESEQ, cont,
3871 newSTATEOP(0, Nullch, Nullop));
3875 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3876 redo = LINKLIST(listop);
3879 PL_copline = whileline;
3881 o = new_logop(OP_AND, 0, &expr, &listop);
3882 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3883 op_free(expr); /* oops, it's a while (0) */
3885 return Nullop; /* listop already freed by new_logop */
3888 ((LISTOP*)listop)->op_last->op_next = condop =
3889 (o == listop ? redo : LINKLIST(o));
3895 NewOp(1101,loop,1,LOOP);
3896 loop->op_type = OP_ENTERLOOP;
3897 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3898 loop->op_private = 0;
3899 loop->op_next = (OP*)loop;
3902 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3904 loop->op_redoop = redo;
3905 loop->op_lastop = o;
3906 o->op_private |= loopflags;
3909 loop->op_nextop = next;
3911 loop->op_nextop = o;
3913 o->op_flags |= flags;
3914 o->op_private |= (flags >> 8);
3919 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3927 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3928 sv->op_type = OP_RV2GV;
3929 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3931 else if (sv->op_type == OP_PADSV) { /* private variable */
3932 padoff = sv->op_targ;
3937 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3938 padoff = sv->op_targ;
3940 iterflags |= OPf_SPECIAL;
3945 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3949 padoff = find_threadsv("_");
3950 iterflags |= OPf_SPECIAL;
3952 sv = newGVOP(OP_GV, 0, PL_defgv);
3955 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3956 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3957 iterflags |= OPf_STACKED;
3959 else if (expr->op_type == OP_NULL &&
3960 (expr->op_flags & OPf_KIDS) &&
3961 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3963 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3964 * set the STACKED flag to indicate that these values are to be
3965 * treated as min/max values by 'pp_iterinit'.
3967 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3968 LOGOP* range = (LOGOP*) flip->op_first;
3969 OP* left = range->op_first;
3970 OP* right = left->op_sibling;
3973 range->op_flags &= ~OPf_KIDS;
3974 range->op_first = Nullop;
3976 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3977 listop->op_first->op_next = range->op_next;
3978 left->op_next = range->op_other;
3979 right->op_next = (OP*)listop;
3980 listop->op_next = listop->op_first;
3983 expr = (OP*)(listop);
3985 iterflags |= OPf_STACKED;
3988 expr = mod(force_list(expr), OP_GREPSTART);
3992 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3993 append_elem(OP_LIST, expr, scalar(sv))));
3994 assert(!loop->op_next);
3995 #ifdef PL_OP_SLAB_ALLOC
3998 NewOp(1234,tmp,1,LOOP);
3999 Copy(loop,tmp,1,LOOP);
4003 Renew(loop, 1, LOOP);
4005 loop->op_targ = padoff;
4006 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
4007 PL_copline = forline;
4008 return newSTATEOP(0, label, wop);
4012 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4018 if (type != OP_GOTO || label->op_type == OP_CONST) {
4019 /* "last()" means "last" */
4020 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4021 o = newOP(type, OPf_SPECIAL);
4023 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4024 ? SvPVx(((SVOP*)label)->op_sv, n_a)
4030 if (label->op_type == OP_ENTERSUB)
4031 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4032 o = newUNOP(type, OPf_STACKED, label);
4034 PL_hints |= HINT_BLOCK_SCOPE;
4039 Perl_cv_undef(pTHX_ CV *cv)
4044 MUTEX_DESTROY(CvMUTEXP(cv));
4045 Safefree(CvMUTEXP(cv));
4048 #endif /* USE_THREADS */
4050 if (!CvXSUB(cv) && CvROOT(cv)) {
4052 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4053 Perl_croak(aTHX_ "Can't undef active subroutine");
4056 Perl_croak(aTHX_ "Can't undef active subroutine");
4057 #endif /* USE_THREADS */
4060 SAVEVPTR(PL_curpad);
4064 op_free(CvROOT(cv));
4065 CvROOT(cv) = Nullop;
4068 SvPOK_off((SV*)cv); /* forget prototype */
4070 SvREFCNT_dec(CvGV(cv));
4072 SvREFCNT_dec(CvOUTSIDE(cv));
4073 CvOUTSIDE(cv) = Nullcv;
4074 if (CvPADLIST(cv)) {
4075 /* may be during global destruction */
4076 if (SvREFCNT(CvPADLIST(cv))) {
4077 I32 i = AvFILLp(CvPADLIST(cv));
4079 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
4080 SV* sv = svp ? *svp : Nullsv;
4083 if (sv == (SV*)PL_comppad_name)
4084 PL_comppad_name = Nullav;
4085 else if (sv == (SV*)PL_comppad) {
4086 PL_comppad = Nullav;
4087 PL_curpad = Null(SV**);
4091 SvREFCNT_dec((SV*)CvPADLIST(cv));
4093 CvPADLIST(cv) = Nullav;
4098 S_cv_dump(pTHX_ CV *cv)
4101 CV *outside = CvOUTSIDE(cv);
4102 AV* padlist = CvPADLIST(cv);
4109 PerlIO_printf(Perl_debug_log,
4110 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
4112 (CvANON(cv) ? "ANON"
4113 : (cv == PL_main_cv) ? "MAIN"
4114 : CvUNIQUE(cv) ? "UNIQUE"
4115 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
4118 : CvANON(outside) ? "ANON"
4119 : (outside == PL_main_cv) ? "MAIN"
4120 : CvUNIQUE(outside) ? "UNIQUE"
4121 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
4126 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
4127 pad = (AV*)*av_fetch(padlist, 1, FALSE);
4128 pname = AvARRAY(pad_name);
4129 ppad = AvARRAY(pad);
4131 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
4132 if (SvPOK(pname[ix]))
4133 PerlIO_printf(Perl_debug_log,
4134 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
4135 (int)ix, PTR2UV(ppad[ix]),
4136 SvFAKE(pname[ix]) ? "FAKE " : "",
4138 (IV)I_32(SvNVX(pname[ix])),
4141 #endif /* DEBUGGING */
4145 S_cv_clone2(pTHX_ CV *proto, CV *outside)
4150 AV* protopadlist = CvPADLIST(proto);
4151 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
4152 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
4153 SV** pname = AvARRAY(protopad_name);
4154 SV** ppad = AvARRAY(protopad);
4155 I32 fname = AvFILLp(protopad_name);
4156 I32 fpad = AvFILLp(protopad);
4160 assert(!CvUNIQUE(proto));
4164 SAVESPTR(PL_comppad_name);
4165 SAVESPTR(PL_compcv);
4167 cv = PL_compcv = (CV*)NEWSV(1104,0);
4168 sv_upgrade((SV *)cv, SvTYPE(proto));
4169 CvFLAGS(cv) = CvFLAGS(proto) & ~CVf_CLONE;
4173 New(666, CvMUTEXP(cv), 1, perl_mutex);
4174 MUTEX_INIT(CvMUTEXP(cv));
4176 #endif /* USE_THREADS */
4177 CvFILE(cv) = CvFILE(proto);
4178 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
4179 CvSTASH(cv) = CvSTASH(proto);
4180 CvROOT(cv) = CvROOT(proto);
4181 CvSTART(cv) = CvSTART(proto);
4183 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
4186 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
4188 PL_comppad_name = newAV();
4189 for (ix = fname; ix >= 0; ix--)
4190 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
4192 PL_comppad = newAV();
4194 comppadlist = newAV();
4195 AvREAL_off(comppadlist);
4196 av_store(comppadlist, 0, (SV*)PL_comppad_name);
4197 av_store(comppadlist, 1, (SV*)PL_comppad);
4198 CvPADLIST(cv) = comppadlist;
4199 av_fill(PL_comppad, AvFILLp(protopad));
4200 PL_curpad = AvARRAY(PL_comppad);
4202 av = newAV(); /* will be @_ */
4204 av_store(PL_comppad, 0, (SV*)av);
4205 AvFLAGS(av) = AVf_REIFY;
4207 for (ix = fpad; ix > 0; ix--) {
4208 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4209 if (namesv && namesv != &PL_sv_undef) {
4210 char *name = SvPVX(namesv); /* XXX */
4211 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4212 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4213 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4215 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4217 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4219 else { /* our own lexical */
4222 /* anon code -- we'll come back for it */
4223 sv = SvREFCNT_inc(ppad[ix]);
4225 else if (*name == '@')
4227 else if (*name == '%')
4236 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
4237 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4240 SV* sv = NEWSV(0,0);
4246 /* Now that vars are all in place, clone nested closures. */
4248 for (ix = fpad; ix > 0; ix--) {
4249 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4251 && namesv != &PL_sv_undef
4252 && !(SvFLAGS(namesv) & SVf_FAKE)
4253 && *SvPVX(namesv) == '&'
4254 && CvCLONE(ppad[ix]))
4256 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4257 SvREFCNT_dec(ppad[ix]);
4260 PL_curpad[ix] = (SV*)kid;
4264 #ifdef DEBUG_CLOSURES
4265 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4267 PerlIO_printf(Perl_debug_log, " from:\n");
4269 PerlIO_printf(Perl_debug_log, " to:\n");
4278 Perl_cv_clone(pTHX_ CV *proto)
4281 LOCK_CRED_MUTEX; /* XXX create separate mutex */
4282 cv = cv_clone2(proto, CvOUTSIDE(proto));
4283 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
4288 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4292 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4293 SV* msg = sv_newmortal();
4297 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4298 sv_setpv(msg, "Prototype mismatch:");
4300 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4302 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4303 sv_catpv(msg, " vs ");
4305 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4307 sv_catpv(msg, "none");
4308 Perl_warner(aTHX_ WARN_PROTOTYPE, "%"SVf, msg);
4313 Perl_cv_const_sv(pTHX_ CV *cv)
4315 if (!cv || !SvPOK(cv) || SvCUR(cv))
4317 return op_const_sv(CvSTART(cv), cv);
4321 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4328 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4329 o = cLISTOPo->op_first->op_sibling;
4331 for (; o; o = o->op_next) {
4332 OPCODE type = o->op_type;
4334 if (sv && o->op_next == o)
4336 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4338 if (type == OP_LEAVESUB || type == OP_RETURN)
4342 if (type == OP_CONST && cSVOPo->op_sv)
4344 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4345 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4346 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4347 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4359 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4369 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4373 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4375 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4379 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4386 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4390 name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4391 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4392 SV *sv = sv_newmortal();
4393 Perl_sv_setpvf(aTHX_ sv, "__ANON__[%s:%"IVdf"]",
4394 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4399 gv = gv_fetchpv(name ? name : (aname ? aname : "__ANON__"),
4400 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4410 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4411 maximum a prototype before. */
4412 if (SvTYPE(gv) > SVt_NULL) {
4413 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4414 && ckWARN_d(WARN_PROTOTYPE))
4416 Perl_warner(aTHX_ WARN_PROTOTYPE, "Runaway prototype");
4418 cv_ckproto((CV*)gv, NULL, ps);
4421 sv_setpv((SV*)gv, ps);
4423 sv_setiv((SV*)gv, -1);
4424 SvREFCNT_dec(PL_compcv);
4425 cv = PL_compcv = NULL;
4426 PL_sub_generation++;
4430 if (!name || GvCVGEN(gv))
4432 else if ((cv = GvCV(gv))) {
4433 bool exists = CvROOT(cv) || CvXSUB(cv);
4434 /* if the subroutine doesn't exist and wasn't pre-declared
4435 * with a prototype, assume it will be AUTOLOADed,
4436 * skipping the prototype check
4438 if (exists || SvPOK(cv))
4439 cv_ckproto(cv, gv, ps);
4440 /* already defined (or promised)? */
4441 if (exists || GvASSUMECV(gv)) {
4443 bool const_changed = TRUE;
4444 if (!block && !attrs) {
4445 /* just a "sub foo;" when &foo is already defined */
4446 SAVEFREESV(PL_compcv);
4449 /* ahem, death to those who redefine active sort subs */
4450 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4451 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4454 if ((const_sv = cv_const_sv(cv)))
4455 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4456 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE))
4458 line_t oldline = CopLINE(PL_curcop);
4459 CopLINE_set(PL_curcop, PL_copline);
4460 Perl_warner(aTHX_ WARN_REDEFINE,
4461 const_sv ? "Constant subroutine %s redefined"
4462 : "Subroutine %s redefined", name);
4463 CopLINE_set(PL_curcop, oldline);
4474 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4475 * before we clobber PL_compcv.
4479 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4480 stash = GvSTASH(CvGV(cv));
4481 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4482 stash = CvSTASH(cv);
4484 stash = PL_curstash;
4487 /* possibly about to re-define existing subr -- ignore old cv */
4488 rcv = (SV*)PL_compcv;
4489 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4490 stash = GvSTASH(gv);
4492 stash = PL_curstash;
4494 apply_attrs(stash, rcv, attrs);
4496 if (cv) { /* must reuse cv if autoloaded */
4498 /* got here with just attrs -- work done, so bug out */
4499 SAVEFREESV(PL_compcv);
4503 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4504 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4505 CvOUTSIDE(PL_compcv) = 0;
4506 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4507 CvPADLIST(PL_compcv) = 0;
4508 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4509 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4510 SvREFCNT_dec(PL_compcv);
4517 PL_sub_generation++;
4520 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4521 CvFILE(cv) = CopFILE(PL_curcop);
4522 CvSTASH(cv) = PL_curstash;
4525 if (!CvMUTEXP(cv)) {
4526 New(666, CvMUTEXP(cv), 1, perl_mutex);
4527 MUTEX_INIT(CvMUTEXP(cv));
4529 #endif /* USE_THREADS */
4532 sv_setpv((SV*)cv, ps);
4534 if (PL_error_count) {
4538 char *s = strrchr(name, ':');
4540 if (strEQ(s, "BEGIN")) {
4542 "BEGIN not safe after errors--compilation aborted";
4543 if (PL_in_eval & EVAL_KEEPERR)
4544 Perl_croak(aTHX_ not_safe);
4546 /* force display of errors found but not reported */
4547 sv_catpv(ERRSV, not_safe);
4548 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4555 PL_copline = NOLINE;
4560 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4561 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4564 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4567 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4569 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4570 OpREFCNT_set(CvROOT(cv), 1);
4571 CvSTART(cv) = LINKLIST(CvROOT(cv));
4572 CvROOT(cv)->op_next = 0;
4575 /* now that optimizer has done its work, adjust pad values */
4577 SV **namep = AvARRAY(PL_comppad_name);
4578 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4581 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4584 * The only things that a clonable function needs in its
4585 * pad are references to outer lexicals and anonymous subs.
4586 * The rest are created anew during cloning.
4588 if (!((namesv = namep[ix]) != Nullsv &&
4589 namesv != &PL_sv_undef &&
4591 *SvPVX(namesv) == '&')))
4593 SvREFCNT_dec(PL_curpad[ix]);
4594 PL_curpad[ix] = Nullsv;
4599 AV *av = newAV(); /* Will be @_ */
4601 av_store(PL_comppad, 0, (SV*)av);
4602 AvFLAGS(av) = AVf_REIFY;
4604 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4605 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4607 if (!SvPADMY(PL_curpad[ix]))
4608 SvPADTMP_on(PL_curpad[ix]);
4612 if (name || aname) {
4614 char *tname = (name ? name : aname);
4616 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4617 SV *sv = NEWSV(0,0);
4618 SV *tmpstr = sv_newmortal();
4619 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4623 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4625 (long)PL_subline, (long)CopLINE(PL_curcop));
4626 gv_efullname3(tmpstr, gv, Nullch);
4627 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4628 hv = GvHVn(db_postponed);
4629 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4630 && (pcv = GvCV(db_postponed)))
4636 call_sv((SV*)pcv, G_DISCARD);
4640 if ((s = strrchr(tname,':')))
4645 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4648 if (strEQ(s, "BEGIN")) {
4649 I32 oldscope = PL_scopestack_ix;
4651 SAVECOPFILE(&PL_compiling);
4652 SAVECOPLINE(&PL_compiling);
4654 sv_setsv(PL_rs, PL_nrs);
4657 PL_beginav = newAV();
4658 DEBUG_x( dump_sub(gv) );
4659 av_push(PL_beginav, (SV*)cv);
4660 GvCV(gv) = 0; /* cv has been hijacked */
4661 call_list(oldscope, PL_beginav);
4663 PL_curcop = &PL_compiling;
4664 PL_compiling.op_private = PL_hints;
4667 else if (strEQ(s, "END") && !PL_error_count) {
4670 DEBUG_x( dump_sub(gv) );
4671 av_unshift(PL_endav, 1);
4672 av_store(PL_endav, 0, (SV*)cv);
4673 GvCV(gv) = 0; /* cv has been hijacked */
4675 else if (strEQ(s, "CHECK") && !PL_error_count) {
4677 PL_checkav = newAV();
4678 DEBUG_x( dump_sub(gv) );
4679 if (PL_main_start && ckWARN(WARN_VOID))
4680 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4681 av_unshift(PL_checkav, 1);
4682 av_store(PL_checkav, 0, (SV*)cv);
4683 GvCV(gv) = 0; /* cv has been hijacked */
4685 else if (strEQ(s, "INIT") && !PL_error_count) {
4687 PL_initav = newAV();
4688 DEBUG_x( dump_sub(gv) );
4689 if (PL_main_start && ckWARN(WARN_VOID))
4690 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4691 av_push(PL_initav, (SV*)cv);
4692 GvCV(gv) = 0; /* cv has been hijacked */
4697 PL_copline = NOLINE;
4702 /* XXX unsafe for threads if eval_owner isn't held */
4704 =for apidoc newCONSTSUB
4706 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4707 eligible for inlining at compile-time.
4713 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4719 SAVECOPLINE(PL_curcop);
4720 CopLINE_set(PL_curcop, PL_copline);
4723 PL_hints &= ~HINT_BLOCK_SCOPE;
4726 SAVESPTR(PL_curstash);
4727 SAVECOPSTASH(PL_curcop);
4728 PL_curstash = stash;
4730 CopSTASHPV(PL_curcop) = stash ? HvNAME(stash) : Nullch;
4732 CopSTASH(PL_curcop) = stash;
4737 start_subparse(FALSE, 0),
4738 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4739 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4741 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4748 =for apidoc U||newXS
4750 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4756 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4759 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4762 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4764 /* just a cached method */
4768 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4769 /* already defined (or promised) */
4770 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4771 && HvNAME(GvSTASH(CvGV(cv)))
4772 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4773 line_t oldline = CopLINE(PL_curcop);
4774 if (PL_copline != NOLINE)
4775 CopLINE_set(PL_curcop, PL_copline);
4776 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4777 CopLINE_set(PL_curcop, oldline);
4784 if (cv) /* must reuse cv if autoloaded */
4787 cv = (CV*)NEWSV(1105,0);
4788 sv_upgrade((SV *)cv, SVt_PVCV);
4792 PL_sub_generation++;
4795 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4797 New(666, CvMUTEXP(cv), 1, perl_mutex);
4798 MUTEX_INIT(CvMUTEXP(cv));
4800 #endif /* USE_THREADS */
4801 (void)gv_fetchfile(filename);
4802 CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4803 an external constant string */
4804 CvXSUB(cv) = subaddr;
4807 char *s = strrchr(name,':');
4813 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4816 if (strEQ(s, "BEGIN")) {
4818 PL_beginav = newAV();
4819 av_push(PL_beginav, (SV*)cv);
4820 GvCV(gv) = 0; /* cv has been hijacked */
4822 else if (strEQ(s, "END")) {
4825 av_unshift(PL_endav, 1);
4826 av_store(PL_endav, 0, (SV*)cv);
4827 GvCV(gv) = 0; /* cv has been hijacked */
4829 else if (strEQ(s, "CHECK")) {
4831 PL_checkav = newAV();
4832 if (PL_main_start && ckWARN(WARN_VOID))
4833 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4834 av_unshift(PL_checkav, 1);
4835 av_store(PL_checkav, 0, (SV*)cv);
4836 GvCV(gv) = 0; /* cv has been hijacked */
4838 else if (strEQ(s, "INIT")) {
4840 PL_initav = newAV();
4841 if (PL_main_start && ckWARN(WARN_VOID))
4842 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4843 av_push(PL_initav, (SV*)cv);
4844 GvCV(gv) = 0; /* cv has been hijacked */
4855 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4865 name = SvPVx(cSVOPo->op_sv, n_a);
4868 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4870 if ((cv = GvFORM(gv))) {
4871 if (ckWARN(WARN_REDEFINE)) {
4872 line_t oldline = CopLINE(PL_curcop);
4874 CopLINE_set(PL_curcop, PL_copline);
4875 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4876 CopLINE_set(PL_curcop, oldline);
4882 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4883 CvFILE(cv) = CopFILE(PL_curcop);
4885 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4886 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4887 SvPADTMP_on(PL_curpad[ix]);
4890 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4891 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4892 OpREFCNT_set(CvROOT(cv), 1);
4893 CvSTART(cv) = LINKLIST(CvROOT(cv));
4894 CvROOT(cv)->op_next = 0;
4897 PL_copline = NOLINE;
4902 Perl_newANONLIST(pTHX_ OP *o)
4904 return newUNOP(OP_REFGEN, 0,
4905 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4909 Perl_newANONHASH(pTHX_ OP *o)
4911 return newUNOP(OP_REFGEN, 0,
4912 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4916 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4918 return newANONATTRSUB(floor, proto, Nullop, block);
4922 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4924 return newUNOP(OP_REFGEN, 0,
4925 newSVOP(OP_ANONCODE, 0,
4926 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4930 Perl_oopsAV(pTHX_ OP *o)
4932 switch (o->op_type) {
4934 o->op_type = OP_PADAV;
4935 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4936 return ref(o, OP_RV2AV);
4939 o->op_type = OP_RV2AV;
4940 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4945 if (ckWARN_d(WARN_INTERNAL))
4946 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4953 Perl_oopsHV(pTHX_ OP *o)
4957 switch (o->op_type) {
4960 o->op_type = OP_PADHV;
4961 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4962 return ref(o, OP_RV2HV);
4966 o->op_type = OP_RV2HV;
4967 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4972 if (ckWARN_d(WARN_INTERNAL))
4973 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4980 Perl_newAVREF(pTHX_ OP *o)
4982 if (o->op_type == OP_PADANY) {
4983 o->op_type = OP_PADAV;
4984 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4987 return newUNOP(OP_RV2AV, 0, scalar(o));
4991 Perl_newGVREF(pTHX_ I32 type, OP *o)
4993 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4994 return newUNOP(OP_NULL, 0, o);
4995 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4999 Perl_newHVREF(pTHX_ OP *o)
5001 if (o->op_type == OP_PADANY) {
5002 o->op_type = OP_PADHV;
5003 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5006 return newUNOP(OP_RV2HV, 0, scalar(o));
5010 Perl_oopsCV(pTHX_ OP *o)
5012 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5018 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5020 return newUNOP(OP_RV2CV, flags, scalar(o));
5024 Perl_newSVREF(pTHX_ OP *o)
5026 if (o->op_type == OP_PADANY) {
5027 o->op_type = OP_PADSV;
5028 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5031 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5032 o->op_flags |= OPpDONE_SVREF;
5035 return newUNOP(OP_RV2SV, 0, scalar(o));
5038 /* Check routines. */
5041 Perl_ck_anoncode(pTHX_ OP *o)
5046 name = NEWSV(1106,0);
5047 sv_upgrade(name, SVt_PVNV);
5048 sv_setpvn(name, "&", 1);
5051 ix = pad_alloc(o->op_type, SVs_PADMY);
5052 av_store(PL_comppad_name, ix, name);
5053 av_store(PL_comppad, ix, cSVOPo->op_sv);
5054 SvPADMY_on(cSVOPo->op_sv);
5055 cSVOPo->op_sv = Nullsv;
5056 cSVOPo->op_targ = ix;
5061 Perl_ck_bitop(pTHX_ OP *o)
5063 o->op_private = PL_hints;
5068 Perl_ck_concat(pTHX_ OP *o)
5070 if (cUNOPo->op_first->op_type == OP_CONCAT)
5071 o->op_flags |= OPf_STACKED;
5076 Perl_ck_spair(pTHX_ OP *o)
5078 if (o->op_flags & OPf_KIDS) {
5081 OPCODE type = o->op_type;
5082 o = modkids(ck_fun(o), type);
5083 kid = cUNOPo->op_first;
5084 newop = kUNOP->op_first->op_sibling;
5086 (newop->op_sibling ||
5087 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
5088 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
5089 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
5093 op_free(kUNOP->op_first);
5094 kUNOP->op_first = newop;
5096 o->op_ppaddr = PL_ppaddr[++o->op_type];
5101 Perl_ck_delete(pTHX_ OP *o)
5105 if (o->op_flags & OPf_KIDS) {
5106 OP *kid = cUNOPo->op_first;
5107 switch (kid->op_type) {
5109 o->op_flags |= OPf_SPECIAL;
5112 o->op_private |= OPpSLICE;
5115 o->op_flags |= OPf_SPECIAL;
5120 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5121 PL_op_desc[o->op_type]);
5129 Perl_ck_eof(pTHX_ OP *o)
5131 I32 type = o->op_type;
5133 if (o->op_flags & OPf_KIDS) {
5134 if (cLISTOPo->op_first->op_type == OP_STUB) {
5136 o = newUNOP(type, OPf_SPECIAL,
5137 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
5145 Perl_ck_eval(pTHX_ OP *o)
5147 PL_hints |= HINT_BLOCK_SCOPE;
5148 if (o->op_flags & OPf_KIDS) {
5149 SVOP *kid = (SVOP*)cUNOPo->op_first;
5152 o->op_flags &= ~OPf_KIDS;
5155 else if (kid->op_type == OP_LINESEQ) {
5158 kid->op_next = o->op_next;
5159 cUNOPo->op_first = 0;
5162 NewOp(1101, enter, 1, LOGOP);
5163 enter->op_type = OP_ENTERTRY;
5164 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5165 enter->op_private = 0;
5167 /* establish postfix order */
5168 enter->op_next = (OP*)enter;
5170 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5171 o->op_type = OP_LEAVETRY;
5172 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5173 enter->op_other = o;
5181 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5183 o->op_targ = (PADOFFSET)PL_hints;
5188 Perl_ck_exit(pTHX_ OP *o)
5191 HV *table = GvHV(PL_hintgv);
5193 SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5194 if (svp && *svp && SvTRUE(*svp))
5195 o->op_private |= OPpEXIT_VMSISH;
5202 Perl_ck_exec(pTHX_ OP *o)
5205 if (o->op_flags & OPf_STACKED) {
5207 kid = cUNOPo->op_first->op_sibling;
5208 if (kid->op_type == OP_RV2GV)
5217 Perl_ck_exists(pTHX_ OP *o)
5220 if (o->op_flags & OPf_KIDS) {
5221 OP *kid = cUNOPo->op_first;
5222 if (kid->op_type == OP_ENTERSUB) {
5223 (void) ref(kid, o->op_type);
5224 if (kid->op_type != OP_RV2CV && !PL_error_count)
5225 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5226 PL_op_desc[o->op_type]);
5227 o->op_private |= OPpEXISTS_SUB;
5229 else if (kid->op_type == OP_AELEM)
5230 o->op_flags |= OPf_SPECIAL;
5231 else if (kid->op_type != OP_HELEM)
5232 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5233 PL_op_desc[o->op_type]);
5241 Perl_ck_gvconst(pTHX_ register OP *o)
5243 o = fold_constants(o);
5244 if (o->op_type == OP_CONST)
5251 Perl_ck_rvconst(pTHX_ register OP *o)
5254 SVOP *kid = (SVOP*)cUNOPo->op_first;
5256 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5257 if (kid->op_type == OP_CONST) {
5261 SV *kidsv = kid->op_sv;
5264 /* Is it a constant from cv_const_sv()? */
5265 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5266 SV *rsv = SvRV(kidsv);
5267 int svtype = SvTYPE(rsv);
5268 char *badtype = Nullch;
5270 switch (o->op_type) {
5272 if (svtype > SVt_PVMG)
5273 badtype = "a SCALAR";
5276 if (svtype != SVt_PVAV)
5277 badtype = "an ARRAY";
5280 if (svtype != SVt_PVHV) {
5281 if (svtype == SVt_PVAV) { /* pseudohash? */
5282 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5283 if (ksv && SvROK(*ksv)
5284 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5293 if (svtype != SVt_PVCV)
5298 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5301 name = SvPV(kidsv, n_a);
5302 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5303 char *badthing = Nullch;
5304 switch (o->op_type) {
5306 badthing = "a SCALAR";
5309 badthing = "an ARRAY";
5312 badthing = "a HASH";
5317 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5321 * This is a little tricky. We only want to add the symbol if we
5322 * didn't add it in the lexer. Otherwise we get duplicate strict
5323 * warnings. But if we didn't add it in the lexer, we must at
5324 * least pretend like we wanted to add it even if it existed before,
5325 * or we get possible typo warnings. OPpCONST_ENTERED says
5326 * whether the lexer already added THIS instance of this symbol.
5328 iscv = (o->op_type == OP_RV2CV) * 2;
5330 gv = gv_fetchpv(name,
5331 iscv | !(kid->op_private & OPpCONST_ENTERED),
5334 : o->op_type == OP_RV2SV
5336 : o->op_type == OP_RV2AV
5338 : o->op_type == OP_RV2HV
5341 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5343 kid->op_type = OP_GV;
5344 SvREFCNT_dec(kid->op_sv);
5346 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5347 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5348 SvREFCNT_dec(PL_curpad[kPADOP->op_padix]);
5350 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5352 kid->op_sv = SvREFCNT_inc(gv);
5354 kid->op_ppaddr = PL_ppaddr[OP_GV];
5361 Perl_ck_ftst(pTHX_ OP *o)
5364 I32 type = o->op_type;
5366 if (o->op_flags & OPf_REF) {
5369 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5370 SVOP *kid = (SVOP*)cUNOPo->op_first;
5372 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5374 OP *newop = newGVOP(type, OPf_REF,
5375 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5382 if (type == OP_FTTTY)
5383 o = newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5386 o = newUNOP(type, 0, newDEFSVOP());
5389 if (type == OP_FTTEXT || type == OP_FTBINARY) {
5391 if (PL_hints & HINT_LOCALE)
5392 o->op_private |= OPpLOCALE;
5399 Perl_ck_fun(pTHX_ OP *o)
5406 int type = o->op_type;
5407 register I32 oa = PL_opargs[type] >> OASHIFT;
5409 if (o->op_flags & OPf_STACKED) {
5410 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5413 return no_fh_allowed(o);
5416 if (o->op_flags & OPf_KIDS) {
5418 tokid = &cLISTOPo->op_first;
5419 kid = cLISTOPo->op_first;
5420 if (kid->op_type == OP_PUSHMARK ||
5421 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5423 tokid = &kid->op_sibling;
5424 kid = kid->op_sibling;
5426 if (!kid && PL_opargs[type] & OA_DEFGV)
5427 *tokid = kid = newDEFSVOP();
5431 sibl = kid->op_sibling;
5434 /* list seen where single (scalar) arg expected? */
5435 if (numargs == 1 && !(oa >> 4)
5436 && kid->op_type == OP_LIST && type != OP_SCALAR)
5438 return too_many_arguments(o,PL_op_desc[type]);
5451 if (kid->op_type == OP_CONST &&
5452 (kid->op_private & OPpCONST_BARE))
5454 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5455 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5456 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5457 if (ckWARN(WARN_DEPRECATED))
5458 Perl_warner(aTHX_ WARN_DEPRECATED,
5459 "Array @%s missing the @ in argument %"IVdf" of %s()",
5460 name, (IV)numargs, PL_op_desc[type]);
5463 kid->op_sibling = sibl;
5466 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5467 bad_type(numargs, "array", PL_op_desc[type], kid);
5471 if (kid->op_type == OP_CONST &&
5472 (kid->op_private & OPpCONST_BARE))
5474 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5475 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5476 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5477 if (ckWARN(WARN_DEPRECATED))
5478 Perl_warner(aTHX_ WARN_DEPRECATED,
5479 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5480 name, (IV)numargs, PL_op_desc[type]);
5483 kid->op_sibling = sibl;
5486 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5487 bad_type(numargs, "hash", PL_op_desc[type], kid);
5492 OP *newop = newUNOP(OP_NULL, 0, kid);
5493 kid->op_sibling = 0;
5495 newop->op_next = newop;
5497 kid->op_sibling = sibl;
5502 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5503 if (kid->op_type == OP_CONST &&
5504 (kid->op_private & OPpCONST_BARE))
5506 OP *newop = newGVOP(OP_GV, 0,
5507 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5512 else if (kid->op_type == OP_READLINE) {
5513 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5514 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5517 I32 flags = OPf_SPECIAL;
5521 /* is this op a FH constructor? */
5522 if (is_handle_constructor(o,numargs)) {
5523 char *name = Nullch;
5527 /* Set a flag to tell rv2gv to vivify
5528 * need to "prove" flag does not mean something
5529 * else already - NI-S 1999/05/07
5532 if (kid->op_type == OP_PADSV) {
5533 SV **namep = av_fetch(PL_comppad_name,
5535 if (namep && *namep)
5536 name = SvPV(*namep, len);
5538 else if (kid->op_type == OP_RV2SV
5539 && kUNOP->op_first->op_type == OP_GV)
5541 GV *gv = cGVOPx_gv(kUNOP->op_first);
5543 len = GvNAMELEN(gv);
5545 else if (kid->op_type == OP_AELEM
5546 || kid->op_type == OP_HELEM)
5548 name = "__ANONIO__";
5554 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5555 namesv = PL_curpad[targ];
5556 (void)SvUPGRADE(namesv, SVt_PV);
5558 sv_setpvn(namesv, "$", 1);
5559 sv_catpvn(namesv, name, len);
5562 kid->op_sibling = 0;
5563 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5564 kid->op_targ = targ;
5565 kid->op_private |= priv;
5567 kid->op_sibling = sibl;
5573 mod(scalar(kid), type);
5577 tokid = &kid->op_sibling;
5578 kid = kid->op_sibling;
5580 o->op_private |= numargs;
5582 return too_many_arguments(o,PL_op_desc[o->op_type]);
5585 else if (PL_opargs[type] & OA_DEFGV) {
5587 return newUNOP(type, 0, newDEFSVOP());
5591 while (oa & OA_OPTIONAL)
5593 if (oa && oa != OA_LIST)
5594 return too_few_arguments(o,PL_op_desc[o->op_type]);
5600 Perl_ck_glob(pTHX_ OP *o)
5605 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5606 append_elem(OP_GLOB, o, newDEFSVOP());
5608 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5609 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5611 #if !defined(PERL_EXTERNAL_GLOB)
5612 /* XXX this can be tightened up and made more failsafe. */
5615 Perl_load_module(aTHX_ 0, newSVpvn("File::Glob", 10), Nullsv,
5616 /* null-terminated import list */
5617 newSVpvn(":globally", 9), Nullsv);
5618 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5621 #endif /* PERL_EXTERNAL_GLOB */
5623 if (gv && GvIMPORTED_CV(gv)) {
5624 append_elem(OP_GLOB, o,
5625 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5626 o->op_type = OP_LIST;
5627 o->op_ppaddr = PL_ppaddr[OP_LIST];
5628 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5629 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5630 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5631 append_elem(OP_LIST, o,
5632 scalar(newUNOP(OP_RV2CV, 0,
5633 newGVOP(OP_GV, 0, gv)))));
5634 o = newUNOP(OP_NULL, 0, ck_subr(o));
5635 o->op_targ = OP_GLOB; /* hint at what it used to be */
5638 gv = newGVgen("main");
5640 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5646 Perl_ck_grep(pTHX_ OP *o)
5650 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5652 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5653 NewOp(1101, gwop, 1, LOGOP);
5655 if (o->op_flags & OPf_STACKED) {
5658 kid = cLISTOPo->op_first->op_sibling;
5659 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5662 kid->op_next = (OP*)gwop;
5663 o->op_flags &= ~OPf_STACKED;
5665 kid = cLISTOPo->op_first->op_sibling;
5666 if (type == OP_MAPWHILE)
5673 kid = cLISTOPo->op_first->op_sibling;
5674 if (kid->op_type != OP_NULL)
5675 Perl_croak(aTHX_ "panic: ck_grep");
5676 kid = kUNOP->op_first;
5678 gwop->op_type = type;
5679 gwop->op_ppaddr = PL_ppaddr[type];
5680 gwop->op_first = listkids(o);
5681 gwop->op_flags |= OPf_KIDS;
5682 gwop->op_private = 1;
5683 gwop->op_other = LINKLIST(kid);
5684 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5685 kid->op_next = (OP*)gwop;
5687 kid = cLISTOPo->op_first->op_sibling;
5688 if (!kid || !kid->op_sibling)
5689 return too_few_arguments(o,PL_op_desc[o->op_type]);
5690 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5691 mod(kid, OP_GREPSTART);
5697 Perl_ck_index(pTHX_ OP *o)
5699 if (o->op_flags & OPf_KIDS) {
5700 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5702 kid = kid->op_sibling; /* get past "big" */
5703 if (kid && kid->op_type == OP_CONST)
5704 fbm_compile(((SVOP*)kid)->op_sv, 0);
5710 Perl_ck_lengthconst(pTHX_ OP *o)
5712 /* XXX length optimization goes here */
5717 Perl_ck_lfun(pTHX_ OP *o)
5719 OPCODE type = o->op_type;
5720 return modkids(ck_fun(o), type);
5724 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5727 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5728 switch (cUNOPo->op_first->op_type) {
5730 break; /* Globals via GV can be undef */
5732 case OP_AASSIGN: /* Is this a good idea? */
5733 Perl_warner(aTHX_ WARN_DEPRECATED,
5734 "defined(@array) is deprecated");
5735 Perl_warner(aTHX_ WARN_DEPRECATED,
5736 "\t(Maybe you should just omit the defined()?)\n");
5739 break; /* Globals via GV can be undef */
5741 Perl_warner(aTHX_ WARN_DEPRECATED,
5742 "defined(%%hash) is deprecated");
5743 Perl_warner(aTHX_ WARN_DEPRECATED,
5744 "\t(Maybe you should just omit the defined()?)\n");
5755 Perl_ck_rfun(pTHX_ OP *o)
5757 OPCODE type = o->op_type;
5758 return refkids(ck_fun(o), type);
5762 Perl_ck_listiob(pTHX_ OP *o)
5766 kid = cLISTOPo->op_first;
5769 kid = cLISTOPo->op_first;
5771 if (kid->op_type == OP_PUSHMARK)
5772 kid = kid->op_sibling;
5773 if (kid && o->op_flags & OPf_STACKED)
5774 kid = kid->op_sibling;
5775 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5776 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5777 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5778 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5779 cLISTOPo->op_first->op_sibling = kid;
5780 cLISTOPo->op_last = kid;
5781 kid = kid->op_sibling;
5786 append_elem(o->op_type, o, newDEFSVOP());
5792 if (PL_hints & HINT_LOCALE)
5793 o->op_private |= OPpLOCALE;
5800 Perl_ck_fun_locale(pTHX_ OP *o)
5806 if (PL_hints & HINT_LOCALE)
5807 o->op_private |= OPpLOCALE;
5814 Perl_ck_sassign(pTHX_ OP *o)
5816 OP *kid = cLISTOPo->op_first;
5817 /* has a disposable target? */
5818 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5819 && !(kid->op_flags & OPf_STACKED)
5820 /* Cannot steal the second time! */
5821 && !(kid->op_private & OPpTARGET_MY))
5823 OP *kkid = kid->op_sibling;
5825 /* Can just relocate the target. */
5826 if (kkid && kkid->op_type == OP_PADSV
5827 && !(kkid->op_private & OPpLVAL_INTRO))
5829 kid->op_targ = kkid->op_targ;
5831 /* Now we do not need PADSV and SASSIGN. */
5832 kid->op_sibling = o->op_sibling; /* NULL */
5833 cLISTOPo->op_first = NULL;
5836 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5844 Perl_ck_scmp(pTHX_ OP *o)
5848 if (PL_hints & HINT_LOCALE)
5849 o->op_private |= OPpLOCALE;
5856 Perl_ck_match(pTHX_ OP *o)
5858 o->op_private |= OPpRUNTIME;
5863 Perl_ck_method(pTHX_ OP *o)
5865 OP *kid = cUNOPo->op_first;
5866 if (kid->op_type == OP_CONST) {
5867 SV* sv = kSVOP->op_sv;
5868 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5870 (void)SvUPGRADE(sv, SVt_PVIV);
5872 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5873 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5874 kSVOP->op_sv = Nullsv;
5883 Perl_ck_null(pTHX_ OP *o)
5889 Perl_ck_open(pTHX_ OP *o)
5891 HV *table = GvHV(PL_hintgv);
5895 svp = hv_fetch(table, "open_IN", 7, FALSE);
5897 mode = mode_from_discipline(*svp);
5898 if (mode & O_BINARY)
5899 o->op_private |= OPpOPEN_IN_RAW;
5900 else if (mode & O_TEXT)
5901 o->op_private |= OPpOPEN_IN_CRLF;
5904 svp = hv_fetch(table, "open_OUT", 8, FALSE);
5906 mode = mode_from_discipline(*svp);
5907 if (mode & O_BINARY)
5908 o->op_private |= OPpOPEN_OUT_RAW;
5909 else if (mode & O_TEXT)
5910 o->op_private |= OPpOPEN_OUT_CRLF;
5913 if (o->op_type == OP_BACKTICK)
5919 Perl_ck_repeat(pTHX_ OP *o)
5921 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5922 o->op_private |= OPpREPEAT_DOLIST;
5923 cBINOPo->op_first = force_list(cBINOPo->op_first);
5931 Perl_ck_require(pTHX_ OP *o)
5933 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5934 SVOP *kid = (SVOP*)cUNOPo->op_first;
5936 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5938 for (s = SvPVX(kid->op_sv); *s; s++) {
5939 if (*s == ':' && s[1] == ':') {
5941 Move(s+2, s+1, strlen(s+2)+1, char);
5942 --SvCUR(kid->op_sv);
5945 if (SvREADONLY(kid->op_sv)) {
5946 SvREADONLY_off(kid->op_sv);
5947 sv_catpvn(kid->op_sv, ".pm", 3);
5948 SvREADONLY_on(kid->op_sv);
5951 sv_catpvn(kid->op_sv, ".pm", 3);
5959 Perl_ck_retarget(pTHX_ OP *o)
5961 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5968 Perl_ck_select(pTHX_ OP *o)
5971 if (o->op_flags & OPf_KIDS) {
5972 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5973 if (kid && kid->op_sibling) {
5974 o->op_type = OP_SSELECT;
5975 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5977 return fold_constants(o);
5981 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5982 if (kid && kid->op_type == OP_RV2GV)
5983 kid->op_private &= ~HINT_STRICT_REFS;
5988 Perl_ck_shift(pTHX_ OP *o)
5990 I32 type = o->op_type;
5992 if (!(o->op_flags & OPf_KIDS)) {
5997 if (!CvUNIQUE(PL_compcv)) {
5998 argop = newOP(OP_PADAV, OPf_REF);
5999 argop->op_targ = 0; /* PL_curpad[0] is @_ */
6002 argop = newUNOP(OP_RV2AV, 0,
6003 scalar(newGVOP(OP_GV, 0,
6004 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6007 argop = newUNOP(OP_RV2AV, 0,
6008 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
6009 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6010 #endif /* USE_THREADS */
6011 return newUNOP(type, 0, scalar(argop));
6013 return scalar(modkids(ck_fun(o), type));
6017 Perl_ck_sort(pTHX_ OP *o)
6022 if (PL_hints & HINT_LOCALE)
6023 o->op_private |= OPpLOCALE;
6026 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6028 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6029 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6031 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6033 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6035 if (kid->op_type == OP_SCOPE) {
6039 else if (kid->op_type == OP_LEAVE) {
6040 if (o->op_type == OP_SORT) {
6041 null(kid); /* wipe out leave */
6044 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6045 if (k->op_next == kid)
6047 /* don't descend into loops */
6048 else if (k->op_type == OP_ENTERLOOP
6049 || k->op_type == OP_ENTERITER)
6051 k = cLOOPx(k)->op_lastop;
6056 kid->op_next = 0; /* just disconnect the leave */
6057 k = kLISTOP->op_first;
6062 if (o->op_type == OP_SORT) {
6063 /* provide scalar context for comparison function/block */
6069 o->op_flags |= OPf_SPECIAL;
6071 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6074 firstkid = firstkid->op_sibling;
6077 /* provide list context for arguments */
6078 if (o->op_type == OP_SORT)
6085 S_simplify_sort(pTHX_ OP *o)
6088 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6092 if (!(o->op_flags & OPf_STACKED))
6094 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6095 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6096 kid = kUNOP->op_first; /* get past null */
6097 if (kid->op_type != OP_SCOPE)
6099 kid = kLISTOP->op_last; /* get past scope */
6100 switch(kid->op_type) {
6108 k = kid; /* remember this node*/
6109 if (kBINOP->op_first->op_type != OP_RV2SV)
6111 kid = kBINOP->op_first; /* get past cmp */
6112 if (kUNOP->op_first->op_type != OP_GV)
6114 kid = kUNOP->op_first; /* get past rv2sv */
6116 if (GvSTASH(gv) != PL_curstash)
6118 if (strEQ(GvNAME(gv), "a"))
6120 else if (strEQ(GvNAME(gv), "b"))
6124 kid = k; /* back to cmp */
6125 if (kBINOP->op_last->op_type != OP_RV2SV)
6127 kid = kBINOP->op_last; /* down to 2nd arg */
6128 if (kUNOP->op_first->op_type != OP_GV)
6130 kid = kUNOP->op_first; /* get past rv2sv */
6132 if (GvSTASH(gv) != PL_curstash
6134 ? strNE(GvNAME(gv), "a")
6135 : strNE(GvNAME(gv), "b")))
6137 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6139 o->op_private |= OPpSORT_REVERSE;
6140 if (k->op_type == OP_NCMP)
6141 o->op_private |= OPpSORT_NUMERIC;
6142 if (k->op_type == OP_I_NCMP)
6143 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6144 kid = cLISTOPo->op_first->op_sibling;
6145 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6146 op_free(kid); /* then delete it */
6147 cLISTOPo->op_children--;
6151 Perl_ck_split(pTHX_ OP *o)
6155 if (o->op_flags & OPf_STACKED)
6156 return no_fh_allowed(o);
6158 kid = cLISTOPo->op_first;
6159 if (kid->op_type != OP_NULL)
6160 Perl_croak(aTHX_ "panic: ck_split");
6161 kid = kid->op_sibling;
6162 op_free(cLISTOPo->op_first);
6163 cLISTOPo->op_first = kid;
6165 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6166 cLISTOPo->op_last = kid; /* There was only one element previously */
6169 if (kid->op_type != OP_MATCH) {
6170 OP *sibl = kid->op_sibling;
6171 kid->op_sibling = 0;
6172 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6173 if (cLISTOPo->op_first == cLISTOPo->op_last)
6174 cLISTOPo->op_last = kid;
6175 cLISTOPo->op_first = kid;
6176 kid->op_sibling = sibl;
6179 kid->op_type = OP_PUSHRE;
6180 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6183 if (!kid->op_sibling)
6184 append_elem(OP_SPLIT, o, newDEFSVOP());
6186 kid = kid->op_sibling;
6189 if (!kid->op_sibling)
6190 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6192 kid = kid->op_sibling;
6195 if (kid->op_sibling)
6196 return too_many_arguments(o,PL_op_desc[o->op_type]);
6202 Perl_ck_join(pTHX_ OP *o)
6204 if (ckWARN(WARN_SYNTAX)) {
6205 OP *kid = cLISTOPo->op_first->op_sibling;
6206 if (kid && kid->op_type == OP_MATCH) {
6207 char *pmstr = "STRING";
6208 if (kPMOP->op_pmregexp)
6209 pmstr = kPMOP->op_pmregexp->precomp;
6210 Perl_warner(aTHX_ WARN_SYNTAX,
6211 "/%s/ should probably be written as \"%s\"",
6219 Perl_ck_subr(pTHX_ OP *o)
6222 OP *prev = ((cUNOPo->op_first->op_sibling)
6223 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6224 OP *o2 = prev->op_sibling;
6233 o->op_private |= OPpENTERSUB_HASTARG;
6234 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6235 if (cvop->op_type == OP_RV2CV) {
6237 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6238 null(cvop); /* disable rv2cv */
6239 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6240 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6241 GV *gv = cGVOPx_gv(tmpop);
6244 tmpop->op_private |= OPpEARLY_CV;
6245 else if (SvPOK(cv)) {
6246 namegv = CvANON(cv) ? gv : CvGV(cv);
6247 proto = SvPV((SV*)cv, n_a);
6251 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6252 if (o2->op_type == OP_CONST)
6253 o2->op_private &= ~OPpCONST_STRICT;
6254 else if (o2->op_type == OP_LIST) {
6255 OP *o = ((UNOP*)o2)->op_first->op_sibling;
6256 if (o && o->op_type == OP_CONST)
6257 o->op_private &= ~OPpCONST_STRICT;
6260 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6261 if (PERLDB_SUB && PL_curstash != PL_debstash)
6262 o->op_private |= OPpENTERSUB_DB;
6263 while (o2 != cvop) {
6267 return too_many_arguments(o, gv_ename(namegv));
6285 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6287 arg == 1 ? "block or sub {}" : "sub {}",
6288 gv_ename(namegv), o2);
6291 /* '*' allows any scalar type, including bareword */
6294 if (o2->op_type == OP_RV2GV)
6295 goto wrapref; /* autoconvert GLOB -> GLOBref */
6296 else if (o2->op_type == OP_CONST)
6297 o2->op_private &= ~OPpCONST_STRICT;
6298 else if (o2->op_type == OP_ENTERSUB) {
6299 /* accidental subroutine, revert to bareword */
6300 OP *gvop = ((UNOP*)o2)->op_first;
6301 if (gvop && gvop->op_type == OP_NULL) {
6302 gvop = ((UNOP*)gvop)->op_first;
6304 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6307 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6308 (gvop = ((UNOP*)gvop)->op_first) &&
6309 gvop->op_type == OP_GV)
6311 GV *gv = cGVOPx_gv(gvop);
6312 OP *sibling = o2->op_sibling;
6313 SV *n = newSVpvn("",0);
6315 gv_fullname3(n, gv, "");
6316 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
6317 sv_chop(n, SvPVX(n)+6);
6318 o2 = newSVOP(OP_CONST, 0, n);
6319 prev->op_sibling = o2;
6320 o2->op_sibling = sibling;
6332 if (o2->op_type != OP_RV2GV)
6333 bad_type(arg, "symbol", gv_ename(namegv), o2);
6336 if (o2->op_type != OP_ENTERSUB)
6337 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6340 if (o2->op_type != OP_RV2SV
6341 && o2->op_type != OP_PADSV
6342 && o2->op_type != OP_HELEM
6343 && o2->op_type != OP_AELEM
6344 && o2->op_type != OP_THREADSV)
6346 bad_type(arg, "scalar", gv_ename(namegv), o2);
6350 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
6351 bad_type(arg, "array", gv_ename(namegv), o2);
6354 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
6355 bad_type(arg, "hash", gv_ename(namegv), o2);
6359 OP* sib = kid->op_sibling;
6360 kid->op_sibling = 0;
6361 o2 = newUNOP(OP_REFGEN, 0, kid);
6362 o2->op_sibling = sib;
6363 prev->op_sibling = o2;
6374 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
6375 gv_ename(namegv), SvPV((SV*)cv, n_a));
6380 mod(o2, OP_ENTERSUB);
6382 o2 = o2->op_sibling;
6384 if (proto && !optional &&
6385 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6386 return too_few_arguments(o, gv_ename(namegv));
6391 Perl_ck_svconst(pTHX_ OP *o)
6393 SvREADONLY_on(cSVOPo->op_sv);
6398 Perl_ck_trunc(pTHX_ OP *o)
6400 if (o->op_flags & OPf_KIDS) {
6401 SVOP *kid = (SVOP*)cUNOPo->op_first;
6403 if (kid->op_type == OP_NULL)
6404 kid = (SVOP*)kid->op_sibling;
6405 if (kid && kid->op_type == OP_CONST &&
6406 (kid->op_private & OPpCONST_BARE))
6408 o->op_flags |= OPf_SPECIAL;
6409 kid->op_private &= ~OPpCONST_STRICT;
6415 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6418 Perl_peep(pTHX_ register OP *o)
6421 register OP* oldop = 0;
6423 OP *last_composite = Nullop;
6425 if (!o || o->op_seq)
6429 SAVEVPTR(PL_curcop);
6430 for (; o; o = o->op_next) {
6436 switch (o->op_type) {
6440 PL_curcop = ((COP*)o); /* for warnings */
6441 o->op_seq = PL_op_seqmax++;
6442 last_composite = Nullop;
6446 if (cSVOPo->op_private & OPpCONST_STRICT)
6447 no_bareword_allowed(o);
6449 /* Relocate sv to the pad for thread safety.
6450 * Despite being a "constant", the SV is written to,
6451 * for reference counts, sv_upgrade() etc. */
6453 PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6454 if (SvPADTMP(cSVOPo->op_sv)) {
6455 /* If op_sv is already a PADTMP then it is being used by
6456 * another pad, so make a copy. */
6457 sv_setsv(PL_curpad[ix],cSVOPo->op_sv);
6458 SvREADONLY_on(PL_curpad[ix]);
6459 SvREFCNT_dec(cSVOPo->op_sv);
6462 SvREFCNT_dec(PL_curpad[ix]);
6463 SvPADTMP_on(cSVOPo->op_sv);
6464 PL_curpad[ix] = cSVOPo->op_sv;
6466 cSVOPo->op_sv = Nullsv;
6470 o->op_seq = PL_op_seqmax++;
6474 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6475 if (o->op_next->op_private & OPpTARGET_MY) {
6476 if (o->op_flags & OPf_STACKED) /* chained concats */
6477 goto ignore_optimization;
6479 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6480 o->op_targ = o->op_next->op_targ;
6481 o->op_next->op_targ = 0;
6482 o->op_private |= OPpTARGET_MY;
6487 ignore_optimization:
6488 o->op_seq = PL_op_seqmax++;
6491 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6492 o->op_seq = PL_op_seqmax++;
6493 break; /* Scalar stub must produce undef. List stub is noop */
6497 if (o->op_targ == OP_NEXTSTATE
6498 || o->op_targ == OP_DBSTATE
6499 || o->op_targ == OP_SETSTATE)
6501 PL_curcop = ((COP*)o);
6508 if (oldop && o->op_next) {
6509 oldop->op_next = o->op_next;
6512 o->op_seq = PL_op_seqmax++;
6516 if (o->op_next->op_type == OP_RV2SV) {
6517 if (!(o->op_next->op_private & OPpDEREF)) {
6519 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6521 o->op_next = o->op_next->op_next;
6522 o->op_type = OP_GVSV;
6523 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6526 else if (o->op_next->op_type == OP_RV2AV) {
6527 OP* pop = o->op_next->op_next;
6529 if (pop->op_type == OP_CONST &&
6530 (PL_op = pop->op_next) &&
6531 pop->op_next->op_type == OP_AELEM &&
6532 !(pop->op_next->op_private &
6533 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6534 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6542 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6543 o->op_next = pop->op_next->op_next;
6544 o->op_type = OP_AELEMFAST;
6545 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6546 o->op_private = (U8)i;
6551 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6553 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6554 /* XXX could check prototype here instead of just carping */
6555 SV *sv = sv_newmortal();
6556 gv_efullname3(sv, gv, Nullch);
6557 Perl_warner(aTHX_ WARN_PROTOTYPE,
6558 "%s() called too early to check prototype",
6563 o->op_seq = PL_op_seqmax++;
6574 o->op_seq = PL_op_seqmax++;
6575 while (cLOGOP->op_other->op_type == OP_NULL)
6576 cLOGOP->op_other = cLOGOP->op_other->op_next;
6577 peep(cLOGOP->op_other);
6581 o->op_seq = PL_op_seqmax++;
6582 peep(cLOOP->op_redoop);
6583 peep(cLOOP->op_nextop);
6584 peep(cLOOP->op_lastop);
6590 o->op_seq = PL_op_seqmax++;
6591 peep(cPMOP->op_pmreplstart);
6595 o->op_seq = PL_op_seqmax++;
6596 if (ckWARN(WARN_SYNTAX) && o->op_next
6597 && o->op_next->op_type == OP_NEXTSTATE) {
6598 if (o->op_next->op_sibling &&
6599 o->op_next->op_sibling->op_type != OP_EXIT &&
6600 o->op_next->op_sibling->op_type != OP_WARN &&
6601 o->op_next->op_sibling->op_type != OP_DIE) {
6602 line_t oldline = CopLINE(PL_curcop);
6604 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6605 Perl_warner(aTHX_ WARN_EXEC,
6606 "Statement unlikely to be reached");
6607 Perl_warner(aTHX_ WARN_EXEC,
6608 "\t(Maybe you meant system() when you said exec()?)\n");
6609 CopLINE_set(PL_curcop, oldline);
6618 SV **svp, **indsvp, *sv;
6623 o->op_seq = PL_op_seqmax++;
6624 if ((o->op_private & (OPpLVAL_INTRO))
6625 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6627 rop = (UNOP*)((BINOP*)o)->op_first;
6628 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6630 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6631 if (!SvOBJECT(lexname))
6633 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6634 if (!fields || !GvHV(*fields))
6636 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6637 key = SvPV(*svp, keylen);
6638 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6640 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6641 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6643 ind = SvIV(*indsvp);
6645 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6646 rop->op_type = OP_RV2AV;
6647 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6648 o->op_type = OP_AELEM;
6649 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6651 if (SvREADONLY(*svp))
6653 SvFLAGS(sv) |= (SvFLAGS(*svp)
6654 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6664 SV **svp, **indsvp, *sv;
6668 SVOP *first_key_op, *key_op;
6670 o->op_seq = PL_op_seqmax++;
6671 if ((o->op_private & (OPpLVAL_INTRO))
6672 /* I bet there's always a pushmark... */
6673 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6674 /* hmmm, no optimization if list contains only one key. */
6676 rop = (UNOP*)((LISTOP*)o)->op_last;
6677 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6679 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6680 if (!SvOBJECT(lexname))
6682 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6683 if (!fields || !GvHV(*fields))
6685 /* Again guessing that the pushmark can be jumped over.... */
6686 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6687 ->op_first->op_sibling;
6688 /* Check that the key list contains only constants. */
6689 for (key_op = first_key_op; key_op;
6690 key_op = (SVOP*)key_op->op_sibling)
6691 if (key_op->op_type != OP_CONST)
6695 rop->op_type = OP_RV2AV;
6696 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6697 o->op_type = OP_ASLICE;
6698 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6699 for (key_op = first_key_op; key_op;
6700 key_op = (SVOP*)key_op->op_sibling) {
6701 svp = cSVOPx_svp(key_op);
6702 key = SvPV(*svp, keylen);
6703 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6705 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6706 "in variable %s of type %s",
6707 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6709 ind = SvIV(*indsvp);
6711 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6713 if (SvREADONLY(*svp))
6715 SvFLAGS(sv) |= (SvFLAGS(*svp)
6716 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6725 if (!(o->op_flags & OPf_WANT)
6726 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6730 o->op_seq = PL_op_seqmax++;
6734 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6735 o->op_seq = PL_op_seqmax++;
6741 if (last_composite) {
6742 OP *r = last_composite;
6744 while (r->op_sibling)
6747 || (r->op_next->op_type == OP_LIST
6748 && r->op_next->op_next == o))
6750 if (last_composite->op_type == OP_RV2AV)
6751 yyerror("Lvalue subs returning arrays not implemented yet");
6753 yyerror("Lvalue subs returning hashes not implemented yet");
6760 o->op_seq = PL_op_seqmax++;