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
38 #define WORD_ALIGN sizeof(U32)
41 #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
43 #define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
52 cxix = dopoptosub(cxstack_ix);
56 switch (cxstack[cxix].blk_gimme) {
69 /* XXXX Should store the old value to allow for tie/overload - and
70 restore in regcomp, where marked with XXXX. */
80 register PMOP *pm = (PMOP*)cLOGOP->op_other;
84 /* prevent recompiling under /o and ithreads. */
85 #if defined(USE_ITHREADS)
86 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
87 if (PL_op->op_flags & OPf_STACKED) {
97 #define tryAMAGICregexp(rx) \
100 if (SvROK(rx) && SvAMAGIC(rx)) { \
101 SV *sv = AMG_CALLun(rx, regexp); \
105 if (SvTYPE(sv) != SVt_REGEXP) \
106 Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \
113 if (PL_op->op_flags & OPf_STACKED) {
114 /* multiple args; concatentate them */
116 tmpstr = PAD_SV(ARGTARG);
117 sv_setpvs(tmpstr, "");
118 while (++MARK <= SP) {
122 tryAMAGICregexp(msv);
124 if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) &&
125 (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign)))
127 sv_setsv(tmpstr, sv);
130 sv_catsv_nomg(tmpstr, msv);
137 tryAMAGICregexp(tmpstr);
140 #undef tryAMAGICregexp
143 SV * const sv = SvRV(tmpstr);
144 if (SvTYPE(sv) == SVt_REGEXP)
147 else if (SvTYPE(tmpstr) == SVt_REGEXP)
148 re = (REGEXP*) tmpstr;
151 /* The match's LHS's get-magic might need to access this op's reg-
152 exp (as is sometimes the case with $'; see bug 70764). So we
153 must call get-magic now before we replace the regexp. Hopeful-
154 ly this hack can be replaced with the approach described at
155 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03
156 /msg122415.html some day. */
157 if(pm->op_type == OP_MATCH) {
159 const bool was_tainted = PL_tainted;
160 if (pm->op_flags & OPf_STACKED)
162 else if (pm->op_private & OPpTARGET_MY)
163 lhs = PAD_SV(pm->op_targ);
166 /* Restore the previous value of PL_tainted (which may have been
167 modified by get-magic), to avoid incorrectly setting the
168 RXf_TAINTED flag further down. */
169 PL_tainted = was_tainted;
172 re = reg_temp_copy(NULL, re);
173 ReREFCNT_dec(PM_GETRE(pm));
178 const char *t = SvOK(tmpstr) ? SvPV_nomg_const(tmpstr, len) : "";
181 assert (re != (REGEXP*) &PL_sv_undef);
183 /* Check against the last compiled regexp. */
184 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
185 memNE(RX_PRECOMP(re), t, len))
187 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
188 U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME;
192 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
194 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
196 } else if (PL_curcop->cop_hints_hash) {
197 SV *ptr = cop_hints_fetch_pvs(PL_curcop, "regcomp", 0);
198 if (ptr && SvIOK(ptr) && SvIV(ptr))
199 eng = INT2PTR(regexp_engine*,SvIV(ptr));
202 if (PL_op->op_flags & OPf_SPECIAL)
203 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
205 if (DO_UTF8(tmpstr)) {
206 assert (SvUTF8(tmpstr));
207 } else if (SvUTF8(tmpstr)) {
208 /* Not doing UTF-8, despite what the SV says. Is this only if
209 we're trapped in use 'bytes'? */
210 /* Make a copy of the octet sequence, but without the flag on,
211 as the compiler now honours the SvUTF8 flag on tmpstr. */
213 const char *const p = SvPV(tmpstr, len);
214 tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
216 else if (SvAMAGIC(tmpstr)) {
217 /* make a copy to avoid extra stringifies */
218 tmpstr = newSVpvn_flags(t, len, SVs_TEMP | SvUTF8(tmpstr));
221 /* If it is gmagical, create a mortal copy, but without calling
222 get-magic, as we have already done that. */
223 if(SvGMAGICAL(tmpstr)) {
224 SV *mortalcopy = sv_newmortal();
225 sv_setsv_flags(mortalcopy, tmpstr, 0);
230 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
232 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
234 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
235 inside tie/overload accessors. */
241 #ifndef INCOMPLETE_TAINTS
244 RX_EXTFLAGS(re) |= RXf_TAINTED;
246 RX_EXTFLAGS(re) &= ~RXf_TAINTED;
250 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
254 #if !defined(USE_ITHREADS)
255 /* can't change the optree at runtime either */
256 /* PMf_KEEP is handled differently under threads to avoid these problems */
257 if (pm->op_pmflags & PMf_KEEP) {
258 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
259 cLOGOP->op_first->op_next = PL_op->op_next;
269 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
270 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
271 register SV * const dstr = cx->sb_dstr;
272 register char *s = cx->sb_s;
273 register char *m = cx->sb_m;
274 char *orig = cx->sb_orig;
275 register REGEXP * const rx = cx->sb_rx;
277 REGEXP *old = PM_GETRE(pm);
284 PM_SETRE(pm,ReREFCNT_inc(rx));
287 rxres_restore(&cx->sb_rxres, rx);
288 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
290 if (cx->sb_iters++) {
291 const I32 saviters = cx->sb_iters;
292 if (cx->sb_iters > cx->sb_maxiters)
293 DIE(aTHX_ "Substitution loop");
295 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
297 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
298 cx->sb_rxtainted |= 2;
299 sv_catsv_nomg(dstr, POPs);
300 /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */
304 if (CxONCE(cx) || s < orig ||
305 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
306 (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
307 ((cx->sb_rflags & REXEC_COPY_STR)
308 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
309 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
311 SV * const targ = cx->sb_targ;
313 assert(cx->sb_strend >= s);
314 if(cx->sb_strend > s) {
315 if (DO_UTF8(dstr) && !SvUTF8(targ))
316 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
318 sv_catpvn(dstr, s, cx->sb_strend - s);
320 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
322 #ifdef PERL_OLD_COPY_ON_WRITE
324 sv_force_normal_flags(targ, SV_COW_DROP_PV);
330 SvPV_set(targ, SvPVX(dstr));
331 SvCUR_set(targ, SvCUR(dstr));
332 SvLEN_set(targ, SvLEN(dstr));
335 SvPV_set(dstr, NULL);
337 TAINT_IF(cx->sb_rxtainted & 1);
338 if (pm->op_pmflags & PMf_NONDESTRUCT)
341 mPUSHi(saviters - 1);
343 (void)SvPOK_only_UTF8(targ);
344 TAINT_IF(cx->sb_rxtainted);
348 LEAVE_SCOPE(cx->sb_oldsave);
350 RETURNOP(pm->op_next);
352 cx->sb_iters = saviters;
354 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
357 cx->sb_orig = orig = RX_SUBBEG(rx);
359 cx->sb_strend = s + (cx->sb_strend - m);
361 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
363 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
364 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
366 sv_catpvn(dstr, s, m-s);
368 cx->sb_s = RX_OFFS(rx)[0].end + orig;
369 { /* Update the pos() information. */
370 SV * const sv = cx->sb_targ;
372 SvUPGRADE(sv, SVt_PVMG);
373 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
374 #ifdef PERL_OLD_COPY_ON_WRITE
376 sv_force_normal_flags(sv, 0);
378 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
381 mg->mg_len = m - orig;
384 (void)ReREFCNT_inc(rx);
385 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
386 rxres_save(&cx->sb_rxres, rx);
388 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
392 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
397 PERL_ARGS_ASSERT_RXRES_SAVE;
400 if (!p || p[1] < RX_NPARENS(rx)) {
401 #ifdef PERL_OLD_COPY_ON_WRITE
402 i = 7 + RX_NPARENS(rx) * 2;
404 i = 6 + RX_NPARENS(rx) * 2;
413 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
414 RX_MATCH_COPIED_off(rx);
416 #ifdef PERL_OLD_COPY_ON_WRITE
417 *p++ = PTR2UV(RX_SAVED_COPY(rx));
418 RX_SAVED_COPY(rx) = NULL;
421 *p++ = RX_NPARENS(rx);
423 *p++ = PTR2UV(RX_SUBBEG(rx));
424 *p++ = (UV)RX_SUBLEN(rx);
425 for (i = 0; i <= RX_NPARENS(rx); ++i) {
426 *p++ = (UV)RX_OFFS(rx)[i].start;
427 *p++ = (UV)RX_OFFS(rx)[i].end;
432 S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
437 PERL_ARGS_ASSERT_RXRES_RESTORE;
440 RX_MATCH_COPY_FREE(rx);
441 RX_MATCH_COPIED_set(rx, *p);
444 #ifdef PERL_OLD_COPY_ON_WRITE
445 if (RX_SAVED_COPY(rx))
446 SvREFCNT_dec (RX_SAVED_COPY(rx));
447 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
451 RX_NPARENS(rx) = *p++;
453 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
454 RX_SUBLEN(rx) = (I32)(*p++);
455 for (i = 0; i <= RX_NPARENS(rx); ++i) {
456 RX_OFFS(rx)[i].start = (I32)(*p++);
457 RX_OFFS(rx)[i].end = (I32)(*p++);
462 S_rxres_free(pTHX_ void **rsp)
464 UV * const p = (UV*)*rsp;
466 PERL_ARGS_ASSERT_RXRES_FREE;
471 void *tmp = INT2PTR(char*,*p);
474 PoisonFree(*p, 1, sizeof(*p));
476 Safefree(INT2PTR(char*,*p));
478 #ifdef PERL_OLD_COPY_ON_WRITE
480 SvREFCNT_dec (INT2PTR(SV*,p[1]));
490 dVAR; dSP; dMARK; dORIGMARK;
491 register SV * const tmpForm = *++MARK;
496 register SV *sv = NULL;
497 const char *item = NULL;
501 bool chopspace = (strchr(PL_chopset, ' ') != NULL);
502 const char *chophere = NULL;
503 char *linemark = NULL;
505 bool gotsome = FALSE;
507 const STRLEN fudge = SvPOK(tmpForm)
508 ? (SvCUR(tmpForm) * (IN_BYTES ? 1 : 3) + 1) : 0;
509 bool item_is_utf8 = FALSE;
510 bool targ_is_utf8 = FALSE;
512 OP * parseres = NULL;
515 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
516 if (SvREADONLY(tmpForm)) {
517 SvREADONLY_off(tmpForm);
518 parseres = doparseform(tmpForm);
519 SvREADONLY_on(tmpForm);
522 parseres = doparseform(tmpForm);
526 SvPV_force(PL_formtarget, len);
527 if (DO_UTF8(PL_formtarget))
529 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
531 f = SvPV_const(tmpForm, len);
532 /* need to jump to the next word */
533 fpc = (U32*)(f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN);
537 const char *name = "???";
540 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
541 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
542 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
543 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
544 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
546 case FF_CHECKNL: name = "CHECKNL"; break;
547 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
548 case FF_SPACE: name = "SPACE"; break;
549 case FF_HALFSPACE: name = "HALFSPACE"; break;
550 case FF_ITEM: name = "ITEM"; break;
551 case FF_CHOP: name = "CHOP"; break;
552 case FF_LINEGLOB: name = "LINEGLOB"; break;
553 case FF_NEWLINE: name = "NEWLINE"; break;
554 case FF_MORE: name = "MORE"; break;
555 case FF_LINEMARK: name = "LINEMARK"; break;
556 case FF_END: name = "END"; break;
557 case FF_0DECIMAL: name = "0DECIMAL"; break;
558 case FF_LINESNGL: name = "LINESNGL"; break;
561 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
563 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
574 if (targ_is_utf8 && !SvUTF8(tmpForm)) {
575 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
577 sv_catpvn_utf8_upgrade(PL_formtarget, f, arg, nsv);
578 t = SvEND(PL_formtarget);
582 if (!targ_is_utf8 && DO_UTF8(tmpForm)) {
583 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
585 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC, fudge + 1);
586 t = SvEND(PL_formtarget);
606 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
613 const char *s = item = SvPV_const(sv, len);
616 itemsize = sv_len_utf8(sv);
617 if (itemsize != (I32)len) {
619 if (itemsize > fieldsize) {
620 itemsize = fieldsize;
621 itembytes = itemsize;
622 sv_pos_u2b(sv, &itembytes, 0);
626 send = chophere = s + itembytes;
636 sv_pos_b2u(sv, &itemsize);
640 item_is_utf8 = FALSE;
641 if (itemsize > fieldsize)
642 itemsize = fieldsize;
643 send = chophere = s + itemsize;
657 const char *s = item = SvPV_const(sv, len);
660 itemsize = sv_len_utf8(sv);
661 if (itemsize != (I32)len) {
663 if (itemsize <= fieldsize) {
664 const char *send = chophere = s + itemsize;
677 itemsize = fieldsize;
678 itembytes = itemsize;
679 sv_pos_u2b(sv, &itembytes, 0);
680 send = chophere = s + itembytes;
681 while (s < send || (s == send && isSPACE(*s))) {
691 if (strchr(PL_chopset, *s))
696 itemsize = chophere - item;
697 sv_pos_b2u(sv, &itemsize);
703 item_is_utf8 = FALSE;
704 if (itemsize <= fieldsize) {
705 const char *const send = chophere = s + itemsize;
718 itemsize = fieldsize;
719 send = chophere = s + itemsize;
720 while (s < send || (s == send && isSPACE(*s))) {
730 if (strchr(PL_chopset, *s))
735 itemsize = chophere - item;
741 arg = fieldsize - itemsize;
750 arg = fieldsize - itemsize;
761 const char *s = item;
765 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
767 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC,
769 t = SvEND(PL_formtarget);
773 if (UTF8_IS_CONTINUED(*s)) {
774 STRLEN skip = UTF8SKIP(s);
791 if ( !((*t++ = *s++) & ~31) )
797 if (targ_is_utf8 && !item_is_utf8) {
798 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
800 sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
801 for (; t < SvEND(PL_formtarget); t++) {
814 const int ch = *t++ = *s++;
817 if ( !((*t++ = *s++) & ~31) )
826 const char *s = chophere;
840 const bool oneline = fpc[-1] == FF_LINESNGL;
841 const char *s = item = SvPV_const(sv, len);
842 item_is_utf8 = DO_UTF8(sv);
845 STRLEN to_copy = itemsize;
846 const char *const send = s + len;
847 const U8 *source = (const U8 *) s;
851 chophere = s + itemsize;
855 to_copy = s - SvPVX_const(sv) - 1;
867 if (targ_is_utf8 && !item_is_utf8) {
868 source = tmp = bytes_to_utf8(source, &to_copy);
869 SvCUR_set(PL_formtarget,
870 t - SvPVX_const(PL_formtarget));
872 if (item_is_utf8 && !targ_is_utf8) {
873 /* Upgrade targ to UTF8, and then we reduce it to
874 a problem we have a simple solution for. */
875 SvCUR_set(PL_formtarget,
876 t - SvPVX_const(PL_formtarget));
878 /* Don't need get magic. */
879 sv_utf8_upgrade_nomg(PL_formtarget);
881 SvCUR_set(PL_formtarget,
882 t - SvPVX_const(PL_formtarget));
885 /* Easy. They agree. */
886 assert (item_is_utf8 == targ_is_utf8);
888 SvGROW(PL_formtarget,
889 SvCUR(PL_formtarget) + to_copy + fudge + 1);
890 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
892 Copy(source, t, to_copy, char);
894 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
896 if (SvGMAGICAL(sv)) {
897 /* Mustn't call sv_pos_b2u() as it does a second
898 mg_get(). Is this a bug? Do we need a _flags()
900 itemsize = utf8_length(source, source + itemsize);
902 sv_pos_b2u(sv, &itemsize);
914 #if defined(USE_LONG_DOUBLE)
917 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
921 "%#0*.*f" : "%0*.*f");
926 #if defined(USE_LONG_DOUBLE)
928 ((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
931 ((arg & 256) ? "%#*.*f" : "%*.*f");
934 /* If the field is marked with ^ and the value is undefined,
936 if ((arg & 512) && !SvOK(sv)) {
944 /* overflow evidence */
945 if (num_overflow(value, fieldsize, arg)) {
951 /* Formats aren't yet marked for locales, so assume "yes". */
953 STORE_NUMERIC_STANDARD_SET_LOCAL();
954 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
955 RESTORE_NUMERIC_STANDARD();
962 while (t-- > linemark && *t == ' ') ;
970 if (arg) { /* repeat until fields exhausted? */
972 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
973 lines += FmLINES(PL_formtarget);
975 SvUTF8_on(PL_formtarget);
976 FmLINES(PL_formtarget) = lines;
978 RETURNOP(cLISTOP->op_first);
989 const char *s = chophere;
990 const char *send = item + len;
992 while (isSPACE(*s) && (s < send))
997 arg = fieldsize - itemsize;
1004 if (strnEQ(s1," ",3)) {
1005 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1016 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1018 SvUTF8_on(PL_formtarget);
1019 FmLINES(PL_formtarget) += lines;
1031 if (PL_stack_base + *PL_markstack_ptr == SP) {
1033 if (GIMME_V == G_SCALAR)
1035 RETURNOP(PL_op->op_next->op_next);
1037 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
1038 pp_pushmark(); /* push dst */
1039 pp_pushmark(); /* push src */
1040 ENTER_with_name("grep"); /* enter outer scope */
1043 if (PL_op->op_private & OPpGREP_LEX)
1044 SAVESPTR(PAD_SVl(PL_op->op_targ));
1047 ENTER_with_name("grep_item"); /* enter inner scope */
1050 src = PL_stack_base[*PL_markstack_ptr];
1052 if (PL_op->op_private & OPpGREP_LEX)
1053 PAD_SVl(PL_op->op_targ) = src;
1058 if (PL_op->op_type == OP_MAPSTART)
1059 pp_pushmark(); /* push top */
1060 return ((LOGOP*)PL_op->op_next)->op_other;
1066 const I32 gimme = GIMME_V;
1067 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1073 /* first, move source pointer to the next item in the source list */
1074 ++PL_markstack_ptr[-1];
1076 /* if there are new items, push them into the destination list */
1077 if (items && gimme != G_VOID) {
1078 /* might need to make room back there first */
1079 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1080 /* XXX this implementation is very pessimal because the stack
1081 * is repeatedly extended for every set of items. Is possible
1082 * to do this without any stack extension or copying at all
1083 * by maintaining a separate list over which the map iterates
1084 * (like foreach does). --gsar */
1086 /* everything in the stack after the destination list moves
1087 * towards the end the stack by the amount of room needed */
1088 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1090 /* items to shift up (accounting for the moved source pointer) */
1091 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1093 /* This optimization is by Ben Tilly and it does
1094 * things differently from what Sarathy (gsar)
1095 * is describing. The downside of this optimization is
1096 * that leaves "holes" (uninitialized and hopefully unused areas)
1097 * to the Perl stack, but on the other hand this
1098 * shouldn't be a problem. If Sarathy's idea gets
1099 * implemented, this optimization should become
1100 * irrelevant. --jhi */
1102 shift = count; /* Avoid shifting too often --Ben Tilly */
1106 dst = (SP += shift);
1107 PL_markstack_ptr[-1] += shift;
1108 *PL_markstack_ptr += shift;
1112 /* copy the new items down to the destination list */
1113 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1114 if (gimme == G_ARRAY) {
1115 /* add returned items to the collection (making mortal copies
1116 * if necessary), then clear the current temps stack frame
1117 * *except* for those items. We do this splicing the items
1118 * into the start of the tmps frame (so some items may be on
1119 * the tmps stack twice), then moving PL_tmps_floor above
1120 * them, then freeing the frame. That way, the only tmps that
1121 * accumulate over iterations are the return values for map.
1122 * We have to do to this way so that everything gets correctly
1123 * freed if we die during the map.
1127 /* make space for the slice */
1128 EXTEND_MORTAL(items);
1129 tmpsbase = PL_tmps_floor + 1;
1130 Move(PL_tmps_stack + tmpsbase,
1131 PL_tmps_stack + tmpsbase + items,
1132 PL_tmps_ix - PL_tmps_floor,
1134 PL_tmps_ix += items;
1139 sv = sv_mortalcopy(sv);
1141 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1143 /* clear the stack frame except for the items */
1144 PL_tmps_floor += items;
1146 /* FREETMPS may have cleared the TEMP flag on some of the items */
1149 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1152 /* scalar context: we don't care about which values map returns
1153 * (we use undef here). And so we certainly don't want to do mortal
1154 * copies of meaningless values. */
1155 while (items-- > 0) {
1157 *dst-- = &PL_sv_undef;
1165 LEAVE_with_name("grep_item"); /* exit inner scope */
1168 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1170 (void)POPMARK; /* pop top */
1171 LEAVE_with_name("grep"); /* exit outer scope */
1172 (void)POPMARK; /* pop src */
1173 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1174 (void)POPMARK; /* pop dst */
1175 SP = PL_stack_base + POPMARK; /* pop original mark */
1176 if (gimme == G_SCALAR) {
1177 if (PL_op->op_private & OPpGREP_LEX) {
1178 SV* sv = sv_newmortal();
1179 sv_setiv(sv, items);
1187 else if (gimme == G_ARRAY)
1194 ENTER_with_name("grep_item"); /* enter inner scope */
1197 /* set $_ to the new source item */
1198 src = PL_stack_base[PL_markstack_ptr[-1]];
1200 if (PL_op->op_private & OPpGREP_LEX)
1201 PAD_SVl(PL_op->op_targ) = src;
1205 RETURNOP(cLOGOP->op_other);
1214 if (GIMME == G_ARRAY)
1216 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1217 return cLOGOP->op_other;
1227 if (GIMME == G_ARRAY) {
1228 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1232 SV * const targ = PAD_SV(PL_op->op_targ);
1235 if (PL_op->op_private & OPpFLIP_LINENUM) {
1236 if (GvIO(PL_last_in_gv)) {
1237 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1240 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1242 flip = SvIV(sv) == SvIV(GvSV(gv));
1248 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1249 if (PL_op->op_flags & OPf_SPECIAL) {
1257 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1260 sv_setpvs(TARG, "");
1266 /* This code tries to decide if "$left .. $right" should use the
1267 magical string increment, or if the range is numeric (we make
1268 an exception for .."0" [#18165]). AMS 20021031. */
1270 #define RANGE_IS_NUMERIC(left,right) ( \
1271 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1272 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1273 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1274 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1275 && (!SvOK(right) || looks_like_number(right))))
1281 if (GIMME == G_ARRAY) {
1287 if (RANGE_IS_NUMERIC(left,right)) {
1290 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1291 (SvOK(right) && SvNV(right) > IV_MAX))
1292 DIE(aTHX_ "Range iterator outside integer range");
1303 SV * const sv = sv_2mortal(newSViv(i++));
1308 SV * const final = sv_mortalcopy(right);
1310 const char * const tmps = SvPV_const(final, len);
1312 SV *sv = sv_mortalcopy(left);
1313 SvPV_force_nolen(sv);
1314 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1316 if (strEQ(SvPVX_const(sv),tmps))
1318 sv = sv_2mortal(newSVsv(sv));
1325 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1329 if (PL_op->op_private & OPpFLIP_LINENUM) {
1330 if (GvIO(PL_last_in_gv)) {
1331 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1334 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1335 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1343 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1344 sv_catpvs(targ, "E0");
1354 static const char * const context_name[] = {
1356 NULL, /* CXt_WHEN never actually needs "block" */
1357 NULL, /* CXt_BLOCK never actually needs "block" */
1358 NULL, /* CXt_GIVEN never actually needs "block" */
1359 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1360 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1361 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1362 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1370 S_dopoptolabel(pTHX_ const char *label)
1375 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1377 for (i = cxstack_ix; i >= 0; i--) {
1378 register const PERL_CONTEXT * const cx = &cxstack[i];
1379 switch (CxTYPE(cx)) {
1385 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1386 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1387 if (CxTYPE(cx) == CXt_NULL)
1390 case CXt_LOOP_LAZYIV:
1391 case CXt_LOOP_LAZYSV:
1393 case CXt_LOOP_PLAIN:
1395 const char *cx_label = CxLABEL(cx);
1396 if (!cx_label || strNE(label, cx_label) ) {
1397 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1398 (long)i, cx_label));
1401 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1412 Perl_dowantarray(pTHX)
1415 const I32 gimme = block_gimme();
1416 return (gimme == G_VOID) ? G_SCALAR : gimme;
1420 Perl_block_gimme(pTHX)
1423 const I32 cxix = dopoptosub(cxstack_ix);
1427 switch (cxstack[cxix].blk_gimme) {
1435 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1442 Perl_is_lvalue_sub(pTHX)
1445 const I32 cxix = dopoptosub(cxstack_ix);
1446 assert(cxix >= 0); /* We should only be called from inside subs */
1448 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1449 return CxLVAL(cxstack + cxix);
1455 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1460 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1462 for (i = startingblock; i >= 0; i--) {
1463 register const PERL_CONTEXT * const cx = &cxstk[i];
1464 switch (CxTYPE(cx)) {
1470 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1478 S_dopoptoeval(pTHX_ I32 startingblock)
1482 for (i = startingblock; i >= 0; i--) {
1483 register const PERL_CONTEXT *cx = &cxstack[i];
1484 switch (CxTYPE(cx)) {
1488 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1496 S_dopoptoloop(pTHX_ I32 startingblock)
1500 for (i = startingblock; i >= 0; i--) {
1501 register const PERL_CONTEXT * const cx = &cxstack[i];
1502 switch (CxTYPE(cx)) {
1508 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1509 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1510 if ((CxTYPE(cx)) == CXt_NULL)
1513 case CXt_LOOP_LAZYIV:
1514 case CXt_LOOP_LAZYSV:
1516 case CXt_LOOP_PLAIN:
1517 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1525 S_dopoptogiven(pTHX_ I32 startingblock)
1529 for (i = startingblock; i >= 0; i--) {
1530 register const PERL_CONTEXT *cx = &cxstack[i];
1531 switch (CxTYPE(cx)) {
1535 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
1537 case CXt_LOOP_PLAIN:
1538 assert(!CxFOREACHDEF(cx));
1540 case CXt_LOOP_LAZYIV:
1541 case CXt_LOOP_LAZYSV:
1543 if (CxFOREACHDEF(cx)) {
1544 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
1553 S_dopoptowhen(pTHX_ I32 startingblock)
1557 for (i = startingblock; i >= 0; i--) {
1558 register const PERL_CONTEXT *cx = &cxstack[i];
1559 switch (CxTYPE(cx)) {
1563 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1571 Perl_dounwind(pTHX_ I32 cxix)
1576 while (cxstack_ix > cxix) {
1578 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1579 DEBUG_CX("UNWIND"); \
1580 /* Note: we don't need to restore the base context info till the end. */
1581 switch (CxTYPE(cx)) {
1584 continue; /* not break */
1592 case CXt_LOOP_LAZYIV:
1593 case CXt_LOOP_LAZYSV:
1595 case CXt_LOOP_PLAIN:
1606 PERL_UNUSED_VAR(optype);
1610 Perl_qerror(pTHX_ SV *err)
1614 PERL_ARGS_ASSERT_QERROR;
1617 if (PL_in_eval & EVAL_KEEPERR) {
1618 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1619 SvPV_nolen_const(err));
1622 sv_catsv(ERRSV, err);
1625 sv_catsv(PL_errors, err);
1627 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
1629 ++PL_parser->error_count;
1633 Perl_die_unwind(pTHX_ SV *msv)
1636 SV *exceptsv = sv_mortalcopy(msv);
1637 U8 in_eval = PL_in_eval;
1638 PERL_ARGS_ASSERT_DIE_UNWIND;
1644 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1645 && PL_curstackinfo->si_prev)
1654 register PERL_CONTEXT *cx;
1657 JMPENV *restartjmpenv;
1660 if (cxix < cxstack_ix)
1663 POPBLOCK(cx,PL_curpm);
1664 if (CxTYPE(cx) != CXt_EVAL) {
1666 const char* message = SvPVx_const(exceptsv, msglen);
1667 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
1668 PerlIO_write(Perl_error_log, message, msglen);
1672 namesv = cx->blk_eval.old_namesv;
1673 oldcop = cx->blk_oldcop;
1674 restartjmpenv = cx->blk_eval.cur_top_env;
1675 restartop = cx->blk_eval.retop;
1677 if (gimme == G_SCALAR)
1678 *++newsp = &PL_sv_undef;
1679 PL_stack_sp = newsp;
1683 /* LEAVE could clobber PL_curcop (see save_re_context())
1684 * XXX it might be better to find a way to avoid messing with
1685 * PL_curcop in save_re_context() instead, but this is a more
1686 * minimal fix --GSAR */
1689 if (optype == OP_REQUIRE) {
1690 const char* const msg = SvPVx_nolen_const(exceptsv);
1691 (void)hv_store(GvHVn(PL_incgv),
1692 SvPVX_const(namesv), SvCUR(namesv),
1694 /* note that unlike pp_entereval, pp_require isn't
1695 * supposed to trap errors. So now that we've popped the
1696 * EVAL that pp_require pushed, and processed the error
1697 * message, rethrow the error */
1698 Perl_croak(aTHX_ "%sCompilation failed in require",
1699 *msg ? msg : "Unknown error\n");
1701 if (in_eval & EVAL_KEEPERR) {
1702 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1703 SvPV_nolen_const(exceptsv));
1706 sv_setsv(ERRSV, exceptsv);
1708 PL_restartjmpenv = restartjmpenv;
1709 PL_restartop = restartop;
1715 write_to_stderr(exceptsv);
1722 dVAR; dSP; dPOPTOPssrl;
1723 if (SvTRUE(left) != SvTRUE(right))
1730 =for apidoc caller_cx
1732 The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1733 returned C<PERL_CONTEXT> structure can be interrogated to find all the
1734 information returned to Perl by C<caller>. Note that XSUBs don't get a
1735 stack frame, so C<caller_cx(0, NULL)> will return information for the
1736 immediately-surrounding Perl code.
1738 This function skips over the automatic calls to C<&DB::sub> made on the
1739 behalf of the debugger. If the stack frame requested was a sub called by
1740 C<DB::sub>, the return value will be the frame for the call to
1741 C<DB::sub>, since that has the correct line number/etc. for the call
1742 site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1743 frame for the sub call itself.
1748 const PERL_CONTEXT *
1749 Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
1751 register I32 cxix = dopoptosub(cxstack_ix);
1752 register const PERL_CONTEXT *cx;
1753 register const PERL_CONTEXT *ccstack = cxstack;
1754 const PERL_SI *top_si = PL_curstackinfo;
1757 /* we may be in a higher stacklevel, so dig down deeper */
1758 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1759 top_si = top_si->si_prev;
1760 ccstack = top_si->si_cxstack;
1761 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1765 /* caller() should not report the automatic calls to &DB::sub */
1766 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1767 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1771 cxix = dopoptosub_at(ccstack, cxix - 1);
1774 cx = &ccstack[cxix];
1775 if (dbcxp) *dbcxp = cx;
1777 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1778 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1779 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1780 field below is defined for any cx. */
1781 /* caller() should not report the automatic calls to &DB::sub */
1782 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1783 cx = &ccstack[dbcxix];
1793 register const PERL_CONTEXT *cx;
1794 const PERL_CONTEXT *dbcx;
1796 const char *stashname;
1802 cx = caller_cx(count, &dbcx);
1804 if (GIMME != G_ARRAY) {
1811 stashname = CopSTASHPV(cx->blk_oldcop);
1812 if (GIMME != G_ARRAY) {
1815 PUSHs(&PL_sv_undef);
1818 sv_setpv(TARG, stashname);
1827 PUSHs(&PL_sv_undef);
1829 mPUSHs(newSVpv(stashname, 0));
1830 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1831 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1834 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1835 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
1836 /* So is ccstack[dbcxix]. */
1838 SV * const sv = newSV(0);
1839 gv_efullname3(sv, cvgv, NULL);
1841 PUSHs(boolSV(CxHASARGS(cx)));
1844 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1845 PUSHs(boolSV(CxHASARGS(cx)));
1849 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1852 gimme = (I32)cx->blk_gimme;
1853 if (gimme == G_VOID)
1854 PUSHs(&PL_sv_undef);
1856 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1857 if (CxTYPE(cx) == CXt_EVAL) {
1859 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1860 PUSHs(cx->blk_eval.cur_text);
1864 else if (cx->blk_eval.old_namesv) {
1865 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1868 /* eval BLOCK (try blocks have old_namesv == 0) */
1870 PUSHs(&PL_sv_undef);
1871 PUSHs(&PL_sv_undef);
1875 PUSHs(&PL_sv_undef);
1876 PUSHs(&PL_sv_undef);
1878 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1879 && CopSTASH_eq(PL_curcop, PL_debstash))
1881 AV * const ary = cx->blk_sub.argarray;
1882 const int off = AvARRAY(ary) - AvALLOC(ary);
1885 Perl_init_dbargs(aTHX);
1887 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1888 av_extend(PL_dbargs, AvFILLp(ary) + off);
1889 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1890 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1892 /* XXX only hints propagated via op_private are currently
1893 * visible (others are not easily accessible, since they
1894 * use the global PL_hints) */
1895 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1898 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1900 if (old_warnings == pWARN_NONE ||
1901 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1902 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1903 else if (old_warnings == pWARN_ALL ||
1904 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1905 /* Get the bit mask for $warnings::Bits{all}, because
1906 * it could have been extended by warnings::register */
1908 HV * const bits = get_hv("warnings::Bits", 0);
1909 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
1910 mask = newSVsv(*bits_all);
1913 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1917 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
1921 PUSHs(cx->blk_oldcop->cop_hints_hash ?
1922 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
1931 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
1932 sv_reset(tmps, CopSTASH(PL_curcop));
1937 /* like pp_nextstate, but used instead when the debugger is active */
1942 PL_curcop = (COP*)PL_op;
1943 TAINT_NOT; /* Each statement is presumed innocent */
1944 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1949 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1950 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1953 register PERL_CONTEXT *cx;
1954 const I32 gimme = G_ARRAY;
1956 GV * const gv = PL_DBgv;
1957 register CV * const cv = GvCV(gv);
1960 DIE(aTHX_ "No DB::DB routine defined");
1962 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1963 /* don't do recursive DB::DB call */
1978 (void)(*CvXSUB(cv))(aTHX_ cv);
1985 PUSHBLOCK(cx, CXt_SUB, SP);
1987 cx->blk_sub.retop = PL_op->op_next;
1990 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1991 RETURNOP(CvSTART(cv));
2001 register PERL_CONTEXT *cx;
2002 const I32 gimme = GIMME_V;
2003 void *itervar; /* location of the iteration variable */
2004 U8 cxtype = CXt_LOOP_FOR;
2006 ENTER_with_name("loop1");
2009 if (PL_op->op_targ) { /* "my" variable */
2010 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
2011 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2012 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2013 SVs_PADSTALE, SVs_PADSTALE);
2015 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
2017 itervar = PL_comppad;
2019 itervar = &PAD_SVl(PL_op->op_targ);
2022 else { /* symbol table variable */
2023 GV * const gv = MUTABLE_GV(POPs);
2024 SV** svp = &GvSV(gv);
2025 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
2027 itervar = (void *)gv;
2030 if (PL_op->op_private & OPpITER_DEF)
2031 cxtype |= CXp_FOR_DEF;
2033 ENTER_with_name("loop2");
2035 PUSHBLOCK(cx, cxtype, SP);
2036 PUSHLOOP_FOR(cx, itervar, MARK);
2037 if (PL_op->op_flags & OPf_STACKED) {
2038 SV *maybe_ary = POPs;
2039 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2041 SV * const right = maybe_ary;
2044 if (RANGE_IS_NUMERIC(sv,right)) {
2045 cx->cx_type &= ~CXTYPEMASK;
2046 cx->cx_type |= CXt_LOOP_LAZYIV;
2047 /* Make sure that no-one re-orders cop.h and breaks our
2049 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
2050 #ifdef NV_PRESERVES_UV
2051 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
2052 (SvNV(sv) > (NV)IV_MAX)))
2054 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
2055 (SvNV(right) < (NV)IV_MIN))))
2057 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
2060 ((SvUV(sv) > (UV)IV_MAX) ||
2061 (SvNV(sv) > (NV)UV_MAX)))))
2063 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
2065 ((SvNV(right) > 0) &&
2066 ((SvUV(right) > (UV)IV_MAX) ||
2067 (SvNV(right) > (NV)UV_MAX))))))
2069 DIE(aTHX_ "Range iterator outside integer range");
2070 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2071 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
2073 /* for correct -Dstv display */
2074 cx->blk_oldsp = sp - PL_stack_base;
2078 cx->cx_type &= ~CXTYPEMASK;
2079 cx->cx_type |= CXt_LOOP_LAZYSV;
2080 /* Make sure that no-one re-orders cop.h and breaks our
2082 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2083 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2084 cx->blk_loop.state_u.lazysv.end = right;
2085 SvREFCNT_inc(right);
2086 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2087 /* This will do the upgrade to SVt_PV, and warn if the value
2088 is uninitialised. */
2089 (void) SvPV_nolen_const(right);
2090 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2091 to replace !SvOK() with a pointer to "". */
2093 SvREFCNT_dec(right);
2094 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2098 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2099 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2100 SvREFCNT_inc(maybe_ary);
2101 cx->blk_loop.state_u.ary.ix =
2102 (PL_op->op_private & OPpITER_REVERSED) ?
2103 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2107 else { /* iterating over items on the stack */
2108 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2109 if (PL_op->op_private & OPpITER_REVERSED) {
2110 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2113 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2123 register PERL_CONTEXT *cx;
2124 const I32 gimme = GIMME_V;
2126 ENTER_with_name("loop1");
2128 ENTER_with_name("loop2");
2130 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2131 PUSHLOOP_PLAIN(cx, SP);
2139 register PERL_CONTEXT *cx;
2146 assert(CxTYPE_is_LOOP(cx));
2148 newsp = PL_stack_base + cx->blk_loop.resetsp;
2151 if (gimme == G_VOID)
2153 else if (gimme == G_SCALAR) {
2155 *++newsp = sv_mortalcopy(*SP);
2157 *++newsp = &PL_sv_undef;
2161 *++newsp = sv_mortalcopy(*++mark);
2162 TAINT_NOT; /* Each item is independent */
2168 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2169 PL_curpm = newpm; /* ... and pop $1 et al */
2171 LEAVE_with_name("loop2");
2172 LEAVE_with_name("loop1");
2180 register PERL_CONTEXT *cx;
2181 bool popsub2 = FALSE;
2182 bool clear_errsv = FALSE;
2191 const I32 cxix = dopoptosub(cxstack_ix);
2194 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2195 * sort block, which is a CXt_NULL
2198 PL_stack_base[1] = *PL_stack_sp;
2199 PL_stack_sp = PL_stack_base + 1;
2203 DIE(aTHX_ "Can't return outside a subroutine");
2205 if (cxix < cxstack_ix)
2208 if (CxMULTICALL(&cxstack[cxix])) {
2209 gimme = cxstack[cxix].blk_gimme;
2210 if (gimme == G_VOID)
2211 PL_stack_sp = PL_stack_base;
2212 else if (gimme == G_SCALAR) {
2213 PL_stack_base[1] = *PL_stack_sp;
2214 PL_stack_sp = PL_stack_base + 1;
2220 switch (CxTYPE(cx)) {
2223 retop = cx->blk_sub.retop;
2224 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2227 if (!(PL_in_eval & EVAL_KEEPERR))
2230 namesv = cx->blk_eval.old_namesv;
2231 retop = cx->blk_eval.retop;
2234 if (optype == OP_REQUIRE &&
2235 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2237 /* Unassume the success we assumed earlier. */
2238 (void)hv_delete(GvHVn(PL_incgv),
2239 SvPVX_const(namesv), SvCUR(namesv),
2241 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
2246 retop = cx->blk_sub.retop;
2249 DIE(aTHX_ "panic: return");
2253 if (gimme == G_SCALAR) {
2256 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2258 *++newsp = SvREFCNT_inc(*SP);
2263 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2265 *++newsp = sv_mortalcopy(sv);
2270 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
2273 *++newsp = sv_mortalcopy(*SP);
2276 *++newsp = &PL_sv_undef;
2278 else if (gimme == G_ARRAY) {
2279 while (++MARK <= SP) {
2280 *++newsp = (popsub2 && SvTEMP(*MARK))
2281 ? *MARK : sv_mortalcopy(*MARK);
2282 TAINT_NOT; /* Each item is independent */
2285 PL_stack_sp = newsp;
2288 /* Stack values are safe: */
2291 POPSUB(cx,sv); /* release CV and @_ ... */
2295 PL_curpm = newpm; /* ... and pop $1 et al */
2308 register PERL_CONTEXT *cx;
2319 if (PL_op->op_flags & OPf_SPECIAL) {
2320 cxix = dopoptoloop(cxstack_ix);
2322 DIE(aTHX_ "Can't \"last\" outside a loop block");
2325 cxix = dopoptolabel(cPVOP->op_pv);
2327 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
2329 if (cxix < cxstack_ix)
2333 cxstack_ix++; /* temporarily protect top context */
2335 switch (CxTYPE(cx)) {
2336 case CXt_LOOP_LAZYIV:
2337 case CXt_LOOP_LAZYSV:
2339 case CXt_LOOP_PLAIN:
2341 newsp = PL_stack_base + cx->blk_loop.resetsp;
2342 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2346 nextop = cx->blk_sub.retop;
2350 nextop = cx->blk_eval.retop;
2354 nextop = cx->blk_sub.retop;
2357 DIE(aTHX_ "panic: last");
2361 if (gimme == G_SCALAR) {
2363 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2364 ? *SP : sv_mortalcopy(*SP);
2366 *++newsp = &PL_sv_undef;
2368 else if (gimme == G_ARRAY) {
2369 while (++MARK <= SP) {
2370 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2371 ? *MARK : sv_mortalcopy(*MARK);
2372 TAINT_NOT; /* Each item is independent */
2380 /* Stack values are safe: */
2382 case CXt_LOOP_LAZYIV:
2383 case CXt_LOOP_PLAIN:
2384 case CXt_LOOP_LAZYSV:
2386 POPLOOP(cx); /* release loop vars ... */
2390 POPSUB(cx,sv); /* release CV and @_ ... */
2393 PL_curpm = newpm; /* ... and pop $1 et al */
2396 PERL_UNUSED_VAR(optype);
2397 PERL_UNUSED_VAR(gimme);
2405 register PERL_CONTEXT *cx;
2408 if (PL_op->op_flags & OPf_SPECIAL) {
2409 cxix = dopoptoloop(cxstack_ix);
2411 DIE(aTHX_ "Can't \"next\" outside a loop block");
2414 cxix = dopoptolabel(cPVOP->op_pv);
2416 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2418 if (cxix < cxstack_ix)
2421 /* clear off anything above the scope we're re-entering, but
2422 * save the rest until after a possible continue block */
2423 inner = PL_scopestack_ix;
2425 if (PL_scopestack_ix < inner)
2426 leave_scope(PL_scopestack[PL_scopestack_ix]);
2427 PL_curcop = cx->blk_oldcop;
2428 return (cx)->blk_loop.my_op->op_nextop;
2435 register PERL_CONTEXT *cx;
2439 if (PL_op->op_flags & OPf_SPECIAL) {
2440 cxix = dopoptoloop(cxstack_ix);
2442 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2445 cxix = dopoptolabel(cPVOP->op_pv);
2447 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2449 if (cxix < cxstack_ix)
2452 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2453 if (redo_op->op_type == OP_ENTER) {
2454 /* pop one less context to avoid $x being freed in while (my $x..) */
2456 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2457 redo_op = redo_op->op_next;
2461 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2462 LEAVE_SCOPE(oldsave);
2464 PL_curcop = cx->blk_oldcop;
2469 S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
2473 static const char too_deep[] = "Target of goto is too deeply nested";
2475 PERL_ARGS_ASSERT_DOFINDLABEL;
2478 Perl_croak(aTHX_ too_deep);
2479 if (o->op_type == OP_LEAVE ||
2480 o->op_type == OP_SCOPE ||
2481 o->op_type == OP_LEAVELOOP ||
2482 o->op_type == OP_LEAVESUB ||
2483 o->op_type == OP_LEAVETRY)
2485 *ops++ = cUNOPo->op_first;
2487 Perl_croak(aTHX_ too_deep);
2490 if (o->op_flags & OPf_KIDS) {
2492 /* First try all the kids at this level, since that's likeliest. */
2493 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2494 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2495 const char *kid_label = CopLABEL(kCOP);
2496 if (kid_label && strEQ(kid_label, label))
2500 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2501 if (kid == PL_lastgotoprobe)
2503 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2506 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2507 ops[-1]->op_type == OP_DBSTATE)
2512 if ((o = dofindlabel(kid, label, ops, oplimit)))
2525 register PERL_CONTEXT *cx;
2526 #define GOTO_DEPTH 64
2527 OP *enterops[GOTO_DEPTH];
2528 const char *label = NULL;
2529 const bool do_dump = (PL_op->op_type == OP_DUMP);
2530 static const char must_have_label[] = "goto must have label";
2532 if (PL_op->op_flags & OPf_STACKED) {
2533 SV * const sv = POPs;
2535 /* This egregious kludge implements goto &subroutine */
2536 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2538 register PERL_CONTEXT *cx;
2539 CV *cv = MUTABLE_CV(SvRV(sv));
2546 if (!CvROOT(cv) && !CvXSUB(cv)) {
2547 const GV * const gv = CvGV(cv);
2551 /* autoloaded stub? */
2552 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2554 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2555 GvNAMELEN(gv), FALSE);
2556 if (autogv && (cv = GvCV(autogv)))
2558 tmpstr = sv_newmortal();
2559 gv_efullname3(tmpstr, gv, NULL);
2560 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2562 DIE(aTHX_ "Goto undefined subroutine");
2565 /* First do some returnish stuff. */
2566 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2568 cxix = dopoptosub(cxstack_ix);
2570 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2571 if (cxix < cxstack_ix)
2575 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2576 if (CxTYPE(cx) == CXt_EVAL) {
2578 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2580 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2582 else if (CxMULTICALL(cx))
2583 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2584 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2585 /* put @_ back onto stack */
2586 AV* av = cx->blk_sub.argarray;
2588 items = AvFILLp(av) + 1;
2589 EXTEND(SP, items+1); /* @_ could have been extended. */
2590 Copy(AvARRAY(av), SP + 1, items, SV*);
2591 SvREFCNT_dec(GvAV(PL_defgv));
2592 GvAV(PL_defgv) = cx->blk_sub.savearray;
2594 /* abandon @_ if it got reified */
2599 av_extend(av, items-1);
2601 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2604 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2605 AV* const av = GvAV(PL_defgv);
2606 items = AvFILLp(av) + 1;
2607 EXTEND(SP, items+1); /* @_ could have been extended. */
2608 Copy(AvARRAY(av), SP + 1, items, SV*);
2612 if (CxTYPE(cx) == CXt_SUB &&
2613 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2614 SvREFCNT_dec(cx->blk_sub.cv);
2615 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2616 LEAVE_SCOPE(oldsave);
2618 /* Now do some callish stuff. */
2620 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2622 OP* const retop = cx->blk_sub.retop;
2627 for (index=0; index<items; index++)
2628 sv_2mortal(SP[-index]);
2631 /* XS subs don't have a CxSUB, so pop it */
2632 POPBLOCK(cx, PL_curpm);
2633 /* Push a mark for the start of arglist */
2636 (void)(*CvXSUB(cv))(aTHX_ cv);
2641 AV* const padlist = CvPADLIST(cv);
2642 if (CxTYPE(cx) == CXt_EVAL) {
2643 PL_in_eval = CxOLD_IN_EVAL(cx);
2644 PL_eval_root = cx->blk_eval.old_eval_root;
2645 cx->cx_type = CXt_SUB;
2647 cx->blk_sub.cv = cv;
2648 cx->blk_sub.olddepth = CvDEPTH(cv);
2651 if (CvDEPTH(cv) < 2)
2652 SvREFCNT_inc_simple_void_NN(cv);
2654 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2655 sub_crush_depth(cv);
2656 pad_push(padlist, CvDEPTH(cv));
2659 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2662 AV *const av = MUTABLE_AV(PAD_SVl(0));
2664 cx->blk_sub.savearray = GvAV(PL_defgv);
2665 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
2666 CX_CURPAD_SAVE(cx->blk_sub);
2667 cx->blk_sub.argarray = av;
2669 if (items >= AvMAX(av) + 1) {
2670 SV **ary = AvALLOC(av);
2671 if (AvARRAY(av) != ary) {
2672 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2675 if (items >= AvMAX(av) + 1) {
2676 AvMAX(av) = items - 1;
2677 Renew(ary,items+1,SV*);
2683 Copy(mark,AvARRAY(av),items,SV*);
2684 AvFILLp(av) = items - 1;
2685 assert(!AvREAL(av));
2687 /* transfer 'ownership' of refcnts to new @_ */
2697 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2698 Perl_get_db_sub(aTHX_ NULL, cv);
2700 CV * const gotocv = get_cvs("DB::goto", 0);
2702 PUSHMARK( PL_stack_sp );
2703 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
2708 RETURNOP(CvSTART(cv));
2712 label = SvPV_nolen_const(sv);
2713 if (!(do_dump || *label))
2714 DIE(aTHX_ must_have_label);
2717 else if (PL_op->op_flags & OPf_SPECIAL) {
2719 DIE(aTHX_ must_have_label);
2722 label = cPVOP->op_pv;
2726 if (label && *label) {
2727 OP *gotoprobe = NULL;
2728 bool leaving_eval = FALSE;
2729 bool in_block = FALSE;
2730 PERL_CONTEXT *last_eval_cx = NULL;
2734 PL_lastgotoprobe = NULL;
2736 for (ix = cxstack_ix; ix >= 0; ix--) {
2738 switch (CxTYPE(cx)) {
2740 leaving_eval = TRUE;
2741 if (!CxTRYBLOCK(cx)) {
2742 gotoprobe = (last_eval_cx ?
2743 last_eval_cx->blk_eval.old_eval_root :
2748 /* else fall through */
2749 case CXt_LOOP_LAZYIV:
2750 case CXt_LOOP_LAZYSV:
2752 case CXt_LOOP_PLAIN:
2755 gotoprobe = cx->blk_oldcop->op_sibling;
2761 gotoprobe = cx->blk_oldcop->op_sibling;
2764 gotoprobe = PL_main_root;
2767 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
2768 gotoprobe = CvROOT(cx->blk_sub.cv);
2774 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2777 DIE(aTHX_ "panic: goto");
2778 gotoprobe = PL_main_root;
2782 retop = dofindlabel(gotoprobe, label,
2783 enterops, enterops + GOTO_DEPTH);
2786 if (gotoprobe->op_sibling &&
2787 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
2788 gotoprobe->op_sibling->op_sibling) {
2789 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
2790 label, enterops, enterops + GOTO_DEPTH);
2795 PL_lastgotoprobe = gotoprobe;
2798 DIE(aTHX_ "Can't find label %s", label);
2800 /* if we're leaving an eval, check before we pop any frames
2801 that we're not going to punt, otherwise the error
2804 if (leaving_eval && *enterops && enterops[1]) {
2806 for (i = 1; enterops[i]; i++)
2807 if (enterops[i]->op_type == OP_ENTERITER)
2808 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2811 if (*enterops && enterops[1]) {
2812 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2814 deprecate("\"goto\" to jump into a construct");
2817 /* pop unwanted frames */
2819 if (ix < cxstack_ix) {
2826 oldsave = PL_scopestack[PL_scopestack_ix];
2827 LEAVE_SCOPE(oldsave);
2830 /* push wanted frames */
2832 if (*enterops && enterops[1]) {
2833 OP * const oldop = PL_op;
2834 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2835 for (; enterops[ix]; ix++) {
2836 PL_op = enterops[ix];
2837 /* Eventually we may want to stack the needed arguments
2838 * for each op. For now, we punt on the hard ones. */
2839 if (PL_op->op_type == OP_ENTERITER)
2840 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2841 PL_op->op_ppaddr(aTHX);
2849 if (!retop) retop = PL_main_start;
2851 PL_restartop = retop;
2852 PL_do_undump = TRUE;
2856 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2857 PL_do_undump = FALSE;
2874 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2876 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
2879 PL_exit_flags |= PERL_EXIT_EXPECTED;
2881 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2882 if (anum || !(PL_minus_c && PL_madskills))
2887 PUSHs(&PL_sv_undef);
2894 S_save_lines(pTHX_ AV *array, SV *sv)
2896 const char *s = SvPVX_const(sv);
2897 const char * const send = SvPVX_const(sv) + SvCUR(sv);
2900 PERL_ARGS_ASSERT_SAVE_LINES;
2902 while (s && s < send) {
2904 SV * const tmpstr = newSV_type(SVt_PVMG);
2906 t = (const char *)memchr(s, '\n', send - s);
2912 sv_setpvn(tmpstr, s, t - s);
2913 av_store(array, line++, tmpstr);
2921 Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
2923 0 is used as continue inside eval,
2925 3 is used for a die caught by an inner eval - continue inner loop
2927 See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
2928 establish a local jmpenv to handle exception traps.
2933 S_docatch(pTHX_ OP *o)
2937 OP * const oldop = PL_op;
2941 assert(CATCH_GET == TRUE);
2948 assert(cxstack_ix >= 0);
2949 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2950 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
2955 /* die caught by an inner eval - continue inner loop */
2956 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
2957 PL_restartjmpenv = NULL;
2958 PL_op = PL_restartop;
2974 /* James Bond: Do you expect me to talk?
2975 Auric Goldfinger: No, Mr. Bond. I expect you to die.
2977 This code is an ugly hack, doesn't work with lexicals in subroutines that are
2978 called more than once, and is only used by regcomp.c, for (?{}) blocks.
2980 Currently it is not used outside the core code. Best if it stays that way.
2983 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
2984 /* sv Text to convert to OP tree. */
2985 /* startop op_free() this to undo. */
2986 /* code Short string id of the caller. */
2988 dVAR; dSP; /* Make POPBLOCK work. */
2994 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2995 char *tmpbuf = tbuf;
2998 CV* runcv = NULL; /* initialise to avoid compiler warnings */
3002 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3004 ENTER_with_name("eval");
3005 lex_start(sv, NULL, 0);
3007 /* switch to eval mode */
3009 if (IN_PERL_COMPILETIME) {
3010 SAVECOPSTASH_FREE(&PL_compiling);
3011 CopSTASH_set(&PL_compiling, PL_curstash);
3013 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3014 SV * const sv = sv_newmortal();
3015 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3016 code, (unsigned long)++PL_evalseq,
3017 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3022 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3023 (unsigned long)++PL_evalseq);
3024 SAVECOPFILE_FREE(&PL_compiling);
3025 CopFILE_set(&PL_compiling, tmpbuf+2);
3026 SAVECOPLINE(&PL_compiling);
3027 CopLINE_set(&PL_compiling, 1);
3028 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3029 deleting the eval's FILEGV from the stash before gv_check() runs
3030 (i.e. before run-time proper). To work around the coredump that
3031 ensues, we always turn GvMULTI_on for any globals that were
3032 introduced within evals. See force_ident(). GSAR 96-10-12 */
3033 safestr = savepvn(tmpbuf, len);
3034 SAVEDELETE(PL_defstash, safestr, len);
3036 #ifdef OP_IN_REGISTER
3042 /* we get here either during compilation, or via pp_regcomp at runtime */
3043 runtime = IN_PERL_RUNTIME;
3045 runcv = find_runcv(NULL);
3048 PL_op->op_type = OP_ENTEREVAL;
3049 PL_op->op_flags = 0; /* Avoid uninit warning. */
3050 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
3052 need_catch = CATCH_GET;
3056 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
3058 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
3059 CATCH_SET(need_catch);
3060 POPBLOCK(cx,PL_curpm);
3063 (*startop)->op_type = OP_NULL;
3064 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
3065 /* XXX DAPM do this properly one year */
3066 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
3067 LEAVE_with_name("eval");
3068 if (IN_PERL_COMPILETIME)
3069 CopHINTS_set(&PL_compiling, PL_hints);
3070 #ifdef OP_IN_REGISTER
3073 PERL_UNUSED_VAR(newsp);
3074 PERL_UNUSED_VAR(optype);
3076 return PL_eval_start;
3081 =for apidoc find_runcv
3083 Locate the CV corresponding to the currently executing sub or eval.
3084 If db_seqp is non_null, skip CVs that are in the DB package and populate
3085 *db_seqp with the cop sequence number at the point that the DB:: code was
3086 entered. (allows debuggers to eval in the scope of the breakpoint rather
3087 than in the scope of the debugger itself).
3093 Perl_find_runcv(pTHX_ U32 *db_seqp)
3099 *db_seqp = PL_curcop->cop_seq;
3100 for (si = PL_curstackinfo; si; si = si->si_prev) {
3102 for (ix = si->si_cxix; ix >= 0; ix--) {
3103 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3104 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3105 CV * const cv = cx->blk_sub.cv;
3106 /* skip DB:: code */
3107 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3108 *db_seqp = cx->blk_oldcop->cop_seq;
3113 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3121 /* Run yyparse() in a setjmp wrapper. Returns:
3122 * 0: yyparse() successful
3123 * 1: yyparse() failed
3127 S_try_yyparse(pTHX_ int gramtype)
3132 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3136 ret = yyparse(gramtype) ? 1 : 0;
3150 /* Compile a require/do, an eval '', or a /(?{...})/.
3151 * In the last case, startop is non-null, and contains the address of
3152 * a pointer that should be set to the just-compiled code.
3153 * outside is the lexically enclosing CV (if any) that invoked us.
3154 * Returns a bool indicating whether the compile was successful; if so,
3155 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3156 * pushes undef (also croaks if startop != NULL).
3160 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
3163 OP * const saveop = PL_op;
3164 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3167 PL_in_eval = (in_require
3168 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3173 SAVESPTR(PL_compcv);
3174 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3175 CvEVAL_on(PL_compcv);
3176 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3177 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3179 CvOUTSIDE_SEQ(PL_compcv) = seq;
3180 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3182 /* set up a scratch pad */
3184 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
3185 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3189 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
3191 /* make sure we compile in the right package */
3193 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3194 SAVESPTR(PL_curstash);
3195 PL_curstash = CopSTASH(PL_curcop);
3197 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3198 SAVESPTR(PL_beginav);
3199 PL_beginav = newAV();
3200 SAVEFREESV(PL_beginav);
3201 SAVESPTR(PL_unitcheckav);
3202 PL_unitcheckav = newAV();
3203 SAVEFREESV(PL_unitcheckav);
3206 SAVEBOOL(PL_madskills);
3210 /* try to compile it */
3212 PL_eval_root = NULL;
3213 PL_curcop = &PL_compiling;
3214 CopARYBASE_set(PL_curcop, 0);
3215 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3216 PL_in_eval |= EVAL_KEEPERR;
3220 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3222 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3223 * so honour CATCH_GET and trap it here if necessary */
3225 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3227 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3228 SV **newsp; /* Used by POPBLOCK. */
3229 PERL_CONTEXT *cx = NULL;
3230 I32 optype; /* Used by POPEVAL. */
3234 PERL_UNUSED_VAR(newsp);
3235 PERL_UNUSED_VAR(optype);
3237 /* note that if yystatus == 3, then the EVAL CX block has already
3238 * been popped, and various vars restored */
3240 if (yystatus != 3) {
3242 op_free(PL_eval_root);
3243 PL_eval_root = NULL;
3245 SP = PL_stack_base + POPMARK; /* pop original mark */
3247 POPBLOCK(cx,PL_curpm);
3249 namesv = cx->blk_eval.old_namesv;
3253 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3255 msg = SvPVx_nolen_const(ERRSV);
3258 /* If cx is still NULL, it means that we didn't go in the
3259 * POPEVAL branch. */
3260 cx = &cxstack[cxstack_ix];
3261 assert(CxTYPE(cx) == CXt_EVAL);
3262 namesv = cx->blk_eval.old_namesv;
3264 (void)hv_store(GvHVn(PL_incgv),
3265 SvPVX_const(namesv), SvCUR(namesv),
3267 Perl_croak(aTHX_ "%sCompilation failed in require",
3268 *msg ? msg : "Unknown error\n");
3271 if (yystatus != 3) {
3272 POPBLOCK(cx,PL_curpm);
3275 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3276 (*msg ? msg : "Unknown error\n"));
3280 sv_setpvs(ERRSV, "Compilation error");
3283 PUSHs(&PL_sv_undef);
3287 CopLINE_set(&PL_compiling, 0);
3289 *startop = PL_eval_root;
3291 SAVEFREEOP(PL_eval_root);
3293 /* Set the context for this new optree.
3294 * Propagate the context from the eval(). */
3295 if ((gimme & G_WANT) == G_VOID)
3296 scalarvoid(PL_eval_root);
3297 else if ((gimme & G_WANT) == G_ARRAY)
3300 scalar(PL_eval_root);
3302 DEBUG_x(dump_eval());
3304 /* Register with debugger: */
3305 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3306 CV * const cv = get_cvs("DB::postponed", 0);
3310 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3312 call_sv(MUTABLE_SV(cv), G_DISCARD);
3316 if (PL_unitcheckav) {
3317 OP *es = PL_eval_start;
3318 call_list(PL_scopestack_ix, PL_unitcheckav);
3322 /* compiled okay, so do it */
3324 CvDEPTH(PL_compcv) = 1;
3325 SP = PL_stack_base + POPMARK; /* pop original mark */
3326 PL_op = saveop; /* The caller may need it. */
3327 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3334 S_check_type_and_open(pTHX_ const char *name)
3337 const int st_rc = PerlLIO_stat(name, &st);
3339 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3341 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3345 return PerlIO_open(name, PERL_SCRIPT_MODE);
3348 #ifndef PERL_DISABLE_PMC
3350 S_doopen_pm(pTHX_ const char *name, const STRLEN namelen)
3354 PERL_ARGS_ASSERT_DOOPEN_PM;
3356 if (namelen > 3 && memEQs(name + namelen - 3, 3, ".pm")) {
3357 SV *const pmcsv = newSV(namelen + 2);
3358 char *const pmc = SvPVX(pmcsv);
3361 memcpy(pmc, name, namelen);
3363 pmc[namelen + 1] = '\0';
3365 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
3366 fp = check_type_and_open(name);
3369 fp = check_type_and_open(pmc);
3371 SvREFCNT_dec(pmcsv);
3374 fp = check_type_and_open(name);
3379 # define doopen_pm(name, namelen) check_type_and_open(name)
3380 #endif /* !PERL_DISABLE_PMC */
3385 register PERL_CONTEXT *cx;
3392 int vms_unixname = 0;
3394 const char *tryname = NULL;
3396 const I32 gimme = GIMME_V;
3397 int filter_has_file = 0;
3398 PerlIO *tryrsfp = NULL;
3399 SV *filter_cache = NULL;
3400 SV *filter_state = NULL;
3401 SV *filter_sub = NULL;
3407 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3408 sv = sv_2mortal(new_version(sv));
3409 if (!sv_derived_from(PL_patchlevel, "version"))
3410 upg_version(PL_patchlevel, TRUE);
3411 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3412 if ( vcmp(sv,PL_patchlevel) <= 0 )
3413 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3414 SVfARG(vnormal(sv)), SVfARG(vnormal(PL_patchlevel)));
3417 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3420 SV * const req = SvRV(sv);
3421 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3423 /* get the left hand term */
3424 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3426 first = SvIV(*av_fetch(lav,0,0));
3427 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3428 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3429 || av_len(lav) > 1 /* FP with > 3 digits */
3430 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3432 DIE(aTHX_ "Perl %"SVf" required--this is only "
3433 "%"SVf", stopped", SVfARG(vnormal(req)),
3434 SVfARG(vnormal(PL_patchlevel)));
3436 else { /* probably 'use 5.10' or 'use 5.8' */
3441 second = SvIV(*av_fetch(lav,1,0));
3443 second /= second >= 600 ? 100 : 10;
3444 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3445 (int)first, (int)second);
3446 upg_version(hintsv, TRUE);
3448 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3449 "--this is only %"SVf", stopped",
3450 SVfARG(vnormal(req)),
3451 SVfARG(vnormal(sv_2mortal(hintsv))),
3452 SVfARG(vnormal(PL_patchlevel)));
3457 /* We do this only with "use", not "require" or "no". */
3458 if (PL_compcv && !(cUNOP->op_first->op_private & OPpCONST_NOVER)) {
3459 /* If we request a version >= 5.9.5, load feature.pm with the
3460 * feature bundle that corresponds to the required version. */
3461 if (vcmp(sv, sv_2mortal(upg_version(newSVnv(5.009005), FALSE))) >= 0) {
3462 SV *const importsv = vnormal(sv);
3463 *SvPVX_mutable(importsv) = ':';
3464 ENTER_with_name("load_feature");
3465 Perl_load_module(aTHX_ 0, newSVpvs("feature"), NULL, importsv, NULL);
3466 LEAVE_with_name("load_feature");
3468 /* If a version >= 5.11.0 is requested, strictures are on by default! */
3469 if (vcmp(sv, sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
3470 PL_hints |= (HINT_STRICT_REFS | HINT_STRICT_SUBS | HINT_STRICT_VARS);
3476 name = SvPV_const(sv, len);
3477 if (!(name && len > 0 && *name))
3478 DIE(aTHX_ "Null filename used");
3479 TAINT_PROPER("require");
3483 /* The key in the %ENV hash is in the syntax of file passed as the argument
3484 * usually this is in UNIX format, but sometimes in VMS format, which
3485 * can result in a module being pulled in more than once.
3486 * To prevent this, the key must be stored in UNIX format if the VMS
3487 * name can be translated to UNIX.
3489 if ((unixname = tounixspec(name, NULL)) != NULL) {
3490 unixlen = strlen(unixname);
3496 /* if not VMS or VMS name can not be translated to UNIX, pass it
3499 unixname = (char *) name;
3502 if (PL_op->op_type == OP_REQUIRE) {
3503 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3504 unixname, unixlen, 0);
3506 if (*svp != &PL_sv_undef)
3509 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3510 "Compilation failed in require", unixname);
3514 /* prepare to compile file */
3516 if (path_is_absolute(name)) {
3518 tryrsfp = doopen_pm(name, len);
3521 AV * const ar = GvAVn(PL_incgv);
3527 namesv = newSV_type(SVt_PV);
3528 for (i = 0; i <= AvFILL(ar); i++) {
3529 SV * const dirsv = *av_fetch(ar, i, TRUE);
3531 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3538 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3539 && !sv_isobject(loader))
3541 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3544 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3545 PTR2UV(SvRV(dirsv)), name);
3546 tryname = SvPVX_const(namesv);
3549 ENTER_with_name("call_INC");
3557 if (sv_isobject(loader))
3558 count = call_method("INC", G_ARRAY);
3560 count = call_sv(loader, G_ARRAY);
3570 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3571 && !isGV_with_GP(SvRV(arg))) {
3572 filter_cache = SvRV(arg);
3573 SvREFCNT_inc_simple_void_NN(filter_cache);
3580 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
3584 if (isGV_with_GP(arg)) {
3585 IO * const io = GvIO((const GV *)arg);
3590 tryrsfp = IoIFP(io);
3591 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3592 PerlIO_close(IoOFP(io));
3603 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3605 SvREFCNT_inc_simple_void_NN(filter_sub);
3608 filter_state = SP[i];
3609 SvREFCNT_inc_simple_void(filter_state);
3613 if (!tryrsfp && (filter_cache || filter_sub)) {
3614 tryrsfp = PerlIO_open(BIT_BUCKET,
3622 LEAVE_with_name("call_INC");
3624 /* Adjust file name if the hook has set an %INC entry.
3625 This needs to happen after the FREETMPS above. */
3626 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3628 tryname = SvPV_nolen_const(*svp);
3635 filter_has_file = 0;
3637 SvREFCNT_dec(filter_cache);
3638 filter_cache = NULL;
3641 SvREFCNT_dec(filter_state);
3642 filter_state = NULL;
3645 SvREFCNT_dec(filter_sub);
3650 if (!path_is_absolute(name)
3656 dir = SvPV_const(dirsv, dirlen);
3664 if ((unixdir = tounixpath(dir, NULL)) == NULL)
3666 sv_setpv(namesv, unixdir);
3667 sv_catpv(namesv, unixname);
3669 # ifdef __SYMBIAN32__
3670 if (PL_origfilename[0] &&
3671 PL_origfilename[1] == ':' &&
3672 !(dir[0] && dir[1] == ':'))
3673 Perl_sv_setpvf(aTHX_ namesv,
3678 Perl_sv_setpvf(aTHX_ namesv,
3682 /* The equivalent of
3683 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3684 but without the need to parse the format string, or
3685 call strlen on either pointer, and with the correct
3686 allocation up front. */
3688 char *tmp = SvGROW(namesv, dirlen + len + 2);
3690 memcpy(tmp, dir, dirlen);
3693 /* name came from an SV, so it will have a '\0' at the
3694 end that we can copy as part of this memcpy(). */
3695 memcpy(tmp, name, len + 1);
3697 SvCUR_set(namesv, dirlen + len + 1);
3699 /* Don't even actually have to turn SvPOK_on() as we
3700 access it directly with SvPVX() below. */
3704 TAINT_PROPER("require");
3705 tryname = SvPVX_const(namesv);
3706 tryrsfp = doopen_pm(tryname, SvCUR(namesv));
3708 if (tryname[0] == '.' && tryname[1] == '/') {
3710 while (*++tryname == '/');
3714 else if (errno == EMFILE)
3715 /* no point in trying other paths if out of handles */
3723 SAVECOPFILE_FREE(&PL_compiling);
3724 CopFILE_set(&PL_compiling, tryname);
3726 SvREFCNT_dec(namesv);
3728 if (PL_op->op_type == OP_REQUIRE) {
3729 if(errno == EMFILE) {
3730 /* diag_listed_as: Can't locate %s */
3731 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno));
3733 if (namesv) { /* did we lookup @INC? */
3734 AV * const ar = GvAVn(PL_incgv);
3736 SV *const inc = newSVpvs_flags("", SVs_TEMP);
3737 for (i = 0; i <= AvFILL(ar); i++) {
3738 sv_catpvs(inc, " ");
3739 sv_catsv(inc, *av_fetch(ar, i, TRUE));
3742 /* diag_listed_as: Can't locate %s */
3744 "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")",
3746 (memEQ(name + len - 2, ".h", 3)
3747 ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""),
3748 (memEQ(name + len - 3, ".ph", 4)
3749 ? " (did you run h2ph?)" : ""),
3754 DIE(aTHX_ "Can't locate %s", name);
3760 SETERRNO(0, SS_NORMAL);
3762 /* Assume success here to prevent recursive requirement. */
3763 /* name is never assigned to again, so len is still strlen(name) */
3764 /* Check whether a hook in @INC has already filled %INC */
3766 (void)hv_store(GvHVn(PL_incgv),
3767 unixname, unixlen, newSVpv(CopFILE(&PL_compiling),0),0);
3769 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
3771 (void)hv_store(GvHVn(PL_incgv),
3772 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
3775 ENTER_with_name("eval");
3777 lex_start(NULL, tryrsfp, 0);
3781 hv_clear(GvHV(PL_hintgv));
3783 SAVECOMPILEWARNINGS();
3784 if (PL_dowarn & G_WARN_ALL_ON)
3785 PL_compiling.cop_warnings = pWARN_ALL ;
3786 else if (PL_dowarn & G_WARN_ALL_OFF)
3787 PL_compiling.cop_warnings = pWARN_NONE ;
3789 PL_compiling.cop_warnings = pWARN_STD ;
3791 if (filter_sub || filter_cache) {
3792 /* We can use the SvPV of the filter PVIO itself as our cache, rather
3793 than hanging another SV from it. In turn, filter_add() optionally
3794 takes the SV to use as the filter (or creates a new SV if passed
3795 NULL), so simply pass in whatever value filter_cache has. */
3796 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
3797 IoLINES(datasv) = filter_has_file;
3798 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
3799 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
3802 /* switch to eval mode */
3803 PUSHBLOCK(cx, CXt_EVAL, SP);
3805 cx->blk_eval.retop = PL_op->op_next;
3807 SAVECOPLINE(&PL_compiling);
3808 CopLINE_set(&PL_compiling, 0);
3812 /* Store and reset encoding. */
3813 encoding = PL_encoding;
3816 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq))
3817 op = DOCATCH(PL_eval_start);
3819 op = PL_op->op_next;
3821 /* Restore encoding. */
3822 PL_encoding = encoding;
3827 /* This is a op added to hold the hints hash for
3828 pp_entereval. The hash can be modified by the code
3829 being eval'ed, so we return a copy instead. */
3835 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
3843 register PERL_CONTEXT *cx;
3845 const I32 gimme = GIMME_V;
3846 const U32 was = PL_breakable_sub_gen;
3847 char tbuf[TYPE_DIGITS(long) + 12];
3848 char *tmpbuf = tbuf;
3852 HV *saved_hh = NULL;
3854 if (PL_op->op_private & OPpEVAL_HAS_HH) {
3855 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
3859 /* make sure we've got a plain PV (no overload etc) before testing
3860 * for taint. Making a copy here is probably overkill, but better
3861 * safe than sorry */
3863 const char * const p = SvPV_const(sv, len);
3865 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
3868 TAINT_IF(SvTAINTED(sv));
3869 TAINT_PROPER("eval");
3871 ENTER_with_name("eval");
3872 lex_start(sv, NULL, 0);
3875 /* switch to eval mode */
3877 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3878 SV * const temp_sv = sv_newmortal();
3879 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
3880 (unsigned long)++PL_evalseq,
3881 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3882 tmpbuf = SvPVX(temp_sv);
3883 len = SvCUR(temp_sv);
3886 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
3887 SAVECOPFILE_FREE(&PL_compiling);
3888 CopFILE_set(&PL_compiling, tmpbuf+2);
3889 SAVECOPLINE(&PL_compiling);
3890 CopLINE_set(&PL_compiling, 1);
3891 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3892 deleting the eval's FILEGV from the stash before gv_check() runs
3893 (i.e. before run-time proper). To work around the coredump that
3894 ensues, we always turn GvMULTI_on for any globals that were
3895 introduced within evals. See force_ident(). GSAR 96-10-12 */
3897 PL_hints = PL_op->op_targ;
3899 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3900 SvREFCNT_dec(GvHV(PL_hintgv));
3901 GvHV(PL_hintgv) = saved_hh;
3903 SAVECOMPILEWARNINGS();
3904 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3905 cophh_free(CopHINTHASH_get(&PL_compiling));
3906 if (Perl_fetch_cop_label(aTHX_ PL_curcop, NULL, NULL)) {
3907 /* The label, if present, is the first entry on the chain. So rather
3908 than writing a blank label in front of it (which involves an
3909 allocation), just use the next entry in the chain. */
3910 PL_compiling.cop_hints_hash
3911 = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next);
3912 /* Check the assumption that this removed the label. */
3913 assert(Perl_fetch_cop_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3916 PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash);
3917 /* special case: an eval '' executed within the DB package gets lexically
3918 * placed in the first non-DB CV rather than the current CV - this
3919 * allows the debugger to execute code, find lexicals etc, in the
3920 * scope of the code being debugged. Passing &seq gets find_runcv
3921 * to do the dirty work for us */
3922 runcv = find_runcv(&seq);
3924 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3926 cx->blk_eval.retop = PL_op->op_next;
3928 /* prepare to compile string */
3930 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
3931 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
3934 if (doeval(gimme, NULL, runcv, seq)) {
3935 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3936 ? (PERLDB_LINE || PERLDB_SAVESRC)
3937 : PERLDB_SAVESRC_NOSUBS) {
3938 /* Retain the filegv we created. */
3940 char *const safestr = savepvn(tmpbuf, len);
3941 SAVEDELETE(PL_defstash, safestr, len);
3943 return DOCATCH(PL_eval_start);
3945 /* We have already left the scope set up earler thanks to the LEAVE
3947 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3948 ? (PERLDB_LINE || PERLDB_SAVESRC)
3949 : PERLDB_SAVESRC_INVALID) {
3950 /* Retain the filegv we created. */
3952 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
3954 return PL_op->op_next;
3965 register PERL_CONTEXT *cx;
3967 const U8 save_flags = PL_op -> op_flags;
3973 namesv = cx->blk_eval.old_namesv;
3974 retop = cx->blk_eval.retop;
3977 if (gimme == G_VOID)
3979 else if (gimme == G_SCALAR) {
3982 if (SvFLAGS(TOPs) & SVs_TEMP)
3985 *MARK = sv_mortalcopy(TOPs);
3989 *MARK = &PL_sv_undef;
3994 /* in case LEAVE wipes old return values */
3995 for (mark = newsp + 1; mark <= SP; mark++) {
3996 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
3997 *mark = sv_mortalcopy(*mark);
3998 TAINT_NOT; /* Each item is independent */
4002 PL_curpm = newpm; /* Don't pop $1 et al till now */
4005 assert(CvDEPTH(PL_compcv) == 1);
4007 CvDEPTH(PL_compcv) = 0;
4009 if (optype == OP_REQUIRE &&
4010 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4012 /* Unassume the success we assumed earlier. */
4013 (void)hv_delete(GvHVn(PL_incgv),
4014 SvPVX_const(namesv), SvCUR(namesv),
4016 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4018 /* die_unwind() did LEAVE, or we won't be here */
4021 LEAVE_with_name("eval");
4022 if (!(save_flags & OPf_SPECIAL)) {
4030 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4031 close to the related Perl_create_eval_scope. */
4033 Perl_delete_eval_scope(pTHX)
4038 register PERL_CONTEXT *cx;
4044 LEAVE_with_name("eval_scope");
4045 PERL_UNUSED_VAR(newsp);
4046 PERL_UNUSED_VAR(gimme);
4047 PERL_UNUSED_VAR(optype);
4050 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4051 also needed by Perl_fold_constants. */
4053 Perl_create_eval_scope(pTHX_ U32 flags)
4056 const I32 gimme = GIMME_V;
4058 ENTER_with_name("eval_scope");
4061 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4064 PL_in_eval = EVAL_INEVAL;
4065 if (flags & G_KEEPERR)
4066 PL_in_eval |= EVAL_KEEPERR;
4069 if (flags & G_FAKINGEVAL) {
4070 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4078 PERL_CONTEXT * const cx = create_eval_scope(0);
4079 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4080 return DOCATCH(PL_op->op_next);
4089 register PERL_CONTEXT *cx;
4094 PERL_UNUSED_VAR(optype);
4097 if (gimme == G_VOID)
4099 else if (gimme == G_SCALAR) {
4103 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4106 *MARK = sv_mortalcopy(TOPs);
4110 *MARK = &PL_sv_undef;
4115 /* in case LEAVE wipes old return values */
4117 for (mark = newsp + 1; mark <= SP; mark++) {
4118 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4119 *mark = sv_mortalcopy(*mark);
4120 TAINT_NOT; /* Each item is independent */
4124 PL_curpm = newpm; /* Don't pop $1 et al till now */
4126 LEAVE_with_name("eval_scope");
4134 register PERL_CONTEXT *cx;
4135 const I32 gimme = GIMME_V;
4137 ENTER_with_name("given");
4140 sv_setsv(PAD_SV(PL_op->op_targ), POPs);
4142 PUSHBLOCK(cx, CXt_GIVEN, SP);
4151 register PERL_CONTEXT *cx;
4155 PERL_UNUSED_CONTEXT;
4158 assert(CxTYPE(cx) == CXt_GIVEN);
4161 if (gimme == G_VOID)
4163 else if (gimme == G_SCALAR) {
4167 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4170 *MARK = sv_mortalcopy(TOPs);
4174 *MARK = &PL_sv_undef;
4179 /* in case LEAVE wipes old return values */
4181 for (mark = newsp + 1; mark <= SP; mark++) {
4182 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4183 *mark = sv_mortalcopy(*mark);
4184 TAINT_NOT; /* Each item is independent */
4188 PL_curpm = newpm; /* Don't pop $1 et al till now */
4190 LEAVE_with_name("given");
4194 /* Helper routines used by pp_smartmatch */
4196 S_make_matcher(pTHX_ REGEXP *re)
4199 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4201 PERL_ARGS_ASSERT_MAKE_MATCHER;
4203 PM_SETRE(matcher, ReREFCNT_inc(re));
4205 SAVEFREEOP((OP *) matcher);
4206 ENTER_with_name("matcher"); SAVETMPS;
4212 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4217 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4219 PL_op = (OP *) matcher;
4224 return (SvTRUEx(POPs));
4228 S_destroy_matcher(pTHX_ PMOP *matcher)
4232 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4233 PERL_UNUSED_ARG(matcher);
4236 LEAVE_with_name("matcher");
4239 /* Do a smart match */
4242 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4243 return do_smartmatch(NULL, NULL);
4246 /* This version of do_smartmatch() implements the
4247 * table of smart matches that is found in perlsyn.
4250 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
4255 bool object_on_left = FALSE;
4256 SV *e = TOPs; /* e is for 'expression' */
4257 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4259 /* Take care only to invoke mg_get() once for each argument.
4260 * Currently we do this by copying the SV if it's magical. */
4263 d = sv_mortalcopy(d);
4270 e = sv_mortalcopy(e);
4272 /* First of all, handle overload magic of the rightmost argument */
4275 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4276 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4278 tmpsv = amagic_call(d, e, smart_amg, 0);
4285 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4288 SP -= 2; /* Pop the values */
4293 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4300 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4301 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4302 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4304 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4305 object_on_left = TRUE;
4308 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4310 if (object_on_left) {
4311 goto sm_any_sub; /* Treat objects like scalars */
4313 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4314 /* Test sub truth for each key */
4316 bool andedresults = TRUE;
4317 HV *hv = (HV*) SvRV(d);
4318 I32 numkeys = hv_iterinit(hv);
4319 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4322 while ( (he = hv_iternext(hv)) ) {
4323 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4324 ENTER_with_name("smartmatch_hash_key_test");
4327 PUSHs(hv_iterkeysv(he));
4329 c = call_sv(e, G_SCALAR);
4332 andedresults = FALSE;
4334 andedresults = SvTRUEx(POPs) && andedresults;
4336 LEAVE_with_name("smartmatch_hash_key_test");
4343 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4344 /* Test sub truth for each element */
4346 bool andedresults = TRUE;
4347 AV *av = (AV*) SvRV(d);
4348 const I32 len = av_len(av);
4349 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4352 for (i = 0; i <= len; ++i) {
4353 SV * const * const svp = av_fetch(av, i, FALSE);
4354 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4355 ENTER_with_name("smartmatch_array_elem_test");
4361 c = call_sv(e, G_SCALAR);
4364 andedresults = FALSE;
4366 andedresults = SvTRUEx(POPs) && andedresults;
4368 LEAVE_with_name("smartmatch_array_elem_test");
4377 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4378 ENTER_with_name("smartmatch_coderef");
4383 c = call_sv(e, G_SCALAR);
4387 else if (SvTEMP(TOPs))
4388 SvREFCNT_inc_void(TOPs);
4390 LEAVE_with_name("smartmatch_coderef");
4395 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4396 if (object_on_left) {
4397 goto sm_any_hash; /* Treat objects like scalars */
4399 else if (!SvOK(d)) {
4400 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4403 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4404 /* Check that the key-sets are identical */
4406 HV *other_hv = MUTABLE_HV(SvRV(d));
4408 bool other_tied = FALSE;
4409 U32 this_key_count = 0,
4410 other_key_count = 0;
4411 HV *hv = MUTABLE_HV(SvRV(e));
4413 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
4414 /* Tied hashes don't know how many keys they have. */
4415 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
4418 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
4419 HV * const temp = other_hv;
4424 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
4427 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
4430 /* The hashes have the same number of keys, so it suffices
4431 to check that one is a subset of the other. */
4432 (void) hv_iterinit(hv);
4433 while ( (he = hv_iternext(hv)) ) {
4434 SV *key = hv_iterkeysv(he);
4436 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
4439 if(!hv_exists_ent(other_hv, key, 0)) {
4440 (void) hv_iterinit(hv); /* reset iterator */
4446 (void) hv_iterinit(other_hv);
4447 while ( hv_iternext(other_hv) )
4451 other_key_count = HvUSEDKEYS(other_hv);
4453 if (this_key_count != other_key_count)
4458 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4459 AV * const other_av = MUTABLE_AV(SvRV(d));
4460 const I32 other_len = av_len(other_av) + 1;
4462 HV *hv = MUTABLE_HV(SvRV(e));
4464 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n"));
4465 for (i = 0; i < other_len; ++i) {
4466 SV ** const svp = av_fetch(other_av, i, FALSE);
4467 DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n"));
4468 if (svp) { /* ??? When can this not happen? */
4469 if (hv_exists_ent(hv, *svp, 0))
4475 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4476 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n"));
4479 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4481 HV *hv = MUTABLE_HV(SvRV(e));
4483 (void) hv_iterinit(hv);
4484 while ( (he = hv_iternext(hv)) ) {
4485 DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n"));
4486 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
4487 (void) hv_iterinit(hv);
4488 destroy_matcher(matcher);
4492 destroy_matcher(matcher);
4498 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n"));
4499 if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0))
4506 else if (SvROK(e) && SvTY