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.
2987 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
2988 /* sv Text to convert to OP tree. */
2989 /* startop op_free() this to undo. */
2990 /* code Short string id of the caller. */
2992 dVAR; dSP; /* Make POPBLOCK work. */
2998 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2999 char *tmpbuf = tbuf;
3002 CV* runcv = NULL; /* initialise to avoid compiler warnings */
3006 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3008 ENTER_with_name("eval");
3009 lex_start(sv, NULL, 0);
3011 /* switch to eval mode */
3013 if (IN_PERL_COMPILETIME) {
3014 SAVECOPSTASH_FREE(&PL_compiling);
3015 CopSTASH_set(&PL_compiling, PL_curstash);
3017 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3018 SV * const sv = sv_newmortal();
3019 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3020 code, (unsigned long)++PL_evalseq,
3021 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3026 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3027 (unsigned long)++PL_evalseq);
3028 SAVECOPFILE_FREE(&PL_compiling);
3029 CopFILE_set(&PL_compiling, tmpbuf+2);
3030 SAVECOPLINE(&PL_compiling);
3031 CopLINE_set(&PL_compiling, 1);
3032 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3033 deleting the eval's FILEGV from the stash before gv_check() runs
3034 (i.e. before run-time proper). To work around the coredump that
3035 ensues, we always turn GvMULTI_on for any globals that were
3036 introduced within evals. See force_ident(). GSAR 96-10-12 */
3037 safestr = savepvn(tmpbuf, len);
3038 SAVEDELETE(PL_defstash, safestr, len);
3040 #ifdef OP_IN_REGISTER
3046 /* we get here either during compilation, or via pp_regcomp at runtime */
3047 runtime = IN_PERL_RUNTIME;
3049 runcv = find_runcv(NULL);
3052 PL_op->op_type = OP_ENTEREVAL;
3053 PL_op->op_flags = 0; /* Avoid uninit warning. */
3054 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
3056 need_catch = CATCH_GET;
3060 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
3062 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
3063 CATCH_SET(need_catch);
3064 POPBLOCK(cx,PL_curpm);
3067 (*startop)->op_type = OP_NULL;
3068 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
3069 /* XXX DAPM do this properly one year */
3070 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
3071 LEAVE_with_name("eval");
3072 if (IN_PERL_COMPILETIME)
3073 CopHINTS_set(&PL_compiling, PL_hints);
3074 #ifdef OP_IN_REGISTER
3077 PERL_UNUSED_VAR(newsp);
3078 PERL_UNUSED_VAR(optype);
3080 return PL_eval_start;
3085 =for apidoc find_runcv
3087 Locate the CV corresponding to the currently executing sub or eval.
3088 If db_seqp is non_null, skip CVs that are in the DB package and populate
3089 *db_seqp with the cop sequence number at the point that the DB:: code was
3090 entered. (allows debuggers to eval in the scope of the breakpoint rather
3091 than in the scope of the debugger itself).
3097 Perl_find_runcv(pTHX_ U32 *db_seqp)
3103 *db_seqp = PL_curcop->cop_seq;
3104 for (si = PL_curstackinfo; si; si = si->si_prev) {
3106 for (ix = si->si_cxix; ix >= 0; ix--) {
3107 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
3108 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3109 CV * const cv = cx->blk_sub.cv;
3110 /* skip DB:: code */
3111 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3112 *db_seqp = cx->blk_oldcop->cop_seq;
3117 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3125 /* Run yyparse() in a setjmp wrapper. Returns:
3126 * 0: yyparse() successful
3127 * 1: yyparse() failed
3131 S_try_yyparse(pTHX_ int gramtype)
3136 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3140 ret = yyparse(gramtype) ? 1 : 0;
3154 /* Compile a require/do, an eval '', or a /(?{...})/.
3155 * In the last case, startop is non-null, and contains the address of
3156 * a pointer that should be set to the just-compiled code.
3157 * outside is the lexically enclosing CV (if any) that invoked us.
3158 * Returns a bool indicating whether the compile was successful; if so,
3159 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3160 * pushes undef (also croaks if startop != NULL).
3164 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
3167 OP * const saveop = PL_op;
3168 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3171 PL_in_eval = (in_require
3172 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3177 SAVESPTR(PL_compcv);
3178 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3179 CvEVAL_on(PL_compcv);
3180 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3181 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3183 CvOUTSIDE_SEQ(PL_compcv) = seq;
3184 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3186 /* set up a scratch pad */
3188 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
3189 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3193 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
3195 /* make sure we compile in the right package */
3197 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3198 SAVESPTR(PL_curstash);
3199 PL_curstash = CopSTASH(PL_curcop);
3201 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3202 SAVESPTR(PL_beginav);
3203 PL_beginav = newAV();
3204 SAVEFREESV(PL_beginav);
3205 SAVESPTR(PL_unitcheckav);
3206 PL_unitcheckav = newAV();
3207 SAVEFREESV(PL_unitcheckav);
3210 SAVEBOOL(PL_madskills);
3214 /* try to compile it */
3216 PL_eval_root = NULL;
3217 PL_curcop = &PL_compiling;
3218 CopARYBASE_set(PL_curcop, 0);
3219 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3220 PL_in_eval |= EVAL_KEEPERR;
3224 CALL_BLOCK_HOOKS(bhk_eval, saveop);
3226 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3227 * so honour CATCH_GET and trap it here if necessary */
3229 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
3231 if (yystatus || PL_parser->error_count || !PL_eval_root) {
3232 SV **newsp; /* Used by POPBLOCK. */
3233 PERL_CONTEXT *cx = NULL;
3234 I32 optype; /* Used by POPEVAL. */
3238 PERL_UNUSED_VAR(newsp);
3239 PERL_UNUSED_VAR(optype);
3241 /* note that if yystatus == 3, then the EVAL CX block has already
3242 * been popped, and various vars restored */
3244 if (yystatus != 3) {
3246 op_free(PL_eval_root);
3247 PL_eval_root = NULL;
3249 SP = PL_stack_base + POPMARK; /* pop original mark */
3251 POPBLOCK(cx,PL_curpm);
3253 namesv = cx->blk_eval.old_namesv;
3257 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3259 msg = SvPVx_nolen_const(ERRSV);
3262 /* If cx is still NULL, it means that we didn't go in the
3263 * POPEVAL branch. */
3264 cx = &cxstack[cxstack_ix];
3265 assert(CxTYPE(cx) == CXt_EVAL);
3266 namesv = cx->blk_eval.old_namesv;
3268 (void)hv_store(GvHVn(PL_incgv),
3269 SvPVX_const(namesv), SvCUR(namesv),
3271 Perl_croak(aTHX_ "%sCompilation failed in require",
3272 *msg ? msg : "Unknown error\n");
3275 if (yystatus != 3) {
3276 POPBLOCK(cx,PL_curpm);
3279 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3280 (*msg ? msg : "Unknown error\n"));
3284 sv_setpvs(ERRSV, "Compilation error");
3287 PUSHs(&PL_sv_undef);
3291 CopLINE_set(&PL_compiling, 0);
3293 *startop = PL_eval_root;
3295 SAVEFREEOP(PL_eval_root);
3297 /* Set the context for this new optree.
3298 * Propagate the context from the eval(). */
3299 if ((gimme & G_WANT) == G_VOID)
3300 scalarvoid(PL_eval_root);
3301 else if ((gimme & G_WANT) == G_ARRAY)
3304 scalar(PL_eval_root);
3306 DEBUG_x(dump_eval());
3308 /* Register with debugger: */
3309 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3310 CV * const cv = get_cvs("DB::postponed", 0);
3314 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3316 call_sv(MUTABLE_SV(cv), G_DISCARD);
3320 if (PL_unitcheckav) {
3321 OP *es = PL_eval_start;
3322 call_list(PL_scopestack_ix, PL_unitcheckav);
3326 /* compiled okay, so do it */
3328 CvDEPTH(PL_compcv) = 1;
3329 SP = PL_stack_base + POPMARK; /* pop original mark */
3330 PL_op = saveop; /* The caller may need it. */
3331 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3338 S_check_type_and_open(pTHX_ SV *name)
3341 const char *p = SvPV_nolen_const(name);
3342 const int st_rc = PerlLIO_stat(p, &st);
3344 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3346 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3350 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
3351 return PerlIO_openn(aTHX_ NULL, PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
3353 return PerlIO_open(p, PERL_SCRIPT_MODE);
3357 #ifndef PERL_DISABLE_PMC
3359 S_doopen_pm(pTHX_ SV *name)
3362 const char *p = SvPV_const(name, namelen);
3364 PERL_ARGS_ASSERT_DOOPEN_PM;
3366 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
3367 SV *const pmcsv = sv_mortalcopy(name);
3370 sv_catpvn(pmcsv, "c", 1);
3372 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3373 return check_type_and_open(pmcsv);
3375 return check_type_and_open(name);
3378 # define doopen_pm(name) check_type_and_open(name)
3379 #endif /* !PERL_DISABLE_PMC */
3384 register PERL_CONTEXT *cx;
3391 int vms_unixname = 0;
3393 const char *tryname = NULL;
3395 const I32 gimme = GIMME_V;
3396 int filter_has_file = 0;
3397 PerlIO *tryrsfp = NULL;
3398 SV *filter_cache = NULL;
3399 SV *filter_state = NULL;
3400 SV *filter_sub = NULL;
3406 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3407 sv = sv_2mortal(new_version(sv));
3408 if (!sv_derived_from(PL_patchlevel, "version"))
3409 upg_version(PL_patchlevel, TRUE);
3410 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3411 if ( vcmp(sv,PL_patchlevel) <= 0 )
3412 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3413 SVfARG(vnormal(sv)), SVfARG(vnormal(PL_patchlevel)));
3416 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3419 SV * const req = SvRV(sv);
3420 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3422 /* get the left hand term */
3423 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3425 first = SvIV(*av_fetch(lav,0,0));
3426 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3427 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3428 || av_len(lav) > 1 /* FP with > 3 digits */
3429 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3431 DIE(aTHX_ "Perl %"SVf" required--this is only "
3432 "%"SVf", stopped", SVfARG(vnormal(req)),
3433 SVfARG(vnormal(PL_patchlevel)));
3435 else { /* probably 'use 5.10' or 'use 5.8' */
3440 second = SvIV(*av_fetch(lav,1,0));
3442 second /= second >= 600 ? 100 : 10;
3443 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3444 (int)first, (int)second);
3445 upg_version(hintsv, TRUE);
3447 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3448 "--this is only %"SVf", stopped",
3449 SVfARG(vnormal(req)),
3450 SVfARG(vnormal(sv_2mortal(hintsv))),
3451 SVfARG(vnormal(PL_patchlevel)));
3456 /* We do this only with "use", not "require" or "no". */
3457 if (PL_compcv && !(cUNOP->op_first->op_private & OPpCONST_NOVER)) {
3458 /* If we request a version >= 5.9.5, load feature.pm with the
3459 * feature bundle that corresponds to the required version. */
3460 if (vcmp(sv, sv_2mortal(upg_version(newSVnv(5.009005), FALSE))) >= 0) {
3461 SV *const importsv = vnormal(sv);
3462 *SvPVX_mutable(importsv) = ':';
3463 ENTER_with_name("load_feature");
3464 Perl_load_module(aTHX_ 0, newSVpvs("feature"), NULL, importsv, NULL);
3465 LEAVE_with_name("load_feature");
3467 /* If a version >= 5.11.0 is requested, strictures are on by default! */
3468 if (vcmp(sv, sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
3469 PL_hints |= (HINT_STRICT_REFS | HINT_STRICT_SUBS | HINT_STRICT_VARS);
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 char *tmpbuf = tbuf;
3850 HV *saved_hh = NULL;
3852 if (PL_op->op_private & OPpEVAL_HAS_HH) {
3853 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
3857 /* make sure we've got a plain PV (no overload etc) before testing
3858 * for taint. Making a copy here is probably overkill, but better
3859 * safe than sorry */
3861 const char * const p = SvPV_const(sv, len);
3863 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
3866 TAINT_IF(SvTAINTED(sv));
3867 TAINT_PROPER("eval");
3869 ENTER_with_name("eval");
3870 lex_start(sv, NULL, 0);
3873 /* switch to eval mode */
3875 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3876 SV * const temp_sv = sv_newmortal();
3877 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
3878 (unsigned long)++PL_evalseq,
3879 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3880 tmpbuf = SvPVX(temp_sv);
3881 len = SvCUR(temp_sv);
3884 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
3885 SAVECOPFILE_FREE(&PL_compiling);
3886 CopFILE_set(&PL_compiling, tmpbuf+2);
3887 SAVECOPLINE(&PL_compiling);
3888 CopLINE_set(&PL_compiling, 1);
3889 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3890 deleting the eval's FILEGV from the stash before gv_check() runs
3891 (i.e. before run-time proper). To work around the coredump that
3892 ensues, we always turn GvMULTI_on for any globals that were
3893 introduced within evals. See force_ident(). GSAR 96-10-12 */
3895 PL_hints = PL_op->op_targ;
3897 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3898 SvREFCNT_dec(GvHV(PL_hintgv));
3899 GvHV(PL_hintgv) = saved_hh;
3901 SAVECOMPILEWARNINGS();
3902 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3903 cophh_free(CopHINTHASH_get(&PL_compiling));
3904 if (Perl_fetch_cop_label(aTHX_ PL_curcop, NULL, NULL)) {
3905 /* The label, if present, is the first entry on the chain. So rather
3906 than writing a blank label in front of it (which involves an
3907 allocation), just use the next entry in the chain. */
3908 PL_compiling.cop_hints_hash
3909 = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next);
3910 /* Check the assumption that this removed the label. */
3911 assert(Perl_fetch_cop_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3914 PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash);
3915 /* special case: an eval '' executed within the DB package gets lexically
3916 * placed in the first non-DB CV rather than the current CV - this
3917 * allows the debugger to execute code, find lexicals etc, in the
3918 * scope of the code being debugged. Passing &seq gets find_runcv
3919 * to do the dirty work for us */
3920 runcv = find_runcv(&seq);
3922 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3924 cx->blk_eval.retop = PL_op->op_next;
3926 /* prepare to compile string */
3928 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
3929 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
3932 if (doeval(gimme, NULL, runcv, seq)) {
3933 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3934 ? (PERLDB_LINE || PERLDB_SAVESRC)
3935 : PERLDB_SAVESRC_NOSUBS) {
3936 /* Retain the filegv we created. */
3938 char *const safestr = savepvn(tmpbuf, len);
3939 SAVEDELETE(PL_defstash, safestr, len);
3941 return DOCATCH(PL_eval_start);
3943 /* We have already left the scope set up earler thanks to the LEAVE
3945 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3946 ? (PERLDB_LINE || PERLDB_SAVESRC)
3947 : PERLDB_SAVESRC_INVALID) {
3948 /* Retain the filegv we created. */
3950 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
3952 return PL_op->op_next;
3963 register PERL_CONTEXT *cx;
3965 const U8 save_flags = PL_op -> op_flags;
3971 namesv = cx->blk_eval.old_namesv;
3972 retop = cx->blk_eval.retop;
3975 if (gimme == G_VOID)
3977 else if (gimme == G_SCALAR) {
3980 if (SvFLAGS(TOPs) & SVs_TEMP)
3983 *MARK = sv_mortalcopy(TOPs);
3987 *MARK = &PL_sv_undef;
3992 /* in case LEAVE wipes old return values */
3993 for (mark = newsp + 1; mark <= SP; mark++) {
3994 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
3995 *mark = sv_mortalcopy(*mark);
3996 TAINT_NOT; /* Each item is independent */
4000 PL_curpm = newpm; /* Don't pop $1 et al till now */
4003 assert(CvDEPTH(PL_compcv) == 1);
4005 CvDEPTH(PL_compcv) = 0;
4007 if (optype == OP_REQUIRE &&
4008 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
4010 /* Unassume the success we assumed earlier. */
4011 (void)hv_delete(GvHVn(PL_incgv),
4012 SvPVX_const(namesv), SvCUR(namesv),
4014 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4016 /* die_unwind() did LEAVE, or we won't be here */
4019 LEAVE_with_name("eval");
4020 if (!(save_flags & OPf_SPECIAL)) {
4028 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4029 close to the related Perl_create_eval_scope. */
4031 Perl_delete_eval_scope(pTHX)
4036 register PERL_CONTEXT *cx;
4042 LEAVE_with_name("eval_scope");
4043 PERL_UNUSED_VAR(newsp);
4044 PERL_UNUSED_VAR(gimme);
4045 PERL_UNUSED_VAR(optype);
4048 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4049 also needed by Perl_fold_constants. */
4051 Perl_create_eval_scope(pTHX_ U32 flags)
4054 const I32 gimme = GIMME_V;
4056 ENTER_with_name("eval_scope");
4059 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
4062 PL_in_eval = EVAL_INEVAL;
4063 if (flags & G_KEEPERR)
4064 PL_in_eval |= EVAL_KEEPERR;
4067 if (flags & G_FAKINGEVAL) {
4068 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4076 PERL_CONTEXT * const cx = create_eval_scope(0);
4077 cx->blk_eval.retop = cLOGOP->op_other->op_next;
4078 return DOCATCH(PL_op->op_next);
4087 register PERL_CONTEXT *cx;
4092 PERL_UNUSED_VAR(optype);
4095 if (gimme == G_VOID)
4097 else if (gimme == G_SCALAR) {
4101 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4104 *MARK = sv_mortalcopy(TOPs);
4108 *MARK = &PL_sv_undef;
4113 /* in case LEAVE wipes old return values */
4115 for (mark = newsp + 1; mark <= SP; mark++) {
4116 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4117 *mark = sv_mortalcopy(*mark);
4118 TAINT_NOT; /* Each item is independent */
4122 PL_curpm = newpm; /* Don't pop $1 et al till now */
4124 LEAVE_with_name("eval_scope");
4132 register PERL_CONTEXT *cx;
4133 const I32 gimme = GIMME_V;
4135 ENTER_with_name("given");
4138 sv_setsv(PAD_SV(PL_op->op_targ), POPs);
4140 PUSHBLOCK(cx, CXt_GIVEN, SP);
4149 register PERL_CONTEXT *cx;
4153 PERL_UNUSED_CONTEXT;
4156 assert(CxTYPE(cx) == CXt_GIVEN);
4159 if (gimme == G_VOID)
4161 else if (gimme == G_SCALAR) {
4165 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
4168 *MARK = sv_mortalcopy(TOPs);
4172 *MARK = &PL_sv_undef;
4177 /* in case LEAVE wipes old return values */
4179 for (mark = newsp + 1; mark <= SP; mark++) {
4180 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
4181 *mark = sv_mortalcopy(*mark);
4182 TAINT_NOT; /* Each item is independent */
4186 PL_curpm = newpm; /* Don't pop $1 et al till now */
4188 LEAVE_with_name("given");
4192 /* Helper routines used by pp_smartmatch */
4194 S_make_matcher(pTHX_ REGEXP *re)
4197 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
4199 PERL_ARGS_ASSERT_MAKE_MATCHER;
4201 PM_SETRE(matcher, ReREFCNT_inc(re));
4203 SAVEFREEOP((OP *) matcher);
4204 ENTER_with_name("matcher"); SAVETMPS;
4210 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4215 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4217 PL_op = (OP *) matcher;
4222 return (SvTRUEx(POPs));
4226 S_destroy_matcher(pTHX_ PMOP *matcher)
4230 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4231 PERL_UNUSED_ARG(matcher);
4234 LEAVE_with_name("matcher");
4237 /* Do a smart match */
4240 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4241 return do_smartmatch(NULL, NULL);
4244 /* This version of do_smartmatch() implements the
4245 * table of smart matches that is found in perlsyn.
4248 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
4253 bool object_on_left = FALSE;
4254 SV *e = TOPs; /* e is for 'expression' */
4255 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4257 /* Take care only to invoke mg_get() once for each argument.
4258 * Currently we do this by copying the SV if it's magical. */
4261 d = sv_mortalcopy(d);
4268 e = sv_mortalcopy(e);
4270 /* First of all, handle overload magic of the rightmost argument */
4273 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4274 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4276 tmpsv = amagic_call(d, e, smart_amg, 0);
4283 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4286 SP -= 2; /* Pop the values */
4291 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4298 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4299 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4300 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4302 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4303 object_on_left = TRUE;
4306 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4308 if (object_on_left) {
4309 goto sm_any_sub; /* Treat objects like scalars */
4311 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4312 /* Test sub truth for each key */
4314 bool andedresults = TRUE;
4315 HV *hv = (HV*) SvRV(d);
4316 I32 numkeys = hv_iterinit(hv);
4317 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4320 while ( (he = hv_iternext(hv)) ) {
4321 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4322 ENTER_with_name("smartmatch_hash_key_test");
4325 PUSHs(hv_iterkeysv(he));
4327 c = call_sv(e, G_SCALAR);
4330 andedresults = FALSE;
4332 andedresults = SvTRUEx(POPs) && andedresults;
4334 LEAVE_with_name("smartmatch_hash_key_test");
4341 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4342 /* Test sub truth for each element */
4344 bool andedresults = TRUE;
4345 AV *av = (AV*) SvRV(d);
4346 const I32 len = av_len(av);
4347 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4350 for (i = 0; i <= len; ++i) {
4351 SV * const * const svp = av_fetch(av, i, FALSE);
4352 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4353 ENTER_with_name("smartmatch_array_elem_test");
4359 c = call_sv(e, G_SCALAR);
4362 andedresults = FALSE;
4364 andedresults = SvTRUEx(POPs) && andedresults;
4366 LEAVE_with_name("smartmatch_array_elem_test");
4375 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4376 ENTER_with_name("smartmatch_coderef");
4381 c = call_sv(e, G_SCALAR);
4385 else if (SvTEMP(TOPs))
4386 SvREFCNT_inc_void(TOPs);
4388 LEAVE_with_name("smartmatch_coderef");
4393 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4394 if (object_on_left) {
4395 goto sm_any_hash; /* Treat objects like scalars */
4397 else if (!SvOK(d)) {
4398 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4401 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4402 /* Check that the key-sets are identical */
4404 HV *other_hv = MUTABLE_HV(SvRV(d));
4406 bool other_tied = FALSE;
4407 U32 this_key_count = 0,
4408 other_key_count = 0;
4409 HV *hv = MUTABLE_HV(SvRV(e));
4411 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
4412 /* Tied hashes don't know how many keys they have. */
4413 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
4416 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
4417 HV * const temp = other_hv;
4422 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
4425 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
4428 /* The hashes have the same number of keys, so it suffices
4429 to check that one is a subset of the other. */
4430 (void) hv_iterinit(hv);
4431 while ( (he = hv_iternext(hv)) ) {
4432 SV *key = hv_iterkeysv(he);
4434 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
4437 if(!hv_exists_ent(other_hv, key, 0)) {
4438 (void) hv_iterinit(hv); /* reset iterator */
4444 (void) hv_iterinit(other_hv);
4445 while ( hv_iternext(other_hv) )
4449 other_key_count = HvUSEDKEYS(other_hv);
4451 if (this_key_count != other_key_count)
4456 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4457 AV * const other_av = MUTABLE_AV(SvRV(d));
4458 const I32 other_len = av_len(other_av) + 1;
4460 HV *hv = MUTABLE_HV(SvRV(e));
4462 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n"));
4463 for (i = 0; i < other_len; ++i) {
4464 SV ** const svp = av_fetch(other_av, i, FALSE);
4465 DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n"));
4466 if (svp) { /* ??? When can this not happen? */
4467 if (hv_exists_ent(hv, *svp, 0))
4473 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4474 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n"));
4477 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4479 HV *hv = MUTABLE_HV(SvRV(e));
4481 (void) hv_iterinit(hv);
4482 while ( (he = hv_iternext(hv)) ) {
4483 DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n"));
4484 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
4485 (void) hv_iterinit(hv);
4486 destroy_matcher(matcher);
4490 destroy_matcher(matcher);
4496 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n"));
4497 if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0))
4504 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) {
4505 if (object_on_left) {
4506 goto sm_any_array; /* Treat objects like scalars */
4508 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4509 AV * const other_av = MUTABLE_AV(SvRV(e));
4510 const I32 other_len = av_len(other_av) + 1;
4513 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n"));
4514 for (i = 0; i < other_len; ++i) {
4515 SV ** const svp = av_fetch(other_av, i, FALSE);
4517 DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n"));
4518 if (svp) { /* ??? When can this not happen? */
4519 if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0))
4525 if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4526 AV *other_av = MUTABLE_AV(SvRV(d));
4527 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n"));
4528 if (av_len(MUTABLE_AV(SvRV(e))) != av_len(other_av))
4532 const I32 other_len = av_len(other_av);
4534 if (NULL == seen_this) {
4535 seen_this = newHV();
4536 (void) sv_2mortal(MUTABLE_SV(seen_this));
4538 if (NULL == seen_other) {
4539 seen_other = newHV();
4540 (void) sv_2mortal(MUTABLE_SV(seen_other));
4542 for(i = 0; i <= other_len; ++i) {
4543 SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4544 SV * const * const other_elem = av_fetch(other_av, i, FALSE);
4546 if (!this_elem || !other_elem) {
4547 if ((this_elem && SvOK(*this_elem))
4548 || (other_elem && SvOK(*other_elem)))
4551 else if (hv_exists_ent(seen_this,
4552 sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) ||
4553 hv_exists_ent(seen_other,
4554 sv_2mortal(newSViv(PTR2IV(*other_elem))), 0))
4556 if (*this_elem != *other_elem)
4560 (void)hv_store_ent(seen_this,
4561 sv_2mortal(newSViv(PTR2IV(*this_elem))),
4563 (void)hv_store_ent(seen_other,
4564 sv_2mortal(newSViv(PTR2IV(*other_elem))),
4570 DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n"));
4571 (void) do_smartmatch(seen_this, seen_other);
4573 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4582 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4583 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n"));
4586 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4587 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4590 for(i = 0; i <= this_len; ++i) {
4591 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4592 DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n"));
4593 if (svp && matcher_matches_sv(matcher, *svp)) {
4594 destroy_matcher(matcher);
4598 destroy_matcher(matcher);
4602 else if (!SvOK(d)) {
4603 /* undef ~~ array */
4604 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4607 DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n"));
4608 for (i = 0; i <= this_len; ++i) {
4609 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4610 DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n"));
4611 if (!svp || !SvOK(*svp))
4620 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4622 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n"));
4623 for (i = 0; i <= this_len; ++i) {
4624 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4631 /* infinite recursion isn't supposed to happen here */
4632 DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n"));
4633 (void) do_smartmatch(NULL, NULL);
4635 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4644 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) {
4645 if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4646 SV *t = d; d = e; e = t;
4647 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n"));
4650 else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4651 SV *t = d; d = e; e = t;
4652 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n"));
4653 goto sm_regex_array;
4656 PMOP * const matcher = make_matcher((REGEXP*) SvRV(e));
4658 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n"));
4660 PUSHs(matcher_matches_sv(matcher, d)
4663 destroy_matcher(matcher);
4668 /* See if there is overload magic on left */
4669 else if (object_on_left && SvAMAGIC(d)) {
4671 DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n"));
4672 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4675 tmpsv = amagic_call(d, e, smart_amg, AMGf_noright);
4683 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n"));
4686 else if (!SvOK(d)) {
4687 /* undef ~~ scalar ; we already know that the scalar is SvOK */
4688 DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n"));
4693 if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) {
4694 DEBUG_M(if (SvNIOK(e))
4695 Perl_deb(aTHX_ " applying rule Any-Num\n");
4697 Perl_deb(aTHX_ " applying rule Num-numish\n");
4699 /* numeric comparison */
4702 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
4713 /* As a last resort, use string comparison */
4714 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n"));
4723 register PERL_CONTEXT *cx;
4724 const I32 gimme = GIMME_V;
4726 /* This is essentially an optimization: if the match
4727 fails, we don't want to push a context and then
4728 pop it again right away, so we skip straight
4729 to the op that follows the leavewhen.
4730 RETURNOP calls PUTBACK which restores the stack pointer after the POPs.
4732 if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs))
4733 RETURNOP(cLOGOP->op_other->op_next);
4735 ENTER_with_name("eval");
4738 PUSHBLOCK(cx, CXt_WHEN, SP);
4747 register PERL_CONTEXT *cx;
4753 assert(CxTYPE(cx) == CXt_WHEN);
4758 PL_curpm = newpm; /* pop $1 et al */
4760 LEAVE_with_name("eval");
4768 register PERL_CONTEXT *cx;
4771 cxix = dopoptowhen(cxstack_ix);
4773 DIE(aTHX_ "Can't \"continue\" outside a when block");
4774 if (cxix < cxstack_ix)
4777 /* clear off anything above the scope we're re-entering */
4778 inner = PL_scopestack_ix;
4780 if (PL_scopestack_ix < inner)
4781 leave_scope(PL_scopestack[PL_scopestack_ix]);
4782 PL_curcop = cx->blk_oldcop;
4783 return cx->blk_givwhen.leave_op;
4790 register PERL_CONTEXT *cx;
4794 cxix = dopoptogiven(cxstack_ix);
4796 if (PL_op->op_flags & OPf_SPECIAL)
4797 DIE(aTHX_ "Can't use when() outside a topicalizer");
4799 DIE(aTHX_ "Can't \"break\" outside a given block");
4801 if (CxFOREACH(&cxstack[cxix]) && (0 == (PL_op->op_flags & OPf_SPECIAL)))
4802 DIE(aTHX_ "Can't \"break\" in a loop topicalizer");
4804 if (cxix < cxstack_ix)
4807 /* clear off anything above the scope we're re-entering */
4808 inner = PL_scopestack_ix;
4810 if (PL_scopestack_ix < inner)
4811 leave_scope(PL_scopestack[PL_scopestack_ix]);
4812 PL_curcop = cx->blk_oldcop;
4815 return (cx)->blk_loop.my_op->op_nextop;
4817 /* RETURNOP calls PUTBACK which restores the old old sp */
4818 RETURNOP(cx->blk_givwhen.leave_op);
4822 S_doparseform(pTHX_ SV *sv)
4825 register char *s = SvPV_force(sv, len);
4826 register char * const send = s + len;
4827 register char *base = NULL;
4828 register I32 skipspaces = 0;
4829 bool noblank = FALSE;
4830 bool repeat = FALSE;
4831 bool postspace = FALSE;
4837 bool unchopnum = FALSE;
4838 int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */
4840 PERL_ARGS_ASSERT_DOPARSEFORM;
4843 Perl_croak(aTHX_ "Null picture in formline");
4845 /* estimate the buffer size needed */
4846 for (base = s; s <= send; s++) {
4847 if (*s == '\n' || *s == '@' || *s == '^')
4853 Newx(fops, maxops, U32);
4858 *fpc++ = FF_LINEMARK;
4859 noblank = repeat = FALSE;
4877 case ' ': case '\t':
4884 } /* else FALL THROUGH */
4892 *fpc++ = FF_LITERAL;
4900 *fpc++ = (U16)skipspaces;
4904 *fpc++ = FF_NEWLINE;
4908 arg = fpc - linepc + 1;
4915 *fpc++ = FF_LINEMARK;
4916 noblank = repeat = FALSE;
4925 ischop = s[-1] == '^';
4931 arg = (s - base) - 1;
4933 *fpc++ = FF_LITERAL;
4941 *fpc++ = 2; /* skip the @* or ^* */
4943 *fpc++ = FF_LINESNGL;
4946 *fpc++ = FF_LINEGLOB;
4948 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
4949 arg = ischop ? 512 : 0;
4954 const char * const f = ++s;
4957 arg |= 256 + (s - f);
4959 *fpc++ = s - base; /* fieldsize for FETCH */
4960 *fpc++ = FF_DECIMAL;
4962 unchopnum |= ! ischop;
4964 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
4965 arg = ischop ? 512 : 0;
4967 s++; /* skip the '0' first */
4971 const char * const f = ++s;
4974 arg |= 256 + (s - f);
4976 *fpc++ = s - base; /* fieldsize for FETCH */
4977 *fpc++ = FF_0DECIMAL;
4979 unchopnum |= ! ischop;
4983 bool ismore = FALSE;
4986 while (*++s == '>') ;
4987 prespace = FF_SPACE;
4989 else if (*s == '|') {
4990 while (*++s == '|') ;
4991 prespace = FF_HALFSPACE;
4996 while (*++s == '<') ;
4999 if (*s == '.' && s[1] == '.' && s[2] == '.') {
5003 *fpc++ = s - base; /* fieldsize for FETCH */
5005 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
5008 *fpc++ = (U16)prespace;
5022 assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */
5024 { /* need to jump to the next word */
5026 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
5027 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U32) + 4);
5028 s = SvPVX(sv) + SvCUR(sv) + z;
5030 Copy(fops, s, arg, U32);
5032 sv_magic(sv, NULL, PERL_MAGIC_fm, NULL, 0);
5035 if (unchopnum && repeat)
5036 DIE(aTHX_ "Repeated format line will never terminate (~~ and @#)");
5042 S_num_overflow(NV value, I32 fldsize, I32 frcsize)
5044 /* Can value be printed in fldsize chars, using %*.*f ? */
5048 int intsize = fldsize - (value < 0 ? 1 : 0);
5055 while (intsize--) pwr *= 10.0;
5056 while (frcsize--) eps /= 10.0;
5059 if (value + eps >= pwr)
5062 if (value - eps <= -pwr)
5069 S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
5072 SV * const datasv = FILTER_DATA(idx);
5073 const int filter_has_file = IoLINES(datasv);
5074 SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv));
5075 SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv));
5080 char *prune_from = NULL;
5081 bool read_from_cache = FALSE;
5084 PERL_ARGS_ASSERT_RUN_USER_FILTER;
5086 assert(maxlen >= 0);
5089 /* I was having segfault trouble under Linux 2.2.5 after a
5090 parse error occured. (Had to hack around it with a test
5091 for PL_parser->error_count == 0.) Solaris doesn't segfault --
5092 not sure where the trouble is yet. XXX */
5095 SV *const cache = datasv;
5098 const char *cache_p = SvPV(cache, cache_len);
5102 /* Running in block mode and we have some cached data already.
5104 if (cache_len >= umaxlen) {
5105 /* In fact, so much data we don't even need to call
5110 const char *const first_nl =
5111 (const char *)memchr(cache_p, '\n', cache_len);
5113 take = first_nl + 1 - cache_p;
5117 sv_catpvn(buf_sv, cache_p, take);
5118 sv_chop(cache, cache_p + take);
5119 /* Definately not EOF */
5123 sv_catsv(buf_sv, cache);
5125 umaxlen -= cache_len;
5128 read_from_cache = TRUE;
5132 /* Filter API says that the filter appends to the contents of the buffer.
5133 Usually the buffer is "", so the details don't matter. But if it's not,
5134 then clearly what it contains is already filtered by this filter, so we
5135 don't want to pass it in a second time.
5136 I'm going to use a mortal in case the upstream filter croaks. */
5137 upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv))
5138 ? sv_newmortal() : buf_sv;
5139 SvUPGRADE(upstream, SVt_PV);
5141 if (filter_has_file) {
5142 status = FILTER_READ(idx+1, upstream, 0);
5145 if (filter_sub && status >= 0) {
5149 ENTER_with_name("call_filter_sub");
5154 DEFSV_set(upstream);
5158 PUSHs(filter_state);
5161 count = call_sv(filter_sub, G_SCALAR);
5173 LEAVE_with_name("call_filter_sub");
5176 if(SvOK(upstream)) {
5177 got_p = SvPV(upstream, got_len);
5179 if (got_len > umaxlen) {
5180 prune_from = got_p + umaxlen;
5183 char *const first_nl = (char *)memchr(got_p, '\n', got_len);
5184 if (first_nl && first_nl + 1 < got_p + got_len) {
5185 /* There's a second line here... */
5186 prune_from = first_nl + 1;
5191 /* Oh. Too long. Stuff some in our cache. */
5192 STRLEN cached_len = got_p + got_len - prune_from;
5193 SV *const cache = datasv;
5196 /* Cache should be empty. */
5197 assert(!SvCUR(cache));
5200 sv_setpvn(cache, prune_from, cached_len);
5201 /* If you ask for block mode, you may well split UTF-8 characters.
5202 "If it breaks, you get to keep both parts"
5203 (Your code is broken if you don't put them back together again
5204 before something notices.) */
5205 if (SvUTF8(upstream)) {
5208 SvCUR_set(upstream, got_len - cached_len);
5210 /* Can't yet be EOF */
5215 /* If they are at EOF but buf_sv has something in it, then they may never
5216 have touched the SV upstream, so it may be undefined. If we naively
5217 concatenate it then we get a warning about use of uninitialised value.
5219 if (upstream != buf_sv && (SvOK(upstream) || SvGMAGICAL(upstream))) {
5220 sv_catsv(buf_sv, upstream);
5224 IoLINES(datasv) = 0;
5226 SvREFCNT_dec(filter_state);
5227 IoTOP_GV(datasv) = NULL;
5230 SvREFCNT_dec(filter_sub);
5231 IoBOTTOM_GV(datasv) = NULL;
5233 filter_del(S_run_user_filter);
5235 if (status == 0 && read_from_cache) {
5236 /* If we read some data from the cache (and by getting here it implies
5237 that we emptied the cache) then we aren't yet at EOF, and mustn't
5238 report that to our caller. */
5244 /* perhaps someone can come up with a better name for
5245 this? it is not really "absolute", per se ... */
5247 S_path_is_absolute(const char *name)
5249 PERL_ARGS_ASSERT_PATH_IS_ABSOLUTE;
5251 if (PERL_FILE_IS_ABSOLUTE(name)
5253 || (*name == '.' && ((name[1] == '/' ||
5254 (name[1] == '.' && name[2] == '/'))
5255 || (name[1] == '\\' ||
5256 ( name[1] == '.' && name[2] == '\\')))
5259 || (*name == '.' && (name[1] == '/' ||
5260 (name[1] == '.' && name[2] == '/')))
5272 * c-indentation-style: bsd
5274 * indent-tabs-mode: t
5277 * ex: set ts=8 sts=4 sw=4 noet: