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;
548 U8 *source; /* source of bytes to append */
549 STRLEN to_copy; /* how may bytes to append */
551 mg = doparseform(tmpForm);
553 fpc = (U32*)mg->mg_ptr;
554 /* the actual string the format was compiled from.
555 * with overload etc, this may not match tmpForm */
559 SvPV_force(PL_formtarget, len);
560 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
561 SvTAINTED_on(PL_formtarget);
562 if (DO_UTF8(PL_formtarget))
564 fudge = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
565 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
567 f = SvPV_const(formsv, len);
571 const char *name = "???";
574 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
575 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
576 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
577 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
578 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
580 case FF_CHECKNL: name = "CHECKNL"; break;
581 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
582 case FF_SPACE: name = "SPACE"; break;
583 case FF_HALFSPACE: name = "HALFSPACE"; break;
584 case FF_ITEM: name = "ITEM"; break;
585 case FF_CHOP: name = "CHOP"; break;
586 case FF_LINEGLOB: name = "LINEGLOB"; break;
587 case FF_NEWLINE: name = "NEWLINE"; break;
588 case FF_MORE: name = "MORE"; break;
589 case FF_LINEMARK: name = "LINEMARK"; break;
590 case FF_END: name = "END"; break;
591 case FF_0DECIMAL: name = "0DECIMAL"; break;
592 case FF_LINESNGL: name = "LINESNGL"; break;
595 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
597 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
608 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
609 if (targ_is_utf8 && !item_is_utf8) {
611 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
614 /* this is an unrolled sv_catpvn_utf8_upgrade(),
615 * but with the addition of s/~/ /g */
617 nsv = newSVpvn_flags(f, arg, SVs_TEMP);
619 sv_setpvn(nsv, f, arg);
621 for (s = SvPVX(nsv); s <= SvEND(nsv); s++)
624 sv_utf8_upgrade(nsv);
625 sv_catsv(PL_formtarget, nsv);
627 t = SvEND(PL_formtarget);
631 if (!targ_is_utf8 && item_is_utf8) {
632 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
634 sv_utf8_upgrade_flags_grow(PL_formtarget, 0, fudge + 1);
635 t = SvEND(PL_formtarget);
639 *t++ = (*f == '~') ? ' ' : *f;
657 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
660 SvTAINTED_on(PL_formtarget);
666 const char *s = item = SvPV_const(sv, len);
669 itemsize = sv_len_utf8(sv);
670 if (itemsize != (I32)len) {
672 if (itemsize > fieldsize) {
673 itemsize = fieldsize;
674 itembytes = itemsize;
675 sv_pos_u2b(sv, &itembytes, 0);
679 send = chophere = s + itembytes;
689 sv_pos_b2u(sv, &itemsize);
693 item_is_utf8 = FALSE;
694 if (itemsize > fieldsize)
695 itemsize = fieldsize;
696 send = chophere = s + itemsize;
710 const char *s = item = SvPV_const(sv, len);
713 itemsize = sv_len_utf8(sv);
714 if (itemsize != (I32)len) {
716 if (itemsize <= fieldsize) {
717 const char *send = chophere = s + itemsize;
730 itemsize = fieldsize;
731 itembytes = itemsize;
732 sv_pos_u2b(sv, &itembytes, 0);
733 send = chophere = s + itembytes;
734 while (s < send || (s == send && isSPACE(*s))) {
744 if (strchr(PL_chopset, *s))
749 itemsize = chophere - item;
750 sv_pos_b2u(sv, &itemsize);
756 item_is_utf8 = FALSE;
757 if (itemsize <= fieldsize) {
758 const char *const send = chophere = s + itemsize;
771 itemsize = fieldsize;
772 send = chophere = s + itemsize;
773 while (s < send || (s == send && isSPACE(*s))) {
783 if (strchr(PL_chopset, *s))
788 itemsize = chophere - item;
794 arg = fieldsize - itemsize;
803 arg = fieldsize - itemsize;
814 const char *s = item;
818 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
820 sv_utf8_upgrade_flags_grow(PL_formtarget, 0,
822 t = SvEND(PL_formtarget);
826 if (UTF8_IS_CONTINUED(*s)) {
827 STRLEN skip = UTF8SKIP(s);
844 if ( !((*t++ = *s++) & ~31) )
850 if (targ_is_utf8 && !item_is_utf8) {
851 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
853 sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
854 for (; t < SvEND(PL_formtarget); t++) {
867 const int ch = *t++ = *s++;
870 if ( !((*t++ = *s++) & ~31) )
879 const char *s = chophere;
893 const bool oneline = fpc[-1] == FF_LINESNGL;
894 const char *s = item = SvPV_const(sv, len);
895 const char *const send = s + len;
897 item_is_utf8 = DO_UTF8(sv);
907 to_copy = s - SvPVX_const(sv) - 1;
923 SvCUR_set(PL_formtarget,
924 t - SvPVX_const(PL_formtarget));
926 if (targ_is_utf8 && !item_is_utf8) {
927 source = tmp = bytes_to_utf8(source, &to_copy);
929 if (item_is_utf8 && !targ_is_utf8) {
930 /* Upgrade targ to UTF8, and then we reduce it to
931 a problem we have a simple solution for.
932 Don't need get magic. */
933 sv_utf8_upgrade_nomg(PL_formtarget);
936 /* Easy. They agree. */
937 assert (item_is_utf8 == targ_is_utf8);
939 SvGROW(PL_formtarget,
940 SvCUR(PL_formtarget) + to_copy + fudge + 1);
941 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
943 Copy(source, t, to_copy, char);
945 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
953 #if defined(USE_LONG_DOUBLE)
956 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
960 "%#0*.*f" : "%0*.*f");
965 #if defined(USE_LONG_DOUBLE)
967 ((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
970 ((arg & 256) ? "%#*.*f" : "%*.*f");
973 /* If the field is marked with ^ and the value is undefined,
975 if ((arg & 512) && !SvOK(sv)) {
983 /* overflow evidence */
984 if (num_overflow(value, fieldsize, arg)) {
990 /* Formats aren't yet marked for locales, so assume "yes". */
992 STORE_NUMERIC_STANDARD_SET_LOCAL();
993 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
994 RESTORE_NUMERIC_STANDARD();
1001 while (t-- > linemark && *t == ' ') ;
1009 if (arg) { /* repeat until fields exhausted? */
1022 const char *s = chophere;
1023 const char *send = item + len;
1025 while (isSPACE(*s) && (s < send))
1030 arg = fieldsize - itemsize;
1037 if (strnEQ(s1," ",3)) {
1038 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1050 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1052 SvUTF8_on(PL_formtarget);
1053 FmLINES(PL_formtarget) += lines;
1055 if (fpc[-1] == FF_BLANK)
1056 RETURNOP(cLISTOP->op_first);
1068 if (PL_stack_base + *PL_markstack_ptr == SP) {
1070 if (GIMME_V == G_SCALAR)
1072 RETURNOP(PL_op->op_next->op_next);
1074 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
1075 Perl_pp_pushmark(aTHX); /* push dst */
1076 Perl_pp_pushmark(aTHX); /* push src */
1077 ENTER_with_name("grep"); /* enter outer scope */
1080 if (PL_op->op_private & OPpGREP_LEX)
1081 SAVESPTR(PAD_SVl(PL_op->op_targ));
1084 ENTER_with_name("grep_item"); /* enter inner scope */
1087 src = PL_stack_base[*PL_markstack_ptr];
1089 if (PL_op->op_private & OPpGREP_LEX)
1090 PAD_SVl(PL_op->op_targ) = src;
1095 if (PL_op->op_type == OP_MAPSTART)
1096 Perl_pp_pushmark(aTHX); /* push top */
1097 return ((LOGOP*)PL_op->op_next)->op_other;
1103 const I32 gimme = GIMME_V;
1104 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1110 /* first, move source pointer to the next item in the source list */
1111 ++PL_markstack_ptr[-1];
1113 /* if there are new items, push them into the destination list */
1114 if (items && gimme != G_VOID) {
1115 /* might need to make room back there first */
1116 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1117 /* XXX this implementation is very pessimal because the stack
1118 * is repeatedly extended for every set of items. Is possible
1119 * to do this without any stack extension or copying at all
1120 * by maintaining a separate list over which the map iterates
1121 * (like foreach does). --gsar */
1123 /* everything in the stack after the destination list moves
1124 * towards the end the stack by the amount of room needed */
1125 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1127 /* items to shift up (accounting for the moved source pointer) */
1128 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1130 /* This optimization is by Ben Tilly and it does
1131 * things differently from what Sarathy (gsar)
1132 * is describing. The downside of this optimization is
1133 * that leaves "holes" (uninitialized and hopefully unused areas)
1134 * to the Perl stack, but on the other hand this
1135 * shouldn't be a problem. If Sarathy's idea gets
1136 * implemented, this optimization should become
1137 * irrelevant. --jhi */
1139 shift = count; /* Avoid shifting too often --Ben Tilly */
1143 dst = (SP += shift);
1144 PL_markstack_ptr[-1] += shift;
1145 *PL_markstack_ptr += shift;
1149 /* copy the new items down to the destination list */
1150 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1151 if (gimme == G_ARRAY) {
1152 /* add returned items to the collection (making mortal copies
1153 * if necessary), then clear the current temps stack frame
1154 * *except* for those items. We do this splicing the items
1155 * into the start of the tmps frame (so some items may be on
1156 * the tmps stack twice), then moving PL_tmps_floor above
1157 * them, then freeing the frame. That way, the only tmps that
1158 * accumulate over iterations are the return values for map.
1159 * We have to do to this way so that everything gets correctly
1160 * freed if we die during the map.
1164 /* make space for the slice */
1165 EXTEND_MORTAL(items);
1166 tmpsbase = PL_tmps_floor + 1;
1167 Move(PL_tmps_stack + tmpsbase,
1168 PL_tmps_stack + tmpsbase + items,
1169 PL_tmps_ix - PL_tmps_floor,
1171 PL_tmps_ix += items;
1176 sv = sv_mortalcopy(sv);
1178 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1180 /* clear the stack frame except for the items */
1181 PL_tmps_floor += items;
1183 /* FREETMPS may have cleared the TEMP flag on some of the items */
1186 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1189 /* scalar context: we don't care about which values map returns
1190 * (we use undef here). And so we certainly don't want to do mortal
1191 * copies of meaningless values. */
1192 while (items-- > 0) {
1194 *dst-- = &PL_sv_undef;
1202 LEAVE_with_name("grep_item"); /* exit inner scope */
1205 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1207 (void)POPMARK; /* pop top */
1208 LEAVE_with_name("grep"); /* exit outer scope */
1209 (void)POPMARK; /* pop src */
1210 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1211 (void)POPMARK; /* pop dst */
1212 SP = PL_stack_base + POPMARK; /* pop original mark */
1213 if (gimme == G_SCALAR) {
1214 if (PL_op->op_private & OPpGREP_LEX) {
1215 SV* sv = sv_newmortal();
1216 sv_setiv(sv, items);
1224 else if (gimme == G_ARRAY)
1231 ENTER_with_name("grep_item"); /* enter inner scope */
1234 /* set $_ to the new source item */
1235 src = PL_stack_base[PL_markstack_ptr[-1]];
1237 if (PL_op->op_private & OPpGREP_LEX)
1238 PAD_SVl(PL_op->op_targ) = src;
1242 RETURNOP(cLOGOP->op_other);
1251 if (GIMME == G_ARRAY)
1253 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1254 return cLOGOP->op_other;
1264 if (GIMME == G_ARRAY) {
1265 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1269 SV * const targ = PAD_SV(PL_op->op_targ);
1272 if (PL_op->op_private & OPpFLIP_LINENUM) {
1273 if (GvIO(PL_last_in_gv)) {
1274 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1277 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1279 flip = SvIV(sv) == SvIV(GvSV(gv));
1285 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1286 if (PL_op->op_flags & OPf_SPECIAL) {
1294 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1297 sv_setpvs(TARG, "");
1303 /* This code tries to decide if "$left .. $right" should use the
1304 magical string increment, or if the range is numeric (we make
1305 an exception for .."0" [#18165]). AMS 20021031. */
1307 #define RANGE_IS_NUMERIC(left,right) ( \
1308 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1309 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1310 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1311 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1312 && (!SvOK(right) || looks_like_number(right))))
1318 if (GIMME == G_ARRAY) {
1324 if (RANGE_IS_NUMERIC(left,right)) {
1327 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1328 (SvOK(right) && SvNV(right) > IV_MAX))
1329 DIE(aTHX_ "Range iterator outside integer range");
1340 SV * const sv = sv_2mortal(newSViv(i++));
1345 SV * const final = sv_mortalcopy(right);
1347 const char * const tmps = SvPV_const(final, len);
1349 SV *sv = sv_mortalcopy(left);
1350 SvPV_force_nolen(sv);
1351 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1353 if (strEQ(SvPVX_const(sv),tmps))
1355 sv = sv_2mortal(newSVsv(sv));
1362 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1366 if (PL_op->op_private & OPpFLIP_LINENUM) {
1367 if (GvIO(PL_last_in_gv)) {
1368 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1371 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1372 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1380 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1381 sv_catpvs(targ, "E0");
1391 static const char * const context_name[] = {
1393 NULL, /* CXt_WHEN never actually needs "block" */
1394 NULL, /* CXt_BLOCK never actually needs "block" */
1395 NULL, /* CXt_GIVEN never actually needs "block" */
1396 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1397 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1398 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1399 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1407 S_dopoptolabel(pTHX_ const char *label)
1412 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1414 for (i = cxstack_ix; i >= 0; i--) {
1415 register const PERL_CONTEXT * const cx = &cxstack[i];
1416 switch (CxTYPE(cx)) {
1422 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1423 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1424 if (CxTYPE(cx) == CXt_NULL)
1427 case CXt_LOOP_LAZYIV:
1428 case CXt_LOOP_LAZYSV:
1430 case CXt_LOOP_PLAIN:
1432 const char *cx_label = CxLABEL(cx);
1433 if (!cx_label || strNE(label, cx_label) ) {
1434 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1435 (long)i, cx_label));
1438 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1449 Perl_dowantarray(pTHX)
1452 const I32 gimme = block_gimme();
1453 return (gimme == G_VOID) ? G_SCALAR : gimme;
1457 Perl_block_gimme(pTHX)
1460 const I32 cxix = dopoptosub(cxstack_ix);
1464 switch (cxstack[cxix].blk_gimme) {
1472 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1479 Perl_is_lvalue_sub(pTHX)
1482 const I32 cxix = dopoptosub(cxstack_ix);
1483 assert(cxix >= 0); /* We should only be called from inside subs */
1485 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1486 return CxLVAL(cxstack + cxix);
1492 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1497 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1499 for (i = startingblock; i >= 0; i--) {
1500 register const PERL_CONTEXT * const cx = &cxstk[i];
1501 switch (CxTYPE(cx)) {
1507 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1515 S_dopoptoeval(pTHX_ I32 startingblock)
1519 for (i = startingblock; i >= 0; i--) {
1520 register const PERL_CONTEXT *cx = &cxstack[i];
1521 switch (CxTYPE(cx)) {
1525 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1533 S_dopoptoloop(pTHX_ I32 startingblock)
1537 for (i = startingblock; i >= 0; i--) {
1538 register const PERL_CONTEXT * const cx = &cxstack[i];
1539 switch (CxTYPE(cx)) {
1545 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1546 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1547 if ((CxTYPE(cx)) == CXt_NULL)
1550 case CXt_LOOP_LAZYIV:
1551 case CXt_LOOP_LAZYSV:
1553 case CXt_LOOP_PLAIN:
1554 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1562 S_dopoptogiven(pTHX_ I32 startingblock)
1566 for (i = startingblock; i >= 0; i--) {
1567 register const PERL_CONTEXT *cx = &cxstack[i];
1568 switch (CxTYPE(cx)) {
1572 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
1574 case CXt_LOOP_PLAIN:
1575 assert(!CxFOREACHDEF(cx));
1577 case CXt_LOOP_LAZYIV:
1578 case CXt_LOOP_LAZYSV:
1580 if (CxFOREACHDEF(cx)) {
1581 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
1590 S_dopoptowhen(pTHX_ I32 startingblock)
1594 for (i = startingblock; i >= 0; i--) {
1595 register const PERL_CONTEXT *cx = &cxstack[i];
1596 switch (CxTYPE(cx)) {
1600 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1608 Perl_dounwind(pTHX_ I32 cxix)
1613 while (cxstack_ix > cxix) {
1615 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1616 DEBUG_CX("UNWIND"); \
1617 /* Note: we don't need to restore the base context info till the end. */
1618 switch (CxTYPE(cx)) {
1621 continue; /* not break */
1629 case CXt_LOOP_LAZYIV:
1630 case CXt_LOOP_LAZYSV:
1632 case CXt_LOOP_PLAIN:
1643 PERL_UNUSED_VAR(optype);
1647 Perl_qerror(pTHX_ SV *err)
1651 PERL_ARGS_ASSERT_QERROR;
1654 if (PL_in_eval & EVAL_KEEPERR) {
1655 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1656 SvPV_nolen_const(err));
1659 sv_catsv(ERRSV, err);
1662 sv_catsv(PL_errors, err);
1664 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
1666 ++PL_parser->error_count;
1670 Perl_die_unwind(pTHX_ SV *msv)
1673 SV *exceptsv = sv_mortalcopy(msv);
1674 U8 in_eval = PL_in_eval;
1675 PERL_ARGS_ASSERT_DIE_UNWIND;
1682 * Historically, perl used to set ERRSV ($@) early in the die
1683 * process and rely on it not getting clobbered during unwinding.
1684 * That sucked, because it was liable to get clobbered, so the
1685 * setting of ERRSV used to emit the exception from eval{} has
1686 * been moved to much later, after unwinding (see just before
1687 * JMPENV_JUMP below). However, some modules were relying on the
1688 * early setting, by examining $@ during unwinding to use it as
1689 * a flag indicating whether the current unwinding was caused by
1690 * an exception. It was never a reliable flag for that purpose,
1691 * being totally open to false positives even without actual
1692 * clobberage, but was useful enough for production code to
1693 * semantically rely on it.
1695 * We'd like to have a proper introspective interface that
1696 * explicitly describes the reason for whatever unwinding
1697 * operations are currently in progress, so that those modules
1698 * work reliably and $@ isn't further overloaded. But we don't
1699 * have one yet. In its absence, as a stopgap measure, ERRSV is
1700 * now *additionally* set here, before unwinding, to serve as the
1701 * (unreliable) flag that it used to.
1703 * This behaviour is temporary, and should be removed when a
1704 * proper way to detect exceptional unwinding has been developed.
1705 * As of 2010-12, the authors of modules relying on the hack
1706 * are aware of the issue, because the modules failed on
1707 * perls 5.13.{1..7} which had late setting of $@ without this
1708 * early-setting hack.
1710 if (!(in_eval & EVAL_KEEPERR)) {
1711 SvTEMP_off(exceptsv);
1712 sv_setsv(ERRSV, exceptsv);
1715 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1716 && PL_curstackinfo->si_prev)
1725 register PERL_CONTEXT *cx;
1728 JMPENV *restartjmpenv;
1731 if (cxix < cxstack_ix)
1734 POPBLOCK(cx,PL_curpm);
1735 if (CxTYPE(cx) != CXt_EVAL) {
1737 const char* message = SvPVx_const(exceptsv, msglen);
1738 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
1739 PerlIO_write(Perl_error_log, message, msglen);
1743 namesv = cx->blk_eval.old_namesv;
1744 oldcop = cx->blk_oldcop;
1745 restartjmpenv = cx->blk_eval.cur_top_env;
1746 restartop = cx->blk_eval.retop;
1748 if (gimme == G_SCALAR)
1749 *++newsp = &PL_sv_undef;
1750 PL_stack_sp = newsp;
1754 /* LEAVE could clobber PL_curcop (see save_re_context())
1755 * XXX it might be better to find a way to avoid messing with
1756 * PL_curcop in save_re_context() instead, but this is a more
1757 * minimal fix --GSAR */
1760 if (optype == OP_REQUIRE) {
1761 const char* const msg = SvPVx_nolen_const(exceptsv);
1762 (void)hv_store(GvHVn(PL_incgv),
1763 SvPVX_const(namesv), SvCUR(namesv),
1765 /* note that unlike pp_entereval, pp_require isn't
1766 * supposed to trap errors. So now that we've popped the
1767 * EVAL that pp_require pushed, and processed the error
1768 * message, rethrow the error */
1769 Perl_croak(aTHX_ "%sCompilation failed in require",
1770 *msg ? msg : "Unknown error\n");
1772 if (in_eval & EVAL_KEEPERR) {
1773 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1774 SvPV_nolen_const(exceptsv));
1777 sv_setsv(ERRSV, exceptsv);
1779 PL_restartjmpenv = restartjmpenv;
1780 PL_restartop = restartop;
1786 write_to_stderr(exceptsv);
1793 dVAR; dSP; dPOPTOPssrl;
1794 if (SvTRUE(left) != SvTRUE(right))
1801 =for apidoc caller_cx
1803 The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1804 returned C<PERL_CONTEXT> structure can be interrogated to find all the
1805 information returned to Perl by C<caller>. Note that XSUBs don't get a
1806 stack frame, so C<caller_cx(0, NULL)> will return information for the
1807 immediately-surrounding Perl code.
1809 This function skips over the automatic calls to C<&DB::sub> made on the
1810 behalf of the debugger. If the stack frame requested was a sub called by
1811 C<DB::sub>, the return value will be the frame for the call to
1812 C<DB::sub>, since that has the correct line number/etc. for the call
1813 site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1814 frame for the sub call itself.
1819 const PERL_CONTEXT *
1820 Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
1822 register I32 cxix = dopoptosub(cxstack_ix);
1823 register const PERL_CONTEXT *cx;
1824 register const PERL_CONTEXT *ccstack = cxstack;
1825 const PERL_SI *top_si = PL_curstackinfo;
1828 /* we may be in a higher stacklevel, so dig down deeper */
1829 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1830 top_si = top_si->si_prev;
1831 ccstack = top_si->si_cxstack;
1832 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1836 /* caller() should not report the automatic calls to &DB::sub */
1837 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1838 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1842 cxix = dopoptosub_at(ccstack, cxix - 1);
1845 cx = &ccstack[cxix];
1846 if (dbcxp) *dbcxp = cx;
1848 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1849 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1850 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1851 field below is defined for any cx. */
1852 /* caller() should not report the automatic calls to &DB::sub */
1853 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1854 cx = &ccstack[dbcxix];
1864 register const PERL_CONTEXT *cx;
1865 const PERL_CONTEXT *dbcx;
1867 const char *stashname;
1873 cx = caller_cx(count, &dbcx);
1875 if (GIMME != G_ARRAY) {
1882 stashname = CopSTASHPV(cx->blk_oldcop);
1883 if (GIMME != G_ARRAY) {
1886 PUSHs(&PL_sv_undef);
1889 sv_setpv(TARG, stashname);
1898 PUSHs(&PL_sv_undef);
1900 mPUSHs(newSVpv(stashname, 0));
1901 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1902 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1905 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1906 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
1907 /* So is ccstack[dbcxix]. */
1909 SV * const sv = newSV(0);
1910 gv_efullname3(sv, cvgv, NULL);
1912 PUSHs(boolSV(CxHASARGS(cx)));
1915 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1916 PUSHs(boolSV(CxHASARGS(cx)));
1920 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1923 gimme = (I32)cx->blk_gimme;
1924 if (gimme == G_VOID)
1925 PUSHs(&PL_sv_undef);
1927 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1928 if (CxTYPE(cx) == CXt_EVAL) {
1930 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1931 PUSHs(cx->blk_eval.cur_text);
1935 else if (cx->blk_eval.old_namesv) {
1936 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1939 /* eval BLOCK (try blocks have old_namesv == 0) */
1941 PUSHs(&PL_sv_undef);
1942 PUSHs(&PL_sv_undef);
1946 PUSHs(&PL_sv_undef);
1947 PUSHs(&PL_sv_undef);
1949 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1950 && CopSTASH_eq(PL_curcop, PL_debstash))
1952 AV * const ary = cx->blk_sub.argarray;
1953 const int off = AvARRAY(ary) - AvALLOC(ary);
1956 Perl_init_dbargs(aTHX);
1958 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1959 av_extend(PL_dbargs, AvFILLp(ary) + off);
1960 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1961 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1963 /* XXX only hints propagated via op_private are currently
1964 * visible (others are not easily accessible, since they
1965 * use the global PL_hints) */
1966 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1969 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1971 if (old_warnings == pWARN_NONE ||
1972 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1973 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1974 else if (old_warnings == pWARN_ALL ||
1975 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1976 /* Get the bit mask for $warnings::Bits{all}, because
1977 * it could have been extended by warnings::register */
1979 HV * const bits = get_hv("warnings::Bits", 0);
1980 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
1981 mask = newSVsv(*bits_all);
1984 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1988 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
1992 PUSHs(cx->blk_oldcop->cop_hints_hash ?
1993 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
2002 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
2003 sv_reset(tmps, CopSTASH(PL_curcop));
2008 /* like pp_nextstate, but used instead when the debugger is active */
2013 PL_curcop = (COP*)PL_op;
2014 TAINT_NOT; /* Each statement is presumed innocent */
2015 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
2020 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
2021 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
2024 register PERL_CONTEXT *cx;
2025 const I32 gimme = G_ARRAY;
2027 GV * const gv = PL_DBgv;
2028 register CV * const cv = GvCV(gv);
2031 DIE(aTHX_ "No DB::DB routine defined");
2033 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2034 /* don't do recursive DB::DB call */
2049 (void)(*CvXSUB(cv))(aTHX_ cv);
2056 PUSHBLOCK(cx, CXt_SUB, SP);
2058 cx->blk_sub.retop = PL_op->op_next;
2061 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
2062 RETURNOP(CvSTART(cv));
2072 register PERL_CONTEXT *cx;
2073 const I32 gimme = GIMME_V;
2074 void *itervar; /* location of the iteration variable */
2075 U8 cxtype = CXt_LOOP_FOR;
2077 ENTER_with_name("loop1");
2080 if (PL_op->op_targ) { /* "my" variable */
2081 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
2082 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2083 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2084 SVs_PADSTALE, SVs_PADSTALE);
2086 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
2088 itervar = PL_comppad;
2090 itervar = &PAD_SVl(PL_op->op_targ);
2093 else { /* symbol table variable */
2094 GV * const gv = MUTABLE_GV(POPs);
2095 SV** svp = &GvSV(gv);
2096 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
2098 itervar = (void *)gv;
2101 if (PL_op->op_private & OPpITER_DEF)
2102 cxtype |= CXp_FOR_DEF;
2104 ENTER_with_name("loop2");
2106 PUSHBLOCK(cx, cxtype, SP);
2107 PUSHLOOP_FOR(cx, itervar, MARK);
2108 if (PL_op->op_flags & OPf_STACKED) {
2109 SV *maybe_ary = POPs;
2110 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2112 SV * const right = maybe_ary;
2115 if (RANGE_IS_NUMERIC(sv,right)) {
2116 cx->cx_type &= ~CXTYPEMASK;
2117 cx->cx_type |= CXt_LOOP_LAZYIV;
2118 /* Make sure that no-one re-orders cop.h and breaks our
2120 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
2121 #ifdef NV_PRESERVES_UV
2122 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
2123 (SvNV(sv) > (NV)IV_MAX)))
2125 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
2126 (SvNV(right) < (NV)IV_MIN))))
2128 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
2131 ((SvUV(sv) > (UV)IV_MAX) ||
2132 (SvNV(sv) > (NV)UV_MAX)))))
2134 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
2136 ((SvNV(right) > 0) &&
2137 ((SvUV(right) > (UV)IV_MAX) ||
2138 (SvNV(right) > (NV)UV_MAX))))))
2140 DIE(aTHX_ "Range iterator outside integer range");
2141 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2142 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
2144 /* for correct -Dstv display */
2145 cx->blk_oldsp = sp - PL_stack_base;
2149 cx->cx_type &= ~CXTYPEMASK;
2150 cx->cx_type |= CXt_LOOP_LAZYSV;
2151 /* Make sure that no-one re-orders cop.h and breaks our
2153 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2154 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2155 cx->blk_loop.state_u.lazysv.end = right;
2156 SvREFCNT_inc(right);
2157 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2158 /* This will do the upgrade to SVt_PV, and warn if the value
2159 is uninitialised. */
2160 (void) SvPV_nolen_const(right);
2161 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2162 to replace !SvOK() with a pointer to "". */
2164 SvREFCNT_dec(right);
2165 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2169 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2170 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2171 SvREFCNT_inc(maybe_ary);
2172 cx->blk_loop.state_u.ary.ix =
2173 (PL_op->op_private & OPpITER_REVERSED) ?
2174 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2178 else { /* iterating over items on the stack */
2179 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2180 if (PL_op->op_private & OPpITER_REVERSED) {
2181 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2184 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2194 register PERL_CONTEXT *cx;
2195 const I32 gimme = GIMME_V;
2197 ENTER_with_name("loop1");
2199 ENTER_with_name("loop2");
2201 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2202 PUSHLOOP_PLAIN(cx, SP);
2210 register PERL_CONTEXT *cx;
2217 assert(CxTYPE_is_LOOP(cx));
2219 newsp = PL_stack_base + cx->blk_loop.resetsp;
2222 if (gimme == G_VOID)
2224 else if (gimme == G_SCALAR) {
2226 *++newsp = sv_mortalcopy(*SP);
2228 *++newsp = &PL_sv_undef;
2232 *++newsp = sv_mortalcopy(*++mark);
2233 TAINT_NOT; /* Each item is independent */
2239 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2240 PL_curpm = newpm; /* ... and pop $1 et al */
2242 LEAVE_with_name("loop2");
2243 LEAVE_with_name("loop1");
2251 register PERL_CONTEXT *cx;
2252 bool popsub2 = FALSE;
2253 bool clear_errsv = FALSE;
2263 const I32 cxix = dopoptosub(cxstack_ix);
2266 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2267 * sort block, which is a CXt_NULL
2270 PL_stack_base[1] = *PL_stack_sp;
2271 PL_stack_sp = PL_stack_base + 1;
2275 DIE(aTHX_ "Can't return outside a subroutine");
2277 if (cxix < cxstack_ix)
2280 if (CxMULTICALL(&cxstack[cxix])) {
2281 gimme = cxstack[cxix].blk_gimme;
2282 if (gimme == G_VOID)
2283 PL_stack_sp = PL_stack_base;
2284 else if (gimme == G_SCALAR) {
2285 PL_stack_base[1] = *PL_stack_sp;
2286 PL_stack_sp = PL_stack_base + 1;
2292 switch (CxTYPE(cx)) {
2295 lval = !!CvLVALUE(cx->blk_sub.cv);
2296 retop = cx->blk_sub.retop;
2297 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2300 if (!(PL_in_eval & EVAL_KEEPERR))
2303 namesv = cx->blk_eval.old_namesv;
2304 retop = cx->blk_eval.retop;
2307 if (optype == OP_REQUIRE &&
2308 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2310 /* Unassume the success we assumed earlier. */
2311 (void)hv_delete(GvHVn(PL_incgv),
2312 SvPVX_const(namesv), SvCUR(namesv),
2314 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
2319 retop = cx->blk_sub.retop;
2322 DIE(aTHX_ "panic: return");
2326 if (gimme == G_SCALAR) {
2329 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2331 *++newsp = SvREFCNT_inc(*SP);
2336 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2338 *++newsp = sv_mortalcopy(sv);
2344 (lval || SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
2347 *++newsp = sv_mortalcopy(*SP);
2350 *++newsp = &PL_sv_undef;
2352 else if (gimme == G_ARRAY) {
2353 while (++MARK <= SP) {
2354 *++newsp = popsub2 && (lval || SvTEMP(*MARK))
2355 ? *MARK : sv_mortalcopy(*MARK);
2356 TAINT_NOT; /* Each item is independent */
2359 PL_stack_sp = newsp;
2362 /* Stack values are safe: */
2365 POPSUB(cx,sv); /* release CV and @_ ... */
2369 PL_curpm = newpm; /* ... and pop $1 et al */
2382 register PERL_CONTEXT *cx;
2393 if (PL_op->op_flags & OPf_SPECIAL) {
2394 cxix = dopoptoloop(cxstack_ix);
2396 DIE(aTHX_ "Can't \"last\" outside a loop block");
2399 cxix = dopoptolabel(cPVOP->op_pv);
2401 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
2403 if (cxix < cxstack_ix)
2407 cxstack_ix++; /* temporarily protect top context */
2409 switch (CxTYPE(cx)) {
2410 case CXt_LOOP_LAZYIV:
2411 case CXt_LOOP_LAZYSV:
2413 case CXt_LOOP_PLAIN:
2415 newsp = PL_stack_base + cx->blk_loop.resetsp;
2416 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2420 nextop = cx->blk_sub.retop;
2424 nextop = cx->blk_eval.retop;
2428 nextop = cx->blk_sub.retop;
2431 DIE(aTHX_ "panic: last");
2435 if (gimme == G_SCALAR) {
2437 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2438 ? *SP : sv_mortalcopy(*SP);
2440 *++newsp = &PL_sv_undef;
2442 else if (gimme == G_ARRAY) {
2443 while (++MARK <= SP) {
2444 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2445 ? *MARK : sv_mortalcopy(*MARK);
2446 TAINT_NOT; /* Each item is independent */
2454 /* Stack values are safe: */
2456 case CXt_LOOP_LAZYIV:
2457 case CXt_LOOP_PLAIN:
2458 case CXt_LOOP_LAZYSV:
2460 POPLOOP(cx); /* release loop vars ... */
2464 POPSUB(cx,sv); /* release CV and @_ ... */
2467 PL_curpm = newpm; /* ... and pop $1 et al */
2470 PERL_UNUSED_VAR(optype);
2471 PERL_UNUSED_VAR(gimme);
2479 register PERL_CONTEXT *cx;
2482 if (PL_op->op_flags & OPf_SPECIAL) {
2483 cxix = dopoptoloop(cxstack_ix);
2485 DIE(aTHX_ "Can't \"next\" outside a loop block");
2488 cxix = dopoptolabel(cPVOP->op_pv);
2490 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2492 if (cxix < cxstack_ix)
2495 /* clear off anything above the scope we're re-entering, but
2496 * save the rest until after a possible continue block */
2497 inner = PL_scopestack_ix;
2499 if (PL_scopestack_ix < inner)
2500 leave_scope(PL_scopestack[PL_scopestack_ix]);
2501 PL_curcop = cx->blk_oldcop;
2502 return (cx)->blk_loop.my_op->op_nextop;
2509 register PERL_CONTEXT *cx;
2513 if (PL_op->op_flags & OPf_SPECIAL) {
2514 cxix = dopoptoloop(cxstack_ix);
2516 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2519 cxix = dopoptolabel(cPVOP->op_pv);
2521 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2523 if (cxix < cxstack_ix)
2526 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2527 if (redo_op->op_type == OP_ENTER) {
2528 /* pop one less context to avoid $x being freed in while (my $x..) */
2530 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2531 redo_op = redo_op->op_next;
2535 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2536 LEAVE_SCOPE(oldsave);
2538 PL_curcop = cx->blk_oldcop;
2543 S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
2547 static const char too_deep[] = "Target of goto is too deeply nested";
2549 PERL_ARGS_ASSERT_DOFINDLABEL;
2552 Perl_croak(aTHX_ too_deep);
2553 if (o->op_type == OP_LEAVE ||
2554 o->op_type == OP_SCOPE ||
2555 o->op_type == OP_LEAVELOOP ||
2556 o->op_type == OP_LEAVESUB ||
2557 o->op_type == OP_LEAVETRY)
2559 *ops++ = cUNOPo->op_first;
2561 Perl_croak(aTHX_ too_deep);
2564 if (o->op_flags & OPf_KIDS) {
2566 /* First try all the kids at this level, since that's likeliest. */
2567 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2568 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2569 const char *kid_label = CopLABEL(kCOP);
2570 if (kid_label && strEQ(kid_label, label))
2574 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2575 if (kid == PL_lastgotoprobe)
2577 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2580 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2581 ops[-1]->op_type == OP_DBSTATE)
2586 if ((o = dofindlabel(kid, label, ops, oplimit)))
2599 register PERL_CONTEXT *cx;
2600 #define GOTO_DEPTH 64
2601 OP *enterops[GOTO_DEPTH];
2602 const char *label = NULL;
2603 const bool do_dump = (PL_op->op_type == OP_DUMP);
2604 static const char must_have_label[] = "goto must have label";
2606 if (PL_op->op_flags & OPf_STACKED) {
2607 SV * const sv = POPs;
2609 /* This egregious kludge implements goto &subroutine */
2610 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2612 register PERL_CONTEXT *cx;
2613 CV *cv = MUTABLE_CV(SvRV(sv));
2620 if (!CvROOT(cv) && !CvXSUB(cv)) {
2621 const GV * const gv = CvGV(cv);
2625 /* autoloaded stub? */
2626 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2628 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2629 GvNAMELEN(gv), FALSE);
2630 if (autogv && (cv = GvCV(autogv)))
2632 tmpstr = sv_newmortal();
2633 gv_efullname3(tmpstr, gv, NULL);
2634 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2636 DIE(aTHX_ "Goto undefined subroutine");
2639 /* First do some returnish stuff. */
2640 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2642 cxix = dopoptosub(cxstack_ix);
2644 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2645 if (cxix < cxstack_ix)
2649 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2650 if (CxTYPE(cx) == CXt_EVAL) {
2652 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2654 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2656 else if (CxMULTICALL(cx))
2657 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2658 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2659 /* put @_ back onto stack */
2660 AV* av = cx->blk_sub.argarray;
2662 items = AvFILLp(av) + 1;
2663 EXTEND(SP, items+1); /* @_ could have been extended. */
2664 Copy(AvARRAY(av), SP + 1, items, SV*);
2665 SvREFCNT_dec(GvAV(PL_defgv));
2666 GvAV(PL_defgv) = cx->blk_sub.savearray;
2668 /* abandon @_ if it got reified */
2673 av_extend(av, items-1);
2675 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2678 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2679 AV* const av = GvAV(PL_defgv);
2680 items = AvFILLp(av) + 1;
2681 EXTEND(SP, items+1); /* @_ could have been extended. */
2682 Copy(AvARRAY(av), SP + 1, items, SV*);
2686 if (CxTYPE(cx) == CXt_SUB &&
2687 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2688 SvREFCNT_dec(cx->blk_sub.cv);
2689 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2690 LEAVE_SCOPE(oldsave);
2692 /* Now do some callish stuff. */
2694 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2696 OP* const retop = cx->blk_sub.retop;
2697 SV **newsp __attribute__unused__;
2698 I32 gimme __attribute__unused__;
2701 for (index=0; index<items; index++)
2702 sv_2mortal(SP[-index]);
2705 /* XS subs don't have a CxSUB, so pop it */
2706 POPBLOCK(cx, PL_curpm);
2707 /* Push a mark for the start of arglist */
2710 (void)(*CvXSUB(cv))(aTHX_ cv);
2715 AV* const padlist = CvPADLIST(cv);
2716 if (CxTYPE(cx) == CXt_EVAL) {
2717 PL_in_eval = CxOLD_IN_EVAL(cx);
2718 PL_eval_root = cx->blk_eval.old_eval_root;
2719 cx->cx_type = CXt_SUB;
2721 cx->blk_sub.cv = cv;
2722 cx->blk_sub.olddepth = CvDEPTH(cv);
2725 if (CvDEPTH(cv) < 2)
2726 SvREFCNT_inc_simple_void_NN(cv);
2728 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2729 sub_crush_depth(cv);
2730 pad_push(padlist, CvDEPTH(cv));
2733 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2736 AV *const av = MUTABLE_AV(PAD_SVl(0));
2738 cx->blk_sub.savearray = GvAV(PL_defgv);
2739 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
2740 CX_CURPAD_SAVE(cx->blk_sub);
2741 cx->blk_sub.argarray = av;
2743 if (items >= AvMAX(av) + 1) {
2744 SV **ary = AvALLOC(av);
2745 if (AvARRAY(av) != ary) {
2746 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2749 if (items >= AvMAX(av) + 1) {
2750 AvMAX(av) = items - 1;
2751 Renew(ary,items+1,SV*);
2757 Copy(mark,AvARRAY(av),items,SV*);
2758 AvFILLp(av) = items - 1;
2759 assert(!AvREAL(av));
2761 /* transfer 'ownership' of refcnts to new @_ */
2771 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2772 Perl_get_db_sub(aTHX_ NULL, cv);
2774 CV * const gotocv = get_cvs("DB::goto", 0);
2776 PUSHMARK( PL_stack_sp );
2777 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
2782 RETURNOP(CvSTART(cv));
2786 label = SvPV_nolen_const(sv);
2787 if (!(do_dump || *label))
2788 DIE(aTHX_ must_have_label);
2791 else if (PL_op->op_flags & OPf_SPECIAL) {
2793 DIE(aTHX_ must_have_label);
2796 label = cPVOP->op_pv;
2800 if (label && *label) {
2801 OP *gotoprobe = NULL;
2802 bool leaving_eval = FALSE;
2803 bool in_block = FALSE;
2804 PERL_CONTEXT *last_eval_cx = NULL;
2808 PL_lastgotoprobe = NULL;
2810 for (ix = cxstack_ix; ix >= 0; ix--) {
2812 switch (CxTYPE(cx)) {
2814 leaving_eval = TRUE;
2815 if (!CxTRYBLOCK(cx)) {
2816 gotoprobe = (last_eval_cx ?
2817 last_eval_cx->blk_eval.old_eval_root :
2822 /* else fall through */
2823 case CXt_LOOP_LAZYIV:
2824 case CXt_LOOP_LAZYSV:
2826 case CXt_LOOP_PLAIN:
2829 gotoprobe = cx->blk_oldcop->op_sibling;
2835 gotoprobe = cx->blk_oldcop->op_sibling;
2838 gotoprobe = PL_main_root;
2841 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
2842 gotoprobe = CvROOT(cx->blk_sub.cv);
2848 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2851 DIE(aTHX_ "panic: goto");
2852 gotoprobe = PL_main_root;
2856 retop = dofindlabel(gotoprobe, label,
2857 enterops, enterops + GOTO_DEPTH);
2860 if (gotoprobe->op_sibling &&
2861 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
2862 gotoprobe->op_sibling->op_sibling) {
2863 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
2864 label, enterops, enterops + GOTO_DEPTH);
2869 PL_lastgotoprobe = gotoprobe;
2872 DIE(aTHX_ "Can't find label %s", label);
2874 /* if we're leaving an eval, check before we pop any frames
2875 that we're not going to punt, otherwise the error
2878 if (leaving_eval && *enterops && enterops[1]) {
2880 for (i = 1; enterops[i]; i++)
2881 if (enterops[i]->op_type == OP_ENTERITER)
2882 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2885 if (*enterops && enterops[1]) {
2886 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2888 deprecate("\"goto\" to jump into a construct");
2891 /* pop unwanted frames */
2893 if (ix < cxstack_ix) {
2900 oldsave = PL_scopestack[PL_scopestack_ix];
2901 LEAVE_SCOPE(oldsave);
2904 /* push wanted frames */
2906 if (*enterops && enterops[1]) {
2907 OP * const oldop = PL_op;
2908 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2909 for (; enterops[ix]; ix++) {
2910 PL_op = enterops[ix];
2911 /* Eventually we may want to stack the needed arguments
2912 * for each op. For now, we punt on the hard ones. */
2913 if (PL_op->op_type == OP_ENTERITER)
2914 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2915 PL_op->op_ppaddr(aTHX);
2923 if (!retop) retop = PL_main_start;
2925 PL_restartop = retop;
2926 PL_do_undump = TRUE;
2930 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2931 PL_do_undump = FALSE;
2948 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2950 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
2953 PL_exit_flags |= PERL_EXIT_EXPECTED;
2955 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2956 if (anum || !(PL_minus_c && PL_madskills))
2961 PUSHs(&PL_sv_undef);
2968 S_save_lines(pTHX_ AV *array, SV *sv)
2970 const char *s = SvPVX_const(sv);
2971 const char * const send = SvPVX_const(sv) + SvCUR(sv);
2974 PERL_ARGS_ASSERT_SAVE_LINES;
2976 while (s && s < send) {
2978 SV * const tmpstr = newSV_type(SVt_PVMG);
2980 t = (const char *)memchr(s, '\n', send - s);
2986 sv_setpvn(tmpstr, s, t - s);
2987 av_store(array, line++, tmpstr);
2995 Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
2997 0 is used as continue inside eval,
2999 3 is used for a die caught by an inner eval - continue inner loop
3001 See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
3002 establish a local jmpenv to handle exception traps.
3007 S_docatch(pTHX_ OP *o)
3011 OP * const oldop = PL_op;
3015 assert(CATCH_GET == TRUE);
3022 assert(cxstack_ix >= 0);
3023 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3024 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
3029 /* die caught by an inner eval - continue inner loop */
3030 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3031 PL_restartjmpenv = NULL;
3032 PL_op = PL_restartop;
3048 /* James Bond: Do you expect me to talk?
3049 Auric Goldfinger: No, Mr. Bond. I expect you to die.
3051 This code is an ugly hack, doesn't work with lexicals in subroutines that are
3052 called more than once, and is only used by regcomp.c, for (?{}) blocks.
3054 Currently it is not used outside the core code. Best if it stays that way.
3056 Hence it's now deprecated, and will be removed.
3059 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
3060 /* sv Text to convert to OP tree. */
3061 /* startop op_free() this to undo. */
3062 /* code Short string id of the caller. */
3064 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3065 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
3068 /* Don't use this. It will go away without warning once the regexp engine is
3069 refactored not to use it. */
3071 Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3074 dVAR; dSP; /* Make POPBLOCK work. */
3080 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3081 char *tmpbuf = tbuf;
3084 CV* runcv = NULL; /* initialise to avoid compiler warnings */
3088 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
3090 ENTER_with_name("eval");
3091 lex_start(sv, NULL, LEX_START_SAME_FILTER);
3093 /* switch to eval mode */
3095 if (IN_PERL_COMPILETIME) {
3096 SAVECOPSTASH_FREE(&PL_compiling);
3097 CopSTASH_set(&PL_compiling, PL_curstash);
3099 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3100 SV * const sv = sv_newmortal();
3101 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3102 code, (unsigned long)++PL_evalseq,
3103 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3108 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3109 (unsigned long)++PL_evalseq);
3110 SAVECOPFILE_FREE(&PL_compiling);
3111 CopFILE_set(&PL_compiling, tmpbuf+2);
3112 SAVECOPLINE(&PL_compiling);
3113 CopLINE_set(&PL_compiling, 1);
3114 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3115 deleting the eval's FILEGV from the stash before gv_check() runs
3116 (i.e. before run-time proper). To work around the coredump that
3117 ensues, we always turn GvMULTI_on for any globals that were
3118 introduced within evals. See force_ident(). GSAR 96-10-12 */
3119 safestr = savepvn(tmpbuf, len);
3120 SAVEDELETE(PL_defstash, safestr, len);
3122 #ifdef OP_IN_REGISTER
3128 /* we get here either during compilation, or via pp_regcomp at runtime */
3129 runtime = IN_PERL_RUNTIME;
3132 runcv = find_runcv(NULL);
3134 /* At run time, we have to fetch the hints from PL_curcop. */
3135 PL_hints = PL_curcop->cop_hints;
3136 if (PL_hints & HINT_LOCALIZE_HH) {
3137 /* SAVEHINTS created a new HV in PL_hintgv, which we
3139 SvREFCNT_dec(GvHV(PL_hintgv));
3141 refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0);
3142 hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints);
3144 SAVECOMPILEWARNINGS();
3145 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3146 cophh_free(CopHINTHASH_get(&PL_compiling));
3147 /* XXX Does this need to avoid copying a label? */
3148 PL_compiling.cop_hints_hash
3149 = cophh_copy(PL_curcop->cop_hints_hash);
3153 PL_op->op_type = OP_ENTEREVAL;
3154 PL_op->op_flags = 0; /* Avoid uninit warning. */
3155 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
3157 need_catch = CATCH_GET;
3161 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
3163 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
3164 CATCH_SET(need_catch);
3165 POPBLOCK(cx,PL_curpm);
3168 (*startop)->op_type = OP_NULL;
3169 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
3170 /* XXX DAPM do this properly one year */
3171 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
3172 LEAVE_with_name("eval");
3173 if (IN_PERL_COMPILETIME)
3174 CopHINTS_set(&PL_compiling, PL_hints);
3175 #ifdef OP_IN_REGISTER
3178 PERL_UNUSED_VAR(newsp);
3179 PERL_UNUSED_VAR(optype);
3181 return PL_eval_start;
3186 =for apidoc find_runcv
3188 Locate the CV corresponding to the currently executing sub or eval.
3189 If db_seqp is non_null, skip CVs that are in the DB package and populate
3190 *db_seqp with the cop sequence number at the point that the DB:: code was
3191 entered. (allows debuggers to eval in the scope of the breakpoint rather
3192 than in the scope of the debugger itself).
3198 Perl_find_runcv(pTHX_ U32 *db_seqp)
3204 *db_seqp = PL_curcop->cop_seq;
3205 for (si = PL_curstackinfo; si; si = si->si_prev) {
3207 for (ix = si->si_cxix; ix >= 0; ix--) {
3208 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3209 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3210 CV * const cv = cx->blk_sub.cv;
3211 /* skip DB:: code */
3212 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3213 *db_seqp = cx->blk_oldcop->cop_seq;
3218 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3226 /* Run yyparse() in a setjmp wrapper. Returns:
3227 * 0: yyparse() successful
3228 * 1: yyparse() failed
3232 S_try_yyparse(pTHX_ int gramtype)
3237 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3241 ret = yyparse(gramtype) ? 1 : 0;
3255 /* Compile a require/do, an eval '', or a /(?{...})/.
3256 * In the last case, startop is non-null, and contains the address of
3257 * a pointer that should be set to the just-compiled code.
3258 * outside is the lexically enclosing CV (if any) that invoked us.
3259 * Returns a bool indicating whether the compile was successful; if so,
3260 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3261 * pushes undef (also croaks if startop != NULL).
3265 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
3268 OP * const saveop = PL_op;
3269 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3272 PL_in_eval = (in_require
3273 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3278 SAVESPTR(PL_compcv);
3279 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3280 CvEVAL_on(PL_compcv);
3281 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3282 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3284 CvOUTSIDE_SEQ(PL_compcv) = seq;
3285 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3287 /* set up a scratch pad */
3289 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
3290 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3294 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
3296 /* make sure we compile in the right package */
3298 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3299 SAVESPTR(PL_curstash);
3300 PL_curstash = CopSTASH(PL_curcop);
3302 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3303 SAVESPTR(PL_beginav);
3304 PL_beginav = newAV();
3305 SAVEFREESV(PL_beginav);
3306 SAVESPTR(PL_unitcheckav);
3307 PL_unitcheckav = newAV();
3308 SAVEFREESV(PL_unitcheckav);
3311 SAVEBOOL(PL_madskills);
3315 /* try to compile it */
3317 PL_eval_root = NULL;
3318 PL_curcop = &PL_compiling;
3319 CopARYBASE_set(PL_curcop, 0);
3320 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3321 PL_in_eval |= EVAL_KEEPERR;
3325 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3327 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3328 * so honour CATCH_GET and trap it here if necessary */
3330 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3332 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3333 SV **newsp; /* Used by POPBLOCK. */
3334 PERL_CONTEXT *cx = NULL;
3335 I32 optype; /* Used by POPEVAL. */
3339 PERL_UNUSED_VAR(newsp);
3340 PERL_UNUSED_VAR(optype);
3342 /* note that if yystatus == 3, then the EVAL CX block has already
3343 * been popped, and various vars restored */
3345 if (yystatus != 3) {
3347 op_free(PL_eval_root);
3348 PL_eval_root = NULL;
3350 SP = PL_stack_base + POPMARK; /* pop original mark */
3352 POPBLOCK(cx,PL_curpm);
3354 namesv = cx->blk_eval.old_namesv;
3358 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3360 msg = SvPVx_nolen_const(ERRSV);
3363 /* If cx is still NULL, it means that we didn't go in the
3364 * POPEVAL branch. */
3365 cx = &cxstack[cxstack_ix];
3366 assert(CxTYPE(cx) == CXt_EVAL);
3367 namesv = cx->blk_eval.old_namesv;
3369 (void)hv_store(GvHVn(PL_incgv),
3370 SvPVX_const(namesv), SvCUR(namesv),
3372 Perl_croak(aTHX_ "%sCompilation failed in require",
3373 *msg ? msg : "Unknown error\n");
3376 if (yystatus != 3) {
3377 POPBLOCK(cx,PL_curpm);
3380 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3381 (*msg ? msg : "Unknown error\n"));
3385 sv_setpvs(ERRSV, "Compilation error");
3388 PUSHs(&PL_sv_undef);
3392 CopLINE_set(&PL_compiling, 0);
3394 *startop = PL_eval_root;
3396 SAVEFREEOP(PL_eval_root);
3398 /* Set the context for this new optree.
3399 * Propagate the context from the eval(). */
3400 if ((gimme & G_WANT) == G_VOID)
3401 scalarvoid(PL_eval_root);
3402 else if ((gimme & G_WANT) == G_ARRAY)
3405 scalar(PL_eval_root);
3407 DEBUG_x(dump_eval());
3409 /* Register with debugger: */
3410 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3411 CV * const cv = get_cvs("DB::postponed", 0);
3415 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3417 call_sv(MUTABLE_SV(cv), G_DISCARD);
3421 if (PL_unitcheckav) {
3422 OP *es = PL_eval_start;
3423 call_list(PL_scopestack_ix, PL_unitcheckav);
3427 /* compiled okay, so do it */
3429 CvDEPTH(PL_compcv) = 1;
3430 SP = PL_stack_base + POPMARK; /* pop original mark */
3431 PL_op = saveop; /* The caller may need it. */
3432 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3439 S_check_type_and_open(pTHX_ SV *name)
3442 const char *p = SvPV_nolen_const(name);
3443 const int st_rc = PerlLIO_stat(p, &st);
3445 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3447 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3451 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3452 return PerlIO_openn(aTHX_ NULL, PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3454 return PerlIO_open(p, PERL_SCRIPT_MODE);
3458 #ifndef PERL_DISABLE_PMC
3460 S_doopen_pm(pTHX_ SV *name)
3463 const char *p = SvPV_const(name, namelen);
3465 PERL_ARGS_ASSERT_DOOPEN_PM;
3467 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3468 SV *const pmcsv = sv_newmortal();
3471 SvSetSV_nosteal(pmcsv,name);
3472 sv_catpvn(pmcsv, "c", 1);
3474 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3475 return check_type_and_open(pmcsv);
3477 return check_type_and_open(name);
3480 # define doopen_pm(name) check_type_and_open(name)
3481 #endif /* !PERL_DISABLE_PMC */
3486 register PERL_CONTEXT *cx;
3493 int vms_unixname = 0;
3495 const char *tryname = NULL;
3497 const I32 gimme = GIMME_V;
3498 int filter_has_file = 0;
3499 PerlIO *tryrsfp = NULL;
3500 SV *filter_cache = NULL;
3501 SV *filter_state = NULL;
3502 SV *filter_sub = NULL;
3508 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3509 sv = sv_2mortal(new_version(sv));
3510 if (!sv_derived_from(PL_patchlevel, "version"))
3511 upg_version(PL_patchlevel, TRUE);
3512 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3513 if ( vcmp(sv,PL_patchlevel) <= 0 )
3514 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3515 SVfARG(sv_2mortal(vnormal(sv))),
3516 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3520 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3523 SV * const req = SvRV(sv);
3524 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3526 /* get the left hand term */
3527 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3529 first = SvIV(*av_fetch(lav,0,0));
3530 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3531 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3532 || av_len(lav) > 1 /* FP with > 3 digits */
3533 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3535 DIE(aTHX_ "Perl %"SVf" required--this is only "
3537 SVfARG(sv_2mortal(vnormal(req))),
3538 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3541 else { /* probably 'use 5.10' or 'use 5.8' */
3546 second = SvIV(*av_fetch(lav,1,0));
3548 second /= second >= 600 ? 100 : 10;
3549 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3550 (int)first, (int)second);
3551 upg_version(hintsv, TRUE);
3553 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3554 "--this is only %"SVf", stopped",
3555 SVfARG(sv_2mortal(vnormal(req))),
3556 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3557 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3565 name = SvPV_const(sv, len);
3566 if (!(name && len > 0 && *name))
3567 DIE(aTHX_ "Null filename used");
3568 TAINT_PROPER("require");
3572 /* The key in the %ENV hash is in the syntax of file passed as the argument
3573 * usually this is in UNIX format, but sometimes in VMS format, which
3574 * can result in a module being pulled in more than once.
3575 * To prevent this, the key must be stored in UNIX format if the VMS
3576 * name can be translated to UNIX.
3578 if ((unixname = tounixspec(name, NULL)) != NULL) {
3579 unixlen = strlen(unixname);
3585 /* if not VMS or VMS name can not be translated to UNIX, pass it
3588 unixname = (char *) name;
3591 if (PL_op->op_type == OP_REQUIRE) {
3592 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3593 unixname, unixlen, 0);
3595 if (*svp != &PL_sv_undef)
3598 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3599 "Compilation failed in require", unixname);
3603 /* prepare to compile file */
3605 if (path_is_absolute(name)) {
3606 /* At this point, name is SvPVX(sv) */
3608 tryrsfp = doopen_pm(sv);
3611 AV * const ar = GvAVn(PL_incgv);
3617 namesv = newSV_type(SVt_PV);
3618 for (i = 0; i <= AvFILL(ar); i++) {
3619 SV * const dirsv = *av_fetch(ar, i, TRUE);
3621 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3628 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3629 && !sv_isobject(loader))
3631 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3634 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3635 PTR2UV(SvRV(dirsv)), name);
3636 tryname = SvPVX_const(namesv);
3639 ENTER_with_name("call_INC");
3647 if (sv_isobject(loader))
3648 count = call_method("INC", G_ARRAY);
3650 count = call_sv(loader, G_ARRAY);
3660 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3661 && !isGV_with_GP(SvRV(arg))) {
3662 filter_cache = SvRV(arg);
3663 SvREFCNT_inc_simple_void_NN(filter_cache);
3670 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
3674 if (isGV_with_GP(arg)) {
3675 IO * const io = GvIO((const GV *)arg);
3680 tryrsfp = IoIFP(io);
3681 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3682 PerlIO_close(IoOFP(io));
3693 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3695 SvREFCNT_inc_simple_void_NN(filter_sub);
3698 filter_state = SP[i];
3699 SvREFCNT_inc_simple_void(filter_state);
3703 if (!tryrsfp && (filter_cache || filter_sub)) {
3704 tryrsfp = PerlIO_open(BIT_BUCKET,
3712 LEAVE_with_name("call_INC");
3714 /* Adjust file name if the hook has set an %INC entry.
3715 This needs to happen after the FREETMPS above. */
3716 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3718 tryname = SvPV_nolen_const(*svp);
3725 filter_has_file = 0;
3727 SvREFCNT_dec(filter_cache);
3728 filter_cache = NULL;
3731 SvREFCNT_dec(filter_state);
3732 filter_state = NULL;
3735 SvREFCNT_dec(filter_sub);
3740 if (!path_is_absolute(name)
3746 dir = SvPV_const(dirsv, dirlen);
3754 if ((unixdir = tounixpath(dir, NULL)) == NULL)
3756 sv_setpv(namesv, unixdir);
3757 sv_catpv(namesv, unixname);
3759 # ifdef __SYMBIAN32__
3760 if (PL_origfilename[0] &&
3761 PL_origfilename[1] == ':' &&
3762 !(dir[0] && dir[1] == ':'))
3763 Perl_sv_setpvf(aTHX_ namesv,
3768 Perl_sv_setpvf(aTHX_ namesv,
3772 /* The equivalent of
3773 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3774 but without the need to parse the format string, or
3775 call strlen on either pointer, and with the correct
3776 allocation up front. */
3778 char *tmp = SvGROW(namesv, dirlen + len + 2);
3780 memcpy(tmp, dir, dirlen);
3783 /* name came from an SV, so it will have a '\0' at the
3784 end that we can copy as part of this memcpy(). */
3785 memcpy(tmp, name, len + 1);
3787 SvCUR_set(namesv, dirlen + len + 1);
3792 TAINT_PROPER("require");
3793 tryname = SvPVX_const(namesv);
3794 tryrsfp = doopen_pm(namesv);
3796 if (tryname[0] == '.' && tryname[1] == '/') {
3798 while (*++tryname == '/');
3802 else if (errno == EMFILE)
3803 /* no point in trying other paths if out of handles */
3812 if (PL_op->op_type == OP_REQUIRE) {
3813 if(errno == EMFILE) {
3814 /* diag_listed_as: Can't locate %s */
3815 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno));
3817 if (namesv) { /* did we lookup @INC? */
3818 AV * const ar = GvAVn(PL_incgv);
3820 SV *const inc = newSVpvs_flags("", SVs_TEMP);
3821 for (i = 0; i <= AvFILL(ar); i++) {
3822 sv_catpvs(inc, " ");
3823 sv_catsv(inc, *av_fetch(ar, i, TRUE));
3826 /* diag_listed_as: Can't locate %s */
3828 "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")",
3830 (memEQ(name + len - 2, ".h", 3)
3831 ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""),
3832 (memEQ(name + len - 3, ".ph", 4)
3833 ? " (did you run h2ph?)" : ""),
3838 DIE(aTHX_ "Can't locate %s", name);
3844 SETERRNO(0, SS_NORMAL);
3846 /* Assume success here to prevent recursive requirement. */
3847 /* name is never assigned to again, so len is still strlen(name) */
3848 /* Check whether a hook in @INC has already filled %INC */
3850 (void)hv_store(GvHVn(PL_incgv),
3851 unixname, unixlen, newSVpv(tryname,0),0);
3853 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
3855 (void)hv_store(GvHVn(PL_incgv),
3856 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
3859 ENTER_with_name("eval");
3861 SAVECOPFILE_FREE(&PL_compiling);
3862 CopFILE_set(&PL_compiling, tryname);
3863 lex_start(NULL, tryrsfp, 0);
3867 hv_clear(GvHV(PL_hintgv));
3869 SAVECOMPILEWARNINGS();
3870 if (PL_dowarn & G_WARN_ALL_ON)
3871 PL_compiling.cop_warnings = pWARN_ALL ;
3872 else if (PL_dowarn & G_WARN_ALL_OFF)
3873 PL_compiling.cop_warnings = pWARN_NONE ;
3875 PL_compiling.cop_warnings = pWARN_STD ;
3877 if (filter_sub || filter_cache) {
3878 /* We can use the SvPV of the filter PVIO itself as our cache, rather
3879 than hanging another SV from it. In turn, filter_add() optionally
3880 takes the SV to use as the filter (or creates a new SV if passed
3881 NULL), so simply pass in whatever value filter_cache has. */
3882 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
3883 IoLINES(datasv) = filter_has_file;
3884 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
3885 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
3888 /* switch to eval mode */
3889 PUSHBLOCK(cx, CXt_EVAL, SP);
3891 cx->blk_eval.retop = PL_op->op_next;
3893 SAVECOPLINE(&PL_compiling);
3894 CopLINE_set(&PL_compiling, 0);
3898 /* Store and reset encoding. */
3899 encoding = PL_encoding;
3902 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq))
3903 op = DOCATCH(PL_eval_start);
3905 op = PL_op->op_next;
3907 /* Restore encoding. */
3908 PL_encoding = encoding;
3913 /* This is a op added to hold the hints hash for
3914 pp_entereval. The hash can be modified by the code
3915 being eval'ed, so we return a copy instead. */
3921 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
3929 register PERL_CONTEXT *cx;
3931 const I32 gimme = GIMME_V;
3932 const U32 was = PL_breakable_sub_gen;
3933 char tbuf[TYPE_DIGITS(long) + 12];
3934 bool saved_delete = FALSE;
3935 char *tmpbuf = tbuf;
3939 HV *saved_hh = NULL;
3941 if (PL_op->op_private & OPpEVAL_HAS_HH) {
3942 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
3946 /* make sure we've got a plain PV (no overload etc) before testing
3947 * for taint. Making a copy here is probably overkill, but better
3948 * safe than sorry */
3950 const char * const p = SvPV_const(sv, len);
3952 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
3955 TAINT_IF(SvTAINTED(sv));
3956 TAINT_PROPER("eval");
3958 ENTER_with_name("eval");
3959 lex_start(sv, NULL, LEX_START_SAME_FILTER);
3962 /* switch to eval mode */
3964 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3965 SV * const temp_sv = sv_newmortal();
3966 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
3967 (unsigned long)++PL_evalseq,
3968 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3969 tmpbuf = SvPVX(temp_sv);
3970 len = SvCUR(temp_sv);
3973 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
3974 SAVECOPFILE_FREE(&PL_compiling);
3975 CopFILE_set(&PL_compiling, tmpbuf+2);
3976 SAVECOPLINE(&PL_compiling);
3977 CopLINE_set(&PL_compiling, 1);
3978 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3979 deleting the eval's FILEGV from the stash before gv_check() runs
3980 (i.e. before run-time proper). To work around the coredump that
3981 ensues, we always turn GvMULTI_on for any globals that were
3982 introduced within evals. See force_ident(). GSAR 96-10-12 */
3984 PL_hints = PL_op->op_targ;
3986 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3987 SvREFCNT_dec(GvHV(PL_hintgv));
3988 GvHV(PL_hintgv) = saved_hh;
3990 SAVECOMPILEWARNINGS();
3991 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3992 cophh_free(CopHINTHASH_get(&PL_compiling));
3993 if (Perl_fetch_cop_label(aTHX_ PL_curcop, NULL, NULL)) {
3994 /* The label, if present, is the first entry on the chain. So rather
3995 than writing a blank label in front of it (which involves an
3996 allocation), just use the next entry in the chain. */
3997 PL_compiling.cop_hints_hash
3998 = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next);
3999 /* Check the assumption that this removed the label. */
4000 assert(Perl_fetch_cop_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
4003 PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash);
4004 /* special case: an eval '' executed within the DB package gets lexically
4005 * placed in the first non-DB CV rather than the current CV - this
4006 * allows the debugger to execute code, find lexicals etc, in the
4007 * scope of the code being debugged. Passing &seq gets find_runcv
4008 * to do the dirty work for us */
4009 runcv = find_runcv(&seq);
4011 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
4013 cx->blk_eval.retop = PL_op->op_next;
4015 /* prepare to compile string */
4017 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
4018 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
4020 char *const safestr = savepvn(tmpbuf, len);
4021 SAVEDELETE(PL_defstash, safestr, len);
4022 saved_delete = TRUE;
4027 if (doeval(gimme, NULL, runcv, seq)) {
4028 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4029 ? (PERLDB_LINE || PERLDB_SAVESRC)
4030 : PERLDB_SAVESRC_NOSUBS) {
4031 /* Retain the filegv we created. */
4032 } else if (!saved_delete) {
4033 char *const safestr = savepvn(tmpbuf, len);
4034 SAVEDELETE(PL_defstash, safestr, len);
4036 return DOCATCH(PL_eval_start);
4038 /* We have already left the scope set up earlier thanks to the LEAVE
4040 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4041 ? (PERLDB_LINE || PERLDB_SAVESRC)
4042 : PERLDB_SAVESRC_INVALID) {
4043 /* Retain the filegv we created. */
4044 } else if (!saved_delete) {
4045 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
4047 return PL_op->op_next;
4058 register PERL_CONTEXT *cx;
4060 const U8 save_flags = PL_op -> op_flags;
4067 namesv = cx->blk_eval.old_namesv;
4068 retop = cx->blk_eval.retop;
4071 if (gimme == G_VOID)
4073 else if (gimme == G_SCALAR) {
4076 if (SvFLAGS(TOPs) & SVs_TEMP)
4079 *MARK = sv_mortalcopy(TOPs);
4083 *MARK = &PL_sv_undef;
4088 /* in case LEAVE wipes old return values */
4089 for (mark = newsp + 1; mark <= SP; mark++) {
4090 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
4091 *mark = sv_mortalcopy(*mark);
4092 TAINT_NOT; /* Each item is independent */
4096 PL_curpm = newpm; /* Don't pop $1 et al till now */
4099 assert(CvDEPTH(PL_compcv) == 1);
4101 CvDEPTH(PL_compcv) = 0;
4103 if (optype == OP_REQUIRE &&
4104 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4106 /* Unassume the success we assumed earlier. */
4107 (void)hv_delete(GvHVn(PL_incgv),
4108 SvPVX_const(namesv), SvCUR(namesv),
4110 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4112 /* die_unwind() did LEAVE, or we won't be here */
4115 LEAVE_with_name("eval");
4116 if (!(save_flags & OPf_SPECIAL)) {
4124 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4125 close to the related Perl_create_eval_scope. */
4127 Perl_delete_eval_scope(pTHX)
4132 register PERL_CONTEXT *cx;
4138 LEAVE_with_name("eval_scope");
4139 PERL_UNUSED_VAR(newsp);
4140 PERL_UNUSED_VAR(gimme);
4141 PERL_UNUSED_VAR(optype);
4144 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4145 also needed by Perl_fold_constants. */
4147 Perl_create_eval_scope(pTHX_ U32 flags)
4150 const I32 gimme = GIMME_V;
4152 ENTER_with_name("eval_scope");
4155 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4158 PL_in_eval = EVAL_INEVAL;
4159 if (flags & G_KEEPERR)
4160 PL_in_eval |= EVAL_KEEPERR;
4163 if (flags & G_FAKINGEVAL) {
4164 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4172 PERL_CONTEXT * const cx = create_eval_scope(0);
4173 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4174 return DOCATCH(PL_op->op_next);
4183 register PERL_CONTEXT *cx;
4189 PERL_UNUSED_VAR(optype);
4192 if (gimme == G_VOID)
4194 else if (gimme == G_SCALAR) {
4198 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4201 *MARK = sv_mortalcopy(TOPs);
4205 *MARK = &PL_sv_undef;
4210 /* in case LEAVE wipes old return values */
4212 for (mark = newsp + 1; mark <= SP; mark++) {
4213 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4214 *mark = sv_mortalcopy(*mark);
4215 TAINT_NOT; /* Each item is independent */
4219 PL_curpm = newpm; /* Don't pop $1 et al till now */
4221 LEAVE_with_name("eval_scope");
4229 register PERL_CONTEXT *cx;
4230 const I32 gimme = GIMME_V;
4232 ENTER_with_name("given");
4235 sv_setsv_mg(PAD_SV(PL_op->op_targ), POPs);
4237 PUSHBLOCK(cx, CXt_GIVEN, SP);
4246 register PERL_CONTEXT *cx;
4250 PERL_UNUSED_CONTEXT;
4253 assert(CxTYPE(cx) == CXt_GIVEN);
4256 if (gimme == G_VOID)
4258 else if (gimme == G_SCALAR) {
4262 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4265 *MARK = sv_mortalcopy(TOPs);
4269 *MARK = &PL_sv_undef;
4274 /* in case LEAVE wipes old return values */
4276 for (mark = newsp + 1; mark <= SP; mark++) {
4277 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4278 *mark = sv_mortalcopy(*mark);
4279 TAINT_NOT; /* Each item is independent */
4283 PL_curpm = newpm; /* Don't pop $1 et al till now */
4285 LEAVE_with_name("given");
4289 /* Helper routines used by pp_smartmatch */
4291 S_make_matcher(pTHX_ REGEXP *re)
4294 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4296 PERL_ARGS_ASSERT_MAKE_MATCHER;
4298 PM_SETRE(matcher, ReREFCNT_inc(re));
4300 SAVEFREEOP((OP *) matcher);
4301 ENTER_with_name("matcher"); SAVETMPS;
4307 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4312 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4314 PL_op = (OP *) matcher;
4317 (void) Perl_pp_match(aTHX);
4319 return (SvTRUEx(POPs));
4323 S_destroy_matcher(pTHX_ PMOP *matcher)
4327 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4328 PERL_UNUSED_ARG(matcher);
4331 LEAVE_with_name("matcher");
4334 /* Do a smart match */
4337 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4338 return do_smartmatch(NULL, NULL);
4341 /* This version of do_smartmatch() implements the
4342 * table of smart matches that is found in perlsyn.
4345 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
4350 bool object_on_left = FALSE;
4351 SV *e = TOPs; /* e is for 'expression' */
4352 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4354 /* Take care only to invoke mg_get() once for each argument.
4355 * Currently we do this by copying the SV if it's magical. */
4358 d = sv_mortalcopy(d);
4365 e = sv_mortalcopy(e);
4367 /* First of all, handle overload magic of the rightmost argument */
4370 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4371 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4373 tmpsv = amagic_call(d, e, smart_amg, 0);
4380 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4383 SP -= 2; /* Pop the values */
4388 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4395 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4396 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4397 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4399 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4400 object_on_left = TRUE;
4403 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4405 if (object_on_left) {
4406 goto sm_any_sub; /* Treat objects like scalars */
4408 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4409 /* Test sub truth for each key */
4411 bool andedresults = TRUE;
4412 HV *hv = (HV*) SvRV(d);
4413 I32 numkeys = hv_iterinit(hv);
4414 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4417 while ( (he = hv_iternext(hv)) ) {
4418 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4419 ENTER_with_name("smartmatch_hash_key_test");
4422 PUSHs(hv_iterkeysv(he));
4424 c = call_sv(e, G_SCALAR);
4427 andedresults = FALSE;
4429 andedresults = SvTRUEx(POPs) && andedresults;
4431 LEAVE_with_name("smartmatch_hash_key_test");
4438 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4439 /* Test sub truth for each element */
4441 bool andedresults = TRUE;
4442 AV *av = (AV*) SvRV(d);
4443 const I32 len = av_len(av);
4444 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4447 for (i = 0; i <= len; ++i) {
4448 SV * const * const svp = av_fetch(av, i, FALSE);
4449 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4450 ENTER_with_name("smartmatch_array_elem_test");
4456 c = call_sv(e, G_SCALAR);
4459 andedresults = FALSE;
4461 andedresults = SvTRUEx(POPs) && andedresults;
4463 LEAVE_with_name("smartmatch_array_elem_test");
4472 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4473 ENTER_with_name("smartmatch_coderef");
4478 c = call_sv(e, G_SCALAR);
4482 else if (SvTEMP(TOPs))
4483 SvREFCNT_inc_void(TOPs);
4485 LEAVE_with_name("smartmatch_coderef");
4490 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4491 if (object_on_left) {
4492 goto sm_any_hash; /* Treat objects like scalars */
4494 else if (!SvOK(d)) {
4495 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4498 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4499 /* Check that the key-sets are identical */