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) \
99 if (SvROK(rx) && SvAMAGIC(rx)) { \
100 SV *sv = AMG_CALLun(rx, regexp); \
104 if (SvTYPE(sv) != SVt_REGEXP) \
105 Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \
112 if (PL_op->op_flags & OPf_STACKED) {
113 /* multiple args; concatentate them */
115 tmpstr = PAD_SV(ARGTARG);
116 sv_setpvs(tmpstr, "");
117 while (++MARK <= SP) {
119 if (PL_amagic_generation) {
122 tryAMAGICregexp(msv);
124 if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) &&
125 (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign)))
127 sv_setsv(tmpstr, sv);
131 sv_catsv(tmpstr, msv);
138 tryAMAGICregexp(tmpstr);
141 #undef tryAMAGICregexp
144 SV * const sv = SvRV(tmpstr);
145 if (SvTYPE(sv) == SVt_REGEXP)
148 else if (SvTYPE(tmpstr) == SVt_REGEXP)
149 re = (REGEXP*) tmpstr;
152 /* The match's LHS's get-magic might need to access this op's reg-
153 exp (as is sometimes the case with $'; see bug 70764). So we
154 must call get-magic now before we replace the regexp. Hopeful-
155 ly this hack can be replaced with the approach described at
156 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03
157 /msg122415.html some day. */
158 OP *matchop = pm->op_next;
160 const bool was_tainted = PL_tainted;
161 if (matchop->op_flags & OPf_STACKED)
163 else if (matchop->op_private & OPpTARGET_MY)
164 lhs = PAD_SV(matchop->op_targ);
167 /* Restore the previous value of PL_tainted (which may have been
168 modified by get-magic), to avoid incorrectly setting the
169 RXf_TAINTED flag further down. */
170 PL_tainted = was_tainted;
172 re = reg_temp_copy(NULL, re);
173 ReREFCNT_dec(PM_GETRE(pm));
178 const char *t = SvOK(tmpstr) ? SvPV_const(tmpstr, len) : "";
180 assert (re != (REGEXP*) &PL_sv_undef);
182 /* Check against the last compiled regexp. */
183 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
184 memNE(RX_PRECOMP(re), t, len))
186 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
187 U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME;
191 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
193 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
195 } else if (PL_curcop->cop_hints_hash) {
196 SV *ptr = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, 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);
218 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
220 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
222 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
223 inside tie/overload accessors. */
229 #ifndef INCOMPLETE_TAINTS
232 RX_EXTFLAGS(re) |= RXf_TAINTED;
234 RX_EXTFLAGS(re) &= ~RXf_TAINTED;
238 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
242 #if !defined(USE_ITHREADS)
243 /* can't change the optree at runtime either */
244 /* PMf_KEEP is handled differently under threads to avoid these problems */
245 if (pm->op_pmflags & PMf_KEEP) {
246 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
247 cLOGOP->op_first->op_next = PL_op->op_next;
257 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
258 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
259 register SV * const dstr = cx->sb_dstr;
260 register char *s = cx->sb_s;
261 register char *m = cx->sb_m;
262 char *orig = cx->sb_orig;
263 register REGEXP * const rx = cx->sb_rx;
265 REGEXP *old = PM_GETRE(pm);
269 PM_SETRE(pm,ReREFCNT_inc(rx));
272 rxres_restore(&cx->sb_rxres, rx);
273 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
275 if (cx->sb_iters++) {
276 const I32 saviters = cx->sb_iters;
277 if (cx->sb_iters > cx->sb_maxiters)
278 DIE(aTHX_ "Substitution loop");
280 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
281 cx->sb_rxtainted |= 2;
282 sv_catsv(dstr, POPs);
283 /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */
287 if (CxONCE(cx) || s < orig ||
288 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
289 (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
290 ((cx->sb_rflags & REXEC_COPY_STR)
291 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
292 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
294 SV * const targ = cx->sb_targ;
296 assert(cx->sb_strend >= s);
297 if(cx->sb_strend > s) {
298 if (DO_UTF8(dstr) && !SvUTF8(targ))
299 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
301 sv_catpvn(dstr, s, cx->sb_strend - s);
303 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
305 #ifdef PERL_OLD_COPY_ON_WRITE
307 sv_force_normal_flags(targ, SV_COW_DROP_PV);
313 SvPV_set(targ, SvPVX(dstr));
314 SvCUR_set(targ, SvCUR(dstr));
315 SvLEN_set(targ, SvLEN(dstr));
318 SvPV_set(dstr, NULL);
320 TAINT_IF(cx->sb_rxtainted & 1);
321 mPUSHi(saviters - 1);
323 (void)SvPOK_only_UTF8(targ);
324 TAINT_IF(cx->sb_rxtainted);
328 LEAVE_SCOPE(cx->sb_oldsave);
330 RETURNOP(pm->op_next);
332 cx->sb_iters = saviters;
334 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
337 cx->sb_orig = orig = RX_SUBBEG(rx);
339 cx->sb_strend = s + (cx->sb_strend - m);
341 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
343 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
344 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
346 sv_catpvn(dstr, s, m-s);
348 cx->sb_s = RX_OFFS(rx)[0].end + orig;
349 { /* Update the pos() information. */
350 SV * const sv = cx->sb_targ;
352 SvUPGRADE(sv, SVt_PVMG);
353 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
354 #ifdef PERL_OLD_COPY_ON_WRITE
356 sv_force_normal_flags(sv, 0);
358 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
361 mg->mg_len = m - orig;
364 (void)ReREFCNT_inc(rx);
365 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
366 rxres_save(&cx->sb_rxres, rx);
367 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
371 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
376 PERL_ARGS_ASSERT_RXRES_SAVE;
379 if (!p || p[1] < RX_NPARENS(rx)) {
380 #ifdef PERL_OLD_COPY_ON_WRITE
381 i = 7 + RX_NPARENS(rx) * 2;
383 i = 6 + RX_NPARENS(rx) * 2;
392 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
393 RX_MATCH_COPIED_off(rx);
395 #ifdef PERL_OLD_COPY_ON_WRITE
396 *p++ = PTR2UV(RX_SAVED_COPY(rx));
397 RX_SAVED_COPY(rx) = NULL;
400 *p++ = RX_NPARENS(rx);
402 *p++ = PTR2UV(RX_SUBBEG(rx));
403 *p++ = (UV)RX_SUBLEN(rx);
404 for (i = 0; i <= RX_NPARENS(rx); ++i) {
405 *p++ = (UV)RX_OFFS(rx)[i].start;
406 *p++ = (UV)RX_OFFS(rx)[i].end;
411 S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
416 PERL_ARGS_ASSERT_RXRES_RESTORE;
419 RX_MATCH_COPY_FREE(rx);
420 RX_MATCH_COPIED_set(rx, *p);
423 #ifdef PERL_OLD_COPY_ON_WRITE
424 if (RX_SAVED_COPY(rx))
425 SvREFCNT_dec (RX_SAVED_COPY(rx));
426 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
430 RX_NPARENS(rx) = *p++;
432 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
433 RX_SUBLEN(rx) = (I32)(*p++);
434 for (i = 0; i <= RX_NPARENS(rx); ++i) {
435 RX_OFFS(rx)[i].start = (I32)(*p++);
436 RX_OFFS(rx)[i].end = (I32)(*p++);
441 S_rxres_free(pTHX_ void **rsp)
443 UV * const p = (UV*)*rsp;
445 PERL_ARGS_ASSERT_RXRES_FREE;
450 void *tmp = INT2PTR(char*,*p);
453 PoisonFree(*p, 1, sizeof(*p));
455 Safefree(INT2PTR(char*,*p));
457 #ifdef PERL_OLD_COPY_ON_WRITE
459 SvREFCNT_dec (INT2PTR(SV*,p[1]));
469 dVAR; dSP; dMARK; dORIGMARK;
470 register SV * const tmpForm = *++MARK;
475 register SV *sv = NULL;
476 const char *item = NULL;
480 bool chopspace = (strchr(PL_chopset, ' ') != NULL);
481 const char *chophere = NULL;
482 char *linemark = NULL;
484 bool gotsome = FALSE;
486 const STRLEN fudge = SvPOK(tmpForm)
487 ? (SvCUR(tmpForm) * (IN_BYTES ? 1 : 3) + 1) : 0;
488 bool item_is_utf8 = FALSE;
489 bool targ_is_utf8 = FALSE;
491 OP * parseres = NULL;
494 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
495 if (SvREADONLY(tmpForm)) {
496 SvREADONLY_off(tmpForm);
497 parseres = doparseform(tmpForm);
498 SvREADONLY_on(tmpForm);
501 parseres = doparseform(tmpForm);
505 SvPV_force(PL_formtarget, len);
506 if (DO_UTF8(PL_formtarget))
508 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
510 f = SvPV_const(tmpForm, len);
511 /* need to jump to the next word */
512 fpc = (U32*)(f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN);
516 const char *name = "???";
519 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
520 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
521 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
522 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
523 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
525 case FF_CHECKNL: name = "CHECKNL"; break;
526 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
527 case FF_SPACE: name = "SPACE"; break;
528 case FF_HALFSPACE: name = "HALFSPACE"; break;
529 case FF_ITEM: name = "ITEM"; break;
530 case FF_CHOP: name = "CHOP"; break;
531 case FF_LINEGLOB: name = "LINEGLOB"; break;
532 case FF_NEWLINE: name = "NEWLINE"; break;
533 case FF_MORE: name = "MORE"; break;
534 case FF_LINEMARK: name = "LINEMARK"; break;
535 case FF_END: name = "END"; break;
536 case FF_0DECIMAL: name = "0DECIMAL"; break;
537 case FF_LINESNGL: name = "LINESNGL"; break;
540 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
542 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
553 if (targ_is_utf8 && !SvUTF8(tmpForm)) {
554 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
556 sv_catpvn_utf8_upgrade(PL_formtarget, f, arg, nsv);
557 t = SvEND(PL_formtarget);
561 if (!targ_is_utf8 && DO_UTF8(tmpForm)) {
562 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
564 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC, fudge + 1);
565 t = SvEND(PL_formtarget);
585 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
592 const char *s = item = SvPV_const(sv, len);
595 itemsize = sv_len_utf8(sv);
596 if (itemsize != (I32)len) {
598 if (itemsize > fieldsize) {
599 itemsize = fieldsize;
600 itembytes = itemsize;
601 sv_pos_u2b(sv, &itembytes, 0);
605 send = chophere = s + itembytes;
615 sv_pos_b2u(sv, &itemsize);
619 item_is_utf8 = FALSE;
620 if (itemsize > fieldsize)
621 itemsize = fieldsize;
622 send = chophere = s + itemsize;
636 const char *s = item = SvPV_const(sv, len);
639 itemsize = sv_len_utf8(sv);
640 if (itemsize != (I32)len) {
642 if (itemsize <= fieldsize) {
643 const char *send = chophere = s + itemsize;
656 itemsize = fieldsize;
657 itembytes = itemsize;
658 sv_pos_u2b(sv, &itembytes, 0);
659 send = chophere = s + itembytes;
660 while (s < send || (s == send && isSPACE(*s))) {
670 if (strchr(PL_chopset, *s))
675 itemsize = chophere - item;
676 sv_pos_b2u(sv, &itemsize);
682 item_is_utf8 = FALSE;
683 if (itemsize <= fieldsize) {
684 const char *const send = chophere = s + itemsize;
697 itemsize = fieldsize;
698 send = chophere = s + itemsize;
699 while (s < send || (s == send && isSPACE(*s))) {
709 if (strchr(PL_chopset, *s))
714 itemsize = chophere - item;
720 arg = fieldsize - itemsize;
729 arg = fieldsize - itemsize;
740 const char *s = item;
744 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
746 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC,
748 t = SvEND(PL_formtarget);
752 if (UTF8_IS_CONTINUED(*s)) {
753 STRLEN skip = UTF8SKIP(s);
770 if ( !((*t++ = *s++) & ~31) )
776 if (targ_is_utf8 && !item_is_utf8) {
777 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
779 sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
780 for (; t < SvEND(PL_formtarget); t++) {
793 const int ch = *t++ = *s++;
796 if ( !((*t++ = *s++) & ~31) )
805 const char *s = chophere;
819 const bool oneline = fpc[-1] == FF_LINESNGL;
820 const char *s = item = SvPV_const(sv, len);
821 item_is_utf8 = DO_UTF8(sv);
824 STRLEN to_copy = itemsize;
825 const char *const send = s + len;
826 const U8 *source = (const U8 *) s;
830 chophere = s + itemsize;
834 to_copy = s - SvPVX_const(sv) - 1;
846 if (targ_is_utf8 && !item_is_utf8) {
847 source = tmp = bytes_to_utf8(source, &to_copy);
848 SvCUR_set(PL_formtarget,
849 t - SvPVX_const(PL_formtarget));
851 if (item_is_utf8 && !targ_is_utf8) {
852 /* Upgrade targ to UTF8, and then we reduce it to
853 a problem we have a simple solution for. */
854 SvCUR_set(PL_formtarget,
855 t - SvPVX_const(PL_formtarget));
857 /* Don't need get magic. */
858 sv_utf8_upgrade_nomg(PL_formtarget);
860 SvCUR_set(PL_formtarget,
861 t - SvPVX_const(PL_formtarget));
864 /* Easy. They agree. */
865 assert (item_is_utf8 == targ_is_utf8);
867 SvGROW(PL_formtarget,
868 SvCUR(PL_formtarget) + to_copy + fudge + 1);
869 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
871 Copy(source, t, to_copy, char);
873 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
875 if (SvGMAGICAL(sv)) {
876 /* Mustn't call sv_pos_b2u() as it does a second
877 mg_get(). Is this a bug? Do we need a _flags()
879 itemsize = utf8_length(source, source + itemsize);
881 sv_pos_b2u(sv, &itemsize);
893 #if defined(USE_LONG_DOUBLE)
896 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
900 "%#0*.*f" : "%0*.*f");
905 #if defined(USE_LONG_DOUBLE)
907 ((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
910 ((arg & 256) ? "%#*.*f" : "%*.*f");
913 /* If the field is marked with ^ and the value is undefined,
915 if ((arg & 512) && !SvOK(sv)) {
923 /* overflow evidence */
924 if (num_overflow(value, fieldsize, arg)) {
930 /* Formats aren't yet marked for locales, so assume "yes". */
932 STORE_NUMERIC_STANDARD_SET_LOCAL();
933 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
934 RESTORE_NUMERIC_STANDARD();
941 while (t-- > linemark && *t == ' ') ;
949 if (arg) { /* repeat until fields exhausted? */
951 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
952 lines += FmLINES(PL_formtarget);
954 SvUTF8_on(PL_formtarget);
955 FmLINES(PL_formtarget) = lines;
957 RETURNOP(cLISTOP->op_first);
968 const char *s = chophere;
969 const char *send = item + len;
971 while (isSPACE(*s) && (s < send))
976 arg = fieldsize - itemsize;
983 if (strnEQ(s1," ",3)) {
984 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
995 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
997 SvUTF8_on(PL_formtarget);
998 FmLINES(PL_formtarget) += lines;
1010 if (PL_stack_base + *PL_markstack_ptr == SP) {
1012 if (GIMME_V == G_SCALAR)
1014 RETURNOP(PL_op->op_next->op_next);
1016 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
1017 pp_pushmark(); /* push dst */
1018 pp_pushmark(); /* push src */
1019 ENTER_with_name("grep"); /* enter outer scope */
1022 if (PL_op->op_private & OPpGREP_LEX)
1023 SAVESPTR(PAD_SVl(PL_op->op_targ));
1026 ENTER_with_name("grep_item"); /* enter inner scope */
1029 src = PL_stack_base[*PL_markstack_ptr];
1031 if (PL_op->op_private & OPpGREP_LEX)
1032 PAD_SVl(PL_op->op_targ) = src;
1037 if (PL_op->op_type == OP_MAPSTART)
1038 pp_pushmark(); /* push top */
1039 return ((LOGOP*)PL_op->op_next)->op_other;
1045 const I32 gimme = GIMME_V;
1046 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
1052 /* first, move source pointer to the next item in the source list */
1053 ++PL_markstack_ptr[-1];
1055 /* if there are new items, push them into the destination list */
1056 if (items && gimme != G_VOID) {
1057 /* might need to make room back there first */
1058 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1059 /* XXX this implementation is very pessimal because the stack
1060 * is repeatedly extended for every set of items. Is possible
1061 * to do this without any stack extension or copying at all
1062 * by maintaining a separate list over which the map iterates
1063 * (like foreach does). --gsar */
1065 /* everything in the stack after the destination list moves
1066 * towards the end the stack by the amount of room needed */
1067 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1069 /* items to shift up (accounting for the moved source pointer) */
1070 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
1072 /* This optimization is by Ben Tilly and it does
1073 * things differently from what Sarathy (gsar)
1074 * is describing. The downside of this optimization is
1075 * that leaves "holes" (uninitialized and hopefully unused areas)
1076 * to the Perl stack, but on the other hand this
1077 * shouldn't be a problem. If Sarathy's idea gets
1078 * implemented, this optimization should become
1079 * irrelevant. --jhi */
1081 shift = count; /* Avoid shifting too often --Ben Tilly */
1085 dst = (SP += shift);
1086 PL_markstack_ptr[-1] += shift;
1087 *PL_markstack_ptr += shift;
1091 /* copy the new items down to the destination list */
1092 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
1093 if (gimme == G_ARRAY) {
1095 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
1098 /* scalar context: we don't care about which values map returns
1099 * (we use undef here). And so we certainly don't want to do mortal
1100 * copies of meaningless values. */
1101 while (items-- > 0) {
1103 *dst-- = &PL_sv_undef;
1107 LEAVE_with_name("grep_item"); /* exit inner scope */
1110 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1112 (void)POPMARK; /* pop top */
1113 LEAVE_with_name("grep"); /* exit outer scope */
1114 (void)POPMARK; /* pop src */
1115 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1116 (void)POPMARK; /* pop dst */
1117 SP = PL_stack_base + POPMARK; /* pop original mark */
1118 if (gimme == G_SCALAR) {
1119 if (PL_op->op_private & OPpGREP_LEX) {
1120 SV* sv = sv_newmortal();
1121 sv_setiv(sv, items);
1129 else if (gimme == G_ARRAY)
1136 ENTER_with_name("grep_item"); /* enter inner scope */
1139 /* set $_ to the new source item */
1140 src = PL_stack_base[PL_markstack_ptr[-1]];
1142 if (PL_op->op_private & OPpGREP_LEX)
1143 PAD_SVl(PL_op->op_targ) = src;
1147 RETURNOP(cLOGOP->op_other);
1156 if (GIMME == G_ARRAY)
1158 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1159 return cLOGOP->op_other;
1169 if (GIMME == G_ARRAY) {
1170 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1174 SV * const targ = PAD_SV(PL_op->op_targ);
1177 if (PL_op->op_private & OPpFLIP_LINENUM) {
1178 if (GvIO(PL_last_in_gv)) {
1179 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1182 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1184 flip = SvIV(sv) == SvIV(GvSV(gv));
1190 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1191 if (PL_op->op_flags & OPf_SPECIAL) {
1199 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1202 sv_setpvs(TARG, "");
1208 /* This code tries to decide if "$left .. $right" should use the
1209 magical string increment, or if the range is numeric (we make
1210 an exception for .."0" [#18165]). AMS 20021031. */
1212 #define RANGE_IS_NUMERIC(left,right) ( \
1213 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1214 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1215 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1216 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1217 && (!SvOK(right) || looks_like_number(right))))
1223 if (GIMME == G_ARRAY) {
1229 if (RANGE_IS_NUMERIC(left,right)) {
1232 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1233 (SvOK(right) && SvNV(right) > IV_MAX))
1234 DIE(aTHX_ "Range iterator outside integer range");
1245 SV * const sv = sv_2mortal(newSViv(i++));
1250 SV * const final = sv_mortalcopy(right);
1252 const char * const tmps = SvPV_const(final, len);
1254 SV *sv = sv_mortalcopy(left);
1255 SvPV_force_nolen(sv);
1256 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1258 if (strEQ(SvPVX_const(sv),tmps))
1260 sv = sv_2mortal(newSVsv(sv));
1267 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1271 if (PL_op->op_private & OPpFLIP_LINENUM) {
1272 if (GvIO(PL_last_in_gv)) {
1273 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1276 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1277 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1285 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1286 sv_catpvs(targ, "E0");
1296 static const char * const context_name[] = {
1298 NULL, /* CXt_WHEN never actually needs "block" */
1299 NULL, /* CXt_BLOCK never actually needs "block" */
1300 NULL, /* CXt_GIVEN never actually needs "block" */
1301 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1302 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1303 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1304 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
1312 S_dopoptolabel(pTHX_ const char *label)
1317 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1319 for (i = cxstack_ix; i >= 0; i--) {
1320 register const PERL_CONTEXT * const cx = &cxstack[i];
1321 switch (CxTYPE(cx)) {
1327 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1328 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1329 if (CxTYPE(cx) == CXt_NULL)
1332 case CXt_LOOP_LAZYIV:
1333 case CXt_LOOP_LAZYSV:
1335 case CXt_LOOP_PLAIN:
1336 if ( !CxLABEL(cx) || strNE(label, CxLABEL(cx)) ) {
1337 DEBUG_l(Perl_deb(aTHX_ "(Skipping label #%ld %s)\n",
1338 (long)i, CxLABEL(cx)));
1341 DEBUG_l( Perl_deb(aTHX_ "(Found label #%ld %s)\n", (long)i, label));
1351 Perl_dowantarray(pTHX)
1354 const I32 gimme = block_gimme();
1355 return (gimme == G_VOID) ? G_SCALAR : gimme;
1359 Perl_block_gimme(pTHX)
1362 const I32 cxix = dopoptosub(cxstack_ix);
1366 switch (cxstack[cxix].blk_gimme) {
1374 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1381 Perl_is_lvalue_sub(pTHX)
1384 const I32 cxix = dopoptosub(cxstack_ix);
1385 assert(cxix >= 0); /* We should only be called from inside subs */
1387 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1388 return CxLVAL(cxstack + cxix);
1394 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1399 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1401 for (i = startingblock; i >= 0; i--) {
1402 register const PERL_CONTEXT * const cx = &cxstk[i];
1403 switch (CxTYPE(cx)) {
1409 DEBUG_l( Perl_deb(aTHX_ "(Found sub #%ld)\n", (long)i));
1417 S_dopoptoeval(pTHX_ I32 startingblock)
1421 for (i = startingblock; i >= 0; i--) {
1422 register const PERL_CONTEXT *cx = &cxstack[i];
1423 switch (CxTYPE(cx)) {
1427 DEBUG_l( Perl_deb(aTHX_ "(Found eval #%ld)\n", (long)i));
1435 S_dopoptoloop(pTHX_ I32 startingblock)
1439 for (i = startingblock; i >= 0; i--) {
1440 register const PERL_CONTEXT * const cx = &cxstack[i];
1441 switch (CxTYPE(cx)) {
1447 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1448 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1449 if ((CxTYPE(cx)) == CXt_NULL)
1452 case CXt_LOOP_LAZYIV:
1453 case CXt_LOOP_LAZYSV:
1455 case CXt_LOOP_PLAIN:
1456 DEBUG_l( Perl_deb(aTHX_ "(Found loop #%ld)\n", (long)i));
1464 S_dopoptogiven(pTHX_ I32 startingblock)
1468 for (i = startingblock; i >= 0; i--) {
1469 register const PERL_CONTEXT *cx = &cxstack[i];
1470 switch (CxTYPE(cx)) {
1474 DEBUG_l( Perl_deb(aTHX_ "(Found given #%ld)\n", (long)i));
1476 case CXt_LOOP_PLAIN:
1477 assert(!CxFOREACHDEF(cx));
1479 case CXt_LOOP_LAZYIV:
1480 case CXt_LOOP_LAZYSV:
1482 if (CxFOREACHDEF(cx)) {
1483 DEBUG_l( Perl_deb(aTHX_ "(Found foreach #%ld)\n", (long)i));
1492 S_dopoptowhen(pTHX_ I32 startingblock)
1496 for (i = startingblock; i >= 0; i--) {
1497 register const PERL_CONTEXT *cx = &cxstack[i];
1498 switch (CxTYPE(cx)) {
1502 DEBUG_l( Perl_deb(aTHX_ "(Found when #%ld)\n", (long)i));
1510 Perl_dounwind(pTHX_ I32 cxix)
1515 while (cxstack_ix > cxix) {
1517 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1518 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
1519 (long) cxstack_ix, PL_block_type[CxTYPE(cx)]));
1520 /* Note: we don't need to restore the base context info till the end. */
1521 switch (CxTYPE(cx)) {
1524 continue; /* not break */
1532 case CXt_LOOP_LAZYIV:
1533 case CXt_LOOP_LAZYSV:
1535 case CXt_LOOP_PLAIN:
1546 PERL_UNUSED_VAR(optype);
1550 Perl_qerror(pTHX_ SV *err)
1554 PERL_ARGS_ASSERT_QERROR;
1557 sv_catsv(ERRSV, err);
1559 sv_catsv(PL_errors, err);
1561 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
1563 ++PL_parser->error_count;
1567 Perl_die_where(pTHX_ SV *msv)
1576 if (PL_in_eval & EVAL_KEEPERR) {
1577 static const char prefix[] = "\t(in cleanup) ";
1578 SV * const err = ERRSV;
1579 const char *e = NULL;
1582 else if (SvCUR(err) >= sizeof(prefix)+SvCUR(msv)-1) {
1585 const char* message = SvPV_const(msv, msglen);
1586 e = SvPV_const(err, len);
1588 if (*e != *message || strNE(e,message))
1593 SvGROW(err, SvCUR(err)+sizeof(prefix)+SvCUR(msv));
1594 sv_catpvn(err, prefix, sizeof(prefix)-1);
1596 start = SvCUR(err)-SvCUR(msv)-sizeof(prefix)+1;
1597 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "%s",
1598 SvPVX_const(err)+start);
1603 const char* message = SvPV_const(msv, msglen);
1604 sv_setpvn(ERRSV, message, msglen);
1605 SvFLAGS(ERRSV) |= SvFLAGS(msv) & SVf_UTF8;
1609 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1610 && PL_curstackinfo->si_prev)
1618 register PERL_CONTEXT *cx;
1621 if (cxix < cxstack_ix)
1624 POPBLOCK(cx,PL_curpm);
1625 if (CxTYPE(cx) != CXt_EVAL) {
1627 const char* message = SvPVx_const( msv ? msv : ERRSV, msglen);
1628 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
1629 PerlIO_write(Perl_error_log, message, msglen);
1634 if (gimme == G_SCALAR)
1635 *++newsp = &PL_sv_undef;
1636 PL_stack_sp = newsp;
1640 /* LEAVE could clobber PL_curcop (see save_re_context())
1641 * XXX it might be better to find a way to avoid messing with
1642 * PL_curcop in save_re_context() instead, but this is a more
1643 * minimal fix --GSAR */
1644 PL_curcop = cx->blk_oldcop;
1646 if (optype == OP_REQUIRE) {
1647 const char* const msg = SvPVx_nolen_const(ERRSV);
1648 SV * const nsv = cx->blk_eval.old_namesv;
1649 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
1651 DIE(aTHX_ "%sCompilation failed in require",
1652 *msg ? msg : "Unknown error\n");
1654 assert(CxTYPE(cx) == CXt_EVAL);
1655 PL_restartop = cx->blk_eval.retop;
1661 write_to_stderr( msv ? msv : ERRSV );
1668 dVAR; dSP; dPOPTOPssrl;
1669 if (SvTRUE(left) != SvTRUE(right))
1679 register I32 cxix = dopoptosub(cxstack_ix);
1680 register const PERL_CONTEXT *cx;
1681 register const PERL_CONTEXT *ccstack = cxstack;
1682 const PERL_SI *top_si = PL_curstackinfo;
1684 const char *stashname;
1691 /* we may be in a higher stacklevel, so dig down deeper */
1692 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1693 top_si = top_si->si_prev;
1694 ccstack = top_si->si_cxstack;
1695 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1698 if (GIMME != G_ARRAY) {
1704 /* caller() should not report the automatic calls to &DB::sub */
1705 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1706 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1710 cxix = dopoptosub_at(ccstack, cxix - 1);
1713 cx = &ccstack[cxix];
1714 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1715 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1716 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1717 field below is defined for any cx. */
1718 /* caller() should not report the automatic calls to &DB::sub */
1719 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1720 cx = &ccstack[dbcxix];
1723 stashname = CopSTASHPV(cx->blk_oldcop);
1724 if (GIMME != G_ARRAY) {
1727 PUSHs(&PL_sv_undef);
1730 sv_setpv(TARG, stashname);
1739 PUSHs(&PL_sv_undef);
1741 mPUSHs(newSVpv(stashname, 0));
1742 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1743 mPUSHi((I32)CopLINE(cx->blk_oldcop));
1746 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1747 GV * const cvgv = CvGV(ccstack[cxix].blk_sub.cv);
1748 /* So is ccstack[dbcxix]. */
1750 SV * const sv = newSV(0);
1751 gv_efullname3(sv, cvgv, NULL);
1753 PUSHs(boolSV(CxHASARGS(cx)));
1756 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
1757 PUSHs(boolSV(CxHASARGS(cx)));
1761 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
1764 gimme = (I32)cx->blk_gimme;
1765 if (gimme == G_VOID)
1766 PUSHs(&PL_sv_undef);
1768 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
1769 if (CxTYPE(cx) == CXt_EVAL) {
1771 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
1772 PUSHs(cx->blk_eval.cur_text);
1776 else if (cx->blk_eval.old_namesv) {
1777 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
1780 /* eval BLOCK (try blocks have old_namesv == 0) */
1782 PUSHs(&PL_sv_undef);
1783 PUSHs(&PL_sv_undef);
1787 PUSHs(&PL_sv_undef);
1788 PUSHs(&PL_sv_undef);
1790 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
1791 && CopSTASH_eq(PL_curcop, PL_debstash))
1793 AV * const ary = cx->blk_sub.argarray;
1794 const int off = AvARRAY(ary) - AvALLOC(ary);
1797 PL_dbargs = GvAV(gv_AVadd(gv_fetchpvs("DB::args", GV_ADDMULTI,
1799 AvREAL_off(PL_dbargs); /* XXX should be REIFY (see av.h) */
1802 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1803 av_extend(PL_dbargs, AvFILLp(ary) + off);
1804 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1805 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1807 /* XXX only hints propagated via op_private are currently
1808 * visible (others are not easily accessible, since they
1809 * use the global PL_hints) */
1810 mPUSHi(CopHINTS_get(cx->blk_oldcop));
1813 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1815 if (old_warnings == pWARN_NONE ||
1816 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1817 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1818 else if (old_warnings == pWARN_ALL ||
1819 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1820 /* Get the bit mask for $warnings::Bits{all}, because
1821 * it could have been extended by warnings::register */
1823 HV * const bits = get_hv("warnings::Bits", 0);
1824 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
1825 mask = newSVsv(*bits_all);
1828 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1832 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
1836 PUSHs(cx->blk_oldcop->cop_hints_hash ?
1837 sv_2mortal(newRV_noinc(
1838 MUTABLE_SV(Perl_refcounted_he_chain_2hv(aTHX_
1839 cx->blk_oldcop->cop_hints_hash))))
1848 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
1849 sv_reset(tmps, CopSTASH(PL_curcop));
1854 /* like pp_nextstate, but used instead when the debugger is active */
1859 PL_curcop = (COP*)PL_op;
1860 TAINT_NOT; /* Each statement is presumed innocent */
1861 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1864 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1865 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1868 register PERL_CONTEXT *cx;
1869 const I32 gimme = G_ARRAY;
1871 GV * const gv = PL_DBgv;
1872 register CV * const cv = GvCV(gv);
1875 DIE(aTHX_ "No DB::DB routine defined");
1877 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1878 /* don't do recursive DB::DB call */
1881 ENTER_with_name("sub");
1893 (void)(*CvXSUB(cv))(aTHX_ cv);
1896 LEAVE_with_name("sub");
1900 PUSHBLOCK(cx, CXt_SUB, SP);
1902 cx->blk_sub.retop = PL_op->op_next;
1905 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1906 RETURNOP(CvSTART(cv));
1916 register PERL_CONTEXT *cx;
1917 const I32 gimme = GIMME_V;
1919 U8 cxtype = CXt_LOOP_FOR;
1924 ENTER_with_name("loop1");
1927 if (PL_op->op_targ) {
1928 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
1929 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
1930 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
1931 SVs_PADSTALE, SVs_PADSTALE);
1933 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
1934 #ifndef USE_ITHREADS
1935 svp = &PAD_SVl(PL_op->op_targ); /* "my" variable */
1941 GV * const gv = MUTABLE_GV(POPs);
1942 svp = &GvSV(gv); /* symbol table variable */
1943 SAVEGENERICSV(*svp);
1946 iterdata = (PAD*)gv;
1950 if (PL_op->op_private & OPpITER_DEF)
1951 cxtype |= CXp_FOR_DEF;
1953 ENTER_with_name("loop2");
1955 PUSHBLOCK(cx, cxtype, SP);
1957 PUSHLOOP_FOR(cx, iterdata, MARK, PL_op->op_targ);
1959 PUSHLOOP_FOR(cx, svp, MARK, 0);
1961 if (PL_op->op_flags & OPf_STACKED) {
1962 SV *maybe_ary = POPs;
1963 if (SvTYPE(maybe_ary) != SVt_PVAV) {
1965 SV * const right = maybe_ary;
1968 if (RANGE_IS_NUMERIC(sv,right)) {
1969 cx->cx_type &= ~CXTYPEMASK;
1970 cx->cx_type |= CXt_LOOP_LAZYIV;
1971 /* Make sure that no-one re-orders cop.h and breaks our
1973 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
1974 #ifdef NV_PRESERVES_UV
1975 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
1976 (SvNV(sv) > (NV)IV_MAX)))
1978 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
1979 (SvNV(right) < (NV)IV_MIN))))
1981 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
1984 ((SvUV(sv) > (UV)IV_MAX) ||
1985 (SvNV(sv) > (NV)UV_MAX)))))
1987 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
1989 ((SvNV(right) > 0) &&
1990 ((SvUV(right) > (UV)IV_MAX) ||
1991 (SvNV(right) > (NV)UV_MAX))))))
1993 DIE(aTHX_ "Range iterator outside integer range");
1994 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
1995 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
1997 /* for correct -Dstv display */
1998 cx->blk_oldsp = sp - PL_stack_base;
2002 cx->cx_type &= ~CXTYPEMASK;
2003 cx->cx_type |= CXt_LOOP_LAZYSV;
2004 /* Make sure that no-one re-orders cop.h and breaks our
2006 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2007 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2008 cx->blk_loop.state_u.lazysv.end = right;
2009 SvREFCNT_inc(right);
2010 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
2011 /* This will do the upgrade to SVt_PV, and warn if the value
2012 is uninitialised. */
2013 (void) SvPV_nolen_const(right);
2014 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2015 to replace !SvOK() with a pointer to "". */
2017 SvREFCNT_dec(right);
2018 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
2022 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
2023 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
2024 SvREFCNT_inc(maybe_ary);
2025 cx->blk_loop.state_u.ary.ix =
2026 (PL_op->op_private & OPpITER_REVERSED) ?
2027 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2031 else { /* iterating over items on the stack */
2032 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
2033 if (PL_op->op_private & OPpITER_REVERSED) {
2034 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
2037 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
2047 register PERL_CONTEXT *cx;
2048 const I32 gimme = GIMME_V;
2050 ENTER_with_name("loop1");
2052 ENTER_with_name("loop2");
2054 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2055 PUSHLOOP_PLAIN(cx, SP);
2063 register PERL_CONTEXT *cx;
2070 assert(CxTYPE_is_LOOP(cx));
2072 newsp = PL_stack_base + cx->blk_loop.resetsp;
2075 if (gimme == G_VOID)
2077 else if (gimme == G_SCALAR) {
2079 *++newsp = sv_mortalcopy(*SP);
2081 *++newsp = &PL_sv_undef;
2085 *++newsp = sv_mortalcopy(*++mark);
2086 TAINT_NOT; /* Each item is independent */
2092 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
2093 PL_curpm = newpm; /* ... and pop $1 et al */
2095 LEAVE_with_name("loop2");
2096 LEAVE_with_name("loop1");
2104 register PERL_CONTEXT *cx;
2105 bool popsub2 = FALSE;
2106 bool clear_errsv = FALSE;
2114 const I32 cxix = dopoptosub(cxstack_ix);
2117 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2118 * sort block, which is a CXt_NULL
2121 PL_stack_base[1] = *PL_stack_sp;
2122 PL_stack_sp = PL_stack_base + 1;
2126 DIE(aTHX_ "Can't return outside a subroutine");
2128 if (cxix < cxstack_ix)
2131 if (CxMULTICALL(&cxstack[cxix])) {
2132 gimme = cxstack[cxix].blk_gimme;
2133 if (gimme == G_VOID)
2134 PL_stack_sp = PL_stack_base;
2135 else if (gimme == G_SCALAR) {
2136 PL_stack_base[1] = *PL_stack_sp;
2137 PL_stack_sp = PL_stack_base + 1;
2143 switch (CxTYPE(cx)) {
2146 retop = cx->blk_sub.retop;
2147 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2150 if (!(PL_in_eval & EVAL_KEEPERR))
2153 retop = cx->blk_eval.retop;
2157 if (optype == OP_REQUIRE &&
2158 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2160 /* Unassume the success we assumed earlier. */
2161 SV * const nsv = cx->blk_eval.old_namesv;
2162 (void)hv_delete(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv), G_DISCARD);
2163 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(nsv));
2168 retop = cx->blk_sub.retop;
2171 DIE(aTHX_ "panic: return");
2175 if (gimme == G_SCALAR) {
2178 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2180 *++newsp = SvREFCNT_inc(*SP);
2185 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2187 *++newsp = sv_mortalcopy(sv);
2192 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
2195 *++newsp = sv_mortalcopy(*SP);
2198 *++newsp = &PL_sv_undef;
2200 else if (gimme == G_ARRAY) {
2201 while (++MARK <= SP) {
2202 *++newsp = (popsub2 && SvTEMP(*MARK))
2203 ? *MARK : sv_mortalcopy(*MARK);
2204 TAINT_NOT; /* Each item is independent */
2207 PL_stack_sp = newsp;
2210 /* Stack values are safe: */
2213 POPSUB(cx,sv); /* release CV and @_ ... */
2217 PL_curpm = newpm; /* ... and pop $1 et al */
2230 register PERL_CONTEXT *cx;
2241 if (PL_op->op_flags & OPf_SPECIAL) {
2242 cxix = dopoptoloop(cxstack_ix);
2244 DIE(aTHX_ "Can't \"last\" outside a loop block");
2247 cxix = dopoptolabel(cPVOP->op_pv);
2249 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
2251 if (cxix < cxstack_ix)
2255 cxstack_ix++; /* temporarily protect top context */
2257 switch (CxTYPE(cx)) {
2258 case CXt_LOOP_LAZYIV:
2259 case CXt_LOOP_LAZYSV:
2261 case CXt_LOOP_PLAIN:
2263 newsp = PL_stack_base + cx->blk_loop.resetsp;
2264 nextop = cx->blk_loop.my_op->op_lastop->op_next;
2268 nextop = cx->blk_sub.retop;
2272 nextop = cx->blk_eval.retop;
2276 nextop = cx->blk_sub.retop;
2279 DIE(aTHX_ "panic: last");
2283 if (gimme == G_SCALAR) {
2285 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2286 ? *SP : sv_mortalcopy(*SP);
2288 *++newsp = &PL_sv_undef;
2290 else if (gimme == G_ARRAY) {
2291 while (++MARK <= SP) {
2292 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2293 ? *MARK : sv_mortalcopy(*MARK);
2294 TAINT_NOT; /* Each item is independent */
2302 /* Stack values are safe: */
2304 case CXt_LOOP_LAZYIV:
2305 case CXt_LOOP_PLAIN:
2306 case CXt_LOOP_LAZYSV:
2308 POPLOOP(cx); /* release loop vars ... */
2312 POPSUB(cx,sv); /* release CV and @_ ... */
2315 PL_curpm = newpm; /* ... and pop $1 et al */
2318 PERL_UNUSED_VAR(optype);
2319 PERL_UNUSED_VAR(gimme);
2327 register PERL_CONTEXT *cx;
2330 if (PL_op->op_flags & OPf_SPECIAL) {
2331 cxix = dopoptoloop(cxstack_ix);
2333 DIE(aTHX_ "Can't \"next\" outside a loop block");
2336 cxix = dopoptolabel(cPVOP->op_pv);
2338 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2340 if (cxix < cxstack_ix)
2343 /* clear off anything above the scope we're re-entering, but
2344 * save the rest until after a possible continue block */
2345 inner = PL_scopestack_ix;
2347 if (PL_scopestack_ix < inner)
2348 leave_scope(PL_scopestack[PL_scopestack_ix]);
2349 PL_curcop = cx->blk_oldcop;
2350 return CX_LOOP_NEXTOP_GET(cx);
2357 register PERL_CONTEXT *cx;
2361 if (PL_op->op_flags & OPf_SPECIAL) {
2362 cxix = dopoptoloop(cxstack_ix);
2364 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2367 cxix = dopoptolabel(cPVOP->op_pv);
2369 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2371 if (cxix < cxstack_ix)
2374 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
2375 if (redo_op->op_type == OP_ENTER) {
2376 /* pop one less context to avoid $x being freed in while (my $x..) */
2378 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2379 redo_op = redo_op->op_next;
2383 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2384 LEAVE_SCOPE(oldsave);
2386 PL_curcop = cx->blk_oldcop;
2391 S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
2395 static const char too_deep[] = "Target of goto is too deeply nested";
2397 PERL_ARGS_ASSERT_DOFINDLABEL;
2400 Perl_croak(aTHX_ too_deep);
2401 if (o->op_type == OP_LEAVE ||
2402 o->op_type == OP_SCOPE ||
2403 o->op_type == OP_LEAVELOOP ||
2404 o->op_type == OP_LEAVESUB ||
2405 o->op_type == OP_LEAVETRY)
2407 *ops++ = cUNOPo->op_first;
2409 Perl_croak(aTHX_ too_deep);
2412 if (o->op_flags & OPf_KIDS) {
2414 /* First try all the kids at this level, since that's likeliest. */
2415 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2416 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
2417 CopLABEL(kCOP) && strEQ(CopLABEL(kCOP), label))
2420 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2421 if (kid == PL_lastgotoprobe)
2423 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2426 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2427 ops[-1]->op_type == OP_DBSTATE)
2432 if ((o = dofindlabel(kid, label, ops, oplimit)))
2445 register PERL_CONTEXT *cx;
2446 #define GOTO_DEPTH 64
2447 OP *enterops[GOTO_DEPTH];
2448 const char *label = NULL;
2449 const bool do_dump = (PL_op->op_type == OP_DUMP);
2450 static const char must_have_label[] = "goto must have label";
2452 if (PL_op->op_flags & OPf_STACKED) {
2453 SV * const sv = POPs;
2455 /* This egregious kludge implements goto &subroutine */
2456 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2458 register PERL_CONTEXT *cx;
2459 CV *cv = MUTABLE_CV(SvRV(sv));
2466 if (!CvROOT(cv) && !CvXSUB(cv)) {
2467 const GV * const gv = CvGV(cv);
2471 /* autoloaded stub? */
2472 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2474 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2475 GvNAMELEN(gv), FALSE);
2476 if (autogv && (cv = GvCV(autogv)))
2478 tmpstr = sv_newmortal();
2479 gv_efullname3(tmpstr, gv, NULL);
2480 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
2482 DIE(aTHX_ "Goto undefined subroutine");
2485 /* First do some returnish stuff. */
2486 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2488 cxix = dopoptosub(cxstack_ix);
2490 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2491 if (cxix < cxstack_ix)
2495 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2496 if (CxTYPE(cx) == CXt_EVAL) {
2498 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2500 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2502 else if (CxMULTICALL(cx))
2503 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2504 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
2505 /* put @_ back onto stack */
2506 AV* av = cx->blk_sub.argarray;
2508 items = AvFILLp(av) + 1;
2509 EXTEND(SP, items+1); /* @_ could have been extended. */
2510 Copy(AvARRAY(av), SP + 1, items, SV*);
2511 SvREFCNT_dec(GvAV(PL_defgv));
2512 GvAV(PL_defgv) = cx->blk_sub.savearray;
2514 /* abandon @_ if it got reified */
2519 av_extend(av, items-1);
2521 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
2524 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2525 AV* const av = GvAV(PL_defgv);
2526 items = AvFILLp(av) + 1;
2527 EXTEND(SP, items+1); /* @_ could have been extended. */
2528 Copy(AvARRAY(av), SP + 1, items, SV*);
2532 if (CxTYPE(cx) == CXt_SUB &&
2533 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2534 SvREFCNT_dec(cx->blk_sub.cv);
2535 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2536 LEAVE_SCOPE(oldsave);
2538 /* Now do some callish stuff. */
2540 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2542 OP* const retop = cx->blk_sub.retop;
2547 for (index=0; index<items; index++)
2548 sv_2mortal(SP[-index]);
2551 /* XS subs don't have a CxSUB, so pop it */
2552 POPBLOCK(cx, PL_curpm);
2553 /* Push a mark for the start of arglist */
2556 (void)(*CvXSUB(cv))(aTHX_ cv);
2557 LEAVE_with_name("sub");
2561 AV* const padlist = CvPADLIST(cv);
2562 if (CxTYPE(cx) == CXt_EVAL) {
2563 PL_in_eval = CxOLD_IN_EVAL(cx);
2564 PL_eval_root = cx->blk_eval.old_eval_root;
2565 cx->cx_type = CXt_SUB;
2567 cx->blk_sub.cv = cv;
2568 cx->blk_sub.olddepth = CvDEPTH(cv);
2571 if (CvDEPTH(cv) < 2)
2572 SvREFCNT_inc_simple_void_NN(cv);
2574 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
2575 sub_crush_depth(cv);
2576 pad_push(padlist, CvDEPTH(cv));
2579 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2582 AV *const av = MUTABLE_AV(PAD_SVl(0));
2584 cx->blk_sub.savearray = GvAV(PL_defgv);
2585 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
2586 CX_CURPAD_SAVE(cx->blk_sub);
2587 cx->blk_sub.argarray = av;
2589 if (items >= AvMAX(av) + 1) {
2590 SV **ary = AvALLOC(av);
2591 if (AvARRAY(av) != ary) {
2592 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2595 if (items >= AvMAX(av) + 1) {
2596 AvMAX(av) = items - 1;
2597 Renew(ary,items+1,SV*);
2603 Copy(mark,AvARRAY(av),items,SV*);
2604 AvFILLp(av) = items - 1;
2605 assert(!AvREAL(av));
2607 /* transfer 'ownership' of refcnts to new @_ */
2617 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2618 Perl_get_db_sub(aTHX_ NULL, cv);
2620 CV * const gotocv = get_cvs("DB::goto", 0);
2622 PUSHMARK( PL_stack_sp );
2623 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
2628 RETURNOP(CvSTART(cv));
2632 label = SvPV_nolen_const(sv);
2633 if (!(do_dump || *label))
2634 DIE(aTHX_ must_have_label);
2637 else if (PL_op->op_flags & OPf_SPECIAL) {
2639 DIE(aTHX_ must_have_label);
2642 label = cPVOP->op_pv;
2644 if (label && *label) {
2645 OP *gotoprobe = NULL;
2646 bool leaving_eval = FALSE;
2647 bool in_block = FALSE;
2648 PERL_CONTEXT *last_eval_cx = NULL;
2652 PL_lastgotoprobe = NULL;
2654 for (ix = cxstack_ix; ix >= 0; ix--) {
2656 switch (CxTYPE(cx)) {
2658 leaving_eval = TRUE;
2659 if (!CxTRYBLOCK(cx)) {
2660 gotoprobe = (last_eval_cx ?
2661 last_eval_cx->blk_eval.old_eval_root :
2666 /* else fall through */
2667 case CXt_LOOP_LAZYIV:
2668 case CXt_LOOP_LAZYSV:
2670 case CXt_LOOP_PLAIN:
2673 gotoprobe = cx->blk_oldcop->op_sibling;
2679 gotoprobe = cx->blk_oldcop->op_sibling;
2682 gotoprobe = PL_main_root;
2685 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
2686 gotoprobe = CvROOT(cx->blk_sub.cv);
2692 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2695 DIE(aTHX_ "panic: goto");
2696 gotoprobe = PL_main_root;
2700 retop = dofindlabel(gotoprobe, label,
2701 enterops, enterops + GOTO_DEPTH);
2705 PL_lastgotoprobe = gotoprobe;
2708 DIE(aTHX_ "Can't find label %s", label);
2710 /* if we're leaving an eval, check before we pop any frames
2711 that we're not going to punt, otherwise the error
2714 if (leaving_eval && *enterops && enterops[1]) {
2716 for (i = 1; enterops[i]; i++)
2717 if (enterops[i]->op_type == OP_ENTERITER)
2718 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2721 if (*enterops && enterops[1]) {
2722 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2724 deprecate("\"goto\" to jump into a construct");
2727 /* pop unwanted frames */
2729 if (ix < cxstack_ix) {
2736 oldsave = PL_scopestack[PL_scopestack_ix];
2737 LEAVE_SCOPE(oldsave);
2740 /* push wanted frames */
2742 if (*enterops && enterops[1]) {
2743 OP * const oldop = PL_op;
2744 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2745 for (; enterops[ix]; ix++) {
2746 PL_op = enterops[ix];
2747 /* Eventually we may want to stack the needed arguments
2748 * for each op. For now, we punt on the hard ones. */
2749 if (PL_op->op_type == OP_ENTERITER)
2750 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2751 CALL_FPTR(PL_op->op_ppaddr)(aTHX);
2759 if (!retop) retop = PL_main_start;
2761 PL_restartop = retop;
2762 PL_do_undump = TRUE;
2766 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2767 PL_do_undump = FALSE;
2784 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2786 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
2789 PL_exit_flags |= PERL_EXIT_EXPECTED;
2791 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2792 if (anum || !(PL_minus_c && PL_madskills))
2797 PUSHs(&PL_sv_undef);
2804 S_save_lines(pTHX_ AV *array, SV *sv)
2806 const char *s = SvPVX_const(sv);
2807 const char * const send = SvPVX_const(sv) + SvCUR(sv);
2810 PERL_ARGS_ASSERT_SAVE_LINES;
2812 while (s && s < send) {
2814 SV * const tmpstr = newSV_type(SVt_PVMG);
2816 t = (const char *)memchr(s, '\n', send - s);
2822 sv_setpvn(tmpstr, s, t - s);
2823 av_store(array, line++, tmpstr);
2829 S_docatch(pTHX_ OP *o)
2833 OP * const oldop = PL_op;
2837 assert(CATCH_GET == TRUE);
2844 assert(cxstack_ix >= 0);
2845 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2846 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
2851 /* die caught by an inner eval - continue inner loop */
2853 /* NB XXX we rely on the old popped CxEVAL still being at the top
2854 * of the stack; the way die_where() currently works, this
2855 * assumption is valid. In theory The cur_top_env value should be
2856 * returned in another global, the way retop (aka PL_restartop)
2858 assert(CxTYPE(&cxstack[cxstack_ix+1]) == CXt_EVAL);
2861 && cxstack[cxstack_ix+1].blk_eval.cur_top_env == PL_top_env)
2863 PL_op = PL_restartop;
2880 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
2881 /* sv Text to convert to OP tree. */
2882 /* startop op_free() this to undo. */
2883 /* code Short string id of the caller. */
2885 /* FIXME - how much of this code is common with pp_entereval? */
2886 dVAR; dSP; /* Make POPBLOCK work. */
2892 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2893 char *tmpbuf = tbuf;
2896 CV* runcv = NULL; /* initialise to avoid compiler warnings */
2899 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
2901 ENTER_with_name("eval");
2902 lex_start(sv, NULL, FALSE);
2904 /* switch to eval mode */
2906 if (IN_PERL_COMPILETIME) {
2907 SAVECOPSTASH_FREE(&PL_compiling);
2908 CopSTASH_set(&PL_compiling, PL_curstash);
2910 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
2911 SV * const sv = sv_newmortal();
2912 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
2913 code, (unsigned long)++PL_evalseq,
2914 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
2919 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
2920 (unsigned long)++PL_evalseq);
2921 SAVECOPFILE_FREE(&PL_compiling);
2922 CopFILE_set(&PL_compiling, tmpbuf+2);
2923 SAVECOPLINE(&PL_compiling);
2924 CopLINE_set(&PL_compiling, 1);
2925 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2926 deleting the eval's FILEGV from the stash before gv_check() runs
2927 (i.e. before run-time proper). To work around the coredump that
2928 ensues, we always turn GvMULTI_on for any globals that were
2929 introduced within evals. See force_ident(). GSAR 96-10-12 */
2930 safestr = savepvn(tmpbuf, len);
2931 SAVEDELETE(PL_defstash, safestr, len);
2933 #ifdef OP_IN_REGISTER
2939 /* we get here either during compilation, or via pp_regcomp at runtime */
2940 runtime = IN_PERL_RUNTIME;
2942 runcv = find_runcv(NULL);
2945 PL_op->op_type = OP_ENTEREVAL;
2946 PL_op->op_flags = 0; /* Avoid uninit warning. */
2947 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
2951 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
2953 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
2954 POPBLOCK(cx,PL_curpm);
2957 (*startop)->op_type = OP_NULL;
2958 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
2960 /* XXX DAPM do this properly one year */
2961 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
2962 LEAVE_with_name("eval");
2963 if (IN_PERL_COMPILETIME)
2964 CopHINTS_set(&PL_compiling, PL_hints);
2965 #ifdef OP_IN_REGISTER
2968 PERL_UNUSED_VAR(newsp);
2969 PERL_UNUSED_VAR(optype);
2971 return PL_eval_start;
2976 =for apidoc find_runcv
2978 Locate the CV corresponding to the currently executing sub or eval.
2979 If db_seqp is non_null, skip CVs that are in the DB package and populate
2980 *db_seqp with the cop sequence number at the point that the DB:: code was
2981 entered. (allows debuggers to eval in the scope of the breakpoint rather
2982 than in the scope of the debugger itself).
2988 Perl_find_runcv(pTHX_ U32 *db_seqp)
2994 *db_seqp = PL_curcop->cop_seq;
2995 for (si = PL_curstackinfo; si; si = si->si_prev) {
2997 for (ix = si->si_cxix; ix >= 0; ix--) {
2998 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
2999 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
3000 CV * const cv = cx->blk_sub.cv;
3001 /* skip DB:: code */
3002 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3003 *db_seqp = cx->blk_oldcop->cop_seq;
3008 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3016 /* Compile a require/do, an eval '', or a /(?{...})/.
3017 * In the last case, startop is non-null, and contains the address of
3018 * a pointer that should be set to the just-compiled code.
3019 * outside is the lexically enclosing CV (if any) that invoked us.
3020 * Returns a bool indicating whether the compile was successful; if so,
3021 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3022 * pushes undef (also croaks if startop != NULL).
3026 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
3029 OP * const saveop = PL_op;
3031 PL_in_eval = ((saveop && saveop->op_type == OP_REQUIRE)
3032 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3037 SAVESPTR(PL_compcv);
3038 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3039 CvEVAL_on(PL_compcv);
3040 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3041 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3043 CvOUTSIDE_SEQ(PL_compcv) = seq;
3044 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
3046 /* set up a scratch pad */
3048 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
3049 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
3053 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
3055 /* make sure we compile in the right package */
3057 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3058 SAVESPTR(PL_curstash);
3059 PL_curstash = CopSTASH(PL_curcop);
3061 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3062 SAVESPTR(PL_beginav);
3063 PL_beginav = newAV();
3064 SAVEFREESV(PL_beginav);
3065 SAVESPTR(PL_unitcheckav);
3066 PL_unitcheckav = newAV();
3067 SAVEFREESV(PL_unitcheckav);
3070 SAVEBOOL(PL_madskills);
3074 /* try to compile it */
3076 PL_eval_root = NULL;
3077 PL_curcop = &PL_compiling;
3078 CopARYBASE_set(PL_curcop, 0);
3079 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
3080 PL_in_eval |= EVAL_KEEPERR;
3083 if (yyparse() || PL_parser->error_count || !PL_eval_root) {
3084 SV **newsp; /* Used by POPBLOCK. */
3085 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
3086 I32 optype = 0; /* Might be reset by POPEVAL. */
3091 op_free(PL_eval_root);
3092 PL_eval_root = NULL;
3094 SP = PL_stack_base + POPMARK; /* pop original mark */
3096 POPBLOCK(cx,PL_curpm);
3100 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
3102 msg = SvPVx_nolen_const(ERRSV);
3103 if (optype == OP_REQUIRE) {
3104 const SV * const nsv = cx->blk_eval.old_namesv;
3105 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
3107 Perl_croak(aTHX_ "%sCompilation failed in require",
3108 *msg ? msg : "Unknown error\n");
3111 POPBLOCK(cx,PL_curpm);
3113 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3114 (*msg ? msg : "Unknown error\n"));
3118 sv_setpvs(ERRSV, "Compilation error");
3121 PERL_UNUSED_VAR(newsp);
3122 PUSHs(&PL_sv_undef);
3126 CopLINE_set(&PL_compiling, 0);
3128 *startop = PL_eval_root;
3130 SAVEFREEOP(PL_eval_root);
3132 /* Set the context for this new optree.
3133 * Propagate the context from the eval(). */
3134 if ((gimme & G_WANT) == G_VOID)
3135 scalarvoid(PL_eval_root);
3136 else if ((gimme & G_WANT) == G_ARRAY)
3139 scalar(PL_eval_root);
3141 DEBUG_x(dump_eval());
3143 /* Register with debugger: */
3144 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
3145 CV * const cv = get_cvs("DB::postponed", 0);
3149 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3151 call_sv(MUTABLE_SV(cv), G_DISCARD);
3156 call_list(PL_scopestack_ix, PL_unitcheckav);
3158 /* compiled okay, so do it */
3160 CvDEPTH(PL_compcv) = 1;
3161 SP = PL_stack_base + POPMARK; /* pop original mark */
3162 PL_op = saveop; /* The caller may need it. */
3163 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
3170 S_check_type_and_open(pTHX_ const char *name)
3173 const int st_rc = PerlLIO_stat(name, &st);
3175 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3177 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3181 return PerlIO_open(name, PERL_SCRIPT_MODE);
3184 #ifndef PERL_DISABLE_PMC
3186 S_doopen_pm(pTHX_ const char *name, const STRLEN namelen)
3190 PERL_ARGS_ASSERT_DOOPEN_PM;
3192 if (namelen > 3 && memEQs(name + namelen - 3, 3, ".pm")) {
3193 SV *const pmcsv = newSV(namelen + 2);
3194 char *const pmc = SvPVX(pmcsv);
3197 memcpy(pmc, name, namelen);
3199 pmc[namelen + 1] = '\0';
3201 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
3202 fp = check_type_and_open(name);
3205 fp = check_type_and_open(pmc);
3207 SvREFCNT_dec(pmcsv);
3210 fp = check_type_and_open(name);
3215 # define doopen_pm(name, namelen) check_type_and_open(name)
3216 #endif /* !PERL_DISABLE_PMC */
3221 register PERL_CONTEXT *cx;
3228 int vms_unixname = 0;
3230 const char *tryname = NULL;
3232 const I32 gimme = GIMME_V;
3233 int filter_has_file = 0;
3234 PerlIO *tryrsfp = NULL;
3235 SV *filter_cache = NULL;
3236 SV *filter_state = NULL;
3237 SV *filter_sub = NULL;
3243 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3244 sv = new_version(sv);
3245 if (!sv_derived_from(PL_patchlevel, "version"))
3246 upg_version(PL_patchlevel, TRUE);
3247 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3248 if ( vcmp(sv,PL_patchlevel) <= 0 )
3249 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3250 SVfARG(vnormal(sv)), SVfARG(vnormal(PL_patchlevel)));
3253 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3256 SV * const req = SvRV(sv);
3257 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
3259 /* get the left hand term */
3260 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
3262 first = SvIV(*av_fetch(lav,0,0));
3263 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
3264 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
3265 || av_len(lav) > 1 /* FP with > 3 digits */
3266 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3268 DIE(aTHX_ "Perl %"SVf" required--this is only "
3269 "%"SVf", stopped", SVfARG(vnormal(req)),
3270 SVfARG(vnormal(PL_patchlevel)));
3272 else { /* probably 'use 5.10' or 'use 5.8' */
3273 SV * hintsv = newSV(0);
3277 second = SvIV(*av_fetch(lav,1,0));
3279 second /= second >= 600 ? 100 : 10;
3280 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.%d",
3281 (int)first, (int)second,0);
3282 upg_version(hintsv, TRUE);
3284 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3285 "--this is only %"SVf", stopped",
3286 SVfARG(vnormal(req)),
3287 SVfARG(vnormal(hintsv)),
3288 SVfARG(vnormal(PL_patchlevel)));
3293 /* We do this only with use, not require. */
3295 /* If we request a version >= 5.9.5, load feature.pm with the
3296 * feature bundle that corresponds to the required version. */
3297 vcmp(sv, sv_2mortal(upg_version(newSVnv(5.009005), FALSE))) >= 0) {
3298 SV *const importsv = vnormal(sv);
3299 *SvPVX_mutable(importsv) = ':';
3300 ENTER_with_name("load_feature");
3301 Perl_load_module(aTHX_ 0, newSVpvs("feature"), NULL, importsv, NULL);
3302 LEAVE_with_name("load_feature");
3304 /* If a version >= 5.11.0 is requested, strictures are on by default! */
3306 vcmp(sv, sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
3307 PL_hints |= (HINT_STRICT_REFS | HINT_STRICT_SUBS | HINT_STRICT_VARS);
3312 name = SvPV_const(sv, len);
3313 if (!(name && len > 0 && *name))
3314 DIE(aTHX_ "Null filename used");
3315 TAINT_PROPER("require");
3319 /* The key in the %ENV hash is in the syntax of file passed as the argument
3320 * usually this is in UNIX format, but sometimes in VMS format, which
3321 * can result in a module being pulled in more than once.
3322 * To prevent this, the key must be stored in UNIX format if the VMS
3323 * name can be translated to UNIX.
3325 if ((unixname = tounixspec(name, NULL)) != NULL) {
3326 unixlen = strlen(unixname);
3332 /* if not VMS or VMS name can not be translated to UNIX, pass it
3335 unixname = (char *) name;
3338 if (PL_op->op_type == OP_REQUIRE) {
3339 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3340 unixname, unixlen, 0);
3342 if (*svp != &PL_sv_undef)
3345 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3346 "Compilation failed in require", unixname);
3350 /* prepare to compile file */
3352 if (path_is_absolute(name)) {
3354 tryrsfp = doopen_pm(name, len);
3357 AV * const ar = GvAVn(PL_incgv);
3363 namesv = newSV_type(SVt_PV);
3364 for (i = 0; i <= AvFILL(ar); i++) {
3365 SV * const dirsv = *av_fetch(ar, i, TRUE);
3367 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
3374 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3375 && !sv_isobject(loader))
3377 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
3380 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3381 PTR2UV(SvRV(dirsv)), name);
3382 tryname = SvPVX_const(namesv);
3385 ENTER_with_name("call_INC");
3393 if (sv_isobject(loader))
3394 count = call_method("INC", G_ARRAY);
3396 count = call_sv(loader, G_ARRAY);
3399 /* Adjust file name if the hook has set an %INC entry */
3400 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3402 tryname = SvPV_nolen_const(*svp);
3411 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3412 && !isGV_with_GP(SvRV(arg))) {
3413 filter_cache = SvRV(arg);
3414 SvREFCNT_inc_simple_void_NN(filter_cache);
3421 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
3425 if (isGV_with_GP(arg)) {
3426 IO * const io = GvIO((const GV *)arg);
3431 tryrsfp = IoIFP(io);
3432 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3433 PerlIO_close(IoOFP(io));
3444 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3446 SvREFCNT_inc_simple_void_NN(filter_sub);
3449 filter_state = SP[i];
3450 SvREFCNT_inc_simple_void(filter_state);
3454 if (!tryrsfp && (filter_cache || filter_sub)) {
3455 tryrsfp = PerlIO_open(BIT_BUCKET,
3463 LEAVE_with_name("call_INC");
3470 filter_has_file = 0;
3472 SvREFCNT_dec(filter_cache);
3473 filter_cache = NULL;
3476 SvREFCNT_dec(filter_state);
3477 filter_state = NULL;
3480 SvREFCNT_dec(filter_sub);
3485 if (!path_is_absolute(name)
3491 dir = SvPV_const(dirsv, dirlen);
3499 if ((unixdir = tounixpath(dir, NULL)) == NULL)
3501 sv_setpv(namesv, unixdir);
3502 sv_catpv(namesv, unixname);
3504 # ifdef __SYMBIAN32__
3505 if (PL_origfilename[0] &&
3506 PL_origfilename[1] == ':' &&
3507 !(dir[0] && dir[1] == ':'))
3508 Perl_sv_setpvf(aTHX_ namesv,
3513 Perl_sv_setpvf(aTHX_ namesv,
3517 /* The equivalent of
3518 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3519 but without the need to parse the format string, or
3520 call strlen on either pointer, and with the correct
3521 allocation up front. */
3523 char *tmp = SvGROW(namesv, dirlen + len + 2);
3525 memcpy(tmp, dir, dirlen);
3528 /* name came from an SV, so it will have a '\0' at the
3529 end that we can copy as part of this memcpy(). */
3530 memcpy(tmp, name, len + 1);
3532 SvCUR_set(namesv, dirlen + len + 1);
3534 /* Don't even actually have to turn SvPOK_on() as we
3535 access it directly with SvPVX() below. */
3539 TAINT_PROPER("require");
3540 tryname = SvPVX_const(namesv);
3541 tryrsfp = doopen_pm(tryname, SvCUR(namesv));
3543 if (tryname[0] == '.' && tryname[1] == '/') {
3545 while (*++tryname == '/');
3549 else if (errno == EMFILE)
3550 /* no point in trying other paths if out of handles */
3557 SAVECOPFILE_FREE(&PL_compiling);
3558 CopFILE_set(&PL_compiling, tryrsfp ? tryname : name);
3559 SvREFCNT_dec(namesv);
3561 if (PL_op->op_type == OP_REQUIRE) {
3562 const char *msgstr = name;
3563 if(errno == EMFILE) {
3565 = sv_2mortal(Perl_newSVpvf(aTHX_ "%s: %s", msgstr,
3567 msgstr = SvPV_nolen_const(msg);
3569 if (namesv) { /* did we lookup @INC? */
3570 AV * const ar = GvAVn(PL_incgv);
3572 SV * const msg = sv_2mortal(Perl_newSVpvf(aTHX_
3573 "%s in @INC%s%s (@INC contains:",
3575 (instr(msgstr, ".h ")
3576 ? " (change .h to .ph maybe?)" : ""),
3577 (instr(msgstr, ".ph ")
3578 ? " (did you run h2ph?)" : "")
3581 for (i = 0; i <= AvFILL(ar); i++) {
3582 sv_catpvs(msg, " ");
3583 sv_catsv(msg, *av_fetch(ar, i, TRUE));
3585 sv_catpvs(msg, ")");
3586 msgstr = SvPV_nolen_const(msg);
3589 DIE(aTHX_ "Can't locate %s", msgstr);
3595 SETERRNO(0, SS_NORMAL);
3597 /* Assume success here to prevent recursive requirement. */
3598 /* name is never assigned to again, so len is still strlen(name) */
3599 /* Check whether a hook in @INC has already filled %INC */
3601 (void)hv_store(GvHVn(PL_incgv),
3602 unixname, unixlen, newSVpv(CopFILE(&PL_compiling),0),0);
3604 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
3606 (void)hv_store(GvHVn(PL_incgv),
3607 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
3610 ENTER_with_name("eval");
3612 lex_start(NULL, tryrsfp, TRUE);
3616 hv_clear(GvHV(PL_hintgv));
3618 SAVECOMPILEWARNINGS();
3619 if (PL_dowarn & G_WARN_ALL_ON)
3620 PL_compiling.cop_warnings = pWARN_ALL ;
3621 else if (PL_dowarn & G_WARN_ALL_OFF)
3622 PL_compiling.cop_warnings = pWARN_NONE ;
3624 PL_compiling.cop_warnings = pWARN_STD ;
3626 if (filter_sub || filter_cache) {
3627 /* We can use the SvPV of the filter PVIO itself as our cache, rather
3628 than hanging another SV from it. In turn, filter_add() optionally
3629 takes the SV to use as the filter (or creates a new SV if passed
3630 NULL), so simply pass in whatever value filter_cache has. */
3631 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
3632 IoLINES(datasv) = filter_has_file;
3633 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
3634 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
3637 /* switch to eval mode */
3638 PUSHBLOCK(cx, CXt_EVAL, SP);
3640 cx->blk_eval.retop = PL_op->op_next;
3642 SAVECOPLINE(&PL_compiling);
3643 CopLINE_set(&PL_compiling, 0);
3647 /* Store and reset encoding. */
3648 encoding = PL_encoding;
3651 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq))
3652 op = DOCATCH(PL_eval_start);
3654 op = PL_op->op_next;
3656 /* Restore encoding. */
3657 PL_encoding = encoding;
3662 /* This is a op added to hold the hints hash for
3663 pp_entereval. The hash can be modified by the code
3664 being eval'ed, so we return a copy instead. */
3670 mXPUSHs(MUTABLE_SV(Perl_hv_copy_hints_hv(aTHX_ MUTABLE_HV(cSVOP_sv))));
3678 register PERL_CONTEXT *cx;
3680 const I32 gimme = GIMME_V;
3681 const U32 was = PL_breakable_sub_gen;
3682 char tbuf[TYPE_DIGITS(long) + 12];
3683 char *tmpbuf = tbuf;
3687 HV *saved_hh = NULL;
3689 if (PL_op->op_private & OPpEVAL_HAS_HH) {
3690 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
3694 TAINT_IF(SvTAINTED(sv));
3695 TAINT_PROPER("eval");
3697 ENTER_with_name("eval");
3698 lex_start(sv, NULL, FALSE);
3701 /* switch to eval mode */
3703 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3704 SV * const temp_sv = sv_newmortal();
3705 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
3706 (unsigned long)++PL_evalseq,
3707 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3708 tmpbuf = SvPVX(temp_sv);
3709 len = SvCUR(temp_sv);
3712 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
3713 SAVECOPFILE_FREE(&PL_compiling);
3714 CopFILE_set(&PL_compiling, tmpbuf+2);
3715 SAVECOPLINE(&PL_compiling);
3716 CopLINE_set(&PL_compiling, 1);
3717 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3718 deleting the eval's FILEGV from the stash before gv_check() runs
3719 (i.e. before run-time proper). To work around the coredump that
3720 ensues, we always turn GvMULTI_on for any globals that were
3721 introduced within evals. See force_ident(). GSAR 96-10-12 */
3723 PL_hints = PL_op->op_targ;
3725 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3726 SvREFCNT_dec(GvHV(PL_hintgv));
3727 GvHV(PL_hintgv) = saved_hh;
3729 SAVECOMPILEWARNINGS();
3730 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3731 if (PL_compiling.cop_hints_hash) {
3732 Perl_refcounted_he_free(aTHX_ PL_compiling.cop_hints_hash);
3734 PL_compiling.cop_hints_hash = PL_curcop->cop_hints_hash;
3735 if (PL_compiling.cop_hints_hash) {
3737 PL_compiling.cop_hints_hash->refcounted_he_refcnt++;
3738 HINTS_REFCNT_UNLOCK;
3740 /* special case: an eval '' executed within the DB package gets lexically
3741 * placed in the first non-DB CV rather than the current CV - this
3742 * allows the debugger to execute code, find lexicals etc, in the
3743 * scope of the code being debugged. Passing &seq gets find_runcv
3744 * to do the dirty work for us */
3745 runcv = find_runcv(&seq);
3747 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3749 cx->blk_eval.retop = PL_op->op_next;
3751 /* prepare to compile string */
3753 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
3754 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
3757 if (doeval(gimme, NULL, runcv, seq)) {
3758 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3759 ? (PERLDB_LINE || PERLDB_SAVESRC)
3760 : PERLDB_SAVESRC_NOSUBS) {
3761 /* Retain the filegv we created. */
3763 char *const safestr = savepvn(tmpbuf, len);
3764 SAVEDELETE(PL_defstash, safestr, len);
3766 return DOCATCH(PL_eval_start);
3768 /* We have already left the scope set up earler thanks to the LEAVE
3770 if (was != PL_breakable_sub_gen /* Some subs defined here. */
3771 ? (PERLDB_LINE || PERLDB_SAVESRC)
3772 : PERLDB_SAVESRC_INVALID) {
3773 /* Retain the filegv we created. */
3775 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
3777 return PL_op->op_next;
3788 register PERL_CONTEXT *cx;
3790 const U8 save_flags = PL_op -> op_flags;
3795 retop = cx->blk_eval.retop;
3798 if (gimme == G_VOID)
3800 else if (gimme == G_SCALAR) {
3803 if (SvFLAGS(TOPs) & SVs_TEMP)
3806 *MARK = sv_mortalcopy(TOPs);
3810 *MARK = &PL_sv_undef;
3815 /* in case LEAVE wipes old return values */
3816 for (mark = newsp + 1; mark <= SP; mark++) {
3817 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
3818 *mark = sv_mortalcopy(*mark);
3819 TAINT_NOT; /* Each item is independent */
3823 PL_curpm = newpm; /* Don't pop $1 et al till now */
3826 assert(CvDEPTH(PL_compcv) == 1);
3828 CvDEPTH(PL_compcv) = 0;
3831 if (optype == OP_REQUIRE &&
3832 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
3834 /* Unassume the success we assumed earlier. */
3835 SV * const nsv = cx->blk_eval.old_namesv;
3836 (void)hv_delete(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv), G_DISCARD);
3837 retop = Perl_die(aTHX_ "%"SVf" did not return a true value", SVfARG(nsv));
3838 /* die_where() did LEAVE, or we won't be here */
3841 LEAVE_with_name("eval");
3842 if (!(save_flags & OPf_SPECIAL)) {
3850 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
3851 close to the related Perl_create_eval_scope. */
3853 Perl_delete_eval_scope(pTHX)
3858 register PERL_CONTEXT *cx;
3864 LEAVE_with_name("eval_scope");
3865 PERL_UNUSED_VAR(newsp);
3866 PERL_UNUSED_VAR(gimme);
3867 PERL_UNUSED_VAR(optype);
3870 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
3871 also needed by Perl_fold_constants. */
3873 Perl_create_eval_scope(pTHX_ U32 flags)
3876 const I32 gimme = GIMME_V;
3878 ENTER_with_name("eval_scope");
3881 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
3884 PL_in_eval = EVAL_INEVAL;
3885 if (flags & G_KEEPERR)
3886 PL_in_eval |= EVAL_KEEPERR;
3889 if (flags & G_FAKINGEVAL) {
3890 PL_eval_root = PL_op; /* Only needed so that goto works right. */
3898 PERL_CONTEXT * const cx = create_eval_scope(0);
3899 cx->blk_eval.retop = cLOGOP->op_other->op_next;
3900 return DOCATCH(PL_op->op_next);
3909 register PERL_CONTEXT *cx;
3914 PERL_UNUSED_VAR(optype);
3917 if (gimme == G_VOID)
3919 else if (gimme == G_SCALAR) {
3923 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
3926 *MARK = sv_mortalcopy(TOPs);
3930 *MARK = &PL_sv_undef;
3935 /* in case LEAVE wipes old return values */
3937 for (mark = newsp + 1; mark <= SP; mark++) {
3938 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
3939 *mark = sv_mortalcopy(*mark);
3940 TAINT_NOT; /* Each item is independent */
3944 PL_curpm = newpm; /* Don't pop $1 et al till now */
3946 LEAVE_with_name("eval_scope");
3954 register PERL_CONTEXT *cx;
3955 const I32 gimme = GIMME_V;
3957 ENTER_with_name("given");
3960 sv_setsv(PAD_SV(PL_op->op_targ), POPs);
3962 PUSHBLOCK(cx, CXt_GIVEN, SP);
3971 register PERL_CONTEXT *cx;
3975 PERL_UNUSED_CONTEXT;
3978 assert(CxTYPE(cx) == CXt_GIVEN);
3983 PL_curpm = newpm; /* pop $1 et al */
3985 LEAVE_with_name("given");
3990 /* Helper routines used by pp_smartmatch */
3992 S_make_matcher(pTHX_ REGEXP *re)
3995 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
3997 PERL_ARGS_ASSERT_MAKE_MATCHER;
3999 PM_SETRE(matcher, ReREFCNT_inc(re));
4001 SAVEFREEOP((OP *) matcher);
4002 ENTER_with_name("matcher"); SAVETMPS;
4008 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4013 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
4015 PL_op = (OP *) matcher;
4020 return (SvTRUEx(POPs));
4024 S_destroy_matcher(pTHX_ PMOP *matcher)
4028 PERL_ARGS_ASSERT_DESTROY_MATCHER;
4029 PERL_UNUSED_ARG(matcher);
4032 LEAVE_with_name("matcher");
4035 /* Do a smart match */
4038 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
4039 return do_smartmatch(NULL, NULL);
4042 /* This version of do_smartmatch() implements the
4043 * table of smart matches that is found in perlsyn.
4046 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
4051 bool object_on_left = FALSE;
4052 SV *e = TOPs; /* e is for 'expression' */
4053 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
4055 /* First of all, handle overload magic of the rightmost argument */
4058 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4059 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4061 tmpsv = amagic_call(d, e, smart_amg, 0);
4068 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
4071 SP -= 2; /* Pop the values */
4073 /* Take care only to invoke mg_get() once for each argument.
4074 * Currently we do this by copying the SV if it's magical. */
4077 d = sv_mortalcopy(d);
4084 e = sv_mortalcopy(e);
4088 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
4095 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4096 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4097 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
4099 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4100 object_on_left = TRUE;
4103 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
4105 if (object_on_left) {
4106 goto sm_any_sub; /* Treat objects like scalars */
4108 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4109 /* Test sub truth for each key */
4111 bool andedresults = TRUE;
4112 HV *hv = (HV*) SvRV(d);
4113 I32 numkeys = hv_iterinit(hv);
4114 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
4117 while ( (he = hv_iternext(hv)) ) {
4118 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
4119 ENTER_with_name("smartmatch_hash_key_test");
4122 PUSHs(hv_iterkeysv(he));
4124 c = call_sv(e, G_SCALAR);
4127 andedresults = FALSE;
4129 andedresults = SvTRUEx(POPs) && andedresults;
4131 LEAVE_with_name("smartmatch_hash_key_test");
4138 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4139 /* Test sub truth for each element */
4141 bool andedresults = TRUE;
4142 AV *av = (AV*) SvRV(d);
4143 const I32 len = av_len(av);
4144 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
4147 for (i = 0; i <= len; ++i) {
4148 SV * const * const svp = av_fetch(av, i, FALSE);
4149 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
4150 ENTER_with_name("smartmatch_array_elem_test");
4156 c = call_sv(e, G_SCALAR);
4159 andedresults = FALSE;
4161 andedresults = SvTRUEx(POPs) && andedresults;
4163 LEAVE_with_name("smartmatch_array_elem_test");
4172 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
4173 ENTER_with_name("smartmatch_coderef");
4178 c = call_sv(e, G_SCALAR);
4182 else if (SvTEMP(TOPs))
4183 SvREFCNT_inc_void(TOPs);
4185 LEAVE_with_name("smartmatch_coderef");
4190 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
4191 if (object_on_left) {
4192 goto sm_any_hash; /* Treat objects like scalars */
4194 else if (!SvOK(d)) {
4195 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
4198 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4199 /* Check that the key-sets are identical */
4201 HV *other_hv = MUTABLE_HV(SvRV(d));
4203 bool other_tied = FALSE;
4204 U32 this_key_count = 0,
4205 other_key_count = 0;
4206 HV *hv = MUTABLE_HV(SvRV(e));
4208 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
4209 /* Tied hashes don't know how many keys they have. */
4210 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
4213 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
4214 HV * const temp = other_hv;
4219 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
4222 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
4225 /* The hashes have the same number of keys, so it suffices
4226 to check that one is a subset of the other. */
4227 (void) hv_iterinit(hv);
4228 while ( (he = hv_iternext(hv)) ) {
4229 SV *key = hv_iterkeysv(he);
4231 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
4234 if(!hv_exists_ent(other_hv, key, 0)) {
4235 (void) hv_iterinit(hv); /* reset iterator */
4241 (void) hv_iterinit(other_hv);
4242 while ( hv_iternext(other_hv) )
4246 other_key_count = HvUSEDKEYS(other_hv);
4248 if (this_key_count != other_key_count)
4253 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4254 AV * const other_av = MUTABLE_AV(SvRV(d));
4255 const I32 other_len = av_len(other_av) + 1;
4257 HV *hv = MUTABLE_HV(SvRV(e));
4259 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n"));
4260 for (i = 0; i < other_len; ++i) {
4261 SV ** const svp = av_fetch(other_av, i, FALSE);
4262 DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n"));
4263 if (svp) { /* ??? When can this not happen? */
4264 if (hv_exists_ent(hv, *svp, 0))
4270 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4271 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n"));
4274 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4276 HV *hv = MUTABLE_HV(SvRV(e));
4278 (void) hv_iterinit(hv);
4279 while ( (he = hv_iternext(hv)) ) {
4280 DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n"));
4281 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
4282 (void) hv_iterinit(hv);
4283 destroy_matcher(matcher);
4287 destroy_matcher(matcher);
4293 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n"));
4294 if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0))
4301 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) {
4302 if (object_on_left) {
4303 goto sm_any_array; /* Treat objects like scalars */
4305 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4306 AV * const other_av = MUTABLE_AV(SvRV(e));
4307 const I32 other_len = av_len(other_av) + 1;
4310 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n"));
4311 for (i = 0; i < other_len; ++i) {
4312 SV ** const svp = av_fetch(other_av, i, FALSE);
4314 DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n"));
4315 if (svp) { /* ??? When can this not happen? */
4316 if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0))
4322 if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4323 AV *other_av = MUTABLE_AV(SvRV(d));
4324 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n"));
4325 if (av_len(MUTABLE_AV(SvRV(e))) != av_len(other_av))
4329 const I32 other_len = av_len(other_av);
4331 if (NULL == seen_this) {
4332 seen_this = newHV();
4333 (void) sv_2mortal(MUTABLE_SV(seen_this));
4335 if (NULL == seen_other) {
4336 seen_other = newHV();
4337 (void) sv_2mortal(MUTABLE_SV(seen_other));
4339 for(i = 0; i <= other_len; ++i) {
4340 SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4341 SV * const * const other_elem = av_fetch(other_av, i, FALSE);
4343 if (!this_elem || !other_elem) {
4344 if ((this_elem && SvOK(*this_elem))
4345 || (other_elem && SvOK(*other_elem)))
4348 else if (hv_exists_ent(seen_this,
4349 sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) ||
4350 hv_exists_ent(seen_other,
4351 sv_2mortal(newSViv(PTR2IV(*other_elem))), 0))
4353 if (*this_elem != *other_elem)
4357 (void)hv_store_ent(seen_this,
4358 sv_2mortal(newSViv(PTR2IV(*this_elem))),
4360 (void)hv_store_ent(seen_other,
4361 sv_2mortal(newSViv(PTR2IV(*other_elem))),
4367 DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n"));
4368 (void) do_smartmatch(seen_this, seen_other);
4370 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4379 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
4380 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n"));
4383 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4384 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4387 for(i = 0; i <= this_len; ++i) {
4388 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4389 DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n"));
4390 if (svp && matcher_matches_sv(matcher, *svp)) {
4391 destroy_matcher(matcher);
4395 destroy_matcher(matcher);
4399 else if (!SvOK(d)) {
4400 /* undef ~~ array */
4401 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4404 DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n"));
4405 for (i = 0; i <= this_len; ++i) {
4406 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4407 DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n"));
4408 if (!svp || !SvOK(*svp))
4417 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4419 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n"));
4420 for (i = 0; i <= this_len; ++i) {
4421 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4428 /* infinite recursion isn't supposed to happen here */
4429 DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n"));
4430 (void) do_smartmatch(NULL, NULL);
4432 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
4441 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) {
4442 if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4443 SV *t = d; d = e; e = t;
4444 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n"));
4447 else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4448 SV *t = d; d = e; e = t;
4449 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n"));
4450 goto sm_regex_array;
4453 PMOP * const matcher = make_matcher((REGEXP*) SvRV(e));
4455 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n"));
4457 PUSHs(matcher_matches_sv(matcher, d)
4460 destroy_matcher(matcher);
4465 /* See if there is overload magic on left */
4466 else if (object_on_left && SvAMAGIC(d)) {
4468 DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n"));
4469 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4472 tmpsv = amagic_call(d, e, smart_amg, AMGf_noright);
4480 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n"));
4483 else if (!SvOK(d)) {
4484 /* undef ~~ scalar ; we already know that the scalar is SvOK */
4485 DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n"));
4490 if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) {
4491 DEBUG_M(if (SvNIOK(e))
4492 Perl_deb(aTHX_ " applying rule Any-Num\n");
4494 Perl_deb(aTHX_ " applying rule Num-numish\n");
4496 /* numeric comparison */
4499 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
4510 /* As a last resort, use string comparison */
4511 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n"));
4520 register PERL_CONTEXT *cx;
4521 const I32 gimme = GIMME_V;
4523 /* This is essentially an optimization: if the match
4524 fails, we don't want to push a context and then
4525 pop it again right away, so we skip straight
4526 to the op that follows the leavewhen.
4528 if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs))
4529 return cLOGOP->op_other->op_next;
4531 ENTER_with_name("eval");
4534 PUSHBLOCK(cx, CXt_WHEN, SP);
4543 register PERL_CONTEXT *cx;
4549 assert(CxTYPE(cx) == CXt_WHEN);
4554 PL_curpm = newpm; /* pop $1 et al */
4556 LEAVE_with_name("eval");
4564 register PERL_CONTEXT *cx;
4567 cxix = dopoptowhen(cxstack_ix);
4569 DIE(aTHX_ "Can't \"continue\" outside a when block");
4570 if (cxix < cxstack_ix)
4573 /* clear off anything above the scope we're re-entering */
4574 inner = PL_scopestack_ix;
4576 if (PL_scopestack_ix < inner)
4577 leave_scope(PL_scopestack[PL_scopestack_ix]);
4578 PL_curcop = cx->blk_oldcop;
4579 return cx->blk_givwhen.leave_op;
4586 register PERL_CONTEXT *cx;
4589 cxix = dopoptogiven(cxstack_ix);
4591 if (PL_op->op_flags & OPf_SPECIAL)
4592 DIE(aTHX_ "Can't use when() outside a topicalizer");
4594 DIE(aTHX_ "Can't \"break\" outside a given block");
4596 if (CxFOREACH(&cxstack[cxix]) && (0 == (PL_op->op_flags & OPf_SPECIAL)))
4597 DIE(aTHX_ "Can't \"break\" in a loop topicalizer");
4599 if (cxix < cxstack_ix)
4602 /* clear off anything above the scope we're re-entering */
4603 inner = PL_scopestack_ix;
4605 if (PL_scopestack_ix < inner)
4606 leave_scope(PL_scopestack[PL_scopestack_ix]);
4607 PL_curcop = cx->blk_oldcop;
4610 return CX_LOOP_NEXTOP_GET(cx);
4612 return cx->blk_givwhen.leave_op;
4616 S_doparseform(pTHX_ SV *sv)
4619 register char *s = SvPV_force(sv, len);
4620 register char * const send = s + len;
4621 register char *base = NULL;
4622 register I32 skipspaces = 0;
4623 bool noblank = FALSE;
4624 bool repeat = FALSE;
4625 bool postspace = FALSE;
4631 bool unchopnum = FALSE;
4632 int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */
4634 PERL_ARGS_ASSERT_DOPARSEFORM;
4637 Perl_croak(aTHX_ "Null picture in formline");
4639 /* estimate the buffer size needed */
4640 for (base = s; s <= send; s++) {
4641 if (*s == '\n' || *s == '@' || *s == '^')
4647 Newx(fops, maxops, U32);
4652 *fpc++ = FF_LINEMARK;
4653 noblank = repeat = FALSE;
4671 case ' ': case '\t':
4678 } /* else FALL THROUGH */
4686 *fpc++ = FF_LITERAL;
4694 *fpc++ = (U16)skipspaces;
4698 *fpc++ = FF_NEWLINE;
4702 arg = fpc - linepc + 1;
4709 *fpc++ = FF_LINEMARK;
4710 noblank = repeat = FALSE;
4719 ischop = s[-1] == '^';
4725 arg = (s - base) - 1;
4727 *fpc++ = FF_LITERAL;
4735 *fpc++ = 2; /* skip the @* or ^* */
4737 *fpc++ = FF_LINESNGL;
4740 *fpc++ = FF_LINEGLOB;
4742 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
4743 arg = ischop ? 512 : 0;
4748 const char * const f = ++s;
4751 arg |= 256 + (s - f);
4753 *fpc++ = s - base; /* fieldsize for FETCH */
4754 *fpc++ = FF_DECIMAL;
4756 unchopnum |= ! ischop;
4758 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
4759 arg = ischop ? 512 : 0;
4761 s++; /* skip the '0' first */
4765 const char * const f = ++s;
4768 arg |= 256 + (s - f);
4770 *fpc++ = s - base; /* fieldsize for FETCH */
4771 *fpc++ = FF_0DECIMAL;
4773 unchopnum |= ! ischop;
4777 bool ismore = FALSE;
4780 while (*++s == '>') ;
4781 prespace = FF_SPACE;
4783 else if (*s == '|') {
4784 while (*++s == '|') ;
4785 prespace = FF_HALFSPACE;
4790 while (*++s == '<') ;
4793 if (*s == '.' && s[1] == '.' && s[2] == '.') {
4797 *fpc++ = s - base; /* fieldsize for FETCH */
4799 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
4802 *fpc++ = (U16)prespace;
4816 assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */
4818 { /* need to jump to the next word */
4820 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
4821 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U32) + 4);
4822 s = SvPVX(sv) + SvCUR(sv) + z;
4824 Copy(fops, s, arg, U32);
4826 sv_magic(sv, NULL, PERL_MAGIC_fm, NULL, 0);
4829 if (unchopnum && repeat)
4830 DIE(aTHX_ "Repeated format line will never terminate (~~ and @#)");
4836 S_num_overflow(NV value, I32 fldsize, I32 frcsize)
4838 /* Can value be printed in fldsize chars, using %*.*f ? */
4842 int intsize = fldsize - (value < 0 ? 1 : 0);
4849 while (intsize--) pwr *= 10.0;
4850 while (frcsize--) eps /= 10.0;
4853 if (value + eps >= pwr)
4856 if (value - eps <= -pwr)
4863 S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
4866 SV * const datasv = FILTER_DATA(idx);
4867 const int filter_has_file = IoLINES(datasv);
4868 SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv));
4869 SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv));
4874 char *prune_from = NULL;
4875 bool read_from_cache = FALSE;
4878 PERL_ARGS_ASSERT_RUN_USER_FILTER;
4880 assert(maxlen >= 0);
4883 /* I was having segfault trouble under Linux 2.2.5 after a
4884 parse error occured. (Had to hack around it with a test
4885 for PL_parser->error_count == 0.) Solaris doesn't segfault --
4886 not sure where the trouble is yet. XXX */
4889 SV *const cache = datasv;
4892 const char *cache_p = SvPV(cache, cache_len);
4896 /* Running in block mode and we have some cached data already.
4898 if (cache_len >= umaxlen) {
4899 /* In fact, so much data we don't even need to call
4904 const char *const first_nl =
4905 (const char *)memchr(cache_p, '\n', cache_len);
4907 take = first_nl + 1 - cache_p;
4911 sv_catpvn(buf_sv, cache_p, take);
4912 sv_chop(cache, cache_p + take);
4913 /* Definately not EOF */
4917 sv_catsv(buf_sv, cache);
4919 umaxlen -= cache_len;
4922 read_from_cache = TRUE;
4926 /* Filter API says that the filter appends to the contents of the buffer.
4927 Usually the buffer is "", so the details don't matter. But if it's not,
4928 then clearly what it contains is already filtered by this filter, so we
4929 don't want to pass it in a second time.
4930 I'm going to use a mortal in case the upstream filter croaks. */
4931 upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv))
4932 ? sv_newmortal() : buf_sv;
4933 SvUPGRADE(upstream, SVt_PV);
4935 if (filter_has_file) {
4936 status = FILTER_READ(idx+1, upstream, 0);
4939 if (filter_sub && status >= 0) {
4943 ENTER_with_name("call_filter_sub");
4948 DEFSV_set(upstream);
4952 PUSHs(filter_state);
4955 count = call_sv(filter_sub, G_SCALAR);
4967 LEAVE_with_name("call_filter_sub");
4970 if(SvOK(upstream)) {
4971 got_p = SvPV(upstream, got_len);
4973 if (got_len > umaxlen) {
4974 prune_from = got_p + umaxlen;
4977 char *const first_nl = (char *)memchr(got_p, '\n', got_len);
4978 if (first_nl && first_nl + 1 < got_p + got_len) {
4979 /* There's a second line here... */
4980 prune_from = first_nl + 1;
4985 /* Oh. Too long. Stuff some in our cache. */
4986 STRLEN cached_len = got_p + got_len - prune_from;
4987 SV *const cache = datasv;
4990 /* Cache should be empty. */
4991 assert(!SvCUR(cache));
4994 sv_setpvn(cache, prune_from, cached_len);
4995 /* If you ask for block mode, you may well split UTF-8 characters.
4996 "If it breaks, you get to keep both parts"
4997 (Your code is broken if you don't put them back together again
4998 before something notices.) */
4999 if (SvUTF8(upstream)) {
5002 SvCUR_set(upstream, got_len - cached_len);
5004 /* Can't yet be EOF */
5009 /* If they are at EOF but buf_sv has something in it, then they may never
5010 have touched the SV upstream, so it may be undefined. If we naively
5011 concatenate it then we get a warning about use of uninitialised value.
5013 if (upstream != buf_sv && (SvOK(upstream) || SvGMAGICAL(upstream))) {
5014 sv_catsv(buf_sv, upstream);
5018 IoLINES(datasv) = 0;
5020 SvREFCNT_dec(filter_state);
5021 IoTOP_GV(datasv) = NULL;
5024 SvREFCNT_dec(filter_sub);
5025 IoBOTTOM_GV(datasv) = NULL;
5027 filter_del(S_run_user_filter);
5029 if (status == 0 && read_from_cache) {
5030 /* If we read some data from the cache (and by getting here it implies
5031 that we emptied the cache) then we aren't yet at EOF, and mustn't
5032 report that to our caller. */
5038 /* perhaps someone can come up with a better name for
5039 this? it is not really "absolute", per se ... */
5041 S_path_is_absolute(const char *name)
5043 PERL_ARGS_ASSERT_PATH_IS_ABSOLUTE;
5045 if (PERL_FILE_IS_ABSOLUTE(name)
5047 || (*name == '.' && ((name[1] == '/' ||
5048 (name[1] == '.' && name[2] == '/'))
5049 || (name[1] == '\\' ||
5050 ( name[1] == '.' && name[2] == '\\')))
5053 || (*name == '.' && (name[1] == '/' ||
5054 (name[1] == '.' && name[2] == '/')))
5066 * c-indentation-style: bsd
5068 * indent-tabs-mode: t
5071 * ex: set ts=8 sts=4 sw=4 noet: