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))
48 cxix = dopoptosub(cxstack_ix);
52 switch (cxstack[cxix].blk_gimme) {
65 /* XXXX Should store the old value to allow for tie/overload - and
66 restore in regcomp, where marked with XXXX. */
76 register PMOP *pm = (PMOP*)cLOGOP->op_other;
80 /* prevent recompiling under /o and ithreads. */
81 #if defined(USE_ITHREADS)
82 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
83 if (PL_op->op_flags & OPf_STACKED) {
93 #define tryAMAGICregexp(rx) \
96 if (SvROK(rx) && SvAMAGIC(rx)) { \
97 SV *sv = AMG_CALLunary(rx, regexp_amg); \
101 if (SvTYPE(sv) != SVt_REGEXP) \
102 Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \
109 if (PL_op->op_flags & OPf_STACKED) {
110 /* multiple args; concatenate them */
112 tmpstr = PAD_SV(ARGTARG);
113 sv_setpvs(tmpstr, "");
114 while (++MARK <= SP) {
118 tryAMAGICregexp(msv);
120 if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) &&
121 (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign)))
123 sv_setsv(tmpstr, sv);
126 sv_catsv_nomg(tmpstr, msv);
133 tryAMAGICregexp(tmpstr);
136 #undef tryAMAGICregexp
139 SV * const sv = SvRV(tmpstr);
140 if (SvTYPE(sv) == SVt_REGEXP)
143 else if (SvTYPE(tmpstr) == SVt_REGEXP)
144 re = (REGEXP*) tmpstr;
147 /* The match's LHS's get-magic might need to access this op's reg-
148 exp (as is sometimes the case with $'; see bug 70764). So we
149 must call get-magic now before we replace the regexp. Hopeful-
150 ly this hack can be replaced with the approach described at
151 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03
152 /msg122415.html some day. */
153 if(pm->op_type == OP_MATCH) {
155 const bool was_tainted = PL_tainted;
156 if (pm->op_flags & OPf_STACKED)
158 else if (pm->op_private & OPpTARGET_MY)
159 lhs = PAD_SV(pm->op_targ);
162 /* Restore the previous value of PL_tainted (which may have been
163 modified by get-magic), to avoid incorrectly setting the
164 RXf_TAINTED flag further down. */
165 PL_tainted = was_tainted;
168 re = reg_temp_copy(NULL, re);
169 ReREFCNT_dec(PM_GETRE(pm));
174 const char *t = SvOK(tmpstr) ? SvPV_nomg_const(tmpstr, len) : "";
177 assert (re != (REGEXP*) &PL_sv_undef);
179 /* Check against the last compiled regexp. */
180 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
181 memNE(RX_PRECOMP(re), t, len))
183 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
184 U32 pm_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
188 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
190 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
192 } else if (PL_curcop->cop_hints_hash) {
193 SV *ptr = cop_hints_fetch_pvs(PL_curcop, "regcomp", 0);
194 if (ptr && SvIOK(ptr) && SvIV(ptr))
195 eng = INT2PTR(regexp_engine*,SvIV(ptr));
198 if (PL_op->op_flags & OPf_SPECIAL)
199 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
201 if (DO_UTF8(tmpstr)) {
202 assert (SvUTF8(tmpstr));
203 } else if (SvUTF8(tmpstr)) {
204 /* Not doing UTF-8, despite what the SV says. Is this only if
205 we're trapped in use 'bytes'? */
206 /* Make a copy of the octet sequence, but without the flag on,
207 as the compiler now honours the SvUTF8 flag on tmpstr. */
209 const char *const p = SvPV(tmpstr, len);
210 tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
212 else if (SvAMAGIC(tmpstr)) {
213 /* make a copy to avoid extra stringifies */
214 tmpstr = newSVpvn_flags(t, len, SVs_TEMP | SvUTF8(tmpstr));
217 /* If it is gmagical, create a mortal copy, but without calling
218 get-magic, as we have already done that. */
219 if(SvGMAGICAL(tmpstr)) {
220 SV *mortalcopy = sv_newmortal();
221 sv_setsv_flags(mortalcopy, tmpstr, 0);
226 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
228 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
230 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
231 inside tie/overload accessors. */
237 #ifndef INCOMPLETE_TAINTS
240 SvTAINTED_on((SV*)re);
241 RX_EXTFLAGS(re) |= RXf_TAINTED;
246 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
250 #if !defined(USE_ITHREADS)
251 /* can't change the optree at runtime either */
252 /* PMf_KEEP is handled differently under threads to avoid these problems */
253 if (pm->op_pmflags & PMf_KEEP) {
254 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
255 cLOGOP->op_first->op_next = PL_op->op_next;
265 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
266 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
267 register SV * const dstr = cx->sb_dstr;
268 register char *s = cx->sb_s;
269 register char *m = cx->sb_m;
270 char *orig = cx->sb_orig;
271 register REGEXP * const rx = cx->sb_rx;
273 REGEXP *old = PM_GETRE(pm);
280 PM_SETRE(pm,ReREFCNT_inc(rx));
283 rxres_restore(&cx->sb_rxres, rx);
284 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
286 if (cx->sb_iters++) {
287 const I32 saviters = cx->sb_iters;
288 if (cx->sb_iters > cx->sb_maxiters)
289 DIE(aTHX_ "Substitution loop");
291 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
293 /* See "how taint works" above pp_subst() */
295 cx->sb_rxtainted |= SUBST_TAINT_REPL;
296 sv_catsv_nomg(dstr, POPs);
297 /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */
301 if (CxONCE(cx) || s < orig ||
302 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
303 (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
304 ((cx->sb_rflags & REXEC_COPY_STR)
305 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
306 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
308 SV * const targ = cx->sb_targ;
310 assert(cx->sb_strend >= s);
311 if(cx->sb_strend > s) {
312 if (DO_UTF8(dstr) && !SvUTF8(targ))
313 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
315 sv_catpvn(dstr, s, cx->sb_strend - s);
317 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
318 cx->sb_rxtainted |= SUBST_TAINT_PAT;
320 #ifdef PERL_OLD_COPY_ON_WRITE
322 sv_force_normal_flags(targ, SV_COW_DROP_PV);
328 SvPV_set(targ, SvPVX(dstr));
329 SvCUR_set(targ, SvCUR(dstr));
330 SvLEN_set(targ, SvLEN(dstr));
333 SvPV_set(dstr, NULL);
335 if (pm->op_pmflags & PMf_NONDESTRUCT)
338 mPUSHi(saviters - 1);
340 (void)SvPOK_only_UTF8(targ);
342 /* update the taint state of various various variables in
343 * preparation for final exit.
344 * See "how taint works" above pp_subst() */
346 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
347 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
348 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
350 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
352 if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET)
353 && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
355 SvTAINTED_on(TOPs); /* taint return value */
356 /* needed for mg_set below */
357 PL_tainted = cBOOL(cx->sb_rxtainted &
358 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL));
361 /* PL_tainted must be correctly set for this mg_set */
364 LEAVE_SCOPE(cx->sb_oldsave);
366 RETURNOP(pm->op_next);
369 cx->sb_iters = saviters;
371 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
374 cx->sb_orig = orig = RX_SUBBEG(rx);
376 cx->sb_strend = s + (cx->sb_strend - m);
378 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
380 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
381 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
383 sv_catpvn(dstr, s, m-s);
385 cx->sb_s = RX_OFFS(rx)[0].end + orig;
386 { /* Update the pos() information. */
387 SV * const sv = cx->sb_targ;
389 SvUPGRADE(sv, SVt_PVMG);
390 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
391 #ifdef PERL_OLD_COPY_ON_WRITE
393 sv_force_normal_flags(sv, 0);
395 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
398 mg->mg_len = m - orig;
401 (void)ReREFCNT_inc(rx);
402 /* update the taint state of various various variables in preparation
403 * for calling the code block.
404 * See "how taint works" above pp_subst() */
406 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
407 cx->sb_rxtainted |= SUBST_TAINT_PAT;
409 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
410 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
411 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
413 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
415 if (cx->sb_iters > 1 && (cx->sb_rxtainted &
416 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
417 SvTAINTED_on(cx->sb_targ);
420 rxres_save(&cx->sb_rxres, rx);
422 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
426 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
431 PERL_ARGS_ASSERT_RXRES_SAVE;
434 if (!p || p[1] < RX_NPARENS(rx)) {
435 #ifdef PERL_OLD_COPY_ON_WRITE
436 i = 7 + RX_NPARENS(rx) * 2;
438 i = 6 + RX_NPARENS(rx) * 2;
447 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
448 RX_MATCH_COPIED_off(rx);
450 #ifdef PERL_OLD_COPY_ON_WRITE
451 *p++ = PTR2UV(RX_SAVED_COPY(rx));
452 RX_SAVED_COPY(rx) = NULL;
455 *p++ = RX_NPARENS(rx);
457 *p++ = PTR2UV(RX_SUBBEG(rx));
458 *p++ = (UV)RX_SUBLEN(rx);
459 for (i = 0; i <= RX_NPARENS(rx); ++i) {
460 *p++ = (UV)RX_OFFS(rx)[i].start;
461 *p++ = (UV)RX_OFFS(rx)[i].end;
466 S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
471 PERL_ARGS_ASSERT_RXRES_RESTORE;
474 RX_MATCH_COPY_FREE(rx);
475 RX_MATCH_COPIED_set(rx, *p);
478 #ifdef PERL_OLD_COPY_ON_WRITE
479 if (RX_SAVED_COPY(rx))
480 SvREFCNT_dec (RX_SAVED_COPY(rx));
481 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
485 RX_NPARENS(rx) = *p++;
487 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
488 RX_SUBLEN(rx) = (I32)(*p++);
489 for (i = 0; i <= RX_NPARENS(rx); ++i) {
490 RX_OFFS(rx)[i].start = (I32)(*p++);
491 RX_OFFS(rx)[i].end = (I32)(*p++);
496 S_rxres_free(pTHX_ void **rsp)
498 UV * const p = (UV*)*rsp;
500 PERL_ARGS_ASSERT_RXRES_FREE;
505 void *tmp = INT2PTR(char*,*p);
508 PoisonFree(*p, 1, sizeof(*p));
510 Safefree(INT2PTR(char*,*p));
512 #ifdef PERL_OLD_COPY_ON_WRITE
514 SvREFCNT_dec (INT2PTR(SV*,p[1]));
524 dVAR; dSP; dMARK; dORIGMARK;
525 register SV * const tmpForm = *++MARK;
526 SV *formsv; /* contains text of original format */
527 register U32 *fpc; /* format ops program counter */
528 register char *t; /* current append position in target string */
529 const char *f; /* current position in format string */
531 register SV *sv = NULL; /* current item */
532 const char *item = NULL;/* string value of current item */
533 I32 itemsize = 0; /* length of current item, possibly truncated */
534 I32 fieldsize = 0; /* width of current field */
535 I32 lines = 0; /* number of lines that have been output */
536 bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
537 const char *chophere = NULL; /* where to chop current item */
538 char *linemark = NULL; /* pos of start of line in output */
540 bool gotsome = FALSE; /* seen at least one non-blank item on this line */
542 STRLEN fudge; /* estimate of output size in bytes */
543 bool item_is_utf8 = FALSE;
544 bool targ_is_utf8 = FALSE;
549 mg = doparseform(tmpForm);
551 fpc = (U32*)mg->mg_ptr;
552 /* the actual string the format was compiled from.
553 * with overload etc, this may not match tmpForm */
557 SvPV_force(PL_formtarget, len);
558 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
559 SvTAINTED_on(PL_formtarget);
560 if (DO_UTF8(PL_formtarget))
562 fudge = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
563 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
565 f = SvPV_const(formsv, len);
569 const char *name = "???";
572 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
573 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
574 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
575 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
576 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
578 case FF_CHECKNL: name = "CHECKNL"; break;
579 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
580 case FF_SPACE: name = "SPACE"; break;
581 case FF_HALFSPACE: name = "HALFSPACE"; break;
582 case FF_ITEM: name = "ITEM"; break;
583 case FF_CHOP: name = "CHOP"; break;
584 case FF_LINEGLOB: name = "LINEGLOB"; break;
585 case FF_NEWLINE: name = "NEWLINE"; break;
586 case FF_MORE: name = "MORE"; break;
587 case FF_LINEMARK: name = "LINEMARK"; break;
588 case FF_END: name = "END"; break;
589 case FF_0DECIMAL: name = "0DECIMAL"; break;
590 case FF_LINESNGL: name = "LINESNGL"; break;
593 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
595 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
606 if (targ_is_utf8 && !SvUTF8(formsv)) {
608 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
611 /* this is an unrolled sv_catpvn_utf8_upgrade(),
612 * but with the addition of s/~/ /g */
614 nsv = newSVpvn_flags(f, arg, SVs_TEMP);
616 sv_setpvn(nsv, f, arg);
618 for (s = SvPVX(nsv); s <= SvEND(nsv); s++)
621 sv_utf8_upgrade(nsv);
622 sv_catsv(PL_formtarget, nsv);
624 t = SvEND(PL_formtarget);
628 if (!targ_is_utf8 && DO_UTF8(formsv)) {
629 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
631 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC, fudge + 1);
632 t = SvEND(PL_formtarget);
636 *t++ = (*f == '~') ? ' ' : *f;
654 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
657 SvTAINTED_on(PL_formtarget);
663 const char *s = item = SvPV_const(sv, len);
666 itemsize = sv_len_utf8(sv);
667 if (itemsize != (I32)len) {
669 if (itemsize > fieldsize) {
670 itemsize = fieldsize;
671 itembytes = itemsize;
672 sv_pos_u2b(sv, &itembytes, 0);
676 send = chophere = s + itembytes;
686 sv_pos_b2u(sv, &itemsize);
690 item_is_utf8 = FALSE;
691 if (itemsize > fieldsize)
692 itemsize = fieldsize;
693 send = chophere = s + itemsize;
707 const char *s = item = SvPV_const(sv, len);
710 itemsize = sv_len_utf8(sv);
711 if (itemsize != (I32)len) {
713 if (itemsize <= fieldsize) {
714 const char *send = chophere = s + itemsize;
727 itemsize = fieldsize;
728 itembytes = itemsize;
729 sv_pos_u2b(sv, &itembytes, 0);
730 send = chophere = s + itembytes;
731 while (s < send || (s == send && isSPACE(*s))) {
741 if (strchr(PL_chopset, *s))
746 itemsize = chophere - item;
747 sv_pos_b2u(sv, &itemsize);
753 item_is_utf8 = FALSE;
754 if (itemsize <= fieldsize) {
755 const char *const send = chophere = s + itemsize;
768 itemsize = fieldsize;
769 send = chophere = s + itemsize;
770 while (s < send || (s == send && isSPACE(*s))) {
780 if (strchr(PL_chopset, *s))
785 itemsize = chophere - item;
791 arg = fieldsize - itemsize;
800 arg = fieldsize - itemsize;
811 const char *s = item;
815 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
817 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC,
819 t = SvEND(PL_formtarget);
823 if (UTF8_IS_CONTINUED(*s)) {
824 STRLEN skip = UTF8SKIP(s);
841 if ( !((*t++ = *s++) & ~31) )
847 if (targ_is_utf8 && !item_is_utf8) {
848 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
850 sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
851 for (; t < SvEND(PL_formtarget); t++) {
864 const int ch = *t++ = *s++;
867 if ( !((*t++ = *s++) & ~31) )
876 const char *s = chophere;
890 const bool oneline = fpc[-1] == FF_LINESNGL;
891 const char *s = item = SvPV_const(sv, len);
892 const char *const send = s + len;
893 STRLEN to_copy = len;
894 const U8 *source = (const U8 *) s;
897 item_is_utf8 = DO_UTF8(sv);
903 chophere = s + itemsize;
907 to_copy = s - SvPVX_const(sv) - 1;
919 if (targ_is_utf8 && !item_is_utf8) {
920 source = tmp = bytes_to_utf8(source, &to_copy);
921 SvCUR_set(PL_formtarget,
922 t - SvPVX_const(PL_formtarget));
924 if (item_is_utf8 && !targ_is_utf8) {
925 /* Upgrade targ to UTF8, and then we reduce it to
926 a problem we have a simple solution for. */
927 SvCUR_set(PL_formtarget,
928 t - SvPVX_const(PL_formtarget));
930 /* Don't need get magic. */
931 sv_utf8_upgrade_nomg(PL_formtarget);
933 SvCUR_set(PL_formtarget,
934 t - SvPVX_const(PL_formtarget));
937 /* Easy. They agree. */
938 assert (item_is_utf8 == targ_is_utf8);
940 SvGROW(PL_formtarget,
941 SvCUR(PL_formtarget) + to_copy + fudge + 1);
942 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
944 Copy(source, t, to_copy, char);
946 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
948 if (SvGMAGICAL(sv)) {
949 /* Mustn't call sv_pos_b2u() as it does a second
950 mg_get(). Is this a bug? Do we need a _flags()
952 itemsize = utf8_length(source, source + itemsize);
954 sv_pos_b2u(sv, &itemsize);
966 #if defined(USE_LONG_DOUBLE)
969 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
973 "%#0*.*f" : "%0*.*f");
978 #if defined(USE_LONG_DOUBLE)
980 ((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
983 ((arg & 256) ? "%#*.*f" : "%*.*f");
986 /* If the field is marked with ^ and the value is undefined,
988 if ((arg & 512) && !SvOK(sv)) {
996 /* overflow evidence */
997 if (num_overflow(value, fieldsize, arg)) {
1003 /* Formats aren't yet marked for locales, so assume "yes". */
1005 STORE_NUMERIC_STANDARD_SET_LOCAL();
1006 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
1007 RESTORE_NUMERIC_STANDARD();
1014 while (t-- > linemark && *t == ' ') ;
1022 if (arg) { /* repeat until fields exhausted? */
1035 const char *s = chophere;
1036 const char *send = item + len;
1038 while (isSPACE(*s) && (s < send))
1043 arg = fieldsize - itemsize;
1050 if (strnEQ(s1," ",3)) {
1051 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1063 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1065 SvUTF8_on(PL_formtarget);
1066 FmLINES(PL_formtarget) += lines;
1068 if (fpc[-1] == FF_BLANK)
1069 RETURNOP(cLISTOP->op_first);
1081 if (PL_stack_base + *PL_markstack_ptr == SP) {
1083 if (GIMME_V == G_SCALAR)
1085 RETURNOP(PL_op->op_next->op_next);
1087 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
1088 Perl_pp_pushmark(aTHX); /* push dst */
1089 Perl_pp_pushmark(aTHX); /* push src */
1090 ENTER_with_name("grep"); /* enter outer scope */
1093 if (PL_op->op_private & OPpGREP_LEX)
1094 SAVESPTR(PAD_SVl(PL_op->op_targ));
1097 ENTER_with_name("grep_item"); /* enter inner scope */
1100 src = PL_stack_base[*PL_markstack_ptr];
1102 if (PL_op->op_private & OPpGREP_LEX)
1103 PAD_SVl(PL_op->op_targ) = src;
1108 if (PL_op->op_type == OP_MAPSTART)
1109 Perl_pp_pushmark(aTHX); /* push top */
1110 return ((LOGOP*)PL_op->op_next)->op_other;
1116 const I32 gimme = GIMME_V;
1117 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1123 /* first, move source pointer to the next item in the source list */
1124 ++PL_markstack_ptr[-1];
1126 /* if there are new items, push them into the destination list */
1127 if (items && gimme != G_VOID) {
1128 /* might need to make room back there first */
1129 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1130 /* XXX this implementation is very pessimal because the stack
1131 * is repeatedly extended for every set of items. Is possible
1132 * to do this without any stack extension or copying at all
1133 * by maintaining a separate list over which the map iterates
1134 * (like foreach does). --gsar */
1136 /* everything in the stack after the destination list moves
1137 * towards the end the stack by the amount of room needed */
1138 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1140 /* items to shift up (accounting for the moved source pointer) */
1141 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1143 /* This optimization is by Ben Tilly and it does
1144 * things differently from what Sarathy (gsar)
1145 * is describing. The downside of this optimization is
1146 * that leaves "holes" (uninitialized and hopefully unused areas)
1147 * to the Perl stack, but on the other hand this
1148 * shouldn't be a problem. If Sarathy's idea gets
1149 * implemented, this optimization should become
1150 * irrelevant. --jhi */
1152 shift = count; /* Avoid shifting too often --Ben Tilly */
1156 dst = (SP += shift);
1157 PL_markstack_ptr[-1] += shift;
1158 *PL_markstack_ptr += shift;
1162 /* copy the new items down to the destination list */
1163 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1164 if (gimme == G_ARRAY) {
1165 /* add returned items to the collection (making mortal copies
1166 * if necessary), then clear the current temps stack frame
1167 * *except* for those items. We do this splicing the items
1168 * into the start of the tmps frame (so some items may be on
1169 * the tmps stack twice), then moving PL_tmps_floor above
1170 * them, then freeing the frame. That way, the only tmps that
1171 * accumulate over iterations are the return values for map.
1172 * We have to do to this way so that everything gets correctly
1173 * freed if we die during the map.
1177 /* make space for the slice */
1178 EXTEND_MORTAL(items);
1179 tmpsbase = PL_tmps_floor + 1;
1180 Move(PL_tmps_stack + tmpsbase,
1181 PL_tmps_stack + tmpsbase + items,
1182 PL_tmps_ix - PL_tmps_floor,
1184 PL_tmps_ix += items;
1189 sv = sv_mortalcopy(sv);
1191 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1193 /* clear the stack frame except for the items */
1194 PL_tmps_floor += items;
1196 /* FREETMPS may have cleared the TEMP flag on some of the items */
1199 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1202 /* scalar context: we don't care about which values map returns
1203 * (we use undef here). And so we certainly don't want to do mortal
1204 * copies of meaningless values. */
1205 while (items-- > 0) {
1207 *dst-- = &PL_sv_undef;
1215 LEAVE_with_name("grep_item"); /* exit inner scope */
1218 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1220 (void)POPMARK; /* pop top */
1221 LEAVE_with_name("grep"); /* exit outer scope */
1222 (void)POPMARK; /* pop src */
1223 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1224 (void)POPMARK; /* pop dst */
1225 SP = PL_stack_base + POPMARK; /* pop original mark */
1226 if (gimme == G_SCALAR) {
1227 if (PL_op->op_private & OPpGREP_LEX) {
1228 SV* sv = sv_newmortal();
1229 sv_setiv(sv, items);
1237 else if (gimme == G_ARRAY)
1244 ENTER_with_name("grep_item"); /* enter inner scope */
1247 /* set $_ to the new source item */
1248 src = PL_stack_base[PL_markstack_ptr[-1]];
1250 if (PL_op->op_private & OPpGREP_LEX)
1251 PAD_SVl(PL_op->op_targ) = src;
1255 RETURNOP(cLOGOP->op_other);
1264 if (GIMME == G_ARRAY)
1266 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1267 return cLOGOP->op_other;
1277 if (GIMME == G_ARRAY) {
1278 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1282 SV * const targ = PAD_SV(PL_op->op_targ);
1285 if (PL_op->op_private & OPpFLIP_LINENUM) {
1286 if (GvIO(PL_last_in_gv)) {
1287 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1290 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1292 flip = SvIV(sv) == SvIV(GvSV(gv));
1298 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1299 if (PL_op->op_flags & OPf_SPECIAL) {
1307 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1310 sv_setpvs(TARG, "");
1316 /* This code tries to decide if "$left .. $right" should use the
1317 magical string increment, or if the range is numeric (we make
1318 an exception for .."0" [#18165]). AMS 20021031. */
1320 #define RANGE_IS_NUMERIC(left,right) ( \
1321 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1322 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1323 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1324 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1325 && (!SvOK(right) || looks_like_number(right))))
1331 if (GIMME == G_ARRAY) {
1337 if (RANGE_IS_NUMERIC(left,right)) {
1340 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1341 (SvOK(right) && SvNV(right) > IV_MAX))
1342 DIE(aTHX_ "Range iterator outside integer range");
1353 SV * const sv = sv_2mortal(newSViv(i++));
1358 SV * const final = sv_mortalcopy(right);
1360 const char * const tmps = SvPV_const(final, len);
1362 SV *sv = sv_mortalcopy(left);
1363 SvPV_force_nolen(sv);
1364 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1366 if (strEQ(SvPVX_const(sv),tmps))
1368 sv = sv_2mortal(newSVsv(sv));
1375 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1379 if (PL_op->op_private & OPpFLIP_LINENUM) {
1380 if (GvIO(PL_last_in_gv)) {
1381 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1384 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1385 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1393 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1394 sv_catpvs(targ, "E0");
1404 static const char * const context_name[] = {
1406 NULL, /* CXt_WHEN never actually needs "block" */
1407 NULL, /* CXt_BLOCK never actually needs "block" */
1408 NULL, /* CXt_GIVEN never actually needs "block" */
1409 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1410 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1411 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1412 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1420 S_dopoptolabel(pTHX_ const char *label)
1425 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1427 for (i = cxstack_ix; i >= 0; i--) {
1428 register const PERL_CONTEXT * const cx = &cxstack[i];
1429 switch (CxTYPE(cx)) {
1435 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1436 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1437 if (CxTYPE(cx) == CXt_NULL)
1440 case CXt_LOOP_LAZYIV:
1441 case CXt_LOOP_LAZYSV:
1443 case CXt_LOOP_PLAIN:
1445 const char *cx_label = CxLABEL(cx);
1446 if (!cx_label || strNE(label, cx_label) ) {
1447 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1448 (long)i, cx_label));
1451 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1462 Perl_dowantarray(pTHX)
1465 const I32 gimme = block_gimme();
1466 return (gimme == G_VOID) ? G_SCALAR : gimme;
1470 Perl_block_gimme(pTHX)
1473 const I32 cxix = dopoptosub(cxstack_ix);
1477 switch (cxstack[cxix].blk_gimme) {
1485 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1492 Perl_is_lvalue_sub(pTHX)
1495 const I32 cxix = dopoptosub(cxstack_ix);
1496 assert(cxix >= 0); /* We should only be called from inside subs */
1498 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1499 return CxLVAL(cxstack + cxix);
1505 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1510 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1512 for (i = startingblock; i >= 0; i--) {
1513 register const PERL_CONTEXT * const cx = &cxstk[i];
1514 switch (CxTYPE(cx)) {
1520 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1528 S_dopoptoeval(pTHX_ I32 startingblock)
1532 for (i = startingblock; i >= 0; i--) {
1533 register const PERL_CONTEXT *cx = &cxstack[i];
1534 switch (CxTYPE(cx)) {
1538 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1546 S_dopoptoloop(pTHX_ I32 startingblock)
1550 for (i = startingblock; i >= 0; i--) {
1551 register const PERL_CONTEXT * const cx = &cxstack[i];
1552 switch (CxTYPE(cx)) {
1558 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1559 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1560 if ((CxTYPE(cx)) == CXt_NULL)
1563 case CXt_LOOP_LAZYIV:
1564 case CXt_LOOP_LAZYSV:
1566 case CXt_LOOP_PLAIN:
1567 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1575 S_dopoptogiven(pTHX_ I32 startingblock)
1579 for (i = startingblock; i >= 0; i--) {
1580 register const PERL_CONTEXT *cx = &cxstack[i];
1581 switch (CxTYPE(cx)) {
1585 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
1587 case CXt_LOOP_PLAIN:
1588 assert(!CxFOREACHDEF(cx));
1590 case CXt_LOOP_LAZYIV:
1591 case CXt_LOOP_LAZYSV:
1593 if (CxFOREACHDEF(cx)) {
1594 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
1603 S_dopoptowhen(pTHX_ I32 startingblock)
1607 for (i = startingblock; i >= 0; i--) {
1608 register const PERL_CONTEXT *cx = &cxstack[i];
1609 switch (CxTYPE(cx)) {
1613 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1621 Perl_dounwind(pTHX_ I32 cxix)
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) %s",
1669 SvPV_nolen_const(err));
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 const char* const msg = SvPVx_nolen_const(exceptsv);
1775 (void)hv_store(GvHVn(PL_incgv),
1776 SvPVX_const(namesv), 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_ "%sCompilation failed in require",
1783 *msg ? msg : "Unknown error\n");
1785 if (in_eval & EVAL_KEEPERR) {
1786 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1787 SvPV_nolen_const(exceptsv));
1790 sv_setsv(ERRSV, exceptsv);
1792 PL_restartjmpenv = restartjmpenv;
1793 PL_restartop = restartop;
1799 write_to_stderr(exceptsv);
1806 dVAR; dSP; dPOPTOPssrl;
1807 if (SvTRUE(left) != SvTRUE(right))
1814 =for apidoc caller_cx
1816 The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1817 returned C<PERL_CONTEXT> structure can be interrogated to find all the
1818 information returned to Perl by C<caller>. Note that XSUBs don't get a
1819 stack frame, so C<caller_cx(0, NULL)> will return information for the
1820 immediately-surrounding Perl code.
1822 This function skips over the automatic calls to C<&DB::sub> made on the
1823 behalf of the debugger. If the stack frame requested was a sub called by
1824 C<DB::sub>, the return value will be the frame for the call to
1825 C<DB::sub>, since that has the correct line number/etc. for the call
1826 site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1827 frame for the sub call itself.
1832 const PERL_CONTEXT *
1833 Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
1835 register I32 cxix = dopoptosub(cxstack_ix);
1836 register const PERL_CONTEXT *cx;
1837 register const PERL_CONTEXT *ccstack = cxstack;
1838 const PERL_SI *top_si = PL_curstackinfo;
1841 /* we may be in a higher stacklevel, so dig down deeper */
1842 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1843 top_si = top_si->si_prev;
1844 ccstack = top_si->si_cxstack;
1845 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1849 /* caller() should not report the automatic calls to &DB::sub */
1850 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1851 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1855 cxix = dopoptosub_at(ccstack, cxix - 1);
1858 cx = &ccstack[cxix];
1859 if (dbcxp) *dbcxp = cx;
1861 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1862 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1863 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1864 field below is defined for any cx. */
1865 /* caller() should not report the automatic calls to &DB::sub */
1866 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1867 cx = &ccstack[dbcxix];
1877 register const PERL_CONTEXT *cx;
1878 const PERL_CONTEXT *dbcx;
1880 const char *stashname;
1886 cx = caller_cx(count, &dbcx);
1888 if (GIMME != G_ARRAY) {
1895 stashname = CopSTASHPV(cx->blk_oldcop);
1896 if (GIMME != G_ARRAY) {
1899 PUSHs(&PL_sv_undef);
1902 sv_setpv(TARG, stashname);
1911 PUSHs(&PL_sv_undef);
1913 mPUSHs(newSVpv(stashname, 0));
1914 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1915 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1918 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1919 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
1920 /* So is ccstack[dbcxix]. */
1922 SV * const sv = newSV(0);
1923 gv_efullname3(sv, cvgv, NULL);
1925 PUSHs(boolSV(CxHASARGS(cx)));
1928 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1929 PUSHs(boolSV(CxHASARGS(cx)));
1933 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1936 gimme = (I32)cx->blk_gimme;
1937 if (gimme == G_VOID)
1938 PUSHs(&PL_sv_undef);
1940 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1941 if (CxTYPE(cx) == CXt_EVAL) {
1943 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1944 PUSHs(cx->blk_eval.cur_text);
1948 else if (cx->blk_eval.old_namesv) {
1949 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1952 /* eval BLOCK (try blocks have old_namesv == 0) */
1954 PUSHs(&PL_sv_undef);
1955 PUSHs(&PL_sv_undef);
1959 PUSHs(&PL_sv_undef);
1960 PUSHs(&PL_sv_undef);
1962 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1963 && CopSTASH_eq(PL_curcop, PL_debstash))
1965 AV * const ary = cx->blk_sub.argarray;
1966 const int off = AvARRAY(ary) - AvALLOC(ary);
1969 Perl_init_dbargs(aTHX);
1971 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1972 av_extend(PL_dbargs, AvFILLp(ary) + off);
1973 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1974 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1976 /* XXX only hints propagated via op_private are currently
1977 * visible (others are not easily accessible, since they
1978 * use the global PL_hints) */
1979 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1982 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1984 if (old_warnings == pWARN_NONE ||
1985 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1986 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1987 else if (old_warnings == pWARN_ALL ||
1988 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1989 /* Get the bit mask for $warnings::Bits{all}, because
1990 * it could have been extended by warnings::register */
1992 HV * const bits = get_hv("warnings::Bits", 0);
1993 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
1994 mask = newSVsv(*bits_all);
1997 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
2001 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
2005 PUSHs(cx->blk_oldcop->cop_hints_hash ?
2006 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
2015 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
2016 sv_reset(tmps, CopSTASH(PL_curcop));
2021 /* like pp_nextstate, but used instead when the debugger is active */
2026 PL_curcop = (COP*)PL_op;
2027 TAINT_NOT; /* Each statement is presumed innocent */
2028 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
2033 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
2034 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
2037 register PERL_CONTEXT *cx;
2038 const I32 gimme = G_ARRAY;
2040 GV * const gv = PL_DBgv;
2041 register CV * const cv = GvCV(gv);
2044 DIE(aTHX_ "No DB::DB routine defined");
2046 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2047 /* don't do recursive DB::DB call */
2062 (void)(*CvXSUB(cv))(aTHX_ cv);
2069 PUSHBLOCK(cx, CXt_SUB, SP);
2071 cx->blk_sub.retop = PL_op->op_next;
2074 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
2075 RETURNOP(CvSTART(cv));
2085 register PERL_CONTEXT *cx;
2086 const I32 gimme = GIMME_V;
2087 void *itervar; /* location of the iteration variable */
2088 U8 cxtype = CXt_LOOP_FOR;
2090 ENTER_with_name("loop1");
2093 if (PL_op->op_targ) { /* "my" variable */
2094 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
2095 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2096 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2097 SVs_PADSTALE, SVs_PADSTALE);
2099 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
2101 itervar = PL_comppad;
2103 itervar = &PAD_SVl(PL_op->op_targ);
2106 else { /* symbol table variable */
2107 GV * const gv = MUTABLE_GV(POPs);
2108 SV** svp = &GvSV(gv);
2109 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
2111 itervar = (void *)gv;
2114 if (PL_op->op_private & OPpITER_DEF)
2115 cxtype |= CXp_FOR_DEF;
2117 ENTER_with_name("loop2");
2119 PUSHBLOCK(cx, cxtype, SP);
2120 PUSHLOOP_FOR(cx, itervar, MARK);
2121 if (PL_op->op_flags & OPf_STACKED) {
2122 SV *maybe_ary = POPs;
2123 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2125 SV * const right = maybe_ary;
2128 if (RANGE_IS_NUMERIC(sv,right)) {
2129 cx->cx_type &= ~CXTYPEMASK;
2130 cx->cx_type |= CXt_LOOP_LAZYIV;
2131 /* Make sure that no-one re-orders cop.h and breaks our
2133 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
2134 #ifdef NV_PRESERVES_UV
2135 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
2136 (SvNV(sv) > (NV)IV_MAX)))
2138 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
2139 (SvNV(right) < (NV)IV_MIN))))
2141 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
2144 ((SvUV(sv) > (UV)IV_MAX) ||
2145 (SvNV(sv) > (NV)UV_MAX)))))
2147 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
2149 ((SvNV(right) > 0) &&
2150 ((SvUV(right) > (UV)IV_MAX) ||
2151 (SvNV(right) > (NV)UV_MAX))))))
2153 DIE(aTHX_ "Range iterator outside integer range");
2154 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2155 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
2157 /* for correct -Dstv display */
2158 cx->blk_oldsp = sp - PL_stack_base;
2162 cx->cx_type &= ~CXTYPEMASK;
2163 cx->cx_type |= CXt_LOOP_LAZYSV;
2164 /* Make sure that no-one re-orders cop.h and breaks our
2166 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2167 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2168 cx->blk_loop.state_u.lazysv.end = right;
2169 SvREFCNT_inc(right);
2170 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2171 /* This will do the upgrade to SVt_PV, and warn if the value
2172 is uninitialised. */
2173 (void) SvPV_nolen_const(right);
2174 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2175 to replace !SvOK() with a pointer to "". */
2177 SvREFCNT_dec(right);
2178 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2182 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2183 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2184 SvREFCNT_inc(maybe_ary);
2185 cx->blk_loop.state_u.ary.ix =
2186 (PL_op->op_private & OPpITER_REVERSED) ?
2187 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2191 else { /* iterating over items on the stack */
2192 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2193 if (PL_op->op_private & OPpITER_REVERSED) {
2194 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2197 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2207 register PERL_CONTEXT *cx;
2208 const I32 gimme = GIMME_V;
2210 ENTER_with_name("loop1");
2212 ENTER_with_name("loop2");
2214 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2215 PUSHLOOP_PLAIN(cx, SP);
2223 register PERL_CONTEXT *cx;
2230 assert(CxTYPE_is_LOOP(cx));
2232 newsp = PL_stack_base + cx->blk_loop.resetsp;
2235 if (gimme == G_VOID)
2237 else if (gimme == G_SCALAR) {
2239 *++newsp = sv_mortalcopy(*SP);
2241 *++newsp = &PL_sv_undef;
2245 *++newsp = sv_mortalcopy(*++mark);
2246 TAINT_NOT; /* Each item is independent */
2252 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2253 PL_curpm = newpm; /* ... and pop $1 et al */
2255 LEAVE_with_name("loop2");
2256 LEAVE_with_name("loop1");
2264 register PERL_CONTEXT *cx;
2265 bool popsub2 = FALSE;
2266 bool clear_errsv = FALSE;
2276 const I32 cxix = dopoptosub(cxstack_ix);
2279 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2280 * sort block, which is a CXt_NULL
2283 PL_stack_base[1] = *PL_stack_sp;
2284 PL_stack_sp = PL_stack_base + 1;
2288 DIE(aTHX_ "Can't return outside a subroutine");
2290 if (cxix < cxstack_ix)
2293 if (CxMULTICALL(&cxstack[cxix])) {
2294 gimme = cxstack[cxix].blk_gimme;
2295 if (gimme == G_VOID)
2296 PL_stack_sp = PL_stack_base;
2297 else if (gimme == G_SCALAR) {
2298 PL_stack_base[1] = *PL_stack_sp;
2299 PL_stack_sp = PL_stack_base + 1;
2305 switch (CxTYPE(cx)) {
2308 lval = !!CvLVALUE(cx->blk_sub.cv);
2309 retop = cx->blk_sub.retop;
2310 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2313 if (!(PL_in_eval & EVAL_KEEPERR))
2316 namesv = cx->blk_eval.old_namesv;
2317 retop = cx->blk_eval.retop;
2320 if (optype == OP_REQUIRE &&
2321 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2323 /* Unassume the success we assumed earlier. */
2324 (void)hv_delete(GvHVn(PL_incgv),
2325 SvPVX_const(namesv), SvCUR(namesv),
2327 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
2332 retop = cx->blk_sub.retop;
2335 DIE(aTHX_ "panic: return");
2339 if (gimme == G_SCALAR) {
2342 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2344 *++newsp = SvREFCNT_inc(*SP);
2349 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2351 *++newsp = sv_mortalcopy(sv);
2357 (lval || SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
2360 *++newsp = sv_mortalcopy(*SP);
2363 *++newsp = &PL_sv_undef;
2365 else if (gimme == G_ARRAY) {
2366 while (++MARK <= SP) {
2367 *++newsp = popsub2 && (lval || SvTEMP(*MARK))
2368 ? *MARK : sv_mortalcopy(*MARK);
2369 TAINT_NOT; /* Each item is independent */
2372 PL_stack_sp = newsp;
2375 /* Stack values are safe: */
2378 POPSUB(cx,sv); /* release CV and @_ ... */
2382 PL_curpm = newpm; /* ... and pop $1 et al */
2395 register PERL_CONTEXT *cx;
2406 if (PL_op->op_flags & OPf_SPECIAL) {
2407 cxix = dopoptoloop(cxstack_ix);
2409 DIE(aTHX_ "Can't \"last\" outside a loop block");
2412 cxix = dopoptolabel(cPVOP->op_pv);
2414 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
2416 if (cxix < cxstack_ix)
2420 cxstack_ix++; /* temporarily protect top context */
2422 switch (CxTYPE(cx)) {
2423 case CXt_LOOP_LAZYIV:
2424 case CXt_LOOP_LAZYSV:
2426 case CXt_LOOP_PLAIN:
2428 newsp = PL_stack_base + cx->blk_loop.resetsp;
2429 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2433 nextop = cx->blk_sub.retop;
2437 nextop = cx->blk_eval.retop;
2441 nextop = cx->blk_sub.retop;
2444 DIE(aTHX_ "panic: last");
2448 if (gimme == G_SCALAR) {
2450 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2451 ? *SP : sv_mortalcopy(*SP);
2453 *++newsp = &PL_sv_undef;
2455 else if (gimme == G_ARRAY) {
2456 while (++MARK <= SP) {
2457 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2458 ? *MARK : sv_mortalcopy(*MARK);
2459 TAINT_NOT; /* Each item is independent */
2467 /* Stack values are safe: */
2469 case CXt_LOOP_LAZYIV:
2470 case CXt_LOOP_PLAIN:
2471 case CXt_LOOP_LAZYSV:
2473 POPLOOP(cx); /* release loop vars ... */
2477 POPSUB(cx,sv); /* release CV and @_ ... */
2480 PL_curpm = newpm; /* ... and pop $1 et al */
2483 PERL_UNUSED_VAR(optype);
2484 PERL_UNUSED_VAR(gimme);
2492 register PERL_CONTEXT *cx;
2495 if (PL_op->op_flags & OPf_SPECIAL) {
2496 cxix = dopoptoloop(cxstack_ix);
2498 DIE(aTHX_ "Can't \"next\" outside a loop block");
2501 cxix = dopoptolabel(cPVOP->op_pv);
2503 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2505 if (cxix < cxstack_ix)
2508 /* clear off anything above the scope we're re-entering, but
2509 * save the rest until after a possible continue block */
2510 inner = PL_scopestack_ix;
2512 if (PL_scopestack_ix < inner)
2513 leave_scope(PL_scopestack[PL_scopestack_ix]);
2514 PL_curcop = cx->blk_oldcop;
2515 return (cx)->blk_loop.my_op->op_nextop;
2522 register PERL_CONTEXT *cx;
2526 if (PL_op->op_flags & OPf_SPECIAL) {
2527 cxix = dopoptoloop(cxstack_ix);
2529 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2532 cxix = dopoptolabel(cPVOP->op_pv);
2534 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2536 if (cxix < cxstack_ix)
2539 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2540 if (redo_op->op_type == OP_ENTER) {
2541 /* pop one less context to avoid $x being freed in while (my $x..) */
2543 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2544 redo_op = redo_op->op_next;
2548 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2549 LEAVE_SCOPE(oldsave);
2551 PL_curcop = cx->blk_oldcop;
2556 S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
2560 static const char too_deep[] = "Target of goto is too deeply nested";
2562 PERL_ARGS_ASSERT_DOFINDLABEL;
2565 Perl_croak(aTHX_ too_deep);
2566 if (o->op_type == OP_LEAVE ||
2567 o->op_type == OP_SCOPE ||
2568 o->op_type == OP_LEAVELOOP ||
2569 o->op_type == OP_LEAVESUB ||
2570 o->op_type == OP_LEAVETRY)
2572 *ops++ = cUNOPo->op_first;
2574 Perl_croak(aTHX_ too_deep);
2577 if (o->op_flags & OPf_KIDS) {
2579 /* First try all the kids at this level, since that's likeliest. */
2580 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2581 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2582 const char *kid_label = CopLABEL(kCOP);
2583 if (kid_label && strEQ(kid_label, label))
2587 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2588 if (kid == PL_lastgotoprobe)
2590 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2593 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2594 ops[-1]->op_type == OP_DBSTATE)
2599 if ((o = dofindlabel(kid, label, ops, oplimit)))
2612 register PERL_CONTEXT *cx;
2613 #define GOTO_DEPTH 64
2614 OP *enterops[GOTO_DEPTH];
2615 const char *label = NULL;
2616 const bool do_dump = (PL_op->op_type == OP_DUMP);
2617 static const char must_have_label[] = "goto must have label";
2619 if (PL_op->op_flags & OPf_STACKED) {
2620 SV * const sv = POPs;
2622 /* This egregious kludge implements goto &subroutine */
2623 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2625 register PERL_CONTEXT *cx;
2626 CV *cv = MUTABLE_CV(SvRV(sv));
2633 if (!CvROOT(cv) && !CvXSUB(cv)) {
2634 const GV * const gv = CvGV(cv);
2638 /* autoloaded stub? */
2639 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2641 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2642 GvNAMELEN(gv), FALSE);
2643 if (autogv && (cv = GvCV(autogv)))
2645 tmpstr = sv_newmortal();
2646 gv_efullname3(tmpstr, gv, NULL);
2647 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2649 DIE(aTHX_ "Goto undefined subroutine");
2652 /* First do some returnish stuff. */
2653 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2655 cxix = dopoptosub(cxstack_ix);
2657 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2658 if (cxix < cxstack_ix)
2662 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2663 if (CxTYPE(cx) == CXt_EVAL) {
2665 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2667 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2669 else if (CxMULTICALL(cx))
2670 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2671 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2672 /* put @_ back onto stack */
2673 AV* av = cx->blk_sub.argarray;
2675 items = AvFILLp(av) + 1;
2676 EXTEND(SP, items+1); /* @_ could have been extended. */
2677 Copy(AvARRAY(av), SP + 1, items, SV*);
2678 SvREFCNT_dec(GvAV(PL_defgv));
2679 GvAV(PL_defgv) = cx->blk_sub.savearray;
2681 /* abandon @_ if it got reified */
2686 av_extend(av, items-1);
2688 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2691 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2692 AV* const av = GvAV(PL_defgv);
2693 items = AvFILLp(av) + 1;
2694 EXTEND(SP, items+1); /* @_ could have been extended. */
2695 Copy(AvARRAY(av), SP + 1, items, SV*);
2699 if (CxTYPE(cx) == CXt_SUB &&
2700 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2701 SvREFCNT_dec(cx->blk_sub.cv);
2702 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2703 LEAVE_SCOPE(oldsave);
2705 /* Now do some callish stuff. */
2707 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2709 OP* const retop = cx->blk_sub.retop;
2710 SV **newsp __attribute__unused__;
2711 I32 gimme __attribute__unused__;
2714 for (index=0; index<items; index++)
2715 sv_2mortal(SP[-index]);
2718 /* XS subs don't have a CxSUB, so pop it */
2719 POPBLOCK(cx, PL_curpm);
2720 /* Push a mark for the start of arglist */
2723 (void)(*CvXSUB(cv))(aTHX_ cv);
2728 AV* const padlist = CvPADLIST(cv);
2729 if (CxTYPE(cx) == CXt_EVAL) {
2730 PL_in_eval = CxOLD_IN_EVAL(cx);
2731 PL_eval_root = cx->blk_eval.old_eval_root;
2732 cx->cx_type = CXt_SUB;
2734 cx->blk_sub.cv = cv;
2735 cx->blk_sub.olddepth = CvDEPTH(cv);
2738 if (CvDEPTH(cv) < 2)
2739 SvREFCNT_inc_simple_void_NN(cv);
2741 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2742 sub_crush_depth(cv);
2743 pad_push(padlist, CvDEPTH(cv));
2746 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2749 AV *const av = MUTABLE_AV(PAD_SVl(0));
2751 cx->blk_sub.savearray = GvAV(PL_defgv);
2752 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
2753 CX_CURPAD_SAVE(cx->blk_sub);
2754 cx->blk_sub.argarray = av;
2756 if (items >= AvMAX(av) + 1) {
2757 SV **ary = AvALLOC(av);
2758 if (AvARRAY(av) != ary) {
2759 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2762 if (items >= AvMAX(av) + 1) {
2763 AvMAX(av) = items - 1;
2764 Renew(ary,items+1,SV*);
2770 Copy(mark,AvARRAY(av),items,SV*);
2771 AvFILLp(av) = items - 1;
2772 assert(!AvREAL(av));
2774 /* transfer 'ownership' of refcnts to new @_ */
2784 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2785 Perl_get_db_sub(aTHX_ NULL, cv);
2787 CV * const gotocv = get_cvs("DB::goto", 0);
2789 PUSHMARK( PL_stack_sp );
2790 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
2795 RETURNOP(CvSTART(cv));
2799 label = SvPV_nolen_const(sv);
2800 if (!(do_dump || *label))
2801 DIE(aTHX_ must_have_label);
2804 else if (PL_op->op_flags & OPf_SPECIAL) {
2806 DIE(aTHX_ must_have_label);
2809 label = cPVOP->op_pv;
2813 if (label && *label) {
2814 OP *gotoprobe = NULL;
2815 bool leaving_eval = FALSE;
2816 bool in_block = FALSE;
2817 PERL_CONTEXT *last_eval_cx = NULL;
2821 PL_lastgotoprobe = NULL;
2823 for (ix = cxstack_ix; ix >= 0; ix--) {
2825 switch (CxTYPE(cx)) {
2827 leaving_eval = TRUE;
2828 if (!CxTRYBLOCK(cx)) {
2829 gotoprobe = (last_eval_cx ?
2830 last_eval_cx->blk_eval.old_eval_root :
2835 /* else fall through */
2836 case CXt_LOOP_LAZYIV:
2837 case CXt_LOOP_LAZYSV:
2839 case CXt_LOOP_PLAIN:
2842 gotoprobe = cx->blk_oldcop->op_sibling;
2848 gotoprobe = cx->blk_oldcop->op_sibling;
2851 gotoprobe = PL_main_root;
2854 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
2855 gotoprobe = CvROOT(cx->blk_sub.cv);
2861 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2864 DIE(aTHX_ "panic: goto");
2865 gotoprobe = PL_main_root;
2869 retop = dofindlabel(gotoprobe, label,
2870 enterops, enterops + GOTO_DEPTH);
2873 if (gotoprobe->op_sibling &&
2874 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
2875 gotoprobe->op_sibling->op_sibling) {
2876 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
2877 label, enterops, enterops + GOTO_DEPTH);
2882 PL_lastgotoprobe = gotoprobe;
2885 DIE(aTHX_ "Can't find label %s", label);
2887 /* if we're leaving an eval, check before we pop any frames
2888 that we're not going to punt, otherwise the error
2891 if (leaving_eval && *enterops && enterops[1]) {
2893 for (i = 1; enterops[i]; i++)
2894 if (enterops[i]->op_type == OP_ENTERITER)
2895 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2898 if (*enterops && enterops[1]) {
2899 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2901 deprecate("\"goto\" to jump into a construct");
2904 /* pop unwanted frames */
2906 if (ix < cxstack_ix) {
2913 oldsave = PL_scopestack[PL_scopestack_ix];
2914 LEAVE_SCOPE(oldsave);
2917 /* push wanted frames */
2919 if (*enterops && enterops[1]) {
2920 OP * const oldop = PL_op;
2921 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2922 for (; enterops[ix]; ix++) {
2923 PL_op = enterops[ix];
2924 /* Eventually we may want to stack the needed arguments
2925 * for each op. For now, we punt on the hard ones. */
2926 if (PL_op->op_type == OP_ENTERITER)
2927 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2928 PL_op->op_ppaddr(aTHX);
2936 if (!retop) retop = PL_main_start;
2938 PL_restartop = retop;
2939 PL_do_undump = TRUE;
2943 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2944 PL_do_undump = FALSE;
2961 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2963 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
2966 PL_exit_flags |= PERL_EXIT_EXPECTED;
2968 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2969 if (anum || !(PL_minus_c && PL_madskills))
2974 PUSHs(&PL_sv_undef);
2981 S_save_lines(pTHX_ AV *array, SV *sv)
2983 const char *s = SvPVX_const(sv);
2984 const char * const send = SvPVX_const(sv) + SvCUR(sv);
2987 PERL_ARGS_ASSERT_SAVE_LINES;
2989 while (s && s < send) {
2991 SV * const tmpstr = newSV_type(SVt_PVMG);
2993 t = (const char *)memchr(s, '\n', send - s);
2999 sv_setpvn(tmpstr, s, t - s);
3000 av_store(array, line++, tmpstr);
3008 Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
3010 0 is used as continue inside eval,
3012 3 is used for a die caught by an inner eval - continue inner loop
3014 See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
3015 establish a local jmpenv to handle exception traps.
3020 S_docatch(pTHX_ OP *o)
3024 OP * const oldop = PL_op;
3028 assert(CATCH_GET == TRUE);
3035 assert(cxstack_ix >= 0);
3036 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3037 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
3042 /* die caught by an inner eval - continue inner loop */
3043 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3044 PL_restartjmpenv = NULL;
3045 PL_op = PL_restartop;
3061 /* James Bond: Do you expect me to talk?
3062 Auric Goldfinger: No, Mr. Bond. I expect you to die.
3064 This code is an ugly hack, doesn't work with lexicals in subroutines that are
3065 called more than once, and is only used by regcomp.c, for (?{}) blocks.
3067 Currently it is not used outside the core code. Best if it stays that way.
3069 Hence it's now deprecated, and will be removed.
3072 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
3073 /* sv Text to convert to OP tree. */
3074 /* startop op_free() this to undo. */
3075 /* code Short string id of the caller. */
3077 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3078 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
3081 /* Don't use this. It will go away without warning once the regexp engine is
3082 refactored not to use it. */
3084 Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3087 dVAR; dSP; /* Make POPBLOCK work. */
3093 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3094 char *tmpbuf = tbuf;
3097 CV* runcv = NULL; /* initialise to avoid compiler warnings */
3101 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
3103 ENTER_with_name("eval");
3104 lex_start(sv, NULL, LEX_START_SAME_FILTER);
3106 /* switch to eval mode */
3108 if (IN_PERL_COMPILETIME) {
3109 SAVECOPSTASH_FREE(&PL_compiling);
3110 CopSTASH_set(&PL_compiling, PL_curstash);
3112 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3113 SV * const sv = sv_newmortal();
3114 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3115 code, (unsigned long)++PL_evalseq,
3116 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3121 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3122 (unsigned long)++PL_evalseq);
3123 SAVECOPFILE_FREE(&PL_compiling);
3124 CopFILE_set(&PL_compiling, tmpbuf+2);
3125 SAVECOPLINE(&PL_compiling);
3126 CopLINE_set(&PL_compiling, 1);
3127 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3128 deleting the eval's FILEGV from the stash before gv_check() runs
3129 (i.e. before run-time proper). To work around the coredump that
3130 ensues, we always turn GvMULTI_on for any globals that were
3131 introduced within evals. See force_ident(). GSAR 96-10-12 */
3132 safestr = savepvn(tmpbuf, len);
3133 SAVEDELETE(PL_defstash, safestr, len);
3135 #ifdef OP_IN_REGISTER
3141 /* we get here either during compilation, or via pp_regcomp at runtime */
3142 runtime = IN_PERL_RUNTIME;
3145 runcv = find_runcv(NULL);
3147 /* At run time, we have to fetch the hints from PL_curcop. */
3148 PL_hints = PL_curcop->cop_hints;
3149 if (PL_hints & HINT_LOCALIZE_HH) {
3150 /* SAVEHINTS created a new HV in PL_hintgv, which we
3152 SvREFCNT_dec(GvHV(PL_hintgv));
3154 refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0);
3155 hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints);
3157 SAVECOMPILEWARNINGS();
3158 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3159 cophh_free(CopHINTHASH_get(&PL_compiling));
3160 /* XXX Does this need to avoid copying a label? */
3161 PL_compiling.cop_hints_hash
3162 = cophh_copy(PL_curcop->cop_hints_hash);
3166 PL_op->op_type = OP_ENTEREVAL;
3167 PL_op->op_flags = 0; /* Avoid uninit warning. */
3168 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
3170 need_catch = CATCH_GET;
3174 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
3176 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
3177 CATCH_SET(need_catch);
3178 POPBLOCK(cx,PL_curpm);
3181 (*startop)->op_type = OP_NULL;
3182 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
3183 /* XXX DAPM do this properly one year */
3184 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
3185 LEAVE_with_name("eval");
3186 if (IN_PERL_COMPILETIME)
3187 CopHINTS_set(&PL_compiling, PL_hints);
3188 #ifdef OP_IN_REGISTER
3191 PERL_UNUSED_VAR(newsp);
3192 PERL_UNUSED_VAR(optype);
3194 return PL_eval_start;
3199 =for apidoc find_runcv
3201 Locate the CV corresponding to the currently executing sub or eval.
3202 If db_seqp is non_null, skip CVs that are in the DB package and populate
3203 *db_seqp with the cop sequence number at the point that the DB:: code was
3204 entered. (allows debuggers to eval in the scope of the breakpoint rather
3205 than in the scope of the debugger itself).
3211 Perl_find_runcv(pTHX_ U32 *db_seqp)
3217 *db_seqp = PL_curcop->cop_seq;
3218 for (si = PL_curstackinfo; si; si = si->si_prev) {
3220 for (ix = si->si_cxix; ix >= 0; ix--) {
3221 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3222 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3223 CV * const cv = cx->blk_sub.cv;
3224 /* skip DB:: code */
3225 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3226 *db_seqp = cx->blk_oldcop->cop_seq;
3231 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3239 /* Run yyparse() in a setjmp wrapper. Returns:
3240 * 0: yyparse() successful
3241 * 1: yyparse() failed
3245 S_try_yyparse(pTHX_ int gramtype)
3250 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3254 ret = yyparse(gramtype) ? 1 : 0;
3268 /* Compile a require/do, an eval '', or a /(?{...})/.
3269 * In the last case, startop is non-null, and contains the address of
3270 * a pointer that should be set to the just-compiled code.
3271 * outside is the lexically enclosing CV (if any) that invoked us.
3272 * Returns a bool indicating whether the compile was successful; if so,
3273 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3274 * pushes undef (also croaks if startop != NULL).
3278 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
3281 OP * const saveop = PL_op;
3282 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3285 PL_in_eval = (in_require
3286 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3291 SAVESPTR(PL_compcv);
3292 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3293 CvEVAL_on(PL_compcv);
3294 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3295 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3297 CvOUTSIDE_SEQ(PL_compcv) = seq;
3298 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3300 /* set up a scratch pad */
3302 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
3303 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3307 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
3309 /* make sure we compile in the right package */
3311 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3312 SAVESPTR(PL_curstash);
3313 PL_curstash = CopSTASH(PL_curcop);
3315 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3316 SAVESPTR(PL_beginav);
3317 PL_beginav = newAV();
3318 SAVEFREESV(PL_beginav);
3319 SAVESPTR(PL_unitcheckav);
3320 PL_unitcheckav = newAV();
3321 SAVEFREESV(PL_unitcheckav);
3324 SAVEBOOL(PL_madskills);
3328 /* try to compile it */
3330 PL_eval_root = NULL;
3331 PL_curcop = &PL_compiling;
3332 CopARYBASE_set(PL_curcop, 0);
3333 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3334 PL_in_eval |= EVAL_KEEPERR;
3338 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3340 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3341 * so honour CATCH_GET and trap it here if necessary */
3343 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3345 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3346 SV **newsp; /* Used by POPBLOCK. */
3347 PERL_CONTEXT *cx = NULL;
3348 I32 optype; /* Used by POPEVAL. */
3352 PERL_UNUSED_VAR(newsp);
3353 PERL_UNUSED_VAR(optype);
3355 /* note that if yystatus == 3, then the EVAL CX block has already
3356 * been popped, and various vars restored */
3358 if (yystatus != 3) {
3360 op_free(PL_eval_root);
3361 PL_eval_root = NULL;
3363 SP = PL_stack_base + POPMARK; /* pop original mark */
3365 POPBLOCK(cx,PL_curpm);
3367 namesv = cx->blk_eval.old_namesv;
3371 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3373 msg = SvPVx_nolen_const(ERRSV);
3376 /* If cx is still NULL, it means that we didn't go in the
3377 * POPEVAL branch. */
3378 cx = &cxstack[cxstack_ix];
3379 assert(CxTYPE(cx) == CXt_EVAL);
3380 namesv = cx->blk_eval.old_namesv;
3382 (void)hv_store(GvHVn(PL_incgv),
3383 SvPVX_const(namesv), SvCUR(namesv),
3385 Perl_croak(aTHX_ "%sCompilation failed in require",
3386 *msg ? msg : "Unknown error\n");
3389 if (yystatus != 3) {
3390 POPBLOCK(cx,PL_curpm);
3393 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3394 (*msg ? msg : "Unknown error\n"));
3398 sv_setpvs(ERRSV, "Compilation error");
3401 PUSHs(&PL_sv_undef);
3405 CopLINE_set(&PL_compiling, 0);
3407 *startop = PL_eval_root;
3409 SAVEFREEOP(PL_eval_root);
3411 /* Set the context for this new optree.
3412 * Propagate the context from the eval(). */
3413 if ((gimme & G_WANT) == G_VOID)
3414 scalarvoid(PL_eval_root);
3415 else if ((gimme & G_WANT) == G_ARRAY)
3418 scalar(PL_eval_root);
3420 DEBUG_x(dump_eval());
3422 /* Register with debugger: */
3423 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3424 CV * const cv = get_cvs("DB::postponed", 0);
3428 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3430 call_sv(MUTABLE_SV(cv), G_DISCARD);
3434 if (PL_unitcheckav) {
3435 OP *es = PL_eval_start;
3436 call_list(PL_scopestack_ix, PL_unitcheckav);
3440 /* compiled okay, so do it */
3442 CvDEPTH(PL_compcv) = 1;
3443 SP = PL_stack_base + POPMARK; /* pop original mark */
3444 PL_op = saveop; /* The caller may need it. */
3445 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3452 S_check_type_and_open(pTHX_ SV *name)
3455 const char *p = SvPV_nolen_const(name);
3456 const int st_rc = PerlLIO_stat(p, &st);
3458 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3460 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3464 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3465 return PerlIO_openn(aTHX_ NULL, PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3467 return PerlIO_open(p, PERL_SCRIPT_MODE);
3471 #ifndef PERL_DISABLE_PMC
3473 S_doopen_pm(pTHX_ SV *name)
3476 const char *p = SvPV_const(name, namelen);
3478 PERL_ARGS_ASSERT_DOOPEN_PM;
3480 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3481 SV *const pmcsv = sv_newmortal();
3484 SvSetSV_nosteal(pmcsv,name);
3485 sv_catpvn(pmcsv, "c", 1);
3487 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3488 return check_type_and_open(pmcsv);
3490 return check_type_and_open(name);
3493 # define doopen_pm(name) check_type_and_open(name)
3494 #endif /* !PERL_DISABLE_PMC */
3499 register PERL_CONTEXT *cx;
3506 int vms_unixname = 0;
3508 const char *tryname = NULL;
3510 const I32 gimme = GIMME_V;
3511 int filter_has_file = 0;
3512 PerlIO *tryrsfp = NULL;
3513 SV *filter_cache = NULL;
3514 SV *filter_state = NULL;
3515 SV *filter_sub = NULL;
3521 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3522 sv = sv_2mortal(new_version(sv));
3523 if (!sv_derived_from(PL_patchlevel, "version"))
3524 upg_version(PL_patchlevel, TRUE);
3525 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3526 if ( vcmp(sv,PL_patchlevel) <= 0 )
3527 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3528 SVfARG(sv_2mortal(vnormal(sv))),
3529 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3533 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3536 SV * const req = SvRV(sv);
3537 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3539 /* get the left hand term */
3540 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3542 first = SvIV(*av_fetch(lav,0,0));
3543 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3544 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3545 || av_len(lav) > 1 /* FP with > 3 digits */
3546 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3548 DIE(aTHX_ "Perl %"SVf" required--this is only "
3550 SVfARG(sv_2mortal(vnormal(req))),
3551 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3554 else { /* probably 'use 5.10' or 'use 5.8' */
3559 second = SvIV(*av_fetch(lav,1,0));
3561 second /= second >= 600 ? 100 : 10;
3562 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3563 (int)first, (int)second);
3564 upg_version(hintsv, TRUE);
3566 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3567 "--this is only %"SVf", stopped",
3568 SVfARG(sv_2mortal(vnormal(req))),
3569 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3570 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3578 name = SvPV_const(sv, len);
3579 if (!(name && len > 0 && *name))
3580 DIE(aTHX_ "Null filename used");
3581 TAINT_PROPER("require");
3585 /* The key in the %ENV hash is in the syntax of file passed as the argument
3586 * usually this is in UNIX format, but sometimes in VMS format, which
3587 * can result in a module being pulled in more than once.
3588 * To prevent this, the key must be stored in UNIX format if the VMS
3589 * name can be translated to UNIX.
3591 if ((unixname = tounixspec(name, NULL)) != NULL) {
3592 unixlen = strlen(unixname);
3598 /* if not VMS or VMS name can not be translated to UNIX, pass it
3601 unixname = (char *) name;
3604 if (PL_op->op_type == OP_REQUIRE) {
3605 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3606 unixname, unixlen, 0);
3608 if (*svp != &PL_sv_undef)
3611 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3612 "Compilation failed in require", unixname);
3616 /* prepare to compile file */
3618 if (path_is_absolute(name)) {
3619 /* At this point, name is SvPVX(sv) */
3621 tryrsfp = doopen_pm(sv);
3624 AV * const ar = GvAVn(PL_incgv);
3630 namesv = newSV_type(SVt_PV);
3631 for (i = 0; i <= AvFILL(ar); i++) {
3632 SV * const dirsv = *av_fetch(ar, i, TRUE);
3634 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3641 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3642 && !sv_isobject(loader))
3644 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3647 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3648 PTR2UV(SvRV(dirsv)), name);
3649 tryname = SvPVX_const(namesv);
3652 ENTER_with_name("call_INC");
3660 if (sv_isobject(loader))
3661 count = call_method("INC", G_ARRAY);
3663 count = call_sv(loader, G_ARRAY);
3673 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3674 && !isGV_with_GP(SvRV(arg))) {
3675 filter_cache = SvRV(arg);
3676 SvREFCNT_inc_simple_void_NN(filter_cache);
3683 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
3687 if (isGV_with_GP(arg)) {
3688 IO * const io = GvIO((const GV *)arg);
3693 tryrsfp = IoIFP(io);
3694 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3695 PerlIO_close(IoOFP(io));
3706 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3708 SvREFCNT_inc_simple_void_NN(filter_sub);
3711 filter_state = SP[i];
3712 SvREFCNT_inc_simple_void(filter_state);
3716 if (!tryrsfp && (filter_cache || filter_sub)) {
3717 tryrsfp = PerlIO_open(BIT_BUCKET,
3725 LEAVE_with_name("call_INC");
3727 /* Adjust file name if the hook has set an %INC entry.
3728 This needs to happen after the FREETMPS above. */
3729 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3731 tryname = SvPV_nolen_const(*svp);
3738 filter_has_file = 0;
3740 SvREFCNT_dec(filter_cache);
3741 filter_cache = NULL;
3744 SvREFCNT_dec(filter_state);
3745 filter_state = NULL;
3748 SvREFCNT_dec(filter_sub);
3753 if (!path_is_absolute(name)
3759 dir = SvPV_const(dirsv, dirlen);
3767 if ((unixdir = tounixpath(dir, NULL)) == NULL)
3769 sv_setpv(namesv, unixdir);
3770 sv_catpv(namesv, unixname);
3772 # ifdef __SYMBIAN32__
3773 if (PL_origfilename[0] &&
3774 PL_origfilename[1] == ':' &&
3775 !(dir[0] && dir[1] == ':'))
3776 Perl_sv_setpvf(aTHX_ namesv,
3781 Perl_sv_setpvf(aTHX_ namesv,
3785 /* The equivalent of
3786 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3787 but without the need to parse the format string, or
3788 call strlen on either pointer, and with the correct
3789 allocation up front. */
3791 char *tmp = SvGROW(namesv, dirlen + len + 2);
3793 memcpy(tmp, dir, dirlen);
3796 /* name came from an SV, so it will have a '\0' at the
3797 end that we can copy as part of this memcpy(). */
3798 memcpy(tmp, name, len + 1);
3800 SvCUR_set(namesv, dirlen + len + 1);
3805 TAINT_PROPER("require");
3806 tryname = SvPVX_const(namesv);
3807 tryrsfp = doopen_pm(namesv);
3809 if (tryname[0] == '.' && tryname[1] == '/') {
3811 while (*++tryname == '/');
3815 else if (errno == EMFILE)
3816 /* no point in trying other paths if out of handles */
3825 if (PL_op->op_type == OP_REQUIRE) {
3826 if(errno == EMFILE) {
3827 /* diag_listed_as: Can't locate %s */
3828 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno));
3830 if (namesv) { /* did we lookup @INC? */
3831 AV * const ar = GvAVn(PL_incgv);
3833 SV *const inc = newSVpvs_flags("", SVs_TEMP);
3834 for (i = 0; i <= AvFILL(ar); i++) {
3835 sv_catpvs(inc, " ");
3836 sv_catsv(inc, *av_fetch(ar, i, TRUE));
3839 /* diag_listed_as: Can't locate %s */
3841 "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")",
3843 (memEQ(name + len - 2, ".h", 3)
3844 ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""),
3845 (memEQ(name + len - 3, ".ph", 4)
3846 ? " (did you run h2ph?)" : ""),
3851 DIE(aTHX_ "Can't locate %s", name);
3857 SETERRNO(0, SS_NORMAL);
3859 /* Assume success here to prevent recursive requirement. */
3860 /* name is never assigned to again, so len is still strlen(name) */
3861 /* Check whether a hook in @INC has already filled %INC */
3863 (void)hv_store(GvHVn(PL_incgv),
3864 unixname, unixlen, newSVpv(tryname,0),0);
3866 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
3868 (void)hv_store(GvHVn(PL_incgv),
3869 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
3872 ENTER_with_name("eval");
3874 SAVECOPFILE_FREE(&PL_compiling);
3875 CopFILE_set(&PL_compiling, tryname);
3876 lex_start(NULL, tryrsfp, 0);
3880 hv_clear(GvHV(PL_hintgv));
3882 SAVECOMPILEWARNINGS();
3883 if (PL_dowarn & G_WARN_ALL_ON)
3884 PL_compiling.cop_warnings = pWARN_ALL ;
3885 else if (PL_dowarn & G_WARN_ALL_OFF)
3886 PL_compiling.cop_warnings = pWARN_NONE ;
3888 PL_compiling.cop_warnings = pWARN_STD ;
3890 if (filter_sub || filter_cache) {
3891 /* We can use the SvPV of the filter PVIO itself as our cache, rather
3892 than hanging another SV from it. In turn, filter_add() optionally
3893 takes the SV to use as the filter (or creates a new SV if passed
3894 NULL), so simply pass in whatever value filter_cache has. */
3895 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
3896 IoLINES(datasv) = filter_has_file;
3897 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
3898 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
3901 /* switch to eval mode */
3902 PUSHBLOCK(cx, CXt_EVAL, SP);
3904 cx->blk_eval.retop = PL_op->op_next;
3906 SAVECOPLINE(&PL_compiling);
3907 CopLINE_set(&PL_compiling, 0);
3911 /* Store and reset encoding. */
3912 encoding = PL_encoding;
3915 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq))
3916 op = DOCATCH(PL_eval_start);
3918 op = PL_op->op_next;
3920 /* Restore encoding. */
3921 PL_encoding = encoding;
3926 /* This is a op added to hold the hints hash for
3927 pp_entereval. The hash can be modified by the code
3928 being eval'ed, so we return a copy instead. */
3934 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
3942 register PERL_CONTEXT *cx;
3944 const I32 gimme = GIMME_V;
3945 const U32 was = PL_breakable_sub_gen;
3946 char tbuf[TYPE_DIGITS(long) + 12];
3947 bool saved_delete = FALSE;
3948 char *tmpbuf = tbuf;
3952 HV *saved_hh = NULL;
3954 if (PL_op->op_private & OPpEVAL_HAS_HH) {
3955 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
3959 /* make sure we've got a plain PV (no overload etc) before testing
3960 * for taint. Making a copy here is probably overkill, but better
3961 * safe than sorry */
3963 const char * const p = SvPV_const(sv, len);
3965 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
3968 TAINT_IF(SvTAINTED(sv));
3969 TAINT_PROPER("eval");
3971 ENTER_with_name("eval");
3972 lex_start(sv, NULL, LEX_START_SAME_FILTER);
3975 /* switch to eval mode */
3977 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3978 SV * const temp_sv = sv_newmortal();
3979 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
3980 (unsigned long)++PL_evalseq,
3981 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3982 tmpbuf = SvPVX(temp_sv);
3983 len = SvCUR(temp_sv);
3986 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
3987 SAVECOPFILE_FREE(&PL_compiling);
3988 CopFILE_set(&PL_compiling, tmpbuf+2);
3989 SAVECOPLINE(&PL_compiling);
3990 CopLINE_set(&PL_compiling, 1);
3991 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3992 deleting the eval's FILEGV from the stash before gv_check() runs
3993 (i.e. before run-time proper). To work around the coredump that
3994 ensues, we always turn GvMULTI_on for any globals that were
3995 introduced within evals. See force_ident(). GSAR 96-10-12 */
3997 PL_hints = PL_op->op_targ;
3999 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
4000 SvREFCNT_dec(GvHV(PL_hintgv));
4001 GvHV(PL_hintgv) = saved_hh;
4003 SAVECOMPILEWARNINGS();
4004 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
4005 cophh_free(CopHINTHASH_get(&PL_compiling));
4006 if (Perl_fetch_cop_label(aTHX_ PL_curcop, NULL, NULL)) {
4007 /* The label, if present, is the first entry on the chain. So rather
4008 than writing a blank label in front of it (which involves an
4009 allocation), just use the next entry in the chain. */
4010 PL_compiling.cop_hints_hash
4011 = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next);
4012 /* Check the assumption that this removed the label. */
4013 assert(Perl_fetch_cop_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
4016 PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash);
4017 /* special case: an eval '' executed within the DB package gets lexically
4018 * placed in the first non-DB CV rather than the current CV - this
4019 * allows the debugger to execute code, find lexicals etc, in the
4020 * scope of the code being debugged. Passing &seq gets find_runcv
4021 * to do the dirty work for us */
4022 runcv = find_runcv(&seq);
4024 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
4026 cx->blk_eval.retop = PL_op->op_next;
4028 /* prepare to compile string */
4030 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
4031 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
4033 char *const safestr = savepvn(tmpbuf, len);
4034 SAVEDELETE(PL_defstash, safestr, len);
4035 saved_delete = TRUE;
4040 if (doeval(gimme, NULL, runcv, seq)) {
4041 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4042 ? (PERLDB_LINE || PERLDB_SAVESRC)
4043 : PERLDB_SAVESRC_NOSUBS) {
4044 /* Retain the filegv we created. */
4045 } else if (!saved_delete) {
4046 char *const safestr = savepvn(tmpbuf, len);
4047 SAVEDELETE(PL_defstash, safestr, len);
4049 return DOCATCH(PL_eval_start);
4051 /* We have already left the scope set up earlier thanks to the LEAVE
4053 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4054 ? (PERLDB_LINE || PERLDB_SAVESRC)
4055 : PERLDB_SAVESRC_INVALID) {
4056 /* Retain the filegv we created. */
4057 } else if (!saved_delete) {
4058 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
4060 return PL_op->op_next;
4071 register PERL_CONTEXT *cx;
4073 const U8 save_flags = PL_op -> op_flags;
4080 namesv = cx->blk_eval.old_namesv;
4081 retop = cx->blk_eval.retop;
4084 if (gimme == G_VOID)
4086 else if (gimme == G_SCALAR) {
4089 if (SvFLAGS(TOPs) & SVs_TEMP)
4092 *MARK = sv_mortalcopy(TOPs);
4096 *MARK = &PL_sv_undef;
4101 /* in case LEAVE wipes old return values */
4102 for (mark = newsp + 1; mark <= SP; mark++) {
4103 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
4104 *mark = sv_mortalcopy(*mark);
4105 TAINT_NOT; /* Each item is independent */
4109 PL_curpm = newpm; /* Don't pop $1 et al till now */
4112 assert(CvDEPTH(PL_compcv) == 1);
4114 CvDEPTH(PL_compcv) = 0;
4116 if (optype == OP_REQUIRE &&
4117 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4119 /* Unassume the success we assumed earlier. */
4120 (void)hv_delete(GvHVn(PL_incgv),
4121 SvPVX_const(namesv), SvCUR(namesv),
4123 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4125 /* die_unwind() did LEAVE, or we won't be here */
4128 LEAVE_with_name("eval");
4129 if (!(save_flags & OPf_SPECIAL)) {
4137 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4138 close to the related Perl_create_eval_scope. */
4140 Perl_delete_eval_scope(pTHX)
4145 register PERL_CONTEXT *cx;
4151 LEAVE_with_name("eval_scope");
4152 PERL_UNUSED_VAR(newsp);
4153 PERL_UNUSED_VAR(gimme);
4154 PERL_UNUSED_VAR(optype);
4157 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4158 also needed by Perl_fold_constants. */
4160 Perl_create_eval_scope(pTHX_ U32 flags)
4163 const I32 gimme = GIMME_V;
4165 ENTER_with_name("eval_scope");
4168 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4171 PL_in_eval = EVAL_INEVAL;
4172 if (flags & G_KEEPERR)
4173 PL_in_eval |= EVAL_KEEPERR;
4176 if (flags & G_FAKINGEVAL) {
4177 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4185 PERL_CONTEXT * const cx = create_eval_scope(0);
4186 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4187 return DOCATCH(PL_op->op_next);
4196 register PERL_CONTEXT *cx;
4202 PERL_UNUSED_VAR(optype);
4205 if (gimme == G_VOID)
4207 else if (gimme == G_SCALAR) {
4211 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4214 *MARK = sv_mortalcopy(TOPs);
4218 *MARK = &PL_sv_undef;
4223 /* in case LEAVE wipes old return values */
4225 for (mark = newsp + 1; mark <= SP; mark++) {
4226 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4227 *mark = sv_mortalcopy(*mark);
4228 TAINT_NOT; /* Each item is independent */
4232 PL_curpm = newpm; /* Don't pop $1 et al till now */
4234 LEAVE_with_name("eval_scope");
4242 register PERL_CONTEXT *cx;
4243 const I32 gimme = GIMME_V;
4245 ENTER_with_name("given");
4248 sv_setsv_mg(PAD_SV(PL_op->op_targ), POPs);
4250 PUSHBLOCK(cx, CXt_GIVEN, SP);
4259 register PERL_CONTEXT *cx;
4263 PERL_UNUSED_CONTEXT;
4266 assert(CxTYPE(cx) == CXt_GIVEN);
4269 if (gimme == G_VOID)
4271 else if (gimme == G_SCALAR) {
4275 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4278 *MARK = sv_mortalcopy(TOPs);
4282 *MARK = &PL_sv_undef;
4287 /* in case LEAVE wipes old return values */
4289 for (mark = newsp + 1; mark <= SP; mark++) {
4290 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4291 *mark = sv_mortalcopy(*mark);
4292 TAINT_NOT; /* Each item is independent */
4296 PL_curpm = newpm; /* Don't pop $1 et al till now */
4298 LEAVE_with_name("given");
4302 /* Helper routines used by pp_smartmatch */
4304 S_make_matcher(pTHX_ REGEXP *re)
4307 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4309 PERL_ARGS_ASSERT_MAKE_MATCHER;
4311 PM_SETRE(matcher, ReREFCNT_inc(re));
4313 SAVEFREEOP((OP *) matcher);
4314 ENTER_with_name("matcher"); SAVETMPS;
4320 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4325 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4327 PL_op = (OP *) matcher;
4330 (void) Perl_pp_match(aTHX);
4332 return (SvTRUEx(POPs));
4336 S_destroy_matcher(pTHX_ PMOP *matcher)
4340 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4341 PERL_UNUSED_ARG(matcher);
4344 LEAVE_with_name("matcher");
4347 /* Do a smart match */
4350 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4351 return do_smartmatch(NULL, NULL);
4354 /* This version of do_smartmatch() implements the
4355 * table of smart matches that is found in perlsyn.
4358 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
4363 bool object_on_left = FALSE;
4364 SV *e = TOPs; /* e is for 'expression' */
4365 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4367 /* Take care only to invoke mg_get() once for each argument.
4368 * Currently we do this by copying the SV if it's magical. */
4371 d = sv_mortalcopy(d);
4378 e = sv_mortalcopy(e);
4380 /* First of all, handle overload magic of the rightmost argument */
4383 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4384 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4386 tmpsv = amagic_call(d, e, smart_amg, 0);
4393 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4396 SP -= 2; /* Pop the values */
4401 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4408 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4409 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4410 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4412 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4413 object_on_left = TRUE;
4416 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4418 if (object_on_left) {
4419 goto sm_any_sub; /* Treat objects like scalars */
4421 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4422 /* Test sub truth for each key */
4424 bool andedresults = TRUE;
4425 HV *hv = (HV*) SvRV(d);
4426 I32 numkeys = hv_iterinit(hv);
4427 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4430 while ( (he = hv_iternext(hv)) ) {
4431 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4432 ENTER_with_name("smartmatch_hash_key_test");
4435 PUSHs(hv_iterkeysv(he));
4437 c = call_sv(e, G_SCALAR);
4440 andedresults = FALSE;
4442 andedresults = SvTRUEx(POPs) && andedresults;
4444 LEAVE_with_name("smartmatch_hash_key_test");
4451 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4452 /* Test sub truth for each element */
4454 bool andedresults = TRUE;
4455 AV *av = (AV*) SvRV(d);
4456 const I32 len = av_len(av);
4457 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4460 for (i = 0; i <= len; ++i) {
4461 SV * const * const svp = av_fetch(av, i, FALSE);
4462 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4463 ENTER_with_name("smartmatch_array_elem_test");
4469 c = call_sv(e, G_SCALAR);
4472 andedresults = FALSE;
4474 andedresults = SvTRUEx(POPs) && andedresults;
4476 LEAVE_with_name("smartmatch_array_elem_test");
4485 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4486 ENTER_with_name("smartmatch_coderef");
4491 c = call_sv(e, G_SCALAR);
4495 else if (SvTEMP(TOPs))
4496 SvREFCNT_inc_void(TOPs);
4498 LEAVE_with_name("smartmatch_coderef");
4503 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4504 if (object_on_left) {