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 = SvPOKp(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 (SvTAINTED(tmpForm))
528 SvTAINTED_on(PL_formtarget);
529 if (DO_UTF8(PL_formtarget))
531 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
533 f = SvPV_const(tmpForm, len);
534 /* need to jump to the next word */
535 fpc = (U32*)(f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN);
539 const char *name = "???";
542 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
543 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
544 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
545 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
546 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
548 case FF_CHECKNL: name = "CHECKNL"; break;
549 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
550 case FF_SPACE: name = "SPACE"; break;
551 case FF_HALFSPACE: name = "HALFSPACE"; break;
552 case FF_ITEM: name = "ITEM"; break;
553 case FF_CHOP: name = "CHOP"; break;
554 case FF_LINEGLOB: name = "LINEGLOB"; break;
555 case FF_NEWLINE: name = "NEWLINE"; break;
556 case FF_MORE: name = "MORE"; break;
557 case FF_LINEMARK: name = "LINEMARK"; break;
558 case FF_END: name = "END"; break;
559 case FF_0DECIMAL: name = "0DECIMAL"; break;
560 case FF_LINESNGL: name = "LINESNGL"; break;
563 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
565 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
576 if (targ_is_utf8 && !SvUTF8(tmpForm)) {
577 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
579 sv_catpvn_utf8_upgrade(PL_formtarget, f, arg, nsv);
580 t = SvEND(PL_formtarget);
584 if (!targ_is_utf8 && DO_UTF8(tmpForm)) {
585 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
587 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC, fudge + 1);
588 t = SvEND(PL_formtarget);
608 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
611 SvTAINTED_on(PL_formtarget);
617 const char *s = item = SvPV_const(sv, len);
620 itemsize = sv_len_utf8(sv);
621 if (itemsize != (I32)len) {
623 if (itemsize > fieldsize) {
624 itemsize = fieldsize;
625 itembytes = itemsize;
626 sv_pos_u2b(sv, &itembytes, 0);
630 send = chophere = s + itembytes;
640 sv_pos_b2u(sv, &itemsize);
644 item_is_utf8 = FALSE;
645 if (itemsize > fieldsize)
646 itemsize = fieldsize;
647 send = chophere = s + itemsize;
661 const char *s = item = SvPV_const(sv, len);
664 itemsize = sv_len_utf8(sv);
665 if (itemsize != (I32)len) {
667 if (itemsize <= fieldsize) {
668 const char *send = chophere = s + itemsize;
681 itemsize = fieldsize;
682 itembytes = itemsize;
683 sv_pos_u2b(sv, &itembytes, 0);
684 send = chophere = s + itembytes;
685 while (s < send || (s == send && isSPACE(*s))) {
695 if (strchr(PL_chopset, *s))
700 itemsize = chophere - item;
701 sv_pos_b2u(sv, &itemsize);
707 item_is_utf8 = FALSE;
708 if (itemsize <= fieldsize) {
709 const char *const send = chophere = s + itemsize;
722 itemsize = fieldsize;
723 send = chophere = s + itemsize;
724 while (s < send || (s == send && isSPACE(*s))) {
734 if (strchr(PL_chopset, *s))
739 itemsize = chophere - item;
745 arg = fieldsize - itemsize;
754 arg = fieldsize - itemsize;
765 const char *s = item;
769 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
771 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC,
773 t = SvEND(PL_formtarget);
777 if (UTF8_IS_CONTINUED(*s)) {
778 STRLEN skip = UTF8SKIP(s);
795 if ( !((*t++ = *s++) & ~31) )
801 if (targ_is_utf8 && !item_is_utf8) {
802 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
804 sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
805 for (; t < SvEND(PL_formtarget); t++) {
818 const int ch = *t++ = *s++;
821 if ( !((*t++ = *s++) & ~31) )
830 const char *s = chophere;
844 const bool oneline = fpc[-1] == FF_LINESNGL;
845 const char *s = item = SvPV_const(sv, len);
846 item_is_utf8 = DO_UTF8(sv);
849 STRLEN to_copy = itemsize;
850 const char *const send = s + len;
851 const U8 *source = (const U8 *) s;
855 chophere = s + itemsize;
859 to_copy = s - SvPVX_const(sv) - 1;
871 if (targ_is_utf8 && !item_is_utf8) {
872 source = tmp = bytes_to_utf8(source, &to_copy);
873 SvCUR_set(PL_formtarget,
874 t - SvPVX_const(PL_formtarget));
876 if (item_is_utf8 && !targ_is_utf8) {
877 /* Upgrade targ to UTF8, and then we reduce it to
878 a problem we have a simple solution for. */
879 SvCUR_set(PL_formtarget,
880 t - SvPVX_const(PL_formtarget));
882 /* Don't need get magic. */
883 sv_utf8_upgrade_nomg(PL_formtarget);
885 SvCUR_set(PL_formtarget,
886 t - SvPVX_const(PL_formtarget));
889 /* Easy. They agree. */
890 assert (item_is_utf8 == targ_is_utf8);
892 SvGROW(PL_formtarget,
893 SvCUR(PL_formtarget) + to_copy + fudge + 1);
894 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
896 Copy(source, t, to_copy, char);
898 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
900 if (SvGMAGICAL(sv)) {
901 /* Mustn't call sv_pos_b2u() as it does a second
902 mg_get(). Is this a bug? Do we need a _flags()
904 itemsize = utf8_length(source, source + itemsize);
906 sv_pos_b2u(sv, &itemsize);
918 #if defined(USE_LONG_DOUBLE)
921 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
925 "%#0*.*f" : "%0*.*f");
930 #if defined(USE_LONG_DOUBLE)
932 ((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
935 ((arg & 256) ? "%#*.*f" : "%*.*f");
938 /* If the field is marked with ^ and the value is undefined,
940 if ((arg & 512) && !SvOK(sv)) {
948 /* overflow evidence */
949 if (num_overflow(value, fieldsize, arg)) {
955 /* Formats aren't yet marked for locales, so assume "yes". */
957 STORE_NUMERIC_STANDARD_SET_LOCAL();
958 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
959 RESTORE_NUMERIC_STANDARD();
966 while (t-- > linemark && *t == ' ') ;
974 if (arg) { /* repeat until fields exhausted? */
976 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
977 lines += FmLINES(PL_formtarget);
979 SvUTF8_on(PL_formtarget);
980 FmLINES(PL_formtarget) = lines;
982 RETURNOP(cLISTOP->op_first);
993 const char *s = chophere;
994 const char *send = item + len;
996 while (isSPACE(*s) && (s < send))
1001 arg = fieldsize - itemsize;
1008 if (strnEQ(s1," ",3)) {
1009 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1020 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1022 SvUTF8_on(PL_formtarget);
1023 FmLINES(PL_formtarget) += lines;
1035 if (PL_stack_base + *PL_markstack_ptr == SP) {
1037 if (GIMME_V == G_SCALAR)
1039 RETURNOP(PL_op->op_next->op_next);
1041 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
1042 pp_pushmark(); /* push dst */
1043 pp_pushmark(); /* push src */
1044 ENTER_with_name("grep"); /* enter outer scope */
1047 if (PL_op->op_private & OPpGREP_LEX)
1048 SAVESPTR(PAD_SVl(PL_op->op_targ));
1051 ENTER_with_name("grep_item"); /* enter inner scope */
1054 src = PL_stack_base[*PL_markstack_ptr];
1056 if (PL_op->op_private & OPpGREP_LEX)
1057 PAD_SVl(PL_op->op_targ) = src;
1062 if (PL_op->op_type == OP_MAPSTART)
1063 pp_pushmark(); /* push top */
1064 return ((LOGOP*)PL_op->op_next)->op_other;
1070 const I32 gimme = GIMME_V;
1071 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1077 /* first, move source pointer to the next item in the source list */
1078 ++PL_markstack_ptr[-1];
1080 /* if there are new items, push them into the destination list */
1081 if (items && gimme != G_VOID) {
1082 /* might need to make room back there first */
1083 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1084 /* XXX this implementation is very pessimal because the stack
1085 * is repeatedly extended for every set of items. Is possible
1086 * to do this without any stack extension or copying at all
1087 * by maintaining a separate list over which the map iterates
1088 * (like foreach does). --gsar */
1090 /* everything in the stack after the destination list moves
1091 * towards the end the stack by the amount of room needed */
1092 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1094 /* items to shift up (accounting for the moved source pointer) */
1095 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1097 /* This optimization is by Ben Tilly and it does
1098 * things differently from what Sarathy (gsar)
1099 * is describing. The downside of this optimization is
1100 * that leaves "holes" (uninitialized and hopefully unused areas)
1101 * to the Perl stack, but on the other hand this
1102 * shouldn't be a problem. If Sarathy's idea gets
1103 * implemented, this optimization should become
1104 * irrelevant. --jhi */
1106 shift = count; /* Avoid shifting too often --Ben Tilly */
1110 dst = (SP += shift);
1111 PL_markstack_ptr[-1] += shift;
1112 *PL_markstack_ptr += shift;
1116 /* copy the new items down to the destination list */
1117 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1118 if (gimme == G_ARRAY) {
1119 /* add returned items to the collection (making mortal copies
1120 * if necessary), then clear the current temps stack frame
1121 * *except* for those items. We do this splicing the items
1122 * into the start of the tmps frame (so some items may be on
1123 * the tmps stack twice), then moving PL_tmps_floor above
1124 * them, then freeing the frame. That way, the only tmps that
1125 * accumulate over iterations are the return values for map.
1126 * We have to do to this way so that everything gets correctly
1127 * freed if we die during the map.
1131 /* make space for the slice */
1132 EXTEND_MORTAL(items);
1133 tmpsbase = PL_tmps_floor + 1;
1134 Move(PL_tmps_stack + tmpsbase,
1135 PL_tmps_stack + tmpsbase + items,
1136 PL_tmps_ix - PL_tmps_floor,
1138 PL_tmps_ix += items;
1143 sv = sv_mortalcopy(sv);
1145 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1147 /* clear the stack frame except for the items */
1148 PL_tmps_floor += items;
1150 /* FREETMPS may have cleared the TEMP flag on some of the items */
1153 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
1156 /* scalar context: we don't care about which values map returns
1157 * (we use undef here). And so we certainly don't want to do mortal
1158 * copies of meaningless values. */
1159 while (items-- > 0) {
1161 *dst-- = &PL_sv_undef;
1169 LEAVE_with_name("grep_item"); /* exit inner scope */
1172 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1174 (void)POPMARK; /* pop top */
1175 LEAVE_with_name("grep"); /* exit outer scope */
1176 (void)POPMARK; /* pop src */
1177 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1178 (void)POPMARK; /* pop dst */
1179 SP = PL_stack_base + POPMARK; /* pop original mark */
1180 if (gimme == G_SCALAR) {
1181 if (PL_op->op_private & OPpGREP_LEX) {
1182 SV* sv = sv_newmortal();
1183 sv_setiv(sv, items);
1191 else if (gimme == G_ARRAY)
1198 ENTER_with_name("grep_item"); /* enter inner scope */
1201 /* set $_ to the new source item */
1202 src = PL_stack_base[PL_markstack_ptr[-1]];
1204 if (PL_op->op_private & OPpGREP_LEX)
1205 PAD_SVl(PL_op->op_targ) = src;
1209 RETURNOP(cLOGOP->op_other);
1218 if (GIMME == G_ARRAY)
1220 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1221 return cLOGOP->op_other;
1231 if (GIMME == G_ARRAY) {
1232 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1236 SV * const targ = PAD_SV(PL_op->op_targ);
1239 if (PL_op->op_private & OPpFLIP_LINENUM) {
1240 if (GvIO(PL_last_in_gv)) {
1241 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1244 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1246 flip = SvIV(sv) == SvIV(GvSV(gv));
1252 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1253 if (PL_op->op_flags & OPf_SPECIAL) {
1261 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1264 sv_setpvs(TARG, "");
1270 /* This code tries to decide if "$left .. $right" should use the
1271 magical string increment, or if the range is numeric (we make
1272 an exception for .."0" [#18165]). AMS 20021031. */
1274 #define RANGE_IS_NUMERIC(left,right) ( \
1275 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1276 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1277 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1278 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1279 && (!SvOK(right) || looks_like_number(right))))
1285 if (GIMME == G_ARRAY) {
1291 if (RANGE_IS_NUMERIC(left,right)) {
1294 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1295 (SvOK(right) && SvNV(right) > IV_MAX))
1296 DIE(aTHX_ "Range iterator outside integer range");
1307 SV * const sv = sv_2mortal(newSViv(i++));
1312 SV * const final = sv_mortalcopy(right);
1314 const char * const tmps = SvPV_const(final, len);
1316 SV *sv = sv_mortalcopy(left);
1317 SvPV_force_nolen(sv);
1318 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1320 if (strEQ(SvPVX_const(sv),tmps))
1322 sv = sv_2mortal(newSVsv(sv));
1329 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1333 if (PL_op->op_private & OPpFLIP_LINENUM) {
1334 if (GvIO(PL_last_in_gv)) {
1335 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1338 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1339 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1347 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1348 sv_catpvs(targ, "E0");
1358 static const char * const context_name[] = {
1360 NULL, /* CXt_WHEN never actually needs "block" */
1361 NULL, /* CXt_BLOCK never actually needs "block" */
1362 NULL, /* CXt_GIVEN never actually needs "block" */
1363 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1364 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1365 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1366 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1374 S_dopoptolabel(pTHX_ const char *label)
1379 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1381 for (i = cxstack_ix; i >= 0; i--) {
1382 register const PERL_CONTEXT * const cx = &cxstack[i];
1383 switch (CxTYPE(cx)) {
1389 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1390 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1391 if (CxTYPE(cx) == CXt_NULL)
1394 case CXt_LOOP_LAZYIV:
1395 case CXt_LOOP_LAZYSV:
1397 case CXt_LOOP_PLAIN:
1399 const char *cx_label = CxLABEL(cx);
1400 if (!cx_label || strNE(label, cx_label) ) {
1401 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
1402 (long)i, cx_label));
1405 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
1416 Perl_dowantarray(pTHX)
1419 const I32 gimme = block_gimme();
1420 return (gimme == G_VOID) ? G_SCALAR : gimme;
1424 Perl_block_gimme(pTHX)
1427 const I32 cxix = dopoptosub(cxstack_ix);
1431 switch (cxstack[cxix].blk_gimme) {
1439 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1446 Perl_is_lvalue_sub(pTHX)
1449 const I32 cxix = dopoptosub(cxstack_ix);
1450 assert(cxix >= 0); /* We should only be called from inside subs */
1452 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1453 return CxLVAL(cxstack + cxix);
1459 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1464 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1466 for (i = startingblock; i >= 0; i--) {
1467 register const PERL_CONTEXT * const cx = &cxstk[i];
1468 switch (CxTYPE(cx)) {
1474 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
1482 S_dopoptoeval(pTHX_ I32 startingblock)
1486 for (i = startingblock; i >= 0; i--) {
1487 register const PERL_CONTEXT *cx = &cxstack[i];
1488 switch (CxTYPE(cx)) {
1492 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
1500 S_dopoptoloop(pTHX_ I32 startingblock)
1504 for (i = startingblock; i >= 0; i--) {
1505 register const PERL_CONTEXT * const cx = &cxstack[i];
1506 switch (CxTYPE(cx)) {
1512 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1513 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1514 if ((CxTYPE(cx)) == CXt_NULL)
1517 case CXt_LOOP_LAZYIV:
1518 case CXt_LOOP_LAZYSV:
1520 case CXt_LOOP_PLAIN:
1521 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
1529 S_dopoptogiven(pTHX_ I32 startingblock)
1533 for (i = startingblock; i >= 0; i--) {
1534 register const PERL_CONTEXT *cx = &cxstack[i];
1535 switch (CxTYPE(cx)) {
1539 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
1541 case CXt_LOOP_PLAIN:
1542 assert(!CxFOREACHDEF(cx));
1544 case CXt_LOOP_LAZYIV:
1545 case CXt_LOOP_LAZYSV:
1547 if (CxFOREACHDEF(cx)) {
1548 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
1557 S_dopoptowhen(pTHX_ I32 startingblock)
1561 for (i = startingblock; i >= 0; i--) {
1562 register const PERL_CONTEXT *cx = &cxstack[i];
1563 switch (CxTYPE(cx)) {
1567 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
1575 Perl_dounwind(pTHX_ I32 cxix)
1580 while (cxstack_ix > cxix) {
1582 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1583 DEBUG_CX("UNWIND"); \
1584 /* Note: we don't need to restore the base context info till the end. */
1585 switch (CxTYPE(cx)) {
1588 continue; /* not break */
1596 case CXt_LOOP_LAZYIV:
1597 case CXt_LOOP_LAZYSV:
1599 case CXt_LOOP_PLAIN:
1610 PERL_UNUSED_VAR(optype);
1614 Perl_qerror(pTHX_ SV *err)
1618 PERL_ARGS_ASSERT_QERROR;
1621 if (PL_in_eval & EVAL_KEEPERR) {
1622 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1623 SvPV_nolen_const(err));
1626 sv_catsv(ERRSV, err);
1629 sv_catsv(PL_errors, err);
1631 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
1633 ++PL_parser->error_count;
1637 Perl_die_unwind(pTHX_ SV *msv)
1640 SV *exceptsv = sv_mortalcopy(msv);
1641 U8 in_eval = PL_in_eval;
1642 PERL_ARGS_ASSERT_DIE_UNWIND;
1648 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1649 && PL_curstackinfo->si_prev)
1658 register PERL_CONTEXT *cx;
1661 JMPENV *restartjmpenv;
1664 if (cxix < cxstack_ix)
1667 POPBLOCK(cx,PL_curpm);
1668 if (CxTYPE(cx) != CXt_EVAL) {
1670 const char* message = SvPVx_const(exceptsv, msglen);
1671 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
1672 PerlIO_write(Perl_error_log, message, msglen);
1676 namesv = cx->blk_eval.old_namesv;
1677 oldcop = cx->blk_oldcop;
1678 restartjmpenv = cx->blk_eval.cur_top_env;
1679 restartop = cx->blk_eval.retop;
1681 if (gimme == G_SCALAR)
1682 *++newsp = &PL_sv_undef;
1683 PL_stack_sp = newsp;
1687 /* LEAVE could clobber PL_curcop (see save_re_context())
1688 * XXX it might be better to find a way to avoid messing with
1689 * PL_curcop in save_re_context() instead, but this is a more
1690 * minimal fix --GSAR */
1693 if (optype == OP_REQUIRE) {
1694 const char* const msg = SvPVx_nolen_const(exceptsv);
1695 (void)hv_store(GvHVn(PL_incgv),
1696 SvPVX_const(namesv), SvCUR(namesv),
1698 /* note that unlike pp_entereval, pp_require isn't
1699 * supposed to trap errors. So now that we've popped the
1700 * EVAL that pp_require pushed, and processed the error
1701 * message, rethrow the error */
1702 Perl_croak(aTHX_ "%sCompilation failed in require",
1703 *msg ? msg : "Unknown error\n");
1705 if (in_eval & EVAL_KEEPERR) {
1706 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
1707 SvPV_nolen_const(exceptsv));
1710 sv_setsv(ERRSV, exceptsv);
1712 PL_restartjmpenv = restartjmpenv;
1713 PL_restartop = restartop;
1719 write_to_stderr(exceptsv);
1726 dVAR; dSP; dPOPTOPssrl;
1727 if (SvTRUE(left) != SvTRUE(right))
1734 =for apidoc caller_cx
1736 The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1737 returned C<PERL_CONTEXT> structure can be interrogated to find all the
1738 information returned to Perl by C<caller>. Note that XSUBs don't get a
1739 stack frame, so C<caller_cx(0, NULL)> will return information for the
1740 immediately-surrounding Perl code.
1742 This function skips over the automatic calls to C<&DB::sub> made on the
1743 behalf of the debugger. If the stack frame requested was a sub called by
1744 C<DB::sub>, the return value will be the frame for the call to
1745 C<DB::sub>, since that has the correct line number/etc. for the call
1746 site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1747 frame for the sub call itself.
1752 const PERL_CONTEXT *
1753 Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
1755 register I32 cxix = dopoptosub(cxstack_ix);
1756 register const PERL_CONTEXT *cx;
1757 register const PERL_CONTEXT *ccstack = cxstack;
1758 const PERL_SI *top_si = PL_curstackinfo;
1761 /* we may be in a higher stacklevel, so dig down deeper */
1762 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1763 top_si = top_si->si_prev;
1764 ccstack = top_si->si_cxstack;
1765 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1769 /* caller() should not report the automatic calls to &DB::sub */
1770 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1771 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1775 cxix = dopoptosub_at(ccstack, cxix - 1);
1778 cx = &ccstack[cxix];
1779 if (dbcxp) *dbcxp = cx;
1781 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1782 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1783 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1784 field below is defined for any cx. */
1785 /* caller() should not report the automatic calls to &DB::sub */
1786 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1787 cx = &ccstack[dbcxix];
1797 register const PERL_CONTEXT *cx;
1798 const PERL_CONTEXT *dbcx;
1800 const char *stashname;
1806 cx = caller_cx(count, &dbcx);
1808 if (GIMME != G_ARRAY) {
1815 stashname = CopSTASHPV(cx->blk_oldcop);
1816 if (GIMME != G_ARRAY) {
1819 PUSHs(&PL_sv_undef);
1822 sv_setpv(TARG, stashname);
1831 PUSHs(&PL_sv_undef);
1833 mPUSHs(newSVpv(stashname, 0));
1834 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1835 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1838 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1839 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
1840 /* So is ccstack[dbcxix]. */
1842 SV * const sv = newSV(0);
1843 gv_efullname3(sv, cvgv, NULL);
1845 PUSHs(boolSV(CxHASARGS(cx)));
1848 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1849 PUSHs(boolSV(CxHASARGS(cx)));
1853 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1856 gimme = (I32)cx->blk_gimme;
1857 if (gimme == G_VOID)
1858 PUSHs(&PL_sv_undef);
1860 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1861 if (CxTYPE(cx) == CXt_EVAL) {
1863 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1864 PUSHs(cx->blk_eval.cur_text);
1868 else if (cx->blk_eval.old_namesv) {
1869 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1872 /* eval BLOCK (try blocks have old_namesv == 0) */
1874 PUSHs(&PL_sv_undef);
1875 PUSHs(&PL_sv_undef);
1879 PUSHs(&PL_sv_undef);
1880 PUSHs(&PL_sv_undef);
1882 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1883 && CopSTASH_eq(PL_curcop, PL_debstash))
1885 AV * const ary = cx->blk_sub.argarray;
1886 const int off = AvARRAY(ary) - AvALLOC(ary);
1889 Perl_init_dbargs(aTHX);
1891 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1892 av_extend(PL_dbargs, AvFILLp(ary) + off);
1893 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1894 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1896 /* XXX only hints propagated via op_private are currently
1897 * visible (others are not easily accessible, since they
1898 * use the global PL_hints) */
1899 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1902 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1904 if (old_warnings == pWARN_NONE ||
1905 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1906 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1907 else if (old_warnings == pWARN_ALL ||
1908 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1909 /* Get the bit mask for $warnings::Bits{all}, because
1910 * it could have been extended by warnings::register */
1912 HV * const bits = get_hv("warnings::Bits", 0);
1913 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
1914 mask = newSVsv(*bits_all);
1917 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1921 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
1925 PUSHs(cx->blk_oldcop->cop_hints_hash ?
1926 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
1935 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
1936 sv_reset(tmps, CopSTASH(PL_curcop));
1941 /* like pp_nextstate, but used instead when the debugger is active */
1946 PL_curcop = (COP*)PL_op;
1947 TAINT_NOT; /* Each statement is presumed innocent */
1948 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1953 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1954 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1957 register PERL_CONTEXT *cx;
1958 const I32 gimme = G_ARRAY;
1960 GV * const gv = PL_DBgv;
1961 register CV * const cv = GvCV(gv);
1964 DIE(aTHX_ "No DB::DB routine defined");
1966 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1967 /* don't do recursive DB::DB call */
1982 (void)(*CvXSUB(cv))(aTHX_ cv);
1989 PUSHBLOCK(cx, CXt_SUB, SP);
1991 cx->blk_sub.retop = PL_op->op_next;
1994 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1995 RETURNOP(CvSTART(cv));
2005 register PERL_CONTEXT *cx;
2006 const I32 gimme = GIMME_V;
2007 void *itervar; /* location of the iteration variable */
2008 U8 cxtype = CXt_LOOP_FOR;
2010 ENTER_with_name("loop1");
2013 if (PL_op->op_targ) { /* "my" variable */
2014 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
2015 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2016 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2017 SVs_PADSTALE, SVs_PADSTALE);
2019 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
2021 itervar = PL_comppad;
2023 itervar = &PAD_SVl(PL_op->op_targ);
2026 else { /* symbol table variable */
2027 GV * const gv = MUTABLE_GV(POPs);
2028 SV** svp = &GvSV(gv);
2029 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
2031 itervar = (void *)gv;
2034 if (PL_op->op_private & OPpITER_DEF)
2035 cxtype |= CXp_FOR_DEF;
2037 ENTER_with_name("loop2");
2039 PUSHBLOCK(cx, cxtype, SP);
2040 PUSHLOOP_FOR(cx, itervar, MARK);
2041 if (PL_op->op_flags & OPf_STACKED) {
2042 SV *maybe_ary = POPs;
2043 if (SvTYPE(maybe_ary) != SVt_PVAV) {
2045 SV * const right = maybe_ary;
2048 if (RANGE_IS_NUMERIC(sv,right)) {
2049 cx->cx_type &= ~CXTYPEMASK;
2050 cx->cx_type |= CXt_LOOP_LAZYIV;
2051 /* Make sure that no-one re-orders cop.h and breaks our
2053 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
2054 #ifdef NV_PRESERVES_UV
2055 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
2056 (SvNV(sv) > (NV)IV_MAX)))
2058 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
2059 (SvNV(right) < (NV)IV_MIN))))
2061 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
2064 ((SvUV(sv) > (UV)IV_MAX) ||
2065 (SvNV(sv) > (NV)UV_MAX)))))
2067 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
2069 ((SvNV(right) > 0) &&
2070 ((SvUV(right) > (UV)IV_MAX) ||
2071 (SvNV(right) > (NV)UV_MAX))))))
2073 DIE(aTHX_ "Range iterator outside integer range");
2074 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2075 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
2077 /* for correct -Dstv display */
2078 cx->blk_oldsp = sp - PL_stack_base;
2082 cx->cx_type &= ~CXTYPEMASK;
2083 cx->cx_type |= CXt_LOOP_LAZYSV;
2084 /* Make sure that no-one re-orders cop.h and breaks our
2086 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2087 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2088 cx->blk_loop.state_u.lazysv.end = right;
2089 SvREFCNT_inc(right);
2090 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2091 /* This will do the upgrade to SVt_PV, and warn if the value
2092 is uninitialised. */
2093 (void) SvPV_nolen_const(right);
2094 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2095 to replace !SvOK() with a pointer to "". */
2097 SvREFCNT_dec(right);
2098 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2102 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2103 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2104 SvREFCNT_inc(maybe_ary);
2105 cx->blk_loop.state_u.ary.ix =
2106 (PL_op->op_private & OPpITER_REVERSED) ?
2107 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2111 else { /* iterating over items on the stack */
2112 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2113 if (PL_op->op_private & OPpITER_REVERSED) {
2114 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2117 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2127 register PERL_CONTEXT *cx;
2128 const I32 gimme = GIMME_V;
2130 ENTER_with_name("loop1");
2132 ENTER_with_name("loop2");
2134 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2135 PUSHLOOP_PLAIN(cx, SP);
2143 register PERL_CONTEXT *cx;
2150 assert(CxTYPE_is_LOOP(cx));
2152 newsp = PL_stack_base + cx->blk_loop.resetsp;
2155 if (gimme == G_VOID)
2157 else if (gimme == G_SCALAR) {
2159 *++newsp = sv_mortalcopy(*SP);
2161 *++newsp = &PL_sv_undef;
2165 *++newsp = sv_mortalcopy(*++mark);
2166 TAINT_NOT; /* Each item is independent */
2172 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2173 PL_curpm = newpm; /* ... and pop $1 et al */
2175 LEAVE_with_name("loop2");
2176 LEAVE_with_name("loop1");
2184 register PERL_CONTEXT *cx;
2185 bool popsub2 = FALSE;
2186 bool clear_errsv = FALSE;
2195 const I32 cxix = dopoptosub(cxstack_ix);
2198 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2199 * sort block, which is a CXt_NULL
2202 PL_stack_base[1] = *PL_stack_sp;
2203 PL_stack_sp = PL_stack_base + 1;
2207 DIE(aTHX_ "Can't return outside a subroutine");
2209 if (cxix < cxstack_ix)
2212 if (CxMULTICALL(&cxstack[cxix])) {
2213 gimme = cxstack[cxix].blk_gimme;
2214 if (gimme == G_VOID)
2215 PL_stack_sp = PL_stack_base;
2216 else if (gimme == G_SCALAR) {
2217 PL_stack_base[1] = *PL_stack_sp;
2218 PL_stack_sp = PL_stack_base + 1;
2224 switch (CxTYPE(cx)) {
2227 retop = cx->blk_sub.retop;
2228 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2231 if (!(PL_in_eval & EVAL_KEEPERR))
2234 namesv = cx->blk_eval.old_namesv;
2235 retop = cx->blk_eval.retop;
2238 if (optype == OP_REQUIRE &&
2239 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2241 /* Unassume the success we assumed earlier. */
2242 (void)hv_delete(GvHVn(PL_incgv),
2243 SvPVX_const(namesv), SvCUR(namesv),
2245 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
2250 retop = cx->blk_sub.retop;
2253 DIE(aTHX_ "panic: return");
2257 if (gimme == G_SCALAR) {
2260 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2262 *++newsp = SvREFCNT_inc(*SP);
2267 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2269 *++newsp = sv_mortalcopy(sv);
2274 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
2277 *++newsp = sv_mortalcopy(*SP);
2280 *++newsp = &PL_sv_undef;
2282 else if (gimme == G_ARRAY) {
2283 while (++MARK <= SP) {
2284 *++newsp = (popsub2 && SvTEMP(*MARK))
2285 ? *MARK : sv_mortalcopy(*MARK);
2286 TAINT_NOT; /* Each item is independent */
2289 PL_stack_sp = newsp;
2292 /* Stack values are safe: */
2295 POPSUB(cx,sv); /* release CV and @_ ... */
2299 PL_curpm = newpm; /* ... and pop $1 et al */
2312 register PERL_CONTEXT *cx;
2323 if (PL_op->op_flags & OPf_SPECIAL) {
2324 cxix = dopoptoloop(cxstack_ix);
2326 DIE(aTHX_ "Can't \"last\" outside a loop block");
2329 cxix = dopoptolabel(cPVOP->op_pv);
2331 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
2333 if (cxix < cxstack_ix)
2337 cxstack_ix++; /* temporarily protect top context */
2339 switch (CxTYPE(cx)) {
2340 case CXt_LOOP_LAZYIV:
2341 case CXt_LOOP_LAZYSV:
2343 case CXt_LOOP_PLAIN:
2345 newsp = PL_stack_base + cx->blk_loop.resetsp;
2346 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2350 nextop = cx->blk_sub.retop;
2354 nextop = cx->blk_eval.retop;
2358 nextop = cx->blk_sub.retop;
2361 DIE(aTHX_ "panic: last");
2365 if (gimme == G_SCALAR) {
2367 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2368 ? *SP : sv_mortalcopy(*SP);
2370 *++newsp = &PL_sv_undef;
2372 else if (gimme == G_ARRAY) {
2373 while (++MARK <= SP) {
2374 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2375 ? *MARK : sv_mortalcopy(*MARK);
2376 TAINT_NOT; /* Each item is independent */
2384 /* Stack values are safe: */
2386 case CXt_LOOP_LAZYIV:
2387 case CXt_LOOP_PLAIN:
2388 case CXt_LOOP_LAZYSV:
2390 POPLOOP(cx); /* release loop vars ... */
2394 POPSUB(cx,sv); /* release CV and @_ ... */
2397 PL_curpm = newpm; /* ... and pop $1 et al */
2400 PERL_UNUSED_VAR(optype);
2401 PERL_UNUSED_VAR(gimme);
2409 register PERL_CONTEXT *cx;
2412 if (PL_op->op_flags & OPf_SPECIAL) {
2413 cxix = dopoptoloop(cxstack_ix);
2415 DIE(aTHX_ "Can't \"next\" outside a loop block");
2418 cxix = dopoptolabel(cPVOP->op_pv);
2420 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2422 if (cxix < cxstack_ix)
2425 /* clear off anything above the scope we're re-entering, but
2426 * save the rest until after a possible continue block */
2427 inner = PL_scopestack_ix;
2429 if (PL_scopestack_ix < inner)
2430 leave_scope(PL_scopestack[PL_scopestack_ix]);
2431 PL_curcop = cx->blk_oldcop;
2432 return (cx)->blk_loop.my_op->op_nextop;
2439 register PERL_CONTEXT *cx;
2443 if (PL_op->op_flags & OPf_SPECIAL) {
2444 cxix = dopoptoloop(cxstack_ix);
2446 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2449 cxix = dopoptolabel(cPVOP->op_pv);
2451 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2453 if (cxix < cxstack_ix)
2456 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2457 if (redo_op->op_type == OP_ENTER) {
2458 /* pop one less context to avoid $x being freed in while (my $x..) */
2460 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2461 redo_op = redo_op->op_next;
2465 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2466 LEAVE_SCOPE(oldsave);
2468 PL_curcop = cx->blk_oldcop;
2473 S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
2477 static const char too_deep[] = "Target of goto is too deeply nested";
2479 PERL_ARGS_ASSERT_DOFINDLABEL;
2482 Perl_croak(aTHX_ too_deep);
2483 if (o->op_type == OP_LEAVE ||
2484 o->op_type == OP_SCOPE ||
2485 o->op_type == OP_LEAVELOOP ||
2486 o->op_type == OP_LEAVESUB ||
2487 o->op_type == OP_LEAVETRY)
2489 *ops++ = cUNOPo->op_first;
2491 Perl_croak(aTHX_ too_deep);
2494 if (o->op_flags & OPf_KIDS) {
2496 /* First try all the kids at this level, since that's likeliest. */
2497 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2498 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2499 const char *kid_label = CopLABEL(kCOP);
2500 if (kid_label && strEQ(kid_label, label))
2504 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2505 if (kid == PL_lastgotoprobe)
2507 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2510 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2511 ops[-1]->op_type == OP_DBSTATE)
2516 if ((o = dofindlabel(kid, label, ops, oplimit)))
2529 register PERL_CONTEXT *cx;
2530 #define GOTO_DEPTH 64
2531 OP *enterops[GOTO_DEPTH];
2532 const char *label = NULL;
2533 const bool do_dump = (PL_op->op_type == OP_DUMP);
2534 static const char must_have_label[] = "goto must have label";
2536 if (PL_op->op_flags & OPf_STACKED) {
2537 SV * const sv = POPs;
2539 /* This egregious kludge implements goto &subroutine */
2540 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2542 register PERL_CONTEXT *cx;
2543 CV *cv = MUTABLE_CV(SvRV(sv));
2550 if (!CvROOT(cv) && !CvXSUB(cv)) {
2551 const GV * const gv = CvGV(cv);
2555 /* autoloaded stub? */
2556 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2558 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2559 GvNAMELEN(gv), FALSE);
2560 if (autogv && (cv = GvCV(autogv)))
2562 tmpstr = sv_newmortal();
2563 gv_efullname3(tmpstr, gv, NULL);
2564 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2566 DIE(aTHX_ "Goto undefined subroutine");
2569 /* First do some returnish stuff. */
2570 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2572 cxix = dopoptosub(cxstack_ix);
2574 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2575 if (cxix < cxstack_ix)
2579 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2580 if (CxTYPE(cx) == CXt_EVAL) {
2582 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2584 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2586 else if (CxMULTICALL(cx))
2587 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2588 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2589 /* put @_ back onto stack */
2590 AV* av = cx->blk_sub.argarray;
2592 items = AvFILLp(av) + 1;
2593 EXTEND(SP, items+1); /* @_ could have been extended. */
2594 Copy(AvARRAY(av), SP + 1, items, SV*);
2595 SvREFCNT_dec(GvAV(PL_defgv));
2596 GvAV(PL_defgv) = cx->blk_sub.savearray;
2598 /* abandon @_ if it got reified */
2603 av_extend(av, items-1);
2605 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2608 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2609 AV* const av = GvAV(PL_defgv);
2610 items = AvFILLp(av) + 1;
2611 EXTEND(SP, items+1); /* @_ could have been extended. */
2612 Copy(AvARRAY(av), SP + 1, items, SV*);
2616 if (CxTYPE(cx) == CXt_SUB &&
2617 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2618 SvREFCNT_dec(cx->blk_sub.cv);
2619 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2620 LEAVE_SCOPE(oldsave);
2622 /* Now do some callish stuff. */
2624 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2626 OP* const retop = cx->blk_sub.retop;
2631 for (index=0; index<items; index++)
2632 sv_2mortal(SP[-index]);
2635 /* XS subs don't have a CxSUB, so pop it */
2636 POPBLOCK(cx, PL_curpm);
2637 /* Push a mark for the start of arglist */
2640 (void)(*CvXSUB(cv))(aTHX_ cv);
2645 AV* const padlist = CvPADLIST(cv);
2646 if (CxTYPE(cx) == CXt_EVAL) {
2647 PL_in_eval = CxOLD_IN_EVAL(cx);
2648 PL_eval_root = cx->blk_eval.old_eval_root;
2649 cx->cx_type = CXt_SUB;
2651 cx->blk_sub.cv = cv;
2652 cx->blk_sub.olddepth = CvDEPTH(cv);
2655 if (CvDEPTH(cv) < 2)
2656 SvREFCNT_inc_simple_void_NN(cv);
2658 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2659 sub_crush_depth(cv);
2660 pad_push(padlist, CvDEPTH(cv));
2663 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2666 AV *const av = MUTABLE_AV(PAD_SVl(0));
2668 cx->blk_sub.savearray = GvAV(PL_defgv);
2669 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
2670 CX_CURPAD_SAVE(cx->blk_sub);
2671 cx->blk_sub.argarray = av;
2673 if (items >= AvMAX(av) + 1) {
2674 SV **ary = AvALLOC(av);
2675 if (AvARRAY(av) != ary) {
2676 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2679 if (items >= AvMAX(av) + 1) {
2680 AvMAX(av) = items - 1;
2681 Renew(ary,items+1,SV*);
2687 Copy(mark,AvARRAY(av),items,SV*);
2688 AvFILLp(av) = items - 1;
2689 assert(!AvREAL(av));
2691 /* transfer 'ownership' of refcnts to new @_ */
2701 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2702 Perl_get_db_sub(aTHX_ NULL, cv);
2704 CV * const gotocv = get_cvs("DB::goto", 0);
2706 PUSHMARK( PL_stack_sp );
2707 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
2712 RETURNOP(CvSTART(cv));
2716 label = SvPV_nolen_const(sv);
2717 if (!(do_dump || *label))
2718 DIE(aTHX_ must_have_label);
2721 else if (PL_op->op_flags & OPf_SPECIAL) {
2723 DIE(aTHX_ must_have_label);
2726 label = cPVOP->op_pv;
2730 if (label && *label) {
2731 OP *gotoprobe = NULL;
2732 bool leaving_eval = FALSE;
2733 bool in_block = FALSE;
2734 PERL_CONTEXT *last_eval_cx = NULL;
2738 PL_lastgotoprobe = NULL;
2740 for (ix = cxstack_ix; ix >= 0; ix--) {
2742 switch (CxTYPE(cx)) {
2744 leaving_eval = TRUE;
2745 if (!CxTRYBLOCK(cx)) {
2746 gotoprobe = (last_eval_cx ?
2747 last_eval_cx->blk_eval.old_eval_root :
2752 /* else fall through */
2753 case CXt_LOOP_LAZYIV:
2754 case CXt_LOOP_LAZYSV:
2756 case CXt_LOOP_PLAIN:
2759 gotoprobe = cx->blk_oldcop->op_sibling;
2765 gotoprobe = cx->blk_oldcop->op_sibling;
2768 gotoprobe = PL_main_root;
2771 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
2772 gotoprobe = CvROOT(cx->blk_sub.cv);
2778 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2781 DIE(aTHX_ "panic: goto");
2782 gotoprobe = PL_main_root;
2786 retop = dofindlabel(gotoprobe, label,
2787 enterops, enterops + GOTO_DEPTH);
2790 if (gotoprobe->op_sibling &&
2791 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
2792 gotoprobe->op_sibling->op_sibling) {
2793 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
2794 label, enterops, enterops + GOTO_DEPTH);
2799 PL_lastgotoprobe = gotoprobe;
2802 DIE(aTHX_ "Can't find label %s", label);
2804 /* if we're leaving an eval, check before we pop any frames
2805 that we're not going to punt, otherwise the error
2808 if (leaving_eval && *enterops && enterops[1]) {
2810 for (i = 1; enterops[i]; i++)
2811 if (enterops[i]->op_type == OP_ENTERITER)
2812 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2815 if (*enterops && enterops[1]) {
2816 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2818 deprecate("\"goto\" to jump into a construct");
2821 /* pop unwanted frames */
2823 if (ix < cxstack_ix) {
2830 oldsave = PL_scopestack[PL_scopestack_ix];
2831 LEAVE_SCOPE(oldsave);
2834 /* push wanted frames */
2836 if (*enterops && enterops[1]) {
2837 OP * const oldop = PL_op;
2838 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2839 for (; enterops[ix]; ix++) {
2840 PL_op = enterops[ix];
2841 /* Eventually we may want to stack the needed arguments
2842 * for each op. For now, we punt on the hard ones. */
2843 if (PL_op->op_type == OP_ENTERITER)
2844 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2845 PL_op->op_ppaddr(aTHX);
2853 if (!retop) retop = PL_main_start;
2855 PL_restartop = retop;
2856 PL_do_undump = TRUE;
2860 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2861 PL_do_undump = FALSE;
2878 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2880 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
2883 PL_exit_flags |= PERL_EXIT_EXPECTED;
2885 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2886 if (anum || !(PL_minus_c && PL_madskills))
2891 PUSHs(&PL_sv_undef);
2898 S_save_lines(pTHX_ AV *array, SV *sv)
2900 const char *s = SvPVX_const(sv);
2901 const char * const send = SvPVX_const(sv) + SvCUR(sv);
2904 PERL_ARGS_ASSERT_SAVE_LINES;
2906 while (s && s < send) {
2908 SV * const tmpstr = newSV_type(SVt_PVMG);
2910 t = (const char *)memchr(s, '\n', send - s);
2916 sv_setpvn(tmpstr, s, t - s);
2917 av_store(array, line++, tmpstr);
2925 Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
2927 0 is used as continue inside eval,
2929 3 is used for a die caught by an inner eval - continue inner loop
2931 See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
2932 establish a local jmpenv to handle exception traps.
2937 S_docatch(pTHX_ OP *o)
2941 OP * const oldop = PL_op;
2945 assert(CATCH_GET == TRUE);
2952 assert(cxstack_ix >= 0);
2953 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2954 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
2959 /* die caught by an inner eval - continue inner loop */
2960 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
2961 PL_restartjmpenv = NULL;
2962 PL_op = PL_restartop;
2978 /* James Bond: Do you expect me to talk?
2979 Auric Goldfinger: No, Mr. Bond. I expect you to die.
2981 This code is an ugly hack, doesn't work with lexicals in subroutines that are
2982 called more than once, and is only used by regcomp.c, for (?{}) blocks.
2984 Currently it is not used outside the core code. Best if it stays that way.
2986 Hence it's now deprecated, and will be removed.
2989 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
2990 /* sv Text to convert to OP tree. */
2991 /* startop op_free() this to undo. */
2992 /* code Short string id of the caller. */
2994 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
2995 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
2998 /* Don't use this. It will go away without warning once the regexp engine is
2999 refactored not to use it. */
3001 Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3004 dVAR; dSP; /* Make POPBLOCK work. */
3010 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3011 char *tmpbuf = tbuf;
3014 CV* runcv = NULL; /* initialise to avoid compiler warnings */
3018 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
3020 ENTER_with_name("eval");
3021 lex_start(sv, NULL, 0);
3023 /* switch to eval mode */
3025 if (IN_PERL_COMPILETIME) {
3026 SAVECOPSTASH_FREE(&PL_compiling);
3027 CopSTASH_set(&PL_compiling, PL_curstash);
3029 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3030 SV * const sv = sv_newmortal();
3031 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3032 code, (unsigned long)++PL_evalseq,
3033 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3038 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3039 (unsigned long)++PL_evalseq);
3040 SAVECOPFILE_FREE(&PL_compiling);
3041 CopFILE_set(&PL_compiling, tmpbuf+2);
3042 SAVECOPLINE(&PL_compiling);
3043 CopLINE_set(&PL_compiling, 1);
3044 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3045 deleting the eval's FILEGV from the stash before gv_check() runs
3046 (i.e. before run-time proper). To work around the coredump that
3047 ensues, we always turn GvMULTI_on for any globals that were
3048 introduced within evals. See force_ident(). GSAR 96-10-12 */
3049 safestr = savepvn(tmpbuf, len);
3050 SAVEDELETE(PL_defstash, safestr, len);
3052 #ifdef OP_IN_REGISTER
3058 /* we get here either during compilation, or via pp_regcomp at runtime */
3059 runtime = IN_PERL_RUNTIME;
3061 runcv = find_runcv(NULL);
3064 PL_op->op_type = OP_ENTEREVAL;
3065 PL_op->op_flags = 0; /* Avoid uninit warning. */
3066 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
3068 need_catch = CATCH_GET;
3072 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
3074 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
3075 CATCH_SET(need_catch);
3076 POPBLOCK(cx,PL_curpm);
3079 (*startop)->op_type = OP_NULL;
3080 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
3081 /* XXX DAPM do this properly one year */
3082 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
3083 LEAVE_with_name("eval");
3084 if (IN_PERL_COMPILETIME)
3085 CopHINTS_set(&PL_compiling, PL_hints);
3086 #ifdef OP_IN_REGISTER
3089 PERL_UNUSED_VAR(newsp);
3090 PERL_UNUSED_VAR(optype);
3092 return PL_eval_start;
3097 =for apidoc find_runcv
3099 Locate the CV corresponding to the currently executing sub or eval.
3100 If db_seqp is non_null, skip CVs that are in the DB package and populate
3101 *db_seqp with the cop sequence number at the point that the DB:: code was
3102 entered. (allows debuggers to eval in the scope of the breakpoint rather
3103 than in the scope of the debugger itself).
3109 Perl_find_runcv(pTHX_ U32 *db_seqp)
3115 *db_seqp = PL_curcop->cop_seq;
3116 for (si = PL_curstackinfo; si; si = si->si_prev) {
3118 for (ix = si->si_cxix; ix >= 0; ix--) {
3119 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3120 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3121 CV * const cv = cx->blk_sub.cv;
3122 /* skip DB:: code */
3123 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3124 *db_seqp = cx->blk_oldcop->cop_seq;
3129 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3137 /* Run yyparse() in a setjmp wrapper. Returns:
3138 * 0: yyparse() successful
3139 * 1: yyparse() failed
3143 S_try_yyparse(pTHX_ int gramtype)
3148 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3152 ret = yyparse(gramtype) ? 1 : 0;
3166 /* Compile a require/do, an eval '', or a /(?{...})/.
3167 * In the last case, startop is non-null, and contains the address of
3168 * a pointer that should be set to the just-compiled code.
3169 * outside is the lexically enclosing CV (if any) that invoked us.
3170 * Returns a bool indicating whether the compile was successful; if so,
3171 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3172 * pushes undef (also croaks if startop != NULL).
3176 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
3179 OP * const saveop = PL_op;
3180 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3183 PL_in_eval = (in_require
3184 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3189 SAVESPTR(PL_compcv);
3190 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3191 CvEVAL_on(PL_compcv);
3192 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3193 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3195 CvOUTSIDE_SEQ(PL_compcv) = seq;
3196 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3198 /* set up a scratch pad */
3200 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
3201 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3205 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
3207 /* make sure we compile in the right package */
3209 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3210 SAVESPTR(PL_curstash);
3211 PL_curstash = CopSTASH(PL_curcop);
3213 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3214 SAVESPTR(PL_beginav);
3215 PL_beginav = newAV();
3216 SAVEFREESV(PL_beginav);
3217 SAVESPTR(PL_unitcheckav);
3218 PL_unitcheckav = newAV();
3219 SAVEFREESV(PL_unitcheckav);
3222 SAVEBOOL(PL_madskills);
3226 /* try to compile it */
3228 PL_eval_root = NULL;
3229 PL_curcop = &PL_compiling;
3230 CopARYBASE_set(PL_curcop, 0);
3231 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3232 PL_in_eval |= EVAL_KEEPERR;
3236 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3238 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3239 * so honour CATCH_GET and trap it here if necessary */
3241 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3243 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3244 SV **newsp; /* Used by POPBLOCK. */
3245 PERL_CONTEXT *cx = NULL;
3246 I32 optype; /* Used by POPEVAL. */
3250 PERL_UNUSED_VAR(newsp);
3251 PERL_UNUSED_VAR(optype);
3253 /* note that if yystatus == 3, then the EVAL CX block has already
3254 * been popped, and various vars restored */
3256 if (yystatus != 3) {
3258 op_free(PL_eval_root);
3259 PL_eval_root = NULL;
3261 SP = PL_stack_base + POPMARK; /* pop original mark */
3263 POPBLOCK(cx,PL_curpm);
3265 namesv = cx->blk_eval.old_namesv;
3269 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3271 msg = SvPVx_nolen_const(ERRSV);
3274 /* If cx is still NULL, it means that we didn't go in the
3275 * POPEVAL branch. */
3276 cx = &cxstack[cxstack_ix];
3277 assert(CxTYPE(cx) == CXt_EVAL);
3278 namesv = cx->blk_eval.old_namesv;
3280 (void)hv_store(GvHVn(PL_incgv),
3281 SvPVX_const(namesv), SvCUR(namesv),
3283 Perl_croak(aTHX_ "%sCompilation failed in require",
3284 *msg ? msg : "Unknown error\n");
3287 if (yystatus != 3) {
3288 POPBLOCK(cx,PL_curpm);
3291 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3292 (*msg ? msg : "Unknown error\n"));
3296 sv_setpvs(ERRSV, "Compilation error");
3299 PUSHs(&PL_sv_undef);
3303 CopLINE_set(&PL_compiling, 0);
3305 *startop = PL_eval_root;
3307 SAVEFREEOP(PL_eval_root);
3309 /* Set the context for this new optree.
3310 * Propagate the context from the eval(). */
3311 if ((gimme & G_WANT) == G_VOID)
3312 scalarvoid(PL_eval_root);
3313 else if ((gimme & G_WANT) == G_ARRAY)
3316 scalar(PL_eval_root);
3318 DEBUG_x(dump_eval());
3320 /* Register with debugger: */
3321 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3322 CV * const cv = get_cvs("DB::postponed", 0);
3326 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3328 call_sv(MUTABLE_SV(cv), G_DISCARD);
3332 if (PL_unitcheckav) {
3333 OP *es = PL_eval_start;
3334 call_list(PL_scopestack_ix, PL_unitcheckav);
3338 /* compiled okay, so do it */
3340 CvDEPTH(PL_compcv) = 1;
3341 SP = PL_stack_base + POPMARK; /* pop original mark */
3342 PL_op = saveop; /* The caller may need it. */
3343 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3350 S_check_type_and_open(pTHX_ SV *name)
3353 const char *p = SvPV_nolen_const(name);
3354 const int st_rc = PerlLIO_stat(p, &st);
3356 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3358 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3362 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3363 return PerlIO_openn(aTHX_ NULL, PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3365 return PerlIO_open(p, PERL_SCRIPT_MODE);
3369 #ifndef PERL_DISABLE_PMC
3371 S_doopen_pm(pTHX_ SV *name)
3374 const char *p = SvPV_const(name, namelen);
3376 PERL_ARGS_ASSERT_DOOPEN_PM;
3378 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3379 SV *const pmcsv = sv_mortalcopy(name);
3382 sv_catpvn(pmcsv, "c", 1);
3384 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3385 return check_type_and_open(pmcsv);
3387 return check_type_and_open(name);
3390 # define doopen_pm(name) check_type_and_open(name)
3391 #endif /* !PERL_DISABLE_PMC */
3396 register PERL_CONTEXT *cx;
3403 int vms_unixname = 0;
3405 const char *tryname = NULL;
3407 const I32 gimme = GIMME_V;
3408 int filter_has_file = 0;
3409 PerlIO *tryrsfp = NULL;
3410 SV *filter_cache = NULL;
3411 SV *filter_state = NULL;
3412 SV *filter_sub = NULL;
3418 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3419 sv = sv_2mortal(new_version(sv));
3420 if (!sv_derived_from(PL_patchlevel, "version"))
3421 upg_version(PL_patchlevel, TRUE);
3422 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3423 if ( vcmp(sv,PL_patchlevel) <= 0 )
3424 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3425 SVfARG(sv_2mortal(vnormal(sv))),
3426 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3430 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3433 SV * const req = SvRV(sv);
3434 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3436 /* get the left hand term */
3437 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3439 first = SvIV(*av_fetch(lav,0,0));
3440 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3441 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3442 || av_len(lav) > 1 /* FP with > 3 digits */
3443 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3445 DIE(aTHX_ "Perl %"SVf" required--this is only "
3447 SVfARG(sv_2mortal(vnormal(req))),
3448 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3451 else { /* probably 'use 5.10' or 'use 5.8' */
3456 second = SvIV(*av_fetch(lav,1,0));
3458 second /= second >= 600 ? 100 : 10;
3459 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3460 (int)first, (int)second);
3461 upg_version(hintsv, TRUE);
3463 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3464 "--this is only %"SVf", stopped",
3465 SVfARG(sv_2mortal(vnormal(req))),
3466 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3467 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3475 name = SvPV_const(sv, len);
3476 if (!(name && len > 0 && *name))
3477 DIE(aTHX_ "Null filename used");
3478 TAINT_PROPER("require");
3482 /* The key in the %ENV hash is in the syntax of file passed as the argument
3483 * usually this is in UNIX format, but sometimes in VMS format, which
3484 * can result in a module being pulled in more than once.
3485 * To prevent this, the key must be stored in UNIX format if the VMS
3486 * name can be translated to UNIX.
3488 if ((unixname = tounixspec(name, NULL)) != NULL) {
3489 unixlen = strlen(unixname);
3495 /* if not VMS or VMS name can not be translated to UNIX, pass it
3498 unixname = (char *) name;
3501 if (PL_op->op_type == OP_REQUIRE) {
3502 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3503 unixname, unixlen, 0);
3505 if (*svp != &PL_sv_undef)
3508 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3509 "Compilation failed in require", unixname);
3513 /* prepare to compile file */
3515 if (path_is_absolute(name)) {
3516 /* At this point, name is SvPVX(sv) */
3518 tryrsfp = doopen_pm(sv);
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);
3702 TAINT_PROPER("require");
3703 tryname = SvPVX_const(namesv);
3704 tryrsfp = doopen_pm(namesv);
3706 if (tryname[0] == '.' && tryname[1] == '/') {
3708 while (*++tryname == '/');
3712 else if (errno == EMFILE)
3713 /* no point in trying other paths if out of handles */
3721 SAVECOPFILE_FREE(&PL_compiling);
3722 CopFILE_set(&PL_compiling, tryname);
3724 SvREFCNT_dec(namesv);
3726 if (PL_op->op_type == OP_REQUIRE) {
3727 if(errno == EMFILE) {
3728 /* diag_listed_as: Can't locate %s */
3729 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno));
3731 if (namesv) { /* did we lookup @INC? */
3732 AV * const ar = GvAVn(PL_incgv);
3734 SV *const inc = newSVpvs_flags("", SVs_TEMP);
3735 for (i = 0; i <= AvFILL(ar); i++) {
3736 sv_catpvs(inc, " ");
3737 sv_catsv(inc, *av_fetch(ar, i, TRUE));
3740 /* diag_listed_as: Can't locate %s */
3742 "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")",
3744 (memEQ(name + len - 2, ".h", 3)
3745 ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""),
3746 (memEQ(name + len - 3, ".ph", 4)
3747 ? " (did you run h2ph?)" : ""),
3752 DIE(aTHX_ "Can't locate %s", name);
3758 SETERRNO(0, SS_NORMAL);
3760 /* Assume success here to prevent recursive requirement. */
3761 /* name is never assigned to again, so len is still strlen(name) */
3762 /* Check whether a hook in @INC has already filled %INC */
3764 (void)hv_store(GvHVn(PL_incgv),
3765 unixname, unixlen, newSVpv(CopFILE(&PL_compiling),0),0);
3767 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
3769 (void)hv_store(GvHVn(PL_incgv),
3770 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
3773 ENTER_with_name("eval");
3775 lex_start(NULL, tryrsfp, 0);
3779 hv_clear(GvHV(PL_hintgv));
3781 SAVECOMPILEWARNINGS();
3782 if (PL_dowarn & G_WARN_ALL_ON)
3783 PL_compiling.cop_warnings = pWARN_ALL ;
3784 else if (PL_dowarn & G_WARN_ALL_OFF)
3785 PL_compiling.cop_warnings = pWARN_NONE ;
3787 PL_compiling.cop_warnings = pWARN_STD ;
3789 if (filter_sub || filter_cache) {
3790 /* We can use the SvPV of the filter PVIO itself as our cache, rather
3791 than hanging another SV from it. In turn, filter_add() optionally
3792 takes the SV to use as the filter (or creates a new SV if passed
3793 NULL), so simply pass in whatever value filter_cache has. */
3794 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
3795 IoLINES(datasv) = filter_has_file;
3796 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
3797 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
3800 /* switch to eval mode */
3801 PUSHBLOCK(cx, CXt_EVAL, SP);
3803 cx->blk_eval.retop = PL_op->op_next;
3805 SAVECOPLINE(&PL_compiling);
3806 CopLINE_set(&PL_compiling, 0);
3810 /* Store and reset encoding. */
3811 encoding = PL_encoding;
3814 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq))
3815 op = DOCATCH(PL_eval_start);
3817 op = PL_op->op_next;
3819 /* Restore encoding. */
3820 PL_encoding = encoding;
3825 /* This is a op added to hold the hints hash for
3826 pp_entereval. The hash can be modified by the code
3827 being eval'ed, so we return a copy instead. */
3833 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
3841 register PERL_CONTEXT *cx;
3843 const I32 gimme = GIMME_V;
3844 const U32 was = PL_breakable_sub_gen;
3845 char tbuf[TYPE_DIGITS(long) + 12];
3846 bool saved_delete = FALSE;
3847 char *tmpbuf = tbuf;
3851 HV *saved_hh = NULL;
3853 if (PL_op->op_private & OPpEVAL_HAS_HH) {
3854 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
3858 /* make sure we've got a plain PV (no overload etc) before testing
3859 * for taint. Making a copy here is probably overkill, but better
3860 * safe than sorry */
3862 const char * const p = SvPV_const(sv, len);
3864 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
3867 TAINT_IF(SvTAINTED(sv));
3868 TAINT_PROPER("eval");
3870 ENTER_with_name("eval");
3871 lex_start(sv, NULL, 0);
3874 /* switch to eval mode */
3876 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3877 SV * const temp_sv = sv_newmortal();
3878 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
3879 (unsigned long)++PL_evalseq,
3880 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3881 tmpbuf = SvPVX(temp_sv);
3882 len = SvCUR(temp_sv);
3885 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
3886 SAVECOPFILE_FREE(&PL_compiling);
3887 CopFILE_set(&PL_compiling, tmpbuf+2);
3888 SAVECOPLINE(&PL_compiling);
3889 CopLINE_set(&PL_compiling, 1);
3890 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3891 deleting the eval's FILEGV from the stash before gv_check() runs
3892 (i.e. before run-time proper). To work around the coredump that
3893 ensues, we always turn GvMULTI_on for any globals that were
3894 introduced within evals. See force_ident(). GSAR 96-10-12 */
3896 PL_hints = PL_op->op_targ;
3898 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3899 SvREFCNT_dec(GvHV(PL_hintgv));
3900 GvHV(PL_hintgv) = saved_hh;
3902 SAVECOMPILEWARNINGS();
3903 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3904 cophh_free(CopHINTHASH_get(&PL_compiling));
3905 if (Perl_fetch_cop_label(aTHX_ PL_curcop, NULL, NULL)) {
3906 /* The label, if present, is the first entry on the chain. So rather
3907 than writing a blank label in front of it (which involves an
3908 allocation), just use the next entry in the chain. */
3909 PL_compiling.cop_hints_hash
3910 = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next);
3911 /* Check the assumption that this removed the label. */
3912 assert(Perl_fetch_cop_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3915 PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash);
3916 /* special case: an eval '' executed within the DB package gets lexically
3917 * placed in the first non-DB CV rather than the current CV - this
3918 * allows the debugger to execute code, find lexicals etc, in the
3919 * scope of the code being debugged. Passing &seq gets find_runcv
3920 * to do the dirty work for us */
3921 runcv = find_runcv(&seq);
3923 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3925 cx->blk_eval.retop = PL_op->op_next;
3927 /* prepare to compile string */
3929 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
3930 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
3932 char *const safestr = savepvn(tmpbuf, len);
3933 SAVEDELETE(PL_defstash, safestr, len);
3934 saved_delete = TRUE;
3939 if (doeval(gimme, NULL, runcv, seq)) {
3940 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3941 ? (PERLDB_LINE || PERLDB_SAVESRC)
3942 : PERLDB_SAVESRC_NOSUBS) {
3943 /* Retain the filegv we created. */
3944 } else if (!saved_delete) {
3945 char *const safestr = savepvn(tmpbuf, len);
3946 SAVEDELETE(PL_defstash, safestr, len);
3948 return DOCATCH(PL_eval_start);
3950 /* We have already left the scope set up earler thanks to the LEAVE
3952 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3953 ? (PERLDB_LINE || PERLDB_SAVESRC)
3954 : PERLDB_SAVESRC_INVALID) {
3955 /* Retain the filegv we created. */
3957 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
3959 return PL_op->op_next;
3970 register PERL_CONTEXT *cx;
3972 const U8 save_flags = PL_op -> op_flags;
3978 namesv = cx->blk_eval.old_namesv;
3979 retop = cx->blk_eval.retop;
3982 if (gimme == G_VOID)
3984 else if (gimme == G_SCALAR) {
3987 if (SvFLAGS(TOPs) & SVs_TEMP)
3990 *MARK = sv_mortalcopy(TOPs);
3994 *MARK = &PL_sv_undef;
3999 /* in case LEAVE wipes old return values */
4000 for (mark = newsp + 1; mark <= SP; mark++) {
4001 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
4002 *mark = sv_mortalcopy(*mark);
4003 TAINT_NOT; /* Each item is independent */
4007 PL_curpm = newpm; /* Don't pop $1 et al till now */
4010 assert(CvDEPTH(PL_compcv) == 1);
4012 CvDEPTH(PL_compcv) = 0;
4014 if (optype == OP_REQUIRE &&
4015 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4017 /* Unassume the success we assumed earlier. */
4018 (void)hv_delete(GvHVn(PL_incgv),
4019 SvPVX_const(namesv), SvCUR(namesv),
4021 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4023 /* die_unwind() did LEAVE, or we won't be here */
4026 LEAVE_with_name("eval");
4027 if (!(save_flags & OPf_SPECIAL)) {
4035 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4036 close to the related Perl_create_eval_scope. */
4038 Perl_delete_eval_scope(pTHX)
4043 register PERL_CONTEXT *cx;
4049 LEAVE_with_name("eval_scope");
4050 PERL_UNUSED_VAR(newsp);
4051 PERL_UNUSED_VAR(gimme);
4052 PERL_UNUSED_VAR(optype);
4055 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4056 also needed by Perl_fold_constants. */
4058 Perl_create_eval_scope(pTHX_ U32 flags)
4061 const I32 gimme = GIMME_V;
4063 ENTER_with_name("eval_scope");
4066 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4069 PL_in_eval = EVAL_INEVAL;
4070 if (flags & G_KEEPERR)
4071 PL_in_eval |= EVAL_KEEPERR;
4074 if (flags & G_FAKINGEVAL) {
4075 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4083 PERL_CONTEXT * const cx = create_eval_scope(0);
4084 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4085 return DOCATCH(PL_op->op_next);
4094 register PERL_CONTEXT *cx;
4099 PERL_UNUSED_VAR(optype);
4102 if (gimme == G_VOID)
4104 else if (gimme == G_SCALAR) {
4108 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4111 *MARK = sv_mortalcopy(TOPs);
4115 *MARK = &PL_sv_undef;
4120 /* in case LEAVE wipes old return values */
4122 for (mark = newsp + 1; mark <= SP; mark++) {
4123 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4124 *mark = sv_mortalcopy(*mark);
4125 TAINT_NOT; /* Each item is independent */
4129 PL_curpm = newpm; /* Don't pop $1 et al till now */
4131 LEAVE_with_name("eval_scope");
4139 register PERL_CONTEXT *cx;
4140 const I32 gimme = GIMME_V;
4142 ENTER_with_name("given");
4145 sv_setsv(PAD_SV(PL_op->op_targ), POPs);
4147 PUSHBLOCK(cx, CXt_GIVEN, SP);
4156 register PERL_CONTEXT *cx;
4160 PERL_UNUSED_CONTEXT;
4163 assert(CxTYPE(cx) == CXt_GIVEN);
4166 if (gimme == G_VOID)
4168 else if (gimme == G_SCALAR) {
4172 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4175 *MARK = sv_mortalcopy(TOPs);
4179 *MARK = &PL_sv_undef;
4184 /* in case LEAVE wipes old return values */
4186 for (mark = newsp + 1; mark <= SP; mark++) {
4187 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4188 *mark = sv_mortalcopy(*mark);
4189 TAINT_NOT; /* Each item is independent */
4193 PL_curpm = newpm; /* Don't pop $1 et al till now */
4195 LEAVE_with_name("given");
4199 /* Helper routines used by pp_smartmatch */
4201 S_make_matcher(pTHX_ REGEXP *re)
4204 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4206 PERL_ARGS_ASSERT_MAKE_MATCHER;
4208 PM_SETRE(matcher, ReREFCNT_inc(re));
4210 SAVEFREEOP((OP *) matcher);
4211 ENTER_with_name("matcher"); SAVETMPS;
4217 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4222 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4224 PL_op = (OP *) matcher;
4229 return (SvTRUEx(POPs));
4233 S_destroy_matcher(pTHX_ PMOP *matcher)
4237 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4238 PERL_UNUSED_ARG(matcher);
4241 LEAVE_with_name("matcher");
4244 /* Do a smart match */
4247 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4248 return do_smartmatch(NULL, NULL);
4251 /* This version of do_smartmatch() implements the
4252 * table of smart matches that is found in perlsyn.
4255 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
4260 bool object_on_left = FALSE;
4261 SV *e = TOPs; /* e is for 'expression' */
4262 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4264 /* Take care only to invoke mg_get() once for each argument.
4265 * Currently we do this by copying the SV if it's magical. */
4268 d = sv_mortalcopy(d);
4275 e = sv_mortalcopy(e);
4277 /* First of all, handle overload magic of the rightmost argument */
4280 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4281 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4283 tmpsv = amagic_call(d, e, smart_amg, 0);
4290 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4293 SP -= 2; /* Pop the values */
4298 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4305 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4306 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4307 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4309 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4310 object_on_left = TRUE;
4313 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4315 if (object_on_left) {
4316 goto sm_any_sub; /* Treat objects like scalars */
4318 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4319 /* Test sub truth for each key */
4321 bool andedresults = TRUE;
4322 HV *hv = (HV*) SvRV(d);
4323 I32 numkeys = hv_iterinit(hv);
4324 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4327 while ( (he = hv_iternext(hv)) ) {
4328 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4329 ENTER_with_name("smartmatch_hash_key_test");
4332 PUSHs(hv_iterkeysv(he));
4334 c = call_sv(e, G_SCALAR);
4337 andedresults = FALSE;
4339 andedresults = SvTRUEx(POPs) && andedresults;
4341 LEAVE_with_name("smartmatch_hash_key_test");
4348 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4349 /* Test sub truth for each element */
4351 bool andedresults = TRUE;
4352 AV *av = (AV*) SvRV(d);
4353 const I32 len = av_len(av);
4354 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4357 for (i = 0; i <= len; ++i) {
4358 SV * const * const svp = av_fetch(av, i, FALSE);
4359 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4360 ENTER_with_name("smartmatch_array_elem_test");
4366 c = call_sv(e, G_SCALAR);
4369 andedresults = FALSE;
4371 andedresults = SvTRUEx(POPs) && andedresults;
4373 LEAVE_with_name("smartmatch_array_elem_test");
4382 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4383 ENTER_with_name("smartmatch_coderef");
4388 c = call_sv(e, G_SCALAR);
4392 else if (SvTEMP(TOPs))
4393 SvREFCNT_inc_void(TOPs);
4395 LEAVE_with_name("smartmatch_coderef");
4400 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4401 if (object_on_left) {
4402 goto sm_any_hash; /* Treat objects like scalars */
4404 else if (!SvOK(d)) {
4405 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4408 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4409 /* Check that the key-sets are identical */
4411 HV *other_hv = MUTABLE_HV(SvRV(d));
4413 bool other_tied = FALSE;
4414 U32 this_key_count = 0,
4415 other_key_count = 0;
4416 HV *hv = MUTABLE_HV(SvRV(e));
4418 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
4419 /* Tied hashes don't know how many keys they have. */
4420 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
4423 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
4424 HV * const temp = other_hv;
4429 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
4432 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
4435 /* The hashes have the same number of keys, so it suffices
4436 to check that one is a subset of the other. */
4437 (void) hv_iterinit(hv);
4438 while ( (he = hv_iternext(hv)) ) {
4439 SV *key = hv_iterkeysv(he);
4441 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
4444 if(!hv_exists_ent(other_hv, key, 0)) {
4445 (void) hv_iterinit(hv); /* reset iterator */
4451 (void) hv_iterinit(other_hv);
4452 while ( hv_iternext(other_hv) )
4456 other_key_count = HvUSEDKEYS(other_hv);
4458 if (this_key_count != other_key_count)
4463 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4464 AV * const other_av = MUTABLE_AV(SvRV(d));
4465 const I32 other_len = av_len(other_av) + 1;
4467 HV *hv = MUTABLE_HV(SvRV(e));
4469 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n"));
4470 for (i = 0; i < other_len; ++i) {
4471 SV ** const svp = av_fetch(other_av, i, FALSE);
4472 DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n"));
4473 if (svp) { /* ??? When can this not happen? */
4474 if (hv_exists_ent(hv, *svp, 0))
4480 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4481 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n"));
4484 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4486 HV *hv = MUTABLE_HV(SvRV(e));
4488 (void) hv_iterinit(hv);
4489 while ( (he = hv_iternext(hv)) ) {
4490 DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n"));
4491 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
4492 (void) hv_iterinit(hv);
4493 destroy_matcher(matcher);
4497 destroy_matcher(matcher);
4503 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n"));
4504 if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0))
4511 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) {
4512 if (object_on_left) {
4513 goto sm_any_array; /* Treat objects like scalars */
4515 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4516 AV * const other_av = MUTABLE_AV(SvRV(e));
4517 const I32 other_len = av_len(other_av) + 1;
4520 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n"));
4521 for (i = 0; i < other_len; ++i) {
4522 SV ** const svp = av_fetch(other_av, i, FALSE);
4524 DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n"));
4525 if (svp) { /* ??? When can this not happen? */
4526 if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0))
4532 if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4533 AV *other_av = MUTABLE_AV(SvRV(d));
4534 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n"));
4535 if (av_len(MUTABLE_AV(SvRV(e))) != av_len(other_av))
4539 const I32 other_len = av_len(other_av);
4541 if (NULL == seen_this) {
4542 seen_this = newHV();
4543 (void) sv_2mortal(MUTABLE_SV(seen_this));
4545 if (NULL == seen_other) {
4546 seen_other = newHV();
4547 (void) sv_2mortal(MUTABLE_SV(seen_other));
4549 for(i = 0; i <= other_len; ++i) {
4550 SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4551 SV * const * const other_elem = av_fetch(other_av, i, FALSE);
4553 if (!this_elem || !other_elem) {
4554 if ((this_elem && SvOK(*this_elem))
4555 || (other_elem && SvOK(*other_elem)))
4558 else if (hv_exists_ent(seen_this,
4559 sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) ||
4560 hv_exists_ent(seen_other,
4561 sv_2mortal(newSViv(PTR2IV(*other_elem))), 0))
4563 if (*this_elem != *other_elem)
4567 (void)hv_store_ent(seen_this,
4568 sv_2mortal(newSViv(PTR2IV(*this_elem))),
4570 (void)hv_store_ent(seen_other,
4571 sv_2mortal(newSViv(PTR2IV(*other_elem))),
4577 DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n"));
4578 (void) do_smartmatch(seen_this, seen_other);
4580 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4589 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4590 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n"));
4593 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4594 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4597 for(i = 0; i <= this_len; ++i) {
4598 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4599 DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n"));
4600 if (svp && matcher_matches_sv(matcher, *svp)) {
4601 destroy_matcher(matcher);
4605 destroy_matcher(matcher);
4609 else if (!SvOK(d)) {
4610 /* undef ~~ array */
4611 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4614 DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n"));
4615 for (i = 0; i <= this_len; ++i) {
4616 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4617 DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n"));
4618 if (!svp || !SvOK(*svp))
4627 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4629 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n"));
4630 for (i = 0; i <= this_len; ++i) {
4631 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4638 /* infinite recursion isn't supposed to happen here */
4639 DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n"));
4640 (void) do_smartmatch(NULL, NULL);
4642 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4651 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) {
4652 if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4653 SV *t = d; d = e; e = t;
4654 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n"));
4657 else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4658 SV *t = d; d = e; e = t;
4659 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n"));
4660 goto sm_regex_array;
4663 PMOP * const matcher = make_matcher((REGEXP*) SvRV(e));
4665 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n"));
4667 PUSHs(matcher_matches_sv(matcher, d)
4670 destroy_matcher(matcher);
4675 /* See if there is overload magic on left */
4676 else if (object_on_left && SvAMAGIC(d)) {
4678 DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n"));
4679 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4682 tmpsv = amagic_call(d, e, smart_amg, AMGf_noright);
4690 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n"));
4693 else if (!SvOK(d)) {
4694 /* undef ~~ scalar ; we already know that the scalar is SvOK */
4695 DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n"));
4700 if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) {
4701 DEBUG_M(if (SvNIOK(e))
4702 Perl_deb(aTHX_ " applying rule Any-Num\n");
4704 Perl_deb(aTHX_ " applying rule Num-numish\n");
4706 /* numeric comparison */
4709 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
4720 /* As a last resort, use string comparison */
4721 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n"));
4730 register PERL_CONTEXT *cx;
4731 const I32 gimme = GIMME_V;
4733 /* This is essentially an optimization: if the match
4734 fails, we don't want to push a context and then
4735 pop it again right away, so we skip straight
4736 to the op that follows the leavewhen.
4737 RETURNOP calls PUTBACK which restores the stack pointer after the POPs.
4739 if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs))
4740 RETURNOP(cLOGOP->op_other->op_next);
4742 ENTER_with_name("eval");
4745 PUSHBLOCK(cx, CXt_WHEN, SP);
4754 register PERL_CONTEXT *cx;
4760 assert(CxTYPE(cx) == CXt_WHEN);
4765 PL_curpm = newpm; /* pop $1 et al */
4767 LEAVE_with_name("eval");
4775 register PERL_CONTEXT *cx;
4778 cxix = dopoptowhen(cxstack_ix);
4780 DIE(aTHX_ "Can't \"continue\" outside a when block");
4781 if (cxix < cxstack_ix)
4784 /* clear off anything above the scope we're re-entering */
4785 inner = PL_scopestack_ix;
4787 if (PL_scopestack_ix < inner)
4788 leave_scope(PL_scopestack[PL_scopestack_ix]);
4789 PL_curcop = cx->blk_oldcop;
4790 return cx->blk_givwhen.leave_op;
4797 register PERL_CONTEXT *cx;
4801 cxix = dopoptogiven(cxstack_ix);
4803 if (PL_op->op_flags & OPf_SPECIAL)
4804 DIE(aTHX_ "Can't use when() outside a topicalizer");
4806 DIE(aTHX_ "Can't \"break\" outside a given block");
4808 if (CxFOREACH(&cxstack[cxix]) && (0 == (PL_op->op_flags & OPf_SPECIAL)))
4809 DIE(aTHX_ "Can't \"break\" in a loop topicalizer");
4811 if (cxix < cxstack_ix)
4814 /* clear off anything above the scope we're re-entering */
4815 inner = PL_scopestack_ix;
4817 if (PL_scopestack_ix < inner)
4818 leave_scope(PL_scopestack[PL_scopestack_ix]);
4819 PL_curcop = cx->blk_oldcop;
4822 return (cx)->blk_loop.my_op->op_nextop;
4824 /* RETURNOP calls PUTBACK which restores the old old sp */
4825 RETURNOP(cx->blk_givwhen.leave_op);
4829 S_doparseform(pTHX_ SV *sv)
4832 register char *s = SvPV_force(sv, len);
4833 register char * const send = s + len;
4834 register char *base = NULL;
4835 register I32 skipspaces = 0;
4836 bool noblank = FALSE;
4837 bool repeat = FALSE;
4838 bool postspace = FALSE;
4844 bool unchopnum = FALSE;
4845 int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */
4847 PERL_ARGS_ASSERT_DOPARSEFORM;
4850 Perl_croak(aTHX_ "Null picture in formline");
4852 /* estimate the buffer size needed */
4853 for (base = s; s <= send; s++) {
4854 if (*s == '\n' || *s == '@' || *s == '^')
4860 Newx(fops, maxops, U32);
4865 *fpc++ = FF_LINEMARK;
4866 noblank = repeat = FALSE;
4884 case ' ': case '\t':
4891 } /* else FALL THROUGH */
4899 *fpc++ = FF_LITERAL;
4907 *fpc++ = (U16)skipspaces;
4911 *fpc++ = FF_NEWLINE;
4915 arg = fpc - linepc + 1;
4922 *fpc++ = FF_LINEMARK;
4923 noblank = repeat = FALSE;
4932 ischop = s[-1] == '^';
4938 arg = (s - base) - 1;
4940 *fpc++ = FF_LITERAL;
4948 *fpc++ = 2; /* skip the @* or ^* */
4950 *fpc++ = FF_LINESNGL;
4953 *fpc++ = FF_LINEGLOB;
4955 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
4956 arg = ischop ? 512 : 0;
4961 const char * const f = ++s;
4964 arg |= 256 + (s - f);
4966 *fpc++ = s - base; /* fieldsize for FETCH */
4967 *fpc++ = FF_DECIMAL;
4969 unchopnum |= ! ischop;
4971 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
4972 arg = ischop ? 512 : 0;
4974 s++; /* skip the '0' first */
4978 const char * const f = ++s;
4981 arg |= 256 + (s - f);
4983 *fpc++ = s - base; /* fieldsize for FETCH */
4984 *fpc++ = FF_0DECIMAL;
4986 unchopnum |= ! ischop;
4990 bool ismore = FALSE;
4993 while (*++s == '>') ;
4994 prespace = FF_SPACE;
4996 else if (*s == '|') {
4997 while (*++s == '|') ;
4998 prespace = FF_HALFSPACE;
5003 while (*++s == '<') ;
5006 if (*s == '.' && s[1] == '.' && s[2] == '.') {
5010 *fpc++ = s - base; /* fieldsize for FETCH */
5012 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
5015 *fpc++ = (U16)prespace;
5029 assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */
5031 { /* need to jump to the next word */
5033 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
5034 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U32) + 4);
5035 s = SvPVX(sv) + SvCUR(sv) + z;
5037 Copy(fops, s, arg, U32);
5039 sv_magic(sv, NULL, PERL_MAGIC_fm, NULL, 0);
5042 if (unchopnum && repeat)
5043 DIE(aTHX_ "Repeated format line will never terminate (~~ and @#)");
5049 S_num_overflow(NV value, I32 fldsize, I32 frcsize)
5051 /* Can value be printed in fldsize chars, using %*.*f ? */
5055 int intsize = fldsize - (value < 0 ? 1 : 0);
5062 while (intsize--) pwr *= 10.0;
5063 while (frcsize--) eps /= 10.0;
5066 if (value + eps >= pwr)
5069 if (value - eps <= -pwr)
5076 S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
5079 SV * const datasv = FILTER_DATA(idx);
5080 const int filter_has_file = IoLINES(datasv);
5081 SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv));
5082 SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv));
5087 char *prune_from = NULL;
5088 bool read_from_cache = FALSE;
5091 PERL_ARGS_ASSERT_RUN_USER_FILTER;
5093 assert(maxlen >= 0);
5096 /* I was having segfault trouble under Linux 2.2.5 after a
5097 parse error occured. (Had to hack around it with a test
5098 for PL_parser->error_count == 0.) Solaris doesn't segfault --
5099 not sure where the trouble is yet. XXX */
5102 SV *const cache = datasv;
5105 const char *cache_p = SvPV(cache, cache_len);
5109 /* Running in block mode and we have some cached data already.
5111 if (cache_len >= umaxlen) {
5112 /* In fact, so much data we don't even need to call
5117 const char *const first_nl =
5118 (const char *)memchr(cache_p, '\n', cache_len);
5120 take = first_nl + 1 - cache_p;
5124 sv_catpvn(buf_sv, cache_p, take);
5125 sv_chop(cache, cache_p + take);
5126 /* Definately not EOF */
5130 sv_catsv(buf_sv, cache);
5132 umaxlen -= cache_len;
5135 read_from_cache = TRUE;
5139 /* Filter API says that the filter appends to the contents of the buffer.
5140 Usually the buffer is "", so the details don't matter. But if it's not,
5141 then clearly what it contains is already filtered by this filter, so we
5142 don't want to pass it in a second time.
5143 I'm going to use a mortal in case the upstream filter croaks. */
5144 upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv))
5145 ? sv_newmortal() : buf_sv;
5146 SvUPGRADE(upstream, SVt_PV);
5148 if (filter_has_file) {
5149 status = FILTER_READ(idx+1, upstream, 0);
5152 if (filter_sub && status >= 0) {
5156 ENTER_with_name("call_filter_sub");
5161 DEFSV_set(upstream);
5165 PUSHs(filter_state);
5168 count = call_sv(filter_sub, G_SCALAR);
5180 LEAVE_with_name("call_filter_sub");
5183 if(SvOK(upstream)) {
5184 got_p = SvPV(upstream, got_len);
5186 if (got_len > umaxlen) {
5187 prune_from = got_p + umaxlen;
5190 char *const first_nl = (char *)memchr(got_p, '\n', got_len);
5191 if (first_nl && first_nl + 1 < got_p + got_len) {
5192 /* There's a second line here... */
5193 prune_from = first_nl + 1;
5198 /* Oh. Too long. Stuff some in our cache. */
5199 STRLEN cached_len = got_p + got_len - prune_from;
5200 SV *const cache = datasv;
5203 /* Cache should be empty. */
5204 assert(!SvCUR(cache));
5207 sv_setpvn(cache, prune_from, cached_len);
5208 /* If you ask for block mode, you may well split UTF-8 characters.
5209 "If it breaks, you get to keep both parts"
5210 (Your code is broken if you don't put them back together again
5211 before something notices.) */
5212 if (SvUTF8(upstream)) {
5215 SvCUR_set(upstream, got_len - cached_len);
5217 /* Can't yet be EOF */
5222 /* If they are at EOF but buf_sv has something in it, then they may never
5223 have touched the SV upstream, so it may be undefined. If we naively
5224 concatenate it then we get a warning about use of uninitialised value.
5226 if (upstream != buf_sv && (SvOK(upstream) || SvGMAGICAL(upstream))) {
5227 sv_catsv(buf_sv, upstream);
5231 IoLINES(datasv) = 0;
5233 SvREFCNT_dec(filter_state);
5234 IoTOP_GV(datasv) = NULL;
5237 SvREFCNT_dec(filter_sub);
5238 IoBOTTOM_GV(datasv) = NULL;
5240 filter_del(S_run_user_filter);
5242 if (status == 0 && read_from_cache) {
5243 /* If we read some data from the cache (and by getting here it implies
5244 that we emptied the cache) then we aren't yet at EOF, and mustn't
5245 report that to our caller. */
5251 /* perhaps someone can come up with a better name for
5252 this? it is not really "absolute", per se ... */
5254 S_path_is_absolute(const char *name)
5256 PERL_ARGS_ASSERT_PATH_IS_ABSOLUTE;
5258 if (PERL_FILE_IS_ABSOLUTE(name)
5260 || (*name == '.' && ((name[1] == '/' ||
5261 (name[1] == '.' && name[2] == '/'))
5262 || (name[1] == '\\' ||
5263 ( name[1] == '.' && name[2] == '\\')))
5266 || (*name == '.' && (name[1] == '/' ||
5267 (name[1] == '.' && name[2] == '/')))
5279 * c-indentation-style: bsd
5281 * indent-tabs-mode: t
5284 * ex: set ts=8 sts=4 sw=4 noet: