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 cv_ckproto(cv, gv, ps);
4435 /* already defined (or promised)? */
4436 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4438 bool const_changed = TRUE;
4439 if (!block && !attrs) {
4440 /* just a "sub foo;" when &foo is already defined */
4441 SAVEFREESV(PL_compcv);
4444 /* ahem, death to those who redefine active sort subs */
4445 if (PL_curstackinfo->si_type == PERLSI_SORT && PL_sortcop == CvSTART(cv))
4446 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s", name);
4449 if ((const_sv = cv_const_sv(cv)))
4450 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
4451 if ((const_sv || const_changed) && ckWARN(WARN_REDEFINE))
4453 line_t oldline = CopLINE(PL_curcop);
4454 CopLINE_set(PL_curcop, PL_copline);
4455 Perl_warner(aTHX_ WARN_REDEFINE,
4456 const_sv ? "Constant subroutine %s redefined"
4457 : "Subroutine %s redefined", name);
4458 CopLINE_set(PL_curcop, oldline);
4469 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4470 * before we clobber PL_compcv.
4474 if (CvGV(cv) && GvSTASH(CvGV(cv)) && HvNAME(GvSTASH(CvGV(cv))))
4475 stash = GvSTASH(CvGV(cv));
4476 else if (CvSTASH(cv) && HvNAME(CvSTASH(cv)))
4477 stash = CvSTASH(cv);
4479 stash = PL_curstash;
4482 /* possibly about to re-define existing subr -- ignore old cv */
4483 rcv = (SV*)PL_compcv;
4484 if (name && GvSTASH(gv) && HvNAME(GvSTASH(gv)))
4485 stash = GvSTASH(gv);
4487 stash = PL_curstash;
4489 apply_attrs(stash, rcv, attrs);
4491 if (cv) { /* must reuse cv if autoloaded */
4493 /* got here with just attrs -- work done, so bug out */
4494 SAVEFREESV(PL_compcv);
4498 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4499 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4500 CvOUTSIDE(PL_compcv) = 0;
4501 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4502 CvPADLIST(PL_compcv) = 0;
4503 if (SvREFCNT(PL_compcv) > 1) /* XXX Make closures transit through stub. */
4504 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc((SV*)cv);
4505 SvREFCNT_dec(PL_compcv);
4512 PL_sub_generation++;
4515 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4516 CvFILE(cv) = CopFILE(PL_curcop);
4517 CvSTASH(cv) = PL_curstash;
4520 if (!CvMUTEXP(cv)) {
4521 New(666, CvMUTEXP(cv), 1, perl_mutex);
4522 MUTEX_INIT(CvMUTEXP(cv));
4524 #endif /* USE_THREADS */
4527 sv_setpv((SV*)cv, ps);
4529 if (PL_error_count) {
4533 char *s = strrchr(name, ':');
4535 if (strEQ(s, "BEGIN")) {
4537 "BEGIN not safe after errors--compilation aborted";
4538 if (PL_in_eval & EVAL_KEEPERR)
4539 Perl_croak(aTHX_ not_safe);
4541 /* force display of errors found but not reported */
4542 sv_catpv(ERRSV, not_safe);
4543 Perl_croak(aTHX_ "%s", SvPVx(ERRSV, n_a));
4550 PL_copline = NOLINE;
4555 if (AvFILLp(PL_comppad_name) < AvFILLp(PL_comppad))
4556 av_store(PL_comppad_name, AvFILLp(PL_comppad), Nullsv);
4559 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0, scalarseq(block));
4562 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4564 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4565 OpREFCNT_set(CvROOT(cv), 1);
4566 CvSTART(cv) = LINKLIST(CvROOT(cv));
4567 CvROOT(cv)->op_next = 0;
4570 /* now that optimizer has done its work, adjust pad values */
4572 SV **namep = AvARRAY(PL_comppad_name);
4573 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4576 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4579 * The only things that a clonable function needs in its
4580 * pad are references to outer lexicals and anonymous subs.
4581 * The rest are created anew during cloning.
4583 if (!((namesv = namep[ix]) != Nullsv &&
4584 namesv != &PL_sv_undef &&
4586 *SvPVX(namesv) == '&')))
4588 SvREFCNT_dec(PL_curpad[ix]);
4589 PL_curpad[ix] = Nullsv;
4594 AV *av = newAV(); /* Will be @_ */
4596 av_store(PL_comppad, 0, (SV*)av);
4597 AvFLAGS(av) = AVf_REIFY;
4599 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4600 if (SvIMMORTAL(PL_curpad[ix]) || IS_PADGV(PL_curpad[ix]) || IS_PADCONST(PL_curpad[ix]))
4602 if (!SvPADMY(PL_curpad[ix]))
4603 SvPADTMP_on(PL_curpad[ix]);
4607 if (name || aname) {
4609 char *tname = (name ? name : aname);
4611 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4612 SV *sv = NEWSV(0,0);
4613 SV *tmpstr = sv_newmortal();
4614 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4618 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4620 (long)PL_subline, (long)CopLINE(PL_curcop));
4621 gv_efullname3(tmpstr, gv, Nullch);
4622 hv_store(GvHV(PL_DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
4623 hv = GvHVn(db_postponed);
4624 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
4625 && (pcv = GvCV(db_postponed)))
4631 call_sv((SV*)pcv, G_DISCARD);
4635 if ((s = strrchr(tname,':')))
4640 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4643 if (strEQ(s, "BEGIN")) {
4644 I32 oldscope = PL_scopestack_ix;
4646 SAVECOPFILE(&PL_compiling);
4647 SAVECOPLINE(&PL_compiling);
4649 sv_setsv(PL_rs, PL_nrs);
4652 PL_beginav = newAV();
4653 DEBUG_x( dump_sub(gv) );
4654 av_push(PL_beginav, (SV*)cv);
4655 GvCV(gv) = 0; /* cv has been hijacked */
4656 call_list(oldscope, PL_beginav);
4658 PL_curcop = &PL_compiling;
4659 PL_compiling.op_private = PL_hints;
4662 else if (strEQ(s, "END") && !PL_error_count) {
4665 DEBUG_x( dump_sub(gv) );
4666 av_unshift(PL_endav, 1);
4667 av_store(PL_endav, 0, (SV*)cv);
4668 GvCV(gv) = 0; /* cv has been hijacked */
4670 else if (strEQ(s, "CHECK") && !PL_error_count) {
4672 PL_checkav = newAV();
4673 DEBUG_x( dump_sub(gv) );
4674 if (PL_main_start && ckWARN(WARN_VOID))
4675 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4676 av_unshift(PL_checkav, 1);
4677 av_store(PL_checkav, 0, (SV*)cv);
4678 GvCV(gv) = 0; /* cv has been hijacked */
4680 else if (strEQ(s, "INIT") && !PL_error_count) {
4682 PL_initav = newAV();
4683 DEBUG_x( dump_sub(gv) );
4684 if (PL_main_start && ckWARN(WARN_VOID))
4685 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4686 av_push(PL_initav, (SV*)cv);
4687 GvCV(gv) = 0; /* cv has been hijacked */
4692 PL_copline = NOLINE;
4697 /* XXX unsafe for threads if eval_owner isn't held */
4699 =for apidoc newCONSTSUB
4701 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4702 eligible for inlining at compile-time.
4708 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4714 SAVECOPLINE(PL_curcop);
4715 CopLINE_set(PL_curcop, PL_copline);
4718 PL_hints &= ~HINT_BLOCK_SCOPE;
4721 SAVESPTR(PL_curstash);
4722 SAVECOPSTASH(PL_curcop);
4723 PL_curstash = stash;
4725 CopSTASHPV(PL_curcop) = stash ? HvNAME(stash) : Nullch;
4727 CopSTASH(PL_curcop) = stash;
4732 start_subparse(FALSE, 0),
4733 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4734 newSVOP(OP_CONST, 0, &PL_sv_no), /* SvPV(&PL_sv_no) == "" -- GMB */
4736 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
4743 =for apidoc U||newXS
4745 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4751 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4754 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
4757 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4759 /* just a cached method */
4763 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4764 /* already defined (or promised) */
4765 if (ckWARN(WARN_REDEFINE) && !(CvGV(cv) && GvSTASH(CvGV(cv))
4766 && HvNAME(GvSTASH(CvGV(cv)))
4767 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
4768 line_t oldline = CopLINE(PL_curcop);
4769 if (PL_copline != NOLINE)
4770 CopLINE_set(PL_curcop, PL_copline);
4771 Perl_warner(aTHX_ WARN_REDEFINE, "Subroutine %s redefined",name);
4772 CopLINE_set(PL_curcop, oldline);
4779 if (cv) /* must reuse cv if autoloaded */
4782 cv = (CV*)NEWSV(1105,0);
4783 sv_upgrade((SV *)cv, SVt_PVCV);
4787 PL_sub_generation++;
4790 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4792 New(666, CvMUTEXP(cv), 1, perl_mutex);
4793 MUTEX_INIT(CvMUTEXP(cv));
4795 #endif /* USE_THREADS */
4796 (void)gv_fetchfile(filename);
4797 CvFILE(cv) = filename; /* NOTE: not copied, as it is expected to be
4798 an external constant string */
4799 CvXSUB(cv) = subaddr;
4802 char *s = strrchr(name,':');
4808 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4811 if (strEQ(s, "BEGIN")) {
4813 PL_beginav = newAV();
4814 av_push(PL_beginav, (SV*)cv);
4815 GvCV(gv) = 0; /* cv has been hijacked */
4817 else if (strEQ(s, "END")) {
4820 av_unshift(PL_endav, 1);
4821 av_store(PL_endav, 0, (SV*)cv);
4822 GvCV(gv) = 0; /* cv has been hijacked */
4824 else if (strEQ(s, "CHECK")) {
4826 PL_checkav = newAV();
4827 if (PL_main_start && ckWARN(WARN_VOID))
4828 Perl_warner(aTHX_ WARN_VOID, "Too late to run CHECK block");
4829 av_unshift(PL_checkav, 1);
4830 av_store(PL_checkav, 0, (SV*)cv);
4831 GvCV(gv) = 0; /* cv has been hijacked */
4833 else if (strEQ(s, "INIT")) {
4835 PL_initav = newAV();
4836 if (PL_main_start && ckWARN(WARN_VOID))
4837 Perl_warner(aTHX_ WARN_VOID, "Too late to run INIT block");
4838 av_push(PL_initav, (SV*)cv);
4839 GvCV(gv) = 0; /* cv has been hijacked */
4850 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4860 name = SvPVx(cSVOPo->op_sv, n_a);
4863 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
4865 if ((cv = GvFORM(gv))) {
4866 if (ckWARN(WARN_REDEFINE)) {
4867 line_t oldline = CopLINE(PL_curcop);
4869 CopLINE_set(PL_curcop, PL_copline);
4870 Perl_warner(aTHX_ WARN_REDEFINE, "Format %s redefined",name);
4871 CopLINE_set(PL_curcop, oldline);
4877 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
4878 CvFILE(cv) = CopFILE(PL_curcop);
4880 for (ix = AvFILLp(PL_comppad); ix > 0; ix--) {
4881 if (!SvPADMY(PL_curpad[ix]) && !SvIMMORTAL(PL_curpad[ix]))
4882 SvPADTMP_on(PL_curpad[ix]);
4885 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4886 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4887 OpREFCNT_set(CvROOT(cv), 1);
4888 CvSTART(cv) = LINKLIST(CvROOT(cv));
4889 CvROOT(cv)->op_next = 0;
4892 PL_copline = NOLINE;
4897 Perl_newANONLIST(pTHX_ OP *o)
4899 return newUNOP(OP_REFGEN, 0,
4900 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
4904 Perl_newANONHASH(pTHX_ OP *o)
4906 return newUNOP(OP_REFGEN, 0,
4907 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
4911 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4913 return newANONATTRSUB(floor, proto, Nullop, block);
4917 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4919 return newUNOP(OP_REFGEN, 0,
4920 newSVOP(OP_ANONCODE, 0,
4921 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4925 Perl_oopsAV(pTHX_ OP *o)
4927 switch (o->op_type) {
4929 o->op_type = OP_PADAV;
4930 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4931 return ref(o, OP_RV2AV);
4934 o->op_type = OP_RV2AV;
4935 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4940 if (ckWARN_d(WARN_INTERNAL))
4941 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsAV");
4948 Perl_oopsHV(pTHX_ OP *o)
4952 switch (o->op_type) {
4955 o->op_type = OP_PADHV;
4956 o->op_ppaddr = PL_ppaddr[OP_PADHV];
4957 return ref(o, OP_RV2HV);
4961 o->op_type = OP_RV2HV;
4962 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
4967 if (ckWARN_d(WARN_INTERNAL))
4968 Perl_warner(aTHX_ WARN_INTERNAL, "oops: oopsHV");
4975 Perl_newAVREF(pTHX_ OP *o)
4977 if (o->op_type == OP_PADANY) {
4978 o->op_type = OP_PADAV;
4979 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4982 return newUNOP(OP_RV2AV, 0, scalar(o));
4986 Perl_newGVREF(pTHX_ I32 type, OP *o)
4988 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
4989 return newUNOP(OP_NULL, 0, o);
4990 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
4994 Perl_newHVREF(pTHX_ OP *o)
4996 if (o->op_type == OP_PADANY) {
4997 o->op_type = OP_PADHV;
4998 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5001 return newUNOP(OP_RV2HV, 0, scalar(o));
5005 Perl_oopsCV(pTHX_ OP *o)
5007 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5013 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5015 return newUNOP(OP_RV2CV, flags, scalar(o));
5019 Perl_newSVREF(pTHX_ OP *o)
5021 if (o->op_type == OP_PADANY) {
5022 o->op_type = OP_PADSV;
5023 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5026 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5027 o->op_flags |= OPpDONE_SVREF;
5030 return newUNOP(OP_RV2SV, 0, scalar(o));
5033 /* Check routines. */
5036 Perl_ck_anoncode(pTHX_ OP *o)
5041 name = NEWSV(1106,0);
5042 sv_upgrade(name, SVt_PVNV);
5043 sv_setpvn(name, "&", 1);
5046 ix = pad_alloc(o->op_type, SVs_PADMY);
5047 av_store(PL_comppad_name, ix, name);
5048 av_store(PL_comppad, ix, cSVOPo->op_sv);
5049 SvPADMY_on(cSVOPo->op_sv);
5050 cSVOPo->op_sv = Nullsv;
5051 cSVOPo->op_targ = ix;
5056 Perl_ck_bitop(pTHX_ OP *o)
5058 o->op_private = PL_hints;
5063 Perl_ck_concat(pTHX_ OP *o)
5065 if (cUNOPo->op_first->op_type == OP_CONCAT)
5066 o->op_flags |= OPf_STACKED;
5071 Perl_ck_spair(pTHX_ OP *o)
5073 if (o->op_flags & OPf_KIDS) {
5076 OPCODE type = o->op_type;
5077 o = modkids(ck_fun(o), type);
5078 kid = cUNOPo->op_first;
5079 newop = kUNOP->op_first->op_sibling;
5081 (newop->op_sibling ||
5082 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
5083 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
5084 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
5088 op_free(kUNOP->op_first);
5089 kUNOP->op_first = newop;
5091 o->op_ppaddr = PL_ppaddr[++o->op_type];
5096 Perl_ck_delete(pTHX_ OP *o)
5100 if (o->op_flags & OPf_KIDS) {
5101 OP *kid = cUNOPo->op_first;
5102 switch (kid->op_type) {
5104 o->op_flags |= OPf_SPECIAL;
5107 o->op_private |= OPpSLICE;
5110 o->op_flags |= OPf_SPECIAL;
5115 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5116 PL_op_desc[o->op_type]);
5124 Perl_ck_eof(pTHX_ OP *o)
5126 I32 type = o->op_type;
5128 if (o->op_flags & OPf_KIDS) {
5129 if (cLISTOPo->op_first->op_type == OP_STUB) {
5131 o = newUNOP(type, OPf_SPECIAL,
5132 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
5140 Perl_ck_eval(pTHX_ OP *o)
5142 PL_hints |= HINT_BLOCK_SCOPE;
5143 if (o->op_flags & OPf_KIDS) {
5144 SVOP *kid = (SVOP*)cUNOPo->op_first;
5147 o->op_flags &= ~OPf_KIDS;
5150 else if (kid->op_type == OP_LINESEQ) {
5153 kid->op_next = o->op_next;
5154 cUNOPo->op_first = 0;
5157 NewOp(1101, enter, 1, LOGOP);
5158 enter->op_type = OP_ENTERTRY;
5159 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5160 enter->op_private = 0;
5162 /* establish postfix order */
5163 enter->op_next = (OP*)enter;
5165 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5166 o->op_type = OP_LEAVETRY;
5167 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5168 enter->op_other = o;
5176 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5178 o->op_targ = (PADOFFSET)PL_hints;
5183 Perl_ck_exit(pTHX_ OP *o)
5186 HV *table = GvHV(PL_hintgv);
5188 SV **svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5189 if (svp && *svp && SvTRUE(*svp))
5190 o->op_private |= OPpEXIT_VMSISH;
5197 Perl_ck_exec(pTHX_ OP *o)
5200 if (o->op_flags & OPf_STACKED) {
5202 kid = cUNOPo->op_first->op_sibling;
5203 if (kid->op_type == OP_RV2GV)
5212 Perl_ck_exists(pTHX_ OP *o)
5215 if (o->op_flags & OPf_KIDS) {
5216 OP *kid = cUNOPo->op_first;
5217 if (kid->op_type == OP_ENTERSUB) {
5218 (void) ref(kid, o->op_type);
5219 if (kid->op_type != OP_RV2CV && !PL_error_count)
5220 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5221 PL_op_desc[o->op_type]);
5222 o->op_private |= OPpEXISTS_SUB;
5224 else if (kid->op_type == OP_AELEM)
5225 o->op_flags |= OPf_SPECIAL;
5226 else if (kid->op_type != OP_HELEM)
5227 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5228 PL_op_desc[o->op_type]);
5236 Perl_ck_gvconst(pTHX_ register OP *o)
5238 o = fold_constants(o);
5239 if (o->op_type == OP_CONST)
5246 Perl_ck_rvconst(pTHX_ register OP *o)
5249 SVOP *kid = (SVOP*)cUNOPo->op_first;
5251 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5252 if (kid->op_type == OP_CONST) {
5256 SV *kidsv = kid->op_sv;
5259 /* Is it a constant from cv_const_sv()? */
5260 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5261 SV *rsv = SvRV(kidsv);
5262 int svtype = SvTYPE(rsv);
5263 char *badtype = Nullch;
5265 switch (o->op_type) {
5267 if (svtype > SVt_PVMG)
5268 badtype = "a SCALAR";
5271 if (svtype != SVt_PVAV)
5272 badtype = "an ARRAY";
5275 if (svtype != SVt_PVHV) {
5276 if (svtype == SVt_PVAV) { /* pseudohash? */
5277 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5278 if (ksv && SvROK(*ksv)
5279 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5288 if (svtype != SVt_PVCV)
5293 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5296 name = SvPV(kidsv, n_a);
5297 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5298 char *badthing = Nullch;
5299 switch (o->op_type) {
5301 badthing = "a SCALAR";
5304 badthing = "an ARRAY";
5307 badthing = "a HASH";
5312 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
5316 * This is a little tricky. We only want to add the symbol if we
5317 * didn't add it in the lexer. Otherwise we get duplicate strict
5318 * warnings. But if we didn't add it in the lexer, we must at
5319 * least pretend like we wanted to add it even if it existed before,
5320 * or we get possible typo warnings. OPpCONST_ENTERED says
5321 * whether the lexer already added THIS instance of this symbol.
5323 iscv = (o->op_type == OP_RV2CV) * 2;
5325 gv = gv_fetchpv(name,
5326 iscv | !(kid->op_private & OPpCONST_ENTERED),
5329 : o->op_type == OP_RV2SV
5331 : o->op_type == OP_RV2AV
5333 : o->op_type == OP_RV2HV
5336 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5338 kid->op_type = OP_GV;
5339 SvREFCNT_dec(kid->op_sv);
5341 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5342 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5343 SvREFCNT_dec(PL_curpad[kPADOP->op_padix]);
5345 PL_curpad[kPADOP->op_padix] = SvREFCNT_inc(gv);
5347 kid->op_sv = SvREFCNT_inc(gv);
5349 kid->op_ppaddr = PL_ppaddr[OP_GV];
5356 Perl_ck_ftst(pTHX_ OP *o)
5359 I32 type = o->op_type;
5361 if (o->op_flags & OPf_REF) {
5364 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5365 SVOP *kid = (SVOP*)cUNOPo->op_first;
5367 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5369 OP *newop = newGVOP(type, OPf_REF,
5370 gv_fetchpv(SvPVx(kid->op_sv, n_a), TRUE, SVt_PVIO));
5377 if (type == OP_FTTTY)
5378 o = newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
5381 o = newUNOP(type, 0, newDEFSVOP());
5384 if (type == OP_FTTEXT || type == OP_FTBINARY) {
5386 if (PL_hints & HINT_LOCALE)
5387 o->op_private |= OPpLOCALE;
5394 Perl_ck_fun(pTHX_ OP *o)
5401 int type = o->op_type;
5402 register I32 oa = PL_opargs[type] >> OASHIFT;
5404 if (o->op_flags & OPf_STACKED) {
5405 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5408 return no_fh_allowed(o);
5411 if (o->op_flags & OPf_KIDS) {
5413 tokid = &cLISTOPo->op_first;
5414 kid = cLISTOPo->op_first;
5415 if (kid->op_type == OP_PUSHMARK ||
5416 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5418 tokid = &kid->op_sibling;
5419 kid = kid->op_sibling;
5421 if (!kid && PL_opargs[type] & OA_DEFGV)
5422 *tokid = kid = newDEFSVOP();
5426 sibl = kid->op_sibling;
5429 /* list seen where single (scalar) arg expected? */
5430 if (numargs == 1 && !(oa >> 4)
5431 && kid->op_type == OP_LIST && type != OP_SCALAR)
5433 return too_many_arguments(o,PL_op_desc[type]);
5446 if (kid->op_type == OP_CONST &&
5447 (kid->op_private & OPpCONST_BARE))
5449 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5450 OP *newop = newAVREF(newGVOP(OP_GV, 0,
5451 gv_fetchpv(name, TRUE, SVt_PVAV) ));
5452 if (ckWARN(WARN_DEPRECATED))
5453 Perl_warner(aTHX_ WARN_DEPRECATED,
5454 "Array @%s missing the @ in argument %"IVdf" of %s()",
5455 name, (IV)numargs, PL_op_desc[type]);
5458 kid->op_sibling = sibl;
5461 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5462 bad_type(numargs, "array", PL_op_desc[type], kid);
5466 if (kid->op_type == OP_CONST &&
5467 (kid->op_private & OPpCONST_BARE))
5469 char *name = SvPVx(((SVOP*)kid)->op_sv, n_a);
5470 OP *newop = newHVREF(newGVOP(OP_GV, 0,
5471 gv_fetchpv(name, TRUE, SVt_PVHV) ));
5472 if (ckWARN(WARN_DEPRECATED))
5473 Perl_warner(aTHX_ WARN_DEPRECATED,
5474 "Hash %%%s missing the %% in argument %"IVdf" of %s()",
5475 name, (IV)numargs, PL_op_desc[type]);
5478 kid->op_sibling = sibl;
5481 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5482 bad_type(numargs, "hash", PL_op_desc[type], kid);
5487 OP *newop = newUNOP(OP_NULL, 0, kid);
5488 kid->op_sibling = 0;
5490 newop->op_next = newop;
5492 kid->op_sibling = sibl;
5497 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5498 if (kid->op_type == OP_CONST &&
5499 (kid->op_private & OPpCONST_BARE))
5501 OP *newop = newGVOP(OP_GV, 0,
5502 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, n_a), TRUE,
5507 else if (kid->op_type == OP_READLINE) {
5508 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5509 bad_type(numargs, "HANDLE", PL_op_desc[o->op_type], kid);
5512 I32 flags = OPf_SPECIAL;
5516 /* is this op a FH constructor? */
5517 if (is_handle_constructor(o,numargs)) {
5518 char *name = Nullch;
5522 /* Set a flag to tell rv2gv to vivify
5523 * need to "prove" flag does not mean something
5524 * else already - NI-S 1999/05/07
5527 if (kid->op_type == OP_PADSV) {
5528 SV **namep = av_fetch(PL_comppad_name,
5530 if (namep && *namep)
5531 name = SvPV(*namep, len);
5533 else if (kid->op_type == OP_RV2SV
5534 && kUNOP->op_first->op_type == OP_GV)
5536 GV *gv = cGVOPx_gv(kUNOP->op_first);
5538 len = GvNAMELEN(gv);
5540 else if (kid->op_type == OP_AELEM
5541 || kid->op_type == OP_HELEM)
5543 name = "__ANONIO__";
5549 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5550 namesv = PL_curpad[targ];
5551 (void)SvUPGRADE(namesv, SVt_PV);
5553 sv_setpvn(namesv, "$", 1);
5554 sv_catpvn(namesv, name, len);
5557 kid->op_sibling = 0;
5558 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5559 kid->op_targ = targ;
5560 kid->op_private |= priv;
5562 kid->op_sibling = sibl;
5568 mod(scalar(kid), type);
5572 tokid = &kid->op_sibling;
5573 kid = kid->op_sibling;
5575 o->op_private |= numargs;
5577 return too_many_arguments(o,PL_op_desc[o->op_type]);
5580 else if (PL_opargs[type] & OA_DEFGV) {
5582 return newUNOP(type, 0, newDEFSVOP());
5586 while (oa & OA_OPTIONAL)
5588 if (oa && oa != OA_LIST)
5589 return too_few_arguments(o,PL_op_desc[o->op_type]);
5595 Perl_ck_glob(pTHX_ OP *o)
5600 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5601 append_elem(OP_GLOB, o, newDEFSVOP());
5603 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
5604 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5606 #if !defined(PERL_EXTERNAL_GLOB)
5607 /* XXX this can be tightened up and made more failsafe. */
5610 Perl_load_module(aTHX_ 0, newSVpvn("File::Glob", 10), Nullsv,
5611 /* null-terminated import list */
5612 newSVpvn(":globally", 9), Nullsv);
5613 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
5616 #endif /* PERL_EXTERNAL_GLOB */
5618 if (gv && GvIMPORTED_CV(gv)) {
5619 append_elem(OP_GLOB, o,
5620 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5621 o->op_type = OP_LIST;
5622 o->op_ppaddr = PL_ppaddr[OP_LIST];
5623 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5624 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5625 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5626 append_elem(OP_LIST, o,
5627 scalar(newUNOP(OP_RV2CV, 0,
5628 newGVOP(OP_GV, 0, gv)))));
5629 o = newUNOP(OP_NULL, 0, ck_subr(o));
5630 o->op_targ = OP_GLOB; /* hint at what it used to be */
5633 gv = newGVgen("main");
5635 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5641 Perl_ck_grep(pTHX_ OP *o)
5645 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5647 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5648 NewOp(1101, gwop, 1, LOGOP);
5650 if (o->op_flags & OPf_STACKED) {
5653 kid = cLISTOPo->op_first->op_sibling;
5654 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
5657 kid->op_next = (OP*)gwop;
5658 o->op_flags &= ~OPf_STACKED;
5660 kid = cLISTOPo->op_first->op_sibling;
5661 if (type == OP_MAPWHILE)
5668 kid = cLISTOPo->op_first->op_sibling;
5669 if (kid->op_type != OP_NULL)
5670 Perl_croak(aTHX_ "panic: ck_grep");
5671 kid = kUNOP->op_first;
5673 gwop->op_type = type;
5674 gwop->op_ppaddr = PL_ppaddr[type];
5675 gwop->op_first = listkids(o);
5676 gwop->op_flags |= OPf_KIDS;
5677 gwop->op_private = 1;
5678 gwop->op_other = LINKLIST(kid);
5679 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5680 kid->op_next = (OP*)gwop;
5682 kid = cLISTOPo->op_first->op_sibling;
5683 if (!kid || !kid->op_sibling)
5684 return too_few_arguments(o,PL_op_desc[o->op_type]);
5685 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5686 mod(kid, OP_GREPSTART);
5692 Perl_ck_index(pTHX_ OP *o)
5694 if (o->op_flags & OPf_KIDS) {
5695 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5697 kid = kid->op_sibling; /* get past "big" */
5698 if (kid && kid->op_type == OP_CONST)
5699 fbm_compile(((SVOP*)kid)->op_sv, 0);
5705 Perl_ck_lengthconst(pTHX_ OP *o)
5707 /* XXX length optimization goes here */
5712 Perl_ck_lfun(pTHX_ OP *o)
5714 OPCODE type = o->op_type;
5715 return modkids(ck_fun(o), type);
5719 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5722 if ((o->op_flags & OPf_KIDS) && ckWARN(WARN_DEPRECATED)) {
5723 switch (cUNOPo->op_first->op_type) {
5725 break; /* Globals via GV can be undef */
5727 case OP_AASSIGN: /* Is this a good idea? */
5728 Perl_warner(aTHX_ WARN_DEPRECATED,
5729 "defined(@array) is deprecated");
5730 Perl_warner(aTHX_ WARN_DEPRECATED,
5731 "\t(Maybe you should just omit the defined()?)\n");
5734 break; /* Globals via GV can be undef */
5736 Perl_warner(aTHX_ WARN_DEPRECATED,
5737 "defined(%%hash) is deprecated");
5738 Perl_warner(aTHX_ WARN_DEPRECATED,
5739 "\t(Maybe you should just omit the defined()?)\n");
5750 Perl_ck_rfun(pTHX_ OP *o)
5752 OPCODE type = o->op_type;
5753 return refkids(ck_fun(o), type);
5757 Perl_ck_listiob(pTHX_ OP *o)
5761 kid = cLISTOPo->op_first;
5764 kid = cLISTOPo->op_first;
5766 if (kid->op_type == OP_PUSHMARK)
5767 kid = kid->op_sibling;
5768 if (kid && o->op_flags & OPf_STACKED)
5769 kid = kid->op_sibling;
5770 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5771 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5772 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5773 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5774 cLISTOPo->op_first->op_sibling = kid;
5775 cLISTOPo->op_last = kid;
5776 kid = kid->op_sibling;
5781 append_elem(o->op_type, o, newDEFSVOP());
5787 if (PL_hints & HINT_LOCALE)
5788 o->op_private |= OPpLOCALE;
5795 Perl_ck_fun_locale(pTHX_ OP *o)
5801 if (PL_hints & HINT_LOCALE)
5802 o->op_private |= OPpLOCALE;
5809 Perl_ck_sassign(pTHX_ OP *o)
5811 OP *kid = cLISTOPo->op_first;
5812 /* has a disposable target? */
5813 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5814 && !(kid->op_flags & OPf_STACKED)
5815 /* Cannot steal the second time! */
5816 && !(kid->op_private & OPpTARGET_MY))
5818 OP *kkid = kid->op_sibling;
5820 /* Can just relocate the target. */
5821 if (kkid && kkid->op_type == OP_PADSV
5822 && !(kkid->op_private & OPpLVAL_INTRO))
5824 kid->op_targ = kkid->op_targ;
5826 /* Now we do not need PADSV and SASSIGN. */
5827 kid->op_sibling = o->op_sibling; /* NULL */
5828 cLISTOPo->op_first = NULL;
5831 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5839 Perl_ck_scmp(pTHX_ OP *o)
5843 if (PL_hints & HINT_LOCALE)
5844 o->op_private |= OPpLOCALE;
5851 Perl_ck_match(pTHX_ OP *o)
5853 o->op_private |= OPpRUNTIME;
5858 Perl_ck_method(pTHX_ OP *o)
5860 OP *kid = cUNOPo->op_first;
5861 if (kid->op_type == OP_CONST) {
5862 SV* sv = kSVOP->op_sv;
5863 if (!(strchr(SvPVX(sv), ':') || strchr(SvPVX(sv), '\''))) {
5865 (void)SvUPGRADE(sv, SVt_PVIV);
5867 PERL_HASH(SvUVX(sv), SvPVX(sv), SvCUR(sv));
5868 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5869 kSVOP->op_sv = Nullsv;
5878 Perl_ck_null(pTHX_ OP *o)
5884 Perl_ck_open(pTHX_ OP *o)
5886 HV *table = GvHV(PL_hintgv);
5890 svp = hv_fetch(table, "open_IN", 7, FALSE);
5892 mode = mode_from_discipline(*svp);
5893 if (mode & O_BINARY)
5894 o->op_private |= OPpOPEN_IN_RAW;
5895 else if (mode & O_TEXT)
5896 o->op_private |= OPpOPEN_IN_CRLF;
5899 svp = hv_fetch(table, "open_OUT", 8, FALSE);
5901 mode = mode_from_discipline(*svp);
5902 if (mode & O_BINARY)
5903 o->op_private |= OPpOPEN_OUT_RAW;
5904 else if (mode & O_TEXT)
5905 o->op_private |= OPpOPEN_OUT_CRLF;
5908 if (o->op_type == OP_BACKTICK)
5914 Perl_ck_repeat(pTHX_ OP *o)
5916 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
5917 o->op_private |= OPpREPEAT_DOLIST;
5918 cBINOPo->op_first = force_list(cBINOPo->op_first);
5926 Perl_ck_require(pTHX_ OP *o)
5928 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
5929 SVOP *kid = (SVOP*)cUNOPo->op_first;
5931 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5933 for (s = SvPVX(kid->op_sv); *s; s++) {
5934 if (*s == ':' && s[1] == ':') {
5936 Move(s+2, s+1, strlen(s+2)+1, char);
5937 --SvCUR(kid->op_sv);
5940 if (SvREADONLY(kid->op_sv)) {
5941 SvREADONLY_off(kid->op_sv);
5942 sv_catpvn(kid->op_sv, ".pm", 3);
5943 SvREADONLY_on(kid->op_sv);
5946 sv_catpvn(kid->op_sv, ".pm", 3);
5954 Perl_ck_retarget(pTHX_ OP *o)
5956 Perl_croak(aTHX_ "NOT IMPL LINE %d",__LINE__);
5963 Perl_ck_select(pTHX_ OP *o)
5966 if (o->op_flags & OPf_KIDS) {
5967 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5968 if (kid && kid->op_sibling) {
5969 o->op_type = OP_SSELECT;
5970 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
5972 return fold_constants(o);
5976 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5977 if (kid && kid->op_type == OP_RV2GV)
5978 kid->op_private &= ~HINT_STRICT_REFS;
5983 Perl_ck_shift(pTHX_ OP *o)
5985 I32 type = o->op_type;
5987 if (!(o->op_flags & OPf_KIDS)) {
5992 if (!CvUNIQUE(PL_compcv)) {
5993 argop = newOP(OP_PADAV, OPf_REF);
5994 argop->op_targ = 0; /* PL_curpad[0] is @_ */
5997 argop = newUNOP(OP_RV2AV, 0,
5998 scalar(newGVOP(OP_GV, 0,
5999 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6002 argop = newUNOP(OP_RV2AV, 0,
6003 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(PL_compcv) ?
6004 PL_defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6005 #endif /* USE_THREADS */
6006 return newUNOP(type, 0, scalar(argop));
6008 return scalar(modkids(ck_fun(o), type));
6012 Perl_ck_sort(pTHX_ OP *o)
6017 if (PL_hints & HINT_LOCALE)
6018 o->op_private |= OPpLOCALE;
6021 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6023 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6024 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6026 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6028 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6030 if (kid->op_type == OP_SCOPE) {
6034 else if (kid->op_type == OP_LEAVE) {
6035 if (o->op_type == OP_SORT) {
6036 null(kid); /* wipe out leave */
6039 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6040 if (k->op_next == kid)
6042 /* don't descend into loops */
6043 else if (k->op_type == OP_ENTERLOOP
6044 || k->op_type == OP_ENTERITER)
6046 k = cLOOPx(k)->op_lastop;
6051 kid->op_next = 0; /* just disconnect the leave */
6052 k = kLISTOP->op_first;
6057 if (o->op_type == OP_SORT) {
6058 /* provide scalar context for comparison function/block */
6064 o->op_flags |= OPf_SPECIAL;
6066 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6069 firstkid = firstkid->op_sibling;
6072 /* provide list context for arguments */
6073 if (o->op_type == OP_SORT)
6080 S_simplify_sort(pTHX_ OP *o)
6083 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6087 if (!(o->op_flags & OPf_STACKED))
6089 GvMULTI_on(gv_fetchpv("a", TRUE, SVt_PV));
6090 GvMULTI_on(gv_fetchpv("b", TRUE, SVt_PV));
6091 kid = kUNOP->op_first; /* get past null */
6092 if (kid->op_type != OP_SCOPE)
6094 kid = kLISTOP->op_last; /* get past scope */
6095 switch(kid->op_type) {
6103 k = kid; /* remember this node*/
6104 if (kBINOP->op_first->op_type != OP_RV2SV)
6106 kid = kBINOP->op_first; /* get past cmp */
6107 if (kUNOP->op_first->op_type != OP_GV)
6109 kid = kUNOP->op_first; /* get past rv2sv */
6111 if (GvSTASH(gv) != PL_curstash)
6113 if (strEQ(GvNAME(gv), "a"))
6115 else if (strEQ(GvNAME(gv), "b"))
6119 kid = k; /* back to cmp */
6120 if (kBINOP->op_last->op_type != OP_RV2SV)
6122 kid = kBINOP->op_last; /* down to 2nd arg */
6123 if (kUNOP->op_first->op_type != OP_GV)
6125 kid = kUNOP->op_first; /* get past rv2sv */
6127 if (GvSTASH(gv) != PL_curstash
6129 ? strNE(GvNAME(gv), "a")
6130 : strNE(GvNAME(gv), "b")))
6132 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6134 o->op_private |= OPpSORT_REVERSE;
6135 if (k->op_type == OP_NCMP)
6136 o->op_private |= OPpSORT_NUMERIC;
6137 if (k->op_type == OP_I_NCMP)
6138 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6139 kid = cLISTOPo->op_first->op_sibling;
6140 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6141 op_free(kid); /* then delete it */
6142 cLISTOPo->op_children--;
6146 Perl_ck_split(pTHX_ OP *o)
6150 if (o->op_flags & OPf_STACKED)
6151 return no_fh_allowed(o);
6153 kid = cLISTOPo->op_first;
6154 if (kid->op_type != OP_NULL)
6155 Perl_croak(aTHX_ "panic: ck_split");
6156 kid = kid->op_sibling;
6157 op_free(cLISTOPo->op_first);
6158 cLISTOPo->op_first = kid;
6160 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvn(" ", 1));
6161 cLISTOPo->op_last = kid; /* There was only one element previously */
6164 if (kid->op_type != OP_MATCH) {
6165 OP *sibl = kid->op_sibling;
6166 kid->op_sibling = 0;
6167 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6168 if (cLISTOPo->op_first == cLISTOPo->op_last)
6169 cLISTOPo->op_last = kid;
6170 cLISTOPo->op_first = kid;
6171 kid->op_sibling = sibl;
6174 kid->op_type = OP_PUSHRE;
6175 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6178 if (!kid->op_sibling)
6179 append_elem(OP_SPLIT, o, newDEFSVOP());
6181 kid = kid->op_sibling;
6184 if (!kid->op_sibling)
6185 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6187 kid = kid->op_sibling;
6190 if (kid->op_sibling)
6191 return too_many_arguments(o,PL_op_desc[o->op_type]);
6197 Perl_ck_join(pTHX_ OP *o)
6199 if (ckWARN(WARN_SYNTAX)) {
6200 OP *kid = cLISTOPo->op_first->op_sibling;
6201 if (kid && kid->op_type == OP_MATCH) {
6202 char *pmstr = "STRING";
6203 if (kPMOP->op_pmregexp)
6204 pmstr = kPMOP->op_pmregexp->precomp;
6205 Perl_warner(aTHX_ WARN_SYNTAX,
6206 "/%s/ should probably be written as \"%s\"",
6214 Perl_ck_subr(pTHX_ OP *o)
6217 OP *prev = ((cUNOPo->op_first->op_sibling)
6218 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6219 OP *o2 = prev->op_sibling;
6228 o->op_private |= OPpENTERSUB_HASTARG;
6229 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6230 if (cvop->op_type == OP_RV2CV) {
6232 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6233 null(cvop); /* disable rv2cv */
6234 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6235 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6236 GV *gv = cGVOPx_gv(tmpop);
6239 tmpop->op_private |= OPpEARLY_CV;
6240 else if (SvPOK(cv)) {
6241 namegv = CvANON(cv) ? gv : CvGV(cv);
6242 proto = SvPV((SV*)cv, n_a);
6246 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6247 if (o2->op_type == OP_CONST)
6248 o2->op_private &= ~OPpCONST_STRICT;
6249 else if (o2->op_type == OP_LIST) {
6250 OP *o = ((UNOP*)o2)->op_first->op_sibling;
6251 if (o && o->op_type == OP_CONST)
6252 o->op_private &= ~OPpCONST_STRICT;
6255 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6256 if (PERLDB_SUB && PL_curstash != PL_debstash)
6257 o->op_private |= OPpENTERSUB_DB;
6258 while (o2 != cvop) {
6262 return too_many_arguments(o, gv_ename(namegv));
6280 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6282 arg == 1 ? "block or sub {}" : "sub {}",
6283 gv_ename(namegv), o2);
6286 /* '*' allows any scalar type, including bareword */
6289 if (o2->op_type == OP_RV2GV)
6290 goto wrapref; /* autoconvert GLOB -> GLOBref */
6291 else if (o2->op_type == OP_CONST)
6292 o2->op_private &= ~OPpCONST_STRICT;
6293 else if (o2->op_type == OP_ENTERSUB) {
6294 /* accidental subroutine, revert to bareword */
6295 OP *gvop = ((UNOP*)o2)->op_first;
6296 if (gvop && gvop->op_type == OP_NULL) {
6297 gvop = ((UNOP*)gvop)->op_first;
6299 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6302 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6303 (gvop = ((UNOP*)gvop)->op_first) &&
6304 gvop->op_type == OP_GV)
6306 GV *gv = cGVOPx_gv(gvop);
6307 OP *sibling = o2->op_sibling;
6308 SV *n = newSVpvn("",0);
6310 gv_fullname3(n, gv, "");
6311 if (SvCUR(n)>6 && strnEQ(SvPVX(n),"main::",6))
6312 sv_chop(n, SvPVX(n)+6);
6313 o2 = newSVOP(OP_CONST, 0, n);
6314 prev->op_sibling = o2;
6315 o2->op_sibling = sibling;
6327 if (o2->op_type != OP_RV2GV)
6328 bad_type(arg, "symbol", gv_ename(namegv), o2);
6331 if (o2->op_type != OP_ENTERSUB)
6332 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6335 if (o2->op_type != OP_RV2SV
6336 && o2->op_type != OP_PADSV
6337 && o2->op_type != OP_HELEM
6338 && o2->op_type != OP_AELEM
6339 && o2->op_type != OP_THREADSV)
6341 bad_type(arg, "scalar", gv_ename(namegv), o2);
6345 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
6346 bad_type(arg, "array", gv_ename(namegv), o2);
6349 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
6350 bad_type(arg, "hash", gv_ename(namegv), o2);
6354 OP* sib = kid->op_sibling;
6355 kid->op_sibling = 0;
6356 o2 = newUNOP(OP_REFGEN, 0, kid);
6357 o2->op_sibling = sib;
6358 prev->op_sibling = o2;
6369 Perl_croak(aTHX_ "Malformed prototype for %s: %s",
6370 gv_ename(namegv), SvPV((SV*)cv, n_a));
6375 mod(o2, OP_ENTERSUB);
6377 o2 = o2->op_sibling;
6379 if (proto && !optional &&
6380 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6381 return too_few_arguments(o, gv_ename(namegv));
6386 Perl_ck_svconst(pTHX_ OP *o)
6388 SvREADONLY_on(cSVOPo->op_sv);
6393 Perl_ck_trunc(pTHX_ OP *o)
6395 if (o->op_flags & OPf_KIDS) {
6396 SVOP *kid = (SVOP*)cUNOPo->op_first;
6398 if (kid->op_type == OP_NULL)
6399 kid = (SVOP*)kid->op_sibling;
6400 if (kid && kid->op_type == OP_CONST &&
6401 (kid->op_private & OPpCONST_BARE))
6403 o->op_flags |= OPf_SPECIAL;
6404 kid->op_private &= ~OPpCONST_STRICT;
6410 /* A peephole optimizer. We visit the ops in the order they're to execute. */
6413 Perl_peep(pTHX_ register OP *o)
6416 register OP* oldop = 0;
6418 OP *last_composite = Nullop;
6420 if (!o || o->op_seq)
6424 SAVEVPTR(PL_curcop);
6425 for (; o; o = o->op_next) {
6431 switch (o->op_type) {
6435 PL_curcop = ((COP*)o); /* for warnings */
6436 o->op_seq = PL_op_seqmax++;
6437 last_composite = Nullop;
6441 if (cSVOPo->op_private & OPpCONST_STRICT)
6442 no_bareword_allowed(o);
6444 /* Relocate sv to the pad for thread safety.
6445 * Despite being a "constant", the SV is written to,
6446 * for reference counts, sv_upgrade() etc. */
6448 PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6449 if (SvPADTMP(cSVOPo->op_sv)) {
6450 /* If op_sv is already a PADTMP then it is being used by
6451 * another pad, so make a copy. */
6452 sv_setsv(PL_curpad[ix],cSVOPo->op_sv);
6453 SvREADONLY_on(PL_curpad[ix]);
6454 SvREFCNT_dec(cSVOPo->op_sv);
6457 SvREFCNT_dec(PL_curpad[ix]);
6458 SvPADTMP_on(cSVOPo->op_sv);
6459 PL_curpad[ix] = cSVOPo->op_sv;
6461 cSVOPo->op_sv = Nullsv;
6465 o->op_seq = PL_op_seqmax++;
6469 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6470 if (o->op_next->op_private & OPpTARGET_MY) {
6471 if (o->op_flags & OPf_STACKED) /* chained concats */
6472 goto ignore_optimization;
6474 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6475 o->op_targ = o->op_next->op_targ;
6476 o->op_next->op_targ = 0;
6477 o->op_private |= OPpTARGET_MY;
6482 ignore_optimization:
6483 o->op_seq = PL_op_seqmax++;
6486 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6487 o->op_seq = PL_op_seqmax++;
6488 break; /* Scalar stub must produce undef. List stub is noop */
6492 if (o->op_targ == OP_NEXTSTATE
6493 || o->op_targ == OP_DBSTATE
6494 || o->op_targ == OP_SETSTATE)
6496 PL_curcop = ((COP*)o);
6503 if (oldop && o->op_next) {
6504 oldop->op_next = o->op_next;
6507 o->op_seq = PL_op_seqmax++;
6511 if (o->op_next->op_type == OP_RV2SV) {
6512 if (!(o->op_next->op_private & OPpDEREF)) {
6514 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6516 o->op_next = o->op_next->op_next;
6517 o->op_type = OP_GVSV;
6518 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6521 else if (o->op_next->op_type == OP_RV2AV) {
6522 OP* pop = o->op_next->op_next;
6524 if (pop->op_type == OP_CONST &&
6525 (PL_op = pop->op_next) &&
6526 pop->op_next->op_type == OP_AELEM &&
6527 !(pop->op_next->op_private &
6528 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
6529 (i = SvIV(((SVOP*)pop)->op_sv) - PL_compiling.cop_arybase)
6537 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6538 o->op_next = pop->op_next->op_next;
6539 o->op_type = OP_AELEMFAST;
6540 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6541 o->op_private = (U8)i;
6546 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6548 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX(GvCV(gv))) {
6549 /* XXX could check prototype here instead of just carping */
6550 SV *sv = sv_newmortal();
6551 gv_efullname3(sv, gv, Nullch);
6552 Perl_warner(aTHX_ WARN_PROTOTYPE,
6553 "%s() called too early to check prototype",
6558 o->op_seq = PL_op_seqmax++;
6569 o->op_seq = PL_op_seqmax++;
6570 while (cLOGOP->op_other->op_type == OP_NULL)
6571 cLOGOP->op_other = cLOGOP->op_other->op_next;
6572 peep(cLOGOP->op_other);
6576 o->op_seq = PL_op_seqmax++;
6577 peep(cLOOP->op_redoop);
6578 peep(cLOOP->op_nextop);
6579 peep(cLOOP->op_lastop);
6585 o->op_seq = PL_op_seqmax++;
6586 peep(cPMOP->op_pmreplstart);
6590 o->op_seq = PL_op_seqmax++;
6591 if (ckWARN(WARN_SYNTAX) && o->op_next
6592 && o->op_next->op_type == OP_NEXTSTATE) {
6593 if (o->op_next->op_sibling &&
6594 o->op_next->op_sibling->op_type != OP_EXIT &&
6595 o->op_next->op_sibling->op_type != OP_WARN &&
6596 o->op_next->op_sibling->op_type != OP_DIE) {
6597 line_t oldline = CopLINE(PL_curcop);
6599 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6600 Perl_warner(aTHX_ WARN_EXEC,
6601 "Statement unlikely to be reached");
6602 Perl_warner(aTHX_ WARN_EXEC,
6603 "\t(Maybe you meant system() when you said exec()?)\n");
6604 CopLINE_set(PL_curcop, oldline);
6613 SV **svp, **indsvp, *sv;
6618 o->op_seq = PL_op_seqmax++;
6619 if ((o->op_private & (OPpLVAL_INTRO))
6620 || ((BINOP*)o)->op_last->op_type != OP_CONST)
6622 rop = (UNOP*)((BINOP*)o)->op_first;
6623 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6625 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6626 if (!SvOBJECT(lexname))
6628 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6629 if (!fields || !GvHV(*fields))
6631 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6632 key = SvPV(*svp, keylen);
6633 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6635 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" in variable %s of type %s",
6636 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6638 ind = SvIV(*indsvp);
6640 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6641 rop->op_type = OP_RV2AV;
6642 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6643 o->op_type = OP_AELEM;
6644 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6646 if (SvREADONLY(*svp))
6648 SvFLAGS(sv) |= (SvFLAGS(*svp)
6649 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6659 SV **svp, **indsvp, *sv;
6663 SVOP *first_key_op, *key_op;
6665 o->op_seq = PL_op_seqmax++;
6666 if ((o->op_private & (OPpLVAL_INTRO))
6667 /* I bet there's always a pushmark... */
6668 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6669 /* hmmm, no optimization if list contains only one key. */
6671 rop = (UNOP*)((LISTOP*)o)->op_last;
6672 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6674 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6675 if (!SvOBJECT(lexname))
6677 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
6678 if (!fields || !GvHV(*fields))
6680 /* Again guessing that the pushmark can be jumped over.... */
6681 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6682 ->op_first->op_sibling;
6683 /* Check that the key list contains only constants. */
6684 for (key_op = first_key_op; key_op;
6685 key_op = (SVOP*)key_op->op_sibling)
6686 if (key_op->op_type != OP_CONST)
6690 rop->op_type = OP_RV2AV;
6691 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6692 o->op_type = OP_ASLICE;
6693 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6694 for (key_op = first_key_op; key_op;
6695 key_op = (SVOP*)key_op->op_sibling) {
6696 svp = cSVOPx_svp(key_op);
6697 key = SvPV(*svp, keylen);
6698 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
6700 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6701 "in variable %s of type %s",
6702 key, SvPV(lexname, n_a), HvNAME(SvSTASH(lexname)));
6704 ind = SvIV(*indsvp);
6706 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6708 if (SvREADONLY(*svp))
6710 SvFLAGS(sv) |= (SvFLAGS(*svp)
6711 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6720 if (!(o->op_flags & OPf_WANT)
6721 || (o->op_flags & OPf_WANT) == OPf_WANT_LIST)
6725 o->op_seq = PL_op_seqmax++;
6729 if (o->op_next && o->op_next->op_type != OP_LEAVESUBLV) {
6730 o->op_seq = PL_op_seqmax++;
6736 if (last_composite) {
6737 OP *r = last_composite;
6739 while (r->op_sibling)
6742 || (r->op_next->op_type == OP_LIST
6743 && r->op_next->op_next == o))
6745 if (last_composite->op_type == OP_RV2AV)
6746 yyerror("Lvalue subs returning arrays not implemented yet");
6748 yyerror("Lvalue subs returning hashes not implemented yet");
6755 o->op_seq = PL_op_seqmax++;