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)
3278 PL_curcop == &PL_compiling
3280 : PL_curcop->cop_seq;
3282 for (si = PL_curstackinfo; si; si = si->si_prev) {
3284 for (ix = si->si_cxix; ix >= 0; ix--) {
3285 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3287 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3288 cv = cx->blk_sub.cv;
3289 /* skip DB:: code */
3290 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3291 *db_seqp = cx->blk_oldcop->cop_seq;
3294 if (cx->cx_type & CXp_SUB_RE)
3297 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3298 cv = cx->blk_eval.cv;
3301 case FIND_RUNCV_padid_eq:
3303 || PadlistNAMES(CvPADLIST(cv)) != INT2PTR(PADNAMELIST *, arg))
3306 case FIND_RUNCV_level_eq:
3307 if (level++ != arg) continue;
3315 return cond == FIND_RUNCV_padid_eq ? NULL : PL_main_cv;
3319 /* Run yyparse() in a setjmp wrapper. Returns:
3320 * 0: yyparse() successful
3321 * 1: yyparse() failed
3325 S_try_yyparse(pTHX_ int gramtype)
3330 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3334 ret = yyparse(gramtype) ? 1 : 0;
3341 assert(0); /* NOTREACHED */
3348 /* Compile a require/do or an eval ''.
3350 * outside is the lexically enclosing CV (if any) that invoked us.
3351 * seq is the current COP scope value.
3352 * hh is the saved hints hash, if any.
3354 * Returns a bool indicating whether the compile was successful; if so,
3355 * PL_eval_start contains the first op of the compiled code; otherwise,
3358 * This function is called from two places: pp_require and pp_entereval.
3359 * These can be distinguished by whether PL_op is entereval.
3363 S_doeval(pTHX_ int gimme, CV* outside, U32 seq, HV *hh)
3366 OP * const saveop = PL_op;
3367 bool clear_hints = saveop->op_type != OP_ENTEREVAL;
3368 COP * const oldcurcop = PL_curcop;
3369 bool in_require = (saveop->op_type == OP_REQUIRE);
3373 PL_in_eval = (in_require
3374 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3376 ((PL_op->op_private & OPpEVAL_RE_REPARSING)
3377 ? EVAL_RE_REPARSING : 0)));
3381 evalcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3383 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3384 cxstack[cxstack_ix].blk_eval.cv = evalcv;
3385 cxstack[cxstack_ix].blk_gimme = gimme;
3387 CvOUTSIDE_SEQ(evalcv) = seq;
3388 CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3390 /* set up a scratch pad */
3392 CvPADLIST(evalcv) = pad_new(padnew_SAVE);
3393 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3397 SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */
3399 /* make sure we compile in the right package */
3401 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3402 SAVEGENERICSV(PL_curstash);
3403 PL_curstash = (HV *)CopSTASH(PL_curcop);
3404 if (SvTYPE(PL_curstash) != SVt_PVHV) PL_curstash = NULL;
3405 else SvREFCNT_inc_simple_void(PL_curstash);
3407 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3408 SAVESPTR(PL_beginav);
3409 PL_beginav = newAV();
3410 SAVEFREESV(PL_beginav);
3411 SAVESPTR(PL_unitcheckav);
3412 PL_unitcheckav = newAV();
3413 SAVEFREESV(PL_unitcheckav);
3416 SAVEBOOL(PL_madskills);
3420 ENTER_with_name("evalcomp");
3421 SAVESPTR(PL_compcv);
3424 /* try to compile it */
3426 PL_eval_root = NULL;
3427 PL_curcop = &PL_compiling;
3428 if ((saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3429 PL_in_eval |= EVAL_KEEPERR;
3436 hv_clear(GvHV(PL_hintgv));
3439 PL_hints = saveop->op_private & OPpEVAL_COPHH
3440 ? oldcurcop->cop_hints : saveop->op_targ;
3442 /* making 'use re eval' not be in scope when compiling the
3443 * qr/mabye_has_runtime_code_block/ ensures that we don't get
3444 * infinite recursion when S_has_runtime_code() gives a false
3445 * positive: the second time round, HINT_RE_EVAL isn't set so we
3446 * don't bother calling S_has_runtime_code() */
3447 if (PL_in_eval & EVAL_RE_REPARSING)
3448 PL_hints &= ~HINT_RE_EVAL;
3451 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3452 SvREFCNT_dec(GvHV(PL_hintgv));
3453 GvHV(PL_hintgv) = hh;
3456 SAVECOMPILEWARNINGS();
3458 if (PL_dowarn & G_WARN_ALL_ON)
3459 PL_compiling.cop_warnings = pWARN_ALL ;
3460 else if (PL_dowarn & G_WARN_ALL_OFF)
3461 PL_compiling.cop_warnings = pWARN_NONE ;
3463 PL_compiling.cop_warnings = pWARN_STD ;
3466 PL_compiling.cop_warnings =
3467 DUP_WARNINGS(oldcurcop->cop_warnings);
3468 cophh_free(CopHINTHASH_get(&PL_compiling));
3469 if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) {
3470 /* The label, if present, is the first entry on the chain. So rather
3471 than writing a blank label in front of it (which involves an
3472 allocation), just use the next entry in the chain. */
3473 PL_compiling.cop_hints_hash
3474 = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next);
3475 /* Check the assumption that this removed the label. */
3476 assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3479 PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash);
3482 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3484 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3485 * so honour CATCH_GET and trap it here if necessary */
3487 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3489 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3490 SV **newsp; /* Used by POPBLOCK. */
3492 I32 optype; /* Used by POPEVAL. */
3498 PERL_UNUSED_VAR(newsp);
3499 PERL_UNUSED_VAR(optype);
3501 /* note that if yystatus == 3, then the EVAL CX block has already
3502 * been popped, and various vars restored */
3504 if (yystatus != 3) {
3506 op_free(PL_eval_root);
3507 PL_eval_root = NULL;
3509 SP = PL_stack_base + POPMARK; /* pop original mark */
3510 POPBLOCK(cx,PL_curpm);
3512 namesv = cx->blk_eval.old_namesv;
3513 /* POPBLOCK renders LEAVE_with_name("evalcomp") unnecessary. */
3514 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3520 /* If cx is still NULL, it means that we didn't go in the
3521 * POPEVAL branch. */
3522 cx = &cxstack[cxstack_ix];
3523 assert(CxTYPE(cx) == CXt_EVAL);
3524 namesv = cx->blk_eval.old_namesv;
3526 (void)hv_store(GvHVn(PL_incgv),
3527 SvPVX_const(namesv),
3528 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
3530 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
3533 : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
3536 if (!*(SvPV_nolen_const(errsv))) {
3537 sv_setpvs(errsv, "Compilation error");
3540 if (gimme != G_ARRAY) PUSHs(&PL_sv_undef);
3545 LEAVE_with_name("evalcomp");
3547 CopLINE_set(&PL_compiling, 0);
3548 SAVEFREEOP(PL_eval_root);
3549 cv_forget_slab(evalcv);
3551 DEBUG_x(dump_eval());
3553 /* Register with debugger: */
3554 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
3555 CV * const cv = get_cvs("DB::postponed", 0);
3559 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3561 call_sv(MUTABLE_SV(cv), G_DISCARD);
3565 if (PL_unitcheckav) {
3566 OP *es = PL_eval_start;
3567 call_list(PL_scopestack_ix, PL_unitcheckav);
3571 /* compiled okay, so do it */
3573 CvDEPTH(evalcv) = 1;
3574 SP = PL_stack_base + POPMARK; /* pop original mark */
3575 PL_op = saveop; /* The caller may need it. */
3576 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3583 S_check_type_and_open(pTHX_ SV *name)
3586 const char *p = SvPV_nolen_const(name);
3587 const int st_rc = PerlLIO_stat(p, &st);
3589 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3591 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3595 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3596 return PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3598 return PerlIO_open(p, PERL_SCRIPT_MODE);
3602 #ifndef PERL_DISABLE_PMC
3604 S_doopen_pm(pTHX_ SV *name)
3607 const char *p = SvPV_const(name, namelen);
3609 PERL_ARGS_ASSERT_DOOPEN_PM;
3611 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3612 SV *const pmcsv = sv_newmortal();
3615 SvSetSV_nosteal(pmcsv,name);
3616 sv_catpvn(pmcsv, "c", 1);
3618 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3619 return check_type_and_open(pmcsv);
3621 return check_type_and_open(name);
3624 # define doopen_pm(name) check_type_and_open(name)
3625 #endif /* !PERL_DISABLE_PMC */
3627 /* require doesn't search for absolute names, or when the name is
3628 explicity relative the current directory */
3629 PERL_STATIC_INLINE bool
3630 S_path_is_searchable(const char *name)
3632 PERL_ARGS_ASSERT_PATH_IS_SEARCHABLE;
3634 if (PERL_FILE_IS_ABSOLUTE(name)
3636 || (*name == '.' && ((name[1] == '/' ||
3637 (name[1] == '.' && name[2] == '/'))
3638 || (name[1] == '\\' ||
3639 ( name[1] == '.' && name[2] == '\\')))
3642 || (*name == '.' && (name[1] == '/' ||
3643 (name[1] == '.' && name[2] == '/')))
3663 int vms_unixname = 0;
3668 const char *tryname = NULL;
3670 const I32 gimme = GIMME_V;
3671 int filter_has_file = 0;
3672 PerlIO *tryrsfp = NULL;
3673 SV *filter_cache = NULL;
3674 SV *filter_state = NULL;
3675 SV *filter_sub = NULL;
3680 bool path_searchable;
3683 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3684 sv = sv_2mortal(new_version(sv));
3685 if (!Perl_sv_derived_from_pvn(aTHX_ PL_patchlevel, STR_WITH_LEN("version"), 0))
3686 upg_version(PL_patchlevel, TRUE);
3687 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3688 if ( vcmp(sv,PL_patchlevel) <= 0 )
3689 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3690 SVfARG(sv_2mortal(vnormal(sv))),
3691 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3695 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3698 SV * const req = SvRV(sv);
3699 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3701 /* get the left hand term */
3702 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3704 first = SvIV(*av_fetch(lav,0,0));
3705 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3706 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3707 || av_len(lav) > 1 /* FP with > 3 digits */
3708 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3710 DIE(aTHX_ "Perl %"SVf" required--this is only "
3712 SVfARG(sv_2mortal(vnormal(req))),
3713 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3716 else { /* probably 'use 5.10' or 'use 5.8' */
3721 second = SvIV(*av_fetch(lav,1,0));
3723 second /= second >= 600 ? 100 : 10;
3724 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3725 (int)first, (int)second);
3726 upg_version(hintsv, TRUE);
3728 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3729 "--this is only %"SVf", stopped",
3730 SVfARG(sv_2mortal(vnormal(req))),
3731 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3732 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3740 name = SvPV_const(sv, len);
3741 if (!(name && len > 0 && *name))
3742 DIE(aTHX_ "Null filename used");
3743 TAINT_PROPER("require");
3745 path_searchable = path_is_searchable(name);
3748 /* The key in the %ENV hash is in the syntax of file passed as the argument
3749 * usually this is in UNIX format, but sometimes in VMS format, which
3750 * can result in a module being pulled in more than once.
3751 * To prevent this, the key must be stored in UNIX format if the VMS
3752 * name can be translated to UNIX.
3755 if ((unixnamebuf = SvPVX(sv_2mortal(newSVpv("", VMS_MAXRSS-1))))
3756 && (unixname = tounixspec(name, unixnamebuf)) != NULL) {
3757 unixlen = strlen(unixname);
3763 /* if not VMS or VMS name can not be translated to UNIX, pass it
3766 unixname = (char *) name;
3769 if (PL_op->op_type == OP_REQUIRE) {
3770 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3771 unixname, unixlen, 0);
3773 if (*svp != &PL_sv_undef)
3776 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3777 "Compilation failed in require", unixname);
3781 LOADING_FILE_PROBE(unixname);
3783 /* prepare to compile file */
3785 if (!path_searchable) {
3786 /* At this point, name is SvPVX(sv) */
3788 tryrsfp = doopen_pm(sv);
3790 if (!tryrsfp && !(errno == EACCES && !path_searchable)) {
3791 AV * const ar = GvAVn(PL_incgv);
3797 namesv = newSV_type(SVt_PV);
3798 for (i = 0; i <= AvFILL(ar); i++) {
3799 SV * const dirsv = *av_fetch(ar, i, TRUE);
3801 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3808 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3809 && !sv_isobject(loader))
3811 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3814 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3815 PTR2UV(SvRV(dirsv)), name);
3816 tryname = SvPVX_const(namesv);
3819 ENTER_with_name("call_INC");
3827 if (sv_isobject(loader))
3828 count = call_method("INC", G_ARRAY);
3830 count = call_sv(loader, G_ARRAY);
3840 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3841 && !isGV_with_GP(SvRV(arg))) {
3842 filter_cache = SvRV(arg);
3849 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
3853 if (isGV_with_GP(arg)) {
3854 IO * const io = GvIO((const GV *)arg);
3859 tryrsfp = IoIFP(io);
3860 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3861 PerlIO_close(IoOFP(io));
3872 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3874 SvREFCNT_inc_simple_void_NN(filter_sub);
3877 filter_state = SP[i];
3878 SvREFCNT_inc_simple_void(filter_state);
3882 if (!tryrsfp && (filter_cache || filter_sub)) {
3883 tryrsfp = PerlIO_open(BIT_BUCKET,
3891 LEAVE_with_name("call_INC");
3893 /* Adjust file name if the hook has set an %INC entry.
3894 This needs to happen after the FREETMPS above. */
3895 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3897 tryname = SvPV_nolen_const(*svp);
3904 filter_has_file = 0;
3905 filter_cache = NULL;
3907 SvREFCNT_dec(filter_state);
3908 filter_state = NULL;
3911 SvREFCNT_dec(filter_sub);
3916 if (path_searchable) {
3921 dir = SvPV_const(dirsv, dirlen);
3928 if (((unixdirbuf = SvPVX(sv_2mortal(newSVpv("", VMS_MAXRSS-1)))) == NULL)
3929 || ((unixdir = tounixpath(dir, unixdirbuf)) == NULL))
3931 sv_setpv(namesv, unixdir);
3932 sv_catpv(namesv, unixname);
3934 # ifdef __SYMBIAN32__
3935 if (PL_origfilename[0] &&
3936 PL_origfilename[1] == ':' &&
3937 !(dir[0] && dir[1] == ':'))
3938 Perl_sv_setpvf(aTHX_ namesv,
3943 Perl_sv_setpvf(aTHX_ namesv,
3947 /* The equivalent of
3948 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3949 but without the need to parse the format string, or
3950 call strlen on either pointer, and with the correct
3951 allocation up front. */
3953 char *tmp = SvGROW(namesv, dirlen + len + 2);
3955 memcpy(tmp, dir, dirlen);
3958 /* Avoid '<dir>//<file>' */
3959 if (!dirlen || *(tmp-1) != '/') {
3963 /* name came from an SV, so it will have a '\0' at the
3964 end that we can copy as part of this memcpy(). */
3965 memcpy(tmp, name, len + 1);
3967 SvCUR_set(namesv, dirlen + len + 1);
3972 TAINT_PROPER("require");
3973 tryname = SvPVX_const(namesv);
3974 tryrsfp = doopen_pm(namesv);
3976 if (tryname[0] == '.' && tryname[1] == '/') {
3978 while (*++tryname == '/') {}
3982 else if (errno == EMFILE || errno == EACCES) {
3983 /* no point in trying other paths if out of handles;
3984 * on the other hand, if we couldn't open one of the
3985 * files, then going on with the search could lead to
3986 * unexpected results; see perl #113422
3995 saved_errno = errno; /* sv_2mortal can realloc things */
3998 if (PL_op->op_type == OP_REQUIRE) {
3999 if(saved_errno == EMFILE || saved_errno == EACCES) {
4000 /* diag_listed_as: Can't locate %s */
4001 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(saved_errno));
4003 if (namesv) { /* did we lookup @INC? */
4004 AV * const ar = GvAVn(PL_incgv);
4006 SV *const msg = newSVpvs_flags("", SVs_TEMP);
4007 SV *const inc = newSVpvs_flags("", SVs_TEMP);
4008 for (i = 0; i <= AvFILL(ar); i++) {
4009 sv_catpvs(inc, " ");
4010 sv_catsv(inc, *av_fetch(ar, i, TRUE));
4012 if (len >= 4 && memEQ(name + len - 3, ".pm", 4)) {
4013 const char *c, *e = name + len - 3;
4014 sv_catpv(msg, " (you may need to install the ");
4015 for (c = name; c < e; c++) {
4017 sv_catpvn(msg, "::", 2);
4020 sv_catpvn(msg, c, 1);
4023 sv_catpv(msg, " module)");
4025 else if (len >= 2 && memEQ(name + len - 2, ".h", 3)) {
4026 sv_catpv(msg, " (change .h to .ph maybe?) (did you run h2ph?)");
4028 else if (len >= 3 && memEQ(name + len - 3, ".ph", 4)) {
4029 sv_catpv(msg, " (did you run h2ph?)");
4032 /* diag_listed_as: Can't locate %s */
4034 "Can't locate %s in @INC%" SVf " (@INC contains:%" SVf ")",
4038 DIE(aTHX_ "Can't locate %s", name);
4045 SETERRNO(0, SS_NORMAL);
4047 /* Assume success here to prevent recursive requirement. */
4048 /* name is never assigned to again, so len is still strlen(name) */
4049 /* Check whether a hook in @INC has already filled %INC */
4051 (void)hv_store(GvHVn(PL_incgv),
4052 unixname, unixlen, newSVpv(tryname,0),0);
4054 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
4056 (void)hv_store(GvHVn(PL_incgv),
4057 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
4060 ENTER_with_name("eval");
4062 SAVECOPFILE_FREE(&PL_compiling);
4063 CopFILE_set(&PL_compiling, tryname);
4064 lex_start(NULL, tryrsfp, 0);
4066 if (filter_sub || filter_cache) {
4067 /* We can use the SvPV of the filter PVIO itself as our cache, rather
4068 than hanging another SV from it. In turn, filter_add() optionally
4069 takes the SV to use as the filter (or creates a new SV if passed
4070 NULL), so simply pass in whatever value filter_cache has. */
4071 SV * const fc = filter_cache ? newSV(0) : NULL;
4073 if (fc) sv_copypv(fc, filter_cache);
4074 datasv = filter_add(S_run_user_filter, fc);
4075 IoLINES(datasv) = filter_has_file;
4076 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
4077 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
4080 /* switch to eval mode */
4081 PUSHBLOCK(cx, CXt_EVAL, SP);
4083 cx->blk_eval.retop = PL_op->op_next;
4085 SAVECOPLINE(&PL_compiling);
4086 CopLINE_set(&PL_compiling, 0);
4090 /* Store and reset encoding. */
4091 encoding = PL_encoding;
4094 if (doeval(gimme, NULL, PL_curcop->cop_seq, NULL))
4095 op = DOCATCH(PL_eval_start);
4097 op = PL_op->op_next;
4099 /* Restore encoding. */
4100 PL_encoding = encoding;
4102 LOADED_FILE_PROBE(unixname);
4107 /* This is a op added to hold the hints hash for
4108 pp_entereval. The hash can be modified by the code
4109 being eval'ed, so we return a copy instead. */
4115 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
4125 const I32 gimme = GIMME_V;
4126 const U32 was = PL_breakable_sub_gen;
4127 char tbuf[TYPE_DIGITS(long) + 12];
4128 bool saved_delete = FALSE;
4129 char *tmpbuf = tbuf;
4132 U32 seq, lex_flags = 0;
4133 HV *saved_hh = NULL;
4134 const bool bytes = PL_op->op_private & OPpEVAL_BYTES;
4136 if (PL_op->op_private & OPpEVAL_HAS_HH) {
4137 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
4139 else if (PL_hints & HINT_LOCALIZE_HH || (
4140 PL_op->op_private & OPpEVAL_COPHH
4141 && PL_curcop->cop_hints & HINT_LOCALIZE_HH
4143 saved_hh = cop_hints_2hv(PL_curcop, 0);
4144 hv_magic(saved_hh, NULL, PERL_MAGIC_hints);
4148 /* make sure we've got a plain PV (no overload etc) before testing
4149 * for taint. Making a copy here is probably overkill, but better
4150 * safe than sorry */
4152 const char * const p = SvPV_const(sv, len);
4154 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
4155 lex_flags |= LEX_START_COPIED;
4157 if (bytes && SvUTF8(sv))
4158 SvPVbyte_force(sv, len);
4160 else if (bytes && SvUTF8(sv)) {
4161 /* Don't modify someone else's scalar */
4164 (void)sv_2mortal(sv);
4165 SvPVbyte_force(sv,len);
4166 lex_flags |= LEX_START_COPIED;
4169 TAINT_IF(SvTAINTED(sv));
4170 TAINT_PROPER("eval");
4172 ENTER_with_name("eval");
4173 lex_start(sv, NULL, lex_flags | (PL_op->op_private & OPpEVAL_UNICODE
4174 ? LEX_IGNORE_UTF8_HINTS
4175 : bytes ? LEX_EVALBYTES : LEX_START_SAME_FILTER
4180 /* switch to eval mode */
4182 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
4183 SV * const temp_sv = sv_newmortal();
4184 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
4185 (unsigned long)++PL_evalseq,
4186 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4187 tmpbuf = SvPVX(temp_sv);
4188 len = SvCUR(temp_sv);
4191 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
4192 SAVECOPFILE_FREE(&PL_compiling);
4193 CopFILE_set(&PL_compiling, tmpbuf+2);
4194 SAVECOPLINE(&PL_compiling);
4195 CopLINE_set(&PL_compiling, 1);
4196 /* special case: an eval '' executed within the DB package gets lexically
4197 * placed in the first non-DB CV rather than the current CV - this
4198 * allows the debugger to execute code, find lexicals etc, in the
4199 * scope of the code being debugged. Passing &seq gets find_runcv
4200 * to do the dirty work for us */
4201 runcv = find_runcv(&seq);
4203 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
4205 cx->blk_eval.retop = PL_op->op_next;
4207 /* prepare to compile string */
4209 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
4210 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
4212 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
4213 deleting the eval's FILEGV from the stash before gv_check() runs
4214 (i.e. before run-time proper). To work around the coredump that
4215 ensues, we always turn GvMULTI_on for any globals that were
4216 introduced within evals. See force_ident(). GSAR 96-10-12 */
4217 char *const safestr = savepvn(tmpbuf, len);
4218 SAVEDELETE(PL_defstash, safestr, len);
4219 saved_delete = TRUE;
4224 if (doeval(gimme, runcv, seq, saved_hh)) {
4225 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4226 ? (PERLDB_LINE || PERLDB_SAVESRC)
4227 : PERLDB_SAVESRC_NOSUBS) {
4228 /* Retain the filegv we created. */
4229 } else if (!saved_delete) {
4230 char *const safestr = savepvn(tmpbuf, len);
4231 SAVEDELETE(PL_defstash, safestr, len);
4233 return DOCATCH(PL_eval_start);
4235 /* We have already left the scope set up earlier thanks to the LEAVE
4237 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4238 ? (PERLDB_LINE || PERLDB_SAVESRC)
4239 : PERLDB_SAVESRC_INVALID) {
4240 /* Retain the filegv we created. */
4241 } else if (!saved_delete) {
4242 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
4244 return PL_op->op_next;
4256 const U8 save_flags = PL_op -> op_flags;
4264 namesv = cx->blk_eval.old_namesv;
4265 retop = cx->blk_eval.retop;
4266 evalcv = cx->blk_eval.cv;
4269 SP = adjust_stack_on_leave((gimme == G_VOID) ? SP : newsp, SP, newsp,
4271 PL_curpm = newpm; /* Don't pop $1 et al till now */
4274 assert(CvDEPTH(evalcv) == 1);
4276 CvDEPTH(evalcv) = 0;
4278 if (optype == OP_REQUIRE &&
4279 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4281 /* Unassume the success we assumed earlier. */
4282 (void)hv_delete(GvHVn(PL_incgv),
4283 SvPVX_const(namesv),
4284 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
4286 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4288 /* die_unwind() did LEAVE, or we won't be here */
4291 LEAVE_with_name("eval");
4292 if (!(save_flags & OPf_SPECIAL)) {
4300 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4301 close to the related Perl_create_eval_scope. */
4303 Perl_delete_eval_scope(pTHX)
4314 LEAVE_with_name("eval_scope");
4315 PERL_UNUSED_VAR(newsp);
4316 PERL_UNUSED_VAR(gimme);
4317 PERL_UNUSED_VAR(optype);
4320 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4321 also needed by Perl_fold_constants. */
4323 Perl_create_eval_scope(pTHX_ U32 flags)
4326 const I32 gimme = GIMME_V;
4328 ENTER_with_name("eval_scope");
4331 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4334 PL_in_eval = EVAL_INEVAL;
4335 if (flags & G_KEEPERR)
4336 PL_in_eval |= EVAL_KEEPERR;
4339 if (flags & G_FAKINGEVAL) {
4340 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4348 PERL_CONTEXT * const cx = create_eval_scope(0);
4349 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4350 return DOCATCH(PL_op->op_next);
4365 PERL_UNUSED_VAR(optype);
4368 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
4369 PL_curpm = newpm; /* Don't pop $1 et al till now */
4371 LEAVE_with_name("eval_scope");
4380 const I32 gimme = GIMME_V;
4382 ENTER_with_name("given");
4385 if (PL_op->op_targ) {
4386 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
4387 SvREFCNT_dec(PAD_SVl(PL_op->op_targ));
4388 PAD_SVl(PL_op->op_targ) = SvREFCNT_inc_NN(POPs);
4395 PUSHBLOCK(cx, CXt_GIVEN, SP);
4408 PERL_UNUSED_CONTEXT;
4411 assert(CxTYPE(cx) == CXt_GIVEN);
4414 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
4415 PL_curpm = newpm; /* Don't pop $1 et al till now */
4417 LEAVE_with_name("given");
4421 /* Helper routines used by pp_smartmatch */
4423 S_make_matcher(pTHX_ REGEXP *re)
4426 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4428 PERL_ARGS_ASSERT_MAKE_MATCHER;
4430 PM_SETRE(matcher, ReREFCNT_inc(re));
4432 SAVEFREEOP((OP *) matcher);
4433 ENTER_with_name("matcher"); SAVETMPS;
4439 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4444 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4446 PL_op = (OP *) matcher;
4449 (void) Perl_pp_match(aTHX);
4451 return (SvTRUEx(POPs));
4455 S_destroy_matcher(pTHX_ PMOP *matcher)
4459 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4460 PERL_UNUSED_ARG(matcher);
4463 LEAVE_with_name("matcher");
4466 /* Do a smart match */
4469 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4470 return do_smartmatch(NULL, NULL, 0);
4473 /* This version of do_smartmatch() implements the
4474 * table of smart matches that is found in perlsyn.
4477 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied)
4482 bool object_on_left = FALSE;
4483 SV *e = TOPs; /* e is for 'expression' */
4484 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4486 /* Take care only to invoke mg_get() once for each argument.
4487 * Currently we do this by copying the SV if it's magical. */
4489 if (!copied && SvGMAGICAL(d))
4490 d = sv_mortalcopy(d);
4497 e = sv_mortalcopy(e);
4499 /* First of all, handle overload magic of the rightmost argument */
4502 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4503 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4505 tmpsv = amagic_call(d, e, smart_amg, AMGf_noleft);
4512 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4515 SP -= 2; /* Pop the values */
4520 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4527 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4528 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4529 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4531 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4532 object_on_left = TRUE;
4535 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4537 if (object_on_left) {
4538 goto sm_any_sub; /* Treat objects like scalars */
4540 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4541 /* Test sub truth for each key */
4543 bool andedresults = TRUE;
4544 HV *hv = (HV*) SvRV(d);
4545 I32 numkeys = hv_iterinit(hv);
4546 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4549 while ( (he = hv_iternext(hv)) ) {
4550 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4551 ENTER_with_name("smartmatch_hash_key_test");
4554 PUSHs(hv_iterkeysv(he));
4556 c = call_sv(e, G_SCALAR);
4559 andedresults = FALSE;
4561 andedresults = SvTRUEx(POPs) && andedresults;
4563 LEAVE_with_name("smartmatch_hash_key_test");
4570 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4571 /* Test sub truth for each element */
4573 bool andedresults = TRUE;
4574 AV *av = (AV*) SvRV(d);
4575 const I32 len = av_len(av);
4576 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4579 for (i = 0; i <= len; ++i) {
4580 SV * const * const svp = av_fetch(av, i, FALSE);
4581 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4582 ENTER_with_name("smartmatch_array_elem_test");
4588 c = call_sv(e, G_SCALAR);
4591 andedresults = FALSE;
4593 andedresults = SvTRUEx(POPs) && andedresults;
4595 LEAVE_with_name("smartmatch_array_elem_test");
4604 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4605 ENTER_with_name("smartmatch_coderef");
4610 c = call_sv(e, G_SCALAR);
4614 else if (SvTEMP(TOPs))
4615 SvREFCNT_inc_void(TOPs);
4617 LEAVE_with_name("smartmatch_coderef");
4622 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4623 if (object_on_left) {
4624 goto sm_any_hash; /* Treat objects like scalars */
4626 else if (!SvOK(d)) {
4627 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4630 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4631 /* Check that the key-sets are identical */
4633 HV *other_hv = MUTABLE_HV(SvRV(d));
4635 bool other_tied = FALSE;
4636 U32 this_key_count = 0,
4637 other_key_count = 0;
4638 HV *hv = MUTABLE_HV(SvRV(e));
4640 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
4641 /* Tied hashes don't know how many keys they have. */
4642 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
4645 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
4646 HV * const temp = other_hv;
4651 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
4654 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
4657 /* The hashes have the same number of keys, so it suffices
4658 to check that one is a subset of the other. */
4659 (void) hv_iterinit(hv);
4660 while ( (he = hv_iternext(hv)) ) {
4661 SV *key = hv_iterkeysv(he);
4663 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
4666 if(!hv_exists_ent(other_hv, key, 0)) {
4667 (void) hv_iterinit(hv); /* reset iterator */
4673 (void) hv_iterinit(other_hv);
4674 while ( hv_iternext(other_hv) )
4678 other_key_count = HvUSEDKEYS(other_hv);
4680 if (this_key_count != other_key_count)
4685 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4686 AV * const other_av = MUTABLE_AV(SvRV(d));
4687 const I32 other_len = av_len(other_av) + 1;
4689 HV *hv = MUTABLE_HV(SvRV(e));
4691 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n"));
4692 for (i = 0; i < other_len; ++i) {
4693 SV ** const svp = av_fetch(other_av, i, FALSE);
4694 DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n"));
4695 if (svp) { /* ??? When can this not happen? */
4696 if (hv_exists_ent(hv, *svp, 0))
4702 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4703 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n"));
4706 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4708 HV *hv = MUTABLE_HV(SvRV(e));
4710 (void) hv_iterinit(hv);
4711 while ( (he = hv_iternext(hv)) ) {
4712 DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n"));
4713 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
4714 (void) hv_iterinit(hv);
4715 destroy_matcher(matcher);
4719 destroy_matcher(matcher);
4725 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n"));
4726 if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0))
4733 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) {
4734 if (object_on_left) {
4735 goto sm_any_array; /* Treat objects like scalars */
4737 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4738 AV * const other_av = MUTABLE_AV(SvRV(e));
4739 const I32 other_len = av_len(other_av) + 1;
4742 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n"));
4743 for (i = 0; i < other_len; ++i) {
4744 SV ** const svp = av_fetch(other_av, i, FALSE);
4746 DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n"));
4747 if (svp) { /* ??? When can this not happen? */
4748 if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0))
4754 if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4755 AV *other_av = MUTABLE_AV(SvRV(d));
4756 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n"));
4757 if (av_len(MUTABLE_AV(SvRV(e))) != av_len(other_av))
4761 const I32 other_len = av_len(other_av);
4763 if (NULL == seen_this) {
4764 seen_this = newHV();
4765 (void) sv_2mortal(MUTABLE_SV(seen_this));
4767 if (NULL == seen_other) {
4768 seen_other = newHV();
4769 (void) sv_2mortal(MUTABLE_SV(seen_other));
4771 for(i = 0; i <= other_len; ++i) {
4772 SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4773 SV * const * const other_elem = av_fetch(other_av, i, FALSE);
4775 if (!this_elem || !other_elem) {
4776 if ((this_elem && SvOK(*this_elem))
4777 || (other_elem && SvOK(*other_elem)))
4780 else if (hv_exists_ent(seen_this,
4781 sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) ||
4782 hv_exists_ent(seen_other,
4783 sv_2mortal(newSViv(PTR2IV(*other_elem))), 0))
4785 if (*this_elem != *other_elem)
4789 (void)hv_store_ent(seen_this,
4790 sv_2mortal(newSViv(PTR2IV(*this_elem))),
4792 (void)hv_store_ent(seen_other,
4793 sv_2mortal(newSViv(PTR2IV(*other_elem))),
4799 DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n"));
4800 (void) do_smartmatch(seen_this, seen_other, 0);
4802 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4811 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4812 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n"));
4815 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4816 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4819 for(i = 0; i <= this_len; ++i) {
4820 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4821 DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n"));
4822 if (svp && matcher_matches_sv(matcher, *svp)) {
4823 destroy_matcher(matcher);
4827 destroy_matcher(matcher);
4831 else if (!SvOK(d)) {
4832 /* undef ~~ array */
4833 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4836 DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n"));
4837 for (i = 0; i <= this_len; ++i) {
4838 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4839 DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n"));
4840 if (!svp || !SvOK(*svp))
4849 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4851 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n"));
4852 for (i = 0; i <= this_len; ++i) {
4853 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4860 /* infinite recursion isn't supposed to happen here */
4861 DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n"));
4862 (void) do_smartmatch(NULL, NULL, 1);
4864 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4873 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) {
4874 if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4875 SV *t = d; d = e; e = t;
4876 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n"));
4879 else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4880 SV *t = d; d = e; e = t;
4881 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n"));
4882 goto sm_regex_array;
4885 PMOP * const matcher = make_matcher((REGEXP*) SvRV(e));
4887 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n"));
4889 PUSHs(matcher_matches_sv(matcher, d)
4892 destroy_matcher(matcher);
4897 /* See if there is overload magic on left */
4898 else if (object_on_left && SvAMAGIC(d)) {
4900 DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n"));
4901 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4904 tmpsv = amagic_call(d, e, smart_amg, AMGf_noright);
4912 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n"));
4915 else if (!SvOK(d)) {
4916 /* undef ~~ scalar ; we already know that the scalar is SvOK */
4917 DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n"));
4922 if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) {
4923 DEBUG_M(if (SvNIOK(e))
4924 Perl_deb(aTHX_ " applying rule Any-Num\n");
4926 Perl_deb(aTHX_ " applying rule Num-numish\n");
4928 /* numeric comparison */
4931 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
4932 (void) Perl_pp_i_eq(aTHX);
4934 (void) Perl_pp_eq(aTHX);
4942 /* As a last resort, use string comparison */
4943 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n"));
4946 return Perl_pp_seq(aTHX);
4953 const I32 gimme = GIMME_V;
4955 /* This is essentially an optimization: if the match
4956 fails, we don't want to push a context and then
4957 pop it again right away, so we skip straight
4958 to the op that follows the leavewhen.
4959 RETURNOP calls PUTBACK which restores the stack pointer after the POPs.
4961 if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs))
4962 RETURNOP(cLOGOP->op_other->op_next);
4964 ENTER_with_name("when");
4967 PUSHBLOCK(cx, CXt_WHEN, SP);
4982 cxix = dopoptogiven(cxstack_ix);
4984 /* diag_listed_as: Can't "when" outside a topicalizer */
4985 DIE(aTHX_ "Can't \"%s\" outside a topicalizer",
4986 PL_op->op_flags & OPf_SPECIAL ? "default" : "when");
4989 assert(CxTYPE(cx) == CXt_WHEN);
4992 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
4993 PL_curpm = newpm; /* pop $1 et al */
4995 LEAVE_with_name("when");
4997 if (cxix < cxstack_ix)
5000 cx = &cxstack[cxix];
5002 if (CxFOREACH(cx)) {
5003 /* clear off anything above the scope we're re-entering */
5004 I32 inner = PL_scopestack_ix;
5007 if (PL_scopestack_ix < inner)
5008 leave_scope(PL_scopestack[PL_scopestack_ix]);
5009 PL_curcop = cx->blk_oldcop;
5012 return cx->blk_loop.my_op->op_nextop;
5016 RETURNOP(cx->blk_givwhen.leave_op);
5029 PERL_UNUSED_VAR(gimme);
5031 cxix = dopoptowhen(cxstack_ix);
5033 DIE(aTHX_ "Can't \"continue\" outside a when block");
5035 if (cxix < cxstack_ix)
5039 assert(CxTYPE(cx) == CXt_WHEN);
5042 PL_curpm = newpm; /* pop $1 et al */
5044 LEAVE_with_name("when");
5045 RETURNOP(cx->blk_givwhen.leave_op->op_next);
5054 cxix = dopoptogiven(cxstack_ix);
5056 DIE(aTHX_ "Can't \"break\" outside a given block");
5058 cx = &cxstack[cxix];
5060 DIE(aTHX_ "Can't \"break\" in a loop topicalizer");
5062 if (cxix < cxstack_ix)
5065 /* Restore the sp at the time we entered the given block */
5068 return cx->blk_givwhen.leave_op;
5072 S_doparseform(pTHX_ SV *sv)
5075 char *s = SvPV(sv, len);
5077 char *base = NULL; /* start of current field */
5078 I32 skipspaces = 0; /* number of contiguous spaces seen */
5079 bool noblank = FALSE; /* ~ or ~~ seen on this line */
5080 bool repeat = FALSE; /* ~~ seen on this line */
5081 bool postspace = FALSE; /* a text field may need right padding */
5084 U32 *linepc = NULL; /* position of last FF_LINEMARK */
5086 bool ischop; /* it's a ^ rather than a @ */
5087 bool unchopnum = FALSE; /* at least one @ (i.e. non-chop) num field seen */
5088 int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */
5092 PERL_ARGS_ASSERT_DOPARSEFORM;
5095 Perl_croak(aTHX_ "Null picture in formline");
5097 if (SvTYPE(sv) >= SVt_PVMG) {
5098 /* This might, of course, still return NULL. */
5099 mg = mg_find(sv, PERL_MAGIC_fm);
5101 sv_upgrade(sv, SVt_PVMG);
5105 /* still the same as previously-compiled string? */
5106 SV *old = mg->mg_obj;
5107 if ( !(!!SvUTF8(old) ^ !!SvUTF8(sv))
5108 && len == SvCUR(old)
5109 && strnEQ(SvPVX(old), SvPVX(sv), len)
5111 DEBUG_f(PerlIO_printf(Perl_debug_log,"Re-using compiled format\n"));
5115 DEBUG_f(PerlIO_printf(Perl_debug_log, "Re-compiling format\n"));
5116 Safefree(mg->mg_ptr);
5122 DEBUG_f(PerlIO_printf(Perl_debug_log, "Compiling format\n"));
5123 mg = sv_magicext(sv, NULL, PERL_MAGIC_fm, &PL_vtbl_fm, NULL, 0);
5126 sv_copy = newSVpvn_utf8(s, len, SvUTF8(sv));
5127 s = SvPV(sv_copy, len); /* work on the copy, not the original */
5131 /* estimate the buffer size needed */
5132 for (base = s; s <= send; s++) {
5133 if (*s == '\n' || *s == '@' || *s == '^')
5139 Newx(fops, maxops, U32);
5144 *fpc++ = FF_LINEMARK;
5145 noblank = repeat = FALSE;
5163 case ' ': case '\t':
5170 } /* else FALL THROUGH */
5178 *fpc++ = FF_LITERAL;
5186 *fpc++ = (U32)skipspaces;
5190 *fpc++ = FF_NEWLINE;
5194 arg = fpc - linepc + 1;
5201 *fpc++ = FF_LINEMARK;
5202 noblank = repeat = FALSE;
5211 ischop = s[-1] == '^';
5217 arg = (s - base) - 1;
5219 *fpc++ = FF_LITERAL;
5225 if (*s == '*') { /* @* or ^* */
5227 *fpc++ = 2; /* skip the @* or ^* */
5229 *fpc++ = FF_LINESNGL;
5232 *fpc++ = FF_LINEGLOB;
5234 else if (*s == '#' || (*s == '.' && s[1] == '#')) { /* @###, ^### */
5235 arg = ischop ? FORM_NUM_BLANK : 0;
5240 const char * const f = ++s;
5243 arg |= FORM_NUM_POINT + (s - f);
5245 *fpc++ = s - base; /* fieldsize for FETCH */
5246 *fpc++ = FF_DECIMAL;
5248 unchopnum |= ! ischop;
5250 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
5251 arg = ischop ? FORM_NUM_BLANK : 0;
5253 s++; /* skip the '0' first */
5257 const char * const f = ++s;
5260 arg |= FORM_NUM_POINT + (s - f);
5262 *fpc++ = s - base; /* fieldsize for FETCH */
5263 *fpc++ = FF_0DECIMAL;
5265 unchopnum |= ! ischop;
5267 else { /* text field */
5269 bool ismore = FALSE;
5272 while (*++s == '>') ;
5273 prespace = FF_SPACE;
5275 else if (*s == '|') {
5276 while (*++s == '|') ;
5277 prespace = FF_HALFSPACE;
5282 while (*++s == '<') ;
5285 if (*s == '.' && s[1] == '.' && s[2] == '.') {
5289 *fpc++ = s - base; /* fieldsize for FETCH */
5291 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
5294 *fpc++ = (U32)prespace; /* add SPACE or HALFSPACE */
5308 assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */
5311 mg->mg_ptr = (char *) fops;
5312 mg->mg_len = arg * sizeof(U32);
5313 mg->mg_obj = sv_copy;
5314 mg->mg_flags |= MGf_REFCOUNTED;
5316 if (unchopnum && repeat)
5317 Perl_die(aTHX_ "Repeated format line will never terminate (~~ and @#)");
5324 S_num_overflow(NV value, I32 fldsize, I32 frcsize)
5326 /* Can value be printed in fldsize chars, using %*.*f ? */
5330 int intsize = fldsize - (value < 0 ? 1 : 0);
5332 if (frcsize & FORM_NUM_POINT)
5334 frcsize &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
5337 while (intsize--) pwr *= 10.0;
5338 while (frcsize--) eps /= 10.0;
5341 if (value + eps >= pwr)
5344 if (value - eps <= -pwr)
5351 S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
5354 SV * const datasv = FILTER_DATA(idx);
5355 const int filter_has_file = IoLINES(datasv);
5356 SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv));
5357 SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv));
5362 char *prune_from = NULL;
5363 bool read_from_cache = FALSE;
5367 PERL_ARGS_ASSERT_RUN_USER_FILTER;
5369 assert(maxlen >= 0);
5372 /* I was having segfault trouble under Linux 2.2.5 after a
5373 parse error occured. (Had to hack around it with a test
5374 for PL_parser->error_count == 0.) Solaris doesn't segfault --
5375 not sure where the trouble is yet. XXX */
5378 SV *const cache = datasv;
5381 const char *cache_p = SvPV(cache, cache_len);
5385 /* Running in block mode and we have some cached data already.
5387 if (cache_len >= umaxlen) {
5388 /* In fact, so much data we don't even need to call
5393 const char *const first_nl =
5394 (const char *)memchr(cache_p, '\n', cache_len);
5396 take = first_nl + 1 - cache_p;
5400 sv_catpvn(buf_sv, cache_p, take);
5401 sv_chop(cache, cache_p + take);
5402 /* Definitely not EOF */
5406 sv_catsv(buf_sv, cache);
5408 umaxlen -= cache_len;
5411 read_from_cache = TRUE;
5415 /* Filter API says that the filter appends to the contents of the buffer.
5416 Usually the buffer is "", so the details don't matter. But if it's not,
5417 then clearly what it contains is already filtered by this filter, so we
5418 don't want to pass it in a second time.
5419 I'm going to use a mortal in case the upstream filter croaks. */
5420 upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv))
5421 ? sv_newmortal() : buf_sv;
5422 SvUPGRADE(upstream, SVt_PV);
5424 if (filter_has_file) {
5425 status = FILTER_READ(idx+1, upstream, 0);
5428 if (filter_sub && status >= 0) {
5432 ENTER_with_name("call_filter_sub");
5437 DEFSV_set(upstream);
5441 PUSHs(filter_state);
5444 count = call_sv(filter_sub, G_SCALAR|G_EVAL);
5453 SV * const errsv = ERRSV;
5454 if (SvTRUE_NN(errsv))
5455 err = newSVsv(errsv);
5461 LEAVE_with_name("call_filter_sub");
5464 if (SvIsCOW(upstream)) sv_force_normal(upstream);
5465 if(!err && SvOK(upstream)) {
5466 got_p = SvPV(upstream, got_len);
5468 if (got_len > umaxlen) {
5469 prune_from = got_p + umaxlen;
5472 char *const first_nl = (char *)memchr(got_p, '\n', got_len);
5473 if (first_nl && first_nl + 1 < got_p + got_len) {
5474 /* There's a second line here... */
5475 prune_from = first_nl + 1;
5479 if (!err && prune_from) {
5480 /* Oh. Too long. Stuff some in our cache. */
5481 STRLEN cached_len = got_p + got_len - prune_from;
5482 SV *const cache = datasv;
5485 /* Cache should be empty. */
5486 assert(!SvCUR(cache));
5489 sv_setpvn(cache, prune_from, cached_len);
5490 /* If you ask for block mode, you may well split UTF-8 characters.
5491 "If it breaks, you get to keep both parts"
5492 (Your code is broken if you don't put them back together again
5493 before something notices.) */
5494 if (SvUTF8(upstream)) {
5497 SvCUR_set(upstream, got_len - cached_len);
5499 /* Can't yet be EOF */
5504 /* If they are at EOF but buf_sv has something in it, then they may never
5505 have touched the SV upstream, so it may be undefined. If we naively
5506 concatenate it then we get a warning about use of uninitialised value.
5508 if (!err && upstream != buf_sv &&
5509 (SvOK(upstream) || SvGMAGICAL(upstream))) {
5510 sv_catsv(buf_sv, upstream);
5514 IoLINES(datasv) = 0;
5516 SvREFCNT_dec(filter_state);
5517 IoTOP_GV(datasv) = NULL;
5520 SvREFCNT_dec(filter_sub);
5521 IoBOTTOM_GV(datasv) = NULL;
5523 filter_del(S_run_user_filter);
5529 if (status == 0 && read_from_cache) {
5530 /* If we read some data from the cache (and by getting here it implies
5531 that we emptied the cache) then we aren't yet at EOF, and mustn't
5532 report that to our caller. */
5540 * c-indentation-style: bsd
5542 * indent-tabs-mode: nil
5545 * ex: set ts=8 sts=4 sw=4 et: