3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * Now far ahead the Road has gone,
13 * And I must follow, if I can,
14 * Pursuing it with eager feet,
15 * Until it joins some larger way
16 * Where many paths and errands meet.
17 * And whither then? I cannot say.
19 * [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains control-oriented pp ("push/pop") functions that
23 * execute the opcodes that make up a perl program. A typical pp function
24 * expects to find its arguments on the stack, and usually pushes its
25 * results onto the stack, hence the 'pp' terminology. Each OP structure
26 * contains a pointer to the relevant pp_foo() function.
28 * Control-oriented means things like pp_enteriter() and pp_next(), which
29 * alter the flow of control of the program.
34 #define PERL_IN_PP_CTL_C
37 #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
39 #define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
46 const PERL_CONTEXT *cx;
49 if (PL_op->op_private & OPpOFFBYONE) {
50 if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF;
53 cxix = dopoptosub(cxstack_ix);
59 switch (cx->blk_gimme) {
72 /* XXXX Should store the old value to allow for tie/overload - and
73 restore in regcomp, where marked with XXXX. */
83 register PMOP *pm = (PMOP*)cLOGOP->op_other;
87 /* prevent recompiling under /o and ithreads. */
88 #if defined(USE_ITHREADS)
89 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
90 if (PL_op->op_flags & OPf_STACKED) {
100 #define tryAMAGICregexp(rx) \
103 if (SvROK(rx) && SvAMAGIC(rx)) { \
104 SV *sv = AMG_CALLunary(rx, regexp_amg); \
108 if (SvTYPE(sv) != SVt_REGEXP) \
109 Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \
116 if (PL_op->op_flags & OPf_STACKED) {
117 /* multiple args; concatenate them */
119 tmpstr = PAD_SV(ARGTARG);
120 sv_setpvs(tmpstr, "");
121 while (++MARK <= SP) {
125 tryAMAGICregexp(msv);
127 if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) &&
128 (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign)))
130 sv_setsv(tmpstr, sv);
134 if (SvROK(msv) && SvTYPE(SvRV(msv)) == SVt_REGEXP) {
137 RX_SEEN_EVALS((REGEXP *)MUTABLE_PTR(msv));
140 sv_catsv_nomg(tmpstr, msv);
147 tryAMAGICregexp(tmpstr);
150 #undef tryAMAGICregexp
153 SV * const sv = SvRV(tmpstr);
154 if (SvTYPE(sv) == SVt_REGEXP)
157 else if (SvTYPE(tmpstr) == SVt_REGEXP)
158 re = (REGEXP*) tmpstr;
161 /* The match's LHS's get-magic might need to access this op's reg-
162 exp (as is sometimes the case with $'; see bug 70764). So we
163 must call get-magic now before we replace the regexp. Hopeful-
164 ly this hack can be replaced with the approach described at
165 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03
166 /msg122415.html some day. */
167 if(pm->op_type == OP_MATCH) {
169 const bool was_tainted = PL_tainted;
170 if (pm->op_flags & OPf_STACKED)
172 else if (pm->op_private & OPpTARGET_MY)
173 lhs = PAD_SV(pm->op_targ);
176 /* Restore the previous value of PL_tainted (which may have been
177 modified by get-magic), to avoid incorrectly setting the
178 RXf_TAINTED flag further down. */
179 PL_tainted = was_tainted;
182 re = reg_temp_copy(NULL, re);
183 ReREFCNT_dec(PM_GETRE(pm));
188 const char *t = SvOK(tmpstr) ? SvPV_nomg_const(tmpstr, len) : "";
191 assert (re != (REGEXP*) &PL_sv_undef);
193 /* Check against the last compiled regexp. */
194 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
195 memNE(RX_PRECOMP(re), t, len))
197 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
198 U32 pm_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
202 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
204 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
206 } else if (PL_curcop->cop_hints_hash) {
207 SV *ptr = cop_hints_fetch_pvs(PL_curcop, "regcomp", 0);
208 if (ptr && SvIOK(ptr) && SvIV(ptr))
209 eng = INT2PTR(regexp_engine*,SvIV(ptr));
212 if (PL_op->op_flags & OPf_SPECIAL)
213 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
215 if (!DO_UTF8(tmpstr) && SvUTF8(tmpstr)) {
216 /* Not doing UTF-8, despite what the SV says. Is this only if
217 we're trapped in use 'bytes'? */
218 /* Make a copy of the octet sequence, but without the flag on,
219 as the compiler now honours the SvUTF8 flag on tmpstr. */
221 const char *const p = SvPV(tmpstr, len);
222 tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
224 else if (SvAMAGIC(tmpstr) || SvGMAGICAL(tmpstr)) {
225 /* make a copy to avoid extra stringifies */
226 tmpstr = newSVpvn_flags(t, len, SVs_TEMP | SvUTF8(tmpstr));
230 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
232 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
234 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
235 inside tie/overload accessors. */
241 #ifndef INCOMPLETE_TAINTS
244 SvTAINTED_on((SV*)re);
245 RX_EXTFLAGS(re) |= RXf_TAINTED;
250 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
254 #if !defined(USE_ITHREADS)
255 /* can't change the optree at runtime either */
256 /* PMf_KEEP is handled differently under threads to avoid these problems */
257 if (pm->op_pmflags & PMf_KEEP) {
258 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
259 cLOGOP->op_first->op_next = PL_op->op_next;
269 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
270 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
271 register SV * const dstr = cx->sb_dstr;
272 register char *s = cx->sb_s;
273 register char *m = cx->sb_m;
274 char *orig = cx->sb_orig;
275 register REGEXP * const rx = cx->sb_rx;
277 REGEXP *old = PM_GETRE(pm);
284 PM_SETRE(pm,ReREFCNT_inc(rx));
287 rxres_restore(&cx->sb_rxres, rx);
288 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
290 if (cx->sb_iters++) {
291 const I32 saviters = cx->sb_iters;
292 if (cx->sb_iters > cx->sb_maxiters)
293 DIE(aTHX_ "Substitution loop");
295 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
297 /* See "how taint works" above pp_subst() */
299 cx->sb_rxtainted |= SUBST_TAINT_REPL;
300 sv_catsv_nomg(dstr, POPs);
301 /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */
305 /* I believe that we can't set REXEC_SCREAM here if
306 SvSCREAM(cx->sb_targ) is true because SvPVX(cx->sb_targ) isn't always
307 equal to s. [See the comment before Perl_re_intuit_start(), which is
308 called from Perl_regexec_flags(), which says that it should be when
309 SvSCREAM() is true.] s, cx->sb_strend and orig will be consistent
310 with SvPVX(cx->sb_targ), as substconst doesn't modify cx->sb_targ
312 if (CxONCE(cx) || s < orig ||
313 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
314 (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
315 ((cx->sb_rflags & REXEC_COPY_STR)
316 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
317 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
319 SV *targ = cx->sb_targ;
321 assert(cx->sb_strend >= s);
322 if(cx->sb_strend > s) {
323 if (DO_UTF8(dstr) && !SvUTF8(targ))
324 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
326 sv_catpvn(dstr, s, cx->sb_strend - s);
328 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
329 cx->sb_rxtainted |= SUBST_TAINT_PAT;
331 if (pm->op_pmflags & PMf_NONDESTRUCT) {
333 /* From here on down we're using the copy, and leaving the
334 original untouched. */
339 sv_force_normal_flags(targ, SV_COW_DROP_PV);
344 SvPV_set(targ, SvPVX(dstr));
345 SvCUR_set(targ, SvCUR(dstr));
346 SvLEN_set(targ, SvLEN(dstr));
349 SvPV_set(dstr, NULL);
351 mPUSHi(saviters - 1);
353 (void)SvPOK_only_UTF8(targ);
356 /* update the taint state of various various variables in
357 * preparation for final exit.
358 * See "how taint works" above pp_subst() */
360 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
361 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
362 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
364 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
366 if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET)
367 && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
369 SvTAINTED_on(TOPs); /* taint return value */
370 /* needed for mg_set below */
371 PL_tainted = cBOOL(cx->sb_rxtainted &
372 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL));
375 /* PL_tainted must be correctly set for this mg_set */
378 LEAVE_SCOPE(cx->sb_oldsave);
380 RETURNOP(pm->op_next);
383 cx->sb_iters = saviters;
385 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
388 cx->sb_orig = orig = RX_SUBBEG(rx);
390 cx->sb_strend = s + (cx->sb_strend - m);
392 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
394 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
395 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
397 sv_catpvn(dstr, s, m-s);
399 cx->sb_s = RX_OFFS(rx)[0].end + orig;
400 { /* Update the pos() information. */
402 = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ;
404 SvUPGRADE(sv, SVt_PVMG);
405 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
406 #ifdef PERL_OLD_COPY_ON_WRITE
408 sv_force_normal_flags(sv, 0);
410 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
413 mg->mg_len = m - orig;
416 (void)ReREFCNT_inc(rx);
417 /* update the taint state of various various variables in preparation
418 * for calling the code block.
419 * See "how taint works" above pp_subst() */
421 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
422 cx->sb_rxtainted |= SUBST_TAINT_PAT;
424 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
425 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
426 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
428 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
430 if (cx->sb_iters > 1 && (cx->sb_rxtainted &
431 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
432 SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT)
433 ? cx->sb_dstr : cx->sb_targ);
436 rxres_save(&cx->sb_rxres, rx);
438 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
442 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
447 PERL_ARGS_ASSERT_RXRES_SAVE;
450 if (!p || p[1] < RX_NPARENS(rx)) {
451 #ifdef PERL_OLD_COPY_ON_WRITE
452 i = 7 + RX_NPARENS(rx) * 2;
454 i = 6 + RX_NPARENS(rx) * 2;
463 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
464 RX_MATCH_COPIED_off(rx);
466 #ifdef PERL_OLD_COPY_ON_WRITE
467 *p++ = PTR2UV(RX_SAVED_COPY(rx));
468 RX_SAVED_COPY(rx) = NULL;
471 *p++ = RX_NPARENS(rx);
473 *p++ = PTR2UV(RX_SUBBEG(rx));
474 *p++ = (UV)RX_SUBLEN(rx);
475 for (i = 0; i <= RX_NPARENS(rx); ++i) {
476 *p++ = (UV)RX_OFFS(rx)[i].start;
477 *p++ = (UV)RX_OFFS(rx)[i].end;
482 S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
487 PERL_ARGS_ASSERT_RXRES_RESTORE;
490 RX_MATCH_COPY_FREE(rx);
491 RX_MATCH_COPIED_set(rx, *p);
494 #ifdef PERL_OLD_COPY_ON_WRITE
495 if (RX_SAVED_COPY(rx))
496 SvREFCNT_dec (RX_SAVED_COPY(rx));
497 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
501 RX_NPARENS(rx) = *p++;
503 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
504 RX_SUBLEN(rx) = (I32)(*p++);
505 for (i = 0; i <= RX_NPARENS(rx); ++i) {
506 RX_OFFS(rx)[i].start = (I32)(*p++);
507 RX_OFFS(rx)[i].end = (I32)(*p++);
512 S_rxres_free(pTHX_ void **rsp)
514 UV * const p = (UV*)*rsp;
516 PERL_ARGS_ASSERT_RXRES_FREE;
521 void *tmp = INT2PTR(char*,*p);
524 PoisonFree(*p, 1, sizeof(*p));
526 Safefree(INT2PTR(char*,*p));
528 #ifdef PERL_OLD_COPY_ON_WRITE
530 SvREFCNT_dec (INT2PTR(SV*,p[1]));
538 #define FORM_NUM_BLANK (1<<30)
539 #define FORM_NUM_POINT (1<<29)
543 dVAR; dSP; dMARK; dORIGMARK;
544 register SV * const tmpForm = *++MARK;
545 SV *formsv; /* contains text of original format */
546 register U32 *fpc; /* format ops program counter */
547 register char *t; /* current append position in target string */
548 const char *f; /* current position in format string */
550 register SV *sv = NULL; /* current item */
551 const char *item = NULL;/* string value of current item */
552 I32 itemsize = 0; /* length of current item, possibly truncated */
553 I32 fieldsize = 0; /* width of current field */
554 I32 lines = 0; /* number of lines that have been output */
555 bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
556 const char *chophere = NULL; /* where to chop current item */
557 STRLEN linemark = 0; /* pos of start of line in output */
559 bool gotsome = FALSE; /* seen at least one non-blank item on this line */
561 STRLEN linemax; /* estimate of output size in bytes */
562 bool item_is_utf8 = FALSE;
563 bool targ_is_utf8 = FALSE;
566 U8 *source; /* source of bytes to append */
567 STRLEN to_copy; /* how may bytes to append */
568 char trans; /* what chars to translate */
570 mg = doparseform(tmpForm);
572 fpc = (U32*)mg->mg_ptr;
573 /* the actual string the format was compiled from.
574 * with overload etc, this may not match tmpForm */
578 SvPV_force(PL_formtarget, len);
579 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
580 SvTAINTED_on(PL_formtarget);
581 if (DO_UTF8(PL_formtarget))
583 linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
584 t = SvGROW(PL_formtarget, len + linemax + 1);
585 /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */
587 f = SvPV_const(formsv, len);
591 const char *name = "???";
594 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
595 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
596 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
597 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
598 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
600 case FF_CHECKNL: name = "CHECKNL"; break;
601 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
602 case FF_SPACE: name = "SPACE"; break;
603 case FF_HALFSPACE: name = "HALFSPACE"; break;
604 case FF_ITEM: name = "ITEM"; break;
605 case FF_CHOP: name = "CHOP"; break;
606 case FF_LINEGLOB: name = "LINEGLOB"; break;
607 case FF_NEWLINE: name = "NEWLINE"; break;
608 case FF_MORE: name = "MORE"; break;
609 case FF_LINEMARK: name = "LINEMARK"; break;
610 case FF_END: name = "END"; break;
611 case FF_0DECIMAL: name = "0DECIMAL"; break;
612 case FF_LINESNGL: name = "LINESNGL"; break;
615 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
617 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
621 linemark = t - SvPVX(PL_formtarget);
631 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
647 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
650 SvTAINTED_on(PL_formtarget);
656 const char *s = item = SvPV_const(sv, len);
659 itemsize = sv_len_utf8(sv);
660 if (itemsize != (I32)len) {
662 if (itemsize > fieldsize) {
663 itemsize = fieldsize;
664 itembytes = itemsize;
665 sv_pos_u2b(sv, &itembytes, 0);
669 send = chophere = s + itembytes;
679 sv_pos_b2u(sv, &itemsize);
683 item_is_utf8 = FALSE;
684 if (itemsize > fieldsize)
685 itemsize = fieldsize;
686 send = chophere = s + itemsize;
700 const char *s = item = SvPV_const(sv, len);
703 itemsize = sv_len_utf8(sv);
704 if (itemsize != (I32)len) {
706 if (itemsize <= fieldsize) {
707 const char *send = chophere = s + itemsize;
720 itemsize = fieldsize;
721 itembytes = itemsize;
722 sv_pos_u2b(sv, &itembytes, 0);
723 send = chophere = s + itembytes;
724 while (s < send || (s == send && isSPACE(*s))) {
734 if (strchr(PL_chopset, *s))
739 itemsize = chophere - item;
740 sv_pos_b2u(sv, &itemsize);
746 item_is_utf8 = FALSE;
747 if (itemsize <= fieldsize) {
748 const char *const send = chophere = s + itemsize;
761 itemsize = fieldsize;
762 send = chophere = s + itemsize;
763 while (s < send || (s == send && isSPACE(*s))) {
773 if (strchr(PL_chopset, *s))
778 itemsize = chophere - item;
784 arg = fieldsize - itemsize;
793 arg = fieldsize - itemsize;
807 /* convert to_copy from chars to bytes */
811 to_copy = s - source;
817 const char *s = chophere;
831 const bool oneline = fpc[-1] == FF_LINESNGL;
832 const char *s = item = SvPV_const(sv, len);
833 const char *const send = s + len;
835 item_is_utf8 = DO_UTF8(sv);
846 to_copy = s - SvPVX_const(sv) - 1;
860 /* append to_copy bytes from source to PL_formstring.
861 * item_is_utf8 implies source is utf8.
862 * if trans, translate certain characters during the copy */
867 SvCUR_set(PL_formtarget,
868 t - SvPVX_const(PL_formtarget));
870 if (targ_is_utf8 && !item_is_utf8) {
871 source = tmp = bytes_to_utf8(source, &to_copy);
873 if (item_is_utf8 && !targ_is_utf8) {
875 /* Upgrade targ to UTF8, and then we reduce it to
876 a problem we have a simple solution for.
877 Don't need get magic. */
878 sv_utf8_upgrade_nomg(PL_formtarget);
880 /* re-calculate linemark */
881 s = (U8*)SvPVX(PL_formtarget);
882 /* the bytes we initially allocated to append the
883 * whole line may have been gobbled up during the
884 * upgrade, so allocate a whole new line's worth
889 linemark = s - (U8*)SvPVX(PL_formtarget);
891 /* Easy. They agree. */
892 assert (item_is_utf8 == targ_is_utf8);
895 /* @* and ^* are the only things that can exceed
896 * the linemax, so grow by the output size, plus
897 * a whole new form's worth in case of any further
899 grow = linemax + to_copy;
901 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1);
902 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
904 Copy(source, t, to_copy, char);
906 /* blank out ~ or control chars, depending on trans.
907 * works on bytes not chars, so relies on not
908 * matching utf8 continuation bytes */
910 U8 *send = s + to_copy;
913 if (trans == '~' ? (ch == '~') :
926 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
934 #if defined(USE_LONG_DOUBLE)
936 ((arg & FORM_NUM_POINT) ?
937 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
940 ((arg & FORM_NUM_POINT) ?
941 "%#0*.*f" : "%0*.*f");
946 #if defined(USE_LONG_DOUBLE)
948 ((arg & FORM_NUM_POINT) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
951 ((arg & FORM_NUM_POINT) ? "%#*.*f" : "%*.*f");
954 /* If the field is marked with ^ and the value is undefined,
956 if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) {
964 /* overflow evidence */
965 if (num_overflow(value, fieldsize, arg)) {
971 /* Formats aren't yet marked for locales, so assume "yes". */
973 STORE_NUMERIC_STANDARD_SET_LOCAL();
974 arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
975 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg, value);
976 RESTORE_NUMERIC_STANDARD();
983 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
991 if (arg) { /* repeat until fields exhausted? */
997 t = SvPVX(PL_formtarget) + linemark;
1004 const char *s = chophere;
1005 const char *send = item + len;
1007 while (isSPACE(*s) && (s < send))
1012 arg = fieldsize - itemsize;
1019 if (strnEQ(s1," ",3)) {
1020 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1031 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
1033 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1035 SvUTF8_on(PL_formtarget);
1036 FmLINES(PL_formtarget) += lines;
1038 if (fpc[-1] == FF_BLANK)
1039 RETURNOP(cLISTOP->op_first);
1051 if (PL_stack_base + *PL_markstack_ptr == SP) {
1053 if (GIMME_V == G_SCALAR)
1055 RETURNOP(PL_op->op_next->op_next);
1057 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
1058 Perl_pp_pushmark(aTHX); /* push dst */
1059 Perl_pp_pushmark(aTHX); /* push src */
1060 ENTER_with_name("grep"); /* enter outer scope */
1063 if (PL_op->op_private & OPpGREP_LEX)
1064 SAVESPTR(PAD_SVl(PL_op->op_targ));
1067 ENTER_with_name("grep_item"); /* enter inner scope */
1070 src = PL_stack_base[*PL_markstack_ptr];
1072 if (PL_op->op_private & OPpGREP_LEX)
1073 PAD_SVl(PL_op->op_targ) = src;
1078 if (PL_op->op_type == OP_MAPSTART)
1079 Perl_pp_pushmark(aTHX); /* push top */
1080 return ((LOGOP*)PL_op->op_next)->op_other;
1086 const I32 gimme = GIMME_V;
1087 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1093 /* first, move source pointer to the next item in the source list */
1094 ++PL_markstack_ptr[-1];
1096 /* if there are new items, push them into the destination list */
1097 if (items && gimme != G_VOID) {
1098 /* might need to make room back there first */
1099 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1100 /* XXX this implementation is very pessimal because the stack
1101 * is repeatedly extended for every set of items. Is possible
1102 * to do this without any stack extension or copying at all
1103 * by maintaining a separate list over which the map iterates
1104 * (like foreach does). --gsar */
1106 /* everything in the stack after the destination list moves
1107 * towards the end the stack by the amount of room needed */
1108 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1110 /* items to shift up (accounting for the moved source pointer) */
1111 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1113 /* This optimization is by Ben Tilly and it does
1114 * things differently from what Sarathy (gsar)
1115 * is describing. The downside of this optimization is
1116 * that leaves "holes" (uninitialized and hopefully unused areas)
1117 * to the Perl stack, but on the other hand this
1118 * shouldn't be a problem. If Sarathy's idea gets
1119 * implemented, this optimization should become
1120 * irrelevant. --jhi */
1122 shift = count; /* Avoid shifting too often --Ben Tilly */
1126 dst = (SP += shift);
1127 PL_markstack_ptr[-1] += shift;
1128 *PL_markstack_ptr += shift;
1132 /* copy the new items down to the destination list */
1133 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1134 if (gimme == G_ARRAY) {
1135 /* add returned items to the collection (making mortal copies
1136 * if necessary), then clear the current temps stack frame
1137 * *except* for those items. We do this splicing the items
1138 * into the start of the tmps frame (so some items may be on
1139 * the tmps stack twice), then moving PL_tmps_floor above
1140 * them, then freeing the frame. That way, the only tmps that
1141 * accumulate over iterations are the return values for map.
1142 * We have to do to this way so that everything gets correctly
1143 * freed if we die during the map.
1147 /* make space for the slice */
1148 EXTEND_MORTAL(items);
1149 tmpsbase = PL_tmps_floor + 1;
1150 Move(PL_tmps_stack + tmpsbase,
1151 PL_tmps_stack + tmpsbase + items,
1152 PL_tmps_ix - PL_tmps_floor,
1154 PL_tmps_ix += items;
1159 sv = sv_mortalcopy(sv);
1161 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1163 /* clear the stack frame except for the items */
1164 PL_tmps_floor += items;
1166 /* FREETMPS may have cleared the TEMP flag on some of the items */
1169 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1172 /* scalar context: we don't care about which values map returns
1173 * (we use undef here). And so we certainly don't want to do mortal
1174 * copies of meaningless values. */
1175 while (items-- > 0) {
1177 *dst-- = &PL_sv_undef;
1185 LEAVE_with_name("grep_item"); /* exit inner scope */
1188 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1190 (void)POPMARK; /* pop top */
1191 LEAVE_with_name("grep"); /* exit outer scope */
1192 (void)POPMARK; /* pop src */
1193 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1194 (void)POPMARK; /* pop dst */
1195 SP = PL_stack_base + POPMARK; /* pop original mark */
1196 if (gimme == G_SCALAR) {
1197 if (PL_op->op_private & OPpGREP_LEX) {
1198 SV* sv = sv_newmortal();
1199 sv_setiv(sv, items);
1207 else if (gimme == G_ARRAY)
1214 ENTER_with_name("grep_item"); /* enter inner scope */
1217 /* set $_ to the new source item */
1218 src = PL_stack_base[PL_markstack_ptr[-1]];
1220 if (PL_op->op_private & OPpGREP_LEX)
1221 PAD_SVl(PL_op->op_targ) = src;
1225 RETURNOP(cLOGOP->op_other);
1234 if (GIMME == G_ARRAY)
1236 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1237 return cLOGOP->op_other;
1247 if (GIMME == G_ARRAY) {
1248 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1252 SV * const targ = PAD_SV(PL_op->op_targ);
1255 if (PL_op->op_private & OPpFLIP_LINENUM) {
1256 if (GvIO(PL_last_in_gv)) {
1257 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1260 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1262 flip = SvIV(sv) == SvIV(GvSV(gv));
1268 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1269 if (PL_op->op_flags & OPf_SPECIAL) {
1277 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1280 sv_setpvs(TARG, "");
1286 /* This code tries to decide if "$left .. $right" should use the
1287 magical string increment, or if the range is numeric (we make
1288 an exception for .."0" [#18165]). AMS 20021031. */
1290 #define RANGE_IS_NUMERIC(left,right) ( \
1291 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1292 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1293 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1294 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1295 && (!SvOK(right) || looks_like_number(right))))
1301 if (GIMME == G_ARRAY) {
1307 if (RANGE_IS_NUMERIC(left,right)) {
1310 if ((SvOK(left) && SvNV_nomg(left) < IV_MIN) ||
1311 (SvOK(right) && SvNV_nomg(right) > IV_MAX))
1312 DIE(aTHX_ "Range iterator outside integer range");
1313 i = SvIV_nomg(left);
1314 max = SvIV_nomg(right);
1323 SV * const sv = sv_2mortal(newSViv(i++));
1329 const char * const lpv = SvPV_nomg_const(left, llen);
1330 const char * const tmps = SvPV_nomg_const(right, len);
1332 SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP);
1333 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1335 if (strEQ(SvPVX_const(sv),tmps))
1337 sv = sv_2mortal(newSVsv(sv));
1344 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1348 if (PL_op->op_private & OPpFLIP_LINENUM) {
1349 if (GvIO(PL_last_in_gv)) {
1350 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1353 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1354 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1362 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1363 sv_catpvs(targ, "E0");
1373 static const char * const context_name[] = {
1375 NULL, /* CXt_WHEN never actually needs "block" */
1376 NULL, /* CXt_BLOCK never actually needs "block" */
1377 NULL, /* CXt_GIVEN never actually needs "block" */
1378 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1379 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1380 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1381 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1389 S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags)
1394 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1396 for (i = cxstack_ix; i >= 0; i--) {
1397 register const PERL_CONTEXT * const cx = &cxstack[i];
1398 switch (CxTYPE(cx)) {
1404 /* diag_listed_as: Exiting subroutine via %s */
1405 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1406 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1407 if (CxTYPE(cx) == CXt_NULL)
1410 case CXt_LOOP_LAZYIV:
1411 case CXt_LOOP_LAZYSV:
1413 case CXt_LOOP_PLAIN:
1415 STRLEN cx_label_len = 0;
1416 U32 cx_label_flags = 0;
1417 const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags);
1419 ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
1422 (const U8*)cx_label, cx_label_len,
1423 (const U8*)label, len) == 0)
1425 (const U8*)label, len,
1426 (const U8*)cx_label, cx_label_len) == 0)
1427 : (len == cx_label_len && ((cx_label == label)
1428 || memEQ(cx_label, label, len))) )) {
1429 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1430 (long)i, cx_label));
1433 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1444 Perl_dowantarray(pTHX)
1447 const I32 gimme = block_gimme();
1448 return (gimme == G_VOID) ? G_SCALAR : gimme;
1452 Perl_block_gimme(pTHX)
1455 const I32 cxix = dopoptosub(cxstack_ix);
1459 switch (cxstack[cxix].blk_gimme) {
1467 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1474 Perl_is_lvalue_sub(pTHX)
1477 const I32 cxix = dopoptosub(cxstack_ix);
1478 assert(cxix >= 0); /* We should only be called from inside subs */
1480 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1481 return CxLVAL(cxstack + cxix);
1486 /* only used by PUSHSUB */
1488 Perl_was_lvalue_sub(pTHX)
1491 const I32 cxix = dopoptosub(cxstack_ix-1);
1492 assert(cxix >= 0); /* We should only be called from inside subs */
1494 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1495 return CxLVAL(cxstack + cxix);
1501 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1506 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1508 for (i = startingblock; i >= 0; i--) {
1509 register const PERL_CONTEXT * const cx = &cxstk[i];
1510 switch (CxTYPE(cx)) {
1516 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1524 S_dopoptoeval(pTHX_ I32 startingblock)
1528 for (i = startingblock; i >= 0; i--) {
1529 register const PERL_CONTEXT *cx = &cxstack[i];
1530 switch (CxTYPE(cx)) {
1534 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1542 S_dopoptoloop(pTHX_ I32 startingblock)
1546 for (i = startingblock; i >= 0; i--) {
1547 register const PERL_CONTEXT * const cx = &cxstack[i];
1548 switch (CxTYPE(cx)) {
1554 /* diag_listed_as: Exiting subroutine via %s */
1555 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1556 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1557 if ((CxTYPE(cx)) == CXt_NULL)
1560 case CXt_LOOP_LAZYIV:
1561 case CXt_LOOP_LAZYSV:
1563 case CXt_LOOP_PLAIN:
1564 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1572 S_dopoptogiven(pTHX_ I32 startingblock)
1576 for (i = startingblock; i >= 0; i--) {
1577 register const PERL_CONTEXT *cx = &cxstack[i];
1578 switch (CxTYPE(cx)) {
1582 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
1584 case CXt_LOOP_PLAIN:
1585 assert(!CxFOREACHDEF(cx));
1587 case CXt_LOOP_LAZYIV:
1588 case CXt_LOOP_LAZYSV:
1590 if (CxFOREACHDEF(cx)) {
1591 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
1600 S_dopoptowhen(pTHX_ I32 startingblock)
1604 for (i = startingblock; i >= 0; i--) {
1605 register const PERL_CONTEXT *cx = &cxstack[i];
1606 switch (CxTYPE(cx)) {
1610 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1618 Perl_dounwind(pTHX_ I32 cxix)
1623 if (!PL_curstackinfo) /* can happen if die during thread cloning */
1626 while (cxstack_ix > cxix) {
1628 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1629 DEBUG_CX("UNWIND"); \
1630 /* Note: we don't need to restore the base context info till the end. */
1631 switch (CxTYPE(cx)) {
1634 continue; /* not break */
1642 case CXt_LOOP_LAZYIV:
1643 case CXt_LOOP_LAZYSV:
1645 case CXt_LOOP_PLAIN:
1656 PERL_UNUSED_VAR(optype);
1660 Perl_qerror(pTHX_ SV *err)
1664 PERL_ARGS_ASSERT_QERROR;
1667 if (PL_in_eval & EVAL_KEEPERR) {
1668 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
1672 sv_catsv(ERRSV, err);
1675 sv_catsv(PL_errors, err);
1677 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
1679 ++PL_parser->error_count;
1683 Perl_die_unwind(pTHX_ SV *msv)
1686 SV *exceptsv = sv_mortalcopy(msv);
1687 U8 in_eval = PL_in_eval;
1688 PERL_ARGS_ASSERT_DIE_UNWIND;
1695 * Historically, perl used to set ERRSV ($@) early in the die
1696 * process and rely on it not getting clobbered during unwinding.
1697 * That sucked, because it was liable to get clobbered, so the
1698 * setting of ERRSV used to emit the exception from eval{} has
1699 * been moved to much later, after unwinding (see just before
1700 * JMPENV_JUMP below). However, some modules were relying on the
1701 * early setting, by examining $@ during unwinding to use it as
1702 * a flag indicating whether the current unwinding was caused by
1703 * an exception. It was never a reliable flag for that purpose,
1704 * being totally open to false positives even without actual
1705 * clobberage, but was useful enough for production code to
1706 * semantically rely on it.
1708 * We'd like to have a proper introspective interface that
1709 * explicitly describes the reason for whatever unwinding
1710 * operations are currently in progress, so that those modules
1711 * work reliably and $@ isn't further overloaded. But we don't
1712 * have one yet. In its absence, as a stopgap measure, ERRSV is
1713 * now *additionally* set here, before unwinding, to serve as the
1714 * (unreliable) flag that it used to.
1716 * This behaviour is temporary, and should be removed when a
1717 * proper way to detect exceptional unwinding has been developed.
1718 * As of 2010-12, the authors of modules relying on the hack
1719 * are aware of the issue, because the modules failed on
1720 * perls 5.13.{1..7} which had late setting of $@ without this
1721 * early-setting hack.
1723 if (!(in_eval & EVAL_KEEPERR)) {
1724 SvTEMP_off(exceptsv);
1725 sv_setsv(ERRSV, exceptsv);
1728 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1729 && PL_curstackinfo->si_prev)
1738 register PERL_CONTEXT *cx;
1741 JMPENV *restartjmpenv;
1744 if (cxix < cxstack_ix)
1747 POPBLOCK(cx,PL_curpm);
1748 if (CxTYPE(cx) != CXt_EVAL) {
1750 const char* message = SvPVx_const(exceptsv, msglen);
1751 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
1752 PerlIO_write(Perl_error_log, message, msglen);
1756 namesv = cx->blk_eval.old_namesv;
1757 oldcop = cx->blk_oldcop;
1758 restartjmpenv = cx->blk_eval.cur_top_env;
1759 restartop = cx->blk_eval.retop;
1761 if (gimme == G_SCALAR)
1762 *++newsp = &PL_sv_undef;
1763 PL_stack_sp = newsp;
1767 /* LEAVE could clobber PL_curcop (see save_re_context())
1768 * XXX it might be better to find a way to avoid messing with
1769 * PL_curcop in save_re_context() instead, but this is a more
1770 * minimal fix --GSAR */
1773 if (optype == OP_REQUIRE) {
1774 (void)hv_store(GvHVn(PL_incgv),
1775 SvPVX_const(namesv),
1776 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
1778 /* note that unlike pp_entereval, pp_require isn't
1779 * supposed to trap errors. So now that we've popped the
1780 * EVAL that pp_require pushed, and processed the error
1781 * message, rethrow the error */
1782 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
1783 SVfARG(exceptsv ? exceptsv : newSVpvs_flags("Unknown error\n",
1786 if (in_eval & EVAL_KEEPERR) {
1787 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
1791 sv_setsv(ERRSV, exceptsv);
1793 PL_restartjmpenv = restartjmpenv;
1794 PL_restartop = restartop;
1800 write_to_stderr(exceptsv);
1807 dVAR; dSP; dPOPTOPssrl;
1808 if (SvTRUE(left) != SvTRUE(right))
1815 =for apidoc caller_cx
1817 The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1818 returned C<PERL_CONTEXT> structure can be interrogated to find all the
1819 information returned to Perl by C<caller>. Note that XSUBs don't get a
1820 stack frame, so C<caller_cx(0, NULL)> will return information for the
1821 immediately-surrounding Perl code.
1823 This function skips over the automatic calls to C<&DB::sub> made on the
1824 behalf of the debugger. If the stack frame requested was a sub called by
1825 C<DB::sub>, the return value will be the frame for the call to
1826 C<DB::sub>, since that has the correct line number/etc. for the call
1827 site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1828 frame for the sub call itself.
1833 const PERL_CONTEXT *
1834 Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
1836 register I32 cxix = dopoptosub(cxstack_ix);
1837 register const PERL_CONTEXT *cx;
1838 register const PERL_CONTEXT *ccstack = cxstack;
1839 const PERL_SI *top_si = PL_curstackinfo;
1842 /* we may be in a higher stacklevel, so dig down deeper */
1843 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1844 top_si = top_si->si_prev;
1845 ccstack = top_si->si_cxstack;
1846 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1850 /* caller() should not report the automatic calls to &DB::sub */
1851 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1852 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1856 cxix = dopoptosub_at(ccstack, cxix - 1);
1859 cx = &ccstack[cxix];
1860 if (dbcxp) *dbcxp = cx;
1862 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1863 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1864 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1865 field below is defined for any cx. */
1866 /* caller() should not report the automatic calls to &DB::sub */
1867 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1868 cx = &ccstack[dbcxix];
1878 register const PERL_CONTEXT *cx;
1879 const PERL_CONTEXT *dbcx;
1881 const HEK *stash_hek;
1883 bool has_arg = MAXARG && TOPs;
1891 cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx);
1893 if (GIMME != G_ARRAY) {
1900 stash_hek = HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop));
1901 if (GIMME != G_ARRAY) {
1904 PUSHs(&PL_sv_undef);
1907 sv_sethek(TARG, stash_hek);
1916 PUSHs(&PL_sv_undef);
1919 sv_sethek(TARG, stash_hek);
1922 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1923 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1926 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1927 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
1928 /* So is ccstack[dbcxix]. */
1930 SV * const sv = newSV(0);
1931 gv_efullname3(sv, cvgv, NULL);
1933 PUSHs(boolSV(CxHASARGS(cx)));
1936 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1937 PUSHs(boolSV(CxHASARGS(cx)));
1941 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1944 gimme = (I32)cx->blk_gimme;
1945 if (gimme == G_VOID)
1946 PUSHs(&PL_sv_undef);
1948 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1949 if (CxTYPE(cx) == CXt_EVAL) {
1951 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1952 PUSHs(cx->blk_eval.cur_text);
1956 else if (cx->blk_eval.old_namesv) {
1957 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1960 /* eval BLOCK (try blocks have old_namesv == 0) */
1962 PUSHs(&PL_sv_undef);
1963 PUSHs(&PL_sv_undef);
1967 PUSHs(&PL_sv_undef);
1968 PUSHs(&PL_sv_undef);
1970 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1971 && CopSTASH_eq(PL_curcop, PL_debstash))
1973 AV * const ary = cx->blk_sub.argarray;
1974 const int off = AvARRAY(ary) - AvALLOC(ary);
1976 Perl_init_dbargs(aTHX);
1978 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1979 av_extend(PL_dbargs, AvFILLp(ary) + off);
1980 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1981 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1983 /* XXX only hints propagated via op_private are currently
1984 * visible (others are not easily accessible, since they
1985 * use the global PL_hints) */
1986 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1989 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1991 if (old_warnings == pWARN_NONE ||
1992 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1993 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1994 else if (old_warnings == pWARN_ALL ||
1995 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1996 /* Get the bit mask for $warnings::Bits{all}, because
1997 * it could have been extended by warnings::register */
1999 HV * const bits = get_hv("warnings::Bits", 0);
2000 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
2001 mask = newSVsv(*bits_all);
2004 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
2008 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
2012 PUSHs(cx->blk_oldcop->cop_hints_hash ?
2013 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
2022 const char * const tmps =
2023 (MAXARG < 1 || (!TOPs && !POPs)) ? (const char *)"" : POPpconstx;
2024 sv_reset(tmps, CopSTASH(PL_curcop));
2029 /* like pp_nextstate, but used instead when the debugger is active */
2034 PL_curcop = (COP*)PL_op;
2035 TAINT_NOT; /* Each statement is presumed innocent */
2036 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
2041 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
2042 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
2045 register PERL_CONTEXT *cx;
2046 const I32 gimme = G_ARRAY;
2048 GV * const gv = PL_DBgv;
2049 register CV * const cv = GvCV(gv);
2052 DIE(aTHX_ "No DB::DB routine defined");
2054 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2055 /* don't do recursive DB::DB call */
2070 (void)(*CvXSUB(cv))(aTHX_ cv);
2077 PUSHBLOCK(cx, CXt_SUB, SP);
2079 cx->blk_sub.retop = PL_op->op_next;
2082 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
2083 RETURNOP(CvSTART(cv));
2091 S_adjust_stack_on_leave(pTHX_ SV **newsp, SV **sp, SV **mark, I32 gimme, U32 flags)
2094 PERL_ARGS_ASSERT_ADJUST_STACK_ON_LEAVE;
2096 if (flags & SVs_PADTMP) {
2097 flags &= ~SVs_PADTMP;
2100 if (gimme == G_SCALAR) {
2102 *++newsp = ((SvFLAGS(*SP) & flags) || (padtmp && SvPADTMP(*SP)))
2103 ? *SP : sv_mortalcopy(*SP);
2105 /* MEXTEND() only updates MARK, so reuse it instead of newsp. */
2108 *++MARK = &PL_sv_undef;
2112 else if (gimme == G_ARRAY) {
2113 /* in case LEAVE wipes old return values */
2114 while (++MARK <= SP) {
2115 if ((SvFLAGS(*MARK) & flags) || (padtmp && SvPADTMP(*MARK)))
2118 *++newsp = sv_mortalcopy(*MARK);
2119 TAINT_NOT; /* Each item is independent */
2122 /* When this function was called with MARK == newsp, we reach this
2123 * point with SP == newsp. */
2132 register PERL_CONTEXT *cx;
2133 I32 gimme = GIMME_V;
2135 ENTER_with_name("block");
2138 PUSHBLOCK(cx, CXt_BLOCK, SP);
2146 register PERL_CONTEXT *cx;
2151 if (PL_op->op_flags & OPf_SPECIAL) {
2152 cx = &cxstack[cxstack_ix];
2153 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
2158 gimme = OP_GIMME(PL_op, (cxstack_ix >= 0) ? gimme : G_SCALAR);
2161 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
2162 PL_curpm = newpm; /* Don't pop $1 et al till now */
2164 LEAVE_with_name("block");
2172 register PERL_CONTEXT *cx;
2173 const I32 gimme = GIMME_V;
2174 void *itervar; /* location of the iteration variable */
2175 U8 cxtype = CXt_LOOP_FOR;
2177 ENTER_with_name("loop1");
2180 if (PL_op->op_targ) { /* "my" variable */
2181 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
2182 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2183 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2184 SVs_PADSTALE, SVs_PADSTALE);
2186 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
2188 itervar = PL_comppad;
2190 itervar = &PAD_SVl(PL_op->op_targ);
2193 else { /* symbol table variable */
2194 GV * const gv = MUTABLE_GV(POPs);
2195 SV** svp = &GvSV(gv);
2196 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
2198 itervar = (void *)gv;
2201 if (PL_op->op_private & OPpITER_DEF)
2202 cxtype |= CXp_FOR_DEF;
2204 ENTER_with_name("loop2");
2206 PUSHBLOCK(cx, cxtype, SP);
2207 PUSHLOOP_FOR(cx, itervar, MARK);
2208 if (PL_op->op_flags & OPf_STACKED) {
2209 SV *maybe_ary = POPs;
2210 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2212 SV * const right = maybe_ary;
2215 if (RANGE_IS_NUMERIC(sv,right)) {
2216 cx->cx_type &= ~CXTYPEMASK;
2217 cx->cx_type |= CXt_LOOP_LAZYIV;
2218 /* Make sure that no-one re-orders cop.h and breaks our
2220 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
2221 #ifdef NV_PRESERVES_UV
2222 if ((SvOK(sv) && ((SvNV_nomg(sv) < (NV)IV_MIN) ||
2223 (SvNV_nomg(sv) > (NV)IV_MAX)))
2225 (SvOK(right) && ((SvNV_nomg(right) > (NV)IV_MAX) ||
2226 (SvNV_nomg(right) < (NV)IV_MIN))))
2228 if ((SvOK(sv) && ((SvNV_nomg(sv) <= (NV)IV_MIN)
2230 ((SvNV_nomg(sv) > 0) &&
2231 ((SvUV_nomg(sv) > (UV)IV_MAX) ||
2232 (SvNV_nomg(sv) > (NV)UV_MAX)))))
2234 (SvOK(right) && ((SvNV_nomg(right) <= (NV)IV_MIN)
2236 ((SvNV_nomg(right) > 0) &&
2237 ((SvUV_nomg(right) > (UV)IV_MAX) ||
2238 (SvNV_nomg(right) > (NV)UV_MAX))
2241 DIE(aTHX_ "Range iterator outside integer range");
2242 cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv);
2243 cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right);
2245 /* for correct -Dstv display */
2246 cx->blk_oldsp = sp - PL_stack_base;
2250 cx->cx_type &= ~CXTYPEMASK;
2251 cx->cx_type |= CXt_LOOP_LAZYSV;
2252 /* Make sure that no-one re-orders cop.h and breaks our
2254 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2255 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2256 cx->blk_loop.state_u.lazysv.end = right;
2257 SvREFCNT_inc(right);
2258 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2259 /* This will do the upgrade to SVt_PV, and warn if the value
2260 is uninitialised. */
2261 (void) SvPV_nolen_const(right);
2262 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2263 to replace !SvOK() with a pointer to "". */
2265 SvREFCNT_dec(right);
2266 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2270 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2271 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2272 SvREFCNT_inc(maybe_ary);
2273 cx->blk_loop.state_u.ary.ix =
2274 (PL_op->op_private & OPpITER_REVERSED) ?
2275 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2279 else { /* iterating over items on the stack */
2280 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2281 if (PL_op->op_private & OPpITER_REVERSED) {
2282 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2285 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2295 register PERL_CONTEXT *cx;
2296 const I32 gimme = GIMME_V;
2298 ENTER_with_name("loop1");
2300 ENTER_with_name("loop2");
2302 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2303 PUSHLOOP_PLAIN(cx, SP);
2311 register PERL_CONTEXT *cx;
2318 assert(CxTYPE_is_LOOP(cx));
2320 newsp = PL_stack_base + cx->blk_loop.resetsp;
2323 SP = adjust_stack_on_leave(newsp, SP, MARK, gimme, 0);
2326 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2327 PL_curpm = newpm; /* ... and pop $1 et al */
2329 LEAVE_with_name("loop2");
2330 LEAVE_with_name("loop1");
2336 S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
2337 PERL_CONTEXT *cx, PMOP *newpm)
2339 const bool ref = !!(CxLVAL(cx) & OPpENTERSUB_INARGS);
2340 if (gimme == G_SCALAR) {
2341 if (CxLVAL(cx) && !ref) { /* Leave it as it is if we can. */
2343 const char *what = NULL;
2345 assert(MARK+1 == SP);
2346 if ((SvPADTMP(TOPs) ||
2347 (SvFLAGS(TOPs) & (SVf_READONLY | SVf_FAKE))
2350 !SvSMAGICAL(TOPs)) {
2352 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2353 : "a readonly value" : "a temporary";
2358 /* sub:lvalue{} will take us here. */
2367 "Can't return %s from lvalue subroutine", what
2372 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2373 if (!SvPADTMP(*SP)) {
2374 *++newsp = SvREFCNT_inc(*SP);
2379 /* FREETMPS could clobber it */
2380 SV *sv = SvREFCNT_inc(*SP);
2382 *++newsp = sv_mortalcopy(sv);
2389 ? sv_mortalcopy(*SP)
2391 ? sv_2mortal(SvREFCNT_inc_simple_NN(*SP))
2396 *++newsp = &PL_sv_undef;
2398 if (CxLVAL(cx) & OPpDEREF) {
2401 TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF);
2405 else if (gimme == G_ARRAY) {
2406 assert (!(CxLVAL(cx) & OPpDEREF));
2407 if (ref || !CxLVAL(cx))
2408 while (++MARK <= SP)
2410 SvFLAGS(*MARK) & SVs_PADTMP
2411 ? sv_mortalcopy(*MARK)
2414 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
2415 else while (++MARK <= SP) {
2416 if (*MARK != &PL_sv_undef
2418 || (SvFLAGS(*MARK) & (SVf_READONLY|SVf_FAKE))
2423 /* Might be flattened array after $#array = */
2430 /* diag_listed_as: Can't return %s from lvalue subroutine */
2432 "Can't return a %s from lvalue subroutine",
2433 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2439 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
2442 PL_stack_sp = newsp;
2448 register PERL_CONTEXT *cx;
2449 bool popsub2 = FALSE;
2450 bool clear_errsv = FALSE;
2460 const I32 cxix = dopoptosub(cxstack_ix);
2463 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2464 * sort block, which is a CXt_NULL
2467 PL_stack_base[1] = *PL_stack_sp;
2468 PL_stack_sp = PL_stack_base + 1;
2472 DIE(aTHX_ "Can't return outside a subroutine");
2474 if (cxix < cxstack_ix)
2477 if (CxMULTICALL(&cxstack[cxix])) {
2478 gimme = cxstack[cxix].blk_gimme;
2479 if (gimme == G_VOID)
2480 PL_stack_sp = PL_stack_base;
2481 else if (gimme == G_SCALAR) {
2482 PL_stack_base[1] = *PL_stack_sp;
2483 PL_stack_sp = PL_stack_base + 1;
2489 switch (CxTYPE(cx)) {
2492 lval = !!CvLVALUE(cx->blk_sub.cv);
2493 retop = cx->blk_sub.retop;
2494 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2497 if (!(PL_in_eval & EVAL_KEEPERR))
2500 namesv = cx->blk_eval.old_namesv;
2501 retop = cx->blk_eval.retop;
2504 if (optype == OP_REQUIRE &&
2505 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2507 /* Unassume the success we assumed earlier. */
2508 (void)hv_delete(GvHVn(PL_incgv),
2509 SvPVX_const(namesv),
2510 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
2512 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
2517 retop = cx->blk_sub.retop;
2520 DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx));
2524 if (lval) S_return_lvalues(aTHX_ MARK, SP, newsp, gimme, cx, newpm);
2526 if (gimme == G_SCALAR) {
2529 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2530 if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1
2531 && !SvMAGICAL(TOPs)) {
2532 *++newsp = SvREFCNT_inc(*SP);
2537 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2539 *++newsp = sv_mortalcopy(sv);
2543 else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1
2544 && !SvMAGICAL(*SP)) {
2548 *++newsp = sv_mortalcopy(*SP);
2551 *++newsp = sv_mortalcopy(*SP);
2554 *++newsp = &PL_sv_undef;
2556 else if (gimme == G_ARRAY) {
2557 while (++MARK <= SP) {
2558 *++newsp = popsub2 && SvTEMP(*MARK) && SvREFCNT(*MARK) == 1
2559 && !SvGMAGICAL(*MARK)
2560 ? *MARK : sv_mortalcopy(*MARK);
2561 TAINT_NOT; /* Each item is independent */
2564 PL_stack_sp = newsp;
2568 /* Stack values are safe: */
2571 POPSUB(cx,sv); /* release CV and @_ ... */
2575 PL_curpm = newpm; /* ... and pop $1 et al */
2584 /* This duplicates parts of pp_leavesub, so that it can share code with
2592 register PERL_CONTEXT *cx;
2595 if (CxMULTICALL(&cxstack[cxstack_ix]))
2599 cxstack_ix++; /* temporarily protect top context */
2603 S_return_lvalues(aTHX_ newsp, SP, newsp, gimme, cx, newpm);
2607 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2608 PL_curpm = newpm; /* ... and pop $1 et al */
2611 return cx->blk_sub.retop;
2618 register PERL_CONTEXT *cx;
2629 if (PL_op->op_flags & OPf_SPECIAL) {
2630 cxix = dopoptoloop(cxstack_ix);
2632 DIE(aTHX_ "Can't \"last\" outside a loop block");
2635 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2636 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
2638 DIE(aTHX_ "Label not found for \"last %"SVf"\"",
2639 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2640 strlen(cPVOP->op_pv),
2641 ((cPVOP->op_private & OPpPV_IS_UTF8)
2642 ? SVf_UTF8 : 0) | SVs_TEMP)));
2644 if (cxix < cxstack_ix)
2648 cxstack_ix++; /* temporarily protect top context */
2650 switch (CxTYPE(cx)) {
2651 case CXt_LOOP_LAZYIV:
2652 case CXt_LOOP_LAZYSV:
2654 case CXt_LOOP_PLAIN:
2656 newsp = PL_stack_base + cx->blk_loop.resetsp;
2657 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2661 nextop = cx->blk_sub.retop;
2665 nextop = cx->blk_eval.retop;
2669 nextop = cx->blk_sub.retop;
2672 DIE(aTHX_ "panic: last, type=%u", (unsigned) CxTYPE(cx));
2676 SP = adjust_stack_on_leave(newsp, SP, MARK, gimme,
2677 pop2 == CXt_SUB ? SVs_TEMP : 0);
2682 /* Stack values are safe: */
2684 case CXt_LOOP_LAZYIV:
2685 case CXt_LOOP_PLAIN:
2686 case CXt_LOOP_LAZYSV:
2688 POPLOOP(cx); /* release loop vars ... */
2692 POPSUB(cx,sv); /* release CV and @_ ... */
2695 PL_curpm = newpm; /* ... and pop $1 et al */
2698 PERL_UNUSED_VAR(optype);
2699 PERL_UNUSED_VAR(gimme);
2707 register PERL_CONTEXT *cx;
2710 if (PL_op->op_flags & OPf_SPECIAL) {
2711 cxix = dopoptoloop(cxstack_ix);
2713 DIE(aTHX_ "Can't \"next\" outside a loop block");
2716 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2717 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
2719 DIE(aTHX_ "Label not found for \"next %"SVf"\"",
2720 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2721 strlen(cPVOP->op_pv),
2722 ((cPVOP->op_private & OPpPV_IS_UTF8)
2723 ? SVf_UTF8 : 0) | SVs_TEMP)));
2725 if (cxix < cxstack_ix)
2728 /* clear off anything above the scope we're re-entering, but
2729 * save the rest until after a possible continue block */
2730 inner = PL_scopestack_ix;
2732 if (PL_scopestack_ix < inner)
2733 leave_scope(PL_scopestack[PL_scopestack_ix]);
2734 PL_curcop = cx->blk_oldcop;
2735 return (cx)->blk_loop.my_op->op_nextop;
2742 register PERL_CONTEXT *cx;
2746 if (PL_op->op_flags & OPf_SPECIAL) {
2747 cxix = dopoptoloop(cxstack_ix);
2749 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2752 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2753 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
2755 DIE(aTHX_ "Label not found for \"redo %"SVf"\"",
2756 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2757 strlen(cPVOP->op_pv),
2758 ((cPVOP->op_private & OPpPV_IS_UTF8)
2759 ? SVf_UTF8 : 0) | SVs_TEMP)));
2761 if (cxix < cxstack_ix)
2764 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2765 if (redo_op->op_type == OP_ENTER) {
2766 /* pop one less context to avoid $x being freed in while (my $x..) */
2768 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2769 redo_op = redo_op->op_next;
2773 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2774 LEAVE_SCOPE(oldsave);
2776 PL_curcop = cx->blk_oldcop;
2781 S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit)
2785 static const char too_deep[] = "Target of goto is too deeply nested";
2787 PERL_ARGS_ASSERT_DOFINDLABEL;
2790 Perl_croak(aTHX_ too_deep);
2791 if (o->op_type == OP_LEAVE ||
2792 o->op_type == OP_SCOPE ||
2793 o->op_type == OP_LEAVELOOP ||
2794 o->op_type == OP_LEAVESUB ||
2795 o->op_type == OP_LEAVETRY)
2797 *ops++ = cUNOPo->op_first;
2799 Perl_croak(aTHX_ too_deep);
2802 if (o->op_flags & OPf_KIDS) {
2804 /* First try all the kids at this level, since that's likeliest. */
2805 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2806 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2807 STRLEN kid_label_len;
2808 U32 kid_label_flags;
2809 const char *kid_label = CopLABEL_len_flags(kCOP,
2810 &kid_label_len, &kid_label_flags);
2812 ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
2815 (const U8*)kid_label, kid_label_len,
2816 (const U8*)label, len) == 0)
2818 (const U8*)label, len,
2819 (const U8*)kid_label, kid_label_len) == 0)
2820 : ( len == kid_label_len && ((kid_label == label)
2821 || memEQ(kid_label, label, len)))))
2825 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2826 if (kid == PL_lastgotoprobe)
2828 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2831 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2832 ops[-1]->op_type == OP_DBSTATE)
2837 if ((o = dofindlabel(kid, label, len, flags, ops, oplimit)))
2850 register PERL_CONTEXT *cx;
2851 #define GOTO_DEPTH 64
2852 OP *enterops[GOTO_DEPTH];
2853 const char *label = NULL;
2854 STRLEN label_len = 0;
2855 U32 label_flags = 0;
2856 const bool do_dump = (PL_op->op_type == OP_DUMP);
2857 static const char must_have_label[] = "goto must have label";
2859 if (PL_op->op_flags & OPf_STACKED) {
2860 SV * const sv = POPs;
2862 /* This egregious kludge implements goto &subroutine */
2863 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2865 register PERL_CONTEXT *cx;
2866 CV *cv = MUTABLE_CV(SvRV(sv));
2873 if (!CvROOT(cv) && !CvXSUB(cv)) {
2874 const GV * const gv = CvGV(cv);
2878 /* autoloaded stub? */
2879 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2881 autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv),
2883 GvNAMEUTF8(gv) ? SVf_UTF8 : 0);
2884 if (autogv && (cv = GvCV(autogv)))
2886 tmpstr = sv_newmortal();
2887 gv_efullname3(tmpstr, gv, NULL);
2888 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2890 DIE(aTHX_ "Goto undefined subroutine");
2893 /* First do some returnish stuff. */
2894 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2896 cxix = dopoptosub(cxstack_ix);
2898 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2899 if (cxix < cxstack_ix)
2903 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2904 if (CxTYPE(cx) == CXt_EVAL) {
2906 /* diag_listed_as: Can't goto subroutine from an eval-%s */
2907 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2909 /* diag_listed_as: Can't goto subroutine from an eval-%s */
2910 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2912 else if (CxMULTICALL(cx))
2913 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2914 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2915 /* put @_ back onto stack */
2916 AV* av = cx->blk_sub.argarray;
2918 items = AvFILLp(av) + 1;
2919 EXTEND(SP, items+1); /* @_ could have been extended. */
2920 Copy(AvARRAY(av), SP + 1, items, SV*);
2921 SvREFCNT_dec(GvAV(PL_defgv));
2922 GvAV(PL_defgv) = cx->blk_sub.savearray;
2924 /* abandon @_ if it got reified */
2929 av_extend(av, items-1);
2931 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2934 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2935 AV* const av = GvAV(PL_defgv);
2936 items = AvFILLp(av) + 1;
2937 EXTEND(SP, items+1); /* @_ could have been extended. */
2938 Copy(AvARRAY(av), SP + 1, items, SV*);
2942 if (CxTYPE(cx) == CXt_SUB &&
2943 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2944 SvREFCNT_dec(cx->blk_sub.cv);
2945 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2946 LEAVE_SCOPE(oldsave);
2948 /* A destructor called during LEAVE_SCOPE could have undefined
2949 * our precious cv. See bug #99850. */
2950 if (!CvROOT(cv) && !CvXSUB(cv)) {
2951 const GV * const gv = CvGV(cv);
2953 SV * const tmpstr = sv_newmortal();
2954 gv_efullname3(tmpstr, gv, NULL);
2955 DIE(aTHX_ "Goto undefined subroutine &%"SVf"",
2958 DIE(aTHX_ "Goto undefined subroutine");
2961 /* Now do some callish stuff. */
2963 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2965 OP* const retop = cx->blk_sub.retop;
2966 SV **newsp PERL_UNUSED_DECL;
2967 I32 gimme PERL_UNUSED_DECL;
2970 for (index=0; index<items; index++)
2971 sv_2mortal(SP[-index]);
2974 /* XS subs don't have a CxSUB, so pop it */
2975 POPBLOCK(cx, PL_curpm);
2976 /* Push a mark for the start of arglist */
2979 (void)(*CvXSUB(cv))(aTHX_ cv);
2984 AV* const padlist = CvPADLIST(cv);
2985 if (CxTYPE(cx) == CXt_EVAL) {
2986 PL_in_eval = CxOLD_IN_EVAL(cx);
2987 PL_eval_root = cx->blk_eval.old_eval_root;
2988 cx->cx_type = CXt_SUB;
2990 cx->blk_sub.cv = cv;
2991 cx->blk_sub.olddepth = CvDEPTH(cv);
2994 if (CvDEPTH(cv) < 2)
2995 SvREFCNT_inc_simple_void_NN(cv);
2997 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2998 sub_crush_depth(cv);
2999 pad_push(padlist, CvDEPTH(cv));
3001 PL_curcop = cx->blk_oldcop;
3003 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
3006 AV *const av = MUTABLE_AV(PAD_SVl(0));
3008 cx->blk_sub.savearray = GvAV(PL_defgv);
3009 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
3010 CX_CURPAD_SAVE(cx->blk_sub);
3011 cx->blk_sub.argarray = av;
3013 if (items >= AvMAX(av) + 1) {
3014 SV **ary = AvALLOC(av);
3015 if (AvARRAY(av) != ary) {
3016 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
3019 if (items >= AvMAX(av) + 1) {
3020 AvMAX(av) = items - 1;
3021 Renew(ary,items+1,SV*);
3027 Copy(mark,AvARRAY(av),items,SV*);
3028 AvFILLp(av) = items - 1;
3029 assert(!AvREAL(av));
3031 /* transfer 'ownership' of refcnts to new @_ */
3041 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
3042 Perl_get_db_sub(aTHX_ NULL, cv);
3044 CV * const gotocv = get_cvs("DB::goto", 0);
3046 PUSHMARK( PL_stack_sp );
3047 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
3052 RETURNOP(CvSTART(cv));
3056 label = SvPV_const(sv, label_len);
3057 label_flags = SvUTF8(sv);
3060 else if (!(PL_op->op_flags & OPf_SPECIAL)) {
3061 label = cPVOP->op_pv;
3062 label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
3063 label_len = strlen(label);
3065 if (!(do_dump || label_len)) DIE(aTHX_ must_have_label);
3070 OP *gotoprobe = NULL;
3071 bool leaving_eval = FALSE;
3072 bool in_block = FALSE;
3073 PERL_CONTEXT *last_eval_cx = NULL;
3077 PL_lastgotoprobe = NULL;
3079 for (ix = cxstack_ix; ix >= 0; ix--) {
3081 switch (CxTYPE(cx)) {
3083 leaving_eval = TRUE;
3084 if (!CxTRYBLOCK(cx)) {
3085 gotoprobe = (last_eval_cx ?
3086 last_eval_cx->blk_eval.old_eval_root :
3091 /* else fall through */
3092 case CXt_LOOP_LAZYIV:
3093 case CXt_LOOP_LAZYSV:
3095 case CXt_LOOP_PLAIN:
3098 gotoprobe = cx->blk_oldcop->op_sibling;
3104 gotoprobe = cx->blk_oldcop->op_sibling;
3107 gotoprobe = PL_main_root;
3110 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
3111 gotoprobe = CvROOT(cx->blk_sub.cv);
3117 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
3120 DIE(aTHX_ "panic: goto, type=%u, ix=%ld",
3121 CxTYPE(cx), (long) ix);
3122 gotoprobe = PL_main_root;
3126 retop = dofindlabel(gotoprobe, label, label_len, label_flags,
3127 enterops, enterops + GOTO_DEPTH);
3130 if (gotoprobe->op_sibling &&
3131 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
3132 gotoprobe->op_sibling->op_sibling) {
3133 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
3134 label, label_len, label_flags, enterops,
3135 enterops + GOTO_DEPTH);
3140 PL_lastgotoprobe = gotoprobe;
3143 DIE(aTHX_ "Can't find label %"SVf,
3144 SVfARG(newSVpvn_flags(label, label_len,
3145 SVs_TEMP | label_flags)));
3147 /* if we're leaving an eval, check before we pop any frames
3148 that we're not going to punt, otherwise the error
3151 if (leaving_eval && *enterops && enterops[1]) {
3153 for (i = 1; enterops[i]; i++)
3154 if (enterops[i]->op_type == OP_ENTERITER)
3155 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3158 if (*enterops && enterops[1]) {
3159 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3161 deprecate("\"goto\" to jump into a construct");
3164 /* pop unwanted frames */
3166 if (ix < cxstack_ix) {
3173 oldsave = PL_scopestack[PL_scopestack_ix];
3174 LEAVE_SCOPE(oldsave);
3177 /* push wanted frames */
3179 if (*enterops && enterops[1]) {
3180 OP * const oldop = PL_op;
3181 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3182 for (; enterops[ix]; ix++) {
3183 PL_op = enterops[ix];
3184 /* Eventually we may want to stack the needed arguments
3185 * for each op. For now, we punt on the hard ones. */
3186 if (PL_op->op_type == OP_ENTERITER)
3187 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3188 PL_op->op_ppaddr(aTHX);
3196 if (!retop) retop = PL_main_start;
3198 PL_restartop = retop;
3199 PL_do_undump = TRUE;
3203 PL_restartop = 0; /* hmm, must be GNU unexec().. */
3204 PL_do_undump = FALSE;
3219 anum = 0; (void)POPs;
3224 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
3226 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
3229 PL_exit_flags |= PERL_EXIT_EXPECTED;
3231 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
3232 if (anum || !(PL_minus_c && PL_madskills))
3237 PUSHs(&PL_sv_undef);
3244 S_save_lines(pTHX_ AV *array, SV *sv)
3246 const char *s = SvPVX_const(sv);
3247 const char * const send = SvPVX_const(sv) + SvCUR(sv);
3250 PERL_ARGS_ASSERT_SAVE_LINES;
3252 while (s && s < send) {
3254 SV * const tmpstr = newSV_type(SVt_PVMG);
3256 t = (const char *)memchr(s, '\n', send - s);
3262 sv_setpvn(tmpstr, s, t - s);
3263 av_store(array, line++, tmpstr);
3271 Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
3273 0 is used as continue inside eval,
3275 3 is used for a die caught by an inner eval - continue inner loop
3277 See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
3278 establish a local jmpenv to handle exception traps.
3283 S_docatch(pTHX_ OP *o)
3287 OP * const oldop = PL_op;
3291 assert(CATCH_GET == TRUE);
3298 assert(cxstack_ix >= 0);
3299 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3300 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
3305 /* die caught by an inner eval - continue inner loop */
3306 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3307 PL_restartjmpenv = NULL;
3308 PL_op = PL_restartop;
3324 /* James Bond: Do you expect me to talk?
3325 Auric Goldfinger: No, Mr. Bond. I expect you to die.
3327 This code is an ugly hack, doesn't work with lexicals in subroutines that are
3328 called more than once, and is only used by regcomp.c, for (?{}) blocks.
3330 Currently it is not used outside the core code. Best if it stays that way.
3332 Hence it's now deprecated, and will be removed.
3335 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
3336 /* sv Text to convert to OP tree. */
3337 /* startop op_free() this to undo. */
3338 /* code Short string id of the caller. */
3340 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3341 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
3344 /* Don't use this. It will go away without warning once the regexp engine is
3345 refactored not to use it. */
3347 Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3350 dVAR; dSP; /* Make POPBLOCK work. */
3356 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3357 char *tmpbuf = tbuf;
3360 CV* runcv = NULL; /* initialise to avoid compiler warnings */
3364 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
3366 ENTER_with_name("eval");
3367 lex_start(sv, NULL, LEX_START_SAME_FILTER);
3369 /* switch to eval mode */
3371 if (IN_PERL_COMPILETIME) {
3372 SAVECOPSTASH_FREE(&PL_compiling);
3373 CopSTASH_set(&PL_compiling, PL_curstash);
3375 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3376 SV * const sv = sv_newmortal();
3377 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3378 code, (unsigned long)++PL_evalseq,
3379 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3384 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3385 (unsigned long)++PL_evalseq);
3386 SAVECOPFILE_FREE(&PL_compiling);
3387 CopFILE_set(&PL_compiling, tmpbuf+2);
3388 SAVECOPLINE(&PL_compiling);
3389 CopLINE_set(&PL_compiling, 1);
3390 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3391 deleting the eval's FILEGV from the stash before gv_check() runs
3392 (i.e. before run-time proper). To work around the coredump that
3393 ensues, we always turn GvMULTI_on for any globals that were
3394 introduced within evals. See force_ident(). GSAR 96-10-12 */
3395 safestr = savepvn(tmpbuf, len);
3396 SAVEDELETE(PL_defstash, safestr, len);
3398 #ifdef OP_IN_REGISTER
3404 /* we get here either during compilation, or via pp_regcomp at runtime */
3405 runtime = IN_PERL_RUNTIME;
3408 runcv = find_runcv(NULL);
3410 /* At run time, we have to fetch the hints from PL_curcop. */
3411 PL_hints = PL_curcop->cop_hints;
3412 if (PL_hints & HINT_LOCALIZE_HH) {
3413 /* SAVEHINTS created a new HV in PL_hintgv, which we
3415 SvREFCNT_dec(GvHV(PL_hintgv));
3417 refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0);
3418 hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints);
3420 SAVECOMPILEWARNINGS();
3421 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3422 cophh_free(CopHINTHASH_get(&PL_compiling));
3423 /* XXX Does this need to avoid copying a label? */
3424 PL_compiling.cop_hints_hash
3425 = cophh_copy(PL_curcop->cop_hints_hash);
3429 PL_op->op_type = OP_ENTEREVAL;
3430 PL_op->op_flags = 0; /* Avoid uninit warning. */
3431 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
3433 need_catch = CATCH_GET;
3437 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq, NULL);
3439 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax, NULL);
3440 CATCH_SET(need_catch);
3441 POPBLOCK(cx,PL_curpm);
3444 (*startop)->op_type = OP_NULL;
3445 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
3446 /* XXX DAPM do this properly one year */
3447 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
3448 LEAVE_with_name("eval");
3449 if (IN_PERL_COMPILETIME)
3450 CopHINTS_set(&PL_compiling, PL_hints);
3451 #ifdef OP_IN_REGISTER
3454 PERL_UNUSED_VAR(newsp);
3455 PERL_UNUSED_VAR(optype);
3457 return PL_eval_start;
3462 =for apidoc find_runcv
3464 Locate the CV corresponding to the currently executing sub or eval.
3465 If db_seqp is non_null, skip CVs that are in the DB package and populate
3466 *db_seqp with the cop sequence number at the point that the DB:: code was
3467 entered. (allows debuggers to eval in the scope of the breakpoint rather
3468 than in the scope of the debugger itself).
3474 Perl_find_runcv(pTHX_ U32 *db_seqp)
3480 *db_seqp = PL_curcop->cop_seq;
3481 for (si = PL_curstackinfo; si; si = si->si_prev) {
3483 for (ix = si->si_cxix; ix >= 0; ix--) {
3484 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3485 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3486 CV * const cv = cx->blk_sub.cv;
3487 /* skip DB:: code */
3488 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3489 *db_seqp = cx->blk_oldcop->cop_seq;
3494 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3495 return cx->blk_eval.cv;
3502 /* Run yyparse() in a setjmp wrapper. Returns:
3503 * 0: yyparse() successful
3504 * 1: yyparse() failed
3508 S_try_yyparse(pTHX_ int gramtype)
3513 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3517 ret = yyparse(gramtype) ? 1 : 0;
3531 /* Compile a require/do, an eval '', or a /(?{...})/.
3532 * In the last case, startop is non-null, and contains the address of
3533 * a pointer that should be set to the just-compiled code.
3534 * outside is the lexically enclosing CV (if any) that invoked us.
3535 * Returns a bool indicating whether the compile was successful; if so,
3536 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3537 * pushes undef (also croaks if startop != NULL).
3540 /* This function is called from three places, sv_compile_2op, pp_require
3541 * and pp_entereval. These can be distinguished as follows:
3542 * sv_compile_2op - startop is non-null
3543 * pp_require - startop is null; saveop is not entereval
3544 * pp_entereval - startop is null; saveop is entereval
3548 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq, HV *hh)
3551 OP * const saveop = PL_op;
3552 COP * const oldcurcop = PL_curcop;
3553 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3557 PL_in_eval = (in_require
3558 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3563 evalcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3565 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3566 cxstack[cxstack_ix].blk_eval.cv = evalcv;
3567 cxstack[cxstack_ix].blk_gimme = gimme;
3569 CvOUTSIDE_SEQ(evalcv) = seq;
3570 CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3572 /* set up a scratch pad */
3574 CvPADLIST(evalcv) = pad_new(padnew_SAVE);
3575 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3579 SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */
3581 /* make sure we compile in the right package */
3583 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3584 SAVEGENERICSV(PL_curstash);
3585 PL_curstash = (HV *)SvREFCNT_inc_simple(CopSTASH(PL_curcop));
3587 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3588 SAVESPTR(PL_beginav);
3589 PL_beginav = newAV();
3590 SAVEFREESV(PL_beginav);
3591 SAVESPTR(PL_unitcheckav);
3592 PL_unitcheckav = newAV();
3593 SAVEFREESV(PL_unitcheckav);
3596 SAVEBOOL(PL_madskills);
3600 if (!startop) ENTER_with_name("evalcomp");
3601 SAVESPTR(PL_compcv);
3604 /* try to compile it */
3606 PL_eval_root = NULL;
3607 PL_curcop = &PL_compiling;
3608 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3609 PL_in_eval |= EVAL_KEEPERR;
3614 bool clear_hints = saveop->op_type != OP_ENTEREVAL;
3618 hv_clear(GvHV(PL_hintgv));
3621 PL_hints = saveop->op_private & OPpEVAL_COPHH
3622 ? oldcurcop->cop_hints : saveop->op_targ;
3624 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3625 SvREFCNT_dec(GvHV(PL_hintgv));
3626 GvHV(PL_hintgv) = hh;
3629 SAVECOMPILEWARNINGS();
3631 if (PL_dowarn & G_WARN_ALL_ON)
3632 PL_compiling.cop_warnings = pWARN_ALL ;
3633 else if (PL_dowarn & G_WARN_ALL_OFF)
3634 PL_compiling.cop_warnings = pWARN_NONE ;
3636 PL_compiling.cop_warnings = pWARN_STD ;
3639 PL_compiling.cop_warnings =
3640 DUP_WARNINGS(oldcurcop->cop_warnings);
3641 cophh_free(CopHINTHASH_get(&PL_compiling));
3642 if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) {
3643 /* The label, if present, is the first entry on the chain. So rather
3644 than writing a blank label in front of it (which involves an
3645 allocation), just use the next entry in the chain. */
3646 PL_compiling.cop_hints_hash
3647 = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next);
3648 /* Check the assumption that this removed the label. */
3649 assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3652 PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash);
3656 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3658 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3659 * so honour CATCH_GET and trap it here if necessary */
3661 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3663 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3664 SV **newsp; /* Used by POPBLOCK. */
3666 I32 optype; /* Used by POPEVAL. */
3671 PERL_UNUSED_VAR(newsp);
3672 PERL_UNUSED_VAR(optype);
3674 /* note that if yystatus == 3, then the EVAL CX block has already
3675 * been popped, and various vars restored */
3677 if (yystatus != 3) {
3679 op_free(PL_eval_root);
3680 PL_eval_root = NULL;
3682 SP = PL_stack_base + POPMARK; /* pop original mark */
3684 POPBLOCK(cx,PL_curpm);
3686 namesv = cx->blk_eval.old_namesv;
3688 /* POPBLOCK renders LEAVE_with_name("evalcomp") unnecessary. */
3689 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3694 /* If cx is still NULL, it means that we didn't go in the
3695 * POPEVAL branch. */
3696 cx = &cxstack[cxstack_ix];
3697 assert(CxTYPE(cx) == CXt_EVAL);
3698 namesv = cx->blk_eval.old_namesv;
3700 (void)hv_store(GvHVn(PL_incgv),
3701 SvPVX_const(namesv),
3702 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
3704 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
3707 : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
3710 if (yystatus != 3) {
3711 POPBLOCK(cx,PL_curpm);
3714 Perl_croak(aTHX_ "%"SVf"Compilation failed in regexp",
3717 : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
3720 if (!*(SvPVx_nolen_const(ERRSV))) {
3721 sv_setpvs(ERRSV, "Compilation error");
3724 if (gimme != G_ARRAY) PUSHs(&PL_sv_undef);
3728 else if (!startop) LEAVE_with_name("evalcomp");
3729 CopLINE_set(&PL_compiling, 0);
3731 *startop = PL_eval_root;
3733 SAVEFREEOP(PL_eval_root);
3735 DEBUG_x(dump_eval());
3737 /* Register with debugger: */
3738 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3739 CV * const cv = get_cvs("DB::postponed", 0);
3743 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3745 call_sv(MUTABLE_SV(cv), G_DISCARD);
3749 if (PL_unitcheckav) {
3750 OP *es = PL_eval_start;
3751 call_list(PL_scopestack_ix, PL_unitcheckav);
3755 /* compiled okay, so do it */
3757 CvDEPTH(evalcv) = 1;
3758 SP = PL_stack_base + POPMARK; /* pop original mark */
3759 PL_op = saveop; /* The caller may need it. */
3760 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3767 S_check_type_and_open(pTHX_ SV *name)
3770 const char *p = SvPV_nolen_const(name);
3771 const int st_rc = PerlLIO_stat(p, &st);
3773 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3775 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3779 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3780 return PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3782 return PerlIO_open(p, PERL_SCRIPT_MODE);
3786 #ifndef PERL_DISABLE_PMC
3788 S_doopen_pm(pTHX_ SV *name)
3791 const char *p = SvPV_const(name, namelen);
3793 PERL_ARGS_ASSERT_DOOPEN_PM;
3795 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3796 SV *const pmcsv = sv_newmortal();
3799 SvSetSV_nosteal(pmcsv,name);
3800 sv_catpvn(pmcsv, "c", 1);
3802 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3803 return check_type_and_open(pmcsv);
3805 return check_type_and_open(name);
3808 # define doopen_pm(name) check_type_and_open(name)
3809 #endif /* !PERL_DISABLE_PMC */
3814 register PERL_CONTEXT *cx;
3821 int vms_unixname = 0;
3823 const char *tryname = NULL;
3825 const I32 gimme = GIMME_V;
3826 int filter_has_file = 0;
3827 PerlIO *tryrsfp = NULL;
3828 SV *filter_cache = NULL;
3829 SV *filter_state = NULL;
3830 SV *filter_sub = NULL;
3836 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3837 sv = sv_2mortal(new_version(sv));
3838 if (!sv_derived_from(PL_patchlevel, "version"))
3839 upg_version(PL_patchlevel, TRUE);
3840 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3841 if ( vcmp(sv,PL_patchlevel) <= 0 )
3842 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3843 SVfARG(sv_2mortal(vnormal(sv))),
3844 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3848 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3851 SV * const req = SvRV(sv);
3852 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3854 /* get the left hand term */
3855 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3857 first = SvIV(*av_fetch(lav,0,0));
3858 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3859 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3860 || av_len(lav) > 1 /* FP with > 3 digits */
3861 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3863 DIE(aTHX_ "Perl %"SVf" required--this is only "
3865 SVfARG(sv_2mortal(vnormal(req))),
3866 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3869 else { /* probably 'use 5.10' or 'use 5.8' */
3874 second = SvIV(*av_fetch(lav,1,0));
3876 second /= second >= 600 ? 100 : 10;
3877 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3878 (int)first, (int)second);
3879 upg_version(hintsv, TRUE);
3881 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3882 "--this is only %"SVf", stopped",
3883 SVfARG(sv_2mortal(vnormal(req))),
3884 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3885 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3893 name = SvPV_const(sv, len);
3894 if (!(name && len > 0 && *name))
3895 DIE(aTHX_ "Null filename used");
3896 TAINT_PROPER("require");
3900 /* The key in the %ENV hash is in the syntax of file passed as the argument
3901 * usually this is in UNIX format, but sometimes in VMS format, which
3902 * can result in a module being pulled in more than once.
3903 * To prevent this, the key must be stored in UNIX format if the VMS
3904 * name can be translated to UNIX.
3906 if ((unixname = tounixspec(name, NULL)) != NULL) {
3907 unixlen = strlen(unixname);
3913 /* if not VMS or VMS name can not be translated to UNIX, pass it
3916 unixname = (char *) name;
3919 if (PL_op->op_type == OP_REQUIRE) {
3920 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3921 unixname, unixlen, 0);
3923 if (*svp != &PL_sv_undef)
3926 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3927 "Compilation failed in require", unixname);
3931 /* prepare to compile file */
3933 if (path_is_absolute(name)) {
3934 /* At this point, name is SvPVX(sv) */
3936 tryrsfp = doopen_pm(sv);
3939 AV * const ar = GvAVn(PL_incgv);
3945 namesv = newSV_type(SVt_PV);
3946 for (i = 0; i <= AvFILL(ar); i++) {
3947 SV * const dirsv = *av_fetch(ar, i, TRUE);
3949 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3956 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3957 && !sv_isobject(loader))
3959 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3962 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3963 PTR2UV(SvRV(dirsv)), name);
3964 tryname = SvPVX_const(namesv);
3967 ENTER_with_name("call_INC");
3975 if (sv_isobject(loader))
3976 count = call_method("INC", G_ARRAY);
3978 count = call_sv(loader, G_ARRAY);
3988 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3989 && !isGV_with_GP(SvRV(arg))) {
3990 filter_cache = SvRV(arg);
3991 SvREFCNT_inc_simple_void_NN(filter_cache);
3998 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
4002 if (isGV_with_GP(arg)) {
4003 IO * const io = GvIO((const GV *)arg);
4008 tryrsfp = IoIFP(io);
4009 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
4010 PerlIO_close(IoOFP(io));
4021 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
4023 SvREFCNT_inc_simple_void_NN(filter_sub);
4026 filter_state = SP[i];
4027 SvREFCNT_inc_simple_void(filter_state);
4031 if (!tryrsfp && (filter_cache || filter_sub)) {
4032 tryrsfp = PerlIO_open(BIT_BUCKET,
4040 LEAVE_with_name("call_INC");
4042 /* Adjust file name if the hook has set an %INC entry.
4043 This needs to happen after the FREETMPS above. */
4044 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
4046 tryname = SvPV_nolen_const(*svp);
4053 filter_has_file = 0;
4055 SvREFCNT_dec(filter_cache);
4056 filter_cache = NULL;
4059 SvREFCNT_dec(filter_state);
4060 filter_state = NULL;
4063 SvREFCNT_dec(filter_sub);
4068 if (!path_is_absolute(name)
4074 dir = SvPV_const(dirsv, dirlen);
4082 if ((unixdir = tounixpath(dir, NULL)) == NULL)
4084 sv_setpv(namesv, unixdir);
4085 sv_catpv(namesv, unixname);
4087 # ifdef __SYMBIAN32__
4088 if (PL_origfilename[0] &&
4089 PL_origfilename[1] == ':' &&
4090 !(dir[0] && dir[1] == ':'))
4091 Perl_sv_setpvf(aTHX_ namesv,
4096 Perl_sv_setpvf(aTHX_ namesv,
4100 /* The equivalent of
4101 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
4102 but without the need to parse the format string, or
4103 call strlen on either pointer, and with the correct
4104 allocation up front. */
4106 char *tmp = SvGROW(namesv, dirlen + len + 2);
4108 memcpy(tmp, dir, dirlen);
4111 /* name came from an SV, so it will have a '\0' at the
4112 end that we can copy as part of this memcpy(). */
4113 memcpy(tmp, name, len + 1);
4115 SvCUR_set(namesv, dirlen + len + 1);
4120 TAINT_PROPER("require");
4121 tryname = SvPVX_const(namesv);
4122 tryrsfp = doopen_pm(namesv);
4124 if (tryname[0] == '.' && tryname[1] == '/') {
4126 while (*++tryname == '/');
4130 else if (errno == EMFILE)
4131 /* no point in trying other paths if out of handles */
4140 if (PL_op->op_type == OP_REQUIRE) {
4141 if(errno == EMFILE) {
4142 /* diag_listed_as: Can't locate %s */
4143 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno));
4145 if (namesv) { /* did we lookup @INC? */
4146 AV * const ar = GvAVn(PL_incgv);
4148 SV *const inc = newSVpvs_flags("", SVs_TEMP);
4149 for (i = 0; i <= AvFILL(ar); i++) {
4150 sv_catpvs(inc, " ");
4151 sv_catsv(inc, *av_fetch(ar, i, TRUE));
4154 /* diag_listed_as: Can't locate %s */
4156 "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")",
4158 (memEQ(name + len - 2, ".h", 3)
4159 ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""),
4160 (memEQ(name + len - 3, ".ph", 4)
4161 ? " (did you run h2ph?)" : ""),
4166 DIE(aTHX_ "Can't locate %s", name);
4172 SETERRNO(0, SS_NORMAL);
4174 /* Assume success here to prevent recursive requirement. */
4175 /* name is never assigned to again, so len is still strlen(name) */
4176 /* Check whether a hook in @INC has already filled %INC */
4178 (void)hv_store(GvHVn(PL_incgv),
4179 unixname, unixlen, newSVpv(tryname,0),0);
4181 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
4183 (void)hv_store(GvHVn(PL_incgv),
4184 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
4187 ENTER_with_name("eval");
4189 SAVECOPFILE_FREE(&PL_compiling);
4190 CopFILE_set(&PL_compiling, tryname);
4191 lex_start(NULL, tryrsfp, 0);
4193 if (filter_sub || filter_cache) {
4194 /* We can use the SvPV of the filter PVIO itself as our cache, rather
4195 than hanging another SV from it. In turn, filter_add() optionally
4196 takes the SV to use as the filter (or creates a new SV if passed
4197 NULL), so simply pass in whatever value filter_cache has. */
4198 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
4199 IoLINES(datasv) = filter_has_file;
4200 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
4201 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
4204 /* switch to eval mode */
4205 PUSHBLOCK(cx, CXt_EVAL, SP);
4207 cx->blk_eval.retop = PL_op->op_next;
4209 SAVECOPLINE(&PL_compiling);
4210 CopLINE_set(&PL_compiling, 0);
4214 /* Store and reset encoding. */
4215 encoding = PL_encoding;
4218 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq, NULL))
4219 op = DOCATCH(PL_eval_start);
4221 op = PL_op->op_next;
4223 /* Restore encoding. */
4224 PL_encoding = encoding;
4229 /* This is a op added to hold the hints hash for
4230 pp_entereval. The hash can be modified by the code
4231 being eval'ed, so we return a copy instead. */
4237 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
4245 register PERL_CONTEXT *cx;
4247 const I32 gimme = GIMME_V;
4248 const U32 was = PL_breakable_sub_gen;
4249 char tbuf[TYPE_DIGITS(long) + 12];
4250 bool saved_delete = FALSE;
4251 char *tmpbuf = tbuf;
4254 U32 seq, lex_flags = 0;
4255 HV *saved_hh = NULL;
4256 const bool bytes = PL_op->op_private & OPpEVAL_BYTES;
4258 if (PL_op->op_private & OPpEVAL_HAS_HH) {
4259 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
4261 else if (PL_hints & HINT_LOCALIZE_HH || (
4262 PL_op->op_private & OPpEVAL_COPHH
4263 && PL_curcop->cop_hints & HINT_LOCALIZE_HH
4265 saved_hh = cop_hints_2hv(PL_curcop, 0);
4266 hv_magic(saved_hh, NULL, PERL_MAGIC_hints);
4270 /* make sure we've got a plain PV (no overload etc) before testing
4271 * for taint. Making a copy here is probably overkill, but better
4272 * safe than sorry */
4274 const char * const p = SvPV_const(sv, len);
4276 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
4277 lex_flags |= LEX_START_COPIED;
4279 if (bytes && SvUTF8(sv))
4280 SvPVbyte_force(sv, len);
4282 else if (bytes && SvUTF8(sv)) {
4283 /* Don't modify someone else's scalar */
4286 (void)sv_2mortal(sv);
4287 SvPVbyte_force(sv,len);
4288 lex_flags |= LEX_START_COPIED;
4291 TAINT_IF(SvTAINTED(sv));
4292 TAINT_PROPER("eval");
4294 ENTER_with_name("eval");
4295 lex_start(sv, NULL, lex_flags | (PL_op->op_private & OPpEVAL_UNICODE
4296 ? LEX_IGNORE_UTF8_HINTS
4297 : bytes ? LEX_EVALBYTES : LEX_START_SAME_FILTER
4302 /* switch to eval mode */
4304 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
4305 SV * const temp_sv = sv_newmortal();
4306 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
4307 (unsigned long)++PL_evalseq,
4308 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4309 tmpbuf = SvPVX(temp_sv);
4310 len = SvCUR(temp_sv);
4313 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
4314 SAVECOPFILE_FREE(&PL_compiling);
4315 CopFILE_set(&PL_compiling, tmpbuf+2);
4316 SAVECOPLINE(&PL_compiling);
4317 CopLINE_set(&PL_compiling, 1);
4318 /* special case: an eval '' executed within the DB package gets lexically
4319 * placed in the first non-DB CV rather than the current CV - this
4320 * allows the debugger to execute code, find lexicals etc, in the
4321 * scope of the code being debugged. Passing &seq gets find_runcv
4322 * to do the dirty work for us */
4323 runcv = find_runcv(&seq);
4325 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
4327 cx->blk_eval.retop = PL_op->op_next;
4329 /* prepare to compile string */
4331 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
4332 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
4334 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
4335 deleting the eval's FILEGV from the stash before gv_check() runs
4336 (i.e. before run-time proper). To work around the coredump that
4337 ensues, we always turn GvMULTI_on for any globals that were
4338 introduced within evals. See force_ident(). GSAR 96-10-12 */
4339 char *const safestr = savepvn(tmpbuf, len);
4340 SAVEDELETE(PL_defstash, safestr, len);
4341 saved_delete = TRUE;
4346 if (doeval(gimme, NULL, runcv, seq, saved_hh)) {
4347 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4348 ? (PERLDB_LINE || PERLDB_SAVESRC)
4349 : PERLDB_SAVESRC_NOSUBS) {
4350 /* Retain the filegv we created. */
4351 } else if (!saved_delete) {
4352 char *const safestr = savepvn(tmpbuf, len);
4353 SAVEDELETE(PL_defstash, safestr, len);
4355 return DOCATCH(PL_eval_start);
4357 /* We have already left the scope set up earlier thanks to the LEAVE
4359 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4360 ? (PERLDB_LINE || PERLDB_SAVESRC)
4361 : PERLDB_SAVESRC_INVALID) {
4362 /* Retain the filegv we created. */
4363 } else if (!saved_delete) {
4364 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
4366 return PL_op->op_next;
4376 register PERL_CONTEXT *cx;
4378 const U8 save_flags = PL_op -> op_flags;
4386 namesv = cx->blk_eval.old_namesv;
4387 retop = cx->blk_eval.retop;
4388 evalcv = cx->blk_eval.cv;
4391 SP = adjust_stack_on_leave((gimme == G_VOID) ? SP : newsp, SP, newsp,
4393 PL_curpm = newpm; /* Don't pop $1 et al till now */
4396 assert(CvDEPTH(evalcv) == 1);
4398 CvDEPTH(evalcv) = 0;
4400 if (optype == OP_REQUIRE &&
4401 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4403 /* Unassume the success we assumed earlier. */
4404 (void)hv_delete(GvHVn(PL_incgv),
4405 SvPVX_const(namesv),
4406 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
4408 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4410 /* die_unwind() did LEAVE, or we won't be here */
4413 LEAVE_with_name("eval");
4414 if (!(save_flags & OPf_SPECIAL)) {
4422 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4423 close to the related Perl_create_eval_scope. */
4425 Perl_delete_eval_scope(pTHX)
4430 register PERL_CONTEXT *cx;
4436 LEAVE_with_name("eval_scope");
4437 PERL_UNUSED_VAR(newsp);
4438 PERL_UNUSED_VAR(gimme);
4439 PERL_UNUSED_VAR(optype);
4442 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4443 also needed by Perl_fold_constants. */
4445 Perl_create_eval_scope(pTHX_ U32 flags)
4448 const I32 gimme = GIMME_V;
4450 ENTER_with_name("eval_scope");
4453 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4456 PL_in_eval = EVAL_INEVAL;
4457 if (flags & G_KEEPERR)
4458 PL_in_eval |= EVAL_KEEPERR;
4461 if (flags & G_FAKINGEVAL) {
4462 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4470 PERL_CONTEXT * const cx = create_eval_scope(0);
4471 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4472 return DOCATCH(PL_op->op_next);
4481 register PERL_CONTEXT *cx;
4487 PERL_UNUSED_VAR(optype);
4490 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
4491 PL_curpm = newpm; /* Don't pop $1 et al till now */
4493 LEAVE_with_name("eval_scope");
4501 register PERL_CONTEXT *cx;
4502 const I32 gimme = GIMME_V;
4504 ENTER_with_name("given");
4507 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
4508 sv_setsv_mg(PAD_SV(PL_op->op_targ), POPs);
4510 PUSHBLOCK(cx, CXt_GIVEN, SP);
4519 register PERL_CONTEXT *cx;
4523 PERL_UNUSED_CONTEXT;
4526 assert(CxTYPE(cx) == CXt_GIVEN);
4529 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
4530 PL_curpm = newpm; /* Don't pop $1 et al till now */
4532 LEAVE_with_name("given");
4536 /* Helper routines used by pp_smartmatch */
4538 S_make_matcher(pTHX_ REGEXP *re)
4541 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4543 PERL_ARGS_ASSERT_MAKE_MATCHER;
4545 PM_SETRE(matcher, ReREFCNT_inc(re));
4547 SAVEFREEOP((OP *) matcher);
4548 ENTER_with_name("matcher"); SAVETMPS;
4554 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4559 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4561 PL_op = (OP *) matcher;
4564 (void) Perl_pp_match(aTHX);
4566 return (SvTRUEx(POPs));
4570 S_destroy_matcher(pTHX_ PMOP *matcher)
4574 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4575 PERL_UNUSED_ARG(matcher);
4578 LEAVE_with_name("matcher");
4581 /* Do a smart match */
4584 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4585 return do_smartmatch(NULL, NULL, 0);
4588 /* This version of do_smartmatch() implements the
4589 * table of smart matches that is found in perlsyn.
4592 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied)
4597 bool object_on_left = FALSE;
4598 SV *e = TOPs; /* e is for 'expression' */
4599 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4601 /* Take care only to invoke mg_get() once for each argument.
4602 * Currently we do this by copying the SV if it's magical. */
4604 if (!copied && SvGMAGICAL(d))
4605 d = sv_mortalcopy(d);
4612 e = sv_mortalcopy(e);
4614 /* First of all, handle overload magic of the rightmost argument */
4617 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4618 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4620 tmpsv = amagic_call(d, e, smart_amg, AMGf_noleft);
4627 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4630 SP -= 2; /* Pop the values */
4635 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4642 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4643 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4644 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4646 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4647 object_on_left = TRUE;
4650 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4652 if (object_on_left) {
4653 goto sm_any_sub; /* Treat objects like scalars */
4655 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4656 /* Test sub truth for each key */
4658 bool andedresults = TRUE;
4659 HV *hv = (HV*) SvRV(d);
4660 I32 numkeys = hv_iterinit(hv);
4661 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4664 while ( (he = hv_iternext(hv)) ) {
4665 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4666 ENTER_with_name("smartmatch_hash_key_test");
4669 PUSHs(hv_iterkeysv(he));
4671 c = call_sv(e, G_SCALAR);
4674 andedresults = FALSE;
4676 andedresults = SvTRUEx(POPs) && andedresults;
4678 LEAVE_with_name("smartmatch_hash_key_test");
4685 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4686 /* Test sub truth for each element */
4688 bool andedresults = TRUE;
4689 AV *av = (AV*) SvRV(d);
4690 const I32 len = av_len(av);
4691 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4694 for (i = 0; i <= len; ++i) {
4695 SV * const * const svp = av_fetch(av, i, FALSE);
4696 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4697 ENTER_with_name("smartmatch_array_elem_test");
4703 c = call_sv(e, G_SCALAR);
4706 andedresults = FALSE;
4708 andedresults = SvTRUEx(POPs) && andedresults;
4710 LEAVE_with_name("smartmatch_array_elem_test");
4719 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4720 ENTER_with_name("smartmatch_coderef");
4725 c = call_sv(e, G_SCALAR);
4729 else if (SvTEMP(TOPs))
4730 SvREFCNT_inc_void(TOPs);
4732 LEAVE_with_name("smartmatch_coderef");
4737 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4738 if (object_on_left) {
4739 goto sm_any_hash; /* Treat objects like scalars */
4741 else if (!SvOK(d)) {
4742 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4745 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4746 /* Check that the key-sets are identical */
4748 HV *other_hv = MUTABLE_HV(SvRV(d));
4750 bool other_tied = FALSE;
4751 U32 this_key_count = 0,
4752 other_key_count = 0;
4753 HV *hv = MUTABLE_HV(SvRV(e));
4755 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
4756 /* Tied hashes don't know how many keys they have. */
4757 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
4760 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
4761 HV * const temp = other_hv;
4766 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
4769 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
4772 /* The hashes have the same number of keys, so it suffices
4773 to check that one is a subset of the other. */
4774 (void) hv_iterinit(hv);
4775 while ( (he = hv_iternext(hv)) ) {
4776 SV *key = hv_iterkeysv(he);
4778 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
4781 if(!hv_exists_ent(other_hv, key, 0)) {
4782 (void) hv_iterinit(hv); /* reset iterator */
4788 (void) hv_iterinit(other_hv);
4789 while ( hv_iternext(other_hv) )
4793 other_key_count = HvUSEDKEYS(other_hv);
4795 if (this_key_count != other_key_count)
4800 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4801 AV * const other_av = MUTABLE_AV(SvRV(d));
4802 const I32 other_len = av_len(other_av) + 1;
4804 HV *hv = MUTABLE_HV(SvRV(e));
4806 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n"));
4807 for (i = 0; i < other_len; ++i) {
4808 SV ** const svp = av_fetch(other_av, i, FALSE);
4809 DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n"));
4810 if (svp) { /* ??? When can this not happen? */
4811 if (hv_exists_ent(hv, *svp, 0))
4817 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4818 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n"));
4821 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4823 HV *hv = MUTABLE_HV(SvRV(e));
4825 (void) hv_iterinit(hv);
4826 while ( (he = hv_iternext(hv)) ) {
4827 DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n"));
4828 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
4829 (void) hv_iterinit(hv);
4830 destroy_matcher(matcher);
4834 destroy_matcher(matcher);
4840 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n"));
4841 if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0))
4848 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) {
4849 if (object_on_left) {
4850 goto sm_any_array; /* Treat objects like scalars */
4852 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4853 AV * const other_av = MUTABLE_AV(SvRV(e));
4854 const I32 other_len = av_len(other_av) + 1;
4857 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n"));
4858 for (i = 0; i < other_len; ++i) {
4859 SV ** const svp = av_fetch(other_av, i, FALSE);
4861 DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n"));
4862 if (svp) { /* ??? When can this not happen? */
4863 if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0))
4869 if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4870 AV *other_av = MUTABLE_AV(SvRV(d));
4871 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n"));
4872 if (av_len(MUTABLE_AV(SvRV(e))) != av_len(other_av))
4876 const I32 other_len = av_len(other_av);
4878 if (NULL == seen_this) {
4879 seen_this = newHV();
4880 (void) sv_2mortal(MUTABLE_SV(seen_this));
4882 if (NULL == seen_other) {
4883 seen_other = newHV();
4884 (void) sv_2mortal(MUTABLE_SV(seen_other));
4886 for(i = 0; i <= other_len; ++i) {
4887 SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4888 SV * const * const other_elem = av_fetch(other_av, i, FALSE);
4890 if (!this_elem || !other_elem) {
4891 if ((this_elem && SvOK(*this_elem))
4892 || (other_elem && SvOK(*other_elem)))
4895 else if (hv_exists_ent(seen_this,
4896 sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) ||
4897 hv_exists_ent(seen_other,
4898 sv_2mortal(newSViv(PTR2IV(*other_elem))), 0))
4900 if (*this_elem != *other_elem)
4904 (void)hv_store_ent(seen_this,
4905 sv_2mortal(newSViv(PTR2IV(*this_elem))),
4907 (void)hv_store_ent(seen_other,
4908 sv_2mortal(newSViv(PTR2IV(*other_elem))),
4914 DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n"));
4915 (void) do_smartmatch(seen_this, seen_other, 0);
4917 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4926 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4927 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n"));
4930 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4931 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4934 for(i = 0; i <= this_len; ++i) {
4935 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4936 DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n"));
4937 if (svp && matcher_matches_sv(matcher, *svp)) {
4938 destroy_matcher(matcher);
4942 destroy_matcher(matcher);
4946 else if (!SvOK(d)) {
4947 /* undef ~~ array */
4948 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4951 DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n"));
4952 for (i = 0; i <= this_len; ++i) {
4953 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4954 DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n"));
4955 if (!svp || !SvOK(*svp))
4964 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4966 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n"));
4967 for (i = 0; i <= this_len; ++i) {
4968 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4975 /* infinite recursion isn't supposed to happen here */
4976 DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n"));
4977 (void) do_smartmatch(NULL, NULL, 1);
4979 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4988 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) {
4989 if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4990 SV *t = d; d = e; e = t;
4991 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n"));
4994 else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4995 SV *t = d; d = e; e = t;
4996 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n"));
4997 goto sm_regex_array;
5000 PMOP * const matcher = make_matcher((REGEXP*) SvRV(e));
5002 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n"));
5004 PUSHs(matcher_matches_sv(matcher, d)
5007 destroy_matcher(matcher);
5012 /* See if there is overload magic on left */
5013 else if (object_on_left && SvAMAGIC(d)) {
5015 DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n"));
5016 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
5019 tmpsv = amagic_call(d, e, smart_amg, AMGf_noright);
5027 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n"));
5030 else if (!SvOK(d)) {
5031 /* undef ~~ scalar ; we already know that the scalar is SvOK */
5032 DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n"));
5037 if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) {
5038 DEBUG_M(if (SvNIOK(e))
5039 Perl_deb(aTHX_ " applying rule Any-Num\n");
5041 Perl_deb(aTHX_ " applying rule Num-numish\n");
5043 /* numeric comparison */
5046 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
5047 (void) Perl_pp_i_eq(aTHX);
5049 (void) Perl_pp_eq(aTHX);
5057 /* As a last resort, use string comparison */
5058 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n"));
5061 return Perl_pp_seq(aTHX);
5067 register PERL_CONTEXT *cx;
5068 const I32 gimme = GIMME_V;
5070 /* This is essentially an optimization: if the match
5071 fails, we don't want to push a context and then
5072 pop it again right away, so we skip straight
5073 to the op that follows the leavewhen.
5074 RETURNOP calls PUTBACK which restores the stack pointer after the POPs.
5076 if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs))
5077 RETURNOP(cLOGOP->op_other->op_next);
5079 ENTER_with_name("when");
5082 PUSHBLOCK(cx, CXt_WHEN, SP);
5092 register PERL_CONTEXT *cx;
5097 cxix = dopoptogiven(cxstack_ix);
5099 /* diag_listed_as: Can't "when" outside a topicalizer */
5100 DIE(aTHX_ "Can't \"%s\" outside a topicalizer",
5101 PL_op->op_flags & OPf_SPECIAL ? "default" : "when");
5104 assert(CxTYPE(cx) == CXt_WHEN);
5107 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
5108 PL_curpm = newpm; /* pop $1 et al */
5110 LEAVE_with_name("when");
5112 if (cxix < cxstack_ix)
5115 cx = &cxstack[cxix];
5117 if (CxFOREACH(cx)) {
5118 /* clear off anything above the scope we're re-entering */
5119 I32 inner = PL_scopestack_ix;
5122 if (PL_scopestack_ix < inner)
5123 leave_scope(PL_scopestack[PL_scopestack_ix]);
5124 PL_curcop = cx->blk_oldcop;
5126 return cx->blk_loop.my_op->op_nextop;
5129 RETURNOP(cx->blk_givwhen.leave_op);
5136 register PERL_CONTEXT *cx;
5141 PERL_UNUSED_VAR(gimme);
5143 cxix = dopoptowhen(cxstack_ix);
5145 DIE(aTHX_ "Can't \"continue\" outside a when block");
5147 if (cxix < cxstack_ix)
5151 assert(CxTYPE(cx) == CXt_WHEN);
5154 PL_curpm = newpm; /* pop $1 et al */
5156 LEAVE_with_name("when");
5157 RETURNOP(cx->blk_givwhen.leave_op->op_next);
5164 register PERL_CONTEXT *cx;
5166 cxix = dopoptogiven(cxstack_ix);
5168 DIE(aTHX_ "Can't \"break\" outside a given block");
5170 cx = &cxstack[cxix];
5172 DIE(aTHX_ "Can't \"break\" in a loop topicalizer");
5174 if (cxix < cxstack_ix)
5177 /* Restore the sp at the time we entered the given block */
5180 return cx->blk_givwhen.leave_op;
5184 S_doparseform(pTHX_ SV *sv)
5187 register char *s = SvPV(sv, len);
5188 register char *send;
5189 register char *base = NULL; /* start of current field */
5190 register I32 skipspaces = 0; /* number of contiguous spaces seen */
5191 bool noblank = FALSE; /* ~ or ~~ seen on this line */
5192 bool repeat = FALSE; /* ~~ seen on this line */
5193 bool postspace = FALSE; /* a text field may need right padding */
5196 U32 *linepc = NULL; /* position of last FF_LINEMARK */
5198 bool ischop; /* it's a ^ rather than a @ */
5199 bool unchopnum = FALSE; /* at least one @ (i.e. non-chop) num field seen */
5200 int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */
5204 PERL_ARGS_ASSERT_DOPARSEFORM;
5207 Perl_croak(aTHX_ "Null picture in formline");
5209 if (SvTYPE(sv) >= SVt_PVMG) {
5210 /* This might, of course, still return NULL. */
5211 mg = mg_find(sv, PERL_MAGIC_fm);
5213 sv_upgrade(sv, SVt_PVMG);
5217 /* still the same as previously-compiled string? */
5218 SV *old = mg->mg_obj;
5219 if ( !(!!SvUTF8(old) ^ !!SvUTF8(sv))
5220 && len == SvCUR(old)
5221 && strnEQ(SvPVX(old), SvPVX(sv), len)
5223 DEBUG_f(PerlIO_printf(Perl_debug_log,"Re-using compiled format\n"));
5227 DEBUG_f(PerlIO_printf(Perl_debug_log, "Re-compiling format\n"));
5228 Safefree(mg->mg_ptr);
5234 DEBUG_f(PerlIO_printf(Perl_debug_log, "Compiling format\n"));
5235 mg = sv_magicext(sv, NULL, PERL_MAGIC_fm, &PL_vtbl_fm, NULL, 0);
5238 sv_copy = newSVpvn_utf8(s, len, SvUTF8(sv));
5239 s = SvPV(sv_copy, len); /* work on the copy, not the original */
5243 /* estimate the buffer size needed */
5244 for (base = s; s <= send; s++) {
5245 if (*s == '\n' || *s == '@' || *s == '^')
5251 Newx(fops, maxops, U32);
5256 *fpc++ = FF_LINEMARK;
5257 noblank = repeat = FALSE;
5275 case ' ': case '\t':
5282 } /* else FALL THROUGH */
5290 *fpc++ = FF_LITERAL;
5298 *fpc++ = (U32)skipspaces;
5302 *fpc++ = FF_NEWLINE;
5306 arg = fpc - linepc + 1;
5313 *fpc++ = FF_LINEMARK;
5314 noblank = repeat = FALSE;
5323 ischop = s[-1] == '^';
5329 arg = (s - base) - 1;
5331 *fpc++ = FF_LITERAL;
5337 if (*s == '*') { /* @* or ^* */
5339 *fpc++ = 2; /* skip the @* or ^* */
5341 *fpc++ = FF_LINESNGL;
5344 *fpc++ = FF_LINEGLOB;
5346 else if (*s == '#' || (*s == '.' && s[1] == '#')) { /* @###, ^### */
5347 arg = ischop ? FORM_NUM_BLANK : 0;
5352 const char * const f = ++s;
5355 arg |= FORM_NUM_POINT + (s - f);
5357 *fpc++ = s - base; /* fieldsize for FETCH */
5358 *fpc++ = FF_DECIMAL;
5360 unchopnum |= ! ischop;
5362 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
5363 arg = ischop ? FORM_NUM_BLANK : 0;
5365 s++; /* skip the '0' first */
5369 const char * const f = ++s;
5372 arg |= FORM_NUM_POINT + (s - f);
5374 *fpc++ = s - base; /* fieldsize for FETCH */
5375 *fpc++ = FF_0DECIMAL;
5377 unchopnum |= ! ischop;
5379 else { /* text field */
5381 bool ismore = FALSE;
5384 while (*++s == '>') ;
5385 prespace = FF_SPACE;
5387 else if (*s == '|') {
5388 while (*++s == '|') ;
5389 prespace = FF_HALFSPACE;
5394 while (*++s == '<') ;
5397 if (*s == '.' && s[1] == '.' && s[2] == '.') {
5401 *fpc++ = s - base; /* fieldsize for FETCH */
5403 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
5406 *fpc++ = (U32)prespace; /* add SPACE or HALFSPACE */
5420 assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */
5423 mg->mg_ptr = (char *) fops;
5424 mg->mg_len = arg * sizeof(U32);
5425 mg->mg_obj = sv_copy;
5426 mg->mg_flags |= MGf_REFCOUNTED;
5428 if (unchopnum && repeat)
5429 Perl_die(aTHX_ "Repeated format line will never terminate (~~ and @#)");
5436 S_num_overflow(NV value, I32 fldsize, I32 frcsize)
5438 /* Can value be printed in fldsize chars, using %*.*f ? */
5442 int intsize = fldsize - (value < 0 ? 1 : 0);
5444 if (frcsize & FORM_NUM_POINT)
5446 frcsize &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
5449 while (intsize--) pwr *= 10.0;
5450 while (frcsize--) eps /= 10.0;
5453 if (value + eps >= pwr)
5456 if (value - eps <= -pwr)
5463 S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
5466 SV * const datasv = FILTER_DATA(idx);
5467 const int filter_has_file = IoLINES(datasv);
5468 SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv));
5469 SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv));
5474 char *prune_from = NULL;
5475 bool read_from_cache = FALSE;
5478 PERL_ARGS_ASSERT_RUN_USER_FILTER;
5480 assert(maxlen >= 0);
5483 /* I was having segfault trouble under Linux 2.2.5 after a
5484 parse error occured. (Had to hack around it with a test
5485 for PL_parser->error_count == 0.) Solaris doesn't segfault --
5486 not sure where the trouble is yet. XXX */
5489 SV *const cache = datasv;
5492 const char *cache_p = SvPV(cache, cache_len);
5496 /* Running in block mode and we have some cached data already.
5498 if (cache_len >= umaxlen) {
5499 /* In fact, so much data we don't even need to call
5504 const char *const first_nl =
5505 (const char *)memchr(cache_p, '\n', cache_len);
5507 take = first_nl + 1 - cache_p;
5511 sv_catpvn(buf_sv, cache_p, take);
5512 sv_chop(cache, cache_p + take);
5513 /* Definitely not EOF */
5517 sv_catsv(buf_sv, cache);
5519 umaxlen -= cache_len;
5522 read_from_cache = TRUE;
5526 /* Filter API says that the filter appends to the contents of the buffer.
5527 Usually the buffer is "", so the details don't matter. But if it's not,
5528 then clearly what it contains is already filtered by this filter, so we
5529 don't want to pass it in a second time.
5530 I'm going to use a mortal in case the upstream filter croaks. */
5531 upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv))
5532 ? sv_newmortal() : buf_sv;
5533 SvUPGRADE(upstream, SVt_PV);
5535 if (filter_has_file) {
5536 status = FILTER_READ(idx+1, upstream, 0);
5539 if (filter_sub && status >= 0) {
5543 ENTER_with_name("call_filter_sub");
5548 DEFSV_set(upstream);
5552 PUSHs(filter_state);
5555 count = call_sv(filter_sub, G_SCALAR);
5567 LEAVE_with_name("call_filter_sub");
5570 if(SvOK(upstream)) {
5571 got_p = SvPV(upstream, got_len);
5573 if (got_len > umaxlen) {
5574 prune_from = got_p + umaxlen;
5577 char *const first_nl = (char *)memchr(got_p, '\n', got_len);
5578 if (first_nl && first_nl + 1 < got_p + got_len) {
5579 /* There's a second line here... */
5580 prune_from = first_nl + 1;
5585 /* Oh. Too long. Stuff some in our cache. */
5586 STRLEN cached_len = got_p + got_len - prune_from;
5587 SV *const cache = datasv;
5590 /* Cache should be empty. */
5591 assert(!SvCUR(cache));
5594 sv_setpvn(cache, prune_from, cached_len);
5595 /* If you ask for block mode, you may well split UTF-8 characters.
5596 "If it breaks, you get to keep both parts"
5597 (Your code is broken if you don't put them back together again
5598 before something notices.) */
5599 if (SvUTF8(upstream)) {
5602 SvCUR_set(upstream, got_len - cached_len);
5604 /* Can't yet be EOF */
5609 /* If they are at EOF but buf_sv has something in it, then they may never
5610 have touched the SV upstream, so it may be undefined. If we naively
5611 concatenate it then we get a warning about use of uninitialised value.
5613 if (upstream != buf_sv && (SvOK(upstream) || SvGMAGICAL(upstream))) {
5614 sv_catsv(buf_sv, upstream);
5618 IoLINES(datasv) = 0;
5620 SvREFCNT_dec(filter_state);
5621 IoTOP_GV(datasv) = NULL;
5624 SvREFCNT_dec(filter_sub);
5625 IoBOTTOM_GV(datasv) = NULL;
5627 filter_del(S_run_user_filter);
5629 if (status == 0 && read_from_cache) {
5630 /* If we read some data from the cache (and by getting here it implies
5631 that we emptied the cache) then we aren't yet at EOF, and mustn't
5632 report that to our caller. */
5638 /* perhaps someone can come up with a better name for
5639 this? it is not really "absolute", per se ... */
5641 S_path_is_absolute(const char *name)
5643 PERL_ARGS_ASSERT_PATH_IS_ABSOLUTE;
5645 if (PERL_FILE_IS_ABSOLUTE(name)
5647 || (*name == '.' && ((name[1] == '/' ||
5648 (name[1] == '.' && name[2] == '/'))
5649 || (name[1] == '\\' ||
5650 ( name[1] == '.' && name[2] == '\\')))
5653 || (*name == '.' && (name[1] == '/' ||
5654 (name[1] == '.' && name[2] == '/')))
5666 * c-indentation-style: bsd
5668 * indent-tabs-mode: t
5671 * ex: set ts=8 sts=4 sw=4 noet: