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) {
80 PMOP *pm = (PMOP*)cLOGOP->op_other;
85 const regexp_engine *eng;
86 bool is_bare_re= FALSE;
88 if (PL_op->op_flags & OPf_STACKED) {
98 /* prevent recompiling under /o and ithreads. */
99 #if defined(USE_ITHREADS)
100 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
107 assert (re != (REGEXP*) &PL_sv_undef);
108 eng = re ? RX_ENGINE(re) : current_re_engine();
111 In the below logic: these are basically the same - check if this regcomp is part of a split.
113 (PL_op->op_pmflags & PMf_split )
114 (PL_op->op_next->op_type == OP_PUSHRE)
116 We could add a new mask for this and copy the PMf_split, if we did
117 some bit definition fiddling first.
119 For now we leave this
122 new_re = (eng->op_comp
124 : &Perl_re_op_compile
125 )(aTHX_ args, nargs, pm->op_code_list, eng, re,
127 (pm->op_pmflags & RXf_PMf_FLAGCOPYMASK),
129 (PL_op->op_flags & OPf_SPECIAL ? PMf_USE_RE_EVAL : 0));
131 if (pm->op_pmflags & PMf_HAS_CV)
132 ReANY(new_re)->qr_anoncv
133 = (CV*) SvREFCNT_inc(PAD_SV(PL_op->op_targ));
137 /* The match's LHS's get-magic might need to access this op's regexp
138 (e.g. $' =~ /$re/ while foo; see bug 70764). So we must call
139 get-magic now before we replace the regexp. Hopefully this hack can
140 be replaced with the approach described at
141 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03/msg122415.html
143 if (pm->op_type == OP_MATCH) {
145 const bool was_tainted = TAINT_get;
146 if (pm->op_flags & OPf_STACKED)
148 else if (pm->op_private & OPpTARGET_MY)
149 lhs = PAD_SV(pm->op_targ);
152 /* Restore the previous value of PL_tainted (which may have been
153 modified by get-magic), to avoid incorrectly setting the
154 RXf_TAINTED flag with RX_TAINT_on further down. */
155 TAINT_set(was_tainted);
157 PERL_UNUSED_VAR(was_tainted);
160 tmp = reg_temp_copy(NULL, new_re);
161 ReREFCNT_dec(new_re);
167 PM_SETRE(pm, new_re);
171 #ifndef INCOMPLETE_TAINTS
172 if (TAINTING_get && TAINT_get) {
173 SvTAINTED_on((SV*)new_re);
178 #if !defined(USE_ITHREADS)
179 /* can't change the optree at runtime either */
180 /* PMf_KEEP is handled differently under threads to avoid these problems */
181 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
183 if (pm->op_pmflags & PMf_KEEP) {
184 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
185 cLOGOP->op_first->op_next = PL_op->op_next;
198 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
199 PMOP * const pm = (PMOP*) cLOGOP->op_other;
200 SV * const dstr = cx->sb_dstr;
203 char *orig = cx->sb_orig;
204 REGEXP * const rx = cx->sb_rx;
206 REGEXP *old = PM_GETRE(pm);
213 PM_SETRE(pm,ReREFCNT_inc(rx));
216 rxres_restore(&cx->sb_rxres, rx);
218 if (cx->sb_iters++) {
219 const I32 saviters = cx->sb_iters;
220 if (cx->sb_iters > cx->sb_maxiters)
221 DIE(aTHX_ "Substitution loop");
223 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
225 /* See "how taint works" above pp_subst() */
227 cx->sb_rxtainted |= SUBST_TAINT_REPL;
228 sv_catsv_nomg(dstr, POPs);
229 if (CxONCE(cx) || s < orig ||
230 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
231 (s == m), cx->sb_targ, NULL,
232 (REXEC_IGNOREPOS|REXEC_NOT_FIRST|REXEC_FAIL_ON_UNDERFLOW)))
234 SV *targ = cx->sb_targ;
236 assert(cx->sb_strend >= s);
237 if(cx->sb_strend > s) {
238 if (DO_UTF8(dstr) && !SvUTF8(targ))
239 sv_catpvn_nomg_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
241 sv_catpvn_nomg(dstr, s, cx->sb_strend - s);
243 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
244 cx->sb_rxtainted |= SUBST_TAINT_PAT;
246 if (pm->op_pmflags & PMf_NONDESTRUCT) {
248 /* From here on down we're using the copy, and leaving the
249 original untouched. */
254 sv_force_normal_flags(targ, SV_COW_DROP_PV);
259 SvPV_set(targ, SvPVX(dstr));
260 SvCUR_set(targ, SvCUR(dstr));
261 SvLEN_set(targ, SvLEN(dstr));
264 SvPV_set(dstr, NULL);
267 mPUSHi(saviters - 1);
269 (void)SvPOK_only_UTF8(targ);
272 /* update the taint state of various various variables in
273 * preparation for final exit.
274 * See "how taint works" above pp_subst() */
276 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
277 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
278 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
280 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
282 if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET)
283 && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
285 SvTAINTED_on(TOPs); /* taint return value */
286 /* needed for mg_set below */
288 cBOOL(cx->sb_rxtainted &
289 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL))
293 /* PL_tainted must be correctly set for this mg_set */
296 LEAVE_SCOPE(cx->sb_oldsave);
299 RETURNOP(pm->op_next);
300 assert(0); /* NOTREACHED */
302 cx->sb_iters = saviters;
304 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
307 assert(!RX_SUBOFFSET(rx));
308 cx->sb_orig = orig = RX_SUBBEG(rx);
310 cx->sb_strend = s + (cx->sb_strend - m);
312 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
314 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
315 sv_catpvn_nomg_utf8_upgrade(dstr, s, m - s, nsv);
317 sv_catpvn_nomg(dstr, s, m-s);
319 cx->sb_s = RX_OFFS(rx)[0].end + orig;
320 { /* Update the pos() information. */
322 = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ;
324 if (!(mg = mg_find_mglob(sv))) {
325 mg = sv_magicext_mglob(sv);
327 mg->mg_len = m - orig;
330 (void)ReREFCNT_inc(rx);
331 /* update the taint state of various various variables in preparation
332 * for calling the code block.
333 * See "how taint works" above pp_subst() */
335 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
336 cx->sb_rxtainted |= SUBST_TAINT_PAT;
338 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
339 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
340 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
342 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
344 if (cx->sb_iters > 1 && (cx->sb_rxtainted &
345 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
346 SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT)
347 ? cx->sb_dstr : cx->sb_targ);
350 rxres_save(&cx->sb_rxres, rx);
352 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
356 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
361 PERL_ARGS_ASSERT_RXRES_SAVE;
364 if (!p || p[1] < RX_NPARENS(rx)) {
366 i = 7 + (RX_NPARENS(rx)+1) * 2;
368 i = 6 + (RX_NPARENS(rx)+1) * 2;
377 /* what (if anything) to free on croak */
378 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
379 RX_MATCH_COPIED_off(rx);
380 *p++ = RX_NPARENS(rx);
383 *p++ = PTR2UV(RX_SAVED_COPY(rx));
384 RX_SAVED_COPY(rx) = NULL;
387 *p++ = PTR2UV(RX_SUBBEG(rx));
388 *p++ = (UV)RX_SUBLEN(rx);
389 *p++ = (UV)RX_SUBOFFSET(rx);
390 *p++ = (UV)RX_SUBCOFFSET(rx);
391 for (i = 0; i <= RX_NPARENS(rx); ++i) {
392 *p++ = (UV)RX_OFFS(rx)[i].start;
393 *p++ = (UV)RX_OFFS(rx)[i].end;
398 S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
403 PERL_ARGS_ASSERT_RXRES_RESTORE;
406 RX_MATCH_COPY_FREE(rx);
407 RX_MATCH_COPIED_set(rx, *p);
409 RX_NPARENS(rx) = *p++;
412 if (RX_SAVED_COPY(rx))
413 SvREFCNT_dec (RX_SAVED_COPY(rx));
414 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
418 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
419 RX_SUBLEN(rx) = (I32)(*p++);
420 RX_SUBOFFSET(rx) = (I32)*p++;
421 RX_SUBCOFFSET(rx) = (I32)*p++;
422 for (i = 0; i <= RX_NPARENS(rx); ++i) {
423 RX_OFFS(rx)[i].start = (I32)(*p++);
424 RX_OFFS(rx)[i].end = (I32)(*p++);
429 S_rxres_free(pTHX_ void **rsp)
431 UV * const p = (UV*)*rsp;
433 PERL_ARGS_ASSERT_RXRES_FREE;
437 void *tmp = INT2PTR(char*,*p);
440 U32 i = 9 + p[1] * 2;
442 U32 i = 8 + p[1] * 2;
447 SvREFCNT_dec (INT2PTR(SV*,p[2]));
450 PoisonFree(p, i, sizeof(UV));
459 #define FORM_NUM_BLANK (1<<30)
460 #define FORM_NUM_POINT (1<<29)
464 dVAR; dSP; dMARK; dORIGMARK;
465 SV * const tmpForm = *++MARK;
466 SV *formsv; /* contains text of original format */
467 U32 *fpc; /* format ops program counter */
468 char *t; /* current append position in target string */
469 const char *f; /* current position in format string */
471 SV *sv = NULL; /* current item */
472 const char *item = NULL;/* string value of current item */
473 I32 itemsize = 0; /* length of current item, possibly truncated */
474 I32 fieldsize = 0; /* width of current field */
475 I32 lines = 0; /* number of lines that have been output */
476 bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
477 const char *chophere = NULL; /* where to chop current item */
478 STRLEN linemark = 0; /* pos of start of line in output */
480 bool gotsome = FALSE; /* seen at least one non-blank item on this line */
482 STRLEN linemax; /* estimate of output size in bytes */
483 bool item_is_utf8 = FALSE;
484 bool targ_is_utf8 = FALSE;
487 U8 *source; /* source of bytes to append */
488 STRLEN to_copy; /* how may bytes to append */
489 char trans; /* what chars to translate */
491 mg = doparseform(tmpForm);
493 fpc = (U32*)mg->mg_ptr;
494 /* the actual string the format was compiled from.
495 * with overload etc, this may not match tmpForm */
499 SvPV_force(PL_formtarget, len);
500 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
501 SvTAINTED_on(PL_formtarget);
502 if (DO_UTF8(PL_formtarget))
504 linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
505 t = SvGROW(PL_formtarget, len + linemax + 1);
506 /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */
508 f = SvPV_const(formsv, len);
512 const char *name = "???";
515 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
516 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
517 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
518 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
519 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
521 case FF_CHECKNL: name = "CHECKNL"; break;
522 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
523 case FF_SPACE: name = "SPACE"; break;
524 case FF_HALFSPACE: name = "HALFSPACE"; break;
525 case FF_ITEM: name = "ITEM"; break;
526 case FF_CHOP: name = "CHOP"; break;
527 case FF_LINEGLOB: name = "LINEGLOB"; break;
528 case FF_NEWLINE: name = "NEWLINE"; break;
529 case FF_MORE: name = "MORE"; break;
530 case FF_LINEMARK: name = "LINEMARK"; break;
531 case FF_END: name = "END"; break;
532 case FF_0DECIMAL: name = "0DECIMAL"; break;
533 case FF_LINESNGL: name = "LINESNGL"; break;
536 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
538 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
542 linemark = t - SvPVX(PL_formtarget);
552 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
568 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
571 SvTAINTED_on(PL_formtarget);
577 const char *s = item = SvPV_const(sv, len);
580 itemsize = sv_len_utf8(sv);
581 if (itemsize != (I32)len) {
583 if (itemsize > fieldsize) {
584 itemsize = fieldsize;
585 itembytes = itemsize;
586 sv_pos_u2b(sv, &itembytes, 0);
590 send = chophere = s + itembytes;
600 sv_pos_b2u(sv, &itemsize);
604 item_is_utf8 = FALSE;
605 if (itemsize > fieldsize)
606 itemsize = fieldsize;
607 send = chophere = s + itemsize;
621 const char *s = item = SvPV_const(sv, len);
624 itemsize = sv_len_utf8(sv);
625 if (itemsize != (I32)len) {
627 if (itemsize <= fieldsize) {
628 const char *send = chophere = s + itemsize;
641 itemsize = fieldsize;
642 itembytes = itemsize;
643 sv_pos_u2b(sv, &itembytes, 0);
644 send = chophere = s + itembytes;
645 while (s < send || (s == send && isSPACE(*s))) {
655 if (strchr(PL_chopset, *s))
660 itemsize = chophere - item;
661 sv_pos_b2u(sv, &itemsize);
667 item_is_utf8 = FALSE;
668 if (itemsize <= fieldsize) {
669 const char *const send = chophere = s + itemsize;
682 itemsize = fieldsize;
683 send = chophere = s + itemsize;
684 while (s < send || (s == send && isSPACE(*s))) {
694 if (strchr(PL_chopset, *s))
699 itemsize = chophere - item;
705 arg = fieldsize - itemsize;
714 arg = fieldsize - itemsize;
728 /* convert to_copy from chars to bytes */
732 to_copy = s - source;
738 const char *s = chophere;
752 const bool oneline = fpc[-1] == FF_LINESNGL;
753 const char *s = item = SvPV_const(sv, len);
754 const char *const send = s + len;
756 item_is_utf8 = DO_UTF8(sv);
767 to_copy = s - SvPVX_const(sv) - 1;
781 /* append to_copy bytes from source to PL_formstring.
782 * item_is_utf8 implies source is utf8.
783 * if trans, translate certain characters during the copy */
788 SvCUR_set(PL_formtarget,
789 t - SvPVX_const(PL_formtarget));
791 if (targ_is_utf8 && !item_is_utf8) {
792 source = tmp = bytes_to_utf8(source, &to_copy);
794 if (item_is_utf8 && !targ_is_utf8) {
796 /* Upgrade targ to UTF8, and then we reduce it to
797 a problem we have a simple solution for.
798 Don't need get magic. */
799 sv_utf8_upgrade_nomg(PL_formtarget);
801 /* re-calculate linemark */
802 s = (U8*)SvPVX(PL_formtarget);
803 /* the bytes we initially allocated to append the
804 * whole line may have been gobbled up during the
805 * upgrade, so allocate a whole new line's worth
810 linemark = s - (U8*)SvPVX(PL_formtarget);
812 /* Easy. They agree. */
813 assert (item_is_utf8 == targ_is_utf8);
816 /* @* and ^* are the only things that can exceed
817 * the linemax, so grow by the output size, plus
818 * a whole new form's worth in case of any further
820 grow = linemax + to_copy;
822 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1);
823 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
825 Copy(source, t, to_copy, char);
827 /* blank out ~ or control chars, depending on trans.
828 * works on bytes not chars, so relies on not
829 * matching utf8 continuation bytes */
831 U8 *send = s + to_copy;
834 if (trans == '~' ? (ch == '~') :
847 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
855 #if defined(USE_LONG_DOUBLE)
857 ((arg & FORM_NUM_POINT) ?
858 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
861 ((arg & FORM_NUM_POINT) ?
862 "%#0*.*f" : "%0*.*f");
867 #if defined(USE_LONG_DOUBLE)
869 ((arg & FORM_NUM_POINT) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
872 ((arg & FORM_NUM_POINT) ? "%#*.*f" : "%*.*f");
875 /* If the field is marked with ^ and the value is undefined,
877 if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) {
885 /* overflow evidence */
886 if (num_overflow(value, fieldsize, arg)) {
892 /* Formats aren't yet marked for locales, so assume "yes". */
894 STORE_NUMERIC_STANDARD_SET_LOCAL();
895 arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
896 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg, value);
897 RESTORE_NUMERIC_STANDARD();
904 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
912 if (arg) { /* repeat until fields exhausted? */
918 t = SvPVX(PL_formtarget) + linemark;
925 const char *s = chophere;
926 const char *send = item + len;
928 while (isSPACE(*s) && (s < send))
933 arg = fieldsize - itemsize;
940 if (strnEQ(s1," ",3)) {
941 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
952 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
954 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
956 SvUTF8_on(PL_formtarget);
957 FmLINES(PL_formtarget) += lines;
959 if (fpc[-1] == FF_BLANK)
960 RETURNOP(cLISTOP->op_first);
972 if (PL_stack_base + *PL_markstack_ptr == SP) {
974 if (GIMME_V == G_SCALAR)
976 RETURNOP(PL_op->op_next->op_next);
978 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
979 Perl_pp_pushmark(aTHX); /* push dst */
980 Perl_pp_pushmark(aTHX); /* push src */
981 ENTER_with_name("grep"); /* enter outer scope */
984 if (PL_op->op_private & OPpGREP_LEX)
985 SAVESPTR(PAD_SVl(PL_op->op_targ));
988 ENTER_with_name("grep_item"); /* enter inner scope */
991 src = PL_stack_base[*PL_markstack_ptr];
992 if (SvPADTMP(src) && !IS_PADGV(src)) {
993 src = PL_stack_base[*PL_markstack_ptr] = sv_mortalcopy(src);
997 if (PL_op->op_private & OPpGREP_LEX)
998 PAD_SVl(PL_op->op_targ) = src;
1003 if (PL_op->op_type == OP_MAPSTART)
1004 Perl_pp_pushmark(aTHX); /* push top */
1005 return ((LOGOP*)PL_op->op_next)->op_other;
1011 const I32 gimme = GIMME_V;
1012 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1018 /* first, move source pointer to the next item in the source list */
1019 ++PL_markstack_ptr[-1];
1021 /* if there are new items, push them into the destination list */
1022 if (items && gimme != G_VOID) {
1023 /* might need to make room back there first */
1024 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1025 /* XXX this implementation is very pessimal because the stack
1026 * is repeatedly extended for every set of items. Is possible
1027 * to do this without any stack extension or copying at all
1028 * by maintaining a separate list over which the map iterates
1029 * (like foreach does). --gsar */
1031 /* everything in the stack after the destination list moves
1032 * towards the end the stack by the amount of room needed */
1033 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1035 /* items to shift up (accounting for the moved source pointer) */
1036 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1038 /* This optimization is by Ben Tilly and it does
1039 * things differently from what Sarathy (gsar)
1040 * is describing. The downside of this optimization is
1041 * that leaves "holes" (uninitialized and hopefully unused areas)
1042 * to the Perl stack, but on the other hand this
1043 * shouldn't be a problem. If Sarathy's idea gets
1044 * implemented, this optimization should become
1045 * irrelevant. --jhi */
1047 shift = count; /* Avoid shifting too often --Ben Tilly */
1051 dst = (SP += shift);
1052 PL_markstack_ptr[-1] += shift;
1053 *PL_markstack_ptr += shift;
1057 /* copy the new items down to the destination list */
1058 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1059 if (gimme == G_ARRAY) {
1060 /* add returned items to the collection (making mortal copies
1061 * if necessary), then clear the current temps stack frame
1062 * *except* for those items. We do this splicing the items
1063 * into the start of the tmps frame (so some items may be on
1064 * the tmps stack twice), then moving PL_tmps_floor above
1065 * them, then freeing the frame. That way, the only tmps that
1066 * accumulate over iterations are the return values for map.
1067 * We have to do to this way so that everything gets correctly
1068 * freed if we die during the map.
1072 /* make space for the slice */
1073 EXTEND_MORTAL(items);
1074 tmpsbase = PL_tmps_floor + 1;
1075 Move(PL_tmps_stack + tmpsbase,
1076 PL_tmps_stack + tmpsbase + items,
1077 PL_tmps_ix - PL_tmps_floor,
1079 PL_tmps_ix += items;
1084 sv = sv_mortalcopy(sv);
1086 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1088 /* clear the stack frame except for the items */
1089 PL_tmps_floor += items;
1091 /* FREETMPS may have cleared the TEMP flag on some of the items */
1094 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1097 /* scalar context: we don't care about which values map returns
1098 * (we use undef here). And so we certainly don't want to do mortal
1099 * copies of meaningless values. */
1100 while (items-- > 0) {
1102 *dst-- = &PL_sv_undef;
1110 LEAVE_with_name("grep_item"); /* exit inner scope */
1113 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1115 (void)POPMARK; /* pop top */
1116 LEAVE_with_name("grep"); /* exit outer scope */
1117 (void)POPMARK; /* pop src */
1118 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1119 (void)POPMARK; /* pop dst */
1120 SP = PL_stack_base + POPMARK; /* pop original mark */
1121 if (gimme == G_SCALAR) {
1122 if (PL_op->op_private & OPpGREP_LEX) {
1123 SV* sv = sv_newmortal();
1124 sv_setiv(sv, items);
1132 else if (gimme == G_ARRAY)
1139 ENTER_with_name("grep_item"); /* enter inner scope */
1142 /* set $_ to the new source item */
1143 src = PL_stack_base[PL_markstack_ptr[-1]];
1144 if (SvPADTMP(src) && !IS_PADGV(src)) src = sv_mortalcopy(src);
1146 if (PL_op->op_private & OPpGREP_LEX)
1147 PAD_SVl(PL_op->op_targ) = src;
1151 RETURNOP(cLOGOP->op_other);
1160 if (GIMME == G_ARRAY)
1162 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1163 return cLOGOP->op_other;
1173 if (GIMME == G_ARRAY) {
1174 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1178 SV * const targ = PAD_SV(PL_op->op_targ);
1181 if (PL_op->op_private & OPpFLIP_LINENUM) {
1182 if (GvIO(PL_last_in_gv)) {
1183 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1186 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1188 flip = SvIV(sv) == SvIV(GvSV(gv));
1194 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1195 if (PL_op->op_flags & OPf_SPECIAL) {
1203 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1206 sv_setpvs(TARG, "");
1212 /* This code tries to decide if "$left .. $right" should use the
1213 magical string increment, or if the range is numeric (we make
1214 an exception for .."0" [#18165]). AMS 20021031. */
1216 #define RANGE_IS_NUMERIC(left,right) ( \
1217 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1218 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1219 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1220 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1221 && (!SvOK(right) || looks_like_number(right))))
1227 if (GIMME == G_ARRAY) {
1233 if (RANGE_IS_NUMERIC(left,right)) {
1236 if ((SvOK(left) && SvNV_nomg(left) < IV_MIN) ||
1237 (SvOK(right) && SvNV_nomg(right) > IV_MAX))
1238 DIE(aTHX_ "Range iterator outside integer range");
1239 i = SvIV_nomg(left);
1240 max = SvIV_nomg(right);
1249 SV * const sv = sv_2mortal(newSViv(i++));
1255 const char * const lpv = SvPV_nomg_const(left, llen);
1256 const char * const tmps = SvPV_nomg_const(right, len);
1258 SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP);
1259 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1261 if (strEQ(SvPVX_const(sv),tmps))
1263 sv = sv_2mortal(newSVsv(sv));
1270 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1274 if (PL_op->op_private & OPpFLIP_LINENUM) {
1275 if (GvIO(PL_last_in_gv)) {
1276 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1279 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1280 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1288 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1289 sv_catpvs(targ, "E0");
1299 static const char * const context_name[] = {
1301 NULL, /* CXt_WHEN never actually needs "block" */
1302 NULL, /* CXt_BLOCK never actually needs "block" */
1303 NULL, /* CXt_GIVEN never actually needs "block" */
1304 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1305 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1306 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1307 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1315 S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags)
1320 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1322 for (i = cxstack_ix; i >= 0; i--) {
1323 const PERL_CONTEXT * const cx = &cxstack[i];
1324 switch (CxTYPE(cx)) {
1330 /* diag_listed_as: Exiting subroutine via %s */
1331 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1332 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1333 if (CxTYPE(cx) == CXt_NULL)
1336 case CXt_LOOP_LAZYIV:
1337 case CXt_LOOP_LAZYSV:
1339 case CXt_LOOP_PLAIN:
1341 STRLEN cx_label_len = 0;
1342 U32 cx_label_flags = 0;
1343 const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags);
1345 ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
1348 (const U8*)cx_label, cx_label_len,
1349 (const U8*)label, len) == 0)
1351 (const U8*)label, len,
1352 (const U8*)cx_label, cx_label_len) == 0)
1353 : (len == cx_label_len && ((cx_label == label)
1354 || memEQ(cx_label, label, len))) )) {
1355 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1356 (long)i, cx_label));
1359 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1370 Perl_dowantarray(pTHX)
1373 const I32 gimme = block_gimme();
1374 return (gimme == G_VOID) ? G_SCALAR : gimme;
1378 Perl_block_gimme(pTHX)
1381 const I32 cxix = dopoptosub(cxstack_ix);
1385 switch (cxstack[cxix].blk_gimme) {
1393 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1394 assert(0); /* NOTREACHED */
1400 Perl_is_lvalue_sub(pTHX)
1403 const I32 cxix = dopoptosub(cxstack_ix);
1404 assert(cxix >= 0); /* We should only be called from inside subs */
1406 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1407 return CxLVAL(cxstack + cxix);
1412 /* only used by PUSHSUB */
1414 Perl_was_lvalue_sub(pTHX)
1417 const I32 cxix = dopoptosub(cxstack_ix-1);
1418 assert(cxix >= 0); /* We should only be called from inside subs */
1420 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1421 return CxLVAL(cxstack + cxix);
1427 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1432 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1434 for (i = startingblock; i >= 0; i--) {
1435 const PERL_CONTEXT * const cx = &cxstk[i];
1436 switch (CxTYPE(cx)) {
1440 /* in sub foo { /(?{...})/ }, foo ends up on the CX stack
1441 * twice; the first for the normal foo() call, and the second
1442 * for a faked up re-entry into the sub to execute the
1443 * code block. Hide this faked entry from the world. */
1444 if (cx->cx_type & CXp_SUB_RE_FAKE)
1448 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1456 S_dopoptoeval(pTHX_ I32 startingblock)
1460 for (i = startingblock; i >= 0; i--) {
1461 const PERL_CONTEXT *cx = &cxstack[i];
1462 switch (CxTYPE(cx)) {
1466 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1474 S_dopoptoloop(pTHX_ I32 startingblock)
1478 for (i = startingblock; i >= 0; i--) {
1479 const PERL_CONTEXT * const cx = &cxstack[i];
1480 switch (CxTYPE(cx)) {
1486 /* diag_listed_as: Exiting subroutine via %s */
1487 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1488 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1489 if ((CxTYPE(cx)) == CXt_NULL)
1492 case CXt_LOOP_LAZYIV:
1493 case CXt_LOOP_LAZYSV:
1495 case CXt_LOOP_PLAIN:
1496 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1504 S_dopoptogiven(pTHX_ I32 startingblock)
1508 for (i = startingblock; i >= 0; i--) {
1509 const PERL_CONTEXT *cx = &cxstack[i];
1510 switch (CxTYPE(cx)) {
1514 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
1516 case CXt_LOOP_PLAIN:
1517 assert(!CxFOREACHDEF(cx));
1519 case CXt_LOOP_LAZYIV:
1520 case CXt_LOOP_LAZYSV:
1522 if (CxFOREACHDEF(cx)) {
1523 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
1532 S_dopoptowhen(pTHX_ I32 startingblock)
1536 for (i = startingblock; i >= 0; i--) {
1537 const PERL_CONTEXT *cx = &cxstack[i];
1538 switch (CxTYPE(cx)) {
1542 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1550 Perl_dounwind(pTHX_ I32 cxix)
1555 if (!PL_curstackinfo) /* can happen if die during thread cloning */
1558 while (cxstack_ix > cxix) {
1560 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1561 DEBUG_CX("UNWIND"); \
1562 /* Note: we don't need to restore the base context info till the end. */
1563 switch (CxTYPE(cx)) {
1566 continue; /* not break */
1574 case CXt_LOOP_LAZYIV:
1575 case CXt_LOOP_LAZYSV:
1577 case CXt_LOOP_PLAIN:
1588 PERL_UNUSED_VAR(optype);
1592 Perl_qerror(pTHX_ SV *err)
1596 PERL_ARGS_ASSERT_QERROR;
1599 if (PL_in_eval & EVAL_KEEPERR) {
1600 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
1604 sv_catsv(ERRSV, err);
1607 sv_catsv(PL_errors, err);
1609 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
1611 ++PL_parser->error_count;
1615 Perl_die_unwind(pTHX_ SV *msv)
1618 SV *exceptsv = sv_mortalcopy(msv);
1619 U8 in_eval = PL_in_eval;
1620 PERL_ARGS_ASSERT_DIE_UNWIND;
1627 * Historically, perl used to set ERRSV ($@) early in the die
1628 * process and rely on it not getting clobbered during unwinding.
1629 * That sucked, because it was liable to get clobbered, so the
1630 * setting of ERRSV used to emit the exception from eval{} has
1631 * been moved to much later, after unwinding (see just before
1632 * JMPENV_JUMP below). However, some modules were relying on the
1633 * early setting, by examining $@ during unwinding to use it as
1634 * a flag indicating whether the current unwinding was caused by
1635 * an exception. It was never a reliable flag for that purpose,
1636 * being totally open to false positives even without actual
1637 * clobberage, but was useful enough for production code to
1638 * semantically rely on it.
1640 * We'd like to have a proper introspective interface that
1641 * explicitly describes the reason for whatever unwinding
1642 * operations are currently in progress, so that those modules
1643 * work reliably and $@ isn't further overloaded. But we don't
1644 * have one yet. In its absence, as a stopgap measure, ERRSV is
1645 * now *additionally* set here, before unwinding, to serve as the
1646 * (unreliable) flag that it used to.
1648 * This behaviour is temporary, and should be removed when a
1649 * proper way to detect exceptional unwinding has been developed.
1650 * As of 2010-12, the authors of modules relying on the hack
1651 * are aware of the issue, because the modules failed on
1652 * perls 5.13.{1..7} which had late setting of $@ without this
1653 * early-setting hack.
1655 if (!(in_eval & EVAL_KEEPERR)) {
1656 SvTEMP_off(exceptsv);
1657 sv_setsv(ERRSV, exceptsv);
1660 if (in_eval & EVAL_KEEPERR) {
1661 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
1665 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1666 && PL_curstackinfo->si_prev)
1678 JMPENV *restartjmpenv;
1681 if (cxix < cxstack_ix)
1684 POPBLOCK(cx,PL_curpm);
1685 if (CxTYPE(cx) != CXt_EVAL) {
1687 const char* message = SvPVx_const(exceptsv, msglen);
1688 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
1689 PerlIO_write(Perl_error_log, message, msglen);
1693 namesv = cx->blk_eval.old_namesv;
1694 oldcop = cx->blk_oldcop;
1695 restartjmpenv = cx->blk_eval.cur_top_env;
1696 restartop = cx->blk_eval.retop;
1698 if (gimme == G_SCALAR)
1699 *++newsp = &PL_sv_undef;
1700 PL_stack_sp = newsp;
1704 /* LEAVE could clobber PL_curcop (see save_re_context())
1705 * XXX it might be better to find a way to avoid messing with
1706 * PL_curcop in save_re_context() instead, but this is a more
1707 * minimal fix --GSAR */
1710 if (optype == OP_REQUIRE) {
1711 (void)hv_store(GvHVn(PL_incgv),
1712 SvPVX_const(namesv),
1713 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
1715 /* note that unlike pp_entereval, pp_require isn't
1716 * supposed to trap errors. So now that we've popped the
1717 * EVAL that pp_require pushed, and processed the error
1718 * message, rethrow the error */
1719 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
1720 SVfARG(exceptsv ? exceptsv : newSVpvs_flags("Unknown error\n",
1723 if (!(in_eval & EVAL_KEEPERR))
1724 sv_setsv(ERRSV, exceptsv);
1725 PL_restartjmpenv = restartjmpenv;
1726 PL_restartop = restartop;
1728 assert(0); /* NOTREACHED */
1732 write_to_stderr(exceptsv);
1734 assert(0); /* NOTREACHED */
1739 dVAR; dSP; dPOPTOPssrl;
1740 if (SvTRUE(left) != SvTRUE(right))
1747 =for apidoc caller_cx
1749 The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1750 returned C<PERL_CONTEXT> structure can be interrogated to find all the
1751 information returned to Perl by C<caller>. Note that XSUBs don't get a
1752 stack frame, so C<caller_cx(0, NULL)> will return information for the
1753 immediately-surrounding Perl code.
1755 This function skips over the automatic calls to C<&DB::sub> made on the
1756 behalf of the debugger. If the stack frame requested was a sub called by
1757 C<DB::sub>, the return value will be the frame for the call to
1758 C<DB::sub>, since that has the correct line number/etc. for the call
1759 site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1760 frame for the sub call itself.
1765 const PERL_CONTEXT *
1766 Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
1768 I32 cxix = dopoptosub(cxstack_ix);
1769 const PERL_CONTEXT *cx;
1770 const PERL_CONTEXT *ccstack = cxstack;
1771 const PERL_SI *top_si = PL_curstackinfo;
1774 /* we may be in a higher stacklevel, so dig down deeper */
1775 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1776 top_si = top_si->si_prev;
1777 ccstack = top_si->si_cxstack;
1778 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1782 /* caller() should not report the automatic calls to &DB::sub */
1783 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1784 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1788 cxix = dopoptosub_at(ccstack, cxix - 1);
1791 cx = &ccstack[cxix];
1792 if (dbcxp) *dbcxp = cx;
1794 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1795 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1796 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1797 field below is defined for any cx. */
1798 /* caller() should not report the automatic calls to &DB::sub */
1799 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1800 cx = &ccstack[dbcxix];
1810 const PERL_CONTEXT *cx;
1811 const PERL_CONTEXT *dbcx;
1813 const HEK *stash_hek;
1815 bool has_arg = MAXARG && TOPs;
1823 cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx);
1825 if (GIMME != G_ARRAY) {
1833 assert(CopSTASH(cx->blk_oldcop));
1834 stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV
1835 ? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop))
1837 if (GIMME != G_ARRAY) {
1840 PUSHs(&PL_sv_undef);
1843 sv_sethek(TARG, stash_hek);
1852 PUSHs(&PL_sv_undef);
1855 sv_sethek(TARG, stash_hek);
1858 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1859 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1862 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1863 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
1864 /* So is ccstack[dbcxix]. */
1865 if (cvgv && isGV(cvgv)) {
1866 SV * const sv = newSV(0);
1867 gv_efullname3(sv, cvgv, NULL);
1869 PUSHs(boolSV(CxHASARGS(cx)));
1872 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1873 PUSHs(boolSV(CxHASARGS(cx)));
1877 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1880 gimme = (I32)cx->blk_gimme;
1881 if (gimme == G_VOID)
1882 PUSHs(&PL_sv_undef);
1884 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1885 if (CxTYPE(cx) == CXt_EVAL) {
1887 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1888 PUSHs(newSVpvn_flags(SvPVX(cx->blk_eval.cur_text),
1889 SvCUR(cx->blk_eval.cur_text)-2,
1890 SvUTF8(cx->blk_eval.cur_text)|SVs_TEMP));
1894 else if (cx->blk_eval.old_namesv) {
1895 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1898 /* eval BLOCK (try blocks have old_namesv == 0) */
1900 PUSHs(&PL_sv_undef);
1901 PUSHs(&PL_sv_undef);
1905 PUSHs(&PL_sv_undef);
1906 PUSHs(&PL_sv_undef);
1908 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1909 && CopSTASH_eq(PL_curcop, PL_debstash))
1911 AV * const ary = cx->blk_sub.argarray;
1912 const int off = AvARRAY(ary) - AvALLOC(ary);
1914 Perl_init_dbargs(aTHX);
1916 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1917 av_extend(PL_dbargs, AvFILLp(ary) + off);
1918 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1919 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1921 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1924 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1926 if (old_warnings == pWARN_NONE)
1927 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1928 else if (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0)
1929 mask = &PL_sv_undef ;
1930 else if (old_warnings == pWARN_ALL ||
1931 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1932 /* Get the bit mask for $warnings::Bits{all}, because
1933 * it could have been extended by warnings::register */
1935 HV * const bits = get_hv("warnings::Bits", 0);
1936 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
1937 mask = newSVsv(*bits_all);
1940 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1944 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
1948 PUSHs(cx->blk_oldcop->cop_hints_hash ?
1949 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
1960 if (MAXARG < 1 || (!TOPs && !POPs))
1961 tmps = NULL, len = 0;
1963 tmps = SvPVx_const(POPs, len);
1964 sv_resetpvn(tmps, len, CopSTASH(PL_curcop));
1969 /* like pp_nextstate, but used instead when the debugger is active */
1974 PL_curcop = (COP*)PL_op;
1975 TAINT_NOT; /* Each statement is presumed innocent */
1976 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1981 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1982 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1986 const I32 gimme = G_ARRAY;
1988 GV * const gv = PL_DBgv;
1991 if (gv && isGV_with_GP(gv))
1994 if (!cv || (!CvROOT(cv) && !CvXSUB(cv)))
1995 DIE(aTHX_ "No DB::DB routine defined");
1997 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1998 /* don't do recursive DB::DB call */
2012 (void)(*CvXSUB(cv))(aTHX_ cv);
2018 PUSHBLOCK(cx, CXt_SUB, SP);
2020 cx->blk_sub.retop = PL_op->op_next;
2022 if (CvDEPTH(cv) >= 2) {
2023 PERL_STACK_OVERFLOW_CHECK();
2024 pad_push(CvPADLIST(cv), CvDEPTH(cv));
2027 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), CvDEPTH(cv));
2028 RETURNOP(CvSTART(cv));
2036 S_adjust_stack_on_leave(pTHX_ SV **newsp, SV **sp, SV **mark, I32 gimme, U32 flags)
2039 PERL_ARGS_ASSERT_ADJUST_STACK_ON_LEAVE;
2041 if (flags & SVs_PADTMP) {
2042 flags &= ~SVs_PADTMP;
2045 if (gimme == G_SCALAR) {
2047 *++newsp = ((SvFLAGS(*SP) & flags) || (padtmp && SvPADTMP(*SP)))
2048 ? *SP : sv_mortalcopy(*SP);
2050 /* MEXTEND() only updates MARK, so reuse it instead of newsp. */
2053 *++MARK = &PL_sv_undef;
2057 else if (gimme == G_ARRAY) {
2058 /* in case LEAVE wipes old return values */
2059 while (++MARK <= SP) {
2060 if ((SvFLAGS(*MARK) & flags) || (padtmp && SvPADTMP(*MARK)))
2063 *++newsp = sv_mortalcopy(*MARK);
2064 TAINT_NOT; /* Each item is independent */
2067 /* When this function was called with MARK == newsp, we reach this
2068 * point with SP == newsp. */
2078 I32 gimme = GIMME_V;
2080 ENTER_with_name("block");
2083 PUSHBLOCK(cx, CXt_BLOCK, SP);
2096 if (PL_op->op_flags & OPf_SPECIAL) {
2097 cx = &cxstack[cxstack_ix];
2098 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
2103 gimme = OP_GIMME(PL_op, (cxstack_ix >= 0) ? gimme : G_SCALAR);
2106 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
2107 PL_curpm = newpm; /* Don't pop $1 et al till now */
2109 LEAVE_with_name("block");
2118 const I32 gimme = GIMME_V;
2119 void *itervar; /* location of the iteration variable */
2120 U8 cxtype = CXt_LOOP_FOR;
2122 ENTER_with_name("loop1");
2125 if (PL_op->op_targ) { /* "my" variable */
2126 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
2127 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2128 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2129 SVs_PADSTALE, SVs_PADSTALE);
2131 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
2133 itervar = PL_comppad;
2135 itervar = &PAD_SVl(PL_op->op_targ);
2138 else { /* symbol table variable */
2139 GV * const gv = MUTABLE_GV(POPs);
2140 SV** svp = &GvSV(gv);
2141 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
2143 itervar = (void *)gv;
2146 if (PL_op->op_private & OPpITER_DEF)
2147 cxtype |= CXp_FOR_DEF;
2149 ENTER_with_name("loop2");
2151 PUSHBLOCK(cx, cxtype, SP);
2152 PUSHLOOP_FOR(cx, itervar, MARK);
2153 if (PL_op->op_flags & OPf_STACKED) {
2154 SV *maybe_ary = POPs;
2155 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2157 SV * const right = maybe_ary;
2160 if (RANGE_IS_NUMERIC(sv,right)) {
2161 cx->cx_type &= ~CXTYPEMASK;
2162 cx->cx_type |= CXt_LOOP_LAZYIV;
2163 /* Make sure that no-one re-orders cop.h and breaks our
2165 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
2166 #ifdef NV_PRESERVES_UV
2167 if ((SvOK(sv) && ((SvNV_nomg(sv) < (NV)IV_MIN) ||
2168 (SvNV_nomg(sv) > (NV)IV_MAX)))
2170 (SvOK(right) && ((SvNV_nomg(right) > (NV)IV_MAX) ||
2171 (SvNV_nomg(right) < (NV)IV_MIN))))
2173 if ((SvOK(sv) && ((SvNV_nomg(sv) <= (NV)IV_MIN)
2175 ((SvNV_nomg(sv) > 0) &&
2176 ((SvUV_nomg(sv) > (UV)IV_MAX) ||
2177 (SvNV_nomg(sv) > (NV)UV_MAX)))))
2179 (SvOK(right) && ((SvNV_nomg(right) <= (NV)IV_MIN)
2181 ((SvNV_nomg(right) > 0) &&
2182 ((SvUV_nomg(right) > (UV)IV_MAX) ||
2183 (SvNV_nomg(right) > (NV)UV_MAX))
2186 DIE(aTHX_ "Range iterator outside integer range");
2187 cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv);
2188 cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right);
2190 /* for correct -Dstv display */
2191 cx->blk_oldsp = sp - PL_stack_base;
2195 cx->cx_type &= ~CXTYPEMASK;
2196 cx->cx_type |= CXt_LOOP_LAZYSV;
2197 /* Make sure that no-one re-orders cop.h and breaks our
2199 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2200 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2201 cx->blk_loop.state_u.lazysv.end = right;
2202 SvREFCNT_inc(right);
2203 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2204 /* This will do the upgrade to SVt_PV, and warn if the value
2205 is uninitialised. */
2206 (void) SvPV_nolen_const(right);
2207 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2208 to replace !SvOK() with a pointer to "". */
2210 SvREFCNT_dec(right);
2211 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2215 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2216 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2217 SvREFCNT_inc(maybe_ary);
2218 cx->blk_loop.state_u.ary.ix =
2219 (PL_op->op_private & OPpITER_REVERSED) ?
2220 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2224 else { /* iterating over items on the stack */
2225 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2226 if (PL_op->op_private & OPpITER_REVERSED) {
2227 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2230 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2241 const I32 gimme = GIMME_V;
2243 ENTER_with_name("loop1");
2245 ENTER_with_name("loop2");
2247 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2248 PUSHLOOP_PLAIN(cx, SP);
2263 assert(CxTYPE_is_LOOP(cx));
2265 newsp = PL_stack_base + cx->blk_loop.resetsp;
2268 SP = adjust_stack_on_leave(newsp, SP, MARK, gimme, 0);
2271 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2272 PL_curpm = newpm; /* ... and pop $1 et al */
2274 LEAVE_with_name("loop2");
2275 LEAVE_with_name("loop1");
2281 S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
2282 PERL_CONTEXT *cx, PMOP *newpm)
2284 const bool ref = !!(CxLVAL(cx) & OPpENTERSUB_INARGS);
2285 if (gimme == G_SCALAR) {
2286 if (CxLVAL(cx) && !ref) { /* Leave it as it is if we can. */
2288 const char *what = NULL;
2290 assert(MARK+1 == SP);
2291 if ((SvPADTMP(TOPs) ||
2292 (SvFLAGS(TOPs) & (SVf_READONLY | SVf_FAKE))
2295 !SvSMAGICAL(TOPs)) {
2297 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2298 : "a readonly value" : "a temporary";
2303 /* sub:lvalue{} will take us here. */
2312 "Can't return %s from lvalue subroutine", what
2317 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2318 if (!SvPADTMP(*SP)) {
2319 *++newsp = SvREFCNT_inc(*SP);
2324 /* FREETMPS could clobber it */
2325 SV *sv = SvREFCNT_inc(*SP);
2327 *++newsp = sv_mortalcopy(sv);
2334 ? sv_mortalcopy(*SP)
2336 ? sv_2mortal(SvREFCNT_inc_simple_NN(*SP))
2341 *++newsp = &PL_sv_undef;
2343 if (CxLVAL(cx) & OPpDEREF) {
2346 TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF);
2350 else if (gimme == G_ARRAY) {
2351 assert (!(CxLVAL(cx) & OPpDEREF));
2352 if (ref || !CxLVAL(cx))
2353 while (++MARK <= SP)
2355 SvFLAGS(*MARK) & SVs_PADTMP
2356 ? sv_mortalcopy(*MARK)
2359 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
2360 else while (++MARK <= SP) {
2361 if (*MARK != &PL_sv_undef
2363 || (SvFLAGS(*MARK) & (SVf_READONLY|SVf_FAKE))
2368 /* Might be flattened array after $#array = */
2375 /* diag_listed_as: Can't return %s from lvalue subroutine */
2377 "Can't return a %s from lvalue subroutine",
2378 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2384 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
2387 PL_stack_sp = newsp;
2394 bool popsub2 = FALSE;
2395 bool clear_errsv = FALSE;
2405 const I32 cxix = dopoptosub(cxstack_ix);
2408 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2409 * sort block, which is a CXt_NULL
2412 PL_stack_base[1] = *PL_stack_sp;
2413 PL_stack_sp = PL_stack_base + 1;
2417 DIE(aTHX_ "Can't return outside a subroutine");
2419 if (cxix < cxstack_ix)
2422 if (CxMULTICALL(&cxstack[cxix])) {
2423 gimme = cxstack[cxix].blk_gimme;
2424 if (gimme == G_VOID)
2425 PL_stack_sp = PL_stack_base;
2426 else if (gimme == G_SCALAR) {
2427 PL_stack_base[1] = *PL_stack_sp;
2428 PL_stack_sp = PL_stack_base + 1;
2434 switch (CxTYPE(cx)) {
2437 lval = !!CvLVALUE(cx->blk_sub.cv);
2438 retop = cx->blk_sub.retop;
2439 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2442 if (!(PL_in_eval & EVAL_KEEPERR))
2445 namesv = cx->blk_eval.old_namesv;
2446 retop = cx->blk_eval.retop;
2449 if (optype == OP_REQUIRE &&
2450 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2452 /* Unassume the success we assumed earlier. */
2453 (void)hv_delete(GvHVn(PL_incgv),
2454 SvPVX_const(namesv),
2455 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
2457 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
2462 retop = cx->blk_sub.retop;
2465 DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx));
2469 if (lval) S_return_lvalues(aTHX_ MARK, SP, newsp, gimme, cx, newpm);
2471 if (gimme == G_SCALAR) {
2474 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2475 if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1
2476 && !SvMAGICAL(TOPs)) {
2477 *++newsp = SvREFCNT_inc(*SP);
2482 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2484 *++newsp = sv_mortalcopy(sv);
2488 else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1
2489 && !SvMAGICAL(*SP)) {
2493 *++newsp = sv_mortalcopy(*SP);
2496 *++newsp = sv_mortalcopy(*SP);
2499 *++newsp = &PL_sv_undef;
2501 else if (gimme == G_ARRAY) {
2502 while (++MARK <= SP) {
2503 *++newsp = popsub2 && SvTEMP(*MARK) && SvREFCNT(*MARK) == 1
2504 && !SvGMAGICAL(*MARK)
2505 ? *MARK : sv_mortalcopy(*MARK);
2506 TAINT_NOT; /* Each item is independent */
2509 PL_stack_sp = newsp;
2513 /* Stack values are safe: */
2516 POPSUB(cx,sv); /* release CV and @_ ... */
2520 PL_curpm = newpm; /* ... and pop $1 et al */
2529 /* This duplicates parts of pp_leavesub, so that it can share code with
2540 if (CxMULTICALL(&cxstack[cxstack_ix]))
2544 cxstack_ix++; /* temporarily protect top context */
2548 S_return_lvalues(aTHX_ newsp, SP, newsp, gimme, cx, newpm);
2552 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2553 PL_curpm = newpm; /* ... and pop $1 et al */
2556 return cx->blk_sub.retop;
2560 S_unwind_loop(pTHX_ const char * const opname)
2564 if (PL_op->op_flags & OPf_SPECIAL) {
2565 cxix = dopoptoloop(cxstack_ix);
2567 /* diag_listed_as: Can't "last" outside a loop block */
2568 Perl_croak(aTHX_ "Can't \"%s\" outside a loop block", opname);
2573 const char * const label =
2574 PL_op->op_flags & OPf_STACKED
2575 ? SvPV(TOPs,label_len)
2576 : (label_len = strlen(cPVOP->op_pv), cPVOP->op_pv);
2577 const U32 label_flags =
2578 PL_op->op_flags & OPf_STACKED
2580 : (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
2582 cxix = dopoptolabel(label, label_len, label_flags);
2584 /* diag_listed_as: Label not found for "last %s" */
2585 Perl_croak(aTHX_ "Label not found for \"%s %"SVf"\"",
2587 SVfARG(PL_op->op_flags & OPf_STACKED
2588 && !SvGMAGICAL(TOPp1s)
2590 : newSVpvn_flags(label,
2592 label_flags | SVs_TEMP)));
2594 if (cxix < cxstack_ix)
2612 S_unwind_loop(aTHX_ "last");
2615 cxstack_ix++; /* temporarily protect top context */
2617 switch (CxTYPE(cx)) {
2618 case CXt_LOOP_LAZYIV:
2619 case CXt_LOOP_LAZYSV:
2621 case CXt_LOOP_PLAIN:
2623 newsp = PL_stack_base + cx->blk_loop.resetsp;
2624 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2628 nextop = cx->blk_sub.retop;
2632 nextop = cx->blk_eval.retop;
2636 nextop = cx->blk_sub.retop;
2639 DIE(aTHX_ "panic: last, type=%u", (unsigned) CxTYPE(cx));
2643 PL_stack_sp = adjust_stack_on_leave(newsp, PL_stack_sp, MARK, gimme,
2644 pop2 == CXt_SUB ? SVs_TEMP : 0);
2648 /* Stack values are safe: */
2650 case CXt_LOOP_LAZYIV:
2651 case CXt_LOOP_PLAIN:
2652 case CXt_LOOP_LAZYSV:
2654 POPLOOP(cx); /* release loop vars ... */
2658 POPSUB(cx,sv); /* release CV and @_ ... */
2661 PL_curpm = newpm; /* ... and pop $1 et al */
2664 PERL_UNUSED_VAR(optype);
2665 PERL_UNUSED_VAR(gimme);
2673 const I32 inner = PL_scopestack_ix;
2675 S_unwind_loop(aTHX_ "next");
2677 /* clear off anything above the scope we're re-entering, but
2678 * save the rest until after a possible continue block */
2680 if (PL_scopestack_ix < inner)
2681 leave_scope(PL_scopestack[PL_scopestack_ix]);
2682 PL_curcop = cx->blk_oldcop;
2684 return (cx)->blk_loop.my_op->op_nextop;
2690 const I32 cxix = S_unwind_loop(aTHX_ "redo");
2693 OP* redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2695 if (redo_op->op_type == OP_ENTER) {
2696 /* pop one less context to avoid $x being freed in while (my $x..) */
2698 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2699 redo_op = redo_op->op_next;
2703 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2704 LEAVE_SCOPE(oldsave);
2706 PL_curcop = cx->blk_oldcop;
2712 S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit)
2716 static const char* const too_deep = "Target of goto is too deeply nested";
2718 PERL_ARGS_ASSERT_DOFINDLABEL;
2721 Perl_croak(aTHX_ "%s", too_deep);
2722 if (o->op_type == OP_LEAVE ||
2723 o->op_type == OP_SCOPE ||
2724 o->op_type == OP_LEAVELOOP ||
2725 o->op_type == OP_LEAVESUB ||
2726 o->op_type == OP_LEAVETRY)
2728 *ops++ = cUNOPo->op_first;
2730 Perl_croak(aTHX_ "%s", too_deep);
2733 if (o->op_flags & OPf_KIDS) {
2735 /* First try all the kids at this level, since that's likeliest. */
2736 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2737 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2738 STRLEN kid_label_len;
2739 U32 kid_label_flags;
2740 const char *kid_label = CopLABEL_len_flags(kCOP,
2741 &kid_label_len, &kid_label_flags);
2743 ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
2746 (const U8*)kid_label, kid_label_len,
2747 (const U8*)label, len) == 0)
2749 (const U8*)label, len,
2750 (const U8*)kid_label, kid_label_len) == 0)
2751 : ( len == kid_label_len && ((kid_label == label)
2752 || memEQ(kid_label, label, len)))))
2756 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2757 if (kid == PL_lastgotoprobe)
2759 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2762 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2763 ops[-1]->op_type == OP_DBSTATE)
2768 if ((o = dofindlabel(kid, label, len, flags, ops, oplimit)))
2782 #define GOTO_DEPTH 64
2783 OP *enterops[GOTO_DEPTH];
2784 const char *label = NULL;
2785 STRLEN label_len = 0;
2786 U32 label_flags = 0;
2787 const bool do_dump = (PL_op->op_type == OP_DUMP);
2788 static const char* const must_have_label = "goto must have label";
2790 if (PL_op->op_flags & OPf_STACKED) {
2791 SV * const sv = POPs;
2794 /* This egregious kludge implements goto &subroutine */
2795 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2798 CV *cv = MUTABLE_CV(SvRV(sv));
2799 AV *arg = GvAV(PL_defgv);
2803 if (!CvROOT(cv) && !CvXSUB(cv)) {
2804 const GV * const gv = CvGV(cv);
2808 /* autoloaded stub? */
2809 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2811 autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv),
2813 GvNAMEUTF8(gv) ? SVf_UTF8 : 0);
2814 if (autogv && (cv = GvCV(autogv)))
2816 tmpstr = sv_newmortal();
2817 gv_efullname3(tmpstr, gv, NULL);
2818 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2820 DIE(aTHX_ "Goto undefined subroutine");
2823 /* First do some returnish stuff. */
2824 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2826 cxix = dopoptosub(cxstack_ix);
2827 if (cxix < cxstack_ix) {
2830 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2836 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2837 if (CxTYPE(cx) == CXt_EVAL) {
2840 /* diag_listed_as: Can't goto subroutine from an eval-%s */
2841 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2843 /* diag_listed_as: Can't goto subroutine from an eval-%s */
2844 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2846 else if (CxMULTICALL(cx))
2849 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2851 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2852 AV* av = cx->blk_sub.argarray;
2854 /* abandon the original @_ if it got reified or if it is
2855 the same as the current @_ */
2856 if (AvREAL(av) || av == arg) {
2860 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2862 else CLEAR_ARGARRAY(av);
2864 /* We donate this refcount later to the callee’s pad. */
2865 SvREFCNT_inc_simple_void(arg);
2866 if (CxTYPE(cx) == CXt_SUB &&
2867 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2868 SvREFCNT_dec(cx->blk_sub.cv);
2869 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2870 LEAVE_SCOPE(oldsave);
2872 /* A destructor called during LEAVE_SCOPE could have undefined
2873 * our precious cv. See bug #99850. */
2874 if (!CvROOT(cv) && !CvXSUB(cv)) {
2875 const GV * const gv = CvGV(cv);
2878 SV * const tmpstr = sv_newmortal();
2879 gv_efullname3(tmpstr, gv, NULL);
2880 DIE(aTHX_ "Goto undefined subroutine &%"SVf"",
2883 DIE(aTHX_ "Goto undefined subroutine");
2886 /* Now do some callish stuff. */
2888 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2890 OP* const retop = cx->blk_sub.retop;
2893 const SSize_t items = AvFILLp(arg) + 1;
2896 PERL_UNUSED_VAR(newsp);
2897 PERL_UNUSED_VAR(gimme);
2899 /* put GvAV(defgv) back onto stack */
2900 EXTEND(SP, items+1); /* @_ could have been extended. */
2901 Copy(AvARRAY(arg), SP + 1, items, SV*);
2906 for (index=0; index<items; index++)
2907 SvREFCNT_inc_void(sv_2mortal(SP[-index]));
2910 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2911 /* Restore old @_ */
2912 arg = GvAV(PL_defgv);
2913 GvAV(PL_defgv) = cx->blk_sub.savearray;
2917 /* XS subs don't have a CxSUB, so pop it */
2918 POPBLOCK(cx, PL_curpm);
2919 /* Push a mark for the start of arglist */
2922 (void)(*CvXSUB(cv))(aTHX_ cv);
2928 PADLIST * const padlist = CvPADLIST(cv);
2929 cx->blk_sub.cv = cv;
2930 cx->blk_sub.olddepth = CvDEPTH(cv);
2933 if (CvDEPTH(cv) < 2)
2934 SvREFCNT_inc_simple_void_NN(cv);
2936 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2937 sub_crush_depth(cv);
2938 pad_push(padlist, CvDEPTH(cv));
2940 PL_curcop = cx->blk_oldcop;
2942 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2945 CX_CURPAD_SAVE(cx->blk_sub);
2947 /* cx->blk_sub.argarray has no reference count, so we
2948 need something to hang on to our argument array so
2949 that cx->blk_sub.argarray does not end up pointing
2950 to freed memory as the result of undef *_. So put
2951 it in the callee’s pad, donating our refer-
2953 SvREFCNT_dec(PAD_SVl(0));
2954 PAD_SVl(0) = (SV *)(cx->blk_sub.argarray = arg);
2956 /* GvAV(PL_defgv) might have been modified on scope
2957 exit, so restore it. */
2958 if (arg != GvAV(PL_defgv)) {
2959 AV * const av = GvAV(PL_defgv);
2960 GvAV(PL_defgv) = (AV *)SvREFCNT_inc_simple(arg);
2964 else SvREFCNT_dec(arg);
2965 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2966 Perl_get_db_sub(aTHX_ NULL, cv);
2968 CV * const gotocv = get_cvs("DB::goto", 0);
2970 PUSHMARK( PL_stack_sp );
2971 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
2977 RETURNOP(CvSTART(cv));
2981 label = SvPV_nomg_const(sv, label_len);
2982 label_flags = SvUTF8(sv);
2985 else if (!(PL_op->op_flags & OPf_SPECIAL)) {
2986 label = cPVOP->op_pv;
2987 label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
2988 label_len = strlen(label);
2990 if (!(do_dump || label_len)) DIE(aTHX_ "%s", must_have_label);
2995 OP *gotoprobe = NULL;
2996 bool leaving_eval = FALSE;
2997 bool in_block = FALSE;
2998 PERL_CONTEXT *last_eval_cx = NULL;
3002 PL_lastgotoprobe = NULL;
3004 for (ix = cxstack_ix; ix >= 0; ix--) {
3006 switch (CxTYPE(cx)) {
3008 leaving_eval = TRUE;
3009 if (!CxTRYBLOCK(cx)) {
3010 gotoprobe = (last_eval_cx ?
3011 last_eval_cx->blk_eval.old_eval_root :
3016 /* else fall through */
3017 case CXt_LOOP_LAZYIV:
3018 case CXt_LOOP_LAZYSV:
3020 case CXt_LOOP_PLAIN:
3023 gotoprobe = cx->blk_oldcop->op_sibling;
3029 gotoprobe = cx->blk_oldcop->op_sibling;
3032 gotoprobe = PL_main_root;
3035 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
3036 gotoprobe = CvROOT(cx->blk_sub.cv);
3042 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
3045 DIE(aTHX_ "panic: goto, type=%u, ix=%ld",
3046 CxTYPE(cx), (long) ix);
3047 gotoprobe = PL_main_root;
3051 retop = dofindlabel(gotoprobe, label, label_len, label_flags,
3052 enterops, enterops + GOTO_DEPTH);
3055 if (gotoprobe->op_sibling &&
3056 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
3057 gotoprobe->op_sibling->op_sibling) {
3058 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
3059 label, label_len, label_flags, enterops,
3060 enterops + GOTO_DEPTH);
3065 PL_lastgotoprobe = gotoprobe;
3068 DIE(aTHX_ "Can't find label %"UTF8f,
3069 UTF8fARG(label_flags, label_len, label));
3071 /* if we're leaving an eval, check before we pop any frames
3072 that we're not going to punt, otherwise the error
3075 if (leaving_eval && *enterops && enterops[1]) {
3077 for (i = 1; enterops[i]; i++)
3078 if (enterops[i]->op_type == OP_ENTERITER)
3079 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3082 if (*enterops && enterops[1]) {
3083 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3085 deprecate("\"goto\" to jump into a construct");
3088 /* pop unwanted frames */
3090 if (ix < cxstack_ix) {
3097 oldsave = PL_scopestack[PL_scopestack_ix];
3098 LEAVE_SCOPE(oldsave);
3101 /* push wanted frames */
3103 if (*enterops && enterops[1]) {
3104 OP * const oldop = PL_op;
3105 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3106 for (; enterops[ix]; ix++) {
3107 PL_op = enterops[ix];
3108 /* Eventually we may want to stack the needed arguments
3109 * for each op. For now, we punt on the hard ones. */
3110 if (PL_op->op_type == OP_ENTERITER)
3111 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3112 PL_op->op_ppaddr(aTHX);
3120 if (!retop) retop = PL_main_start;
3122 PL_restartop = retop;
3123 PL_do_undump = TRUE;
3127 PL_restartop = 0; /* hmm, must be GNU unexec().. */
3128 PL_do_undump = FALSE;
3144 anum = 0; (void)POPs;
3149 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
3151 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
3154 PL_exit_flags |= PERL_EXIT_EXPECTED;
3156 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
3157 if (anum || !(PL_minus_c && PL_madskills))
3162 PUSHs(&PL_sv_undef);
3169 S_save_lines(pTHX_ AV *array, SV *sv)
3171 const char *s = SvPVX_const(sv);
3172 const char * const send = SvPVX_const(sv) + SvCUR(sv);
3175 PERL_ARGS_ASSERT_SAVE_LINES;
3177 while (s && s < send) {
3179 SV * const tmpstr = newSV_type(SVt_PVMG);
3181 t = (const char *)memchr(s, '\n', send - s);
3187 sv_setpvn(tmpstr, s, t - s);
3188 av_store(array, line++, tmpstr);
3196 Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
3198 0 is used as continue inside eval,
3200 3 is used for a die caught by an inner eval - continue inner loop
3202 See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
3203 establish a local jmpenv to handle exception traps.
3208 S_docatch(pTHX_ OP *o)
3212 OP * const oldop = PL_op;
3216 assert(CATCH_GET == TRUE);
3223 assert(cxstack_ix >= 0);
3224 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3225 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
3230 /* die caught by an inner eval - continue inner loop */
3231 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3232 PL_restartjmpenv = NULL;
3233 PL_op = PL_restartop;
3242 assert(0); /* NOTREACHED */
3251 =for apidoc find_runcv
3253 Locate the CV corresponding to the currently executing sub or eval.
3254 If db_seqp is non_null, skip CVs that are in the DB package and populate
3255 *db_seqp with the cop sequence number at the point that the DB:: code was
3256 entered. (allows debuggers to eval in the scope of the breakpoint rather
3257 than in the scope of the debugger itself).
3263 Perl_find_runcv(pTHX_ U32 *db_seqp)
3265 return Perl_find_runcv_where(aTHX_ 0, 0, db_seqp);
3268 /* If this becomes part of the API, it might need a better name. */
3270 Perl_find_runcv_where(pTHX_ U8 cond, IV arg, U32 *db_seqp)
3277 *db_seqp = PL_curcop->cop_seq;
3278 for (si = PL_curstackinfo; si; si = si->si_prev) {
3280 for (ix = si->si_cxix; ix >= 0; ix--) {
3281 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3283 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3284 cv = cx->blk_sub.cv;
3285 /* skip DB:: code */
3286 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3287 *db_seqp = cx->blk_oldcop->cop_seq;
3290 if (cx->cx_type & CXp_SUB_RE)
3293 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3294 cv = cx->blk_eval.cv;
3297 case FIND_RUNCV_padid_eq:
3299 || PadlistNAMES(CvPADLIST(cv)) != INT2PTR(PADNAMELIST *, arg))
3302 case FIND_RUNCV_level_eq:
3303 if (level++ != arg) continue;
3311 return cond == FIND_RUNCV_padid_eq ? NULL : PL_main_cv;
3315 /* Run yyparse() in a setjmp wrapper. Returns:
3316 * 0: yyparse() successful
3317 * 1: yyparse() failed
3321 S_try_yyparse(pTHX_ int gramtype)
3326 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3330 ret = yyparse(gramtype) ? 1 : 0;
3337 assert(0); /* NOTREACHED */
3344 /* Compile a require/do or an eval ''.
3346 * outside is the lexically enclosing CV (if any) that invoked us.
3347 * seq is the current COP scope value.
3348 * hh is the saved hints hash, if any.
3350 * Returns a bool indicating whether the compile was successful; if so,
3351 * PL_eval_start contains the first op of the compiled code; otherwise,
3354 * This function is called from two places: pp_require and pp_entereval.
3355 * These can be distinguished by whether PL_op is entereval.
3359 S_doeval(pTHX_ int gimme, CV* outside, U32 seq, HV *hh)
3362 OP * const saveop = PL_op;
3363 bool clear_hints = saveop->op_type != OP_ENTEREVAL;
3364 COP * const oldcurcop = PL_curcop;
3365 bool in_require = (saveop->op_type == OP_REQUIRE);
3369 PL_in_eval = (in_require
3370 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3372 ((PL_op->op_private & OPpEVAL_RE_REPARSING)
3373 ? EVAL_RE_REPARSING : 0)));
3377 evalcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3379 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3380 cxstack[cxstack_ix].blk_eval.cv = evalcv;
3381 cxstack[cxstack_ix].blk_gimme = gimme;
3383 CvOUTSIDE_SEQ(evalcv) = seq;
3384 CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3386 /* set up a scratch pad */
3388 CvPADLIST(evalcv) = pad_new(padnew_SAVE);
3389 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3393 SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */
3395 /* make sure we compile in the right package */
3397 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3398 SAVEGENERICSV(PL_curstash);
3399 PL_curstash = (HV *)CopSTASH(PL_curcop);
3400 if (SvTYPE(PL_curstash) != SVt_PVHV) PL_curstash = NULL;
3401 else SvREFCNT_inc_simple_void(PL_curstash);
3403 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3404 SAVESPTR(PL_beginav);
3405 PL_beginav = newAV();
3406 SAVEFREESV(PL_beginav);
3407 SAVESPTR(PL_unitcheckav);
3408 PL_unitcheckav = newAV();
3409 SAVEFREESV(PL_unitcheckav);
3412 SAVEBOOL(PL_madskills);
3416 ENTER_with_name("evalcomp");
3417 SAVESPTR(PL_compcv);
3420 /* try to compile it */
3422 PL_eval_root = NULL;
3423 PL_curcop = &PL_compiling;
3424 if ((saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3425 PL_in_eval |= EVAL_KEEPERR;
3432 hv_clear(GvHV(PL_hintgv));
3435 PL_hints = saveop->op_private & OPpEVAL_COPHH
3436 ? oldcurcop->cop_hints : saveop->op_targ;
3438 /* making 'use re eval' not be in scope when compiling the
3439 * qr/mabye_has_runtime_code_block/ ensures that we don't get
3440 * infinite recursion when S_has_runtime_code() gives a false
3441 * positive: the second time round, HINT_RE_EVAL isn't set so we
3442 * don't bother calling S_has_runtime_code() */
3443 if (PL_in_eval & EVAL_RE_REPARSING)
3444 PL_hints &= ~HINT_RE_EVAL;
3447 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3448 SvREFCNT_dec(GvHV(PL_hintgv));
3449 GvHV(PL_hintgv) = hh;
3452 SAVECOMPILEWARNINGS();
3454 if (PL_dowarn & G_WARN_ALL_ON)
3455 PL_compiling.cop_warnings = pWARN_ALL ;
3456 else if (PL_dowarn & G_WARN_ALL_OFF)
3457 PL_compiling.cop_warnings = pWARN_NONE ;
3459 PL_compiling.cop_warnings = pWARN_STD ;
3462 PL_compiling.cop_warnings =
3463 DUP_WARNINGS(oldcurcop->cop_warnings);
3464 cophh_free(CopHINTHASH_get(&PL_compiling));
3465 if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) {
3466 /* The label, if present, is the first entry on the chain. So rather
3467 than writing a blank label in front of it (which involves an
3468 allocation), just use the next entry in the chain. */
3469 PL_compiling.cop_hints_hash
3470 = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next);
3471 /* Check the assumption that this removed the label. */
3472 assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3475 PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash);
3478 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3480 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3481 * so honour CATCH_GET and trap it here if necessary */
3483 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3485 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3486 SV **newsp; /* Used by POPBLOCK. */
3488 I32 optype; /* Used by POPEVAL. */
3494 PERL_UNUSED_VAR(newsp);
3495 PERL_UNUSED_VAR(optype);
3497 /* note that if yystatus == 3, then the EVAL CX block has already
3498 * been popped, and various vars restored */
3500 if (yystatus != 3) {
3502 op_free(PL_eval_root);
3503 PL_eval_root = NULL;
3505 SP = PL_stack_base + POPMARK; /* pop original mark */
3506 POPBLOCK(cx,PL_curpm);
3508 namesv = cx->blk_eval.old_namesv;
3509 /* POPBLOCK renders LEAVE_with_name("evalcomp") unnecessary. */
3510 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3516 /* If cx is still NULL, it means that we didn't go in the
3517 * POPEVAL branch. */
3518 cx = &cxstack[cxstack_ix];
3519 assert(CxTYPE(cx) == CXt_EVAL);
3520 namesv = cx->blk_eval.old_namesv;
3522 (void)hv_store(GvHVn(PL_incgv),
3523 SvPVX_const(namesv),
3524 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
3526 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
3529 : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
3532 if (!*(SvPV_nolen_const(errsv))) {
3533 sv_setpvs(errsv, "Compilation error");
3536 if (gimme != G_ARRAY) PUSHs(&PL_sv_undef);
3541 LEAVE_with_name("evalcomp");
3543 CopLINE_set(&PL_compiling, 0);
3544 SAVEFREEOP(PL_eval_root);
3545 cv_forget_slab(evalcv);
3547 DEBUG_x(dump_eval());
3549 /* Register with debugger: */
3550 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
3551 CV * const cv = get_cvs("DB::postponed", 0);
3555 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3557 call_sv(MUTABLE_SV(cv), G_DISCARD);
3561 if (PL_unitcheckav) {
3562 OP *es = PL_eval_start;
3563 call_list(PL_scopestack_ix, PL_unitcheckav);
3567 /* compiled okay, so do it */
3569 CvDEPTH(evalcv) = 1;
3570 SP = PL_stack_base + POPMARK; /* pop original mark */
3571 PL_op = saveop; /* The caller may need it. */
3572 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3579 S_check_type_and_open(pTHX_ SV *name)
3582 const char *p = SvPV_nolen_const(name);
3583 const int st_rc = PerlLIO_stat(p, &st);
3585 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3587 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3591 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3592 return PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3594 return PerlIO_open(p, PERL_SCRIPT_MODE);
3598 #ifndef PERL_DISABLE_PMC
3600 S_doopen_pm(pTHX_ SV *name)
3603 const char *p = SvPV_const(name, namelen);
3605 PERL_ARGS_ASSERT_DOOPEN_PM;
3607 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3608 SV *const pmcsv = sv_newmortal();
3611 SvSetSV_nosteal(pmcsv,name);
3612 sv_catpvn(pmcsv, "c", 1);
3614 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3615 return check_type_and_open(pmcsv);
3617 return check_type_and_open(name);
3620 # define doopen_pm(name) check_type_and_open(name)
3621 #endif /* !PERL_DISABLE_PMC */
3623 /* require doesn't search for absolute names, or when the name is
3624 explicity relative the current directory */
3625 PERL_STATIC_INLINE bool
3626 S_path_is_searchable(const char *name)
3628 PERL_ARGS_ASSERT_PATH_IS_SEARCHABLE;
3630 if (PERL_FILE_IS_ABSOLUTE(name)
3632 || (*name == '.' && ((name[1] == '/' ||
3633 (name[1] == '.' && name[2] == '/'))
3634 || (name[1] == '\\' ||
3635 ( name[1] == '.' && name[2] == '\\')))
3638 || (*name == '.' && (name[1] == '/' ||
3639 (name[1] == '.' && name[2] == '/')))
3659 int vms_unixname = 0;
3664 const char *tryname = NULL;
3666 const I32 gimme = GIMME_V;
3667 int filter_has_file = 0;
3668 PerlIO *tryrsfp = NULL;
3669 SV *filter_cache = NULL;
3670 SV *filter_state = NULL;
3671 SV *filter_sub = NULL;
3676 bool path_searchable;
3679 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3680 sv = sv_2mortal(new_version(sv));
3681 if (!Perl_sv_derived_from_pvn(aTHX_ PL_patchlevel, STR_WITH_LEN("version"), 0))
3682 upg_version(PL_patchlevel, TRUE);
3683 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3684 if ( vcmp(sv,PL_patchlevel) <= 0 )
3685 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3686 SVfARG(sv_2mortal(vnormal(sv))),
3687 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3691 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3694 SV * const req = SvRV(sv);
3695 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3697 /* get the left hand term */
3698 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3700 first = SvIV(*av_fetch(lav,0,0));
3701 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3702 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3703 || av_len(lav) > 1 /* FP with > 3 digits */
3704 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3706 DIE(aTHX_ "Perl %"SVf" required--this is only "
3708 SVfARG(sv_2mortal(vnormal(req))),
3709 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3712 else { /* probably 'use 5.10' or 'use 5.8' */
3717 second = SvIV(*av_fetch(lav,1,0));
3719 second /= second >= 600 ? 100 : 10;
3720 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3721 (int)first, (int)second);
3722 upg_version(hintsv, TRUE);
3724 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3725 "--this is only %"SVf", stopped",
3726 SVfARG(sv_2mortal(vnormal(req))),
3727 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3728 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3736 name = SvPV_const(sv, len);
3737 if (!(name && len > 0 && *name))
3738 DIE(aTHX_ "Null filename used");
3739 TAINT_PROPER("require");
3741 path_searchable = path_is_searchable(name);
3744 /* The key in the %ENV hash is in the syntax of file passed as the argument
3745 * usually this is in UNIX format, but sometimes in VMS format, which
3746 * can result in a module being pulled in more than once.
3747 * To prevent this, the key must be stored in UNIX format if the VMS
3748 * name can be translated to UNIX.
3751 if ((unixnamebuf = SvPVX(sv_2mortal(newSVpv("", VMS_MAXRSS-1))))
3752 && (unixname = tounixspec(name, unixnamebuf)) != NULL) {
3753 unixlen = strlen(unixname);
3759 /* if not VMS or VMS name can not be translated to UNIX, pass it
3762 unixname = (char *) name;
3765 if (PL_op->op_type == OP_REQUIRE) {
3766 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3767 unixname, unixlen, 0);
3769 if (*svp != &PL_sv_undef)
3772 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3773 "Compilation failed in require", unixname);
3777 LOADING_FILE_PROBE(unixname);
3779 /* prepare to compile file */
3781 if (!path_searchable) {
3782 /* At this point, name is SvPVX(sv) */
3784 tryrsfp = doopen_pm(sv);
3786 if (!tryrsfp && !(errno == EACCES && !path_searchable)) {
3787 AV * const ar = GvAVn(PL_incgv);
3793 namesv = newSV_type(SVt_PV);
3794 for (i = 0; i <= AvFILL(ar); i++) {
3795 SV * const dirsv = *av_fetch(ar, i, TRUE);
3797 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3804 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3805 && !sv_isobject(loader))
3807 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3810 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3811 PTR2UV(SvRV(dirsv)), name);
3812 tryname = SvPVX_const(namesv);
3815 ENTER_with_name("call_INC");
3823 if (sv_isobject(loader))
3824 count = call_method("INC", G_ARRAY);
3826 count = call_sv(loader, G_ARRAY);
3836 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3837 && !isGV_with_GP(SvRV(arg))) {
3838 filter_cache = SvRV(arg);
3845 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
3849 if (isGV_with_GP(arg)) {
3850 IO * const io = GvIO((const GV *)arg);
3855 tryrsfp = IoIFP(io);
3856 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3857 PerlIO_close(IoOFP(io));
3868 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3870 SvREFCNT_inc_simple_void_NN(filter_sub);
3873 filter_state = SP[i];
3874 SvREFCNT_inc_simple_void(filter_state);
3878 if (!tryrsfp && (filter_cache || filter_sub)) {
3879 tryrsfp = PerlIO_open(BIT_BUCKET,
3887 LEAVE_with_name("call_INC");
3889 /* Adjust file name if the hook has set an %INC entry.
3890 This needs to happen after the FREETMPS above. */
3891 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3893 tryname = SvPV_nolen_const(*svp);
3900 filter_has_file = 0;
3901 filter_cache = NULL;
3903 SvREFCNT_dec(filter_state);
3904 filter_state = NULL;
3907 SvREFCNT_dec(filter_sub);
3912 if (path_searchable) {
3917 dir = SvPV_const(dirsv, dirlen);
3924 if (((unixdirbuf = SvPVX(sv_2mortal(newSVpv("", VMS_MAXRSS-1)))) == NULL)
3925 || ((unixdir = tounixpath(dir, unixdirbuf)) == NULL))
3927 sv_setpv(namesv, unixdir);
3928 sv_catpv(namesv, unixname);
3930 # ifdef __SYMBIAN32__
3931 if (PL_origfilename[0] &&
3932 PL_origfilename[1] == ':' &&
3933 !(dir[0] && dir[1] == ':'))
3934 Perl_sv_setpvf(aTHX_ namesv,
3939 Perl_sv_setpvf(aTHX_ namesv,
3943 /* The equivalent of
3944 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3945 but without the need to parse the format string, or
3946 call strlen on either pointer, and with the correct
3947 allocation up front. */
3949 char *tmp = SvGROW(namesv, dirlen + len + 2);
3951 memcpy(tmp, dir, dirlen);
3954 /* Avoid '<dir>//<file>' */
3955 if (!dirlen || *(tmp-1) != '/') {
3959 /* name came from an SV, so it will have a '\0' at the
3960 end that we can copy as part of this memcpy(). */
3961 memcpy(tmp, name, len + 1);
3963 SvCUR_set(namesv, dirlen + len + 1);
3968 TAINT_PROPER("require");
3969 tryname = SvPVX_const(namesv);
3970 tryrsfp = doopen_pm(namesv);
3972 if (tryname[0] == '.' && tryname[1] == '/') {
3974 while (*++tryname == '/') {}
3978 else if (errno == EMFILE || errno == EACCES) {
3979 /* no point in trying other paths if out of handles;
3980 * on the other hand, if we couldn't open one of the
3981 * files, then going on with the search could lead to
3982 * unexpected results; see perl #113422
3991 saved_errno = errno; /* sv_2mortal can realloc things */
3994 if (PL_op->op_type == OP_REQUIRE) {
3995 if(saved_errno == EMFILE || saved_errno == EACCES) {
3996 /* diag_listed_as: Can't locate %s */
3997 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(saved_errno));
3999 if (namesv) { /* did we lookup @INC? */
4000 AV * const ar = GvAVn(PL_incgv);
4002 SV *const msg = newSVpvs_flags("", SVs_TEMP);
4003 SV *const inc = newSVpvs_flags("", SVs_TEMP);
4004 for (i = 0; i <= AvFILL(ar); i++) {
4005 sv_catpvs(inc, " ");
4006 sv_catsv(inc, *av_fetch(ar, i, TRUE));
4008 if (len >= 4 && memEQ(name + len - 3, ".pm", 4)) {
4009 const char *c, *e = name + len - 3;
4010 sv_catpv(msg, " (you may need to install the ");
4011 for (c = name; c < e; c++) {
4013 sv_catpvn(msg, "::", 2);
4016 sv_catpvn(msg, c, 1);
4019 sv_catpv(msg, " module)");
4021 else if (len >= 2 && memEQ(name + len - 2, ".h", 3)) {
4022 sv_catpv(msg, " (change .h to .ph maybe?) (did you run h2ph?)");
4024 else if (len >= 3 && memEQ(name + len - 3, ".ph", 4)) {
4025 sv_catpv(msg, " (did you run h2ph?)");
4028 /* diag_listed_as: Can't locate %s */
4030 "Can't locate %s in @INC%" SVf " (@INC contains:%" SVf ")",
4034 DIE(aTHX_ "Can't locate %s", name);
4041 SETERRNO(0, SS_NORMAL);
4043 /* Assume success here to prevent recursive requirement. */
4044 /* name is never assigned to again, so len is still strlen(name) */
4045 /* Check whether a hook in @INC has already filled %INC */
4047 (void)hv_store(GvHVn(PL_incgv),
4048 unixname, unixlen, newSVpv(tryname,0),0);
4050 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
4052 (void)hv_store(GvHVn(PL_incgv),
4053 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
4056 ENTER_with_name("eval");
4058 SAVECOPFILE_FREE(&PL_compiling);
4059 CopFILE_set(&PL_compiling, tryname);
4060 lex_start(NULL, tryrsfp, 0);
4062 if (filter_sub || filter_cache) {
4063 /* We can use the SvPV of the filter PVIO itself as our cache, rather
4064 than hanging another SV from it. In turn, filter_add() optionally
4065 takes the SV to use as the filter (or creates a new SV if passed
4066 NULL), so simply pass in whatever value filter_cache has. */
4067 SV * const fc = filter_cache ? newSV(0) : NULL;
4069 if (fc) sv_copypv(fc, filter_cache);
4070 datasv = filter_add(S_run_user_filter, fc);
4071 IoLINES(datasv) = filter_has_file;
4072 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
4073 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
4076 /* switch to eval mode */
4077 PUSHBLOCK(cx, CXt_EVAL, SP);
4079 cx->blk_eval.retop = PL_op->op_next;
4081 SAVECOPLINE(&PL_compiling);
4082 CopLINE_set(&PL_compiling, 0);
4086 /* Store and reset encoding. */
4087 encoding = PL_encoding;
4090 if (doeval(gimme, NULL, PL_curcop->cop_seq, NULL))
4091 op = DOCATCH(PL_eval_start);
4093 op = PL_op->op_next;
4095 /* Restore encoding. */
4096 PL_encoding = encoding;
4098 LOADED_FILE_PROBE(unixname);
4103 /* This is a op added to hold the hints hash for
4104 pp_entereval. The hash can be modified by the code
4105 being eval'ed, so we return a copy instead. */
4111 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
4121 const I32 gimme = GIMME_V;
4122 const U32 was = PL_breakable_sub_gen;
4123 char tbuf[TYPE_DIGITS(long) + 12];
4124 bool saved_delete = FALSE;
4125 char *tmpbuf = tbuf;
4128 U32 seq, lex_flags = 0;
4129 HV *saved_hh = NULL;
4130 const bool bytes = PL_op->op_private & OPpEVAL_BYTES;
4132 if (PL_op->op_private & OPpEVAL_HAS_HH) {
4133 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
4135 else if (PL_hints & HINT_LOCALIZE_HH || (
4136 PL_op->op_private & OPpEVAL_COPHH
4137 && PL_curcop->cop_hints & HINT_LOCALIZE_HH
4139 saved_hh = cop_hints_2hv(PL_curcop, 0);
4140 hv_magic(saved_hh, NULL, PERL_MAGIC_hints);
4144 /* make sure we've got a plain PV (no overload etc) before testing
4145 * for taint. Making a copy here is probably overkill, but better
4146 * safe than sorry */
4148 const char * const p = SvPV_const(sv, len);
4150 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
4151 lex_flags |= LEX_START_COPIED;
4153 if (bytes && SvUTF8(sv))
4154 SvPVbyte_force(sv, len);
4156 else if (bytes && SvUTF8(sv)) {
4157 /* Don't modify someone else's scalar */
4160 (void)sv_2mortal(sv);
4161 SvPVbyte_force(sv,len);
4162 lex_flags |= LEX_START_COPIED;
4165 TAINT_IF(SvTAINTED(sv));
4166 TAINT_PROPER("eval");
4168 ENTER_with_name("eval");
4169 lex_start(sv, NULL, lex_flags | (PL_op->op_private & OPpEVAL_UNICODE
4170 ? LEX_IGNORE_UTF8_HINTS
4171 : bytes ? LEX_EVALBYTES : LEX_START_SAME_FILTER
4176 /* switch to eval mode */
4178 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
4179 SV * const temp_sv = sv_newmortal();
4180 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
4181 (unsigned long)++PL_evalseq,
4182 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4183 tmpbuf = SvPVX(temp_sv);
4184 len = SvCUR(temp_sv);
4187 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
4188 SAVECOPFILE_FREE(&PL_compiling);
4189 CopFILE_set(&PL_compiling, tmpbuf+2);
4190 SAVECOPLINE(&PL_compiling);
4191 CopLINE_set(&PL_compiling, 1);
4192 /* special case: an eval '' executed within the DB package gets lexically
4193 * placed in the first non-DB CV rather than the current CV - this
4194 * allows the debugger to execute code, find lexicals etc, in the
4195 * scope of the code being debugged. Passing &seq gets find_runcv
4196 * to do the dirty work for us */
4197 runcv = find_runcv(&seq);
4199 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
4201 cx->blk_eval.retop = PL_op->op_next;
4203 /* prepare to compile string */
4205 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
4206 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
4208 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
4209 deleting the eval's FILEGV from the stash before gv_check() runs
4210 (i.e. before run-time proper). To work around the coredump that
4211 ensues, we always turn GvMULTI_on for any globals that were
4212 introduced within evals. See force_ident(). GSAR 96-10-12 */
4213 char *const safestr = savepvn(tmpbuf, len);
4214 SAVEDELETE(PL_defstash, safestr, len);
4215 saved_delete = TRUE;
4220 if (doeval(gimme, runcv, seq, saved_hh)) {
4221 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4222 ? (PERLDB_LINE || PERLDB_SAVESRC)
4223 : PERLDB_SAVESRC_NOSUBS) {
4224 /* Retain the filegv we created. */
4225 } else if (!saved_delete) {
4226 char *const safestr = savepvn(tmpbuf, len);
4227 SAVEDELETE(PL_defstash, safestr, len);
4229 return DOCATCH(PL_eval_start);
4231 /* We have already left the scope set up earlier thanks to the LEAVE
4233 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4234 ? (PERLDB_LINE || PERLDB_SAVESRC)
4235 : PERLDB_SAVESRC_INVALID) {
4236 /* Retain the filegv we created. */
4237 } else if (!saved_delete) {
4238 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
4240 return PL_op->op_next;
4252 const U8 save_flags = PL_op -> op_flags;
4260 namesv = cx->blk_eval.old_namesv;
4261 retop = cx->blk_eval.retop;
4262 evalcv = cx->blk_eval.cv;
4265 SP = adjust_stack_on_leave((gimme == G_VOID) ? SP : newsp, SP, newsp,
4267 PL_curpm = newpm; /* Don't pop $1 et al till now */
4270 assert(CvDEPTH(evalcv) == 1);
4272 CvDEPTH(evalcv) = 0;
4274 if (optype == OP_REQUIRE &&
4275 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4277 /* Unassume the success we assumed earlier. */
4278 (void)hv_delete(GvHVn(PL_incgv),
4279 SvPVX_const(namesv),
4280 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
4282 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4284 /* die_unwind() did LEAVE, or we won't be here */
4287 LEAVE_with_name("eval");
4288 if (!(save_flags & OPf_SPECIAL)) {
4296 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4297 close to the related Perl_create_eval_scope. */
4299 Perl_delete_eval_scope(pTHX)
4310 LEAVE_with_name("eval_scope");
4311 PERL_UNUSED_VAR(newsp);
4312 PERL_UNUSED_VAR(gimme);
4313 PERL_UNUSED_VAR(optype);
4316 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4317 also needed by Perl_fold_constants. */
4319 Perl_create_eval_scope(pTHX_ U32 flags)
4322 const I32 gimme = GIMME_V;
4324 ENTER_with_name("eval_scope");
4327 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4330 PL_in_eval = EVAL_INEVAL;
4331 if (flags & G_KEEPERR)
4332 PL_in_eval |= EVAL_KEEPERR;
4335 if (flags & G_FAKINGEVAL) {
4336 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4344 PERL_CONTEXT * const cx = create_eval_scope(0);
4345 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4346 return DOCATCH(PL_op->op_next);
4361 PERL_UNUSED_VAR(optype);
4364 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
4365 PL_curpm = newpm; /* Don't pop $1 et al till now */
4367 LEAVE_with_name("eval_scope");
4376 const I32 gimme = GIMME_V;
4378 ENTER_with_name("given");
4381 if (PL_op->op_targ) {
4382 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
4383 SvREFCNT_dec(PAD_SVl(PL_op->op_targ));
4384 PAD_SVl(PL_op->op_targ) = SvREFCNT_inc_NN(POPs);
4391 PUSHBLOCK(cx, CXt_GIVEN, SP);
4404 PERL_UNUSED_CONTEXT;
4407 assert(CxTYPE(cx) == CXt_GIVEN);
4410 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
4411 PL_curpm = newpm; /* Don't pop $1 et al till now */
4413 LEAVE_with_name("given");
4417 /* Helper routines used by pp_smartmatch */
4419 S_make_matcher(pTHX_ REGEXP *re)
4422 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4424 PERL_ARGS_ASSERT_MAKE_MATCHER;
4426 PM_SETRE(matcher, ReREFCNT_inc(re));
4428 SAVEFREEOP((OP *) matcher);
4429 ENTER_with_name("matcher"); SAVETMPS;
4435 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4440 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4442 PL_op = (OP *) matcher;
4445 (void) Perl_pp_match(aTHX);
4447 return (SvTRUEx(POPs));
4451 S_destroy_matcher(pTHX_ PMOP *matcher)
4455 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4456 PERL_UNUSED_ARG(matcher);
4459 LEAVE_with_name("matcher");
4462 /* Do a smart match */
4465 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4466 return do_smartmatch(NULL, NULL, 0);
4469 /* This version of do_smartmatch() implements the
4470 * table of smart matches that is found in perlsyn.
4473 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied)
4478 bool object_on_left = FALSE;
4479 SV *e = TOPs; /* e is for 'expression' */
4480 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4482 /* Take care only to invoke mg_get() once for each argument.
4483 * Currently we do this by copying the SV if it's magical. */
4485 if (!copied && SvGMAGICAL(d))
4486 d = sv_mortalcopy(d);
4493 e = sv_mortalcopy(e);
4495 /* First of all, handle overload magic of the rightmost argument */
4498 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4499 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4501 tmpsv = amagic_call(d, e, smart_amg, AMGf_noleft);
4508 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4511 SP -= 2; /* Pop the values */
4516 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4523 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4524 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4525 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4527 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4528 object_on_left = TRUE;
4531 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4533 if (object_on_left) {
4534 goto sm_any_sub; /* Treat objects like scalars */
4536 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4537 /* Test sub truth for each key */
4539 bool andedresults = TRUE;
4540 HV *hv = (HV*) SvRV(d);
4541 I32 numkeys = hv_iterinit(hv);
4542 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4545 while ( (he = hv_iternext(hv)) ) {
4546 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4547 ENTER_with_name("smartmatch_hash_key_test");
4550 PUSHs(hv_iterkeysv(he));
4552 c = call_sv(e, G_SCALAR);
4555 andedresults = FALSE;
4557 andedresults = SvTRUEx(POPs) && andedresults;
4559 LEAVE_with_name("smartmatch_hash_key_test");
4566 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4567 /* Test sub truth for each element */
4569 bool andedresults = TRUE;
4570 AV *av = (AV*) SvRV(d);
4571 const I32 len = av_len(av);
4572 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4575 for (i = 0; i <= len; ++i) {
4576 SV * const * const svp = av_fetch(av, i, FALSE);
4577 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4578 ENTER_with_name("smartmatch_array_elem_test");
4584 c = call_sv(e, G_SCALAR);
4587 andedresults = FALSE;
4589 andedresults = SvTRUEx(POPs) && andedresults;
4591 LEAVE_with_name("smartmatch_array_elem_test");
4600 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4601 ENTER_with_name("smartmatch_coderef");
4606 c = call_sv(e, G_SCALAR);
4610 else if (SvTEMP(TOPs))
4611 SvREFCNT_inc_void(TOPs);
4613 LEAVE_with_name("smartmatch_coderef");
4618 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4619 if (object_on_left) {
4620 goto sm_any_hash; /* Treat objects like scalars */
4622 else if (!SvOK(d)) {
4623 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4626 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4627 /* Check that the key-sets are identical */
4629 HV *other_hv = MUTABLE_HV(SvRV(d));
4631 bool other_tied = FALSE;
4632 U32 this_key_count = 0,
4633 other_key_count = 0;
4634 HV *hv = MUTABLE_HV(SvRV(e));
4636 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
4637 /* Tied hashes don't know how many keys they have. */
4638 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
4641 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
4642 HV * const temp = other_hv;
4647 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
4650 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
4653 /* The hashes have the same number of keys, so it suffices
4654 to check that one is a subset of the other. */
4655 (void) hv_iterinit(hv);
4656 while ( (he = hv_iternext(hv)) ) {
4657 SV *key = hv_iterkeysv(he);
4659 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
4662 if(!hv_exists_ent(other_hv, key, 0)) {
4663 (void) hv_iterinit(hv); /* reset iterator */
4669 (void) hv_iterinit(other_hv);
4670 while ( hv_iternext(other_hv) )
4674 other_key_count = HvUSEDKEYS(other_hv);
4676 if (this_key_count != other_key_count)
4681 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4682 AV * const other_av = MUTABLE_AV(SvRV(d));
4683 const I32 other_len = av_len(other_av) + 1;
4685 HV *hv = MUTABLE_HV(SvRV(e));
4687 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n"));
4688 for (i = 0; i < other_len; ++i) {
4689 SV ** const svp = av_fetch(other_av, i, FALSE);
4690 DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n"));
4691 if (svp) { /* ??? When can this not happen? */
4692 if (hv_exists_ent(hv, *svp, 0))
4698 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4699 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n"));
4702 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4704 HV *hv = MUTABLE_HV(SvRV(e));
4706 (void) hv_iterinit(hv);
4707 while ( (he = hv_iternext(hv)) ) {
4708 DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n"));
4709 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
4710 (void) hv_iterinit(hv);
4711 destroy_matcher(matcher);
4715 destroy_matcher(matcher);
4721 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n"));
4722 if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0))
4729 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) {
4730 if (object_on_left) {
4731 goto sm_any_array; /* Treat objects like scalars */
4733 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4734 AV * const other_av = MUTABLE_AV(SvRV(e));
4735 const I32 other_len = av_len(other_av) + 1;
4738 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n"));
4739 for (i = 0; i < other_len; ++i) {
4740 SV ** const svp = av_fetch(other_av, i, FALSE);
4742 DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n"));
4743 if (svp) { /* ??? When can this not happen? */
4744 if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0))
4750 if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4751 AV *other_av = MUTABLE_AV(SvRV(d));
4752 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n"));
4753 if (av_len(MUTABLE_AV(SvRV(e))) != av_len(other_av))
4757 const I32 other_len = av_len(other_av);
4759 if (NULL == seen_this) {
4760 seen_this = newHV();
4761 (void) sv_2mortal(MUTABLE_SV(seen_this));
4763 if (NULL == seen_other) {
4764 seen_other = newHV();
4765 (void) sv_2mortal(MUTABLE_SV(seen_other));
4767 for(i = 0; i <= other_len; ++i) {
4768 SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4769 SV * const * const other_elem = av_fetch(other_av, i, FALSE);
4771 if (!this_elem || !other_elem) {
4772 if ((this_elem && SvOK(*this_elem))
4773 || (other_elem && SvOK(*other_elem)))
4776 else if (hv_exists_ent(seen_this,
4777 sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) ||
4778 hv_exists_ent(seen_other,
4779 sv_2mortal(newSViv(PTR2IV(*other_elem))), 0))
4781 if (*this_elem != *other_elem)
4785 (void)hv_store_ent(seen_this,
4786 sv_2mortal(newSViv(PTR2IV(*this_elem))),
4788 (void)hv_store_ent(seen_other,
4789 sv_2mortal(newSViv(PTR2IV(*other_elem))),
4795 DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n"));
4796 (void) do_smartmatch(seen_this, seen_other, 0);
4798 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4807 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4808 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n"));
4811 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4812 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4815 for(i = 0; i <= this_len; ++i) {
4816 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4817 DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n"));
4818 if (svp && matcher_matches_sv(matcher, *svp)) {
4819 destroy_matcher(matcher);
4823 destroy_matcher(matcher);
4827 else if (!SvOK(d)) {
4828 /* undef ~~ array */
4829 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4832 DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n"));
4833 for (i = 0; i <= this_len; ++i) {
4834 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4835 DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n"));
4836 if (!svp || !SvOK(*svp))
4845 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4847 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n"));
4848 for (i = 0; i <= this_len; ++i) {
4849 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4856 /* infinite recursion isn't supposed to happen here */
4857 DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n"));
4858 (void) do_smartmatch(NULL, NULL, 1);
4860 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4869 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) {
4870 if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4871 SV *t = d; d = e; e = t;
4872 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n"));
4875 else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4876 SV *t = d; d = e; e = t;
4877 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n"));
4878 goto sm_regex_array;
4881 PMOP * const matcher = make_matcher((REGEXP*) SvRV(e));
4883 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n"));
4885 PUSHs(matcher_matches_sv(matcher, d)
4888 destroy_matcher(matcher);
4893 /* See if there is overload magic on left */
4894 else if (object_on_left && SvAMAGIC(d)) {
4896 DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n"));
4897 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4900 tmpsv = amagic_call(d, e, smart_amg, AMGf_noright);
4908 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n"));
4911 else if (!SvOK(d)) {
4912 /* undef ~~ scalar ; we already know that the scalar is SvOK */
4913 DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n"));
4918 if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) {
4919 DEBUG_M(if (SvNIOK(e))
4920 Perl_deb(aTHX_ " applying rule Any-Num\n");
4922 Perl_deb(aTHX_ " applying rule Num-numish\n");
4924 /* numeric comparison */
4927 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
4928 (void) Perl_pp_i_eq(aTHX);
4930 (void) Perl_pp_eq(aTHX);
4938 /* As a last resort, use string comparison */
4939 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n"));
4942 return Perl_pp_seq(aTHX);
4949 const I32 gimme = GIMME_V;
4951 /* This is essentially an optimization: if the match
4952 fails, we don't want to push a context and then
4953 pop it again right away, so we skip straight
4954 to the op that follows the leavewhen.
4955 RETURNOP calls PUTBACK which restores the stack pointer after the POPs.
4957 if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs))
4958 RETURNOP(cLOGOP->op_other->op_next);
4960 ENTER_with_name("when");
4963 PUSHBLOCK(cx, CXt_WHEN, SP);
4978 cxix = dopoptogiven(cxstack_ix);
4980 /* diag_listed_as: Can't "when" outside a topicalizer */
4981 DIE(aTHX_ "Can't \"%s\" outside a topicalizer",
4982 PL_op->op_flags & OPf_SPECIAL ? "default" : "when");
4985 assert(CxTYPE(cx) == CXt_WHEN);
4988 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
4989 PL_curpm = newpm; /* pop $1 et al */
4991 LEAVE_with_name("when");
4993 if (cxix < cxstack_ix)
4996 cx = &cxstack[cxix];
4998 if (CxFOREACH(cx)) {
4999 /* clear off anything above the scope we're re-entering */
5000 I32 inner = PL_scopestack_ix;
5003 if (PL_scopestack_ix < inner)
5004 leave_scope(PL_scopestack[PL_scopestack_ix]);
5005 PL_curcop = cx->blk_oldcop;
5008 return cx->blk_loop.my_op->op_nextop;
5012 RETURNOP(cx->blk_givwhen.leave_op);
5025 PERL_UNUSED_VAR(gimme);
5027 cxix = dopoptowhen(cxstack_ix);
5029 DIE(aTHX_ "Can't \"continue\" outside a when block");
5031 if (cxix < cxstack_ix)
5035 assert(CxTYPE(cx) == CXt_WHEN);
5038 PL_curpm = newpm; /* pop $1 et al */
5040 LEAVE_with_name("when");
5041 RETURNOP(cx->blk_givwhen.leave_op->op_next);
5050 cxix = dopoptogiven(cxstack_ix);
5052 DIE(aTHX_ "Can't \"break\" outside a given block");
5054 cx = &cxstack[cxix];
5056 DIE(aTHX_ "Can't \"break\" in a loop topicalizer");
5058 if (cxix < cxstack_ix)
5061 /* Restore the sp at the time we entered the given block */
5064 return cx->blk_givwhen.leave_op;
5068 S_doparseform(pTHX_ SV *sv)
5071 char *s = SvPV(sv, len);
5073 char *base = NULL; /* start of current field */
5074 I32 skipspaces = 0; /* number of contiguous spaces seen */
5075 bool noblank = FALSE; /* ~ or ~~ seen on this line */
5076 bool repeat = FALSE; /* ~~ seen on this line */
5077 bool postspace = FALSE; /* a text field may need right padding */
5080 U32 *linepc = NULL; /* position of last FF_LINEMARK */
5082 bool ischop; /* it's a ^ rather than a @ */
5083 bool unchopnum = FALSE; /* at least one @ (i.e. non-chop) num field seen */
5084 int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */
5088 PERL_ARGS_ASSERT_DOPARSEFORM;
5091 Perl_croak(aTHX_ "Null picture in formline");
5093 if (SvTYPE(sv) >= SVt_PVMG) {
5094 /* This might, of course, still return NULL. */
5095 mg = mg_find(sv, PERL_MAGIC_fm);
5097 sv_upgrade(sv, SVt_PVMG);
5101 /* still the same as previously-compiled string? */
5102 SV *old = mg->mg_obj;
5103 if ( !(!!SvUTF8(old) ^ !!SvUTF8(sv))
5104 && len == SvCUR(old)
5105 && strnEQ(SvPVX(old), SvPVX(sv), len)
5107 DEBUG_f(PerlIO_printf(Perl_debug_log,"Re-using compiled format\n"));
5111 DEBUG_f(PerlIO_printf(Perl_debug_log, "Re-compiling format\n"));
5112 Safefree(mg->mg_ptr);
5118 DEBUG_f(PerlIO_printf(Perl_debug_log, "Compiling format\n"));
5119 mg = sv_magicext(sv, NULL, PERL_MAGIC_fm, &PL_vtbl_fm, NULL, 0);
5122 sv_copy = newSVpvn_utf8(s, len, SvUTF8(sv));
5123 s = SvPV(sv_copy, len); /* work on the copy, not the original */
5127 /* estimate the buffer size needed */
5128 for (base = s; s <= send; s++) {
5129 if (*s == '\n' || *s == '@' || *s == '^')
5135 Newx(fops, maxops, U32);
5140 *fpc++ = FF_LINEMARK;
5141 noblank = repeat = FALSE;
5159 case ' ': case '\t':
5166 } /* else FALL THROUGH */
5174 *fpc++ = FF_LITERAL;
5182 *fpc++ = (U32)skipspaces;
5186 *fpc++ = FF_NEWLINE;
5190 arg = fpc - linepc + 1;
5197 *fpc++ = FF_LINEMARK;
5198 noblank = repeat = FALSE;
5207 ischop = s[-1] == '^';
5213 arg = (s - base) - 1;
5215 *fpc++ = FF_LITERAL;
5221 if (*s == '*') { /* @* or ^* */
5223 *fpc++ = 2; /* skip the @* or ^* */
5225 *fpc++ = FF_LINESNGL;
5228 *fpc++ = FF_LINEGLOB;
5230 else if (*s == '#' || (*s == '.' && s[1] == '#')) { /* @###, ^### */
5231 arg = ischop ? FORM_NUM_BLANK : 0;
5236 const char * const f = ++s;
5239 arg |= FORM_NUM_POINT + (s - f);
5241 *fpc++ = s - base; /* fieldsize for FETCH */
5242 *fpc++ = FF_DECIMAL;
5244 unchopnum |= ! ischop;
5246 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
5247 arg = ischop ? FORM_NUM_BLANK : 0;
5249 s++; /* skip the '0' first */
5253 const char * const f = ++s;
5256 arg |= FORM_NUM_POINT + (s - f);
5258 *fpc++ = s - base; /* fieldsize for FETCH */
5259 *fpc++ = FF_0DECIMAL;
5261 unchopnum |= ! ischop;
5263 else { /* text field */
5265 bool ismore = FALSE;
5268 while (*++s == '>') ;
5269 prespace = FF_SPACE;
5271 else if (*s == '|') {
5272 while (*++s == '|') ;
5273 prespace = FF_HALFSPACE;
5278 while (*++s == '<') ;
5281 if (*s == '.' && s[1] == '.' && s[2] == '.') {
5285 *fpc++ = s - base; /* fieldsize for FETCH */
5287 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
5290 *fpc++ = (U32)prespace; /* add SPACE or HALFSPACE */
5304 assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */
5307 mg->mg_ptr = (char *) fops;
5308 mg->mg_len = arg * sizeof(U32);
5309 mg->mg_obj = sv_copy;
5310 mg->mg_flags |= MGf_REFCOUNTED;
5312 if (unchopnum && repeat)
5313 Perl_die(aTHX_ "Repeated format line will never terminate (~~ and @#)");
5320 S_num_overflow(NV value, I32 fldsize, I32 frcsize)
5322 /* Can value be printed in fldsize chars, using %*.*f ? */
5326 int intsize = fldsize - (value < 0 ? 1 : 0);
5328 if (frcsize & FORM_NUM_POINT)
5330 frcsize &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
5333 while (intsize--) pwr *= 10.0;
5334 while (frcsize--) eps /= 10.0;
5337 if (value + eps >= pwr)
5340 if (value - eps <= -pwr)
5347 S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
5350 SV * const datasv = FILTER_DATA(idx);
5351 const int filter_has_file = IoLINES(datasv);
5352 SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv));
5353 SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv));
5358 char *prune_from = NULL;
5359 bool read_from_cache = FALSE;
5363 PERL_ARGS_ASSERT_RUN_USER_FILTER;
5365 assert(maxlen >= 0);
5368 /* I was having segfault trouble under Linux 2.2.5 after a
5369 parse error occured. (Had to hack around it with a test
5370 for PL_parser->error_count == 0.) Solaris doesn't segfault --
5371 not sure where the trouble is yet. XXX */
5374 SV *const cache = datasv;
5377 const char *cache_p = SvPV(cache, cache_len);
5381 /* Running in block mode and we have some cached data already.
5383 if (cache_len >= umaxlen) {
5384 /* In fact, so much data we don't even need to call
5389 const char *const first_nl =
5390 (const char *)memchr(cache_p, '\n', cache_len);
5392 take = first_nl + 1 - cache_p;
5396 sv_catpvn(buf_sv, cache_p, take);
5397 sv_chop(cache, cache_p + take);
5398 /* Definitely not EOF */
5402 sv_catsv(buf_sv, cache);
5404 umaxlen -= cache_len;
5407 read_from_cache = TRUE;
5411 /* Filter API says that the filter appends to the contents of the buffer.
5412 Usually the buffer is "", so the details don't matter. But if it's not,
5413 then clearly what it contains is already filtered by this filter, so we
5414 don't want to pass it in a second time.
5415 I'm going to use a mortal in case the upstream filter croaks. */
5416 upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv))
5417 ? sv_newmortal() : buf_sv;
5418 SvUPGRADE(upstream, SVt_PV);
5420 if (filter_has_file) {
5421 status = FILTER_READ(idx+1, upstream, 0);
5424 if (filter_sub && status >= 0) {
5428 ENTER_with_name("call_filter_sub");
5433 DEFSV_set(upstream);
5437 PUSHs(filter_state);
5440 count = call_sv(filter_sub, G_SCALAR|G_EVAL);
5449 SV * const errsv = ERRSV;
5450 if (SvTRUE_NN(errsv))
5451 err = newSVsv(errsv);
5457 LEAVE_with_name("call_filter_sub");
5460 if (SvIsCOW(upstream)) sv_force_normal(upstream);
5461 if(!err && SvOK(upstream)) {
5462 got_p = SvPV(upstream, got_len);
5464 if (got_len > umaxlen) {
5465 prune_from = got_p + umaxlen;
5468 char *const first_nl = (char *)memchr(got_p, '\n', got_len);
5469 if (first_nl && first_nl + 1 < got_p + got_len) {
5470 /* There's a second line here... */
5471 prune_from = first_nl + 1;
5475 if (!err && prune_from) {
5476 /* Oh. Too long. Stuff some in our cache. */
5477 STRLEN cached_len = got_p + got_len - prune_from;
5478 SV *const cache = datasv;
5481 /* Cache should be empty. */
5482 assert(!SvCUR(cache));
5485 sv_setpvn(cache, prune_from, cached_len);
5486 /* If you ask for block mode, you may well split UTF-8 characters.
5487 "If it breaks, you get to keep both parts"
5488 (Your code is broken if you don't put them back together again
5489 before something notices.) */
5490 if (SvUTF8(upstream)) {
5493 SvCUR_set(upstream, got_len - cached_len);
5495 /* Can't yet be EOF */
5500 /* If they are at EOF but buf_sv has something in it, then they may never
5501 have touched the SV upstream, so it may be undefined. If we naively
5502 concatenate it then we get a warning about use of uninitialised value.
5504 if (!err && upstream != buf_sv &&
5505 (SvOK(upstream) || SvGMAGICAL(upstream))) {
5506 sv_catsv(buf_sv, upstream);
5510 IoLINES(datasv) = 0;
5512 SvREFCNT_dec(filter_state);
5513 IoTOP_GV(datasv) = NULL;
5516 SvREFCNT_dec(filter_sub);
5517 IoBOTTOM_GV(datasv) = NULL;
5519 filter_del(S_run_user_filter);
5525 if (status == 0 && read_from_cache) {
5526 /* If we read some data from the cache (and by getting here it implies
5527 that we emptied the cache) then we aren't yet at EOF, and mustn't
5528 report that to our caller. */
5536 * c-indentation-style: bsd
5538 * indent-tabs-mode: nil
5541 * ex: set ts=8 sts=4 sw=4 et: