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]));
522 #define FORM_NUM_BLANK (1<<30)
523 #define FORM_NUM_POINT (1<<29)
527 dVAR; dSP; dMARK; dORIGMARK;
528 register SV * const tmpForm = *++MARK;
529 SV *formsv; /* contains text of original format */
530 register U32 *fpc; /* format ops program counter */
531 register char *t; /* current append position in target string */
532 const char *f; /* current position in format string */
534 register SV *sv = NULL; /* current item */
535 const char *item = NULL;/* string value of current item */
536 I32 itemsize = 0; /* length of current item, possibly truncated */
537 I32 fieldsize = 0; /* width of current field */
538 I32 lines = 0; /* number of lines that have been output */
539 bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
540 const char *chophere = NULL; /* where to chop current item */
541 STRLEN linemark = 0; /* pos of start of line in output */
543 bool gotsome = FALSE; /* seen at least one non-blank item on this line */
545 STRLEN linemax; /* estimate of output size in bytes */
546 bool item_is_utf8 = FALSE;
547 bool targ_is_utf8 = FALSE;
550 U8 *source; /* source of bytes to append */
551 STRLEN to_copy; /* how may bytes to append */
552 char trans; /* what chars to translate */
554 mg = doparseform(tmpForm);
556 fpc = (U32*)mg->mg_ptr;
557 /* the actual string the format was compiled from.
558 * with overload etc, this may not match tmpForm */
562 SvPV_force(PL_formtarget, len);
563 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
564 SvTAINTED_on(PL_formtarget);
565 if (DO_UTF8(PL_formtarget))
567 linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
568 t = SvGROW(PL_formtarget, len + linemax + 1);
569 /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */
571 f = SvPV_const(formsv, len);
575 const char *name = "???";
578 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
579 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
580 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
581 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
582 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
584 case FF_CHECKNL: name = "CHECKNL"; break;
585 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
586 case FF_SPACE: name = "SPACE"; break;
587 case FF_HALFSPACE: name = "HALFSPACE"; break;
588 case FF_ITEM: name = "ITEM"; break;
589 case FF_CHOP: name = "CHOP"; break;
590 case FF_LINEGLOB: name = "LINEGLOB"; break;
591 case FF_NEWLINE: name = "NEWLINE"; break;
592 case FF_MORE: name = "MORE"; break;
593 case FF_LINEMARK: name = "LINEMARK"; break;
594 case FF_END: name = "END"; break;
595 case FF_0DECIMAL: name = "0DECIMAL"; break;
596 case FF_LINESNGL: name = "LINESNGL"; break;
599 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
601 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
605 linemark = t - SvPVX(PL_formtarget);
615 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
631 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
634 SvTAINTED_on(PL_formtarget);
640 const char *s = item = SvPV_const(sv, len);
643 itemsize = sv_len_utf8(sv);
644 if (itemsize != (I32)len) {
646 if (itemsize > fieldsize) {
647 itemsize = fieldsize;
648 itembytes = itemsize;
649 sv_pos_u2b(sv, &itembytes, 0);
653 send = chophere = s + itembytes;
663 sv_pos_b2u(sv, &itemsize);
667 item_is_utf8 = FALSE;
668 if (itemsize > fieldsize)
669 itemsize = fieldsize;
670 send = chophere = s + itemsize;
684 const char *s = item = SvPV_const(sv, len);
687 itemsize = sv_len_utf8(sv);
688 if (itemsize != (I32)len) {
690 if (itemsize <= fieldsize) {
691 const char *send = chophere = s + itemsize;
704 itemsize = fieldsize;
705 itembytes = itemsize;
706 sv_pos_u2b(sv, &itembytes, 0);
707 send = chophere = s + itembytes;
708 while (s < send || (s == send && isSPACE(*s))) {
718 if (strchr(PL_chopset, *s))
723 itemsize = chophere - item;
724 sv_pos_b2u(sv, &itemsize);
730 item_is_utf8 = FALSE;
731 if (itemsize <= fieldsize) {
732 const char *const send = chophere = s + itemsize;
745 itemsize = fieldsize;
746 send = chophere = s + itemsize;
747 while (s < send || (s == send && isSPACE(*s))) {
757 if (strchr(PL_chopset, *s))
762 itemsize = chophere - item;
768 arg = fieldsize - itemsize;
777 arg = fieldsize - itemsize;
791 /* convert to_copy from chars to bytes */
795 to_copy = s - source;
801 const char *s = chophere;
815 const bool oneline = fpc[-1] == FF_LINESNGL;
816 const char *s = item = SvPV_const(sv, len);
817 const char *const send = s + len;
819 item_is_utf8 = DO_UTF8(sv);
830 to_copy = s - SvPVX_const(sv) - 1;
844 /* append to_copy bytes from source to PL_formstring.
845 * item_is_utf8 implies source is utf8.
846 * if trans, translate certain characters during the copy */
851 SvCUR_set(PL_formtarget,
852 t - SvPVX_const(PL_formtarget));
854 if (targ_is_utf8 && !item_is_utf8) {
855 source = tmp = bytes_to_utf8(source, &to_copy);
857 if (item_is_utf8 && !targ_is_utf8) {
859 /* Upgrade targ to UTF8, and then we reduce it to
860 a problem we have a simple solution for.
861 Don't need get magic. */
862 sv_utf8_upgrade_nomg(PL_formtarget);
864 /* re-calculate linemark */
865 s = (U8*)SvPVX(PL_formtarget);
866 /* the bytes we initially allocated to append the
867 * whole line may have been gobbled up during the
868 * upgrade, so allocate a whole new line's worth
873 linemark = s - (U8*)SvPVX(PL_formtarget);
875 /* Easy. They agree. */
876 assert (item_is_utf8 == targ_is_utf8);
879 /* @* and ^* are the only things that can exceed
880 * the linemax, so grow by the output size, plus
881 * a whole new form's worth in case of any further
883 grow = linemax + to_copy;
885 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1);
886 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
888 Copy(source, t, to_copy, char);
890 /* blank out ~ or control chars, depending on trans.
891 * works on bytes not chars, so relies on not
892 * matching utf8 continuation bytes */
894 U8 *send = s + to_copy;
897 if (trans == '~' ? (ch == '~') :
910 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
918 #if defined(USE_LONG_DOUBLE)
920 ((arg & FORM_NUM_POINT) ?
921 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
924 ((arg & FORM_NUM_POINT) ?
925 "%#0*.*f" : "%0*.*f");
930 #if defined(USE_LONG_DOUBLE)
932 ((arg & FORM_NUM_POINT) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
935 ((arg & FORM_NUM_POINT) ? "%#*.*f" : "%*.*f");
938 /* If the field is marked with ^ and the value is undefined,
940 if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) {
948 /* overflow evidence */
949 if (num_overflow(value, fieldsize, arg)) {
955 /* Formats aren't yet marked for locales, so assume "yes". */
957 STORE_NUMERIC_STANDARD_SET_LOCAL();
958 arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
959 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg, value);
960 RESTORE_NUMERIC_STANDARD();
967 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
975 if (arg) { /* repeat until fields exhausted? */
981 t = SvPVX(PL_formtarget) + linemark;
988 const char *s = chophere;
989 const char *send = item + len;
991 while (isSPACE(*s) && (s < send))
996 arg = fieldsize - itemsize;
1003 if (strnEQ(s1," ",3)) {
1004 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1015 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
1017 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1019 SvUTF8_on(PL_formtarget);
1020 FmLINES(PL_formtarget) += lines;
1022 if (fpc[-1] == FF_BLANK)
1023 RETURNOP(cLISTOP->op_first);
1035 if (PL_stack_base + *PL_markstack_ptr == SP) {
1037 if (GIMME_V == G_SCALAR)
1039 RETURNOP(PL_op->op_next->op_next);
1041 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
1042 Perl_pp_pushmark(aTHX); /* push dst */
1043 Perl_pp_pushmark(aTHX); /* push src */
1044 ENTER_with_name("grep"); /* enter outer scope */
1047 if (PL_op->op_private & OPpGREP_LEX)
1048 SAVESPTR(PAD_SVl(PL_op->op_targ));
1051 ENTER_with_name("grep_item"); /* enter inner scope */
1054 src = PL_stack_base[*PL_markstack_ptr];
1056 if (PL_op->op_private & OPpGREP_LEX)
1057 PAD_SVl(PL_op->op_targ) = src;
1062 if (PL_op->op_type == OP_MAPSTART)
1063 Perl_pp_pushmark(aTHX); /* push top */
1064 return ((LOGOP*)PL_op->op_next)->op_other;
1070 const I32 gimme = GIMME_V;
1071 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1077 /* first, move source pointer to the next item in the source list */
1078 ++PL_markstack_ptr[-1];
1080 /* if there are new items, push them into the destination list */
1081 if (items && gimme != G_VOID) {
1082 /* might need to make room back there first */
1083 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1084 /* XXX this implementation is very pessimal because the stack
1085 * is repeatedly extended for every set of items. Is possible
1086 * to do this without any stack extension or copying at all
1087 * by maintaining a separate list over which the map iterates
1088 * (like foreach does). --gsar */
1090 /* everything in the stack after the destination list moves
1091 * towards the end the stack by the amount of room needed */
1092 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1094 /* items to shift up (accounting for the moved source pointer) */
1095 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1097 /* This optimization is by Ben Tilly and it does
1098 * things differently from what Sarathy (gsar)
1099 * is describing. The downside of this optimization is
1100 * that leaves "holes" (uninitialized and hopefully unused areas)
1101 * to the Perl stack, but on the other hand this
1102 * shouldn't be a problem. If Sarathy's idea gets
1103 * implemented, this optimization should become
1104 * irrelevant. --jhi */
1106 shift = count; /* Avoid shifting too often --Ben Tilly */
1110 dst = (SP += shift);
1111 PL_markstack_ptr[-1] += shift;
1112 *PL_markstack_ptr += shift;
1116 /* copy the new items down to the destination list */
1117 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1118 if (gimme == G_ARRAY) {
1119 /* add returned items to the collection (making mortal copies
1120 * if necessary), then clear the current temps stack frame
1121 * *except* for those items. We do this splicing the items
1122 * into the start of the tmps frame (so some items may be on
1123 * the tmps stack twice), then moving PL_tmps_floor above
1124 * them, then freeing the frame. That way, the only tmps that
1125 * accumulate over iterations are the return values for map.
1126 * We have to do to this way so that everything gets correctly
1127 * freed if we die during the map.
1131 /* make space for the slice */
1132 EXTEND_MORTAL(items);
1133 tmpsbase = PL_tmps_floor + 1;
1134 Move(PL_tmps_stack + tmpsbase,
1135 PL_tmps_stack + tmpsbase + items,
1136 PL_tmps_ix - PL_tmps_floor,
1138 PL_tmps_ix += items;
1143 sv = sv_mortalcopy(sv);
1145 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1147 /* clear the stack frame except for the items */
1148 PL_tmps_floor += items;
1150 /* FREETMPS may have cleared the TEMP flag on some of the items */
1153 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1156 /* scalar context: we don't care about which values map returns
1157 * (we use undef here). And so we certainly don't want to do mortal
1158 * copies of meaningless values. */
1159 while (items-- > 0) {
1161 *dst-- = &PL_sv_undef;
1169 LEAVE_with_name("grep_item"); /* exit inner scope */
1172 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1174 (void)POPMARK; /* pop top */
1175 LEAVE_with_name("grep"); /* exit outer scope */
1176 (void)POPMARK; /* pop src */
1177 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1178 (void)POPMARK; /* pop dst */
1179 SP = PL_stack_base + POPMARK; /* pop original mark */
1180 if (gimme == G_SCALAR) {
1181 if (PL_op->op_private & OPpGREP_LEX) {
1182 SV* sv = sv_newmortal();
1183 sv_setiv(sv, items);
1191 else if (gimme == G_ARRAY)
1198 ENTER_with_name("grep_item"); /* enter inner scope */
1201 /* set $_ to the new source item */
1202 src = PL_stack_base[PL_markstack_ptr[-1]];
1204 if (PL_op->op_private & OPpGREP_LEX)
1205 PAD_SVl(PL_op->op_targ) = src;
1209 RETURNOP(cLOGOP->op_other);
1218 if (GIMME == G_ARRAY)
1220 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1221 return cLOGOP->op_other;
1231 if (GIMME == G_ARRAY) {
1232 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1236 SV * const targ = PAD_SV(PL_op->op_targ);
1239 if (PL_op->op_private & OPpFLIP_LINENUM) {
1240 if (GvIO(PL_last_in_gv)) {
1241 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1244 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1246 flip = SvIV(sv) == SvIV(GvSV(gv));
1252 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1253 if (PL_op->op_flags & OPf_SPECIAL) {
1261 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1264 sv_setpvs(TARG, "");
1270 /* This code tries to decide if "$left .. $right" should use the
1271 magical string increment, or if the range is numeric (we make
1272 an exception for .."0" [#18165]). AMS 20021031. */
1274 #define RANGE_IS_NUMERIC(left,right) ( \
1275 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1276 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1277 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1278 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1279 && (!SvOK(right) || looks_like_number(right))))
1285 if (GIMME == G_ARRAY) {
1291 if (RANGE_IS_NUMERIC(left,right)) {
1294 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1295 (SvOK(right) && SvNV(right) > IV_MAX))
1296 DIE(aTHX_ "Range iterator outside integer range");
1307 SV * const sv = sv_2mortal(newSViv(i++));
1312 SV * const final = sv_mortalcopy(right);
1314 const char * const tmps = SvPV_const(final, len);
1316 SV *sv = sv_mortalcopy(left);
1317 SvPV_force_nolen(sv);
1318 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1320 if (strEQ(SvPVX_const(sv),tmps))
1322 sv = sv_2mortal(newSVsv(sv));
1329 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1333 if (PL_op->op_private & OPpFLIP_LINENUM) {
1334 if (GvIO(PL_last_in_gv)) {
1335 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1338 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1339 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1347 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1348 sv_catpvs(targ, "E0");
1358 static const char * const context_name[] = {
1360 NULL, /* CXt_WHEN never actually needs "block" */
1361 NULL, /* CXt_BLOCK never actually needs "block" */
1362 NULL, /* CXt_GIVEN never actually needs "block" */
1363 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1364 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1365 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1366 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1374 S_dopoptolabel(pTHX_ const char *label)
1379 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1381 for (i = cxstack_ix; i >= 0; i--) {
1382 register const PERL_CONTEXT * const cx = &cxstack[i];
1383 switch (CxTYPE(cx)) {
1389 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1390 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1391 if (CxTYPE(cx) == CXt_NULL)
1394 case CXt_LOOP_LAZYIV:
1395 case CXt_LOOP_LAZYSV:
1397 case CXt_LOOP_PLAIN:
1399 const char *cx_label = CxLABEL(cx);
1400 if (!cx_label || strNE(label, cx_label) ) {
1401 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1402 (long)i, cx_label));
1405 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1416 Perl_dowantarray(pTHX)
1419 const I32 gimme = block_gimme();
1420 return (gimme == G_VOID) ? G_SCALAR : gimme;
1424 Perl_block_gimme(pTHX)
1427 const I32 cxix = dopoptosub(cxstack_ix);
1431 switch (cxstack[cxix].blk_gimme) {
1439 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1446 Perl_is_lvalue_sub(pTHX)
1449 const I32 cxix = dopoptosub(cxstack_ix);
1450 assert(cxix >= 0); /* We should only be called from inside subs */
1452 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1453 return CxLVAL(cxstack + cxix);
1459 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1464 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1466 for (i = startingblock; i >= 0; i--) {
1467 register const PERL_CONTEXT * const cx = &cxstk[i];
1468 switch (CxTYPE(cx)) {
1474 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1482 S_dopoptoeval(pTHX_ I32 startingblock)
1486 for (i = startingblock; i >= 0; i--) {
1487 register const PERL_CONTEXT *cx = &cxstack[i];
1488 switch (CxTYPE(cx)) {
1492 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1500 S_dopoptoloop(pTHX_ I32 startingblock)
1504 for (i = startingblock; i >= 0; i--) {
1505 register const PERL_CONTEXT * const cx = &cxstack[i];
1506 switch (CxTYPE(cx)) {
1512 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1513 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1514 if ((CxTYPE(cx)) == CXt_NULL)
1517 case CXt_LOOP_LAZYIV:
1518 case CXt_LOOP_LAZYSV:
1520 case CXt_LOOP_PLAIN:
1521 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1529 S_dopoptogiven(pTHX_ I32 startingblock)
1533 for (i = startingblock; i >= 0; i--) {
1534 register const PERL_CONTEXT *cx = &cxstack[i];
1535 switch (CxTYPE(cx)) {
1539 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
1541 case CXt_LOOP_PLAIN:
1542 assert(!CxFOREACHDEF(cx));
1544 case CXt_LOOP_LAZYIV:
1545 case CXt_LOOP_LAZYSV:
1547 if (CxFOREACHDEF(cx)) {
1548 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
1557 S_dopoptowhen(pTHX_ I32 startingblock)
1561 for (i = startingblock; i >= 0; i--) {
1562 register const PERL_CONTEXT *cx = &cxstack[i];
1563 switch (CxTYPE(cx)) {
1567 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1575 Perl_dounwind(pTHX_ I32 cxix)
1580 while (cxstack_ix > cxix) {
1582 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1583 DEBUG_CX("UNWIND"); \
1584 /* Note: we don't need to restore the base context info till the end. */
1585 switch (CxTYPE(cx)) {
1588 continue; /* not break */
1596 case CXt_LOOP_LAZYIV:
1597 case CXt_LOOP_LAZYSV:
1599 case CXt_LOOP_PLAIN:
1610 PERL_UNUSED_VAR(optype);
1614 Perl_qerror(pTHX_ SV *err)
1618 PERL_ARGS_ASSERT_QERROR;
1621 if (PL_in_eval & EVAL_KEEPERR) {
1622 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1623 SvPV_nolen_const(err));
1626 sv_catsv(ERRSV, err);
1629 sv_catsv(PL_errors, err);
1631 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
1633 ++PL_parser->error_count;
1637 Perl_die_unwind(pTHX_ SV *msv)
1640 SV *exceptsv = sv_mortalcopy(msv);
1641 U8 in_eval = PL_in_eval;
1642 PERL_ARGS_ASSERT_DIE_UNWIND;
1649 * Historically, perl used to set ERRSV ($@) early in the die
1650 * process and rely on it not getting clobbered during unwinding.
1651 * That sucked, because it was liable to get clobbered, so the
1652 * setting of ERRSV used to emit the exception from eval{} has
1653 * been moved to much later, after unwinding (see just before
1654 * JMPENV_JUMP below). However, some modules were relying on the
1655 * early setting, by examining $@ during unwinding to use it as
1656 * a flag indicating whether the current unwinding was caused by
1657 * an exception. It was never a reliable flag for that purpose,
1658 * being totally open to false positives even without actual
1659 * clobberage, but was useful enough for production code to
1660 * semantically rely on it.
1662 * We'd like to have a proper introspective interface that
1663 * explicitly describes the reason for whatever unwinding
1664 * operations are currently in progress, so that those modules
1665 * work reliably and $@ isn't further overloaded. But we don't
1666 * have one yet. In its absence, as a stopgap measure, ERRSV is
1667 * now *additionally* set here, before unwinding, to serve as the
1668 * (unreliable) flag that it used to.
1670 * This behaviour is temporary, and should be removed when a
1671 * proper way to detect exceptional unwinding has been developed.
1672 * As of 2010-12, the authors of modules relying on the hack
1673 * are aware of the issue, because the modules failed on
1674 * perls 5.13.{1..7} which had late setting of $@ without this
1675 * early-setting hack.
1677 if (!(in_eval & EVAL_KEEPERR)) {
1678 SvTEMP_off(exceptsv);
1679 sv_setsv(ERRSV, exceptsv);
1682 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1683 && PL_curstackinfo->si_prev)
1692 register PERL_CONTEXT *cx;
1695 JMPENV *restartjmpenv;
1698 if (cxix < cxstack_ix)
1701 POPBLOCK(cx,PL_curpm);
1702 if (CxTYPE(cx) != CXt_EVAL) {
1704 const char* message = SvPVx_const(exceptsv, msglen);
1705 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
1706 PerlIO_write(Perl_error_log, message, msglen);
1710 namesv = cx->blk_eval.old_namesv;
1711 oldcop = cx->blk_oldcop;
1712 restartjmpenv = cx->blk_eval.cur_top_env;
1713 restartop = cx->blk_eval.retop;
1715 if (gimme == G_SCALAR)
1716 *++newsp = &PL_sv_undef;
1717 PL_stack_sp = newsp;
1721 /* LEAVE could clobber PL_curcop (see save_re_context())
1722 * XXX it might be better to find a way to avoid messing with
1723 * PL_curcop in save_re_context() instead, but this is a more
1724 * minimal fix --GSAR */
1727 if (optype == OP_REQUIRE) {
1728 const char* const msg = SvPVx_nolen_const(exceptsv);
1729 (void)hv_store(GvHVn(PL_incgv),
1730 SvPVX_const(namesv), SvCUR(namesv),
1732 /* note that unlike pp_entereval, pp_require isn't
1733 * supposed to trap errors. So now that we've popped the
1734 * EVAL that pp_require pushed, and processed the error
1735 * message, rethrow the error */
1736 Perl_croak(aTHX_ "%sCompilation failed in require",
1737 *msg ? msg : "Unknown error\n");
1739 if (in_eval & EVAL_KEEPERR) {
1740 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1741 SvPV_nolen_const(exceptsv));
1744 sv_setsv(ERRSV, exceptsv);
1746 PL_restartjmpenv = restartjmpenv;
1747 PL_restartop = restartop;
1753 write_to_stderr(exceptsv);
1760 dVAR; dSP; dPOPTOPssrl;
1761 if (SvTRUE(left) != SvTRUE(right))
1768 =for apidoc caller_cx
1770 The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1771 returned C<PERL_CONTEXT> structure can be interrogated to find all the
1772 information returned to Perl by C<caller>. Note that XSUBs don't get a
1773 stack frame, so C<caller_cx(0, NULL)> will return information for the
1774 immediately-surrounding Perl code.
1776 This function skips over the automatic calls to C<&DB::sub> made on the
1777 behalf of the debugger. If the stack frame requested was a sub called by
1778 C<DB::sub>, the return value will be the frame for the call to
1779 C<DB::sub>, since that has the correct line number/etc. for the call
1780 site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1781 frame for the sub call itself.
1786 const PERL_CONTEXT *
1787 Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
1789 register I32 cxix = dopoptosub(cxstack_ix);
1790 register const PERL_CONTEXT *cx;
1791 register const PERL_CONTEXT *ccstack = cxstack;
1792 const PERL_SI *top_si = PL_curstackinfo;
1795 /* we may be in a higher stacklevel, so dig down deeper */
1796 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1797 top_si = top_si->si_prev;
1798 ccstack = top_si->si_cxstack;
1799 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1803 /* caller() should not report the automatic calls to &DB::sub */
1804 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1805 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1809 cxix = dopoptosub_at(ccstack, cxix - 1);
1812 cx = &ccstack[cxix];
1813 if (dbcxp) *dbcxp = cx;
1815 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1816 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1817 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1818 field below is defined for any cx. */
1819 /* caller() should not report the automatic calls to &DB::sub */
1820 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1821 cx = &ccstack[dbcxix];
1831 register const PERL_CONTEXT *cx;
1832 const PERL_CONTEXT *dbcx;
1834 const char *stashname;
1840 cx = caller_cx(count, &dbcx);
1842 if (GIMME != G_ARRAY) {
1849 stashname = CopSTASHPV(cx->blk_oldcop);
1850 if (GIMME != G_ARRAY) {
1853 PUSHs(&PL_sv_undef);
1856 sv_setpv(TARG, stashname);
1865 PUSHs(&PL_sv_undef);
1867 mPUSHs(newSVpv(stashname, 0));
1868 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1869 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1872 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1873 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
1874 /* So is ccstack[dbcxix]. */
1876 SV * const sv = newSV(0);
1877 gv_efullname3(sv, cvgv, NULL);
1879 PUSHs(boolSV(CxHASARGS(cx)));
1882 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1883 PUSHs(boolSV(CxHASARGS(cx)));
1887 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1890 gimme = (I32)cx->blk_gimme;
1891 if (gimme == G_VOID)
1892 PUSHs(&PL_sv_undef);
1894 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1895 if (CxTYPE(cx) == CXt_EVAL) {
1897 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1898 PUSHs(cx->blk_eval.cur_text);
1902 else if (cx->blk_eval.old_namesv) {
1903 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1906 /* eval BLOCK (try blocks have old_namesv == 0) */
1908 PUSHs(&PL_sv_undef);
1909 PUSHs(&PL_sv_undef);
1913 PUSHs(&PL_sv_undef);
1914 PUSHs(&PL_sv_undef);
1916 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1917 && CopSTASH_eq(PL_curcop, PL_debstash))
1919 AV * const ary = cx->blk_sub.argarray;
1920 const int off = AvARRAY(ary) - AvALLOC(ary);
1923 Perl_init_dbargs(aTHX);
1925 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1926 av_extend(PL_dbargs, AvFILLp(ary) + off);
1927 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1928 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1930 /* XXX only hints propagated via op_private are currently
1931 * visible (others are not easily accessible, since they
1932 * use the global PL_hints) */
1933 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1936 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1938 if (old_warnings == pWARN_NONE ||
1939 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1940 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1941 else if (old_warnings == pWARN_ALL ||
1942 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1943 /* Get the bit mask for $warnings::Bits{all}, because
1944 * it could have been extended by warnings::register */
1946 HV * const bits = get_hv("warnings::Bits", 0);
1947 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
1948 mask = newSVsv(*bits_all);
1951 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1955 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
1959 PUSHs(cx->blk_oldcop->cop_hints_hash ?
1960 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
1969 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
1970 sv_reset(tmps, CopSTASH(PL_curcop));
1975 /* like pp_nextstate, but used instead when the debugger is active */
1980 PL_curcop = (COP*)PL_op;
1981 TAINT_NOT; /* Each statement is presumed innocent */
1982 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1987 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1988 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1991 register PERL_CONTEXT *cx;
1992 const I32 gimme = G_ARRAY;
1994 GV * const gv = PL_DBgv;
1995 register CV * const cv = GvCV(gv);
1998 DIE(aTHX_ "No DB::DB routine defined");
2000 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2001 /* don't do recursive DB::DB call */
2016 (void)(*CvXSUB(cv))(aTHX_ cv);
2023 PUSHBLOCK(cx, CXt_SUB, SP);
2025 cx->blk_sub.retop = PL_op->op_next;
2028 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
2029 RETURNOP(CvSTART(cv));
2039 register PERL_CONTEXT *cx;
2040 const I32 gimme = GIMME_V;
2041 void *itervar; /* location of the iteration variable */
2042 U8 cxtype = CXt_LOOP_FOR;
2044 ENTER_with_name("loop1");
2047 if (PL_op->op_targ) { /* "my" variable */
2048 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
2049 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2050 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2051 SVs_PADSTALE, SVs_PADSTALE);
2053 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
2055 itervar = PL_comppad;
2057 itervar = &PAD_SVl(PL_op->op_targ);
2060 else { /* symbol table variable */
2061 GV * const gv = MUTABLE_GV(POPs);
2062 SV** svp = &GvSV(gv);
2063 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
2065 itervar = (void *)gv;
2068 if (PL_op->op_private & OPpITER_DEF)
2069 cxtype |= CXp_FOR_DEF;
2071 ENTER_with_name("loop2");
2073 PUSHBLOCK(cx, cxtype, SP);
2074 PUSHLOOP_FOR(cx, itervar, MARK);
2075 if (PL_op->op_flags & OPf_STACKED) {
2076 SV *maybe_ary = POPs;
2077 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2079 SV * const right = maybe_ary;
2082 if (RANGE_IS_NUMERIC(sv,right)) {
2083 cx->cx_type &= ~CXTYPEMASK;
2084 cx->cx_type |= CXt_LOOP_LAZYIV;
2085 /* Make sure that no-one re-orders cop.h and breaks our
2087 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
2088 #ifdef NV_PRESERVES_UV
2089 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
2090 (SvNV(sv) > (NV)IV_MAX)))
2092 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
2093 (SvNV(right) < (NV)IV_MIN))))
2095 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
2098 ((SvUV(sv) > (UV)IV_MAX) ||
2099 (SvNV(sv) > (NV)UV_MAX)))))
2101 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
2103 ((SvNV(right) > 0) &&
2104 ((SvUV(right) > (UV)IV_MAX) ||
2105 (SvNV(right) > (NV)UV_MAX))))))
2107 DIE(aTHX_ "Range iterator outside integer range");
2108 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2109 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
2111 /* for correct -Dstv display */
2112 cx->blk_oldsp = sp - PL_stack_base;
2116 cx->cx_type &= ~CXTYPEMASK;
2117 cx->cx_type |= CXt_LOOP_LAZYSV;
2118 /* Make sure that no-one re-orders cop.h and breaks our
2120 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2121 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2122 cx->blk_loop.state_u.lazysv.end = right;
2123 SvREFCNT_inc(right);
2124 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2125 /* This will do the upgrade to SVt_PV, and warn if the value
2126 is uninitialised. */
2127 (void) SvPV_nolen_const(right);
2128 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2129 to replace !SvOK() with a pointer to "". */
2131 SvREFCNT_dec(right);
2132 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2136 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2137 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2138 SvREFCNT_inc(maybe_ary);
2139 cx->blk_loop.state_u.ary.ix =
2140 (PL_op->op_private & OPpITER_REVERSED) ?
2141 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2145 else { /* iterating over items on the stack */
2146 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2147 if (PL_op->op_private & OPpITER_REVERSED) {
2148 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2151 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2161 register PERL_CONTEXT *cx;
2162 const I32 gimme = GIMME_V;
2164 ENTER_with_name("loop1");
2166 ENTER_with_name("loop2");
2168 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2169 PUSHLOOP_PLAIN(cx, SP);
2177 register PERL_CONTEXT *cx;
2184 assert(CxTYPE_is_LOOP(cx));
2186 newsp = PL_stack_base + cx->blk_loop.resetsp;
2189 if (gimme == G_VOID)
2191 else if (gimme == G_SCALAR) {
2193 *++newsp = sv_mortalcopy(*SP);
2195 *++newsp = &PL_sv_undef;
2199 *++newsp = sv_mortalcopy(*++mark);
2200 TAINT_NOT; /* Each item is independent */
2206 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2207 PL_curpm = newpm; /* ... and pop $1 et al */
2209 LEAVE_with_name("loop2");
2210 LEAVE_with_name("loop1");
2218 register PERL_CONTEXT *cx;
2219 bool popsub2 = FALSE;
2220 bool clear_errsv = FALSE;
2230 const I32 cxix = dopoptosub(cxstack_ix);
2233 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2234 * sort block, which is a CXt_NULL
2237 PL_stack_base[1] = *PL_stack_sp;
2238 PL_stack_sp = PL_stack_base + 1;
2242 DIE(aTHX_ "Can't return outside a subroutine");
2244 if (cxix < cxstack_ix)
2247 if (CxMULTICALL(&cxstack[cxix])) {
2248 gimme = cxstack[cxix].blk_gimme;
2249 if (gimme == G_VOID)
2250 PL_stack_sp = PL_stack_base;
2251 else if (gimme == G_SCALAR) {
2252 PL_stack_base[1] = *PL_stack_sp;
2253 PL_stack_sp = PL_stack_base + 1;
2259 switch (CxTYPE(cx)) {
2262 lval = !!CvLVALUE(cx->blk_sub.cv);
2263 retop = cx->blk_sub.retop;
2264 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2267 if (!(PL_in_eval & EVAL_KEEPERR))
2270 namesv = cx->blk_eval.old_namesv;
2271 retop = cx->blk_eval.retop;
2274 if (optype == OP_REQUIRE &&
2275 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2277 /* Unassume the success we assumed earlier. */
2278 (void)hv_delete(GvHVn(PL_incgv),
2279 SvPVX_const(namesv), SvCUR(namesv),
2281 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
2286 retop = cx->blk_sub.retop;
2289 DIE(aTHX_ "panic: return");
2293 if (gimme == G_SCALAR) {
2296 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2298 *++newsp = SvREFCNT_inc(*SP);
2303 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2305 *++newsp = sv_mortalcopy(sv);
2311 (lval || SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
2314 *++newsp = sv_mortalcopy(*SP);
2317 *++newsp = &PL_sv_undef;
2319 else if (gimme == G_ARRAY) {
2320 while (++MARK <= SP) {
2321 *++newsp = popsub2 && (lval || SvTEMP(*MARK))
2322 ? *MARK : sv_mortalcopy(*MARK);
2323 TAINT_NOT; /* Each item is independent */
2326 PL_stack_sp = newsp;
2329 /* Stack values are safe: */
2332 POPSUB(cx,sv); /* release CV and @_ ... */
2336 PL_curpm = newpm; /* ... and pop $1 et al */
2349 register PERL_CONTEXT *cx;
2360 if (PL_op->op_flags & OPf_SPECIAL) {
2361 cxix = dopoptoloop(cxstack_ix);
2363 DIE(aTHX_ "Can't \"last\" outside a loop block");
2366 cxix = dopoptolabel(cPVOP->op_pv);
2368 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
2370 if (cxix < cxstack_ix)
2374 cxstack_ix++; /* temporarily protect top context */
2376 switch (CxTYPE(cx)) {
2377 case CXt_LOOP_LAZYIV:
2378 case CXt_LOOP_LAZYSV:
2380 case CXt_LOOP_PLAIN:
2382 newsp = PL_stack_base + cx->blk_loop.resetsp;
2383 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2387 nextop = cx->blk_sub.retop;
2391 nextop = cx->blk_eval.retop;
2395 nextop = cx->blk_sub.retop;
2398 DIE(aTHX_ "panic: last");
2402 if (gimme == G_SCALAR) {
2404 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2405 ? *SP : sv_mortalcopy(*SP);
2407 *++newsp = &PL_sv_undef;
2409 else if (gimme == G_ARRAY) {
2410 while (++MARK <= SP) {
2411 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2412 ? *MARK : sv_mortalcopy(*MARK);
2413 TAINT_NOT; /* Each item is independent */
2421 /* Stack values are safe: */
2423 case CXt_LOOP_LAZYIV:
2424 case CXt_LOOP_PLAIN:
2425 case CXt_LOOP_LAZYSV:
2427 POPLOOP(cx); /* release loop vars ... */
2431 POPSUB(cx,sv); /* release CV and @_ ... */
2434 PL_curpm = newpm; /* ... and pop $1 et al */
2437 PERL_UNUSED_VAR(optype);
2438 PERL_UNUSED_VAR(gimme);
2446 register PERL_CONTEXT *cx;
2449 if (PL_op->op_flags & OPf_SPECIAL) {
2450 cxix = dopoptoloop(cxstack_ix);
2452 DIE(aTHX_ "Can't \"next\" outside a loop block");
2455 cxix = dopoptolabel(cPVOP->op_pv);
2457 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2459 if (cxix < cxstack_ix)
2462 /* clear off anything above the scope we're re-entering, but
2463 * save the rest until after a possible continue block */
2464 inner = PL_scopestack_ix;
2466 if (PL_scopestack_ix < inner)
2467 leave_scope(PL_scopestack[PL_scopestack_ix]);
2468 PL_curcop = cx->blk_oldcop;
2469 return (cx)->blk_loop.my_op->op_nextop;
2476 register PERL_CONTEXT *cx;
2480 if (PL_op->op_flags & OPf_SPECIAL) {
2481 cxix = dopoptoloop(cxstack_ix);
2483 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2486 cxix = dopoptolabel(cPVOP->op_pv);
2488 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2490 if (cxix < cxstack_ix)
2493 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2494 if (redo_op->op_type == OP_ENTER) {
2495 /* pop one less context to avoid $x being freed in while (my $x..) */
2497 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2498 redo_op = redo_op->op_next;
2502 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2503 LEAVE_SCOPE(oldsave);
2505 PL_curcop = cx->blk_oldcop;
2510 S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
2514 static const char too_deep[] = "Target of goto is too deeply nested";
2516 PERL_ARGS_ASSERT_DOFINDLABEL;
2519 Perl_croak(aTHX_ too_deep);
2520 if (o->op_type == OP_LEAVE ||
2521 o->op_type == OP_SCOPE ||
2522 o->op_type == OP_LEAVELOOP ||
2523 o->op_type == OP_LEAVESUB ||
2524 o->op_type == OP_LEAVETRY)
2526 *ops++ = cUNOPo->op_first;
2528 Perl_croak(aTHX_ too_deep);
2531 if (o->op_flags & OPf_KIDS) {
2533 /* First try all the kids at this level, since that's likeliest. */
2534 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2535 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2536 const char *kid_label = CopLABEL(kCOP);
2537 if (kid_label && strEQ(kid_label, label))
2541 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2542 if (kid == PL_lastgotoprobe)
2544 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2547 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2548 ops[-1]->op_type == OP_DBSTATE)
2553 if ((o = dofindlabel(kid, label, ops, oplimit)))
2566 register PERL_CONTEXT *cx;
2567 #define GOTO_DEPTH 64
2568 OP *enterops[GOTO_DEPTH];
2569 const char *label = NULL;
2570 const bool do_dump = (PL_op->op_type == OP_DUMP);
2571 static const char must_have_label[] = "goto must have label";
2573 if (PL_op->op_flags & OPf_STACKED) {
2574 SV * const sv = POPs;
2576 /* This egregious kludge implements goto &subroutine */
2577 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2579 register PERL_CONTEXT *cx;
2580 CV *cv = MUTABLE_CV(SvRV(sv));
2587 if (!CvROOT(cv) && !CvXSUB(cv)) {
2588 const GV * const gv = CvGV(cv);
2592 /* autoloaded stub? */
2593 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2595 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2596 GvNAMELEN(gv), FALSE);
2597 if (autogv && (cv = GvCV(autogv)))
2599 tmpstr = sv_newmortal();
2600 gv_efullname3(tmpstr, gv, NULL);
2601 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2603 DIE(aTHX_ "Goto undefined subroutine");
2606 /* First do some returnish stuff. */
2607 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2609 cxix = dopoptosub(cxstack_ix);
2611 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2612 if (cxix < cxstack_ix)
2616 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2617 if (CxTYPE(cx) == CXt_EVAL) {
2619 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2621 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2623 else if (CxMULTICALL(cx))
2624 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2625 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2626 /* put @_ back onto stack */
2627 AV* av = cx->blk_sub.argarray;
2629 items = AvFILLp(av) + 1;
2630 EXTEND(SP, items+1); /* @_ could have been extended. */
2631 Copy(AvARRAY(av), SP + 1, items, SV*);
2632 SvREFCNT_dec(GvAV(PL_defgv));
2633 GvAV(PL_defgv) = cx->blk_sub.savearray;
2635 /* abandon @_ if it got reified */
2640 av_extend(av, items-1);
2642 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2645 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2646 AV* const av = GvAV(PL_defgv);
2647 items = AvFILLp(av) + 1;
2648 EXTEND(SP, items+1); /* @_ could have been extended. */
2649 Copy(AvARRAY(av), SP + 1, items, SV*);
2653 if (CxTYPE(cx) == CXt_SUB &&
2654 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2655 SvREFCNT_dec(cx->blk_sub.cv);
2656 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2657 LEAVE_SCOPE(oldsave);
2659 /* Now do some callish stuff. */
2661 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2663 OP* const retop = cx->blk_sub.retop;
2664 SV **newsp __attribute__unused__;
2665 I32 gimme __attribute__unused__;
2668 for (index=0; index<items; index++)
2669 sv_2mortal(SP[-index]);
2672 /* XS subs don't have a CxSUB, so pop it */
2673 POPBLOCK(cx, PL_curpm);
2674 /* Push a mark for the start of arglist */
2677 (void)(*CvXSUB(cv))(aTHX_ cv);
2682 AV* const padlist = CvPADLIST(cv);
2683 if (CxTYPE(cx) == CXt_EVAL) {
2684 PL_in_eval = CxOLD_IN_EVAL(cx);
2685 PL_eval_root = cx->blk_eval.old_eval_root;
2686 cx->cx_type = CXt_SUB;
2688 cx->blk_sub.cv = cv;
2689 cx->blk_sub.olddepth = CvDEPTH(cv);
2692 if (CvDEPTH(cv) < 2)
2693 SvREFCNT_inc_simple_void_NN(cv);
2695 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2696 sub_crush_depth(cv);
2697 pad_push(padlist, CvDEPTH(cv));
2700 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2703 AV *const av = MUTABLE_AV(PAD_SVl(0));
2705 cx->blk_sub.savearray = GvAV(PL_defgv);
2706 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
2707 CX_CURPAD_SAVE(cx->blk_sub);
2708 cx->blk_sub.argarray = av;
2710 if (items >= AvMAX(av) + 1) {
2711 SV **ary = AvALLOC(av);
2712 if (AvARRAY(av) != ary) {
2713 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2716 if (items >= AvMAX(av) + 1) {
2717 AvMAX(av) = items - 1;
2718 Renew(ary,items+1,SV*);
2724 Copy(mark,AvARRAY(av),items,SV*);
2725 AvFILLp(av) = items - 1;
2726 assert(!AvREAL(av));
2728 /* transfer 'ownership' of refcnts to new @_ */
2738 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2739 Perl_get_db_sub(aTHX_ NULL, cv);
2741 CV * const gotocv = get_cvs("DB::goto", 0);
2743 PUSHMARK( PL_stack_sp );
2744 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
2749 RETURNOP(CvSTART(cv));
2753 label = SvPV_nolen_const(sv);
2754 if (!(do_dump || *label))
2755 DIE(aTHX_ must_have_label);
2758 else if (PL_op->op_flags & OPf_SPECIAL) {
2760 DIE(aTHX_ must_have_label);
2763 label = cPVOP->op_pv;
2767 if (label && *label) {
2768 OP *gotoprobe = NULL;
2769 bool leaving_eval = FALSE;
2770 bool in_block = FALSE;
2771 PERL_CONTEXT *last_eval_cx = NULL;
2775 PL_lastgotoprobe = NULL;
2777 for (ix = cxstack_ix; ix >= 0; ix--) {
2779 switch (CxTYPE(cx)) {
2781 leaving_eval = TRUE;
2782 if (!CxTRYBLOCK(cx)) {
2783 gotoprobe = (last_eval_cx ?
2784 last_eval_cx->blk_eval.old_eval_root :
2789 /* else fall through */
2790 case CXt_LOOP_LAZYIV:
2791 case CXt_LOOP_LAZYSV:
2793 case CXt_LOOP_PLAIN:
2796 gotoprobe = cx->blk_oldcop->op_sibling;
2802 gotoprobe = cx->blk_oldcop->op_sibling;
2805 gotoprobe = PL_main_root;
2808 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
2809 gotoprobe = CvROOT(cx->blk_sub.cv);
2815 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2818 DIE(aTHX_ "panic: goto");
2819 gotoprobe = PL_main_root;
2823 retop = dofindlabel(gotoprobe, label,
2824 enterops, enterops + GOTO_DEPTH);
2827 if (gotoprobe->op_sibling &&
2828 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
2829 gotoprobe->op_sibling->op_sibling) {
2830 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
2831 label, enterops, enterops + GOTO_DEPTH);
2836 PL_lastgotoprobe = gotoprobe;
2839 DIE(aTHX_ "Can't find label %s", label);
2841 /* if we're leaving an eval, check before we pop any frames
2842 that we're not going to punt, otherwise the error
2845 if (leaving_eval && *enterops && enterops[1]) {
2847 for (i = 1; enterops[i]; i++)
2848 if (enterops[i]->op_type == OP_ENTERITER)
2849 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2852 if (*enterops && enterops[1]) {
2853 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2855 deprecate("\"goto\" to jump into a construct");
2858 /* pop unwanted frames */
2860 if (ix < cxstack_ix) {
2867 oldsave = PL_scopestack[PL_scopestack_ix];
2868 LEAVE_SCOPE(oldsave);
2871 /* push wanted frames */
2873 if (*enterops && enterops[1]) {
2874 OP * const oldop = PL_op;
2875 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2876 for (; enterops[ix]; ix++) {
2877 PL_op = enterops[ix];
2878 /* Eventually we may want to stack the needed arguments
2879 * for each op. For now, we punt on the hard ones. */
2880 if (PL_op->op_type == OP_ENTERITER)
2881 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2882 PL_op->op_ppaddr(aTHX);
2890 if (!retop) retop = PL_main_start;
2892 PL_restartop = retop;
2893 PL_do_undump = TRUE;
2897 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2898 PL_do_undump = FALSE;
2915 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2917 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
2920 PL_exit_flags |= PERL_EXIT_EXPECTED;
2922 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2923 if (anum || !(PL_minus_c && PL_madskills))
2928 PUSHs(&PL_sv_undef);
2935 S_save_lines(pTHX_ AV *array, SV *sv)
2937 const char *s = SvPVX_const(sv);
2938 const char * const send = SvPVX_const(sv) + SvCUR(sv);
2941 PERL_ARGS_ASSERT_SAVE_LINES;
2943 while (s && s < send) {
2945 SV * const tmpstr = newSV_type(SVt_PVMG);
2947 t = (const char *)memchr(s, '\n', send - s);
2953 sv_setpvn(tmpstr, s, t - s);
2954 av_store(array, line++, tmpstr);
2962 Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
2964 0 is used as continue inside eval,
2966 3 is used for a die caught by an inner eval - continue inner loop
2968 See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
2969 establish a local jmpenv to handle exception traps.
2974 S_docatch(pTHX_ OP *o)
2978 OP * const oldop = PL_op;
2982 assert(CATCH_GET == TRUE);
2989 assert(cxstack_ix >= 0);
2990 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2991 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
2996 /* die caught by an inner eval - continue inner loop */
2997 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
2998 PL_restartjmpenv = NULL;
2999 PL_op = PL_restartop;
3015 /* James Bond: Do you expect me to talk?
3016 Auric Goldfinger: No, Mr. Bond. I expect you to die.
3018 This code is an ugly hack, doesn't work with lexicals in subroutines that are
3019 called more than once, and is only used by regcomp.c, for (?{}) blocks.
3021 Currently it is not used outside the core code. Best if it stays that way.
3023 Hence it's now deprecated, and will be removed.
3026 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
3027 /* sv Text to convert to OP tree. */
3028 /* startop op_free() this to undo. */
3029 /* code Short string id of the caller. */
3031 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3032 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
3035 /* Don't use this. It will go away without warning once the regexp engine is
3036 refactored not to use it. */
3038 Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3041 dVAR; dSP; /* Make POPBLOCK work. */
3047 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3048 char *tmpbuf = tbuf;
3051 CV* runcv = NULL; /* initialise to avoid compiler warnings */
3055 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
3057 ENTER_with_name("eval");
3058 lex_start(sv, NULL, LEX_START_SAME_FILTER);
3060 /* switch to eval mode */
3062 if (IN_PERL_COMPILETIME) {
3063 SAVECOPSTASH_FREE(&PL_compiling);
3064 CopSTASH_set(&PL_compiling, PL_curstash);
3066 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3067 SV * const sv = sv_newmortal();
3068 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3069 code, (unsigned long)++PL_evalseq,
3070 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3075 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3076 (unsigned long)++PL_evalseq);
3077 SAVECOPFILE_FREE(&PL_compiling);
3078 CopFILE_set(&PL_compiling, tmpbuf+2);
3079 SAVECOPLINE(&PL_compiling);
3080 CopLINE_set(&PL_compiling, 1);
3081 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3082 deleting the eval's FILEGV from the stash before gv_check() runs
3083 (i.e. before run-time proper). To work around the coredump that
3084 ensues, we always turn GvMULTI_on for any globals that were
3085 introduced within evals. See force_ident(). GSAR 96-10-12 */
3086 safestr = savepvn(tmpbuf, len);
3087 SAVEDELETE(PL_defstash, safestr, len);
3089 #ifdef OP_IN_REGISTER
3095 /* we get here either during compilation, or via pp_regcomp at runtime */
3096 runtime = IN_PERL_RUNTIME;
3099 runcv = find_runcv(NULL);
3101 /* At run time, we have to fetch the hints from PL_curcop. */
3102 PL_hints = PL_curcop->cop_hints;
3103 if (PL_hints & HINT_LOCALIZE_HH) {
3104 /* SAVEHINTS created a new HV in PL_hintgv, which we
3106 SvREFCNT_dec(GvHV(PL_hintgv));
3108 refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0);
3109 hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints);
3111 SAVECOMPILEWARNINGS();
3112 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3113 cophh_free(CopHINTHASH_get(&PL_compiling));
3114 /* XXX Does this need to avoid copying a label? */
3115 PL_compiling.cop_hints_hash
3116 = cophh_copy(PL_curcop->cop_hints_hash);
3120 PL_op->op_type = OP_ENTEREVAL;
3121 PL_op->op_flags = 0; /* Avoid uninit warning. */
3122 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
3124 need_catch = CATCH_GET;
3128 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
3130 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
3131 CATCH_SET(need_catch);
3132 POPBLOCK(cx,PL_curpm);
3135 (*startop)->op_type = OP_NULL;
3136 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
3137 /* XXX DAPM do this properly one year */
3138 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
3139 LEAVE_with_name("eval");
3140 if (IN_PERL_COMPILETIME)
3141 CopHINTS_set(&PL_compiling, PL_hints);
3142 #ifdef OP_IN_REGISTER
3145 PERL_UNUSED_VAR(newsp);
3146 PERL_UNUSED_VAR(optype);
3148 return PL_eval_start;
3153 =for apidoc find_runcv
3155 Locate the CV corresponding to the currently executing sub or eval.
3156 If db_seqp is non_null, skip CVs that are in the DB package and populate
3157 *db_seqp with the cop sequence number at the point that the DB:: code was
3158 entered. (allows debuggers to eval in the scope of the breakpoint rather
3159 than in the scope of the debugger itself).
3165 Perl_find_runcv(pTHX_ U32 *db_seqp)
3171 *db_seqp = PL_curcop->cop_seq;
3172 for (si = PL_curstackinfo; si; si = si->si_prev) {
3174 for (ix = si->si_cxix; ix >= 0; ix--) {
3175 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3176 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3177 CV * const cv = cx->blk_sub.cv;
3178 /* skip DB:: code */
3179 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3180 *db_seqp = cx->blk_oldcop->cop_seq;
3185 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3193 /* Run yyparse() in a setjmp wrapper. Returns:
3194 * 0: yyparse() successful
3195 * 1: yyparse() failed
3199 S_try_yyparse(pTHX_ int gramtype)
3204 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3208 ret = yyparse(gramtype) ? 1 : 0;
3222 /* Compile a require/do, an eval '', or a /(?{...})/.
3223 * In the last case, startop is non-null, and contains the address of
3224 * a pointer that should be set to the just-compiled code.
3225 * outside is the lexically enclosing CV (if any) that invoked us.
3226 * Returns a bool indicating whether the compile was successful; if so,
3227 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3228 * pushes undef (also croaks if startop != NULL).
3232 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
3235 OP * const saveop = PL_op;
3236 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3239 PL_in_eval = (in_require
3240 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3245 SAVESPTR(PL_compcv);
3246 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3247 CvEVAL_on(PL_compcv);
3248 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3249 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3251 CvOUTSIDE_SEQ(PL_compcv) = seq;
3252 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3254 /* set up a scratch pad */
3256 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
3257 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3261 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
3263 /* make sure we compile in the right package */
3265 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3266 SAVESPTR(PL_curstash);
3267 PL_curstash = CopSTASH(PL_curcop);
3269 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3270 SAVESPTR(PL_beginav);
3271 PL_beginav = newAV();
3272 SAVEFREESV(PL_beginav);
3273 SAVESPTR(PL_unitcheckav);
3274 PL_unitcheckav = newAV();
3275 SAVEFREESV(PL_unitcheckav);
3278 SAVEBOOL(PL_madskills);
3282 /* try to compile it */
3284 PL_eval_root = NULL;
3285 PL_curcop = &PL_compiling;
3286 CopARYBASE_set(PL_curcop, 0);
3287 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3288 PL_in_eval |= EVAL_KEEPERR;
3292 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3294 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3295 * so honour CATCH_GET and trap it here if necessary */
3297 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3299 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3300 SV **newsp; /* Used by POPBLOCK. */
3301 PERL_CONTEXT *cx = NULL;
3302 I32 optype; /* Used by POPEVAL. */
3306 PERL_UNUSED_VAR(newsp);
3307 PERL_UNUSED_VAR(optype);
3309 /* note that if yystatus == 3, then the EVAL CX block has already
3310 * been popped, and various vars restored */
3312 if (yystatus != 3) {
3314 op_free(PL_eval_root);
3315 PL_eval_root = NULL;
3317 SP = PL_stack_base + POPMARK; /* pop original mark */
3319 POPBLOCK(cx,PL_curpm);
3321 namesv = cx->blk_eval.old_namesv;
3325 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3327 msg = SvPVx_nolen_const(ERRSV);
3330 /* If cx is still NULL, it means that we didn't go in the
3331 * POPEVAL branch. */
3332 cx = &cxstack[cxstack_ix];
3333 assert(CxTYPE(cx) == CXt_EVAL);
3334 namesv = cx->blk_eval.old_namesv;
3336 (void)hv_store(GvHVn(PL_incgv),
3337 SvPVX_const(namesv), SvCUR(namesv),
3339 Perl_croak(aTHX_ "%sCompilation failed in require",
3340 *msg ? msg : "Unknown error\n");
3343 if (yystatus != 3) {
3344 POPBLOCK(cx,PL_curpm);
3347 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3348 (*msg ? msg : "Unknown error\n"));
3352 sv_setpvs(ERRSV, "Compilation error");
3355 PUSHs(&PL_sv_undef);
3359 CopLINE_set(&PL_compiling, 0);
3361 *startop = PL_eval_root;
3363 SAVEFREEOP(PL_eval_root);
3365 /* Set the context for this new optree.
3366 * Propagate the context from the eval(). */
3367 if ((gimme & G_WANT) == G_VOID)
3368 scalarvoid(PL_eval_root);
3369 else if ((gimme & G_WANT) == G_ARRAY)
3372 scalar(PL_eval_root);
3374 DEBUG_x(dump_eval());
3376 /* Register with debugger: */
3377 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3378 CV * const cv = get_cvs("DB::postponed", 0);
3382 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3384 call_sv(MUTABLE_SV(cv), G_DISCARD);
3388 if (PL_unitcheckav) {
3389 OP *es = PL_eval_start;
3390 call_list(PL_scopestack_ix, PL_unitcheckav);
3394 /* compiled okay, so do it */
3396 CvDEPTH(PL_compcv) = 1;
3397 SP = PL_stack_base + POPMARK; /* pop original mark */
3398 PL_op = saveop; /* The caller may need it. */
3399 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3406 S_check_type_and_open(pTHX_ SV *name)
3409 const char *p = SvPV_nolen_const(name);
3410 const int st_rc = PerlLIO_stat(p, &st);
3412 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3414 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3418 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3419 return PerlIO_openn(aTHX_ NULL, PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3421 return PerlIO_open(p, PERL_SCRIPT_MODE);
3425 #ifndef PERL_DISABLE_PMC
3427 S_doopen_pm(pTHX_ SV *name)
3430 const char *p = SvPV_const(name, namelen);
3432 PERL_ARGS_ASSERT_DOOPEN_PM;
3434 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3435 SV *const pmcsv = sv_newmortal();
3438 SvSetSV_nosteal(pmcsv,name);
3439 sv_catpvn(pmcsv, "c", 1);
3441 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3442 return check_type_and_open(pmcsv);
3444 return check_type_and_open(name);
3447 # define doopen_pm(name) check_type_and_open(name)
3448 #endif /* !PERL_DISABLE_PMC */
3453 register PERL_CONTEXT *cx;
3460 int vms_unixname = 0;
3462 const char *tryname = NULL;
3464 const I32 gimme = GIMME_V;
3465 int filter_has_file = 0;
3466 PerlIO *tryrsfp = NULL;
3467 SV *filter_cache = NULL;
3468 SV *filter_state = NULL;
3469 SV *filter_sub = NULL;
3475 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3476 sv = sv_2mortal(new_version(sv));
3477 if (!sv_derived_from(PL_patchlevel, "version"))
3478 upg_version(PL_patchlevel, TRUE);
3479 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3480 if ( vcmp(sv,PL_patchlevel) <= 0 )
3481 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3482 SVfARG(sv_2mortal(vnormal(sv))),
3483 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3487 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3490 SV * const req = SvRV(sv);
3491 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3493 /* get the left hand term */
3494 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3496 first = SvIV(*av_fetch(lav,0,0));
3497 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3498 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3499 || av_len(lav) > 1 /* FP with > 3 digits */
3500 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3502 DIE(aTHX_ "Perl %"SVf" required--this is only "
3504 SVfARG(sv_2mortal(vnormal(req))),
3505 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3508 else { /* probably 'use 5.10' or 'use 5.8' */
3513 second = SvIV(*av_fetch(lav,1,0));
3515 second /= second >= 600 ? 100 : 10;
3516 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3517 (int)first, (int)second);
3518 upg_version(hintsv, TRUE);
3520 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3521 "--this is only %"SVf", stopped",
3522 SVfARG(sv_2mortal(vnormal(req))),
3523 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3524 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3532 name = SvPV_const(sv, len);
3533 if (!(name && len > 0 && *name))
3534 DIE(aTHX_ "Null filename used");
3535 TAINT_PROPER("require");
3539 /* The key in the %ENV hash is in the syntax of file passed as the argument
3540 * usually this is in UNIX format, but sometimes in VMS format, which
3541 * can result in a module being pulled in more than once.
3542 * To prevent this, the key must be stored in UNIX format if the VMS
3543 * name can be translated to UNIX.
3545 if ((unixname = tounixspec(name, NULL)) != NULL) {
3546 unixlen = strlen(unixname);
3552 /* if not VMS or VMS name can not be translated to UNIX, pass it
3555 unixname = (char *) name;
3558 if (PL_op->op_type == OP_REQUIRE) {
3559 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3560 unixname, unixlen, 0);
3562 if (*svp != &PL_sv_undef)
3565 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3566 "Compilation failed in require", unixname);
3570 /* prepare to compile file */
3572 if (path_is_absolute(name)) {
3573 /* At this point, name is SvPVX(sv) */
3575 tryrsfp = doopen_pm(sv);
3578 AV * const ar = GvAVn(PL_incgv);
3584 namesv = newSV_type(SVt_PV);
3585 for (i = 0; i <= AvFILL(ar); i++) {
3586 SV * const dirsv = *av_fetch(ar, i, TRUE);
3588 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3595 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3596 && !sv_isobject(loader))
3598 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3601 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3602 PTR2UV(SvRV(dirsv)), name);
3603 tryname = SvPVX_const(namesv);
3606 ENTER_with_name("call_INC");
3614 if (sv_isobject(loader))
3615 count = call_method("INC", G_ARRAY);
3617 count = call_sv(loader, G_ARRAY);
3627 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3628 && !isGV_with_GP(SvRV(arg))) {
3629 filter_cache = SvRV(arg);
3630 SvREFCNT_inc_simple_void_NN(filter_cache);
3637 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
3641 if (isGV_with_GP(arg)) {
3642 IO * const io = GvIO((const GV *)arg);
3647 tryrsfp = IoIFP(io);
3648 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3649 PerlIO_close(IoOFP(io));
3660 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3662 SvREFCNT_inc_simple_void_NN(filter_sub);
3665 filter_state = SP[i];
3666 SvREFCNT_inc_simple_void(filter_state);
3670 if (!tryrsfp && (filter_cache || filter_sub)) {
3671 tryrsfp = PerlIO_open(BIT_BUCKET,
3679 LEAVE_with_name("call_INC");
3681 /* Adjust file name if the hook has set an %INC entry.
3682 This needs to happen after the FREETMPS above. */
3683 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3685 tryname = SvPV_nolen_const(*svp);
3692 filter_has_file = 0;
3694 SvREFCNT_dec(filter_cache);
3695 filter_cache = NULL;
3698 SvREFCNT_dec(filter_state);
3699 filter_state = NULL;
3702 SvREFCNT_dec(filter_sub);
3707 if (!path_is_absolute(name)
3713 dir = SvPV_const(dirsv, dirlen);
3721 if ((unixdir = tounixpath(dir, NULL)) == NULL)
3723 sv_setpv(namesv, unixdir);
3724 sv_catpv(namesv, unixname);
3726 # ifdef __SYMBIAN32__
3727 if (PL_origfilename[0] &&
3728 PL_origfilename[1] == ':' &&
3729 !(dir[0] && dir[1] == ':'))
3730 Perl_sv_setpvf(aTHX_ namesv,
3735 Perl_sv_setpvf(aTHX_ namesv,
3739 /* The equivalent of
3740 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3741 but without the need to parse the format string, or
3742 call strlen on either pointer, and with the correct
3743 allocation up front. */
3745 char *tmp = SvGROW(namesv, dirlen + len + 2);
3747 memcpy(tmp, dir, dirlen);
3750 /* name came from an SV, so it will have a '\0' at the
3751 end that we can copy as part of this memcpy(). */
3752 memcpy(tmp, name, len + 1);
3754 SvCUR_set(namesv, dirlen + len + 1);
3759 TAINT_PROPER("require");
3760 tryname = SvPVX_const(namesv);
3761 tryrsfp = doopen_pm(namesv);
3763 if (tryname[0] == '.' && tryname[1] == '/') {
3765 while (*++tryname == '/');
3769 else if (errno == EMFILE)
3770 /* no point in trying other paths if out of handles */
3779 if (PL_op->op_type == OP_REQUIRE) {
3780 if(errno == EMFILE) {
3781 /* diag_listed_as: Can't locate %s */
3782 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno));
3784 if (namesv) { /* did we lookup @INC? */
3785 AV * const ar = GvAVn(PL_incgv);
3787 SV *const inc = newSVpvs_flags("", SVs_TEMP);
3788 for (i = 0; i <= AvFILL(ar); i++) {
3789 sv_catpvs(inc, " ");
3790 sv_catsv(inc, *av_fetch(ar, i, TRUE));
3793 /* diag_listed_as: Can't locate %s */
3795 "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")",
3797 (memEQ(name + len - 2, ".h", 3)
3798 ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""),
3799 (memEQ(name + len - 3, ".ph", 4)
3800 ? " (did you run h2ph?)" : ""),
3805 DIE(aTHX_ "Can't locate %s", name);
3811 SETERRNO(0, SS_NORMAL);
3813 /* Assume success here to prevent recursive requirement. */
3814 /* name is never assigned to again, so len is still strlen(name) */
3815 /* Check whether a hook in @INC has already filled %INC */
3817 (void)hv_store(GvHVn(PL_incgv),
3818 unixname, unixlen, newSVpv(tryname,0),0);
3820 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
3822 (void)hv_store(GvHVn(PL_incgv),
3823 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
3826 ENTER_with_name("eval");
3828 SAVECOPFILE_FREE(&PL_compiling);
3829 CopFILE_set(&PL_compiling, tryname);
3830 lex_start(NULL, tryrsfp, 0);
3834 hv_clear(GvHV(PL_hintgv));
3836 SAVECOMPILEWARNINGS();
3837 if (PL_dowarn & G_WARN_ALL_ON)
3838 PL_compiling.cop_warnings = pWARN_ALL ;
3839 else if (PL_dowarn & G_WARN_ALL_OFF)
3840 PL_compiling.cop_warnings = pWARN_NONE ;
3842 PL_compiling.cop_warnings = pWARN_STD ;
3844 if (filter_sub || filter_cache) {
3845 /* We can use the SvPV of the filter PVIO itself as our cache, rather
3846 than hanging another SV from it. In turn, filter_add() optionally
3847 takes the SV to use as the filter (or creates a new SV if passed
3848 NULL), so simply pass in whatever value filter_cache has. */
3849 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
3850 IoLINES(datasv) = filter_has_file;
3851 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
3852 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
3855 /* switch to eval mode */
3856 PUSHBLOCK(cx, CXt_EVAL, SP);
3858 cx->blk_eval.retop = PL_op->op_next;
3860 SAVECOPLINE(&PL_compiling);
3861 CopLINE_set(&PL_compiling, 0);
3865 /* Store and reset encoding. */
3866 encoding = PL_encoding;
3869 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq))
3870 op = DOCATCH(PL_eval_start);
3872 op = PL_op->op_next;
3874 /* Restore encoding. */
3875 PL_encoding = encoding;
3880 /* This is a op added to hold the hints hash for
3881 pp_entereval. The hash can be modified by the code
3882 being eval'ed, so we return a copy instead. */
3888 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
3896 register PERL_CONTEXT *cx;
3898 const I32 gimme = GIMME_V;
3899 const U32 was = PL_breakable_sub_gen;
3900 char tbuf[TYPE_DIGITS(long) + 12];
3901 bool saved_delete = FALSE;
3902 char *tmpbuf = tbuf;
3906 HV *saved_hh = NULL;
3908 if (PL_op->op_private & OPpEVAL_HAS_HH) {
3909 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
3913 /* make sure we've got a plain PV (no overload etc) before testing
3914 * for taint. Making a copy here is probably overkill, but better
3915 * safe than sorry */
3917 const char * const p = SvPV_const(sv, len);
3919 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
3922 TAINT_IF(SvTAINTED(sv));
3923 TAINT_PROPER("eval");
3925 ENTER_with_name("eval");
3926 lex_start(sv, NULL, LEX_START_SAME_FILTER);
3929 /* switch to eval mode */
3931 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3932 SV * const temp_sv = sv_newmortal();
3933 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
3934 (unsigned long)++PL_evalseq,
3935 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3936 tmpbuf = SvPVX(temp_sv);
3937 len = SvCUR(temp_sv);
3940 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
3941 SAVECOPFILE_FREE(&PL_compiling);
3942 CopFILE_set(&PL_compiling, tmpbuf+2);
3943 SAVECOPLINE(&PL_compiling);
3944 CopLINE_set(&PL_compiling, 1);
3945 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3946 deleting the eval's FILEGV from the stash before gv_check() runs
3947 (i.e. before run-time proper). To work around the coredump that
3948 ensues, we always turn GvMULTI_on for any globals that were
3949 introduced within evals. See force_ident(). GSAR 96-10-12 */
3951 PL_hints = PL_op->op_targ;
3953 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3954 SvREFCNT_dec(GvHV(PL_hintgv));
3955 GvHV(PL_hintgv) = saved_hh;
3957 SAVECOMPILEWARNINGS();
3958 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3959 cophh_free(CopHINTHASH_get(&PL_compiling));
3960 if (Perl_fetch_cop_label(aTHX_ PL_curcop, NULL, NULL)) {
3961 /* The label, if present, is the first entry on the chain. So rather
3962 than writing a blank label in front of it (which involves an
3963 allocation), just use the next entry in the chain. */
3964 PL_compiling.cop_hints_hash
3965 = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next);
3966 /* Check the assumption that this removed the label. */
3967 assert(Perl_fetch_cop_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3970 PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash);
3971 /* special case: an eval '' executed within the DB package gets lexically
3972 * placed in the first non-DB CV rather than the current CV - this
3973 * allows the debugger to execute code, find lexicals etc, in the
3974 * scope of the code being debugged. Passing &seq gets find_runcv
3975 * to do the dirty work for us */
3976 runcv = find_runcv(&seq);
3978 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3980 cx->blk_eval.retop = PL_op->op_next;
3982 /* prepare to compile string */
3984 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
3985 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
3987 char *const safestr = savepvn(tmpbuf, len);
3988 SAVEDELETE(PL_defstash, safestr, len);
3989 saved_delete = TRUE;
3994 if (doeval(gimme, NULL, runcv, seq)) {
3995 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3996 ? (PERLDB_LINE || PERLDB_SAVESRC)
3997 : PERLDB_SAVESRC_NOSUBS) {
3998 /* Retain the filegv we created. */
3999 } else if (!saved_delete) {
4000 char *const safestr = savepvn(tmpbuf, len);
4001 SAVEDELETE(PL_defstash, safestr, len);
4003 return DOCATCH(PL_eval_start);
4005 /* We have already left the scope set up earlier thanks to the LEAVE
4007 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4008 ? (PERLDB_LINE || PERLDB_SAVESRC)
4009 : PERLDB_SAVESRC_INVALID) {
4010 /* Retain the filegv we created. */
4011 } else if (!saved_delete) {
4012 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
4014 return PL_op->op_next;
4025 register PERL_CONTEXT *cx;
4027 const U8 save_flags = PL_op -> op_flags;
4034 namesv = cx->blk_eval.old_namesv;
4035 retop = cx->blk_eval.retop;
4038 if (gimme == G_VOID)
4040 else if (gimme == G_SCALAR) {
4043 if (SvFLAGS(TOPs) & SVs_TEMP)
4046 *MARK = sv_mortalcopy(TOPs);
4050 *MARK = &PL_sv_undef;
4055 /* in case LEAVE wipes old return values */
4056 for (mark = newsp + 1; mark <= SP; mark++) {
4057 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
4058 *mark = sv_mortalcopy(*mark);
4059 TAINT_NOT; /* Each item is independent */
4063 PL_curpm = newpm; /* Don't pop $1 et al till now */
4066 assert(CvDEPTH(PL_compcv) == 1);
4068 CvDEPTH(PL_compcv) = 0;
4070 if (optype == OP_REQUIRE &&
4071 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4073 /* Unassume the success we assumed earlier. */
4074 (void)hv_delete(GvHVn(PL_incgv),
4075 SvPVX_const(namesv), SvCUR(namesv),
4077 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4079 /* die_unwind() did LEAVE, or we won't be here */
4082 LEAVE_with_name("eval");
4083 if (!(save_flags & OPf_SPECIAL)) {
4091 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4092 close to the related Perl_create_eval_scope. */
4094 Perl_delete_eval_scope(pTHX)
4099 register PERL_CONTEXT *cx;
4105 LEAVE_with_name("eval_scope");
4106 PERL_UNUSED_VAR(newsp);
4107 PERL_UNUSED_VAR(gimme);
4108 PERL_UNUSED_VAR(optype);
4111 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4112 also needed by Perl_fold_constants. */
4114 Perl_create_eval_scope(pTHX_ U32 flags)
4117 const I32 gimme = GIMME_V;
4119 ENTER_with_name("eval_scope");
4122 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4125 PL_in_eval = EVAL_INEVAL;
4126 if (flags & G_KEEPERR)
4127 PL_in_eval |= EVAL_KEEPERR;
4130 if (flags & G_FAKINGEVAL) {
4131 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4139 PERL_CONTEXT * const cx = create_eval_scope(0);
4140 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4141 return DOCATCH(PL_op->op_next);
4150 register PERL_CONTEXT *cx;
4156 PERL_UNUSED_VAR(optype);
4159 if (gimme == G_VOID)
4161 else if (gimme == G_SCALAR) {
4165 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4168 *MARK = sv_mortalcopy(TOPs);
4172 *MARK = &PL_sv_undef;
4177 /* in case LEAVE wipes old return values */
4179 for (mark = newsp + 1; mark <= SP; mark++) {
4180 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4181 *mark = sv_mortalcopy(*mark);
4182 TAINT_NOT; /* Each item is independent */
4186 PL_curpm = newpm; /* Don't pop $1 et al till now */
4188 LEAVE_with_name("eval_scope");
4196 register PERL_CONTEXT *cx;
4197 const I32 gimme = GIMME_V;
4199 ENTER_with_name("given");
4202 sv_setsv_mg(PAD_SV(PL_op->op_targ), POPs);
4204 PUSHBLOCK(cx, CXt_GIVEN, SP);
4213 register PERL_CONTEXT *cx;
4217 PERL_UNUSED_CONTEXT;
4220 assert(CxTYPE(cx) == CXt_GIVEN);
4223 if (gimme == G_VOID)
4225 else if (gimme == G_SCALAR) {
4229 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4232 *MARK = sv_mortalcopy(TOPs);
4236 *MARK = &PL_sv_undef;
4241 /* in case LEAVE wipes old return values */
4243 for (mark = newsp + 1; mark <= SP; mark++) {
4244 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4245 *mark = sv_mortalcopy(*mark);
4246 TAINT_NOT; /* Each item is independent */
4250 PL_curpm = newpm; /* Don't pop $1 et al till now */
4252 LEAVE_with_name("given");
4256 /* Helper routines used by pp_smartmatch */
4258 S_make_matcher(pTHX_ REGEXP *re)
4261 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4263 PERL_ARGS_ASSERT_MAKE_MATCHER;
4265 PM_SETRE(matcher, ReREFCNT_inc(re));
4267 SAVEFREEOP((OP *) matcher);
4268 ENTER_with_name("matcher"); SAVETMPS;
4274 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4279 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4281 PL_op = (OP *) matcher;
4284 (void) Perl_pp_match(aTHX);
4286 return (SvTRUEx(POPs));
4290 S_destroy_matcher(pTHX_ PMOP *matcher)
4294 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4295 PERL_UNUSED_ARG(matcher);
4298 LEAVE_with_name("matcher");
4301 /* Do a smart match */
4304 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4305 return do_smartmatch(NULL, NULL);
4308 /* This version of do_smartmatch() implements the
4309 * table of smart matches that is found in perlsyn.
4312 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
4317 bool object_on_left = FALSE;
4318 SV *e = TOPs; /* e is for 'expression' */
4319 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4321 /* Take care only to invoke mg_get() once for each argument.
4322 * Currently we do this by copying the SV if it's magical. */
4325 d = sv_mortalcopy(d);
4332 e = sv_mortalcopy(e);
4334 /* First of all, handle overload magic of the rightmost argument */
4337 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4338 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4340 tmpsv = amagic_call(d, e, smart_amg, 0);
4347 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4350 SP -= 2; /* Pop the values */
4355 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4362 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4363 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4364 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4366 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4367 object_on_left = TRUE;
4370 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4372 if (object_on_left) {
4373 goto sm_any_sub; /* Treat objects like scalars */
4375 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4376 /* Test sub truth for each key */
4378 bool andedresults = TRUE;
4379 HV *hv = (HV*) SvRV(d);
4380 I32 numkeys = hv_iterinit(hv);
4381 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4384 while ( (he = hv_iternext(hv)) ) {
4385 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4386 ENTER_with_name("smartmatch_hash_key_test");
4389 PUSHs(hv_iterkeysv(he));
4391 c = call_sv(e, G_SCALAR);
4394 andedresults = FALSE;
4396 andedresults = SvTRUEx(POPs) && andedresults;
4398 LEAVE_with_name("smartmatch_hash_key_test");
4405 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4406 /* Test sub truth for each element */
4408 bool andedresults = TRUE;
4409 AV *av = (AV*) SvRV(d);
4410 const I32 len = av_len(av);
4411 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4414 for (i = 0; i <= len; ++i) {
4415 SV * const * const svp = av_fetch(av, i, FALSE);
4416 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4417 ENTER_with_name("smartmatch_array_elem_test");
4423 c = call_sv(e, G_SCALAR);
4426 andedresults = FALSE;
4428 andedresults = SvTRUEx(POPs) && andedresults;
4430 LEAVE_with_name("smartmatch_array_elem_test");
4439 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4440 ENTER_with_name("smartmatch_coderef");
4445 c = call_sv(e, G_SCALAR);
4449 else if (SvTEMP(TOPs))
4450 SvREFCNT_inc_void(TOPs);
4452 LEAVE_with_name("smartmatch_coderef");
4457 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4458 if (object_on_left) {
4459 goto sm_any_hash; /* Treat objects like scalars */
4461 else if (!SvOK(d)) {
4462 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4465 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4466 /* Check that the key-sets are identical */
4468 HV *other_hv = MUTABLE_HV(SvRV(d));
4470 bool other_tied = FALSE;
4471 U32 this_key_count = 0,
4472 other_key_count = 0;
4473 HV *hv = MUTABLE_HV(SvRV(e));
4475 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
4476 /* Tied hashes don't know how many keys they have. */
4477 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
4480 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
4481 HV * const temp = other_hv;
4486 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
4489 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
4492 /* The hashes have the same number of keys, so it suffices
4493 to check that one is a subset of the other. */
4494 (void) hv_iterinit(hv);
4495 while ( (he = hv_iternext(hv)) ) {
4496 SV *key = hv_iterkeysv(he);
4498 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
4501 if(!hv_exists_ent(other_hv, key, 0)) {
4502 (void) hv_iterinit(hv); /* reset iterator */
4508 (void) hv_iterinit(other_hv);
4509 while ( hv_iternext(other_hv) )
4513 other_key_count = HvUSEDKEYS(other_hv);
4515 if (this_key_count != other_key_count)