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;
2578 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2579 SV* listsv = newSVpvn("# comment\n",10);
2581 U8* tend = t + tlen;
2582 U8* rend = r + rlen;
2597 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2598 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2601 U8 tmpbuf[UTF8_MAXLEN];
2604 New(1109, cp, tlen, U8*);
2606 transv = newSVpvn("",0);
2615 qsort(cp, i, sizeof(U8*), utf8compare);
2616 for (j = 0; j < i; j++) {
2618 UV val = utf8_to_uv(s, &ulen);
2620 diff = val - nextmin;
2622 t = uv_to_utf8(tmpbuf,nextmin);
2623 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2625 t = uv_to_utf8(tmpbuf, val - 1);
2626 sv_catpvn(transv, "\377", 1);
2627 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2631 val = utf8_to_uv(s+1, &ulen);
2635 t = uv_to_utf8(tmpbuf,nextmin);
2636 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2637 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2638 sv_catpvn(transv, "\377", 1);
2639 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2640 t = (U8*)SvPVX(transv);
2641 tlen = SvCUR(transv);
2644 else if (!rlen && !del) {
2645 r = t; rlen = tlen; rend = tend;
2648 if (to_utf && from_utf) { /* only counting characters */
2650 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2651 o->op_private |= OPpTRANS_IDENTICAL;
2653 else { /* straight latin-1 translation */
2654 if (tlen == 4 && memEQ((char *)t, "\0\377\303\277", 4) &&
2655 rlen == 4 && memEQ((char *)r, "\0\377\303\277", 4))
2656 o->op_private |= OPpTRANS_IDENTICAL;
2660 while (t < tend || tfirst <= tlast) {
2661 /* see if we need more "t" chars */
2662 if (tfirst > tlast) {
2663 tfirst = (I32)utf8_to_uv(t, &ulen);
2665 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2666 tlast = (I32)utf8_to_uv(++t, &ulen);
2673 /* now see if we need more "r" chars */
2674 if (rfirst > rlast) {
2676 rfirst = (I32)utf8_to_uv(r, &ulen);
2678 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2679 rlast = (I32)utf8_to_uv(++r, &ulen);
2688 rfirst = rlast = 0xffffffff;
2692 /* now see which range will peter our first, if either. */
2693 tdiff = tlast - tfirst;
2694 rdiff = rlast - rfirst;
2701 if (rfirst == 0xffffffff) {
2702 diff = tdiff; /* oops, pretend rdiff is infinite */
2704 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2705 (long)tfirst, (long)tlast);
2707 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2711 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2712 (long)tfirst, (long)(tfirst + diff),
2715 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2716 (long)tfirst, (long)rfirst);
2718 if (rfirst + diff > max)
2719 max = rfirst + diff;
2724 else if (rfirst <= 0x800)
2725 grows |= (tfirst < 0x80);
2726 else if (rfirst <= 0x10000)
2727 grows |= (tfirst < 0x800);
2728 else if (rfirst <= 0x200000)
2729 grows |= (tfirst < 0x10000);
2730 else if (rfirst <= 0x4000000)
2731 grows |= (tfirst < 0x200000);
2732 else if (rfirst <= 0x80000000)
2733 grows |= (tfirst < 0x4000000);
2745 else if (max > 0xff)
2750 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2751 SvREFCNT_dec(listsv);
2753 SvREFCNT_dec(transv);
2755 if (!del && havefinal)
2756 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2757 newSVuv((UV)final), 0);
2759 if (grows && to_utf)
2760 o->op_private |= OPpTRANS_GROWS;
2767 tbl = (short*)cPVOPo->op_pv;
2769 Zero(tbl, 256, short);
2770 for (i = 0; i < tlen; i++)
2772 for (i = 0, j = 0; i < 256; i++) {
2788 if (!rlen && !del) {
2791 o->op_private |= OPpTRANS_IDENTICAL;
2793 for (i = 0; i < 256; i++)
2795 for (i = 0, j = 0; i < tlen; i++,j++) {
2798 if (tbl[t[i]] == -1)
2804 if (tbl[t[i]] == -1)
2815 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2820 NewOp(1101, pmop, 1, PMOP);
2821 pmop->op_type = type;
2822 pmop->op_ppaddr = PL_ppaddr[type];
2823 pmop->op_flags = flags;
2824 pmop->op_private = 0 | (flags >> 8);
2826 if (PL_hints & HINT_RE_TAINT)
2827 pmop->op_pmpermflags |= PMf_RETAINT;
2828 if (PL_hints & HINT_LOCALE)
2829 pmop->op_pmpermflags |= PMf_LOCALE;
2830 pmop->op_pmflags = pmop->op_pmpermflags;
2832 /* link into pm list */
2833 if (type != OP_TRANS && PL_curstash) {
2834 pmop->op_pmnext = HvPMROOT(PL_curstash);
2835 HvPMROOT(PL_curstash) = pmop;
2842 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2847 I32 repl_has_vars = 0;
2849 if (o->op_type == OP_TRANS)
2850 return pmtrans(o, expr, repl);
2852 PL_hints |= HINT_BLOCK_SCOPE;
2855 if (expr->op_type == OP_CONST) {
2857 SV *pat = ((SVOP*)expr)->op_sv;
2858 char *p = SvPV(pat, plen);
2859 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2860 sv_setpvn(pat, "\\s+", 3);
2861 p = SvPV(pat, plen);
2862 pm->op_pmflags |= PMf_SKIPWHITE;
2864 if ((PL_hints & HINT_UTF8) || (SvUTF8(pat) && !(PL_hints & HINT_BYTE)))
2865 pm->op_pmdynflags |= PMdf_UTF8;
2866 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
2867 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2868 pm->op_pmflags |= PMf_WHITE;
2872 if (PL_hints & HINT_UTF8)
2873 pm->op_pmdynflags |= PMdf_UTF8;
2874 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2875 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2877 : OP_REGCMAYBE),0,expr);
2879 NewOp(1101, rcop, 1, LOGOP);
2880 rcop->op_type = OP_REGCOMP;
2881 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2882 rcop->op_first = scalar(expr);
2883 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2884 ? (OPf_SPECIAL | OPf_KIDS)
2886 rcop->op_private = 1;
2889 /* establish postfix order */
2890 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2892 rcop->op_next = expr;
2893 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2896 rcop->op_next = LINKLIST(expr);
2897 expr->op_next = (OP*)rcop;
2900 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2905 if (pm->op_pmflags & PMf_EVAL) {
2907 if (CopLINE(PL_curcop) < PL_multi_end)
2908 CopLINE_set(PL_curcop, PL_multi_end);
2911 else if (repl->op_type == OP_THREADSV
2912 && strchr("&`'123456789+",
2913 PL_threadsv_names[repl->op_targ]))
2917 #endif /* USE_THREADS */
2918 else if (repl->op_type == OP_CONST)
2922 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2923 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2925 if (curop->op_type == OP_THREADSV) {
2927 if (strchr("&`'123456789+", curop->op_private))
2931 if (curop->op_type == OP_GV) {
2932 GV *gv = cGVOPx_gv(curop);
2934 if (strchr("&`'123456789+", *GvENAME(gv)))
2937 #endif /* USE_THREADS */
2938 else if (curop->op_type == OP_RV2CV)
2940 else if (curop->op_type == OP_RV2SV ||
2941 curop->op_type == OP_RV2AV ||
2942 curop->op_type == OP_RV2HV ||
2943 curop->op_type == OP_RV2GV) {
2944 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2947 else if (curop->op_type == OP_PADSV ||
2948 curop->op_type == OP_PADAV ||
2949 curop->op_type == OP_PADHV ||
2950 curop->op_type == OP_PADANY) {
2953 else if (curop->op_type == OP_PUSHRE)
2954 ; /* Okay here, dangerous in newASSIGNOP */
2963 && (!pm->op_pmregexp
2964 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2965 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2966 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2967 prepend_elem(o->op_type, scalar(repl), o);
2970 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2971 pm->op_pmflags |= PMf_MAYBE_CONST;
2972 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2974 NewOp(1101, rcop, 1, LOGOP);
2975 rcop->op_type = OP_SUBSTCONT;
2976 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2977 rcop->op_first = scalar(repl);
2978 rcop->op_flags |= OPf_KIDS;
2979 rcop->op_private = 1;
2982 /* establish postfix order */
2983 rcop->op_next = LINKLIST(repl);
2984 repl->op_next = (OP*)rcop;
2986 pm->op_pmreplroot = scalar((OP*)rcop);
2987 pm->op_pmreplstart = LINKLIST(rcop);
2996 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2999 NewOp(1101, svop, 1, SVOP);
3000 svop->op_type = type;
3001 svop->op_ppaddr = PL_ppaddr[type];
3003 svop->op_next = (OP*)svop;
3004 svop->op_flags = flags;
3005 if (PL_opargs[type] & OA_RETSCALAR)
3007 if (PL_opargs[type] & OA_TARGET)
3008 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3009 return CHECKOP(type, svop);
3013 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3016 NewOp(1101, padop, 1, PADOP);
3017 padop->op_type = type;
3018 padop->op_ppaddr = PL_ppaddr[type];
3019 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3020 SvREFCNT_dec(PL_curpad[padop->op_padix]);
3021 PL_curpad[padop->op_padix] = sv;
3023 padop->op_next = (OP*)padop;
3024 padop->op_flags = flags;
3025 if (PL_opargs[type] & OA_RETSCALAR)
3027 if (PL_opargs[type] & OA_TARGET)
3028 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3029 return CHECKOP(type, padop);
3033 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3038 return newPADOP(type, flags, SvREFCNT_inc(gv));
3040 return newSVOP(type, flags, SvREFCNT_inc(gv));
3045 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3048 NewOp(1101, pvop, 1, PVOP);
3049 pvop->op_type = type;
3050 pvop->op_ppaddr = PL_ppaddr[type];
3052 pvop->op_next = (OP*)pvop;
3053 pvop->op_flags = flags;
3054 if (PL_opargs[type] & OA_RETSCALAR)
3056 if (PL_opargs[type] & OA_TARGET)
3057 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3058 return CHECKOP(type, pvop);
3062 Perl_package(pTHX_ OP *o)
3067 save_hptr(&PL_curstash);
3068 save_item(PL_curstname);
3073 name = SvPV(sv, len);
3074 PL_curstash = gv_stashpvn(name,len,TRUE);
3075 sv_setpvn(PL_curstname, name, len);
3079 sv_setpv(PL_curstname,"<none>");
3080 PL_curstash = Nullhv;
3082 PL_hints |= HINT_BLOCK_SCOPE;
3083 PL_copline = NOLINE;
3088 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
3096 if (id->op_type != OP_CONST)
3097 Perl_croak(aTHX_ "Module name must be constant");
3101 if (version != Nullop) {
3102 SV *vesv = ((SVOP*)version)->op_sv;
3104 if (arg == Nullop && !SvNIOKp(vesv)) {
3111 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3112 Perl_croak(aTHX_ "Version number must be constant number");
3114 /* Make copy of id so we don't free it twice */
3115 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3117 /* Fake up a method call to VERSION */
3118 meth = newSVpvn("VERSION",7);
3119 sv_upgrade(meth, SVt_PVIV);
3120 (void)SvIOK_on(meth);
3121 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3122 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3123 append_elem(OP_LIST,
3124 prepend_elem(OP_LIST, pack, list(version)),
3125 newSVOP(OP_METHOD_NAMED, 0, meth)));
3129 /* Fake up an import/unimport */
3130 if (arg && arg->op_type == OP_STUB)
3131 imop = arg; /* no import on explicit () */
3132 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
3133 imop = Nullop; /* use 5.0; */
3138 /* Make copy of id so we don't free it twice */
3139 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3141 /* Fake up a method call to import/unimport */
3142 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);;
3143 sv_upgrade(meth, SVt_PVIV);
3144 (void)SvIOK_on(meth);
3145 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
3146 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3147 append_elem(OP_LIST,
3148 prepend_elem(OP_LIST, pack, list(arg)),
3149 newSVOP(OP_METHOD_NAMED, 0, meth)));
3152 /* Fake up a require, handle override, if any */
3153 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3154 if (!(gv && GvIMPORTED_CV(gv)))
3155 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3157 if (gv && GvIMPORTED_CV(gv)) {
3158 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3159 append_elem(OP_LIST, id,
3160 scalar(newUNOP(OP_RV2CV, 0,
3165 rqop = newUNOP(OP_REQUIRE, 0, id);
3168 /* Fake up the BEGIN {}, which does its thing immediately. */
3170 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
3173 append_elem(OP_LINESEQ,
3174 append_elem(OP_LINESEQ,
3175 newSTATEOP(0, Nullch, rqop),
3176 newSTATEOP(0, Nullch, veop)),
3177 newSTATEOP(0, Nullch, imop) ));
3179 PL_hints |= HINT_BLOCK_SCOPE;
3180 PL_copline = NOLINE;
3185 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3188 va_start(args, ver);
3189 vload_module(flags, name, ver, &args);
3193 #ifdef PERL_IMPLICIT_CONTEXT
3195 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3199 va_start(args, ver);
3200 vload_module(flags, name, ver, &args);
3206 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3208 OP *modname, *veop, *imop;
3210 modname = newSVOP(OP_CONST, 0, name);
3211 modname->op_private |= OPpCONST_BARE;
3213 veop = newSVOP(OP_CONST, 0, ver);
3217 if (flags & PERL_LOADMOD_NOIMPORT) {
3218 imop = sawparens(newNULLLIST());
3220 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3221 imop = va_arg(*args, OP*);
3226 sv = va_arg(*args, SV*);
3228 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3229 sv = va_arg(*args, SV*);
3233 line_t ocopline = PL_copline;
3234 int oexpect = PL_expect;
3236 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3237 veop, modname, imop);
3238 PL_expect = oexpect;
3239 PL_copline = ocopline;
3244 Perl_dofile(pTHX_ OP *term)
3249 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3250 if (!(gv && GvIMPORTED_CV(gv)))
3251 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3253 if (gv && GvIMPORTED_CV(gv)) {
3254 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3255 append_elem(OP_LIST, term,
3256 scalar(newUNOP(OP_RV2CV, 0,
3261 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3267 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3269 return newBINOP(OP_LSLICE, flags,
3270 list(force_list(subscript)),
3271 list(force_list(listval)) );
3275 S_list_assignment(pTHX_ register OP *o)
3280 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3281 o = cUNOPo->op_first;
3283 if (o->op_type == OP_COND_EXPR) {
3284 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3285 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3290 yyerror("Assignment to both a list and a scalar");
3294 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3295 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3296 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3299 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3302 if (o->op_type == OP_RV2SV)
3309 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3314 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3315 return newLOGOP(optype, 0,
3316 mod(scalar(left), optype),
3317 newUNOP(OP_SASSIGN, 0, scalar(right)));
3320 return newBINOP(optype, OPf_STACKED,
3321 mod(scalar(left), optype), scalar(right));
3325 if (list_assignment(left)) {
3330 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3331 left = mod(left, OP_AASSIGN);
3339 curop = list(force_list(left));
3340 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3341 o->op_private = 0 | (flags >> 8);
3342 for (curop = ((LISTOP*)curop)->op_first;
3343 curop; curop = curop->op_sibling)
3345 if (curop->op_type == OP_RV2HV &&
3346 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3347 o->op_private |= OPpASSIGN_HASH;
3351 if (!(left->op_private & OPpLVAL_INTRO)) {
3354 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3355 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3356 if (curop->op_type == OP_GV) {
3357 GV *gv = cGVOPx_gv(curop);
3358 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3360 SvCUR(gv) = PL_generation;
3362 else if (curop->op_type == OP_PADSV ||
3363 curop->op_type == OP_PADAV ||
3364 curop->op_type == OP_PADHV ||
3365 curop->op_type == OP_PADANY) {
3366 SV **svp = AvARRAY(PL_comppad_name);
3367 SV *sv = svp[curop->op_targ];
3368 if (SvCUR(sv) == PL_generation)
3370 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
3372 else if (curop->op_type == OP_RV2CV)
3374 else if (curop->op_type == OP_RV2SV ||
3375 curop->op_type == OP_RV2AV ||
3376 curop->op_type == OP_RV2HV ||
3377 curop->op_type == OP_RV2GV) {
3378 if (lastop->op_type != OP_GV) /* funny deref? */
3381 else if (curop->op_type == OP_PUSHRE) {
3382 if (((PMOP*)curop)->op_pmreplroot) {
3383 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3384 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
3386 SvCUR(gv) = PL_generation;
3395 o->op_private |= OPpASSIGN_COMMON;
3397 if (right && right->op_type == OP_SPLIT) {
3399 if ((tmpop = ((LISTOP*)right)->op_first) &&
3400 tmpop->op_type == OP_PUSHRE)
3402 PMOP *pm = (PMOP*)tmpop;
3403 if (left->op_type == OP_RV2AV &&
3404 !(left->op_private & OPpLVAL_INTRO) &&
3405 !(o->op_private & OPpASSIGN_COMMON) )
3407 tmpop = ((UNOP*)left)->op_first;
3408 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3410 pm->op_pmreplroot = (OP*)cPADOPx(tmpop)->op_padix;
3411 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3413 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3414 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3416 pm->op_pmflags |= PMf_ONCE;
3417 tmpop = cUNOPo->op_first; /* to list (nulled) */
3418 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3419 tmpop->op_sibling = Nullop; /* don't free split */
3420 right->op_next = tmpop->op_next; /* fix starting loc */
3421 op_free(o); /* blow off assign */
3422 right->op_flags &= ~OPf_WANT;
3423 /* "I don't know and I don't care." */
3428 if (PL_modcount < 10000 &&
3429 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3431 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3433 sv_setiv(sv, PL_modcount+1);
3441 right = newOP(OP_UNDEF, 0);
3442 if (right->op_type == OP_READLINE) {
3443 right->op_flags |= OPf_STACKED;
3444 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3447 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3448 o = newBINOP(OP_SASSIGN, flags,
3449 scalar(right), mod(scalar(left), OP_SASSIGN) );
3461 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3464 U32 seq = intro_my();
3467 NewOp(1101, cop, 1, COP);
3468 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3469 cop->op_type = OP_DBSTATE;
3470 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3473 cop->op_type = OP_NEXTSTATE;
3474 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3476 cop->op_flags = flags;
3477 cop->op_private = (PL_hints & HINT_BYTE);
3479 cop->op_private |= NATIVE_HINTS;
3481 PL_compiling.op_private = cop->op_private;
3482 cop->op_next = (OP*)cop;
3485 cop->cop_label = label;
3486 PL_hints |= HINT_BLOCK_SCOPE;
3489 cop->cop_arybase = PL_curcop->cop_arybase;
3490 if (specialWARN(PL_curcop->cop_warnings))
3491 cop->cop_warnings = PL_curcop->cop_warnings ;
3493 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3496 if (PL_copline == NOLINE)
3497 CopLINE_set(cop, CopLINE(PL_curcop));
3499 CopLINE_set(cop, PL_copline);
3500 PL_copline = NOLINE;
3503 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3505 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3507 CopSTASH_set(cop, PL_curstash);
3509 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3510 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3511 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
3512 (void)SvIOK_on(*svp);
3513 SvIVX(*svp) = PTR2IV(cop);
3517 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3520 /* "Introduce" my variables to visible status. */
3528 if (! PL_min_intro_pending)
3529 return PL_cop_seqmax;
3531 svp = AvARRAY(PL_comppad_name);
3532 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3533 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
3534 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
3535 SvNVX(sv) = (NV)PL_cop_seqmax;
3538 PL_min_intro_pending = 0;
3539 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3540 return PL_cop_seqmax++;
3544 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3546 return new_logop(type, flags, &first, &other);
3550 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3555 OP *first = *firstp;
3556 OP *other = *otherp;
3558 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3559 return newBINOP(type, flags, scalar(first), scalar(other));
3561 scalarboolean(first);
3562 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3563 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3564 if (type == OP_AND || type == OP_OR) {
3570 first = *firstp = cUNOPo->op_first;
3572 first->op_next = o->op_next;
3573 cUNOPo->op_first = Nullop;
3577 if (first->op_type == OP_CONST) {
3578 if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE))
3579 Perl_warner(aTHX_ WARN_BAREWORD, "Bareword found in conditional");
3580 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3591 else if (first->op_type == OP_WANTARRAY) {
3597 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
3598 OP *k1 = ((UNOP*)first)->op_first;
3599 OP *k2 = k1->op_sibling;
3601 switch (first->op_type)
3604 if (k2 && k2->op_type == OP_READLINE
3605 && (k2->op_flags & OPf_STACKED)
3606 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3608 warnop = k2->op_type;
3613 if (k1->op_type == OP_READDIR
3614 || k1->op_type == OP_GLOB
3615 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3616 || k1->op_type == OP_EACH)
3618 warnop = ((k1->op_type == OP_NULL)
3619 ? k1->op_targ : k1->op_type);
3624 line_t oldline = CopLINE(PL_curcop);
3625 CopLINE_set(PL_curcop, PL_copline);
3626 Perl_warner(aTHX_ WARN_MISC,
3627 "Value of %s%s can be \"0\"; test with defined()",
3629 ((warnop == OP_READLINE || warnop == OP_GLOB)
3630 ? " construct" : "() operator"));
3631 CopLINE_set(PL_curcop, oldline);
3638 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3639 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3641 NewOp(1101, logop, 1, LOGOP);
3643 logop->op_type = type;
3644 logop->op_ppaddr = PL_ppaddr[type];
3645 logop->op_first = first;
3646 logop->op_flags = flags | OPf_KIDS;
3647 logop->op_other = LINKLIST(other);
3648 logop->op_private = 1 | (flags >> 8);
3650 /* establish postfix order */
3651 logop->op_next = LINKLIST(first);
3652 first->op_next = (OP*)logop;
3653 first->op_sibling = other;
3655 o = newUNOP(OP_NULL, 0, (OP*)logop);
3662 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3670 return newLOGOP(OP_AND, 0, first, trueop);
3672 return newLOGOP(OP_OR, 0, first, falseop);
3674 scalarboolean(first);
3675 if (first->op_type == OP_CONST) {
3676 if (SvTRUE(((SVOP*)first)->op_sv)) {
3687 else if (first->op_type == OP_WANTARRAY) {
3691 NewOp(1101, logop, 1, LOGOP);
3692 logop->op_type = OP_COND_EXPR;
3693 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3694 logop->op_first = first;
3695 logop->op_flags = flags | OPf_KIDS;
3696 logop->op_private = 1 | (flags >> 8);
3697 logop->op_other = LINKLIST(trueop);
3698 logop->op_next = LINKLIST(falseop);
3701 /* establish postfix order */
3702 start = LINKLIST(first);
3703 first->op_next = (OP*)logop;
3705 first->op_sibling = trueop;
3706 trueop->op_sibling = falseop;
3707 o = newUNOP(OP_NULL, 0, (OP*)logop);
3709 trueop->op_next = falseop->op_next = o;
3716 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3725 NewOp(1101, range, 1, LOGOP);
3727 range->op_type = OP_RANGE;
3728 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3729 range->op_first = left;
3730 range->op_flags = OPf_KIDS;
3731 leftstart = LINKLIST(left);
3732 range->op_other = LINKLIST(right);
3733 range->op_private = 1 | (flags >> 8);
3735 left->op_sibling = right;
3737 range->op_next = (OP*)range;
3738 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3739 flop = newUNOP(OP_FLOP, 0, flip);
3740 o = newUNOP(OP_NULL, 0, flop);
3742 range->op_next = leftstart;
3744 left->op_next = flip;
3745 right->op_next = flop;
3747 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3748 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3749 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3750 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3752 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3753 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3756 if (!flip->op_private || !flop->op_private)
3757 linklist(o); /* blow off optimizer unless constant */
3763 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3768 int once = block && block->op_flags & OPf_SPECIAL &&
3769 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3772 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3773 return block; /* do {} while 0 does once */
3774 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3775 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3776 expr = newUNOP(OP_DEFINED, 0,
3777 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3778 } else if (expr->op_flags & OPf_KIDS) {
3779 OP *k1 = ((UNOP*)expr)->op_first;
3780 OP *k2 = (k1) ? k1->op_sibling : NULL;
3781 switch (expr->op_type) {
3783 if (k2 && k2->op_type == OP_READLINE
3784 && (k2->op_flags & OPf_STACKED)
3785 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3786 expr = newUNOP(OP_DEFINED, 0, expr);
3790 if (k1->op_type == OP_READDIR
3791 || k1->op_type == OP_GLOB
3792 || (k1->op_type == OP_NULL && k1->op_targ == OP_NULL)
3793 || k1->op_type == OP_EACH)
3794 expr = newUNOP(OP_DEFINED, 0, expr);
3800 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3801 o = new_logop(OP_AND, 0, &expr, &listop);
3804 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3806 if (once && o != listop)
3807 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3810 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3812 o->op_flags |= flags;
3814 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3819 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
3829 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3830 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
3831 expr = newUNOP(OP_DEFINED, 0,
3832 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3833 } else if (expr && (expr->op_flags & OPf_KIDS)) {
3834 OP *k1 = ((UNOP*)expr)->op_first;
3835 OP *k2 = (k1) ? k1->op_sibling : NULL;
3836 switch (expr->op_type) {
3838 if (k2 && k2->op_type == OP_READLINE
3839 && (k2->op_flags & OPf_STACKED)
3840 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3841 expr = newUNOP(OP_DEFINED, 0, expr);
3845 if (k1->op_type == OP_READDIR
3846 || k1->op_type == OP_GLOB
3847 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3848 || k1->op_type == OP_EACH)
3849 expr = newUNOP(OP_DEFINED, 0, expr);
3855 block = newOP(OP_NULL, 0);
3857 block = scope(block);
3861 next = LINKLIST(cont);
3862 loopflags |= OPpLOOP_CONTINUE;
3865 OP *unstack = newOP(OP_UNSTACK, 0);
3868 cont = append_elem(OP_LINESEQ, cont, unstack);
3869 if ((line_t)whileline != NOLINE) {
3870 PL_copline = whileline;
3871 cont = append_elem(OP_LINESEQ, cont,
3872 newSTATEOP(0, Nullch, Nullop));
3876 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3877 redo = LINKLIST(listop);
3880 PL_copline = whileline;
3882 o = new_logop(OP_AND, 0, &expr, &listop);
3883 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3884 op_free(expr); /* oops, it's a while (0) */
3886 return Nullop; /* listop already freed by new_logop */
3889 ((LISTOP*)listop)->op_last->op_next = condop =
3890 (o == listop ? redo : LINKLIST(o));
3896 NewOp(1101,loop,1,LOOP);
3897 loop->op_type = OP_ENTERLOOP;
3898 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3899 loop->op_private = 0;
3900 loop->op_next = (OP*)loop;
3903 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3905 loop->op_redoop = redo;
3906 loop->op_lastop = o;
3907 o->op_private |= loopflags;
3910 loop->op_nextop = next;
3912 loop->op_nextop = o;
3914 o->op_flags |= flags;
3915 o->op_private |= (flags >> 8);
3920 Perl_newFOROP(pTHX_ I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3928 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3929 sv->op_type = OP_RV2GV;
3930 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3932 else if (sv->op_type == OP_PADSV) { /* private variable */
3933 padoff = sv->op_targ;
3938 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3939 padoff = sv->op_targ;
3941 iterflags |= OPf_SPECIAL;
3946 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3950 padoff = find_threadsv("_");
3951 iterflags |= OPf_SPECIAL;
3953 sv = newGVOP(OP_GV, 0, PL_defgv);
3956 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3957 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3958 iterflags |= OPf_STACKED;
3960 else if (expr->op_type == OP_NULL &&
3961 (expr->op_flags & OPf_KIDS) &&
3962 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3964 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3965 * set the STACKED flag to indicate that these values are to be
3966 * treated as min/max values by 'pp_iterinit'.
3968 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3969 LOGOP* range = (LOGOP*) flip->op_first;
3970 OP* left = range->op_first;
3971 OP* right = left->op_sibling;
3974 range->op_flags &= ~OPf_KIDS;
3975 range->op_first = Nullop;
3977 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3978 listop->op_first->op_next = range->op_next;
3979 left->op_next = range->op_other;
3980 right->op_next = (OP*)listop;
3981 listop->op_next = listop->op_first;
3984 expr = (OP*)(listop);
3986 iterflags |= OPf_STACKED;
3989 expr = mod(force_list(expr), OP_GREPSTART);
3993 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3994 append_elem(OP_LIST, expr, scalar(sv))));
3995 assert(!loop->op_next);
3996 #ifdef PL_OP_SLAB_ALLOC
3999 NewOp(1234,tmp,1,LOOP);
4000 Copy(loop,tmp,1,LOOP);
4004 Renew(loop, 1, LOOP);
4006 loop->op_targ = padoff;
4007 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
4008 PL_copline = forline;
4009 return newSTATEOP(0, label, wop);
4013 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4019 if (type != OP_GOTO || label->op_type == OP_CONST) {
4020 /* "last()" means "last" */
4021 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4022 o = newOP(type, OPf_SPECIAL);
4024 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4025 ? SvPVx(((SVOP*)label)->op_sv, n_a)
4031 if (label->op_type == OP_ENTERSUB)
4032 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4033 o = newUNOP(type, OPf_STACKED, label);
4035 PL_hints |= HINT_BLOCK_SCOPE;
4040 Perl_cv_undef(pTHX_ CV *cv)
4045 MUTEX_DESTROY(CvMUTEXP(cv));
4046 Safefree(CvMUTEXP(cv));
4049 #endif /* USE_THREADS */
4051 if (!CvXSUB(cv) && CvROOT(cv)) {
4053 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4054 Perl_croak(aTHX_ "Can't undef active subroutine");
4057 Perl_croak(aTHX_ "Can't undef active subroutine");
4058 #endif /* USE_THREADS */
4061 SAVEVPTR(PL_curpad);
4065 op_free(CvROOT(cv));
4066 CvROOT(cv) = Nullop;
4069 SvPOK_off((SV*)cv); /* forget prototype */
4071 SvREFCNT_dec(CvGV(cv));
4073 SvREFCNT_dec(CvOUTSIDE(cv));
4074 CvOUTSIDE(cv) = Nullcv;
4075 if (CvPADLIST(cv)) {
4076 /* may be during global destruction */
4077 if (SvREFCNT(CvPADLIST(cv))) {
4078 I32 i = AvFILLp(CvPADLIST(cv));
4080 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
4081 SV* sv = svp ? *svp : Nullsv;
4084 if (sv == (SV*)PL_comppad_name)
4085 PL_comppad_name = Nullav;
4086 else if (sv == (SV*)PL_comppad) {
4087 PL_comppad = Nullav;
4088 PL_curpad = Null(SV**);
4092 SvREFCNT_dec((SV*)CvPADLIST(cv));
4094 CvPADLIST(cv) = Nullav;
4099 S_cv_dump(pTHX_ CV *cv)
4102 CV *outside = CvOUTSIDE(cv);
4103 AV* padlist = CvPADLIST(cv);
4110 PerlIO_printf(Perl_debug_log,
4111 "\tCV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n",
4113 (CvANON(cv) ? "ANON"
4114 : (cv == PL_main_cv) ? "MAIN"
4115 : CvUNIQUE(cv) ? "UNIQUE"
4116 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
4119 : CvANON(outside) ? "ANON"
4120 : (outside == PL_main_cv) ? "MAIN"
4121 : CvUNIQUE(outside) ? "UNIQUE"
4122 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
4127 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
4128 pad = (AV*)*av_fetch(padlist, 1, FALSE);
4129 pname = AvARRAY(pad_name);
4130 ppad = AvARRAY(pad);
4132 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
4133 if (SvPOK(pname[ix]))
4134 PerlIO_printf(Perl_debug_log,
4135 "\t%4d. 0x%"UVxf" (%s\"%s\" %"IVdf"-%"IVdf")\n",
4136 (int)ix, PTR2UV(ppad[ix]),
4137 SvFAKE(pname[ix]) ? "FAKE " : "",
4139 (IV)I_32(SvNVX(pname[ix])),
4142 #endif /* DEBUGGING */
4146 S_cv_clone2(pTHX_ CV *proto, CV *outside)
4151 AV* protopadlist = CvPADLIST(proto);
4152 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
4153 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
4154 SV** pname = AvARRAY(protopad_name);
4155 SV** ppad = AvARRAY(protopad);
4156 I32 fname = AvFILLp(protopad_name);
4157 I32 fpad = AvFILLp(protopad);
4161 assert(!CvUNIQUE(proto));
4165 SAVESPTR(PL_comppad_name);
4166 SAVESPTR(PL_compcv);
4168 cv = PL_compcv = (CV*)NEWSV(1104,0);
4169 sv_upgrade((SV *)cv, SvTYPE(proto));
4170 CvFLAGS(cv) = CvFLAGS(proto) & ~CVf_CLONE;
4174 New(666, CvMUTEXP(cv), 1, perl_mutex);
4175 MUTEX_INIT(CvMUTEXP(cv));
4177 #endif /* USE_THREADS */
4178 CvFILE(cv) = CvFILE(proto);
4179 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
4180 CvSTASH(cv) = CvSTASH(proto);
4181 CvROOT(cv) = CvROOT(proto);
4182 CvSTART(cv) = CvSTART(proto);
4184 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
4187 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
4189 PL_comppad_name = newAV();
4190 for (ix = fname; ix >= 0; ix--)
4191 av_store(PL_comppad_name, ix, SvREFCNT_inc(pname[ix]));
4193 PL_comppad = newAV();
4195 comppadlist = newAV();
4196 AvREAL_off(comppadlist);
4197 av_store(comppadlist, 0, (SV*)PL_comppad_name);
4198 av_store(comppadlist, 1, (SV*)PL_comppad);
4199 CvPADLIST(cv) = comppadlist;
4200 av_fill(PL_comppad, AvFILLp(protopad));
4201 PL_curpad = AvARRAY(PL_comppad);
4203 av = newAV(); /* will be @_ */
4205 av_store(PL_comppad, 0, (SV*)av);
4206 AvFLAGS(av) = AVf_REIFY;
4208 for (ix = fpad; ix > 0; ix--) {
4209 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4210 if (namesv && namesv != &PL_sv_undef) {
4211 char *name = SvPVX(namesv); /* XXX */
4212 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
4213 I32 off = pad_findlex(name, ix, SvIVX(namesv),
4214 CvOUTSIDE(cv), cxstack_ix, 0, 0);
4216 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4218 Perl_croak(aTHX_ "panic: cv_clone: %s", name);
4220 else { /* our own lexical */
4223 /* anon code -- we'll come back for it */
4224 sv = SvREFCNT_inc(ppad[ix]);
4226 else if (*name == '@')
4228 else if (*name == '%')
4237 else if (IS_PADGV(ppad[ix]) || IS_PADCONST(ppad[ix])) {
4238 PL_curpad[ix] = SvREFCNT_inc(ppad[ix]);
4241 SV* sv = NEWSV(0,0);
4247 /* Now that vars are all in place, clone nested closures. */
4249 for (ix = fpad; ix > 0; ix--) {
4250 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
4252 && namesv != &PL_sv_undef
4253 && !(SvFLAGS(namesv) & SVf_FAKE)
4254 && *SvPVX(namesv) == '&'
4255 && CvCLONE(ppad[ix]))
4257 CV *kid = cv_clone2((CV*)ppad[ix], cv);
4258 SvREFCNT_dec(ppad[ix]);
4261 PL_curpad[ix] = (SV*)kid;
4265 #ifdef DEBUG_CLOSURES
4266 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
4268 PerlIO_printf(Perl_debug_log, " from:\n");
4270 PerlIO_printf(Perl_debug_log, " to:\n");
4279 Perl_cv_clone(pTHX_ CV *proto)
4282 LOCK_CRED_MUTEX; /* XXX create separate mutex */
4283 cv = cv_clone2(proto, CvOUTSIDE(proto));
4284 UNLOCK_CRED_MUTEX; /* XXX create separate mutex */
4289 Perl_cv_ckproto(pTHX_ CV *cv, GV *gv, char *p)
4293 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4294 SV* msg = sv_newmortal();
4298 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4299 sv_setpv(msg, "Prototype mismatch:");
4301 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4303 Perl_sv_catpvf(aTHX_ msg, " (%s)", SvPVX(cv));
4304 sv_catpv(msg, " vs ");
4306 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4308 sv_catpv(msg, "none");
4309 Perl_warner(aTHX_ WARN_PROTOTYPE, "%"SVf, msg);
4314 Perl_cv_const_sv(pTHX_ CV *cv)
4316 if (!cv || !SvPOK(cv) || SvCUR(cv))
4318 return op_const_sv(CvSTART(cv), cv);
4322 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4329 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4330 o = cLISTOPo->op_first->op_sibling;
4332 for (; o; o = o->op_next) {
4333 OPCODE type = o->op_type;
4335 if (sv && o->op_next == o)
4337 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4339 if (type == OP_LEAVESUB || type == OP_RETURN)
4343 if (type == OP_CONST && cSVOPo->op_sv)
4345 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4346 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
4347 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
4348 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
4360 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4370 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4374 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4376 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4380 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4387 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, n_a) : Nullch;
4391 name = o ? SvPVx(cSVOPo->op_sv, n_a) : Nullch;
4392 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4393 SV *sv = sv_newmortal();
4394 Perl_sv_setpvf(aTHX_ sv, "__ANON__[%s:%"IVdf"]",
4395 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4400 gv = gv_fetchpv(name ? name : (aname ? aname : "__ANON__"),
4401 GV_ADDMULTI | ((block || attrs) ? 0 : GV_NOINIT),
4411 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4412 maximum a prototype before. */
4413 if (SvTYPE(gv) > SVt_NULL) {
4414 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4415 && ckWARN_d(WARN_PROTOTYPE))
4417 Perl_warner(aTHX_ WARN_PROTOTYPE, "Runaway prototype");
4419 cv_ckproto((CV*)gv, NULL, ps);
4422 sv_setpv((SV*)gv, ps);
4424 sv_setiv((SV*)gv, -1);
4425 SvREFCNT_dec(PL_compcv);
4426 cv = PL_compcv = NULL;
4427 PL_sub_generation++;
4431 if (!name || GvCVGEN(gv))
4433 else if ((cv = GvCV(gv))) {
4434 bool exists = CvROOT(cv) || CvXSUB(cv);
4435 /* if the subroutine doesn't exist and wasn't pre-declared
4436 * with a prototype, assume it will be AUTOLOADed,
4437 * skipping the prototype check
4439 if (exists || SvPOK(cv))
4440 cv_ckproto(cv, gv, ps);
4441 /* already defined (or promised)? */
4442 if (exists || GvASSUMECV(gv)) {
4444 bool const_changed = TRUE;
4445 if (!block && !attrs) {
4446 /* just a "sub foo;" when &foo is already defined */
4447 SAVEFREESV(PL_compcv);
4450 /* ahem, death to those who redefine active sort subs */
4451 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4452 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4455 if ((const_sv = cv_const_sv(cv)))
4456 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4457 if ((const_sv && const_changed) || ckWARN(WARN_REDEFINE))
4459 line_t oldline = CopLINE(PL_curcop);
4460 CopLINE_set(PL_curcop, PL_copline);
4461 Perl_warner(aTHX_ WARN_REDEFINE,
4462 const_sv ? "Constant subroutine %s redefined"
4463 : "Subroutine %s redefined", name);
4464 CopLINE_set(PL_curcop, oldline);
4475 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4476 * before we clobber PL_compcv.
4480 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4481 stash = GvSTASH(CvGV(cv));
4482 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4483 stash = CvSTASH(cv);
4485 stash = PL_curstash;
4488 /* possibly about to re-define existing subr -- ignore old cv */
4489 rcv = (SV*)PL_compcv;
4490 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4491 stash = GvSTASH(gv);
4493 stash = PL_curstash;
4495 apply_attrs(stash, rcv, attrs);
4497 if (cv) { /* must reuse cv if autoloaded */
4499 /* got here with just attrs -- work done, so bug out */
4500 SAVEFREESV(PL_compcv);
4504 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4505 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4506 CvOUTSIDE(PL_compcv) = 0;
4507 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4508 CvPADLIST(PL_compcv) = 0;
4509 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4510 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4511 SvREFCNT_dec(PL_compcv);
4518 PL_sub_generation++;
4521 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4522 CvFILE(cv) = CopFILE(PL_curcop);
4523 CvSTASH(cv) = PL_curstash;
4526 if (!CvMUTEXP(cv)) {
4527 New(666, CvMUTEXP(cv), 1, perl_mutex);
4528 MUTEX_INIT(CvMUTEXP(cv));
4530 #endif /* USE_THREADS */
4533 sv_setpv((SV*)cv, ps);
4535 if (PL_error_count) {
4539 char *s = strrchr(name, ':');
4541 if (strEQ(s, "BEGIN")) {
4543 "BEGIN not safe after errors--compilation aborted";
4544 if (PL_in_eval & EVAL_KEEPERR)
4545 Perl_croak(aTHX_ not_safe);
4547 /* force display of errors found but not reported */
4548 sv_catpv(ERRSV, not_safe);
4549 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4556 PL_copline = NOLINE;
4561 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4562 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4565 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4568 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4570 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4571 OpREFCNT_set(CvROOT(cv), 1);
4572 CvSTART(cv) = LINKLIST(CvROOT(cv));
4573 CvROOT(cv)->op_next = 0;
4576 /* now that optimizer has done its work, adjust pad values */
4578 SV **namep = AvARRAY(PL_comppad_name);
4579 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4582 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4585 * The only things that a clonable function needs in its
4586 * pad are references to outer lexicals and anonymous subs.
4587 * The rest are created anew during cloning.
4589 if (!((namesv = namep[ix]) != Nullsv &&
4590 namesv != &PL_sv_undef &&
4592 *SvPVX(namesv) == '&')))
4594 SvREFCNT_dec(PL_curpad[ix]);
4595 PL_curpad[ix] = Nullsv;
4600 AV *av = newAV(); /* Will be @_ */
4602 av_store(PL_comppad, 0, (SV*)av);
4603 AvFLAGS(av) = AVf_REIFY;
4605 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4606 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4608 if (!SvPADMY(PL_curpad[ix]))
4609 SvPADTMP_on(PL_curpad[ix]);
4613 if (name || aname) {
4615 char *tname = (name ? name : aname);
4617 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4618 SV *sv = NEWSV(0,0);
4619 SV *tmpstr = sv_newmortal();
4620 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4624 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4626 (long)PL_subline, (long)CopLINE(PL_curcop));
4627 gv_efullname3(tmpstr, gv, Nullch);
4628 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4629 hv = GvHVn(db_postponed);
4630 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4631 && (pcv = GvCV(db_postponed)))
4637 call_sv((SV*)pcv, G_DISCARD);
4641 if ((s = strrchr(tname,':')))
4646 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4649 if (strEQ(s, "BEGIN")) {
4650 I32 oldscope = PL_scopestack_ix;
4652 SAVECOPFILE(&PL_compiling);
4653 SAVECOPLINE(&PL_compiling);
4655 sv_setsv(PL_rs, PL_nrs);
4658 PL_beginav = newAV();
4659 DEBUG_x( dump_sub(gv) );
4660 av_push(PL_beginav, (SV*)cv);
4661 GvCV(gv) = 0; /* cv has been hijacked */
4662 call_list(oldscope, PL_beginav);
4664 PL_curcop = &PL_compiling;
4665 PL_compiling.op_private = PL_hints;
4668 else if (strEQ(s, "END") && !PL_error_count) {
4671 DEBUG_x( dump_sub(gv) );
4672 av_unshift(PL_endav, 1);
4673 av_store(PL_endav, 0, (SV*)cv);
4674 GvCV(gv) = 0; /* cv has been hijacked */
4676 else if (strEQ(s, "CHECK") && !PL_error_count) {
4678 PL_checkav = newAV();
4679 DEBUG_x( dump_sub(gv) );
4680 if (PL_main_start && ckWARN(WARN_VOID))
4681 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4682 av_unshift(PL_checkav, 1);
4683 av_store(PL_checkav, 0, (SV*)cv);
4684 GvCV(gv) = 0; /* cv has been hijacked */
4686 else if (strEQ(s, "INIT") && !PL_error_count) {
4688 PL_initav = newAV();
4689 DEBUG_x( dump_sub(gv) );
4690 if (PL_main_start && ckWARN(WARN_VOID))
4691 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4692 av_push(PL_initav, (SV*)cv);
4693 GvCV(gv) = 0; /* cv has been hijacked */
4698 PL_copline = NOLINE;
4703 /* XXX unsafe for threads if eval_owner isn't held */
4705 =for apidoc newCONSTSUB
4707 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4708 eligible for inlining at compile-time.
4714 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4720 SAVECOPLINE(PL_curcop);
4721 CopLINE_set(PL_curcop, PL_copline);
4724 PL_hints &= ~HINT_BLOCK_SCOPE;
4727 SAVESPTR(PL_curstash);
4728 SAVECOPSTASH(PL_curcop);
4729 PL_curstash = stash;
4731 CopSTASHPV(PL_curcop) = stash ? HvNAME(stash) : Nullch;
4733 CopSTASH(PL_curcop) = stash;
4738 start_subparse(FALSE, 0),
4739 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4740 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4742 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4749 =for apidoc U||newXS
4751 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4757 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4760 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4763 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4765 /* just a cached method */
4769 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4770 /* already defined (or promised) */
4771 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4772 && HvNAME(GvSTASH(CvGV(cv)))
4773 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4774 line_t oldline = CopLINE(PL_curcop);
4775 if (PL_copline != NOLINE)
4776 CopLINE_set(PL_curcop, PL_copline);
4777 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4778 CopLINE_set(PL_curcop, oldline);
4785 if (cv) /* must reuse cv if autoloaded */
4788 cv = (CV*)NEWSV(1105,0);
4789 sv_upgrade((SV *)cv, SVt_PVCV);
4793 PL_sub_generation++;
4796 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4798 New(666, CvMUTEXP(cv), 1, perl_mutex);
4799 MUTEX_INIT(CvMUTEXP(cv));
4801 #endif /* USE_THREADS */
4802 (void)gv_fetchfile(filename);
4803 CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4804 an external constant string */
4805 CvXSUB(cv) = subaddr;
4808 char *s = strrchr(name,':');
4814 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4817 if (strEQ(s, "BEGIN")) {
4819 PL_beginav = newAV();
4820 av_push(PL_beginav, (SV*)cv);
4821 GvCV(gv) = 0; /* cv has been hijacked */
4823 else if (strEQ(s, "END")) {
4826 av_unshift(PL_endav, 1);
4827 av_store(PL_endav, 0, (SV*)cv);
4828 GvCV(gv) = 0; /* cv has been hijacked */
4830 else if (strEQ(s, "CHECK")) {
4832 PL_checkav = newAV();
4833 if (PL_main_start && ckWARN(WARN_VOID))
4834 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4835 av_unshift(PL_checkav, 1);
4836 av_store(PL_checkav, 0, (SV*)cv);
4837 GvCV(gv) = 0; /* cv has been hijacked */
4839 else if (strEQ(s, "INIT")) {
4841 PL_initav = newAV();
4842 if (PL_main_start && ckWARN(WARN_VOID))
4843 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4844 av_push(PL_initav, (SV*)cv);
4845 GvCV(gv) = 0; /* cv has been hijacked */
4856 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4866 name = SvPVx(cSVOPo->op_sv, n_a);
4869 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4871 if ((cv = GvFORM(gv))) {
4872 if (ckWARN(WARN_REDEFINE)) {
4873 line_t oldline = CopLINE(PL_curcop);
4875 CopLINE_set(PL_curcop, PL_copline);
4876 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4877 CopLINE_set(PL_curcop, oldline);
4883 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4884 CvFILE(cv) = CopFILE(PL_curcop);
4886 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4887 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4888 SvPADTMP_on(PL_curpad[ix]);
4891 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4892 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4893 OpREFCNT_set(CvROOT(cv), 1);
4894 CvSTART(cv) = LINKLIST(CvROOT(cv));
4895 CvROOT(cv)->op_next = 0;
4898 PL_copline = NOLINE;
4903 Perl_newANONLIST(pTHX_ OP *o)
4905 return newUNOP(OP_REFGEN, 0,
4906 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4910 Perl_newANONHASH(pTHX_ OP *o)
4912 return newUNOP(OP_REFGEN, 0,
4913 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4917 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4919 return newANONATTRSUB(floor, proto, Nullop, block);
4923 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4925 return newUNOP(OP_REFGEN, 0,
4926 newSVOP(OP_ANONCODE, 0,
4927 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4931 Perl_oopsAV(pTHX_ OP *o)
4933 switch (o->op_type) {
4935 o->op_type = OP_PADAV;
4936 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4937 return ref(o, OP_RV2AV);
4940 o->op_type = OP_RV2AV;
4941 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4946 if (ckWARN_d(WARN_INTERNAL))
4947 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4954 Perl_oopsHV(pTHX_ OP *o)
4958 switch (o->op_type) {
4961 o->op_type = OP_PADHV;
4962 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4963 return ref(o, OP_RV2HV);
4967 o->op_type = OP_RV2HV;
4968 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4973 if (ckWARN_d(WARN_INTERNAL))
4974 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4981 Perl_newAVREF(pTHX_ OP *o)
4983 if (o->op_type == OP_PADANY) {
4984 o->op_type = OP_PADAV;
4985 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4988 return newUNOP(OP_RV2AV, 0, scalar(o));
4992 Perl_newGVREF(pTHX_ I32 type, OP *o)
4994 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4995 return newUNOP(OP_NULL, 0, o);
4996 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
5000 Perl_newHVREF(pTHX_ OP *o)
5002 if (o->op_type == OP_PADANY) {
5003 o->op_type = OP_PADHV;
5004 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5007 return newUNOP(OP_RV2HV, 0, scalar(o));
5011 Perl_oopsCV(pTHX_ OP *o)
5013 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5019 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5021 return newUNOP(OP_RV2CV, flags, scalar(o));
5025 Perl_newSVREF(pTHX_ OP *o)
5027 if (o->op_type == OP_PADANY) {
5028 o->op_type = OP_PADSV;
5029 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5032 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5033 o->op_flags |= OPpDONE_SVREF;
5036 return newUNOP(OP_RV2SV, 0, scalar(o));
5039 /* Check routines. */
5042 Perl_ck_anoncode(pTHX_ OP *o)
5047 name = NEWSV(1106,0);
5048 sv_upgrade(name, SVt_PVNV);
5049 sv_setpvn(name, "&", 1);
5052 ix = pad_alloc(o->op_type, SVs_PADMY);
5053 av_store(PL_comppad_name, ix, name);
5054 av_store(PL_comppad, ix, cSVOPo->op_sv);
5055 SvPADMY_on(cSVOPo->op_sv);
5056 cSVOPo->op_sv = Nullsv;
5057 cSVOPo->op_targ = ix;
5062 Perl_ck_bitop(pTHX_ OP *o)
5064 o->op_private = PL_hints;
5069 Perl_ck_concat(pTHX_ OP *o)
5071 if (cUNOPo->op_first->op_type == OP_CONCAT)
5072 o->op_flags |= OPf_STACKED;
5077 Perl_ck_spair(pTHX_ OP *o)
5079 if (o->op_flags & OPf_KIDS) {
5082 OPCODE type = o->op_type;
5083 o = modkids(ck_fun(o), type);
5084 kid = cUNOPo->op_first;
5085 newop = kUNOP->op_first->op_sibling;
5087 (newop->op_sibling ||
5088 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
5089 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
5090 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
5094 op_free(kUNOP->op_first);
5095 kUNOP->op_first = newop;
5097 o->op_ppaddr = PL_ppaddr[++o->op_type];
5102 Perl_ck_delete(pTHX_ OP *o)
5106 if (o->op_flags & OPf_KIDS) {
5107 OP *kid = cUNOPo->op_first;
5108 switch (kid->op_type) {
5110 o->op_flags |= OPf_SPECIAL;
5113 o->op_private |= OPpSLICE;
5116 o->op_flags |= OPf_SPECIAL;
5121 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5122 PL_op_desc[o->op_type]);
5130 Perl_ck_eof(pTHX_ OP *o)
5132 I32 type = o->op_type;
5134 if (o->op_flags & OPf_KIDS) {
5135 if (cLISTOPo->op_first->op_type == OP_STUB) {
5137 o = newUNOP(type, OPf_SPECIAL,
5138 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
5146 Perl_ck_eval(pTHX_ OP *o)
5148 PL_hints |= HINT_BLOCK_SCOPE;
5149 if (o->op_flags & OPf_KIDS) {
5150 SVOP *kid = (SVOP*)cUNOPo->op_first;
5153 o->op_flags &= ~OPf_KIDS;
5156 else if (kid->op_type == OP_LINESEQ) {
5159 kid->op_next = o->op_next;
5160 cUNOPo->op_first = 0;
5163 NewOp(1101, enter, 1, LOGOP);
5164 enter->op_type = OP_ENTERTRY;
5165 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5166 enter->op_private = 0;
5168 /* establish postfix order */
5169 enter->op_next = (OP*)enter;
5171 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5172 o->op_type = OP_LEAVETRY;
5173 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5174 enter->op_other = o;
5182 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5184 o->op_targ = (PADOFFSET)PL_hints;
5189 Perl_ck_exit(pTHX_ OP *o)
5192 HV *table = GvHV(PL_hintgv);
5194 SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5195 if (svp && *svp && SvTRUE(*svp))
5196 o->op_private |= OPpEXIT_VMSISH;
5203 Perl_ck_exec(pTHX_ OP *o)
5206 if (o->op_flags & OPf_STACKED) {
5208 kid = cUNOPo->op_first->op_sibling;
5209 if (kid->op_type == OP_RV2GV)
5218 Perl_ck_exists(pTHX_ OP *o)
5221 if (o->op_flags & OPf_KIDS) {
5222 OP *kid = cUNOPo->op_first;
5223 if (kid->op_type == OP_ENTERSUB) {
5224 (void) ref(kid, o->op_type);
5225 if (kid->op_type != OP_RV2CV && !PL_error_count)
5226 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5227 PL_op_desc[o->op_type]);
5228 o->op_private |= OPpEXISTS_SUB;
5230 else if (kid->op_type == OP_AELEM)
5231 o->op_flags |= OPf_SPECIAL;
5232 else if (kid->op_type != OP_HELEM)
5233 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5234 PL_op_desc[o->op_type]);
5242 Perl_ck_gvconst(pTHX_ register OP *o)
5244 o = fold_constants(o);
5245 if (o->op_type == OP_CONST)
5252 Perl_ck_rvconst(pTHX_ register OP *o)
5255 SVOP *kid = (SVOP*)cUNOPo->op_first;
5257 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5258 if (kid->op_type == OP_CONST) {
5262 SV *kidsv = kid->op_sv;
5265 /* Is it a constant from cv_const_sv()? */
5266 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5267 SV *rsv = SvRV(kidsv);
5268 int svtype = SvTYPE(rsv);
5269 char *badtype = Nullch;
5271 switch (o->op_type) {
5273 if (svtype > SVt_PVMG)
5274 badtype = "a SCALAR";
5277 if (svtype != SVt_PVAV)
5278 badtype = "an ARRAY";
5281 if (svtype != SVt_PVHV) {
5282 if (svtype == SVt_PVAV) { /* pseudohash? */
5283 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5284 if (ksv && SvROK(*ksv)
5285 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5294 if (svtype != SVt_PVCV)
5299 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5302 name = SvPV(kidsv, n_a);
5303 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5304 char *badthing = Nullch;
5305 switch (o->op_type) {
5307 badthing = "a SCALAR";
5310 badthing = "an ARRAY";
5313 badthing = "a HASH";
5318 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5322 * This is a little tricky. We only want to add the symbol if we
5323 * didn't add it in the lexer. Otherwise we get duplicate strict
5324 * warnings. But if we didn't add it in the lexer, we must at
5325 * least pretend like we wanted to add it even if it existed before,
5326 * or we get possible typo warnings. OPpCONST_ENTERED says
5327 * whether the lexer already added THIS instance of this symbol.
5329 iscv = (o->op_type == OP_RV2CV) * 2;
5331 gv = gv_fetchpv(name,
5332 iscv | !(kid->op_private & OPpCONST_ENTERED),
5335 : o->op_type == OP_RV2SV
5337 : o->op_type == OP_RV2AV
5339 : o->op_type == OP_RV2HV
5342 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5344 kid->op_type = OP_GV;
5345 SvREFCNT_dec(kid->op_sv);
5347 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5348 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5349 SvREFCNT_dec(PL_curpad[kPADOP->op_padix]);
5351 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5353 kid->op_sv = SvREFCNT_inc(gv);
5355 kid->op_ppaddr = PL_ppaddr[OP_GV];
5362 Perl_ck_ftst(pTHX_ OP *o)
5365 I32 type = o->op_type;
5367 if (o->op_flags & OPf_REF) {
5370 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5371 SVOP *kid = (SVOP*)cUNOPo->op_first;
5373 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5375 OP *newop = newGVOP(type, OPf_REF,
5376 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5383 if (type == OP_FTTTY)
5384 o = newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5387 o = newUNOP(type, 0, newDEFSVOP());
5390 if (type == OP_FTTEXT || type == OP_FTBINARY) {
5392 if (PL_hints & HINT_LOCALE)
5393 o->op_private |= OPpLOCALE;
5400 Perl_ck_fun(pTHX_ OP *o)
5407 int type = o->op_type;
5408 register I32 oa = PL_opargs[type] >> OASHIFT;
5410 if (o->op_flags & OPf_STACKED) {
5411 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5414 return no_fh_allowed(o);
5417 if (o->op_flags & OPf_KIDS) {
5419 tokid = &cLISTOPo->op_first;
5420 kid = cLISTOPo->op_first;
5421 if (kid->op_type == OP_PUSHMARK ||
5422 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5424 tokid = &kid->op_sibling;
5425 kid = kid->op_sibling;
5427 if (!kid && PL_opargs[type] & OA_DEFGV)
5428 *tokid = kid = newDEFSVOP();
5432 sibl = kid->op_sibling;
5435 /* list seen where single (scalar) arg expected? */
5436 if (numargs == 1 && !(oa >> 4)
5437 && kid->op_type == OP_LIST && type != OP_SCALAR)
5439 return too_many_arguments(o,PL_op_desc[type]);
5452 if (kid->op_type == OP_CONST &&
5453 (kid->op_private & OPpCONST_BARE))
5455 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5456 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5457 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5458 if (ckWARN(WARN_DEPRECATED))
5459 Perl_warner(aTHX_ WARN_DEPRECATED,
5460 "Array @%s missing the @ in argument %"IVdf" of %s()",
5461 name, (IV)numargs, PL_op_desc[type]);
5464 kid->op_sibling = sibl;
5467 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5468 bad_type(numargs, "array", PL_op_desc[type], kid);
5472 if (kid->op_type == OP_CONST &&
5473 (kid->op_private & OPpCONST_BARE))
5475 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5476 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5477 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5478 if (ckWARN(WARN_DEPRECATED))
5479 Perl_warner(aTHX_ WARN_DEPRECATED,
5480 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5481 name, (IV)numargs, PL_op_desc[type]);
5484 kid->op_sibling = sibl;
5487 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5488 bad_type(numargs, "hash", PL_op_desc[type], kid);
5493 OP *newop = newUNOP(OP_NULL, 0, kid);
5494 kid->op_sibling = 0;
5496 newop->op_next = newop;
5498 kid->op_sibling = sibl;
5503 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5504 if (kid->op_type == OP_CONST &&
5505 (kid->op_private & OPpCONST_BARE))
5507 OP *newop = newGVOP(OP_GV, 0,
5508 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5513 else if (kid->op_type == OP_READLINE) {
5514 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5515 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5518 I32 flags = OPf_SPECIAL;
5522 /* is this op a FH constructor? */
5523 if (is_handle_constructor(o,numargs)) {
5524 char *name = Nullch;
5528 /* Set a flag to tell rv2gv to vivify
5529 * need to "prove" flag does not mean something
5530 * else already - NI-S 1999/05/07
5533 if (kid->op_type == OP_PADSV) {
5534 SV **namep = av_fetch(PL_comppad_name,
5536 if (namep && *namep)
5537 name = SvPV(*namep, len);
5539 else if (kid->op_type == OP_RV2SV
5540 && kUNOP->op_first->op_type == OP_GV)
5542 GV *gv = cGVOPx_gv(kUNOP->op_first);
5544 len = GvNAMELEN(gv);
5546 else if (kid->op_type == OP_AELEM
5547 || kid->op_type == OP_HELEM)
5549 name = "__ANONIO__";
5555 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5556 namesv = PL_curpad[targ];
5557 (void)SvUPGRADE(namesv, SVt_PV);
5559 sv_setpvn(namesv, "$", 1);
5560 sv_catpvn(namesv, name, len);
5563 kid->op_sibling = 0;
5564 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5565 kid->op_targ = targ;
5566 kid->op_private |= priv;
5568 kid->op_sibling = sibl;
5574 mod(scalar(kid), type);
5578 tokid = &kid->op_sibling;
5579 kid = kid->op_sibling;
5581 o->op_private |= numargs;
5583 return too_many_arguments(o,PL_op_desc[o->op_type]);
5586 else if (PL_opargs[type] & OA_DEFGV) {
5588 return newUNOP(type, 0, newDEFSVOP());
5592 while (oa & OA_OPTIONAL)
5594 if (oa && oa != OA_LIST)
5595 return too_few_arguments(o,PL_op_desc[o->op_type]);
5601 Perl_ck_glob(pTHX_ OP *o)
5606 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5607 append_elem(OP_GLOB, o, newDEFSVOP());
5609 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5610 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5612 #if !defined(PERL_EXTERNAL_GLOB)
5613 /* XXX this can be tightened up and made more failsafe. */
5616 Perl_load_module(aTHX_ 0, newSVpvn("File::Glob", 10), Nullsv,
5617 /* null-terminated import list */
5618 newSVpvn(":globally", 9), Nullsv);
5619 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5622 #endif /* PERL_EXTERNAL_GLOB */
5624 if (gv && GvIMPORTED_CV(gv)) {
5625 append_elem(OP_GLOB, o,
5626 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5627 o->op_type = OP_LIST;
5628 o->op_ppaddr = PL_ppaddr[OP_LIST];
5629 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5630 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5631 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5632 append_elem(OP_LIST, o,
5633 scalar(newUNOP(OP_RV2CV, 0,
5634 newGVOP(OP_GV, 0, gv)))));
5635 o = newUNOP(OP_NULL, 0, ck_subr(o));
5636 o->op_targ = OP_GLOB; /* hint at what it used to be */
5639 gv = newGVgen("main");
5641 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5647 Perl_ck_grep(pTHX_ OP *o)
5651 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5653 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5654 NewOp(1101, gwop, 1, LOGOP);
5656 if (o->op_flags & OPf_STACKED) {
5659 kid = cLISTOPo->op_first->op_sibling;
5660 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5663 kid->op_next = (OP*)gwop;
5664 o->op_flags &= ~OPf_STACKED;
5666 kid = cLISTOPo->op_first->op_sibling;
5667 if (type == OP_MAPWHILE)
5674 kid = cLISTOPo->op_first->op_sibling;
5675 if (kid->op_type != OP_NULL)
5676 Perl_croak(aTHX_ "panic: ck_grep");
5677 kid = kUNOP->op_first;
5679 gwop->op_type = type;
5680 gwop->op_ppaddr = PL_ppaddr[type];
5681 gwop->op_first = listkids(o);
5682 gwop->op_flags |= OPf_KIDS;
5683 gwop->op_private = 1;
5684 gwop->op_other = LINKLIST(kid);
5685 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5686 kid->op_next = (OP*)gwop;
5688 kid = cLISTOPo->op_first->op_sibling;
5689 if (!kid || !kid->op_sibling)
5690 return too_few_arguments(o,PL_op_desc[o->op_type]);
5691 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5692 mod(kid, OP_GREPSTART);
5698 Perl_ck_index(pTHX_ OP *o)
5700 if (o->op_flags & OPf_KIDS) {
5701 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5703 kid = kid->op_sibling; /* get past "big" */
5704 if (kid && kid->op_type == OP_CONST)
5705 fbm_compile(((SVOP*)kid)->op_sv, 0);
5711 Perl_ck_lengthconst(pTHX_ OP *o)
5713 /* XXX length optimization goes here */
5718 Perl_ck_lfun(pTHX_ OP *o)
5720 OPCODE type = o->op_type;
5721 return modkids(ck_fun(o), type);
5725 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5728 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5729 switch (cUNOPo->op_first->op_type) {
5731 break; /* Globals via GV can be undef */
5733 case OP_AASSIGN: /* Is this a good idea? */
5734 Perl_warner(aTHX_ WARN_DEPRECATED,
5735 "defined(@array) is deprecated");
5736 Perl_warner(aTHX_ WARN_DEPRECATED,
5737 "\t(Maybe you should just omit the defined()?)\n");
5740 break; /* Globals via GV can be undef */
5742 Perl_warner(aTHX_ WARN_DEPRECATED,
5743 "defined(%%hash) is deprecated");
5744 Perl_warner(aTHX_ WARN_DEPRECATED,
5745 "\t(Maybe you should just omit the defined()?)\n");
5756 Perl_ck_rfun(pTHX_ OP *o)
5758 OPCODE type = o->op_type;
5759 return refkids(ck_fun(o), type);
5763 Perl_ck_listiob(pTHX_ OP *o)
5767 kid = cLISTOPo->op_first;
5770 kid = cLISTOPo->op_first;
5772 if (kid->op_type == OP_PUSHMARK)
5773 kid = kid->op_sibling;
5774 if (kid && o->op_flags & OPf_STACKED)
5775 kid = kid->op_sibling;
5776 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5777 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5778 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5779 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5780 cLISTOPo->op_first->op_sibling = kid;
5781 cLISTOPo->op_last = kid;
5782 kid = kid->op_sibling;
5787 append_elem(o->op_type, o, newDEFSVOP());
5793 if (PL_hints & HINT_LOCALE)
5794 o->op_private |= OPpLOCALE;
5801 Perl_ck_fun_locale(pTHX_ OP *o)
5807 if (PL_hints & HINT_LOCALE)
5808 o->op_private |= OPpLOCALE;
5815 Perl_ck_sassign(pTHX_ OP *o)
5817 OP *kid = cLISTOPo->op_first;
5818 /* has a disposable target? */
5819 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5820 && !(kid->op_flags & OPf_STACKED)
5821 /* Cannot steal the second time! */
5822 && !(kid->op_private & OPpTARGET_MY))
5824 OP *kkid = kid->op_sibling;
5826 /* Can just relocate the target. */
5827 if (kkid && kkid->op_type == OP_PADSV
5828 && !(kkid->op_private & OPpLVAL_INTRO))
5830 kid->op_targ = kkid->op_targ;
5832 /* Now we do not need PADSV and SASSIGN. */
5833 kid->op_sibling = o->op_sibling; /* NULL */
5834 cLISTOPo->op_first = NULL;
5837 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5845 Perl_ck_scmp(pTHX_ OP *o)
5849 if (PL_hints & HINT_LOCALE)
5850 o->op_private |= OPpLOCALE;
5857 Perl_ck_match(pTHX_ OP *o)
5859 o->op_private |= OPpRUNTIME;
5864 Perl_ck_method(pTHX_ OP *o)
5866 OP *kid = cUNOPo->op_first;
5867 if (kid->op_type == OP_CONST) {
5868 SV* sv = kSVOP->op_sv;
5869 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5871 (void)SvUPGRADE(sv, SVt_PVIV);
5873 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5874 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5875 kSVOP->op_sv = Nullsv;
5884 Perl_ck_null(pTHX_ OP *o)
5890 Perl_ck_open(pTHX_ OP *o)
5892 HV *table = GvHV(PL_hintgv);
5896 svp = hv_fetch(table, "open_IN", 7, FALSE);
5898 mode = mode_from_discipline(*svp);
5899 if (mode & O_BINARY)
5900 o->op_private |= OPpOPEN_IN_RAW;
5901 else if (mode & O_TEXT)
5902 o->op_private |= OPpOPEN_IN_CRLF;
5905 svp = hv_fetch(table, "open_OUT", 8, FALSE);
5907 mode = mode_from_discipline(*svp);
5908 if (mode & O_BINARY)
5909 o->op_private |= OPpOPEN_OUT_RAW;
5910 else if (mode & O_TEXT)
5911 o->op_private |= OPpOPEN_OUT_CRLF;
5914 if (o->op_type == OP_BACKTICK)
5920 Perl_ck_repeat(pTHX_ OP *o)
5922 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5923 o->op_private |= OPpREPEAT_DOLIST;
5924 cBINOPo->op_first = force_list(cBINOPo->op_first);
5932 Perl_ck_require(pTHX_ OP *o)
5934 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5935 SVOP *kid = (SVOP*)cUNOPo->op_first;
5937 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5939 for (s = SvPVX(kid->op_sv); *s; s++) {
5940 if (*s == ':' && s[1] == ':') {
5942 Move(s+2, s+1, strlen(s+2)+1, char);
5943 --SvCUR(kid->op_sv);
5946 if (SvREADONLY(kid->op_sv)) {
5947 SvREADONLY_off(kid->op_sv);
5948 sv_catpvn(kid->op_sv, ".pm", 3);
5949 SvREADONLY_on(kid->op_sv);
5952 sv_catpvn(kid->op_sv, ".pm", 3);
5960 Perl_ck_retarget(pTHX_ OP *o)
5962 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5969 Perl_ck_select(pTHX_ OP *o)
5972 if (o->op_flags & OPf_KIDS) {
5973 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5974 if (kid && kid->op_sibling) {
5975 o->op_type = OP_SSELECT;
5976 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5978 return fold_constants(o);
5982 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5983 if (kid && kid->op_type == OP_RV2GV)
5984 kid->op_private &= ~HINT_STRICT_REFS;
5989 Perl_ck_shift(pTHX_ OP *o)
5991 I32 type = o->op_type;
5993 if (!(o->op_flags & OPf_KIDS)) {
5998 if (!CvUNIQUE(PL_compcv)) {
5999 argop = newOP(OP_PADAV, OPf_REF);
6000 argop->op_targ = 0; /* PL_curpad[0] is @_ */
6003 argop = newUNOP(OP_RV2AV, 0,
6004 scalar(newGVOP(OP_GV, 0,
6005 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6008 argop = newUNOP(OP_RV2AV, 0,
6009 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
6010 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6011 #endif /* USE_THREADS */
6012 return newUNOP(type, 0, scalar(argop));
6014 return scalar(modkids(ck_fun(o), type));
6018 Perl_ck_sort(pTHX_ OP *o)
6023 if (PL_hints & HINT_LOCALE)
6024 o->op_private |= OPpLOCALE;
6027 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6029 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6030 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6032 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6034 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6036 if (kid->op_type == OP_SCOPE) {
6040 else if (kid->op_type == OP_LEAVE) {
6041 if (o->op_type == OP_SORT) {
6042 null(kid); /* wipe out leave */
6045 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6046 if (k->op_next == kid)
6048 /* don't descend into loops */
6049 else if (k->op_type == OP_ENTERLOOP
6050 || k->op_type == OP_ENTERITER)
6052 k = cLOOPx(k)->op_lastop;
6057 kid->op_next = 0; /* just disconnect the leave */
6058 k = kLISTOP->op_first;
6063 if (o->op_type == OP_SORT) {
6064 /* provide scalar context for comparison function/block */
6070 o->op_flags |= OPf_SPECIAL;
6072 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6075 firstkid = firstkid->op_sibling;
6078 /* provide list context for arguments */
6079 if (o->op_type == OP_SORT)
6086 S_simplify_sort(pTHX_ OP *o)
6089 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6093 if (!(o->op_flags & OPf_STACKED))
6095 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6096 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6097 kid = kUNOP->op_first; /* get past null */
6098 if (kid->op_type != OP_SCOPE)
6100 kid = kLISTOP->op_last; /* get past scope */
6101 switch(kid->op_type) {
6109 k = kid; /* remember this node*/
6110 if (kBINOP->op_first->op_type != OP_RV2SV)
6112 kid = kBINOP->op_first; /* get past cmp */
6113 if (kUNOP->op_first->op_type != OP_GV)
6115 kid = kUNOP->op_first; /* get past rv2sv */
6117 if (GvSTASH(gv) != PL_curstash)
6119 if (strEQ(GvNAME(gv), "a"))
6121 else if (strEQ(GvNAME(gv), "b"))
6125 kid = k; /* back to cmp */
6126 if (kBINOP->op_last->op_type != OP_RV2SV)
6128 kid = kBINOP->op_last; /* down to 2nd arg */
6129 if (kUNOP->op_first->op_type != OP_GV)
6131 kid = kUNOP->op_first; /* get past rv2sv */
6133 if (GvSTASH(gv) != PL_curstash
6135 ? strNE(GvNAME(gv), "a")
6136 : strNE(GvNAME(gv), "b")))
6138 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6140 o->op_private |= OPpSORT_REVERSE;
6141 if (k->op_type == OP_NCMP)
6142 o->op_private |= OPpSORT_NUMERIC;
6143 if (k->op_type == OP_I_NCMP)
6144 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6145 kid = cLISTOPo->op_first->op_sibling;
6146 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6147 op_free(kid); /* then delete it */
6148 cLISTOPo->op_children--;
6152 Perl_ck_split(pTHX_ OP *o)
6156 if (o->op_flags & OPf_STACKED)
6157 return no_fh_allowed(o);
6159 kid = cLISTOPo->op_first;
6160 if (kid->op_type != OP_NULL)
6161 Perl_croak(aTHX_ "panic: ck_split");
6162 kid = kid->op_sibling;
6163 op_free(cLISTOPo->op_first);
6164 cLISTOPo->op_first = kid;
6166 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6167 cLISTOPo->op_last = kid; /* There was only one element previously */
6170 if (kid->op_type != OP_MATCH) {
6171 OP *sibl = kid->op_sibling;
6172 kid->op_sibling = 0;
6173 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6174 if (cLISTOPo->op_first == cLISTOPo->op_last)
6175 cLISTOPo->op_last = kid;
6176 cLISTOPo->op_first = kid;
6177 kid->op_sibling = sibl;
6180 kid->op_type = OP_PUSHRE;
6181 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6184 if (!kid->op_sibling)
6185 append_elem(OP_SPLIT, o, newDEFSVOP());
6187 kid = kid->op_sibling;
6190 if (!kid->op_sibling)
6191 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6193 kid = kid->op_sibling;
6196 if (kid->op_sibling)
6197 return too_many_arguments(o,PL_op_desc[o->op_type]);
6203 Perl_ck_join(pTHX_ OP *o)
6205 if (ckWARN(WARN_SYNTAX)) {
6206 OP *kid = cLISTOPo->op_first->op_sibling;
6207 if (kid && kid->op_type == OP_MATCH) {
6208 char *pmstr = "STRING";
6209 if (kPMOP->op_pmregexp)
6210 pmstr = kPMOP->op_pmregexp->precomp;
6211 Perl_warner(aTHX_ WARN_SYNTAX,
6212 "/%s/ should probably be written as \"%s\"",
6220 Perl_ck_subr(pTHX_ OP *o)
6223 OP *prev = ((cUNOPo->op_first->op_sibling)
6224 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6225 OP *o2 = prev->op_sibling;
6234 o->op_private |= OPpENTERSUB_HASTARG;
6235 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6236 if (cvop->op_type == OP_RV2CV) {
6238 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6239 null(cvop); /* disable rv2cv */
6240 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6241 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6242 GV *gv = cGVOPx_gv(tmpop);
6245 tmpop->op_private |= OPpEARLY_CV;
6246 else if (SvPOK(cv)) {
6247 namegv = CvANON(cv) ? gv : CvGV(cv);
6248 proto = SvPV((SV*)cv, n_a);
6252 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6253 if (o2->op_type == OP_CONST)
6254 o2->op_private &= ~OPpCONST_STRICT;
6255 else if (o2->op_type == OP_LIST) {
6256 OP *o = ((UNOP*)o2)->op_first->op_sibling;
6257 if (o && o->op_type == OP_CONST)
6258 o->op_private &= ~OPpCONST_STRICT;
6261 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6262 if (PERLDB_SUB && PL_curstash != PL_debstash)
6263 o->op_private |= OPpENTERSUB_DB;
6264 while (o2 != cvop) {
6268 return too_many_arguments(o, gv_ename(namegv));
6286 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6288 arg == 1 ? "block or sub {}" : "sub {}",
6289 gv_ename(namegv), o2);
6292 /* '*' allows any scalar type, including bareword */
6295 if (o2->op_type == OP_RV2GV)
6296 goto wrapref; /* autoconvert GLOB -> GLOBref */
6297 else if (o2->op_type == OP_CONST)
6298 o2->op_private &= ~OPpCONST_STRICT;
6299 else if (o2->op_type == OP_ENTERSUB) {
6300 /* accidental subroutine, revert to bareword */
6301 OP *gvop = ((UNOP*)o2)->op_first;
6302 if (gvop && gvop->op_type == OP_NULL) {
6303 gvop = ((UNOP*)gvop)->op_first;
6305 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6308 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6309 (gvop = ((UNOP*)gvop)->op_first) &&
6310 gvop->op_type == OP_GV)
6312 GV *gv = cGVOPx_gv(gvop);
6313 OP *sibling = o2->op_sibling;
6314 SV *n = newSVpvn("",0);
6316 gv_fullname3(n, gv, "");
6317 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
6318 sv_chop(n, SvPVX(n)+6);
6319 o2 = newSVOP(OP_CONST, 0, n);
6320 prev->op_sibling = o2;
6321 o2->op_sibling = sibling;
6333 if (o2->op_type != OP_RV2GV)
6334 bad_type(arg, "symbol", gv_ename(namegv), o2);
6337 if (o2->op_type != OP_ENTERSUB)
6338 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6341 if (o2->op_type != OP_RV2SV
6342 && o2->op_type != OP_PADSV
6343 && o2->op_type != OP_HELEM
6344 && o2->op_type != OP_AELEM
6345 && o2->op_type != OP_THREADSV)
6347 bad_type(arg, "scalar", gv_ename(namegv), o2);
6351 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
6352 bad_type(arg, "array", gv_ename(namegv), o2);
6355 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
6356 bad_type(arg, "hash", gv_ename(namegv), o2);
6360 OP* sib = kid->op_sibling;
6361 kid->op_sibling = 0;
6362 o2 = newUNOP(OP_REFGEN, 0, kid);
6363 o2->op_sibling = sib;
6364 prev->op_sibling = o2;
6375 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
6376 gv_ename(namegv), SvPV((SV*)cv, n_a));
6381 mod(o2, OP_ENTERSUB);
6383 o2 = o2->op_sibling;
6385 if (proto && !optional &&
6386 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6387 return too_few_arguments(o, gv_ename(namegv));
6392 Perl_ck_svconst(pTHX_ OP *o)
6394 SvREADONLY_on(cSVOPo->op_sv);
6399 Perl_ck_trunc(pTHX_ OP *o)
6401 if (o->op_flags & OPf_KIDS) {
6402 SVOP *kid = (SVOP*)cUNOPo->op_first;
6404 if (kid->op_type == OP_NULL)
6405 kid = (SVOP*)kid->op_sibling;
6406 if (kid && kid->op_type == OP_CONST &&
6407 (kid->op_private & OPpCONST_BARE))
6409 o->op_flags |= OPf_SPECIAL;
6410 kid->op_private &= ~OPpCONST_STRICT;
6416 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6419 Perl_peep(pTHX_ register OP *o)
6422 register OP* oldop = 0;
6424 OP *last_composite = Nullop;
6426 if (!o || o->op_seq)
6430 SAVEVPTR(PL_curcop);
6431 for (; o; o = o->op_next) {
6437 switch (o->op_type) {
6441 PL_curcop = ((COP*)o); /* for warnings */
6442 o->op_seq = PL_op_seqmax++;
6443 last_composite = Nullop;
6447 if (cSVOPo->op_private & OPpCONST_STRICT)
6448 no_bareword_allowed(o);
6450 /* Relocate sv to the pad for thread safety.
6451 * Despite being a "constant", the SV is written to,
6452 * for reference counts, sv_upgrade() etc. */
6454 PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6455 if (SvPADTMP(cSVOPo->op_sv)) {
6456 /* If op_sv is already a PADTMP then it is being used by
6457 * another pad, so make a copy. */
6458 sv_setsv(PL_curpad[ix],cSVOPo->op_sv);
6459 SvREADONLY_on(PL_curpad[ix]);
6460 SvREFCNT_dec(cSVOPo->op_sv);
6463 SvREFCNT_dec(PL_curpad[ix]);
6464 SvPADTMP_on(cSVOPo->op_sv);
6465 PL_curpad[ix] = cSVOPo->op_sv;
6467 cSVOPo->op_sv = Nullsv;
6471 o->op_seq = PL_op_seqmax++;
6475 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6476 if (o->op_next->op_private & OPpTARGET_MY) {
6477 if (o->op_flags & OPf_STACKED) /* chained concats */
6478 goto ignore_optimization;
6480 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6481 o->op_targ = o->op_next->op_targ;
6482 o->op_next->op_targ = 0;
6483 o->op_private |= OPpTARGET_MY;
6488 ignore_optimization:
6489 o->op_seq = PL_op_seqmax++;
6492 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6493 o->op_seq = PL_op_seqmax++;
6494 break; /* Scalar stub must produce undef. List stub is noop */
6498 if (o->op_targ == OP_NEXTSTATE
6499 || o->op_targ == OP_DBSTATE
6500 || o->op_targ == OP_SETSTATE)
6502 PL_curcop = ((COP*)o);
6509 if (oldop && o->op_next) {
6510 oldop->op_next = o->op_next;
6513 o->op_seq = PL_op_seqmax++;
6517 if (o->op_next->op_type == OP_RV2SV) {
6518 if (!(o->op_next->op_private & OPpDEREF)) {
6520 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6522 o->op_next = o->op_next->op_next;
6523 o->op_type = OP_GVSV;
6524 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6527 else if (o->op_next->op_type == OP_RV2AV) {
6528 OP* pop = o->op_next->op_next;
6530 if (pop->op_type == OP_CONST &&
6531 (PL_op = pop->op_next) &&
6532 pop->op_next->op_type == OP_AELEM &&
6533 !(pop->op_next->op_private &
6534 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6535 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6543 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6544 o->op_next = pop->op_next->op_next;
6545 o->op_type = OP_AELEMFAST;
6546 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6547 o->op_private = (U8)i;
6552 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6554 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6555 /* XXX could check prototype here instead of just carping */
6556 SV *sv = sv_newmortal();
6557 gv_efullname3(sv, gv, Nullch);
6558 Perl_warner(aTHX_ WARN_PROTOTYPE,
6559 "%s() called too early to check prototype",
6564 o->op_seq = PL_op_seqmax++;
6575 o->op_seq = PL_op_seqmax++;
6576 while (cLOGOP->op_other->op_type == OP_NULL)
6577 cLOGOP->op_other = cLOGOP->op_other->op_next;
6578 peep(cLOGOP->op_other);
6582 o->op_seq = PL_op_seqmax++;
6583 peep(cLOOP->op_redoop);
6584 peep(cLOOP->op_nextop);
6585 peep(cLOOP->op_lastop);
6591 o->op_seq = PL_op_seqmax++;
6592 peep(cPMOP->op_pmreplstart);
6596 o->op_seq = PL_op_seqmax++;
6597 if (ckWARN(WARN_SYNTAX) && o->op_next
6598 && o->op_next->op_type == OP_NEXTSTATE) {
6599 if (o->op_next->op_sibling &&
6600 o->op_next->op_sibling->op_type != OP_EXIT &&
6601 o->op_next->op_sibling->op_type != OP_WARN &&
6602 o->op_next->op_sibling->op_type != OP_DIE) {
6603 line_t oldline = CopLINE(PL_curcop);
6605 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6606 Perl_warner(aTHX_ WARN_EXEC,
6607 "Statement unlikely to be reached");
6608 Perl_warner(aTHX_ WARN_EXEC,
6609 "\t(Maybe you meant system() when you said exec()?)\n");
6610 CopLINE_set(PL_curcop, oldline);
6619 SV **svp, **indsvp, *sv;
6624 o->op_seq = PL_op_seqmax++;
6625 if ((o->op_private & (OPpLVAL_INTRO))
6626 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6628 rop = (UNOP*)((BINOP*)o)->op_first;
6629 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6631 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6632 if (!SvOBJECT(lexname))
6634 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6635 if (!fields || !GvHV(*fields))
6637 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6638 key = SvPV(*svp, keylen);
6639 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6641 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6642 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6644 ind = SvIV(*indsvp);
6646 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6647 rop->op_type = OP_RV2AV;
6648 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6649 o->op_type = OP_AELEM;
6650 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6652 if (SvREADONLY(*svp))
6654 SvFLAGS(sv) |= (SvFLAGS(*svp)
6655 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6665 SV **svp, **indsvp, *sv;
6669 SVOP *first_key_op, *key_op;
6671 o->op_seq = PL_op_seqmax++;
6672 if ((o->op_private & (OPpLVAL_INTRO))
6673 /* I bet there's always a pushmark... */
6674 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6675 /* hmmm, no optimization if list contains only one key. */
6677 rop = (UNOP*)((LISTOP*)o)->op_last;
6678 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6680 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6681 if (!SvOBJECT(lexname))
6683 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6684 if (!fields || !GvHV(*fields))
6686 /* Again guessing that the pushmark can be jumped over.... */
6687 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6688 ->op_first->op_sibling;
6689 /* Check that the key list contains only constants. */
6690 for (key_op = first_key_op; key_op;
6691 key_op = (SVOP*)key_op->op_sibling)
6692 if (key_op->op_type != OP_CONST)
6696 rop->op_type = OP_RV2AV;
6697 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6698 o->op_type = OP_ASLICE;
6699 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6700 for (key_op = first_key_op; key_op;
6701 key_op = (SVOP*)key_op->op_sibling) {
6702 svp = cSVOPx_svp(key_op);
6703 key = SvPV(*svp, keylen);
6704 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6706 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6707 "in variable %s of type %s",
6708 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6710 ind = SvIV(*indsvp);
6712 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6714 if (SvREADONLY(*svp))
6716 SvFLAGS(sv) |= (SvFLAGS(*svp)
6717 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6726 if (!(o->op_flags & OPf_WANT)
6727 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6731 o->op_seq = PL_op_seqmax++;
6735 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6736 o->op_seq = PL_op_seqmax++;
6742 if (last_composite) {
6743 OP *r = last_composite;
6745 while (r->op_sibling)
6748 || (r->op_next->op_type == OP_LIST
6749 && r->op_next->op_next == o))
6751 if (last_composite->op_type == OP_RV2AV)
6752 yyerror("Lvalue subs returning arrays not implemented yet");
6754 yyerror("Lvalue subs returning hashes not implemented yet");
6761 o->op_seq = PL_op_seqmax++;