3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * Now far ahead the Road has gone,
13 * And I must follow, if I can,
14 * Pursuing it with eager feet,
15 * Until it joins some larger way
16 * Where many paths and errands meet.
17 * And whither then? I cannot say.
19 * [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains control-oriented pp ("push/pop") functions that
23 * execute the opcodes that make up a perl program. A typical pp function
24 * expects to find its arguments on the stack, and usually pushes its
25 * results onto the stack, hence the 'pp' terminology. Each OP structure
26 * contains a pointer to the relevant pp_foo() function.
28 * Control-oriented means things like pp_enteriter() and pp_next(), which
29 * alter the flow of control of the program.
34 #define PERL_IN_PP_CTL_C
37 #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
39 #define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
46 const PERL_CONTEXT *cx;
49 if (PL_op->op_private & OPpOFFBYONE) {
50 if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF;
53 cxix = dopoptosub(cxstack_ix);
59 switch (cx->blk_gimme) {
72 /* XXXX Should store the old value to allow for tie/overload - and
73 restore in regcomp, where marked with XXXX. */
83 register PMOP *pm = (PMOP*)cLOGOP->op_other;
87 /* prevent recompiling under /o and ithreads. */
88 #if defined(USE_ITHREADS)
89 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
90 if (PL_op->op_flags & OPf_STACKED) {
100 #define tryAMAGICregexp(rx) \
103 if (SvROK(rx) && SvAMAGIC(rx)) { \
104 SV *sv = AMG_CALLunary(rx, regexp_amg); \
108 if (SvTYPE(sv) != SVt_REGEXP) \
109 Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \
116 if (PL_op->op_flags & OPf_STACKED) {
117 /* multiple args; concatenate them */
119 tmpstr = PAD_SV(ARGTARG);
120 sv_setpvs(tmpstr, "");
121 while (++MARK <= SP) {
125 tryAMAGICregexp(msv);
127 if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) &&
128 (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign)))
130 sv_setsv(tmpstr, sv);
134 if (SvROK(msv) && SvTYPE(SvRV(msv)) == SVt_REGEXP) {
137 RX_SEEN_EVALS((REGEXP *)MUTABLE_PTR(msv));
140 sv_catsv_nomg(tmpstr, msv);
147 tryAMAGICregexp(tmpstr);
150 #undef tryAMAGICregexp
153 SV * const sv = SvRV(tmpstr);
154 if (SvTYPE(sv) == SVt_REGEXP)
157 else if (SvTYPE(tmpstr) == SVt_REGEXP)
158 re = (REGEXP*) tmpstr;
161 /* The match's LHS's get-magic might need to access this op's reg-
162 exp (as is sometimes the case with $'; see bug 70764). So we
163 must call get-magic now before we replace the regexp. Hopeful-
164 ly this hack can be replaced with the approach described at
165 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03
166 /msg122415.html some day. */
167 if(pm->op_type == OP_MATCH) {
169 const bool was_tainted = PL_tainted;
170 if (pm->op_flags & OPf_STACKED)
172 else if (pm->op_private & OPpTARGET_MY)
173 lhs = PAD_SV(pm->op_targ);
176 /* Restore the previous value of PL_tainted (which may have been
177 modified by get-magic), to avoid incorrectly setting the
178 RXf_TAINTED flag further down. */
179 PL_tainted = was_tainted;
182 re = reg_temp_copy(NULL, re);
183 ReREFCNT_dec(PM_GETRE(pm));
188 const char *t = SvOK(tmpstr) ? SvPV_nomg_const(tmpstr, len) : "";
191 assert (re != (REGEXP*) &PL_sv_undef);
193 /* Check against the last compiled regexp. */
194 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
195 memNE(RX_PRECOMP(re), t, len))
197 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
198 U32 pm_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
202 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
204 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
206 } else if (PL_curcop->cop_hints_hash) {
207 SV *ptr = cop_hints_fetch_pvs(PL_curcop, "regcomp", 0);
208 if (ptr && SvIOK(ptr) && SvIV(ptr))
209 eng = INT2PTR(regexp_engine*,SvIV(ptr));
212 if (PL_op->op_flags & OPf_SPECIAL)
213 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
215 if (!DO_UTF8(tmpstr) && SvUTF8(tmpstr)) {
216 /* Not doing UTF-8, despite what the SV says. Is this only if
217 we're trapped in use 'bytes'? */
218 /* Make a copy of the octet sequence, but without the flag on,
219 as the compiler now honours the SvUTF8 flag on tmpstr. */
221 const char *const p = SvPV(tmpstr, len);
222 tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
224 else if (SvAMAGIC(tmpstr) || SvGMAGICAL(tmpstr)) {
225 /* make a copy to avoid extra stringifies */
226 tmpstr = newSVpvn_flags(t, len, SVs_TEMP | SvUTF8(tmpstr));
230 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
232 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
234 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
235 inside tie/overload accessors. */
241 #ifndef INCOMPLETE_TAINTS
244 SvTAINTED_on((SV*)re);
245 RX_EXTFLAGS(re) |= RXf_TAINTED;
250 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
254 #if !defined(USE_ITHREADS)
255 /* can't change the optree at runtime either */
256 /* PMf_KEEP is handled differently under threads to avoid these problems */
257 if (pm->op_pmflags & PMf_KEEP) {
258 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
259 cLOGOP->op_first->op_next = PL_op->op_next;
269 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
270 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
271 register SV * const dstr = cx->sb_dstr;
272 register char *s = cx->sb_s;
273 register char *m = cx->sb_m;
274 char *orig = cx->sb_orig;
275 register REGEXP * const rx = cx->sb_rx;
277 REGEXP *old = PM_GETRE(pm);
284 PM_SETRE(pm,ReREFCNT_inc(rx));
287 rxres_restore(&cx->sb_rxres, rx);
288 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
290 if (cx->sb_iters++) {
291 const I32 saviters = cx->sb_iters;
292 if (cx->sb_iters > cx->sb_maxiters)
293 DIE(aTHX_ "Substitution loop");
295 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
297 /* See "how taint works" above pp_subst() */
299 cx->sb_rxtainted |= SUBST_TAINT_REPL;
300 sv_catsv_nomg(dstr, POPs);
301 /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */
305 /* I believe that we can't set REXEC_SCREAM here if
306 SvSCREAM(cx->sb_targ) is true because SvPVX(cx->sb_targ) isn't always
307 equal to s. [See the comment before Perl_re_intuit_start(), which is
308 called from Perl_regexec_flags(), which says that it should be when
309 SvSCREAM() is true.] s, cx->sb_strend and orig will be consistent
310 with SvPVX(cx->sb_targ), as substconst doesn't modify cx->sb_targ
312 if (CxONCE(cx) || s < orig ||
313 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
314 (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
315 ((cx->sb_rflags & REXEC_COPY_STR)
316 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
317 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
319 SV *targ = cx->sb_targ;
321 assert(cx->sb_strend >= s);
322 if(cx->sb_strend > s) {
323 if (DO_UTF8(dstr) && !SvUTF8(targ))
324 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
326 sv_catpvn(dstr, s, cx->sb_strend - s);
328 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
329 cx->sb_rxtainted |= SUBST_TAINT_PAT;
331 if (pm->op_pmflags & PMf_NONDESTRUCT) {
333 /* From here on down we're using the copy, and leaving the
334 original untouched. */
339 sv_force_normal_flags(targ, SV_COW_DROP_PV);
344 SvPV_set(targ, SvPVX(dstr));
345 SvCUR_set(targ, SvCUR(dstr));
346 SvLEN_set(targ, SvLEN(dstr));
349 SvPV_set(dstr, NULL);
351 mPUSHi(saviters - 1);
353 (void)SvPOK_only_UTF8(targ);
356 /* update the taint state of various various variables in
357 * preparation for final exit.
358 * See "how taint works" above pp_subst() */
360 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
361 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
362 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
364 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
366 if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET)
367 && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
369 SvTAINTED_on(TOPs); /* taint return value */
370 /* needed for mg_set below */
371 PL_tainted = cBOOL(cx->sb_rxtainted &
372 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL));
375 /* PL_tainted must be correctly set for this mg_set */
378 LEAVE_SCOPE(cx->sb_oldsave);
380 RETURNOP(pm->op_next);
383 cx->sb_iters = saviters;
385 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
388 cx->sb_orig = orig = RX_SUBBEG(rx);
390 cx->sb_strend = s + (cx->sb_strend - m);
392 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
394 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
395 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
397 sv_catpvn(dstr, s, m-s);
399 cx->sb_s = RX_OFFS(rx)[0].end + orig;
400 { /* Update the pos() information. */
402 = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ;
404 SvUPGRADE(sv, SVt_PVMG);
405 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
406 #ifdef PERL_OLD_COPY_ON_WRITE
408 sv_force_normal_flags(sv, 0);
410 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
413 mg->mg_len = m - orig;
416 (void)ReREFCNT_inc(rx);
417 /* update the taint state of various various variables in preparation
418 * for calling the code block.
419 * See "how taint works" above pp_subst() */
421 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
422 cx->sb_rxtainted |= SUBST_TAINT_PAT;
424 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
425 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
426 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
428 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
430 if (cx->sb_iters > 1 && (cx->sb_rxtainted &
431 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
432 SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT)
433 ? cx->sb_dstr : cx->sb_targ);
436 rxres_save(&cx->sb_rxres, rx);
438 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
442 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
447 PERL_ARGS_ASSERT_RXRES_SAVE;
450 if (!p || p[1] < RX_NPARENS(rx)) {
451 #ifdef PERL_OLD_COPY_ON_WRITE
452 i = 7 + RX_NPARENS(rx) * 2;
454 i = 6 + RX_NPARENS(rx) * 2;
463 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
464 RX_MATCH_COPIED_off(rx);
466 #ifdef PERL_OLD_COPY_ON_WRITE
467 *p++ = PTR2UV(RX_SAVED_COPY(rx));
468 RX_SAVED_COPY(rx) = NULL;
471 *p++ = RX_NPARENS(rx);
473 *p++ = PTR2UV(RX_SUBBEG(rx));
474 *p++ = (UV)RX_SUBLEN(rx);
475 for (i = 0; i <= RX_NPARENS(rx); ++i) {
476 *p++ = (UV)RX_OFFS(rx)[i].start;
477 *p++ = (UV)RX_OFFS(rx)[i].end;
482 S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
487 PERL_ARGS_ASSERT_RXRES_RESTORE;
490 RX_MATCH_COPY_FREE(rx);
491 RX_MATCH_COPIED_set(rx, *p);
494 #ifdef PERL_OLD_COPY_ON_WRITE
495 if (RX_SAVED_COPY(rx))
496 SvREFCNT_dec (RX_SAVED_COPY(rx));
497 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
501 RX_NPARENS(rx) = *p++;
503 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
504 RX_SUBLEN(rx) = (I32)(*p++);
505 for (i = 0; i <= RX_NPARENS(rx); ++i) {
506 RX_OFFS(rx)[i].start = (I32)(*p++);
507 RX_OFFS(rx)[i].end = (I32)(*p++);
512 S_rxres_free(pTHX_ void **rsp)
514 UV * const p = (UV*)*rsp;
516 PERL_ARGS_ASSERT_RXRES_FREE;
521 void *tmp = INT2PTR(char*,*p);
524 PoisonFree(*p, 1, sizeof(*p));
526 Safefree(INT2PTR(char*,*p));
528 #ifdef PERL_OLD_COPY_ON_WRITE
530 SvREFCNT_dec (INT2PTR(SV*,p[1]));
538 #define FORM_NUM_BLANK (1<<30)
539 #define FORM_NUM_POINT (1<<29)
543 dVAR; dSP; dMARK; dORIGMARK;
544 register SV * const tmpForm = *++MARK;
545 SV *formsv; /* contains text of original format */
546 register U32 *fpc; /* format ops program counter */
547 register char *t; /* current append position in target string */
548 const char *f; /* current position in format string */
550 register SV *sv = NULL; /* current item */
551 const char *item = NULL;/* string value of current item */
552 I32 itemsize = 0; /* length of current item, possibly truncated */
553 I32 fieldsize = 0; /* width of current field */
554 I32 lines = 0; /* number of lines that have been output */
555 bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
556 const char *chophere = NULL; /* where to chop current item */
557 STRLEN linemark = 0; /* pos of start of line in output */
559 bool gotsome = FALSE; /* seen at least one non-blank item on this line */
561 STRLEN linemax; /* estimate of output size in bytes */
562 bool item_is_utf8 = FALSE;
563 bool targ_is_utf8 = FALSE;
566 U8 *source; /* source of bytes to append */
567 STRLEN to_copy; /* how may bytes to append */
568 char trans; /* what chars to translate */
570 mg = doparseform(tmpForm);
572 fpc = (U32*)mg->mg_ptr;
573 /* the actual string the format was compiled from.
574 * with overload etc, this may not match tmpForm */
578 SvPV_force(PL_formtarget, len);
579 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
580 SvTAINTED_on(PL_formtarget);
581 if (DO_UTF8(PL_formtarget))
583 linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
584 t = SvGROW(PL_formtarget, len + linemax + 1);
585 /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */
587 f = SvPV_const(formsv, len);
591 const char *name = "???";
594 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
595 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
596 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
597 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
598 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
600 case FF_CHECKNL: name = "CHECKNL"; break;
601 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
602 case FF_SPACE: name = "SPACE"; break;
603 case FF_HALFSPACE: name = "HALFSPACE"; break;
604 case FF_ITEM: name = "ITEM"; break;
605 case FF_CHOP: name = "CHOP"; break;
606 case FF_LINEGLOB: name = "LINEGLOB"; break;
607 case FF_NEWLINE: name = "NEWLINE"; break;
608 case FF_MORE: name = "MORE"; break;
609 case FF_LINEMARK: name = "LINEMARK"; break;
610 case FF_END: name = "END"; break;
611 case FF_0DECIMAL: name = "0DECIMAL"; break;
612 case FF_LINESNGL: name = "LINESNGL"; break;
615 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
617 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
621 linemark = t - SvPVX(PL_formtarget);
631 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
647 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
650 SvTAINTED_on(PL_formtarget);
656 const char *s = item = SvPV_const(sv, len);
659 itemsize = sv_len_utf8(sv);
660 if (itemsize != (I32)len) {
662 if (itemsize > fieldsize) {
663 itemsize = fieldsize;
664 itembytes = itemsize;
665 sv_pos_u2b(sv, &itembytes, 0);
669 send = chophere = s + itembytes;
679 sv_pos_b2u(sv, &itemsize);
683 item_is_utf8 = FALSE;
684 if (itemsize > fieldsize)
685 itemsize = fieldsize;
686 send = chophere = s + itemsize;
700 const char *s = item = SvPV_const(sv, len);
703 itemsize = sv_len_utf8(sv);
704 if (itemsize != (I32)len) {
706 if (itemsize <= fieldsize) {
707 const char *send = chophere = s + itemsize;
720 itemsize = fieldsize;
721 itembytes = itemsize;
722 sv_pos_u2b(sv, &itembytes, 0);
723 send = chophere = s + itembytes;
724 while (s < send || (s == send && isSPACE(*s))) {
734 if (strchr(PL_chopset, *s))
739 itemsize = chophere - item;
740 sv_pos_b2u(sv, &itemsize);
746 item_is_utf8 = FALSE;
747 if (itemsize <= fieldsize) {
748 const char *const send = chophere = s + itemsize;
761 itemsize = fieldsize;
762 send = chophere = s + itemsize;
763 while (s < send || (s == send && isSPACE(*s))) {
773 if (strchr(PL_chopset, *s))
778 itemsize = chophere - item;
784 arg = fieldsize - itemsize;
793 arg = fieldsize - itemsize;
807 /* convert to_copy from chars to bytes */
811 to_copy = s - source;
817 const char *s = chophere;
831 const bool oneline = fpc[-1] == FF_LINESNGL;
832 const char *s = item = SvPV_const(sv, len);
833 const char *const send = s + len;
835 item_is_utf8 = DO_UTF8(sv);
846 to_copy = s - SvPVX_const(sv) - 1;
860 /* append to_copy bytes from source to PL_formstring.
861 * item_is_utf8 implies source is utf8.
862 * if trans, translate certain characters during the copy */
867 SvCUR_set(PL_formtarget,
868 t - SvPVX_const(PL_formtarget));
870 if (targ_is_utf8 && !item_is_utf8) {
871 source = tmp = bytes_to_utf8(source, &to_copy);
873 if (item_is_utf8 && !targ_is_utf8) {
875 /* Upgrade targ to UTF8, and then we reduce it to
876 a problem we have a simple solution for.
877 Don't need get magic. */
878 sv_utf8_upgrade_nomg(PL_formtarget);
880 /* re-calculate linemark */
881 s = (U8*)SvPVX(PL_formtarget);
882 /* the bytes we initially allocated to append the
883 * whole line may have been gobbled up during the
884 * upgrade, so allocate a whole new line's worth
889 linemark = s - (U8*)SvPVX(PL_formtarget);
891 /* Easy. They agree. */
892 assert (item_is_utf8 == targ_is_utf8);
895 /* @* and ^* are the only things that can exceed
896 * the linemax, so grow by the output size, plus
897 * a whole new form's worth in case of any further
899 grow = linemax + to_copy;
901 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1);
902 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
904 Copy(source, t, to_copy, char);
906 /* blank out ~ or control chars, depending on trans.
907 * works on bytes not chars, so relies on not
908 * matching utf8 continuation bytes */
910 U8 *send = s + to_copy;
913 if (trans == '~' ? (ch == '~') :
926 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
934 #if defined(USE_LONG_DOUBLE)
936 ((arg & FORM_NUM_POINT) ?
937 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
940 ((arg & FORM_NUM_POINT) ?
941 "%#0*.*f" : "%0*.*f");
946 #if defined(USE_LONG_DOUBLE)
948 ((arg & FORM_NUM_POINT) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
951 ((arg & FORM_NUM_POINT) ? "%#*.*f" : "%*.*f");
954 /* If the field is marked with ^ and the value is undefined,
956 if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) {
964 /* overflow evidence */
965 if (num_overflow(value, fieldsize, arg)) {
971 /* Formats aren't yet marked for locales, so assume "yes". */
973 STORE_NUMERIC_STANDARD_SET_LOCAL();
974 arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
975 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg, value);
976 RESTORE_NUMERIC_STANDARD();
983 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
991 if (arg) { /* repeat until fields exhausted? */
997 t = SvPVX(PL_formtarget) + linemark;
1004 const char *s = chophere;
1005 const char *send = item + len;
1007 while (isSPACE(*s) && (s < send))
1012 arg = fieldsize - itemsize;
1019 if (strnEQ(s1," ",3)) {
1020 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1031 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
1033 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1035 SvUTF8_on(PL_formtarget);
1036 FmLINES(PL_formtarget) += lines;
1038 if (fpc[-1] == FF_BLANK)
1039 RETURNOP(cLISTOP->op_first);
1051 if (PL_stack_base + *PL_markstack_ptr == SP) {
1053 if (GIMME_V == G_SCALAR)
1055 RETURNOP(PL_op->op_next->op_next);
1057 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
1058 Perl_pp_pushmark(aTHX); /* push dst */
1059 Perl_pp_pushmark(aTHX); /* push src */
1060 ENTER_with_name("grep"); /* enter outer scope */
1063 if (PL_op->op_private & OPpGREP_LEX)
1064 SAVESPTR(PAD_SVl(PL_op->op_targ));
1067 ENTER_with_name("grep_item"); /* enter inner scope */
1070 src = PL_stack_base[*PL_markstack_ptr];
1072 if (PL_op->op_private & OPpGREP_LEX)
1073 PAD_SVl(PL_op->op_targ) = src;
1078 if (PL_op->op_type == OP_MAPSTART)
1079 Perl_pp_pushmark(aTHX); /* push top */
1080 return ((LOGOP*)PL_op->op_next)->op_other;
1086 const I32 gimme = GIMME_V;
1087 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1093 /* first, move source pointer to the next item in the source list */
1094 ++PL_markstack_ptr[-1];
1096 /* if there are new items, push them into the destination list */
1097 if (items && gimme != G_VOID) {
1098 /* might need to make room back there first */
1099 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1100 /* XXX this implementation is very pessimal because the stack
1101 * is repeatedly extended for every set of items. Is possible
1102 * to do this without any stack extension or copying at all
1103 * by maintaining a separate list over which the map iterates
1104 * (like foreach does). --gsar */
1106 /* everything in the stack after the destination list moves
1107 * towards the end the stack by the amount of room needed */
1108 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1110 /* items to shift up (accounting for the moved source pointer) */
1111 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1113 /* This optimization is by Ben Tilly and it does
1114 * things differently from what Sarathy (gsar)
1115 * is describing. The downside of this optimization is
1116 * that leaves "holes" (uninitialized and hopefully unused areas)
1117 * to the Perl stack, but on the other hand this
1118 * shouldn't be a problem. If Sarathy's idea gets
1119 * implemented, this optimization should become
1120 * irrelevant. --jhi */
1122 shift = count; /* Avoid shifting too often --Ben Tilly */
1126 dst = (SP += shift);
1127 PL_markstack_ptr[-1] += shift;
1128 *PL_markstack_ptr += shift;
1132 /* copy the new items down to the destination list */
1133 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1134 if (gimme == G_ARRAY) {
1135 /* add returned items to the collection (making mortal copies
1136 * if necessary), then clear the current temps stack frame
1137 * *except* for those items. We do this splicing the items
1138 * into the start of the tmps frame (so some items may be on
1139 * the tmps stack twice), then moving PL_tmps_floor above
1140 * them, then freeing the frame. That way, the only tmps that
1141 * accumulate over iterations are the return values for map.
1142 * We have to do to this way so that everything gets correctly
1143 * freed if we die during the map.
1147 /* make space for the slice */
1148 EXTEND_MORTAL(items);
1149 tmpsbase = PL_tmps_floor + 1;
1150 Move(PL_tmps_stack + tmpsbase,
1151 PL_tmps_stack + tmpsbase + items,
1152 PL_tmps_ix - PL_tmps_floor,
1154 PL_tmps_ix += items;
1159 sv = sv_mortalcopy(sv);
1161 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1163 /* clear the stack frame except for the items */
1164 PL_tmps_floor += items;
1166 /* FREETMPS may have cleared the TEMP flag on some of the items */
1169 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1172 /* scalar context: we don't care about which values map returns
1173 * (we use undef here). And so we certainly don't want to do mortal
1174 * copies of meaningless values. */
1175 while (items-- > 0) {
1177 *dst-- = &PL_sv_undef;
1185 LEAVE_with_name("grep_item"); /* exit inner scope */
1188 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1190 (void)POPMARK; /* pop top */
1191 LEAVE_with_name("grep"); /* exit outer scope */
1192 (void)POPMARK; /* pop src */
1193 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1194 (void)POPMARK; /* pop dst */
1195 SP = PL_stack_base + POPMARK; /* pop original mark */
1196 if (gimme == G_SCALAR) {
1197 if (PL_op->op_private & OPpGREP_LEX) {
1198 SV* sv = sv_newmortal();
1199 sv_setiv(sv, items);
1207 else if (gimme == G_ARRAY)
1214 ENTER_with_name("grep_item"); /* enter inner scope */
1217 /* set $_ to the new source item */
1218 src = PL_stack_base[PL_markstack_ptr[-1]];
1220 if (PL_op->op_private & OPpGREP_LEX)
1221 PAD_SVl(PL_op->op_targ) = src;
1225 RETURNOP(cLOGOP->op_other);
1234 if (GIMME == G_ARRAY)
1236 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1237 return cLOGOP->op_other;
1247 if (GIMME == G_ARRAY) {
1248 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1252 SV * const targ = PAD_SV(PL_op->op_targ);
1255 if (PL_op->op_private & OPpFLIP_LINENUM) {
1256 if (GvIO(PL_last_in_gv)) {
1257 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1260 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1262 flip = SvIV(sv) == SvIV(GvSV(gv));
1268 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1269 if (PL_op->op_flags & OPf_SPECIAL) {
1277 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1280 sv_setpvs(TARG, "");
1286 /* This code tries to decide if "$left .. $right" should use the
1287 magical string increment, or if the range is numeric (we make
1288 an exception for .."0" [#18165]). AMS 20021031. */
1290 #define RANGE_IS_NUMERIC(left,right) ( \
1291 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1292 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1293 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1294 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1295 && (!SvOK(right) || looks_like_number(right))))
1301 if (GIMME == G_ARRAY) {
1307 if (RANGE_IS_NUMERIC(left,right)) {
1310 if ((SvOK(left) && SvNV_nomg(left) < IV_MIN) ||
1311 (SvOK(right) && SvNV_nomg(right) > IV_MAX))
1312 DIE(aTHX_ "Range iterator outside integer range");
1313 i = SvIV_nomg(left);
1314 max = SvIV_nomg(right);
1323 SV * const sv = sv_2mortal(newSViv(i++));
1329 const char * const lpv = SvPV_nomg_const(left, llen);
1330 const char * const tmps = SvPV_nomg_const(right, len);
1332 SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP);
1333 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1335 if (strEQ(SvPVX_const(sv),tmps))
1337 sv = sv_2mortal(newSVsv(sv));
1344 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1348 if (PL_op->op_private & OPpFLIP_LINENUM) {
1349 if (GvIO(PL_last_in_gv)) {
1350 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1353 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1354 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1362 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1363 sv_catpvs(targ, "E0");
1373 static const char * const context_name[] = {
1375 NULL, /* CXt_WHEN never actually needs "block" */
1376 NULL, /* CXt_BLOCK never actually needs "block" */
1377 NULL, /* CXt_GIVEN never actually needs "block" */
1378 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1379 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1380 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1381 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1389 S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags)
1394 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1396 for (i = cxstack_ix; i >= 0; i--) {
1397 register const PERL_CONTEXT * const cx = &cxstack[i];
1398 switch (CxTYPE(cx)) {
1404 /* diag_listed_as: Exiting subroutine via %s */
1405 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1406 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1407 if (CxTYPE(cx) == CXt_NULL)
1410 case CXt_LOOP_LAZYIV:
1411 case CXt_LOOP_LAZYSV:
1413 case CXt_LOOP_PLAIN:
1415 STRLEN cx_label_len = 0;
1416 U32 cx_label_flags = 0;
1417 const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags);
1419 ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
1422 (const U8*)cx_label, cx_label_len,
1423 (const U8*)label, len) == 0)
1425 (const U8*)label, len,
1426 (const U8*)cx_label, cx_label_len) == 0)
1427 : (len == cx_label_len && ((cx_label == label)
1428 || memEQ(cx_label, label, len))) )) {
1429 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1430 (long)i, cx_label));
1433 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1444 Perl_dowantarray(pTHX)
1447 const I32 gimme = block_gimme();
1448 return (gimme == G_VOID) ? G_SCALAR : gimme;
1452 Perl_block_gimme(pTHX)
1455 const I32 cxix = dopoptosub(cxstack_ix);
1459 switch (cxstack[cxix].blk_gimme) {
1467 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1474 Perl_is_lvalue_sub(pTHX)
1477 const I32 cxix = dopoptosub(cxstack_ix);
1478 assert(cxix >= 0); /* We should only be called from inside subs */
1480 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1481 return CxLVAL(cxstack + cxix);
1486 /* only used by PUSHSUB */
1488 Perl_was_lvalue_sub(pTHX)
1491 const I32 cxix = dopoptosub(cxstack_ix-1);
1492 assert(cxix >= 0); /* We should only be called from inside subs */
1494 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1495 return CxLVAL(cxstack + cxix);
1501 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1506 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1508 for (i = startingblock; i >= 0; i--) {
1509 register const PERL_CONTEXT * const cx = &cxstk[i];
1510 switch (CxTYPE(cx)) {
1516 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1524 S_dopoptoeval(pTHX_ I32 startingblock)
1528 for (i = startingblock; i >= 0; i--) {
1529 register const PERL_CONTEXT *cx = &cxstack[i];
1530 switch (CxTYPE(cx)) {
1534 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1542 S_dopoptoloop(pTHX_ I32 startingblock)
1546 for (i = startingblock; i >= 0; i--) {
1547 register const PERL_CONTEXT * const cx = &cxstack[i];
1548 switch (CxTYPE(cx)) {
1554 /* diag_listed_as: Exiting subroutine via %s */
1555 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1556 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1557 if ((CxTYPE(cx)) == CXt_NULL)
1560 case CXt_LOOP_LAZYIV:
1561 case CXt_LOOP_LAZYSV:
1563 case CXt_LOOP_PLAIN:
1564 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1572 S_dopoptogiven(pTHX_ I32 startingblock)
1576 for (i = startingblock; i >= 0; i--) {
1577 register const PERL_CONTEXT *cx = &cxstack[i];
1578 switch (CxTYPE(cx)) {
1582 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
1584 case CXt_LOOP_PLAIN:
1585 assert(!CxFOREACHDEF(cx));
1587 case CXt_LOOP_LAZYIV:
1588 case CXt_LOOP_LAZYSV:
1590 if (CxFOREACHDEF(cx)) {
1591 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
1600 S_dopoptowhen(pTHX_ I32 startingblock)
1604 for (i = startingblock; i >= 0; i--) {
1605 register const PERL_CONTEXT *cx = &cxstack[i];
1606 switch (CxTYPE(cx)) {
1610 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1618 Perl_dounwind(pTHX_ I32 cxix)
1623 if (!PL_curstackinfo) /* can happen if die during thread cloning */
1626 while (cxstack_ix > cxix) {
1628 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1629 DEBUG_CX("UNWIND"); \
1630 /* Note: we don't need to restore the base context info till the end. */
1631 switch (CxTYPE(cx)) {
1634 continue; /* not break */
1642 case CXt_LOOP_LAZYIV:
1643 case CXt_LOOP_LAZYSV:
1645 case CXt_LOOP_PLAIN:
1656 PERL_UNUSED_VAR(optype);
1660 Perl_qerror(pTHX_ SV *err)
1664 PERL_ARGS_ASSERT_QERROR;
1667 if (PL_in_eval & EVAL_KEEPERR) {
1668 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
1672 sv_catsv(ERRSV, err);
1675 sv_catsv(PL_errors, err);
1677 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
1679 ++PL_parser->error_count;
1683 Perl_die_unwind(pTHX_ SV *msv)
1686 SV *exceptsv = sv_mortalcopy(msv);
1687 U8 in_eval = PL_in_eval;
1688 PERL_ARGS_ASSERT_DIE_UNWIND;
1695 * Historically, perl used to set ERRSV ($@) early in the die
1696 * process and rely on it not getting clobbered during unwinding.
1697 * That sucked, because it was liable to get clobbered, so the
1698 * setting of ERRSV used to emit the exception from eval{} has
1699 * been moved to much later, after unwinding (see just before
1700 * JMPENV_JUMP below). However, some modules were relying on the
1701 * early setting, by examining $@ during unwinding to use it as
1702 * a flag indicating whether the current unwinding was caused by
1703 * an exception. It was never a reliable flag for that purpose,
1704 * being totally open to false positives even without actual
1705 * clobberage, but was useful enough for production code to
1706 * semantically rely on it.
1708 * We'd like to have a proper introspective interface that
1709 * explicitly describes the reason for whatever unwinding
1710 * operations are currently in progress, so that those modules
1711 * work reliably and $@ isn't further overloaded. But we don't
1712 * have one yet. In its absence, as a stopgap measure, ERRSV is
1713 * now *additionally* set here, before unwinding, to serve as the
1714 * (unreliable) flag that it used to.
1716 * This behaviour is temporary, and should be removed when a
1717 * proper way to detect exceptional unwinding has been developed.
1718 * As of 2010-12, the authors of modules relying on the hack
1719 * are aware of the issue, because the modules failed on
1720 * perls 5.13.{1..7} which had late setting of $@ without this
1721 * early-setting hack.
1723 if (!(in_eval & EVAL_KEEPERR)) {
1724 SvTEMP_off(exceptsv);
1725 sv_setsv(ERRSV, exceptsv);
1728 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1729 && PL_curstackinfo->si_prev)
1738 register PERL_CONTEXT *cx;
1741 JMPENV *restartjmpenv;
1744 if (cxix < cxstack_ix)
1747 POPBLOCK(cx,PL_curpm);
1748 if (CxTYPE(cx) != CXt_EVAL) {
1750 const char* message = SvPVx_const(exceptsv, msglen);
1751 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
1752 PerlIO_write(Perl_error_log, message, msglen);
1756 namesv = cx->blk_eval.old_namesv;
1757 oldcop = cx->blk_oldcop;
1758 restartjmpenv = cx->blk_eval.cur_top_env;
1759 restartop = cx->blk_eval.retop;
1761 if (gimme == G_SCALAR)
1762 *++newsp = &PL_sv_undef;
1763 PL_stack_sp = newsp;
1767 /* LEAVE could clobber PL_curcop (see save_re_context())
1768 * XXX it might be better to find a way to avoid messing with
1769 * PL_curcop in save_re_context() instead, but this is a more
1770 * minimal fix --GSAR */
1773 if (optype == OP_REQUIRE) {
1774 (void)hv_store(GvHVn(PL_incgv),
1775 SvPVX_const(namesv),
1776 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
1778 /* note that unlike pp_entereval, pp_require isn't
1779 * supposed to trap errors. So now that we've popped the
1780 * EVAL that pp_require pushed, and processed the error
1781 * message, rethrow the error */
1782 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
1783 SVfARG(exceptsv ? exceptsv : newSVpvs_flags("Unknown error\n",
1786 if (in_eval & EVAL_KEEPERR) {
1787 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
1791 sv_setsv(ERRSV, exceptsv);
1793 PL_restartjmpenv = restartjmpenv;
1794 PL_restartop = restartop;
1800 write_to_stderr(exceptsv);
1807 dVAR; dSP; dPOPTOPssrl;
1808 if (SvTRUE(left) != SvTRUE(right))
1815 =for apidoc caller_cx
1817 The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1818 returned C<PERL_CONTEXT> structure can be interrogated to find all the
1819 information returned to Perl by C<caller>. Note that XSUBs don't get a
1820 stack frame, so C<caller_cx(0, NULL)> will return information for the
1821 immediately-surrounding Perl code.
1823 This function skips over the automatic calls to C<&DB::sub> made on the
1824 behalf of the debugger. If the stack frame requested was a sub called by
1825 C<DB::sub>, the return value will be the frame for the call to
1826 C<DB::sub>, since that has the correct line number/etc. for the call
1827 site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1828 frame for the sub call itself.
1833 const PERL_CONTEXT *
1834 Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
1836 register I32 cxix = dopoptosub(cxstack_ix);
1837 register const PERL_CONTEXT *cx;
1838 register const PERL_CONTEXT *ccstack = cxstack;
1839 const PERL_SI *top_si = PL_curstackinfo;
1842 /* we may be in a higher stacklevel, so dig down deeper */
1843 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1844 top_si = top_si->si_prev;
1845 ccstack = top_si->si_cxstack;
1846 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1850 /* caller() should not report the automatic calls to &DB::sub */
1851 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1852 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1856 cxix = dopoptosub_at(ccstack, cxix - 1);
1859 cx = &ccstack[cxix];
1860 if (dbcxp) *dbcxp = cx;
1862 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1863 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1864 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1865 field below is defined for any cx. */
1866 /* caller() should not report the automatic calls to &DB::sub */
1867 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1868 cx = &ccstack[dbcxix];
1878 register const PERL_CONTEXT *cx;
1879 const PERL_CONTEXT *dbcx;
1881 const HEK *stash_hek;
1883 bool has_arg = MAXARG && TOPs;
1891 cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx);
1893 if (GIMME != G_ARRAY) {
1900 stash_hek = HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop));
1901 if (GIMME != G_ARRAY) {
1904 PUSHs(&PL_sv_undef);
1907 sv_sethek(TARG, stash_hek);
1916 PUSHs(&PL_sv_undef);
1919 sv_sethek(TARG, stash_hek);
1922 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1923 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1926 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1927 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
1928 /* So is ccstack[dbcxix]. */
1930 SV * const sv = newSV(0);
1931 gv_efullname3(sv, cvgv, NULL);
1933 PUSHs(boolSV(CxHASARGS(cx)));
1936 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1937 PUSHs(boolSV(CxHASARGS(cx)));
1941 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1944 gimme = (I32)cx->blk_gimme;
1945 if (gimme == G_VOID)
1946 PUSHs(&PL_sv_undef);
1948 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1949 if (CxTYPE(cx) == CXt_EVAL) {
1951 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1952 PUSHs(cx->blk_eval.cur_text);
1956 else if (cx->blk_eval.old_namesv) {
1957 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1960 /* eval BLOCK (try blocks have old_namesv == 0) */
1962 PUSHs(&PL_sv_undef);
1963 PUSHs(&PL_sv_undef);
1967 PUSHs(&PL_sv_undef);
1968 PUSHs(&PL_sv_undef);
1970 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1971 && CopSTASH_eq(PL_curcop, PL_debstash))
1973 AV * const ary = cx->blk_sub.argarray;
1974 const int off = AvARRAY(ary) - AvALLOC(ary);
1976 Perl_init_dbargs(aTHX);
1978 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1979 av_extend(PL_dbargs, AvFILLp(ary) + off);
1980 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1981 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1983 /* XXX only hints propagated via op_private are currently
1984 * visible (others are not easily accessible, since they
1985 * use the global PL_hints) */
1986 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1989 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1991 if (old_warnings == pWARN_NONE ||
1992 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1993 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1994 else if (old_warnings == pWARN_ALL ||
1995 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1996 /* Get the bit mask for $warnings::Bits{all}, because
1997 * it could have been extended by warnings::register */
1999 HV * const bits = get_hv("warnings::Bits", 0);
2000 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
2001 mask = newSVsv(*bits_all);
2004 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
2008 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
2012 PUSHs(cx->blk_oldcop->cop_hints_hash ?
2013 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
2022 const char * const tmps =
2023 (MAXARG < 1 || (!TOPs && !POPs)) ? (const char *)"" : POPpconstx;
2024 sv_reset(tmps, CopSTASH(PL_curcop));
2029 /* like pp_nextstate, but used instead when the debugger is active */
2034 PL_curcop = (COP*)PL_op;
2035 TAINT_NOT; /* Each statement is presumed innocent */
2036 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
2041 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
2042 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
2045 register PERL_CONTEXT *cx;
2046 const I32 gimme = G_ARRAY;
2048 GV * const gv = PL_DBgv;
2049 register CV * const cv = GvCV(gv);
2052 DIE(aTHX_ "No DB::DB routine defined");
2054 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2055 /* don't do recursive DB::DB call */
2070 (void)(*CvXSUB(cv))(aTHX_ cv);
2077 PUSHBLOCK(cx, CXt_SUB, SP);
2079 cx->blk_sub.retop = PL_op->op_next;
2082 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
2083 RETURNOP(CvSTART(cv));
2091 S_adjust_stack_on_leave(pTHX_ SV **newsp, SV **sp, SV **mark, I32 gimme, U32 flags)
2094 PERL_ARGS_ASSERT_ADJUST_STACK_ON_LEAVE;
2096 if (flags & SVs_PADTMP) {
2097 flags &= ~SVs_PADTMP;
2100 if (gimme == G_SCALAR) {
2102 *++newsp = ((SvFLAGS(*SP) & flags) || (padtmp && SvPADTMP(*SP)))
2103 ? *SP : sv_mortalcopy(*SP);
2105 /* MEXTEND() only updates MARK, so reuse it instead of newsp. */
2108 *++MARK = &PL_sv_undef;
2112 else if (gimme == G_ARRAY) {
2113 /* in case LEAVE wipes old return values */
2114 while (++MARK <= SP) {
2115 if ((SvFLAGS(*MARK) & flags) || (padtmp && SvPADTMP(*MARK)))
2118 *++newsp = sv_mortalcopy(*MARK);
2119 TAINT_NOT; /* Each item is independent */
2122 /* When this function was called with MARK == newsp, we reach this
2123 * point with SP == newsp. */
2132 register PERL_CONTEXT *cx;
2133 I32 gimme = GIMME_V;
2135 ENTER_with_name("block");
2138 PUSHBLOCK(cx, CXt_BLOCK, SP);
2146 register PERL_CONTEXT *cx;
2151 if (PL_op->op_flags & OPf_SPECIAL) {
2152 cx = &cxstack[cxstack_ix];
2153 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
2158 gimme = OP_GIMME(PL_op, (cxstack_ix >= 0) ? gimme : G_SCALAR);
2161 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
2162 PL_curpm = newpm; /* Don't pop $1 et al till now */
2164 LEAVE_with_name("block");
2172 register PERL_CONTEXT *cx;
2173 const I32 gimme = GIMME_V;
2174 void *itervar; /* location of the iteration variable */
2175 U8 cxtype = CXt_LOOP_FOR;
2177 ENTER_with_name("loop1");
2180 if (PL_op->op_targ) { /* "my" variable */
2181 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
2182 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2183 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2184 SVs_PADSTALE, SVs_PADSTALE);
2186 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
2188 itervar = PL_comppad;
2190 itervar = &PAD_SVl(PL_op->op_targ);
2193 else { /* symbol table variable */
2194 GV * const gv = MUTABLE_GV(POPs);
2195 SV** svp = &GvSV(gv);
2196 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
2198 itervar = (void *)gv;
2201 if (PL_op->op_private & OPpITER_DEF)
2202 cxtype |= CXp_FOR_DEF;
2204 ENTER_with_name("loop2");
2206 PUSHBLOCK(cx, cxtype, SP);
2207 PUSHLOOP_FOR(cx, itervar, MARK);
2208 if (PL_op->op_flags & OPf_STACKED) {
2209 SV *maybe_ary = POPs;
2210 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2212 SV * const right = maybe_ary;
2215 if (RANGE_IS_NUMERIC(sv,right)) {
2216 cx->cx_type &= ~CXTYPEMASK;
2217 cx->cx_type |= CXt_LOOP_LAZYIV;
2218 /* Make sure that no-one re-orders cop.h and breaks our
2220 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
2221 #ifdef NV_PRESERVES_UV
2222 if ((SvOK(sv) && ((SvNV_nomg(sv) < (NV)IV_MIN) ||
2223 (SvNV_nomg(sv) > (NV)IV_MAX)))
2225 (SvOK(right) && ((SvNV_nomg(right) > (NV)IV_MAX) ||
2226 (SvNV_nomg(right) < (NV)IV_MIN))))
2228 if ((SvOK(sv) && ((SvNV_nomg(sv) <= (NV)IV_MIN)
2230 ((SvNV_nomg(sv) > 0) &&
2231 ((SvUV_nomg(sv) > (UV)IV_MAX) ||
2232 (SvNV_nomg(sv) > (NV)UV_MAX)))))
2234 (SvOK(right) && ((SvNV_nomg(right) <= (NV)IV_MIN)
2236 ((SvNV_nomg(right) > 0) &&
2237 ((SvUV_nomg(right) > (UV)IV_MAX) ||
2238 (SvNV_nomg(right) > (NV)UV_MAX))
2241 DIE(aTHX_ "Range iterator outside integer range");
2242 cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv);
2243 cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right);
2245 /* for correct -Dstv display */
2246 cx->blk_oldsp = sp - PL_stack_base;
2250 cx->cx_type &= ~CXTYPEMASK;
2251 cx->cx_type |= CXt_LOOP_LAZYSV;
2252 /* Make sure that no-one re-orders cop.h and breaks our
2254 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2255 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2256 cx->blk_loop.state_u.lazysv.end = right;
2257 SvREFCNT_inc(right);
2258 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2259 /* This will do the upgrade to SVt_PV, and warn if the value
2260 is uninitialised. */
2261 (void) SvPV_nolen_const(right);
2262 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2263 to replace !SvOK() with a pointer to "". */
2265 SvREFCNT_dec(right);
2266 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2270 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2271 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2272 SvREFCNT_inc(maybe_ary);
2273 cx->blk_loop.state_u.ary.ix =
2274 (PL_op->op_private & OPpITER_REVERSED) ?
2275 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2279 else { /* iterating over items on the stack */
2280 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2281 if (PL_op->op_private & OPpITER_REVERSED) {
2282 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2285 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2295 register PERL_CONTEXT *cx;
2296 const I32 gimme = GIMME_V;
2298 ENTER_with_name("loop1");
2300 ENTER_with_name("loop2");
2302 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2303 PUSHLOOP_PLAIN(cx, SP);
2311 register PERL_CONTEXT *cx;
2318 assert(CxTYPE_is_LOOP(cx));
2320 newsp = PL_stack_base + cx->blk_loop.resetsp;
2323 SP = adjust_stack_on_leave(newsp, SP, MARK, gimme, 0);
2326 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2327 PL_curpm = newpm; /* ... and pop $1 et al */
2329 LEAVE_with_name("loop2");
2330 LEAVE_with_name("loop1");
2336 S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
2337 PERL_CONTEXT *cx, PMOP *newpm)
2339 const bool ref = !!(CxLVAL(cx) & OPpENTERSUB_INARGS);
2340 if (gimme == G_SCALAR) {
2341 if (CxLVAL(cx) && !ref) { /* Leave it as it is if we can. */
2343 const char *what = NULL;
2345 assert(MARK+1 == SP);
2346 if ((SvPADTMP(TOPs) ||
2347 (SvFLAGS(TOPs) & (SVf_READONLY | SVf_FAKE))
2350 !SvSMAGICAL(TOPs)) {
2352 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2353 : "a readonly value" : "a temporary";
2358 /* sub:lvalue{} will take us here. */
2367 "Can't return %s from lvalue subroutine", what
2372 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2373 *++newsp = SvREFCNT_inc(*SP);
2380 ? sv_2mortal(SvREFCNT_inc_simple_NN(*SP))
2385 *++newsp = &PL_sv_undef;
2387 if (CxLVAL(cx) & OPpDEREF) {
2390 TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF);
2394 else if (gimme == G_ARRAY) {
2395 assert (!(CxLVAL(cx) & OPpDEREF));
2396 if (ref || !CxLVAL(cx))
2397 while (++MARK <= SP)
2401 : ref && SvFLAGS(*MARK) & SVs_PADTMP
2402 ? sv_mortalcopy(*MARK)
2403 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
2404 else while (++MARK <= SP) {
2405 if (*MARK != &PL_sv_undef
2407 || (SvFLAGS(*MARK) & (SVf_READONLY|SVf_FAKE))
2412 /* Might be flattened array after $#array = */
2419 /* diag_listed_as: Can't return %s from lvalue subroutine */
2421 "Can't return a %s from lvalue subroutine",
2422 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2428 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
2431 PL_stack_sp = newsp;
2437 register PERL_CONTEXT *cx;
2438 bool popsub2 = FALSE;
2439 bool clear_errsv = FALSE;
2449 const I32 cxix = dopoptosub(cxstack_ix);
2452 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2453 * sort block, which is a CXt_NULL
2456 PL_stack_base[1] = *PL_stack_sp;
2457 PL_stack_sp = PL_stack_base + 1;
2461 DIE(aTHX_ "Can't return outside a subroutine");
2463 if (cxix < cxstack_ix)
2466 if (CxMULTICALL(&cxstack[cxix])) {
2467 gimme = cxstack[cxix].blk_gimme;
2468 if (gimme == G_VOID)
2469 PL_stack_sp = PL_stack_base;
2470 else if (gimme == G_SCALAR) {
2471 PL_stack_base[1] = *PL_stack_sp;
2472 PL_stack_sp = PL_stack_base + 1;
2478 switch (CxTYPE(cx)) {
2481 lval = !!CvLVALUE(cx->blk_sub.cv);
2482 retop = cx->blk_sub.retop;
2483 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2486 if (!(PL_in_eval & EVAL_KEEPERR))
2489 namesv = cx->blk_eval.old_namesv;
2490 retop = cx->blk_eval.retop;
2493 if (optype == OP_REQUIRE &&
2494 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2496 /* Unassume the success we assumed earlier. */
2497 (void)hv_delete(GvHVn(PL_incgv),
2498 SvPVX_const(namesv),
2499 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
2501 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
2506 retop = cx->blk_sub.retop;
2509 DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx));
2513 if (lval) S_return_lvalues(aTHX_ MARK, SP, newsp, gimme, cx, newpm);
2515 if (gimme == G_SCALAR) {
2518 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2519 if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1
2520 && !SvMAGICAL(TOPs)) {
2521 *++newsp = SvREFCNT_inc(*SP);
2526 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2528 *++newsp = sv_mortalcopy(sv);
2532 else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1
2533 && !SvMAGICAL(*SP)) {
2537 *++newsp = sv_mortalcopy(*SP);
2540 *++newsp = sv_mortalcopy(*SP);
2543 *++newsp = &PL_sv_undef;
2545 else if (gimme == G_ARRAY) {
2546 while (++MARK <= SP) {
2547 *++newsp = popsub2 && SvTEMP(*MARK) && SvREFCNT(*MARK) == 1
2548 && !SvGMAGICAL(*MARK)
2549 ? *MARK : sv_mortalcopy(*MARK);
2550 TAINT_NOT; /* Each item is independent */
2553 PL_stack_sp = newsp;
2557 /* Stack values are safe: */
2560 POPSUB(cx,sv); /* release CV and @_ ... */
2564 PL_curpm = newpm; /* ... and pop $1 et al */
2573 /* This duplicates parts of pp_leavesub, so that it can share code with
2581 register PERL_CONTEXT *cx;
2584 if (CxMULTICALL(&cxstack[cxstack_ix]))
2588 cxstack_ix++; /* temporarily protect top context */
2592 S_return_lvalues(aTHX_ newsp, SP, newsp, gimme, cx, newpm);
2596 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2597 PL_curpm = newpm; /* ... and pop $1 et al */
2600 return cx->blk_sub.retop;
2607 register PERL_CONTEXT *cx;
2618 if (PL_op->op_flags & OPf_SPECIAL) {
2619 cxix = dopoptoloop(cxstack_ix);
2621 DIE(aTHX_ "Can't \"last\" outside a loop block");
2624 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2625 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
2627 DIE(aTHX_ "Label not found for \"last %"SVf"\"",
2628 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2629 strlen(cPVOP->op_pv),
2630 ((cPVOP->op_private & OPpPV_IS_UTF8)
2631 ? SVf_UTF8 : 0) | SVs_TEMP)));
2633 if (cxix < cxstack_ix)
2637 cxstack_ix++; /* temporarily protect top context */
2639 switch (CxTYPE(cx)) {
2640 case CXt_LOOP_LAZYIV:
2641 case CXt_LOOP_LAZYSV:
2643 case CXt_LOOP_PLAIN:
2645 newsp = PL_stack_base + cx->blk_loop.resetsp;
2646 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2650 nextop = cx->blk_sub.retop;
2654 nextop = cx->blk_eval.retop;
2658 nextop = cx->blk_sub.retop;
2661 DIE(aTHX_ "panic: last, type=%u", (unsigned) CxTYPE(cx));
2665 SP = adjust_stack_on_leave(newsp, SP, MARK, gimme,
2666 pop2 == CXt_SUB ? SVs_TEMP : 0);
2671 /* Stack values are safe: */
2673 case CXt_LOOP_LAZYIV:
2674 case CXt_LOOP_PLAIN:
2675 case CXt_LOOP_LAZYSV:
2677 POPLOOP(cx); /* release loop vars ... */
2681 POPSUB(cx,sv); /* release CV and @_ ... */
2684 PL_curpm = newpm; /* ... and pop $1 et al */
2687 PERL_UNUSED_VAR(optype);
2688 PERL_UNUSED_VAR(gimme);
2696 register PERL_CONTEXT *cx;
2699 if (PL_op->op_flags & OPf_SPECIAL) {
2700 cxix = dopoptoloop(cxstack_ix);
2702 DIE(aTHX_ "Can't \"next\" outside a loop block");
2705 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2706 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
2708 DIE(aTHX_ "Label not found for \"next %"SVf"\"",
2709 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2710 strlen(cPVOP->op_pv),
2711 ((cPVOP->op_private & OPpPV_IS_UTF8)
2712 ? SVf_UTF8 : 0) | SVs_TEMP)));
2714 if (cxix < cxstack_ix)
2717 /* clear off anything above the scope we're re-entering, but
2718 * save the rest until after a possible continue block */
2719 inner = PL_scopestack_ix;
2721 if (PL_scopestack_ix < inner)
2722 leave_scope(PL_scopestack[PL_scopestack_ix]);
2723 PL_curcop = cx->blk_oldcop;
2724 return (cx)->blk_loop.my_op->op_nextop;
2731 register PERL_CONTEXT *cx;
2735 if (PL_op->op_flags & OPf_SPECIAL) {
2736 cxix = dopoptoloop(cxstack_ix);
2738 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2741 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2742 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
2744 DIE(aTHX_ "Label not found for \"redo %"SVf"\"",
2745 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2746 strlen(cPVOP->op_pv),
2747 ((cPVOP->op_private & OPpPV_IS_UTF8)
2748 ? SVf_UTF8 : 0) | SVs_TEMP)));
2750 if (cxix < cxstack_ix)
2753 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2754 if (redo_op->op_type == OP_ENTER) {
2755 /* pop one less context to avoid $x being freed in while (my $x..) */
2757 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2758 redo_op = redo_op->op_next;
2762 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2763 LEAVE_SCOPE(oldsave);
2765 PL_curcop = cx->blk_oldcop;
2770 S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit)
2774 static const char too_deep[] = "Target of goto is too deeply nested";
2776 PERL_ARGS_ASSERT_DOFINDLABEL;
2779 Perl_croak(aTHX_ too_deep);
2780 if (o->op_type == OP_LEAVE ||
2781 o->op_type == OP_SCOPE ||
2782 o->op_type == OP_LEAVELOOP ||
2783 o->op_type == OP_LEAVESUB ||
2784 o->op_type == OP_LEAVETRY)
2786 *ops++ = cUNOPo->op_first;
2788 Perl_croak(aTHX_ too_deep);
2791 if (o->op_flags & OPf_KIDS) {
2793 /* First try all the kids at this level, since that's likeliest. */
2794 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2795 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2796 STRLEN kid_label_len;
2797 U32 kid_label_flags;
2798 const char *kid_label = CopLABEL_len_flags(kCOP,
2799 &kid_label_len, &kid_label_flags);
2801 ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
2804 (const U8*)kid_label, kid_label_len,
2805 (const U8*)label, len) == 0)
2807 (const U8*)label, len,
2808 (const U8*)kid_label, kid_label_len) == 0)
2809 : ( len == kid_label_len && ((kid_label == label)
2810 || memEQ(kid_label, label, len)))))
2814 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2815 if (kid == PL_lastgotoprobe)
2817 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2820 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2821 ops[-1]->op_type == OP_DBSTATE)
2826 if ((o = dofindlabel(kid, label, len, flags, ops, oplimit)))
2839 register PERL_CONTEXT *cx;
2840 #define GOTO_DEPTH 64
2841 OP *enterops[GOTO_DEPTH];
2842 const char *label = NULL;
2843 STRLEN label_len = 0;
2844 U32 label_flags = 0;
2845 const bool do_dump = (PL_op->op_type == OP_DUMP);
2846 static const char must_have_label[] = "goto must have label";
2848 if (PL_op->op_flags & OPf_STACKED) {
2849 SV * const sv = POPs;
2851 /* This egregious kludge implements goto &subroutine */
2852 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2854 register PERL_CONTEXT *cx;
2855 CV *cv = MUTABLE_CV(SvRV(sv));
2862 if (!CvROOT(cv) && !CvXSUB(cv)) {
2863 const GV * const gv = CvGV(cv);
2867 /* autoloaded stub? */
2868 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2870 autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv),
2872 GvNAMEUTF8(gv) ? SVf_UTF8 : 0);
2873 if (autogv && (cv = GvCV(autogv)))
2875 tmpstr = sv_newmortal();
2876 gv_efullname3(tmpstr, gv, NULL);
2877 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2879 DIE(aTHX_ "Goto undefined subroutine");
2882 /* First do some returnish stuff. */
2883 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2885 cxix = dopoptosub(cxstack_ix);
2887 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2888 if (cxix < cxstack_ix)
2892 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2893 if (CxTYPE(cx) == CXt_EVAL) {
2895 /* diag_listed_as: Can't goto subroutine from an eval-%s */
2896 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2898 /* diag_listed_as: Can't goto subroutine from an eval-%s */
2899 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2901 else if (CxMULTICALL(cx))
2902 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2903 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2904 /* put @_ back onto stack */
2905 AV* av = cx->blk_sub.argarray;
2907 items = AvFILLp(av) + 1;
2908 EXTEND(SP, items+1); /* @_ could have been extended. */
2909 Copy(AvARRAY(av), SP + 1, items, SV*);
2910 SvREFCNT_dec(GvAV(PL_defgv));
2911 GvAV(PL_defgv) = cx->blk_sub.savearray;
2913 /* abandon @_ if it got reified */
2918 av_extend(av, items-1);
2920 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2923 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2924 AV* const av = GvAV(PL_defgv);
2925 items = AvFILLp(av) + 1;
2926 EXTEND(SP, items+1); /* @_ could have been extended. */
2927 Copy(AvARRAY(av), SP + 1, items, SV*);
2931 if (CxTYPE(cx) == CXt_SUB &&
2932 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2933 SvREFCNT_dec(cx->blk_sub.cv);
2934 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2935 LEAVE_SCOPE(oldsave);
2937 /* A destructor called during LEAVE_SCOPE could have undefined
2938 * our precious cv. See bug #99850. */
2939 if (!CvROOT(cv) && !CvXSUB(cv)) {
2940 const GV * const gv = CvGV(cv);
2942 SV * const tmpstr = sv_newmortal();
2943 gv_efullname3(tmpstr, gv, NULL);
2944 DIE(aTHX_ "Goto undefined subroutine &%"SVf"",
2947 DIE(aTHX_ "Goto undefined subroutine");
2950 /* Now do some callish stuff. */
2952 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2954 OP* const retop = cx->blk_sub.retop;
2955 SV **newsp PERL_UNUSED_DECL;
2956 I32 gimme PERL_UNUSED_DECL;
2959 for (index=0; index<items; index++)
2960 sv_2mortal(SP[-index]);
2963 /* XS subs don't have a CxSUB, so pop it */
2964 POPBLOCK(cx, PL_curpm);
2965 /* Push a mark for the start of arglist */
2968 (void)(*CvXSUB(cv))(aTHX_ cv);
2973 AV* const padlist = CvPADLIST(cv);
2974 if (CxTYPE(cx) == CXt_EVAL) {
2975 PL_in_eval = CxOLD_IN_EVAL(cx);
2976 PL_eval_root = cx->blk_eval.old_eval_root;
2977 cx->cx_type = CXt_SUB;
2979 cx->blk_sub.cv = cv;
2980 cx->blk_sub.olddepth = CvDEPTH(cv);
2983 if (CvDEPTH(cv) < 2)
2984 SvREFCNT_inc_simple_void_NN(cv);
2986 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2987 sub_crush_depth(cv);
2988 pad_push(padlist, CvDEPTH(cv));
2990 PL_curcop = cx->blk_oldcop;
2992 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2995 AV *const av = MUTABLE_AV(PAD_SVl(0));
2997 cx->blk_sub.savearray = GvAV(PL_defgv);
2998 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
2999 CX_CURPAD_SAVE(cx->blk_sub);
3000 cx->blk_sub.argarray = av;
3002 if (items >= AvMAX(av) + 1) {
3003 SV **ary = AvALLOC(av);
3004 if (AvARRAY(av) != ary) {
3005 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
3008 if (items >= AvMAX(av) + 1) {
3009 AvMAX(av) = items - 1;
3010 Renew(ary,items+1,SV*);
3016 Copy(mark,AvARRAY(av),items,SV*);
3017 AvFILLp(av) = items - 1;
3018 assert(!AvREAL(av));
3020 /* transfer 'ownership' of refcnts to new @_ */
3030 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
3031 Perl_get_db_sub(aTHX_ NULL, cv);
3033 CV * const gotocv = get_cvs("DB::goto", 0);
3035 PUSHMARK( PL_stack_sp );
3036 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
3041 RETURNOP(CvSTART(cv));
3045 label = SvPV_const(sv, label_len);
3046 label_flags = SvUTF8(sv);
3047 if (!(do_dump || *label))
3048 DIE(aTHX_ must_have_label);
3051 else if (PL_op->op_flags & OPf_SPECIAL) {
3053 DIE(aTHX_ must_have_label);
3056 label = cPVOP->op_pv;
3057 label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
3058 label_len = strlen(label);
3063 if (label && *label) {
3064 OP *gotoprobe = NULL;
3065 bool leaving_eval = FALSE;
3066 bool in_block = FALSE;
3067 PERL_CONTEXT *last_eval_cx = NULL;
3071 PL_lastgotoprobe = NULL;
3073 for (ix = cxstack_ix; ix >= 0; ix--) {
3075 switch (CxTYPE(cx)) {
3077 leaving_eval = TRUE;
3078 if (!CxTRYBLOCK(cx)) {
3079 gotoprobe = (last_eval_cx ?
3080 last_eval_cx->blk_eval.old_eval_root :
3085 /* else fall through */
3086 case CXt_LOOP_LAZYIV:
3087 case CXt_LOOP_LAZYSV:
3089 case CXt_LOOP_PLAIN:
3092 gotoprobe = cx->blk_oldcop->op_sibling;
3098 gotoprobe = cx->blk_oldcop->op_sibling;
3101 gotoprobe = PL_main_root;
3104 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
3105 gotoprobe = CvROOT(cx->blk_sub.cv);
3111 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
3114 DIE(aTHX_ "panic: goto, type=%u, ix=%ld",
3115 CxTYPE(cx), (long) ix);
3116 gotoprobe = PL_main_root;
3120 retop = dofindlabel(gotoprobe, label, label_len, label_flags,
3121 enterops, enterops + GOTO_DEPTH);
3124 if (gotoprobe->op_sibling &&
3125 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
3126 gotoprobe->op_sibling->op_sibling) {
3127 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
3128 label, label_len, label_flags, enterops,
3129 enterops + GOTO_DEPTH);
3134 PL_lastgotoprobe = gotoprobe;
3137 DIE(aTHX_ "Can't find label %"SVf,
3138 SVfARG(newSVpvn_flags(label, label_len,
3139 SVs_TEMP | label_flags)));
3141 /* if we're leaving an eval, check before we pop any frames
3142 that we're not going to punt, otherwise the error
3145 if (leaving_eval && *enterops && enterops[1]) {
3147 for (i = 1; enterops[i]; i++)
3148 if (enterops[i]->op_type == OP_ENTERITER)
3149 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3152 if (*enterops && enterops[1]) {
3153 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3155 deprecate("\"goto\" to jump into a construct");
3158 /* pop unwanted frames */
3160 if (ix < cxstack_ix) {
3167 oldsave = PL_scopestack[PL_scopestack_ix];
3168 LEAVE_SCOPE(oldsave);
3171 /* push wanted frames */
3173 if (*enterops && enterops[1]) {
3174 OP * const oldop = PL_op;
3175 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3176 for (; enterops[ix]; ix++) {
3177 PL_op = enterops[ix];
3178 /* Eventually we may want to stack the needed arguments
3179 * for each op. For now, we punt on the hard ones. */
3180 if (PL_op->op_type == OP_ENTERITER)
3181 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3182 PL_op->op_ppaddr(aTHX);
3190 if (!retop) retop = PL_main_start;
3192 PL_restartop = retop;
3193 PL_do_undump = TRUE;
3197 PL_restartop = 0; /* hmm, must be GNU unexec().. */
3198 PL_do_undump = FALSE;
3213 anum = 0; (void)POPs;
3218 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
3220 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
3223 PL_exit_flags |= PERL_EXIT_EXPECTED;
3225 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
3226 if (anum || !(PL_minus_c && PL_madskills))
3231 PUSHs(&PL_sv_undef);
3238 S_save_lines(pTHX_ AV *array, SV *sv)
3240 const char *s = SvPVX_const(sv);
3241 const char * const send = SvPVX_const(sv) + SvCUR(sv);
3244 PERL_ARGS_ASSERT_SAVE_LINES;
3246 while (s && s < send) {
3248 SV * const tmpstr = newSV_type(SVt_PVMG);
3250 t = (const char *)memchr(s, '\n', send - s);
3256 sv_setpvn(tmpstr, s, t - s);
3257 av_store(array, line++, tmpstr);
3265 Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
3267 0 is used as continue inside eval,
3269 3 is used for a die caught by an inner eval - continue inner loop
3271 See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
3272 establish a local jmpenv to handle exception traps.
3277 S_docatch(pTHX_ OP *o)
3281 OP * const oldop = PL_op;
3285 assert(CATCH_GET == TRUE);
3292 assert(cxstack_ix >= 0);
3293 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3294 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
3299 /* die caught by an inner eval - continue inner loop */
3300 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3301 PL_restartjmpenv = NULL;
3302 PL_op = PL_restartop;
3318 /* James Bond: Do you expect me to talk?
3319 Auric Goldfinger: No, Mr. Bond. I expect you to die.
3321 This code is an ugly hack, doesn't work with lexicals in subroutines that are
3322 called more than once, and is only used by regcomp.c, for (?{}) blocks.
3324 Currently it is not used outside the core code. Best if it stays that way.
3326 Hence it's now deprecated, and will be removed.
3329 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
3330 /* sv Text to convert to OP tree. */
3331 /* startop op_free() this to undo. */
3332 /* code Short string id of the caller. */
3334 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3335 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
3338 /* Don't use this. It will go away without warning once the regexp engine is
3339 refactored not to use it. */
3341 Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3344 dVAR; dSP; /* Make POPBLOCK work. */
3350 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3351 char *tmpbuf = tbuf;
3354 CV* runcv = NULL; /* initialise to avoid compiler warnings */
3358 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
3360 ENTER_with_name("eval");
3361 lex_start(sv, NULL, LEX_START_SAME_FILTER);
3363 /* switch to eval mode */
3365 if (IN_PERL_COMPILETIME) {
3366 SAVECOPSTASH_FREE(&PL_compiling);
3367 CopSTASH_set(&PL_compiling, PL_curstash);
3369 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3370 SV * const sv = sv_newmortal();
3371 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3372 code, (unsigned long)++PL_evalseq,
3373 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3378 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3379 (unsigned long)++PL_evalseq);
3380 SAVECOPFILE_FREE(&PL_compiling);
3381 CopFILE_set(&PL_compiling, tmpbuf+2);
3382 SAVECOPLINE(&PL_compiling);
3383 CopLINE_set(&PL_compiling, 1);
3384 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3385 deleting the eval's FILEGV from the stash before gv_check() runs
3386 (i.e. before run-time proper). To work around the coredump that
3387 ensues, we always turn GvMULTI_on for any globals that were
3388 introduced within evals. See force_ident(). GSAR 96-10-12 */
3389 safestr = savepvn(tmpbuf, len);
3390 SAVEDELETE(PL_defstash, safestr, len);
3392 #ifdef OP_IN_REGISTER
3398 /* we get here either during compilation, or via pp_regcomp at runtime */
3399 runtime = IN_PERL_RUNTIME;
3402 runcv = find_runcv(NULL);
3404 /* At run time, we have to fetch the hints from PL_curcop. */
3405 PL_hints = PL_curcop->cop_hints;
3406 if (PL_hints & HINT_LOCALIZE_HH) {
3407 /* SAVEHINTS created a new HV in PL_hintgv, which we
3409 SvREFCNT_dec(GvHV(PL_hintgv));
3411 refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0);
3412 hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints);
3414 SAVECOMPILEWARNINGS();
3415 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3416 cophh_free(CopHINTHASH_get(&PL_compiling));
3417 /* XXX Does this need to avoid copying a label? */
3418 PL_compiling.cop_hints_hash
3419 = cophh_copy(PL_curcop->cop_hints_hash);
3423 PL_op->op_type = OP_ENTEREVAL;
3424 PL_op->op_flags = 0; /* Avoid uninit warning. */
3425 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
3427 need_catch = CATCH_GET;
3431 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq, NULL);
3433 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax, NULL);
3434 CATCH_SET(need_catch);
3435 POPBLOCK(cx,PL_curpm);
3438 (*startop)->op_type = OP_NULL;
3439 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
3440 /* XXX DAPM do this properly one year */
3441 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
3442 LEAVE_with_name("eval");
3443 if (IN_PERL_COMPILETIME)
3444 CopHINTS_set(&PL_compiling, PL_hints);
3445 #ifdef OP_IN_REGISTER
3448 PERL_UNUSED_VAR(newsp);
3449 PERL_UNUSED_VAR(optype);
3451 return PL_eval_start;
3456 =for apidoc find_runcv
3458 Locate the CV corresponding to the currently executing sub or eval.
3459 If db_seqp is non_null, skip CVs that are in the DB package and populate
3460 *db_seqp with the cop sequence number at the point that the DB:: code was
3461 entered. (allows debuggers to eval in the scope of the breakpoint rather
3462 than in the scope of the debugger itself).
3468 Perl_find_runcv(pTHX_ U32 *db_seqp)
3474 *db_seqp = PL_curcop->cop_seq;
3475 for (si = PL_curstackinfo; si; si = si->si_prev) {
3477 for (ix = si->si_cxix; ix >= 0; ix--) {
3478 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3479 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3480 CV * const cv = cx->blk_sub.cv;
3481 /* skip DB:: code */
3482 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3483 *db_seqp = cx->blk_oldcop->cop_seq;
3488 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3489 return cx->blk_eval.cv;
3496 /* Run yyparse() in a setjmp wrapper. Returns:
3497 * 0: yyparse() successful
3498 * 1: yyparse() failed
3502 S_try_yyparse(pTHX_ int gramtype)
3507 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3511 ret = yyparse(gramtype) ? 1 : 0;
3525 /* Compile a require/do, an eval '', or a /(?{...})/.
3526 * In the last case, startop is non-null, and contains the address of
3527 * a pointer that should be set to the just-compiled code.
3528 * outside is the lexically enclosing CV (if any) that invoked us.
3529 * Returns a bool indicating whether the compile was successful; if so,
3530 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3531 * pushes undef (also croaks if startop != NULL).
3534 /* This function is called from three places, sv_compile_2op, pp_require
3535 * and pp_entereval. These can be distinguished as follows:
3536 * sv_compile_2op - startop is non-null
3537 * pp_require - startop is null; saveop is not entereval
3538 * pp_entereval - startop is null; saveop is entereval
3542 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq, HV *hh)
3545 OP * const saveop = PL_op;
3546 COP * const oldcurcop = PL_curcop;
3547 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3551 PL_in_eval = (in_require
3552 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3557 evalcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3559 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3560 cxstack[cxstack_ix].blk_eval.cv = evalcv;
3561 cxstack[cxstack_ix].blk_gimme = gimme;
3563 CvOUTSIDE_SEQ(evalcv) = seq;
3564 CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3566 /* set up a scratch pad */
3568 CvPADLIST(evalcv) = pad_new(padnew_SAVE);
3569 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3573 SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */
3575 /* make sure we compile in the right package */
3577 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3578 SAVEGENERICSV(PL_curstash);
3579 PL_curstash = (HV *)SvREFCNT_inc_simple(CopSTASH(PL_curcop));
3581 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3582 SAVESPTR(PL_beginav);
3583 PL_beginav = newAV();
3584 SAVEFREESV(PL_beginav);
3585 SAVESPTR(PL_unitcheckav);
3586 PL_unitcheckav = newAV();
3587 SAVEFREESV(PL_unitcheckav);
3590 SAVEBOOL(PL_madskills);
3594 if (!startop) ENTER_with_name("evalcomp");
3595 SAVESPTR(PL_compcv);
3598 /* try to compile it */
3600 PL_eval_root = NULL;
3601 PL_curcop = &PL_compiling;
3602 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3603 PL_in_eval |= EVAL_KEEPERR;
3608 bool clear_hints = saveop->op_type != OP_ENTEREVAL;
3612 hv_clear(GvHV(PL_hintgv));
3615 PL_hints = saveop->op_private & OPpEVAL_COPHH
3616 ? oldcurcop->cop_hints : saveop->op_targ;
3618 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3619 SvREFCNT_dec(GvHV(PL_hintgv));
3620 GvHV(PL_hintgv) = hh;
3623 SAVECOMPILEWARNINGS();
3625 if (PL_dowarn & G_WARN_ALL_ON)
3626 PL_compiling.cop_warnings = pWARN_ALL ;
3627 else if (PL_dowarn & G_WARN_ALL_OFF)
3628 PL_compiling.cop_warnings = pWARN_NONE ;
3630 PL_compiling.cop_warnings = pWARN_STD ;
3633 PL_compiling.cop_warnings =
3634 DUP_WARNINGS(oldcurcop->cop_warnings);
3635 cophh_free(CopHINTHASH_get(&PL_compiling));
3636 if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) {
3637 /* The label, if present, is the first entry on the chain. So rather
3638 than writing a blank label in front of it (which involves an
3639 allocation), just use the next entry in the chain. */
3640 PL_compiling.cop_hints_hash
3641 = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next);
3642 /* Check the assumption that this removed the label. */
3643 assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3646 PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash);
3650 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3652 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3653 * so honour CATCH_GET and trap it here if necessary */
3655 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3657 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3658 SV **newsp; /* Used by POPBLOCK. */
3660 I32 optype; /* Used by POPEVAL. */
3665 PERL_UNUSED_VAR(newsp);
3666 PERL_UNUSED_VAR(optype);
3668 /* note that if yystatus == 3, then the EVAL CX block has already
3669 * been popped, and various vars restored */
3671 if (yystatus != 3) {
3673 op_free(PL_eval_root);
3674 PL_eval_root = NULL;
3676 SP = PL_stack_base + POPMARK; /* pop original mark */
3678 POPBLOCK(cx,PL_curpm);
3680 namesv = cx->blk_eval.old_namesv;
3682 /* POPBLOCK renders LEAVE_with_name("evalcomp") unnecessary. */
3683 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3688 /* If cx is still NULL, it means that we didn't go in the
3689 * POPEVAL branch. */
3690 cx = &cxstack[cxstack_ix];
3691 assert(CxTYPE(cx) == CXt_EVAL);
3692 namesv = cx->blk_eval.old_namesv;
3694 (void)hv_store(GvHVn(PL_incgv),
3695 SvPVX_const(namesv),
3696 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
3698 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
3701 : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
3704 if (yystatus != 3) {
3705 POPBLOCK(cx,PL_curpm);
3708 Perl_croak(aTHX_ "%"SVf"Compilation failed in regexp",
3711 : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
3714 if (!*(SvPVx_nolen_const(ERRSV))) {
3715 sv_setpvs(ERRSV, "Compilation error");
3718 if (gimme != G_ARRAY) PUSHs(&PL_sv_undef);
3722 else if (!startop) LEAVE_with_name("evalcomp");
3723 CopLINE_set(&PL_compiling, 0);
3725 *startop = PL_eval_root;
3727 SAVEFREEOP(PL_eval_root);
3729 DEBUG_x(dump_eval());
3731 /* Register with debugger: */
3732 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3733 CV * const cv = get_cvs("DB::postponed", 0);
3737 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3739 call_sv(MUTABLE_SV(cv), G_DISCARD);
3743 if (PL_unitcheckav) {
3744 OP *es = PL_eval_start;
3745 call_list(PL_scopestack_ix, PL_unitcheckav);
3749 /* compiled okay, so do it */
3751 CvDEPTH(evalcv) = 1;
3752 SP = PL_stack_base + POPMARK; /* pop original mark */
3753 PL_op = saveop; /* The caller may need it. */
3754 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3761 S_check_type_and_open(pTHX_ SV *name)
3764 const char *p = SvPV_nolen_const(name);
3765 const int st_rc = PerlLIO_stat(p, &st);
3767 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3769 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3773 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3774 return PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3776 return PerlIO_open(p, PERL_SCRIPT_MODE);
3780 #ifndef PERL_DISABLE_PMC
3782 S_doopen_pm(pTHX_ SV *name)
3785 const char *p = SvPV_const(name, namelen);
3787 PERL_ARGS_ASSERT_DOOPEN_PM;
3789 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3790 SV *const pmcsv = sv_newmortal();
3793 SvSetSV_nosteal(pmcsv,name);
3794 sv_catpvn(pmcsv, "c", 1);
3796 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3797 return check_type_and_open(pmcsv);
3799 return check_type_and_open(name);
3802 # define doopen_pm(name) check_type_and_open(name)
3803 #endif /* !PERL_DISABLE_PMC */
3808 register PERL_CONTEXT *cx;
3815 int vms_unixname = 0;
3817 const char *tryname = NULL;
3819 const I32 gimme = GIMME_V;
3820 int filter_has_file = 0;
3821 PerlIO *tryrsfp = NULL;
3822 SV *filter_cache = NULL;
3823 SV *filter_state = NULL;
3824 SV *filter_sub = NULL;
3830 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3831 sv = sv_2mortal(new_version(sv));
3832 if (!sv_derived_from(PL_patchlevel, "version"))
3833 upg_version(PL_patchlevel, TRUE);
3834 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3835 if ( vcmp(sv,PL_patchlevel) <= 0 )
3836 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3837 SVfARG(sv_2mortal(vnormal(sv))),
3838 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3842 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3845 SV * const req = SvRV(sv);
3846 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3848 /* get the left hand term */
3849 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3851 first = SvIV(*av_fetch(lav,0,0));
3852 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3853 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3854 || av_len(lav) > 1 /* FP with > 3 digits */
3855 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3857 DIE(aTHX_ "Perl %"SVf" required--this is only "
3859 SVfARG(sv_2mortal(vnormal(req))),
3860 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3863 else { /* probably 'use 5.10' or 'use 5.8' */
3868 second = SvIV(*av_fetch(lav,1,0));
3870 second /= second >= 600 ? 100 : 10;
3871 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3872 (int)first, (int)second);
3873 upg_version(hintsv, TRUE);
3875 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3876 "--this is only %"SVf", stopped",
3877 SVfARG(sv_2mortal(vnormal(req))),
3878 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3879 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3887 name = SvPV_const(sv, len);
3888 if (!(name && len > 0 && *name))
3889 DIE(aTHX_ "Null filename used");
3890 TAINT_PROPER("require");
3894 /* The key in the %ENV hash is in the syntax of file passed as the argument
3895 * usually this is in UNIX format, but sometimes in VMS format, which
3896 * can result in a module being pulled in more than once.
3897 * To prevent this, the key must be stored in UNIX format if the VMS
3898 * name can be translated to UNIX.
3900 if ((unixname = tounixspec(name, NULL)) != NULL) {
3901 unixlen = strlen(unixname);
3907 /* if not VMS or VMS name can not be translated to UNIX, pass it
3910 unixname = (char *) name;
3913 if (PL_op->op_type == OP_REQUIRE) {
3914 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3915 unixname, unixlen, 0);
3917 if (*svp != &PL_sv_undef)
3920 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3921 "Compilation failed in require", unixname);
3925 /* prepare to compile file */
3927 if (path_is_absolute(name)) {
3928 /* At this point, name is SvPVX(sv) */
3930 tryrsfp = doopen_pm(sv);
3933 AV * const ar = GvAVn(PL_incgv);
3939 namesv = newSV_type(SVt_PV);
3940 for (i = 0; i <= AvFILL(ar); i++) {
3941 SV * const dirsv = *av_fetch(ar, i, TRUE);
3943 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3950 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3951 && !sv_isobject(loader))
3953 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3956 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3957 PTR2UV(SvRV(dirsv)), name);
3958 tryname = SvPVX_const(namesv);
3961 ENTER_with_name("call_INC");
3969 if (sv_isobject(loader))
3970 count = call_method("INC", G_ARRAY);
3972 count = call_sv(loader, G_ARRAY);
3982 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3983 && !isGV_with_GP(SvRV(arg))) {
3984 filter_cache = SvRV(arg);
3985 SvREFCNT_inc_simple_void_NN(filter_cache);
3992 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
3996 if (isGV_with_GP(arg)) {
3997 IO * const io = GvIO((const GV *)arg);
4002 tryrsfp = IoIFP(io);
4003 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
4004 PerlIO_close(IoOFP(io));
4015 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
4017 SvREFCNT_inc_simple_void_NN(filter_sub);
4020 filter_state = SP[i];
4021 SvREFCNT_inc_simple_void(filter_state);
4025 if (!tryrsfp && (filter_cache || filter_sub)) {
4026 tryrsfp = PerlIO_open(BIT_BUCKET,
4034 LEAVE_with_name("call_INC");
4036 /* Adjust file name if the hook has set an %INC entry.
4037 This needs to happen after the FREETMPS above. */
4038 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
4040 tryname = SvPV_nolen_const(*svp);
4047 filter_has_file = 0;
4049 SvREFCNT_dec(filter_cache);
4050 filter_cache = NULL;
4053 SvREFCNT_dec(filter_state);
4054 filter_state = NULL;
4057 SvREFCNT_dec(filter_sub);
4062 if (!path_is_absolute(name)
4068 dir = SvPV_const(dirsv, dirlen);
4076 if ((unixdir = tounixpath(dir, NULL)) == NULL)
4078 sv_setpv(namesv, unixdir);
4079 sv_catpv(namesv, unixname);
4081 # ifdef __SYMBIAN32__
4082 if (PL_origfilename[0] &&
4083 PL_origfilename[1] == ':' &&
4084 !(dir[0] && dir[1] == ':'))
4085 Perl_sv_setpvf(aTHX_ namesv,
4090 Perl_sv_setpvf(aTHX_ namesv,
4094 /* The equivalent of
4095 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
4096 but without the need to parse the format string, or
4097 call strlen on either pointer, and with the correct
4098 allocation up front. */
4100 char *tmp = SvGROW(namesv, dirlen + len + 2);
4102 memcpy(tmp, dir, dirlen);
4105 /* name came from an SV, so it will have a '\0' at the
4106 end that we can copy as part of this memcpy(). */
4107 memcpy(tmp, name, len + 1);
4109 SvCUR_set(namesv, dirlen + len + 1);
4114 TAINT_PROPER("require");
4115 tryname = SvPVX_const(namesv);
4116 tryrsfp = doopen_pm(namesv);
4118 if (tryname[0] == '.' && tryname[1] == '/') {
4120 while (*++tryname == '/');
4124 else if (errno == EMFILE)
4125 /* no point in trying other paths if out of handles */
4134 if (PL_op->op_type == OP_REQUIRE) {
4135 if(errno == EMFILE) {
4136 /* diag_listed_as: Can't locate %s */
4137 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno));
4139 if (namesv) { /* did we lookup @INC? */
4140 AV * const ar = GvAVn(PL_incgv);
4142 SV *const inc = newSVpvs_flags("", SVs_TEMP);
4143 for (i = 0; i <= AvFILL(ar); i++) {
4144 sv_catpvs(inc, " ");
4145 sv_catsv(inc, *av_fetch(ar, i, TRUE));
4148 /* diag_listed_as: Can't locate %s */
4150 "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")",
4152 (memEQ(name + len - 2, ".h", 3)
4153 ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""),
4154 (memEQ(name + len - 3, ".ph", 4)
4155 ? " (did you run h2ph?)" : ""),
4160 DIE(aTHX_ "Can't locate %s", name);
4166 SETERRNO(0, SS_NORMAL);
4168 /* Assume success here to prevent recursive requirement. */
4169 /* name is never assigned to again, so len is still strlen(name) */
4170 /* Check whether a hook in @INC has already filled %INC */
4172 (void)hv_store(GvHVn(PL_incgv),
4173 unixname, unixlen, newSVpv(tryname,0),0);
4175 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
4177 (void)hv_store(GvHVn(PL_incgv),
4178 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
4181 ENTER_with_name("eval");
4183 SAVECOPFILE_FREE(&PL_compiling);
4184 CopFILE_set(&PL_compiling, tryname);
4185 lex_start(NULL, tryrsfp, 0);
4187 if (filter_sub || filter_cache) {
4188 /* We can use the SvPV of the filter PVIO itself as our cache, rather
4189 than hanging another SV from it. In turn, filter_add() optionally
4190 takes the SV to use as the filter (or creates a new SV if passed
4191 NULL), so simply pass in whatever value filter_cache has. */
4192 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
4193 IoLINES(datasv) = filter_has_file;
4194 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
4195 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
4198 /* switch to eval mode */
4199 PUSHBLOCK(cx, CXt_EVAL, SP);
4201 cx->blk_eval.retop = PL_op->op_next;
4203 SAVECOPLINE(&PL_compiling);
4204 CopLINE_set(&PL_compiling, 0);
4208 /* Store and reset encoding. */
4209 encoding = PL_encoding;
4212 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq, NULL))
4213 op = DOCATCH(PL_eval_start);
4215 op = PL_op->op_next;
4217 /* Restore encoding. */
4218 PL_encoding = encoding;
4223 /* This is a op added to hold the hints hash for
4224 pp_entereval. The hash can be modified by the code
4225 being eval'ed, so we return a copy instead. */
4231 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
4239 register PERL_CONTEXT *cx;
4241 const I32 gimme = GIMME_V;
4242 const U32 was = PL_breakable_sub_gen;
4243 char tbuf[TYPE_DIGITS(long) + 12];
4244 bool saved_delete = FALSE;
4245 char *tmpbuf = tbuf;
4248 U32 seq, lex_flags = 0;
4249 HV *saved_hh = NULL;
4250 const bool bytes = PL_op->op_private & OPpEVAL_BYTES;
4252 if (PL_op->op_private & OPpEVAL_HAS_HH) {
4253 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
4255 else if (PL_hints & HINT_LOCALIZE_HH || (
4256 PL_op->op_private & OPpEVAL_COPHH
4257 && PL_curcop->cop_hints & HINT_LOCALIZE_HH
4259 saved_hh = cop_hints_2hv(PL_curcop, 0);
4260 hv_magic(saved_hh, NULL, PERL_MAGIC_hints);
4264 /* make sure we've got a plain PV (no overload etc) before testing
4265 * for taint. Making a copy here is probably overkill, but better
4266 * safe than sorry */
4268 const char * const p = SvPV_const(sv, len);
4270 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
4271 lex_flags |= LEX_START_COPIED;
4273 if (bytes && SvUTF8(sv))
4274 SvPVbyte_force(sv, len);
4276 else if (bytes && SvUTF8(sv)) {
4277 /* Don't modify someone else's scalar */
4280 (void)sv_2mortal(sv);
4281 SvPVbyte_force(sv,len);
4282 lex_flags |= LEX_START_COPIED;
4285 TAINT_IF(SvTAINTED(sv));
4286 TAINT_PROPER("eval");
4288 ENTER_with_name("eval");
4289 lex_start(sv, NULL, lex_flags | (PL_op->op_private & OPpEVAL_UNICODE
4290 ? LEX_IGNORE_UTF8_HINTS
4291 : bytes ? LEX_EVALBYTES : LEX_START_SAME_FILTER
4296 /* switch to eval mode */
4298 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
4299 SV * const temp_sv = sv_newmortal();
4300 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
4301 (unsigned long)++PL_evalseq,
4302 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4303 tmpbuf = SvPVX(temp_sv);
4304 len = SvCUR(temp_sv);
4307 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
4308 SAVECOPFILE_FREE(&PL_compiling);
4309 CopFILE_set(&PL_compiling, tmpbuf+2);
4310 SAVECOPLINE(&PL_compiling);
4311 CopLINE_set(&PL_compiling, 1);
4312 /* special case: an eval '' executed within the DB package gets lexically
4313 * placed in the first non-DB CV rather than the current CV - this
4314 * allows the debugger to execute code, find lexicals etc, in the
4315 * scope of the code being debugged. Passing &seq gets find_runcv
4316 * to do the dirty work for us */
4317 runcv = find_runcv(&seq);
4319 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
4321 cx->blk_eval.retop = PL_op->op_next;
4323 /* prepare to compile string */
4325 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
4326 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
4328 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
4329 deleting the eval's FILEGV from the stash before gv_check() runs
4330 (i.e. before run-time proper). To work around the coredump that
4331 ensues, we always turn GvMULTI_on for any globals that were
4332 introduced within evals. See force_ident(). GSAR 96-10-12 */
4333 char *const safestr = savepvn(tmpbuf, len);
4334 SAVEDELETE(PL_defstash, safestr, len);
4335 saved_delete = TRUE;
4340 if (doeval(gimme, NULL, runcv, seq, saved_hh)) {
4341 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4342 ? (PERLDB_LINE || PERLDB_SAVESRC)
4343 : PERLDB_SAVESRC_NOSUBS) {
4344 /* Retain the filegv we created. */
4345 } else if (!saved_delete) {
4346 char *const safestr = savepvn(tmpbuf, len);
4347 SAVEDELETE(PL_defstash, safestr, len);
4349 return DOCATCH(PL_eval_start);
4351 /* We have already left the scope set up earlier thanks to the LEAVE
4353 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4354 ? (PERLDB_LINE || PERLDB_SAVESRC)
4355 : PERLDB_SAVESRC_INVALID) {
4356 /* Retain the filegv we created. */
4357 } else if (!saved_delete) {
4358 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
4360 return PL_op->op_next;
4370 register PERL_CONTEXT *cx;
4372 const U8 save_flags = PL_op -> op_flags;
4380 namesv = cx->blk_eval.old_namesv;
4381 retop = cx->blk_eval.retop;
4382 evalcv = cx->blk_eval.cv;
4385 SP = adjust_stack_on_leave((gimme == G_VOID) ? SP : newsp, SP, newsp,
4387 PL_curpm = newpm; /* Don't pop $1 et al till now */
4390 assert(CvDEPTH(evalcv) == 1);
4392 CvDEPTH(evalcv) = 0;
4394 if (optype == OP_REQUIRE &&
4395 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4397 /* Unassume the success we assumed earlier. */
4398 (void)hv_delete(GvHVn(PL_incgv),
4399 SvPVX_const(namesv),
4400 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
4402 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4404 /* die_unwind() did LEAVE, or we won't be here */
4407 LEAVE_with_name("eval");
4408 if (!(save_flags & OPf_SPECIAL)) {
4416 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4417 close to the related Perl_create_eval_scope. */
4419 Perl_delete_eval_scope(pTHX)
4424 register PERL_CONTEXT *cx;
4430 LEAVE_with_name("eval_scope");
4431 PERL_UNUSED_VAR(newsp);
4432 PERL_UNUSED_VAR(gimme);
4433 PERL_UNUSED_VAR(optype);
4436 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4437 also needed by Perl_fold_constants. */
4439 Perl_create_eval_scope(pTHX_ U32 flags)