3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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.
20 /* This file contains control-oriented pp ("push/pop") functions that
21 * execute the opcodes that make up a perl program. A typical pp function
22 * expects to find its arguments on the stack, and usually pushes its
23 * results onto the stack, hence the 'pp' terminology. Each OP structure
24 * contains a pointer to the relevant pp_foo() function.
26 * Control-oriented means things like pp_enteriter() and pp_next(), which
27 * alter the flow of control of the program.
32 #define PERL_IN_PP_CTL_C
36 #define WORD_ALIGN sizeof(U32)
39 #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
48 cxix = dopoptosub(cxstack_ix);
52 switch (cxstack[cxix].blk_gimme) {
65 /* XXXX Should store the old value to allow for tie/overload - and
66 restore in regcomp, where marked with XXXX. */
76 register PMOP *pm = (PMOP*)cLOGOP->op_other;
80 /* prevent recompiling under /o and ithreads. */
81 #if defined(USE_ITHREADS)
82 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
83 if (PL_op->op_flags & OPf_STACKED) {
92 if (PL_op->op_flags & OPf_STACKED) {
93 /* multiple args; concatentate them */
95 tmpstr = PAD_SV(ARGTARG);
96 sv_setpvn(tmpstr, "", 0);
97 while (++MARK <= SP) {
98 if (PL_amagic_generation) {
100 if ((SvAMAGIC(tmpstr) || SvAMAGIC(*MARK)) &&
101 (sv = amagic_call(tmpstr, *MARK, concat_amg, AMGf_assign)))
103 sv_setsv(tmpstr, sv);
107 sv_catsv(tmpstr, *MARK);
116 SV * const sv = SvRV(tmpstr);
118 mg = mg_find(sv, PERL_MAGIC_qr);
121 regexp * const re = (regexp *)mg->mg_obj;
122 ReREFCNT_dec(PM_GETRE(pm));
123 PM_SETRE(pm, ReREFCNT_inc(re));
127 const char *t = SvPV_const(tmpstr, len);
128 regexp * const re = PM_GETRE(pm);
130 /* Check against the last compiled regexp. */
131 if (!re || !re->precomp || re->prelen != (I32)len ||
132 memNE(re->precomp, t, len))
136 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
138 if (PL_op->op_flags & OPf_SPECIAL)
139 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
141 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
143 pm->op_pmdynflags |= PMdf_DYN_UTF8;
145 pm->op_pmdynflags &= ~PMdf_DYN_UTF8;
146 if (pm->op_pmdynflags & PMdf_UTF8)
147 t = (char*)bytes_to_utf8((U8*)t, &len);
149 PM_SETRE(pm, CALLREGCOMP(aTHX_ (char *)t, (char *)t + len, pm));
150 if (!DO_UTF8(tmpstr) && (pm->op_pmdynflags & PMdf_UTF8))
152 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
153 inside tie/overload accessors. */
157 #ifndef INCOMPLETE_TAINTS
160 pm->op_pmdynflags |= PMdf_TAINTED;
162 pm->op_pmdynflags &= ~PMdf_TAINTED;
166 if (!PM_GETRE(pm)->prelen && PL_curpm)
168 else if (strEQ("\\s+", PM_GETRE(pm)->precomp))
169 pm->op_pmflags |= PMf_WHITE;
171 pm->op_pmflags &= ~PMf_WHITE;
173 /* XXX runtime compiled output needs to move to the pad */
174 if (pm->op_pmflags & PMf_KEEP) {
175 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
176 #if !defined(USE_ITHREADS)
177 /* XXX can't change the optree at runtime either */
178 cLOGOP->op_first->op_next = PL_op->op_next;
188 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
189 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
190 register SV * const dstr = cx->sb_dstr;
191 register char *s = cx->sb_s;
192 register char *m = cx->sb_m;
193 char *orig = cx->sb_orig;
194 register REGEXP * const rx = cx->sb_rx;
196 REGEXP *old = PM_GETRE(pm);
200 PM_SETRE(pm,ReREFCNT_inc(rx));
203 rxres_restore(&cx->sb_rxres, rx);
204 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
206 if (cx->sb_iters++) {
207 const I32 saviters = cx->sb_iters;
208 if (cx->sb_iters > cx->sb_maxiters)
209 DIE(aTHX_ "Substitution loop");
211 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
212 cx->sb_rxtainted |= 2;
213 sv_catsv(dstr, POPs);
214 FREETMPS; /* Prevent excess tmp stack */
217 if (cx->sb_once || !CALLREGEXEC(aTHX_ rx, s, cx->sb_strend, orig,
218 s == m, cx->sb_targ, NULL,
219 ((cx->sb_rflags & REXEC_COPY_STR)
220 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
221 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
223 SV * const targ = cx->sb_targ;
225 assert(cx->sb_strend >= s);
226 if(cx->sb_strend > s) {
227 if (DO_UTF8(dstr) && !SvUTF8(targ))
228 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
230 sv_catpvn(dstr, s, cx->sb_strend - s);
232 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
234 #ifdef PERL_OLD_COPY_ON_WRITE
236 sv_force_normal_flags(targ, SV_COW_DROP_PV);
242 SvPV_set(targ, SvPVX(dstr));
243 SvCUR_set(targ, SvCUR(dstr));
244 SvLEN_set(targ, SvLEN(dstr));
247 SvPV_set(dstr, NULL);
250 TAINT_IF(cx->sb_rxtainted & 1);
251 PUSHs(sv_2mortal(newSViv(saviters - 1)));
253 (void)SvPOK_only_UTF8(targ);
254 TAINT_IF(cx->sb_rxtainted);
258 LEAVE_SCOPE(cx->sb_oldsave);
260 RETURNOP(pm->op_next);
262 cx->sb_iters = saviters;
264 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
267 cx->sb_orig = orig = rx->subbeg;
269 cx->sb_strend = s + (cx->sb_strend - m);
271 cx->sb_m = m = rx->startp[0] + orig;
273 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
274 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
276 sv_catpvn(dstr, s, m-s);
278 cx->sb_s = rx->endp[0] + orig;
279 { /* Update the pos() information. */
280 SV * const sv = cx->sb_targ;
283 if (SvTYPE(sv) < SVt_PVMG)
284 SvUPGRADE(sv, SVt_PVMG);
285 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
286 #ifdef PERL_OLD_COPY_ON_WRITE
288 sv_force_normal_flags(sv, 0);
290 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
299 (void)ReREFCNT_inc(rx);
300 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
301 rxres_save(&cx->sb_rxres, rx);
302 RETURNOP(pm->op_pmreplstart);
306 Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
312 if (!p || p[1] < rx->nparens) {
313 #ifdef PERL_OLD_COPY_ON_WRITE
314 i = 7 + rx->nparens * 2;
316 i = 6 + rx->nparens * 2;
325 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? rx->subbeg : NULL);
326 RX_MATCH_COPIED_off(rx);
328 #ifdef PERL_OLD_COPY_ON_WRITE
329 *p++ = PTR2UV(rx->saved_copy);
330 rx->saved_copy = NULL;
335 *p++ = PTR2UV(rx->subbeg);
336 *p++ = (UV)rx->sublen;
337 for (i = 0; i <= rx->nparens; ++i) {
338 *p++ = (UV)rx->startp[i];
339 *p++ = (UV)rx->endp[i];
344 Perl_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
350 RX_MATCH_COPY_FREE(rx);
351 RX_MATCH_COPIED_set(rx, *p);
354 #ifdef PERL_OLD_COPY_ON_WRITE
356 SvREFCNT_dec (rx->saved_copy);
357 rx->saved_copy = INT2PTR(SV*,*p);
363 rx->subbeg = INT2PTR(char*,*p++);
364 rx->sublen = (I32)(*p++);
365 for (i = 0; i <= rx->nparens; ++i) {
366 rx->startp[i] = (I32)(*p++);
367 rx->endp[i] = (I32)(*p++);
372 Perl_rxres_free(pTHX_ void **rsp)
374 UV * const p = (UV*)*rsp;
379 void *tmp = INT2PTR(char*,*p);
382 PoisonFree(*p, 1, sizeof(*p));
384 Safefree(INT2PTR(char*,*p));
386 #ifdef PERL_OLD_COPY_ON_WRITE
388 SvREFCNT_dec (INT2PTR(SV*,p[1]));
398 dVAR; dSP; dMARK; dORIGMARK;
399 register SV * const tmpForm = *++MARK;
404 register SV *sv = NULL;
405 const char *item = NULL;
409 bool chopspace = (strchr(PL_chopset, ' ') != NULL);
410 const char *chophere = NULL;
411 char *linemark = NULL;
413 bool gotsome = FALSE;
415 const STRLEN fudge = SvPOK(tmpForm)
416 ? (SvCUR(tmpForm) * (IN_BYTES ? 1 : 3) + 1) : 0;
417 bool item_is_utf8 = FALSE;
418 bool targ_is_utf8 = FALSE;
420 OP * parseres = NULL;
424 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
425 if (SvREADONLY(tmpForm)) {
426 SvREADONLY_off(tmpForm);
427 parseres = doparseform(tmpForm);
428 SvREADONLY_on(tmpForm);
431 parseres = doparseform(tmpForm);
435 SvPV_force(PL_formtarget, len);
436 if (DO_UTF8(PL_formtarget))
438 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
440 f = SvPV_const(tmpForm, len);
441 /* need to jump to the next word */
442 fpc = (U32*)(f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN);
446 const char *name = "???";
449 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
450 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
451 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
452 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
453 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
455 case FF_CHECKNL: name = "CHECKNL"; break;
456 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
457 case FF_SPACE: name = "SPACE"; break;
458 case FF_HALFSPACE: name = "HALFSPACE"; break;
459 case FF_ITEM: name = "ITEM"; break;
460 case FF_CHOP: name = "CHOP"; break;
461 case FF_LINEGLOB: name = "LINEGLOB"; break;
462 case FF_NEWLINE: name = "NEWLINE"; break;
463 case FF_MORE: name = "MORE"; break;
464 case FF_LINEMARK: name = "LINEMARK"; break;
465 case FF_END: name = "END"; break;
466 case FF_0DECIMAL: name = "0DECIMAL"; break;
467 case FF_LINESNGL: name = "LINESNGL"; break;
470 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
472 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
483 if (targ_is_utf8 && !SvUTF8(tmpForm)) {
484 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
486 sv_catpvn_utf8_upgrade(PL_formtarget, f, arg, nsv);
487 t = SvEND(PL_formtarget);
490 if (!targ_is_utf8 && DO_UTF8(tmpForm)) {
491 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
493 sv_utf8_upgrade(PL_formtarget);
494 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
495 t = SvEND(PL_formtarget);
515 if (ckWARN(WARN_SYNTAX))
516 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
523 const char *s = item = SvPV_const(sv, len);
526 itemsize = sv_len_utf8(sv);
527 if (itemsize != (I32)len) {
529 if (itemsize > fieldsize) {
530 itemsize = fieldsize;
531 itembytes = itemsize;
532 sv_pos_u2b(sv, &itembytes, 0);
536 send = chophere = s + itembytes;
546 sv_pos_b2u(sv, &itemsize);
550 item_is_utf8 = FALSE;
551 if (itemsize > fieldsize)
552 itemsize = fieldsize;
553 send = chophere = s + itemsize;
567 const char *s = item = SvPV_const(sv, len);
570 itemsize = sv_len_utf8(sv);
571 if (itemsize != (I32)len) {
573 if (itemsize <= fieldsize) {
574 const char *send = chophere = s + itemsize;
587 itemsize = fieldsize;
588 itembytes = itemsize;
589 sv_pos_u2b(sv, &itembytes, 0);
590 send = chophere = s + itembytes;
591 while (s < send || (s == send && isSPACE(*s))) {
601 if (strchr(PL_chopset, *s))
606 itemsize = chophere - item;
607 sv_pos_b2u(sv, &itemsize);
613 item_is_utf8 = FALSE;
614 if (itemsize <= fieldsize) {
615 const char *const send = chophere = s + itemsize;
628 itemsize = fieldsize;
629 send = chophere = s + itemsize;
630 while (s < send || (s == send && isSPACE(*s))) {
640 if (strchr(PL_chopset, *s))
645 itemsize = chophere - item;
651 arg = fieldsize - itemsize;
660 arg = fieldsize - itemsize;
671 const char *s = item;
675 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
677 sv_utf8_upgrade(PL_formtarget);
678 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
679 t = SvEND(PL_formtarget);
683 if (UTF8_IS_CONTINUED(*s)) {
684 STRLEN skip = UTF8SKIP(s);
701 if ( !((*t++ = *s++) & ~31) )
707 if (targ_is_utf8 && !item_is_utf8) {
708 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
710 sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
711 for (; t < SvEND(PL_formtarget); t++) {
724 const int ch = *t++ = *s++;
727 if ( !((*t++ = *s++) & ~31) )
736 const char *s = chophere;
754 const char *s = item = SvPV_const(sv, len);
756 if ((item_is_utf8 = DO_UTF8(sv)))
757 itemsize = sv_len_utf8(sv);
759 bool chopped = FALSE;
760 const char *const send = s + len;
762 chophere = s + itemsize;
778 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
780 SvUTF8_on(PL_formtarget);
782 SvCUR_set(sv, chophere - item);
783 sv_catsv(PL_formtarget, sv);
784 SvCUR_set(sv, itemsize);
786 sv_catsv(PL_formtarget, sv);
788 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) - 1);
789 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
790 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
799 #if defined(USE_LONG_DOUBLE)
800 fmt = (arg & 256) ? "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl;
802 fmt = (arg & 256) ? "%#0*.*f" : "%0*.*f";
807 #if defined(USE_LONG_DOUBLE)
808 fmt = (arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl;
810 fmt = (arg & 256) ? "%#*.*f" : "%*.*f";
813 /* If the field is marked with ^ and the value is undefined,
815 if ((arg & 512) && !SvOK(sv)) {
823 /* overflow evidence */
824 if (num_overflow(value, fieldsize, arg)) {
830 /* Formats aren't yet marked for locales, so assume "yes". */
832 STORE_NUMERIC_STANDARD_SET_LOCAL();
833 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
834 RESTORE_NUMERIC_STANDARD();
841 while (t-- > linemark && *t == ' ') ;
849 if (arg) { /* repeat until fields exhausted? */
851 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
852 lines += FmLINES(PL_formtarget);
855 if (strnEQ(linemark, linemark - arg, arg))
856 DIE(aTHX_ "Runaway format");
859 SvUTF8_on(PL_formtarget);
860 FmLINES(PL_formtarget) = lines;
862 RETURNOP(cLISTOP->op_first);
873 const char *s = chophere;
874 const char *send = item + len;
876 while (isSPACE(*s) && (s < send))
881 arg = fieldsize - itemsize;
888 if (strnEQ(s1," ",3)) {
889 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
900 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
902 SvUTF8_on(PL_formtarget);
903 FmLINES(PL_formtarget) += lines;
915 if (PL_stack_base + *PL_markstack_ptr == SP) {
917 if (GIMME_V == G_SCALAR)
918 XPUSHs(sv_2mortal(newSViv(0)));
919 RETURNOP(PL_op->op_next->op_next);
921 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
922 pp_pushmark(); /* push dst */
923 pp_pushmark(); /* push src */
924 ENTER; /* enter outer scope */
927 if (PL_op->op_private & OPpGREP_LEX)
928 SAVESPTR(PAD_SVl(PL_op->op_targ));
931 ENTER; /* enter inner scope */
934 src = PL_stack_base[*PL_markstack_ptr];
936 if (PL_op->op_private & OPpGREP_LEX)
937 PAD_SVl(PL_op->op_targ) = src;
942 if (PL_op->op_type == OP_MAPSTART)
943 pp_pushmark(); /* push top */
944 return ((LOGOP*)PL_op->op_next)->op_other;
950 const I32 gimme = GIMME_V;
951 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
957 /* first, move source pointer to the next item in the source list */
958 ++PL_markstack_ptr[-1];
960 /* if there are new items, push them into the destination list */
961 if (items && gimme != G_VOID) {
962 /* might need to make room back there first */
963 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
964 /* XXX this implementation is very pessimal because the stack
965 * is repeatedly extended for every set of items. Is possible
966 * to do this without any stack extension or copying at all
967 * by maintaining a separate list over which the map iterates
968 * (like foreach does). --gsar */
970 /* everything in the stack after the destination list moves
971 * towards the end the stack by the amount of room needed */
972 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
974 /* items to shift up (accounting for the moved source pointer) */
975 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
977 /* This optimization is by Ben Tilly and it does
978 * things differently from what Sarathy (gsar)
979 * is describing. The downside of this optimization is
980 * that leaves "holes" (uninitialized and hopefully unused areas)
981 * to the Perl stack, but on the other hand this
982 * shouldn't be a problem. If Sarathy's idea gets
983 * implemented, this optimization should become
984 * irrelevant. --jhi */
986 shift = count; /* Avoid shifting too often --Ben Tilly */
991 PL_markstack_ptr[-1] += shift;
992 *PL_markstack_ptr += shift;
996 /* copy the new items down to the destination list */
997 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
998 if (gimme == G_ARRAY) {
1000 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
1003 /* scalar context: we don't care about which values map returns
1004 * (we use undef here). And so we certainly don't want to do mortal
1005 * copies of meaningless values. */
1006 while (items-- > 0) {
1008 *dst-- = &PL_sv_undef;
1012 LEAVE; /* exit inner scope */
1015 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
1017 (void)POPMARK; /* pop top */
1018 LEAVE; /* exit outer scope */
1019 (void)POPMARK; /* pop src */
1020 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
1021 (void)POPMARK; /* pop dst */
1022 SP = PL_stack_base + POPMARK; /* pop original mark */
1023 if (gimme == G_SCALAR) {
1024 if (PL_op->op_private & OPpGREP_LEX) {
1025 SV* sv = sv_newmortal();
1026 sv_setiv(sv, items);
1034 else if (gimme == G_ARRAY)
1041 ENTER; /* enter inner scope */
1044 /* set $_ to the new source item */
1045 src = PL_stack_base[PL_markstack_ptr[-1]];
1047 if (PL_op->op_private & OPpGREP_LEX)
1048 PAD_SVl(PL_op->op_targ) = src;
1052 RETURNOP(cLOGOP->op_other);
1061 if (GIMME == G_ARRAY)
1063 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1064 return cLOGOP->op_other;
1074 if (GIMME == G_ARRAY) {
1075 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1079 SV * const targ = PAD_SV(PL_op->op_targ);
1082 if (PL_op->op_private & OPpFLIP_LINENUM) {
1083 if (GvIO(PL_last_in_gv)) {
1084 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1087 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1089 flip = SvIV(sv) == SvIV(GvSV(gv));
1095 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
1096 if (PL_op->op_flags & OPf_SPECIAL) {
1104 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
1107 sv_setpvn(TARG, "", 0);
1113 /* This code tries to decide if "$left .. $right" should use the
1114 magical string increment, or if the range is numeric (we make
1115 an exception for .."0" [#18165]). AMS 20021031. */
1117 #define RANGE_IS_NUMERIC(left,right) ( \
1118 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1119 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
1120 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
1121 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
1122 && (!SvOK(right) || looks_like_number(right))))
1128 if (GIMME == G_ARRAY) {
1134 if (RANGE_IS_NUMERIC(left,right)) {
1137 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1138 (SvOK(right) && SvNV(right) > IV_MAX))
1139 DIE(aTHX_ "Range iterator outside integer range");
1150 SV * const sv = sv_2mortal(newSViv(i++));
1155 SV * const final = sv_mortalcopy(right);
1157 const char * const tmps = SvPV_const(final, len);
1159 SV *sv = sv_mortalcopy(left);
1160 SvPV_force_nolen(sv);
1161 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
1163 if (strEQ(SvPVX_const(sv),tmps))
1165 sv = sv_2mortal(newSVsv(sv));
1172 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
1176 if (PL_op->op_private & OPpFLIP_LINENUM) {
1177 if (GvIO(PL_last_in_gv)) {
1178 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1181 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
1182 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1190 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
1191 sv_catpvs(targ, "E0");
1201 static const char * const context_name[] = {
1214 S_dopoptolabel(pTHX_ const char *label)
1219 for (i = cxstack_ix; i >= 0; i--) {
1220 register const PERL_CONTEXT * const cx = &cxstack[i];
1221 switch (CxTYPE(cx)) {
1229 if (ckWARN(WARN_EXITING))
1230 Perl_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1231 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1232 if (CxTYPE(cx) == CXt_NULL)
1236 if ( !cx->blk_loop.label || strNE(label, cx->blk_loop.label) ) {
1237 DEBUG_l(Perl_deb(aTHX_ "(Skipping label #%ld %s)\n",
1238 (long)i, cx->blk_loop.label));
1241 DEBUG_l( Perl_deb(aTHX_ "(Found label #%ld %s)\n", (long)i, label));
1251 Perl_dowantarray(pTHX)
1254 const I32 gimme = block_gimme();
1255 return (gimme == G_VOID) ? G_SCALAR : gimme;
1259 Perl_block_gimme(pTHX)
1262 const I32 cxix = dopoptosub(cxstack_ix);
1266 switch (cxstack[cxix].blk_gimme) {
1274 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
1281 Perl_is_lvalue_sub(pTHX)
1284 const I32 cxix = dopoptosub(cxstack_ix);
1285 assert(cxix >= 0); /* We should only be called from inside subs */
1287 if (cxstack[cxix].blk_sub.lval && CvLVALUE(cxstack[cxix].blk_sub.cv))
1288 return cxstack[cxix].blk_sub.lval;
1294 S_dopoptosub(pTHX_ I32 startingblock)
1297 return dopoptosub_at(cxstack, startingblock);
1301 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
1305 for (i = startingblock; i >= 0; i--) {
1306 register const PERL_CONTEXT * const cx = &cxstk[i];
1307 switch (CxTYPE(cx)) {
1313 DEBUG_l( Perl_deb(aTHX_ "(Found sub #%ld)\n", (long)i));
1321 S_dopoptoeval(pTHX_ I32 startingblock)
1325 for (i = startingblock; i >= 0; i--) {
1326 register const PERL_CONTEXT *cx = &cxstack[i];
1327 switch (CxTYPE(cx)) {
1331 DEBUG_l( Perl_deb(aTHX_ "(Found eval #%ld)\n", (long)i));
1339 S_dopoptoloop(pTHX_ I32 startingblock)
1343 for (i = startingblock; i >= 0; i--) {
1344 register const PERL_CONTEXT * const cx = &cxstack[i];
1345 switch (CxTYPE(cx)) {
1351 if (ckWARN(WARN_EXITING))
1352 Perl_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1353 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1354 if ((CxTYPE(cx)) == CXt_NULL)
1358 DEBUG_l( Perl_deb(aTHX_ "(Found loop #%ld)\n", (long)i));
1366 S_dopoptogiven(pTHX_ I32 startingblock)
1370 for (i = startingblock; i >= 0; i--) {
1371 register const PERL_CONTEXT *cx = &cxstack[i];
1372 switch (CxTYPE(cx)) {
1376 DEBUG_l( Perl_deb(aTHX_ "(Found given #%ld)\n", (long)i));
1379 if (CxFOREACHDEF(cx)) {
1380 DEBUG_l( Perl_deb(aTHX_ "(Found foreach #%ld)\n", (long)i));
1389 S_dopoptowhen(pTHX_ I32 startingblock)
1393 for (i = startingblock; i >= 0; i--) {
1394 register const PERL_CONTEXT *cx = &cxstack[i];
1395 switch (CxTYPE(cx)) {
1399 DEBUG_l( Perl_deb(aTHX_ "(Found when #%ld)\n", (long)i));
1407 Perl_dounwind(pTHX_ I32 cxix)
1412 while (cxstack_ix > cxix) {
1414 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1415 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
1416 (long) cxstack_ix, PL_block_type[CxTYPE(cx)]));
1417 /* Note: we don't need to restore the base context info till the end. */
1418 switch (CxTYPE(cx)) {
1421 continue; /* not break */
1440 PERL_UNUSED_VAR(optype);
1444 Perl_qerror(pTHX_ SV *err)
1448 sv_catsv(ERRSV, err);
1450 sv_catsv(PL_errors, err);
1452 Perl_warn(aTHX_ "%"SVf, (void*)err);
1457 Perl_die_where(pTHX_ const char *message, STRLEN msglen)
1466 if (PL_in_eval & EVAL_KEEPERR) {
1467 static const char prefix[] = "\t(in cleanup) ";
1468 SV * const err = ERRSV;
1469 const char *e = NULL;
1471 sv_setpvn(err,"",0);
1472 else if (SvCUR(err) >= sizeof(prefix)+msglen-1) {
1474 e = SvPV_const(err, len);
1476 if (*e != *message || strNE(e,message))
1480 SvGROW(err, SvCUR(err)+sizeof(prefix)+msglen);
1481 sv_catpvn(err, prefix, sizeof(prefix)-1);
1482 sv_catpvn(err, message, msglen);
1483 if (ckWARN(WARN_MISC)) {
1484 const STRLEN start = SvCUR(err)-msglen-sizeof(prefix)+1;
1485 Perl_warner(aTHX_ packWARN(WARN_MISC), SvPVX_const(err)+start);
1490 sv_setpvn(ERRSV, message, msglen);
1494 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1495 && PL_curstackinfo->si_prev)
1503 register PERL_CONTEXT *cx;
1506 if (cxix < cxstack_ix)
1509 POPBLOCK(cx,PL_curpm);
1510 if (CxTYPE(cx) != CXt_EVAL) {
1512 message = SvPVx_const(ERRSV, msglen);
1513 PerlIO_write(Perl_error_log, "panic: die ", 11);
1514 PerlIO_write(Perl_error_log, message, msglen);
1519 if (gimme == G_SCALAR)
1520 *++newsp = &PL_sv_undef;
1521 PL_stack_sp = newsp;
1525 /* LEAVE could clobber PL_curcop (see save_re_context())
1526 * XXX it might be better to find a way to avoid messing with
1527 * PL_curcop in save_re_context() instead, but this is a more
1528 * minimal fix --GSAR */
1529 PL_curcop = cx->blk_oldcop;
1531 if (optype == OP_REQUIRE) {
1532 const char* const msg = SvPVx_nolen_const(ERRSV);
1533 SV * const nsv = cx->blk_eval.old_namesv;
1534 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
1536 DIE(aTHX_ "%sCompilation failed in require",
1537 *msg ? msg : "Unknown error\n");
1539 assert(CxTYPE(cx) == CXt_EVAL);
1540 return cx->blk_eval.retop;
1544 message = SvPVx_const(ERRSV, msglen);
1546 write_to_stderr(message, msglen);
1554 dVAR; dSP; dPOPTOPssrl;
1555 if (SvTRUE(left) != SvTRUE(right))
1565 register I32 cxix = dopoptosub(cxstack_ix);
1566 register const PERL_CONTEXT *cx;
1567 register const PERL_CONTEXT *ccstack = cxstack;
1568 const PERL_SI *top_si = PL_curstackinfo;
1570 const char *stashname;
1577 /* we may be in a higher stacklevel, so dig down deeper */
1578 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1579 top_si = top_si->si_prev;
1580 ccstack = top_si->si_cxstack;
1581 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1584 if (GIMME != G_ARRAY) {
1590 /* caller() should not report the automatic calls to &DB::sub */
1591 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
1592 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1596 cxix = dopoptosub_at(ccstack, cxix - 1);
1599 cx = &ccstack[cxix];
1600 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1601 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1602 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
1603 field below is defined for any cx. */
1604 /* caller() should not report the automatic calls to &DB::sub */
1605 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1606 cx = &ccstack[dbcxix];
1609 stashname = CopSTASHPV(cx->blk_oldcop);
1610 if (GIMME != G_ARRAY) {
1613 PUSHs(&PL_sv_undef);
1616 sv_setpv(TARG, stashname);
1625 PUSHs(&PL_sv_undef);
1627 PUSHs(sv_2mortal(newSVpv(stashname, 0)));
1628 PUSHs(sv_2mortal(newSVpv(OutCopFILE(cx->blk_oldcop), 0)));
1629 PUSHs(sv_2mortal(newSViv((I32)CopLINE(cx->blk_oldcop))));
1632 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1633 GV * const cvgv = CvGV(ccstack[cxix].blk_sub.cv);
1634 /* So is ccstack[dbcxix]. */
1636 SV * const sv = newSV(0);
1637 gv_efullname3(sv, cvgv, NULL);
1638 PUSHs(sv_2mortal(sv));
1639 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1642 PUSHs(sv_2mortal(newSVpvs("(unknown)")));
1643 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1647 PUSHs(sv_2mortal(newSVpvs("(eval)")));
1648 PUSHs(sv_2mortal(newSViv(0)));
1650 gimme = (I32)cx->blk_gimme;
1651 if (gimme == G_VOID)
1652 PUSHs(&PL_sv_undef);
1654 PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
1655 if (CxTYPE(cx) == CXt_EVAL) {
1657 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
1658 PUSHs(cx->blk_eval.cur_text);
1662 else if (cx->blk_eval.old_namesv) {
1663 PUSHs(sv_2mortal(newSVsv(cx->blk_eval.old_namesv)));
1666 /* eval BLOCK (try blocks have old_namesv == 0) */
1668 PUSHs(&PL_sv_undef);
1669 PUSHs(&PL_sv_undef);
1673 PUSHs(&PL_sv_undef);
1674 PUSHs(&PL_sv_undef);
1676 if (CxTYPE(cx) == CXt_SUB && cx->blk_sub.hasargs
1677 && CopSTASH_eq(PL_curcop, PL_debstash))
1679 AV * const ary = cx->blk_sub.argarray;
1680 const int off = AvARRAY(ary) - AvALLOC(ary);
1683 GV* const tmpgv = gv_fetchpvs("DB::args", GV_ADD, SVt_PVAV);
1684 PL_dbargs = GvAV(gv_AVadd(tmpgv));
1686 AvREAL_off(PL_dbargs); /* XXX should be REIFY (see av.h) */
1689 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1690 av_extend(PL_dbargs, AvFILLp(ary) + off);
1691 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1692 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
1694 /* XXX only hints propagated via op_private are currently
1695 * visible (others are not easily accessible, since they
1696 * use the global PL_hints) */
1697 PUSHs(sv_2mortal(newSViv(CopHINTS_get(cx->blk_oldcop))));
1700 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
1702 if (old_warnings == pWARN_NONE ||
1703 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
1704 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
1705 else if (old_warnings == pWARN_ALL ||
1706 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1707 /* Get the bit mask for $warnings::Bits{all}, because
1708 * it could have been extended by warnings::register */
1710 HV * const bits = get_hv("warnings::Bits", FALSE);
1711 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
1712 mask = newSVsv(*bits_all);
1715 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1719 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
1720 PUSHs(sv_2mortal(mask));
1723 PUSHs(cx->blk_oldcop->cop_hints ?
1724 sv_2mortal(newRV_noinc(
1725 (SV*)Perl_refcounted_he_chain_2hv(aTHX_
1726 cx->blk_oldcop->cop_hints)))
1735 const char * const tmps = (MAXARG < 1) ? "" : POPpconstx;
1736 sv_reset(tmps, CopSTASH(PL_curcop));
1741 /* like pp_nextstate, but used instead when the debugger is active */
1746 PL_curcop = (COP*)PL_op;
1747 TAINT_NOT; /* Each statement is presumed innocent */
1748 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
1751 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1752 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
1755 register PERL_CONTEXT *cx;
1756 const I32 gimme = G_ARRAY;
1758 GV * const gv = PL_DBgv;
1759 register CV * const cv = GvCV(gv);
1762 DIE(aTHX_ "No DB::DB routine defined");
1764 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1765 /* don't do recursive DB::DB call */
1780 (void)(*CvXSUB(cv))(aTHX_ cv);
1787 PUSHBLOCK(cx, CXt_SUB, SP);
1789 cx->blk_sub.retop = PL_op->op_next;
1792 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1793 RETURNOP(CvSTART(cv));
1803 register PERL_CONTEXT *cx;
1804 const I32 gimme = GIMME_V;
1806 U32 cxtype = CXt_LOOP | CXp_FOREACH;
1814 if (PL_op->op_targ) {
1815 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
1816 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
1817 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
1818 SVs_PADSTALE, SVs_PADSTALE);
1820 #ifndef USE_ITHREADS
1821 svp = &PAD_SVl(PL_op->op_targ); /* "my" variable */
1824 SAVEPADSV(PL_op->op_targ);
1825 iterdata = INT2PTR(void*, PL_op->op_targ);
1826 cxtype |= CXp_PADVAR;
1830 GV * const gv = (GV*)POPs;
1831 svp = &GvSV(gv); /* symbol table variable */
1832 SAVEGENERICSV(*svp);
1835 iterdata = (void*)gv;
1839 if (PL_op->op_private & OPpITER_DEF)
1840 cxtype |= CXp_FOR_DEF;
1844 PUSHBLOCK(cx, cxtype, SP);
1846 PUSHLOOP(cx, iterdata, MARK);
1848 PUSHLOOP(cx, svp, MARK);
1850 if (PL_op->op_flags & OPf_STACKED) {
1851 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
1852 if (SvTYPE(cx->blk_loop.iterary) != SVt_PVAV) {
1854 SV * const right = (SV*)cx->blk_loop.iterary;
1857 if (RANGE_IS_NUMERIC(sv,right)) {
1858 if ((SvOK(sv) && SvNV(sv) < IV_MIN) ||
1859 (SvOK(right) && SvNV(right) >= IV_MAX))
1860 DIE(aTHX_ "Range iterator outside integer range");
1861 cx->blk_loop.iterix = SvIV(sv);
1862 cx->blk_loop.itermax = SvIV(right);
1864 /* for correct -Dstv display */
1865 cx->blk_oldsp = sp - PL_stack_base;
1869 cx->blk_loop.iterlval = newSVsv(sv);
1870 (void) SvPV_force_nolen(cx->blk_loop.iterlval);
1871 (void) SvPV_nolen_const(right);
1874 else if (PL_op->op_private & OPpITER_REVERSED) {
1875 cx->blk_loop.itermax = 0;
1876 cx->blk_loop.iterix = AvFILL(cx->blk_loop.iterary) + 1;
1881 cx->blk_loop.iterary = PL_curstack;
1882 AvFILLp(PL_curstack) = SP - PL_stack_base;
1883 if (PL_op->op_private & OPpITER_REVERSED) {
1884 cx->blk_loop.itermax = MARK - PL_stack_base + 1;
1885 cx->blk_loop.iterix = cx->blk_oldsp + 1;
1888 cx->blk_loop.iterix = MARK - PL_stack_base;
1898 register PERL_CONTEXT *cx;
1899 const I32 gimme = GIMME_V;
1905 PUSHBLOCK(cx, CXt_LOOP, SP);
1906 PUSHLOOP(cx, 0, SP);
1914 register PERL_CONTEXT *cx;
1921 assert(CxTYPE(cx) == CXt_LOOP);
1923 newsp = PL_stack_base + cx->blk_loop.resetsp;
1926 if (gimme == G_VOID)
1928 else if (gimme == G_SCALAR) {
1930 *++newsp = sv_mortalcopy(*SP);
1932 *++newsp = &PL_sv_undef;
1936 *++newsp = sv_mortalcopy(*++mark);
1937 TAINT_NOT; /* Each item is independent */
1943 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
1944 PL_curpm = newpm; /* ... and pop $1 et al */
1955 register PERL_CONTEXT *cx;
1956 bool popsub2 = FALSE;
1957 bool clear_errsv = FALSE;
1965 const I32 cxix = dopoptosub(cxstack_ix);
1968 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
1969 * sort block, which is a CXt_NULL
1972 PL_stack_base[1] = *PL_stack_sp;
1973 PL_stack_sp = PL_stack_base + 1;
1977 DIE(aTHX_ "Can't return outside a subroutine");
1979 if (cxix < cxstack_ix)
1982 if (CxMULTICALL(&cxstack[cxix])) {
1983 gimme = cxstack[cxix].blk_gimme;
1984 if (gimme == G_VOID)
1985 PL_stack_sp = PL_stack_base;
1986 else if (gimme == G_SCALAR) {
1987 PL_stack_base[1] = *PL_stack_sp;
1988 PL_stack_sp = PL_stack_base + 1;
1994 switch (CxTYPE(cx)) {
1997 retop = cx->blk_sub.retop;
1998 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
2001 if (!(PL_in_eval & EVAL_KEEPERR))
2004 retop = cx->blk_eval.retop;
2008 if (optype == OP_REQUIRE &&
2009 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2011 /* Unassume the success we assumed earlier. */
2012 SV * const nsv = cx->blk_eval.old_namesv;
2013 (void)hv_delete(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv), G_DISCARD);
2014 DIE(aTHX_ "%"SVf" did not return a true value", (void*)nsv);
2019 retop = cx->blk_sub.retop;
2022 DIE(aTHX_ "panic: return");
2026 if (gimme == G_SCALAR) {
2029 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2031 *++newsp = SvREFCNT_inc(*SP);
2036 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
2038 *++newsp = sv_mortalcopy(sv);
2043 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
2046 *++newsp = sv_mortalcopy(*SP);
2049 *++newsp = &PL_sv_undef;
2051 else if (gimme == G_ARRAY) {
2052 while (++MARK <= SP) {
2053 *++newsp = (popsub2 && SvTEMP(*MARK))
2054 ? *MARK : sv_mortalcopy(*MARK);
2055 TAINT_NOT; /* Each item is independent */
2058 PL_stack_sp = newsp;
2061 /* Stack values are safe: */
2064 POPSUB(cx,sv); /* release CV and @_ ... */
2068 PL_curpm = newpm; /* ... and pop $1 et al */
2072 sv_setpvn(ERRSV,"",0);
2080 register PERL_CONTEXT *cx;
2091 if (PL_op->op_flags & OPf_SPECIAL) {
2092 cxix = dopoptoloop(cxstack_ix);
2094 DIE(aTHX_ "Can't \"last\" outside a loop block");
2097 cxix = dopoptolabel(cPVOP->op_pv);
2099 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
2101 if (cxix < cxstack_ix)
2105 cxstack_ix++; /* temporarily protect top context */
2107 switch (CxTYPE(cx)) {
2110 newsp = PL_stack_base + cx->blk_loop.resetsp;
2111 nextop = cx->blk_loop.last_op->op_next;
2115 nextop = cx->blk_sub.retop;
2119 nextop = cx->blk_eval.retop;
2123 nextop = cx->blk_sub.retop;
2126 DIE(aTHX_ "panic: last");
2130 if (gimme == G_SCALAR) {
2132 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2133 ? *SP : sv_mortalcopy(*SP);
2135 *++newsp = &PL_sv_undef;
2137 else if (gimme == G_ARRAY) {
2138 while (++MARK <= SP) {
2139 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2140 ? *MARK : sv_mortalcopy(*MARK);
2141 TAINT_NOT; /* Each item is independent */
2149 /* Stack values are safe: */
2152 POPLOOP(cx); /* release loop vars ... */
2156 POPSUB(cx,sv); /* release CV and @_ ... */
2159 PL_curpm = newpm; /* ... and pop $1 et al */
2162 PERL_UNUSED_VAR(optype);
2163 PERL_UNUSED_VAR(gimme);
2171 register PERL_CONTEXT *cx;
2174 if (PL_op->op_flags & OPf_SPECIAL) {
2175 cxix = dopoptoloop(cxstack_ix);
2177 DIE(aTHX_ "Can't \"next\" outside a loop block");
2180 cxix = dopoptolabel(cPVOP->op_pv);
2182 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
2184 if (cxix < cxstack_ix)
2187 /* clear off anything above the scope we're re-entering, but
2188 * save the rest until after a possible continue block */
2189 inner = PL_scopestack_ix;
2191 if (PL_scopestack_ix < inner)
2192 leave_scope(PL_scopestack[PL_scopestack_ix]);
2193 PL_curcop = cx->blk_oldcop;
2194 return cx->blk_loop.next_op;
2201 register PERL_CONTEXT *cx;
2205 if (PL_op->op_flags & OPf_SPECIAL) {
2206 cxix = dopoptoloop(cxstack_ix);
2208 DIE(aTHX_ "Can't \"redo\" outside a loop block");
2211 cxix = dopoptolabel(cPVOP->op_pv);
2213 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
2215 if (cxix < cxstack_ix)
2218 redo_op = cxstack[cxix].blk_loop.redo_op;
2219 if (redo_op->op_type == OP_ENTER) {
2220 /* pop one less context to avoid $x being freed in while (my $x..) */
2222 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2223 redo_op = redo_op->op_next;
2227 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2228 LEAVE_SCOPE(oldsave);
2230 PL_curcop = cx->blk_oldcop;
2235 S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
2239 static const char too_deep[] = "Target of goto is too deeply nested";
2242 Perl_croak(aTHX_ too_deep);
2243 if (o->op_type == OP_LEAVE ||
2244 o->op_type == OP_SCOPE ||
2245 o->op_type == OP_LEAVELOOP ||
2246 o->op_type == OP_LEAVESUB ||
2247 o->op_type == OP_LEAVETRY)
2249 *ops++ = cUNOPo->op_first;
2251 Perl_croak(aTHX_ too_deep);
2254 if (o->op_flags & OPf_KIDS) {
2256 /* First try all the kids at this level, since that's likeliest. */
2257 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2258 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
2259 kCOP->cop_label && strEQ(kCOP->cop_label, label))
2262 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
2263 if (kid == PL_lastgotoprobe)
2265 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2268 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2269 ops[-1]->op_type == OP_DBSTATE)
2274 if ((o = dofindlabel(kid, label, ops, oplimit)))
2287 register PERL_CONTEXT *cx;
2288 #define GOTO_DEPTH 64
2289 OP *enterops[GOTO_DEPTH];
2290 const char *label = NULL;
2291 const bool do_dump = (PL_op->op_type == OP_DUMP);
2292 static const char must_have_label[] = "goto must have label";
2294 if (PL_op->op_flags & OPf_STACKED) {
2295 SV * const sv = POPs;
2297 /* This egregious kludge implements goto &subroutine */
2298 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2300 register PERL_CONTEXT *cx;
2301 CV* cv = (CV*)SvRV(sv);
2308 if (!CvROOT(cv) && !CvXSUB(cv)) {
2309 const GV * const gv = CvGV(cv);
2313 /* autoloaded stub? */
2314 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2316 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2317 GvNAMELEN(gv), FALSE);
2318 if (autogv && (cv = GvCV(autogv)))
2320 tmpstr = sv_newmortal();
2321 gv_efullname3(tmpstr, gv, NULL);
2322 DIE(aTHX_ "Goto undefined subroutine &%"SVf"",(void*)tmpstr);
2324 DIE(aTHX_ "Goto undefined subroutine");
2327 /* First do some returnish stuff. */
2328 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
2330 cxix = dopoptosub(cxstack_ix);
2332 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
2333 if (cxix < cxstack_ix)
2337 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2338 if (CxTYPE(cx) == CXt_EVAL) {
2340 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2342 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2344 else if (CxMULTICALL(cx))
2345 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
2346 if (CxTYPE(cx) == CXt_SUB && cx->blk_sub.hasargs) {
2347 /* put @_ back onto stack */
2348 AV* av = cx->blk_sub.argarray;
2350 items = AvFILLp(av) + 1;
2351 EXTEND(SP, items+1); /* @_ could have been extended. */
2352 Copy(AvARRAY(av), SP + 1, items, SV*);
2353 SvREFCNT_dec(GvAV(PL_defgv));
2354 GvAV(PL_defgv) = cx->blk_sub.savearray;
2356 /* abandon @_ if it got reified */
2361 av_extend(av, items-1);
2363 PAD_SVl(0) = (SV*)(cx->blk_sub.argarray = av);
2366 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
2367 AV* const av = GvAV(PL_defgv);
2368 items = AvFILLp(av) + 1;
2369 EXTEND(SP, items+1); /* @_ could have been extended. */
2370 Copy(AvARRAY(av), SP + 1, items, SV*);
2374 if (CxTYPE(cx) == CXt_SUB &&
2375 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
2376 SvREFCNT_dec(cx->blk_sub.cv);
2377 oldsave = PL_scopestack[PL_scopestack_ix - 1];
2378 LEAVE_SCOPE(oldsave);
2380 /* Now do some callish stuff. */
2382 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
2384 OP* const retop = cx->blk_sub.retop;
2389 for (index=0; index<items; index++)
2390 sv_2mortal(SP[-index]);
2393 /* XS subs don't have a CxSUB, so pop it */
2394 POPBLOCK(cx, PL_curpm);
2395 /* Push a mark for the start of arglist */
2398 (void)(*CvXSUB(cv))(aTHX_ cv);
2403 AV* const padlist = CvPADLIST(cv);
2404 if (CxTYPE(cx) == CXt_EVAL) {
2405 PL_in_eval = cx->blk_eval.old_in_eval;
2406 PL_eval_root = cx->blk_eval.old_eval_root;
2407 cx->cx_type = CXt_SUB;
2408 cx->blk_sub.hasargs = 0;
2410 cx->blk_sub.cv = cv;
2411 cx->blk_sub.olddepth = CvDEPTH(cv);
2414 if (CvDEPTH(cv) < 2)
2415 SvREFCNT_inc_simple_void_NN(cv);
2417 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION))
2418 sub_crush_depth(cv);
2419 pad_push(padlist, CvDEPTH(cv));
2422 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2423 if (cx->blk_sub.hasargs)
2425 AV* const av = (AV*)PAD_SVl(0);
2427 cx->blk_sub.savearray = GvAV(PL_defgv);
2428 GvAV(PL_defgv) = (AV*)SvREFCNT_inc_simple(av);
2429 CX_CURPAD_SAVE(cx->blk_sub);
2430 cx->blk_sub.argarray = av;
2432 if (items >= AvMAX(av) + 1) {
2433 SV **ary = AvALLOC(av);
2434 if (AvARRAY(av) != ary) {
2435 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2436 SvPV_set(av, (char*)ary);
2438 if (items >= AvMAX(av) + 1) {
2439 AvMAX(av) = items - 1;
2440 Renew(ary,items+1,SV*);
2442 SvPV_set(av, (char*)ary);
2446 Copy(mark,AvARRAY(av),items,SV*);
2447 AvFILLp(av) = items - 1;
2448 assert(!AvREAL(av));
2450 /* transfer 'ownership' of refcnts to new @_ */
2460 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
2462 * We do not care about using sv to call CV;
2463 * it's for informational purposes only.
2465 SV * const sv = GvSV(PL_DBsub);
2467 if (PERLDB_SUB_NN) {
2468 const int type = SvTYPE(sv);
2469 if (type < SVt_PVIV && type != SVt_IV)
2470 sv_upgrade(sv, SVt_PVIV);
2472 SvIV_set(sv, PTR2IV(cv)); /* Do it the quickest way */
2474 gv_efullname3(sv, CvGV(cv), NULL);
2477 CV * const gotocv = get_cv("DB::goto", FALSE);
2479 PUSHMARK( PL_stack_sp );
2480 call_sv((SV*)gotocv, G_SCALAR | G_NODEBUG);
2485 RETURNOP(CvSTART(cv));
2489 label = SvPV_nolen_const(sv);
2490 if (!(do_dump || *label))
2491 DIE(aTHX_ must_have_label);
2494 else if (PL_op->op_flags & OPf_SPECIAL) {
2496 DIE(aTHX_ must_have_label);
2499 label = cPVOP->op_pv;
2501 if (label && *label) {
2502 OP *gotoprobe = NULL;
2503 bool leaving_eval = FALSE;
2504 bool in_block = FALSE;
2505 PERL_CONTEXT *last_eval_cx = NULL;
2509 PL_lastgotoprobe = NULL;
2511 for (ix = cxstack_ix; ix >= 0; ix--) {
2513 switch (CxTYPE(cx)) {
2515 leaving_eval = TRUE;
2516 if (!CxTRYBLOCK(cx)) {
2517 gotoprobe = (last_eval_cx ?
2518 last_eval_cx->blk_eval.old_eval_root :
2523 /* else fall through */
2525 gotoprobe = cx->blk_oldcop->op_sibling;
2531 gotoprobe = cx->blk_oldcop->op_sibling;
2534 gotoprobe = PL_main_root;
2537 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
2538 gotoprobe = CvROOT(cx->blk_sub.cv);
2544 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
2547 DIE(aTHX_ "panic: goto");
2548 gotoprobe = PL_main_root;
2552 retop = dofindlabel(gotoprobe, label,
2553 enterops, enterops + GOTO_DEPTH);
2557 PL_lastgotoprobe = gotoprobe;
2560 DIE(aTHX_ "Can't find label %s", label);
2562 /* if we're leaving an eval, check before we pop any frames
2563 that we're not going to punt, otherwise the error
2566 if (leaving_eval && *enterops && enterops[1]) {
2568 for (i = 1; enterops[i]; i++)
2569 if (enterops[i]->op_type == OP_ENTERITER)
2570 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2573 /* pop unwanted frames */
2575 if (ix < cxstack_ix) {
2582 oldsave = PL_scopestack[PL_scopestack_ix];
2583 LEAVE_SCOPE(oldsave);
2586 /* push wanted frames */
2588 if (*enterops && enterops[1]) {
2589 OP * const oldop = PL_op;
2590 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2591 for (; enterops[ix]; ix++) {
2592 PL_op = enterops[ix];
2593 /* Eventually we may want to stack the needed arguments
2594 * for each op. For now, we punt on the hard ones. */
2595 if (PL_op->op_type == OP_ENTERITER)
2596 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2597 CALL_FPTR(PL_op->op_ppaddr)(aTHX);
2605 if (!retop) retop = PL_main_start;
2607 PL_restartop = retop;
2608 PL_do_undump = TRUE;
2612 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2613 PL_do_undump = FALSE;
2630 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
2632 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
2635 PL_exit_flags |= PERL_EXIT_EXPECTED;
2637 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2638 if (anum || !(PL_minus_c && PL_madskills))
2643 PUSHs(&PL_sv_undef);
2650 S_save_lines(pTHX_ AV *array, SV *sv)
2652 const char *s = SvPVX_const(sv);
2653 const char * const send = SvPVX_const(sv) + SvCUR(sv);
2656 while (s && s < send) {
2658 SV * const tmpstr = newSV(0);
2660 sv_upgrade(tmpstr, SVt_PVMG);
2661 t = strchr(s, '\n');
2667 sv_setpvn(tmpstr, s, t - s);
2668 av_store(array, line++, tmpstr);
2674 S_docatch_body(pTHX)
2682 S_docatch(pTHX_ OP *o)
2686 OP * const oldop = PL_op;
2690 assert(CATCH_GET == TRUE);
2697 assert(cxstack_ix >= 0);
2698 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2699 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
2704 /* die caught by an inner eval - continue inner loop */
2706 /* NB XXX we rely on the old popped CxEVAL still being at the top
2707 * of the stack; the way die_where() currently works, this
2708 * assumption is valid. In theory The cur_top_env value should be
2709 * returned in another global, the way retop (aka PL_restartop)
2711 assert(CxTYPE(&cxstack[cxstack_ix+1]) == CXt_EVAL);
2714 && cxstack[cxstack_ix+1].blk_eval.cur_top_env == PL_top_env)
2716 PL_op = PL_restartop;
2733 Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
2734 /* sv Text to convert to OP tree. */
2735 /* startop op_free() this to undo. */
2736 /* code Short string id of the caller. */
2738 /* FIXME - how much of this code is common with pp_entereval? */
2739 dVAR; dSP; /* Make POPBLOCK work. */
2746 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2747 char *tmpbuf = tbuf;
2750 CV* runcv = NULL; /* initialise to avoid compiler warnings */
2756 /* switch to eval mode */
2758 if (IN_PERL_COMPILETIME) {
2759 SAVECOPSTASH_FREE(&PL_compiling);
2760 CopSTASH_set(&PL_compiling, PL_curstash);
2762 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
2763 SV * const sv = sv_newmortal();
2764 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
2765 code, (unsigned long)++PL_evalseq,
2766 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
2771 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
2772 (unsigned long)++PL_evalseq);
2773 SAVECOPFILE_FREE(&PL_compiling);
2774 CopFILE_set(&PL_compiling, tmpbuf+2);
2775 SAVECOPLINE(&PL_compiling);
2776 CopLINE_set(&PL_compiling, 1);
2777 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2778 deleting the eval's FILEGV from the stash before gv_check() runs
2779 (i.e. before run-time proper). To work around the coredump that
2780 ensues, we always turn GvMULTI_on for any globals that were
2781 introduced within evals. See force_ident(). GSAR 96-10-12 */
2782 safestr = savepvn(tmpbuf, len);
2783 SAVEDELETE(PL_defstash, safestr, len);
2785 #ifdef OP_IN_REGISTER
2791 /* we get here either during compilation, or via pp_regcomp at runtime */
2792 runtime = IN_PERL_RUNTIME;
2794 runcv = find_runcv(NULL);
2797 PL_op->op_type = OP_ENTEREVAL;
2798 PL_op->op_flags = 0; /* Avoid uninit warning. */
2799 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
2800 PUSHEVAL(cx, 0, NULL);
2803 rop = doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
2805 rop = doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
2806 POPBLOCK(cx,PL_curpm);
2809 (*startop)->op_type = OP_NULL;
2810 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
2812 /* XXX DAPM do this properly one year */
2813 *padp = (AV*)SvREFCNT_inc_simple(PL_comppad);
2815 if (IN_PERL_COMPILETIME)
2816 CopHINTS_set(&PL_compiling, PL_hints);
2817 #ifdef OP_IN_REGISTER
2820 PERL_UNUSED_VAR(newsp);
2821 PERL_UNUSED_VAR(optype);
2828 =for apidoc find_runcv
2830 Locate the CV corresponding to the currently executing sub or eval.
2831 If db_seqp is non_null, skip CVs that are in the DB package and populate
2832 *db_seqp with the cop sequence number at the point that the DB:: code was
2833 entered. (allows debuggers to eval in the scope of the breakpoint rather
2834 than in the scope of the debugger itself).
2840 Perl_find_runcv(pTHX_ U32 *db_seqp)
2846 *db_seqp = PL_curcop->cop_seq;
2847 for (si = PL_curstackinfo; si; si = si->si_prev) {
2849 for (ix = si->si_cxix; ix >= 0; ix--) {
2850 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
2851 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
2852 CV * const cv = cx->blk_sub.cv;
2853 /* skip DB:: code */
2854 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
2855 *db_seqp = cx->blk_oldcop->cop_seq;
2860 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
2868 /* Compile a require/do, an eval '', or a /(?{...})/.
2869 * In the last case, startop is non-null, and contains the address of
2870 * a pointer that should be set to the just-compiled code.
2871 * outside is the lexically enclosing CV (if any) that invoked us.
2874 /* With USE_5005THREADS, eval_owner must be held on entry to doeval */
2876 S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
2879 OP * const saveop = PL_op;
2881 PL_in_eval = ((saveop && saveop->op_type == OP_REQUIRE)
2882 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
2887 SAVESPTR(PL_compcv);
2888 PL_compcv = (CV*)newSV(0);
2889 sv_upgrade((SV *)PL_compcv, SVt_PVCV);
2890 CvEVAL_on(PL_compcv);
2891 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2892 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
2894 CvOUTSIDE_SEQ(PL_compcv) = seq;
2895 CvOUTSIDE(PL_compcv) = (CV*)SvREFCNT_inc_simple(outside);
2897 /* set up a scratch pad */
2899 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
2900 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
2904 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
2906 /* make sure we compile in the right package */
2908 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
2909 SAVESPTR(PL_curstash);
2910 PL_curstash = CopSTASH(PL_curcop);
2912 SAVESPTR(PL_beginav);
2913 PL_beginav = newAV();
2914 SAVEFREESV(PL_beginav);
2915 SAVEI32(PL_error_count);
2918 SAVEI32(PL_madskills);
2922 /* try to compile it */
2924 PL_eval_root = NULL;
2926 PL_curcop = &PL_compiling;
2927 CopARYBASE_set(PL_curcop, 0);
2928 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
2929 PL_in_eval |= EVAL_KEEPERR;
2931 sv_setpvn(ERRSV,"",0);
2932 if (yyparse() || PL_error_count || !PL_eval_root) {
2933 SV **newsp; /* Used by POPBLOCK. */
2934 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
2935 I32 optype = 0; /* Might be reset by POPEVAL. */
2940 op_free(PL_eval_root);
2941 PL_eval_root = NULL;
2943 SP = PL_stack_base + POPMARK; /* pop original mark */
2945 POPBLOCK(cx,PL_curpm);
2951 msg = SvPVx_nolen_const(ERRSV);
2952 if (optype == OP_REQUIRE) {
2953 const SV * const nsv = cx->blk_eval.old_namesv;
2954 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
2956 DIE(aTHX_ "%sCompilation failed in require",
2957 *msg ? msg : "Unknown error\n");
2960 POPBLOCK(cx,PL_curpm);
2962 Perl_croak(aTHX_ "%sCompilation failed in regexp",
2963 (*msg ? msg : "Unknown error\n"));
2967 sv_setpv(ERRSV, "Compilation error");
2970 PERL_UNUSED_VAR(newsp);
2973 CopLINE_set(&PL_compiling, 0);
2975 *startop = PL_eval_root;
2977 SAVEFREEOP(PL_eval_root);
2979 /* Set the context for this new optree.
2980 * If the last op is an OP_REQUIRE, force scalar context.
2981 * Otherwise, propagate the context from the eval(). */
2982 if (PL_eval_root->op_type == OP_LEAVEEVAL
2983 && cUNOPx(PL_eval_root)->op_first->op_type == OP_LINESEQ
2984 && cLISTOPx(cUNOPx(PL_eval_root)->op_first)->op_last->op_type
2986 scalar(PL_eval_root);
2987 else if (gimme & G_VOID)
2988 scalarvoid(PL_eval_root);
2989 else if (gimme & G_ARRAY)
2992 scalar(PL_eval_root);
2994 DEBUG_x(dump_eval());
2996 /* Register with debugger: */
2997 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
2998 CV * const cv = get_cv("DB::postponed", FALSE);
3002 XPUSHs((SV*)CopFILEGV(&PL_compiling));
3004 call_sv((SV*)cv, G_DISCARD);
3008 /* compiled okay, so do it */
3010 CvDEPTH(PL_compcv) = 1;
3011 SP = PL_stack_base + POPMARK; /* pop original mark */
3012 PL_op = saveop; /* The caller may need it. */
3013 PL_lex_state = LEX_NOTPARSING; /* $^S needs this. */
3015 RETURNOP(PL_eval_start);
3019 S_check_type_and_open(pTHX_ const char *name, const char *mode)
3022 const int st_rc = PerlLIO_stat(name, &st);
3024 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
3028 return PerlIO_open(name, mode);
3032 S_doopen_pm(pTHX_ const char *name, const char *mode)
3034 #ifndef PERL_DISABLE_PMC
3035 const STRLEN namelen = strlen(name);
3038 if (namelen > 3 && strEQ(name + namelen - 3, ".pm")) {
3039 SV * const pmcsv = Perl_newSVpvf(aTHX_ "%s%c", name, 'c');
3040 const char * const pmc = SvPV_nolen_const(pmcsv);
3042 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
3043 fp = check_type_and_open(name, mode);
3046 fp = check_type_and_open(pmc, mode);
3048 SvREFCNT_dec(pmcsv);
3051 fp = check_type_and_open(name, mode);
3055 return check_type_and_open(name, mode);
3056 #endif /* !PERL_DISABLE_PMC */
3062 register PERL_CONTEXT *cx;
3066 const char *tryname = NULL;
3068 const I32 gimme = GIMME_V;
3069 int filter_has_file = 0;
3070 PerlIO *tryrsfp = NULL;
3071 SV *filter_cache = NULL;
3072 SV *filter_state = NULL;
3073 SV *filter_sub = NULL;
3079 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
3080 if ( SvVOK(sv) && ckWARN(WARN_PORTABLE) ) /* require v5.6.1 */
3081 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
3082 "v-string in use/require non-portable");
3084 sv = new_version(sv);
3085 if (!sv_derived_from(PL_patchlevel, "version"))
3086 upg_version(PL_patchlevel);
3087 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3088 if ( vcmp(sv,PL_patchlevel) <= 0 )
3089 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
3090 (void*)vnormal(sv), (void*)vnormal(PL_patchlevel));
3093 if ( vcmp(sv,PL_patchlevel) > 0 )
3094 DIE(aTHX_ "Perl %"SVf" required--this is only %"SVf", stopped",
3095 (void*)vnormal(sv), (void*)vnormal(PL_patchlevel));
3100 name = SvPV_const(sv, len);
3101 if (!(name && len > 0 && *name))
3102 DIE(aTHX_ "Null filename used");
3103 TAINT_PROPER("require");
3104 if (PL_op->op_type == OP_REQUIRE) {
3105 SV * const * const svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3107 if (*svp != &PL_sv_undef)
3110 DIE(aTHX_ "Compilation failed in require");
3114 /* prepare to compile file */
3116 if (path_is_absolute(name)) {
3118 tryrsfp = doopen_pm(name,PERL_SCRIPT_MODE);
3120 #ifdef MACOS_TRADITIONAL
3124 MacPerl_CanonDir(name, newname, 1);
3125 if (path_is_absolute(newname)) {
3127 tryrsfp = doopen_pm(newname,PERL_SCRIPT_MODE);
3132 AV * const ar = GvAVn(PL_incgv);
3136 if ((unixname = tounixspec(name, NULL)) != NULL)
3140 for (i = 0; i <= AvFILL(ar); i++) {
3141 SV * const dirsv = *av_fetch(ar, i, TRUE);
3147 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3148 && !sv_isobject(loader))
3150 loader = *av_fetch((AV *)SvRV(loader), 0, TRUE);
3153 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
3154 PTR2UV(SvRV(dirsv)), name);
3155 tryname = SvPVX_const(namesv);
3166 if (sv_isobject(loader))
3167 count = call_method("INC", G_ARRAY);
3169 count = call_sv(loader, G_ARRAY);
3179 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3180 && !isGV_with_GP(SvRV(arg))) {
3181 filter_cache = SvRV(arg);
3182 SvREFCNT_inc_simple_void_NN(filter_cache);
3189 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVGV) {
3193 if (SvTYPE(arg) == SVt_PVGV) {
3194 IO * const io = GvIO((GV *)arg);
3199 tryrsfp = IoIFP(io);
3200 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3201 PerlIO_close(IoOFP(io));
3212 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
3214 SvREFCNT_inc_simple_void_NN(filter_sub);
3217 filter_state = SP[i];
3218 SvREFCNT_inc_simple_void(filter_state);
3222 if (!tryrsfp && (filter_cache || filter_sub)) {
3223 tryrsfp = PerlIO_open(BIT_BUCKET,
3238 filter_has_file = 0;
3240 SvREFCNT_dec(filter_cache);
3241 filter_cache = NULL;
3244 SvREFCNT_dec(filter_state);
3245 filter_state = NULL;
3248 SvREFCNT_dec(filter_sub);
3253 if (!path_is_absolute(name)
3254 #ifdef MACOS_TRADITIONAL
3255 /* We consider paths of the form :a:b ambiguous and interpret them first
3256 as global then as local
3258 || (*name == ':' && name[1] != ':' && strchr(name+2, ':'))
3261 const char *dir = SvPVx_nolen_const(dirsv);
3262 #ifdef MACOS_TRADITIONAL
3266 MacPerl_CanonDir(name, buf2, 1);
3267 Perl_sv_setpvf(aTHX_ namesv, "%s%s", MacPerl_CanonDir(dir, buf1, 0), buf2+(buf2[0] == ':'));
3271 if ((unixdir = tounixpath(dir, NULL)) == NULL)
3273 sv_setpv(namesv, unixdir);
3274 sv_catpv(namesv, unixname);
3276 # ifdef __SYMBIAN32__
3277 if (PL_origfilename[0] &&
3278 PL_origfilename[1] == ':' &&
3279 !(dir[0] && dir[1] == ':'))
3280 Perl_sv_setpvf(aTHX_ namesv,
3285 Perl_sv_setpvf(aTHX_ namesv,
3289 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
3293 TAINT_PROPER("require");
3294 tryname = SvPVX_const(namesv);
3295 tryrsfp = doopen_pm(tryname, PERL_SCRIPT_MODE);
3297 if (tryname[0] == '.' && tryname[1] == '/')
3306 SAVECOPFILE_FREE(&PL_compiling);
3307 CopFILE_set(&PL_compiling, tryrsfp ? tryname : name);
3308 SvREFCNT_dec(namesv);
3310 if (PL_op->op_type == OP_REQUIRE) {
3311 const char *msgstr = name;
3312 if(errno == EMFILE) {
3314 = sv_2mortal(Perl_newSVpvf(aTHX_ "%s: %s", msgstr,
3316 msgstr = SvPV_nolen_const(msg);
3318 if (namesv) { /* did we lookup @INC? */
3319 AV * const ar = GvAVn(PL_incgv);
3321 SV * const msg = sv_2mortal(Perl_newSVpvf(aTHX_
3322 "%s in @INC%s%s (@INC contains:",
3324 (instr(msgstr, ".h ")
3325 ? " (change .h to .ph maybe?)" : ""),
3326 (instr(msgstr, ".ph ")
3327 ? " (did you run h2ph?)" : "")
3330 for (i = 0; i <= AvFILL(ar); i++) {
3331 sv_catpvs(msg, " ");
3332 sv_catsv(msg, *av_fetch(ar, i, TRUE));
3334 sv_catpvs(msg, ")");
3335 msgstr = SvPV_nolen_const(msg);
3338 DIE(aTHX_ "Can't locate %s", msgstr);
3344 SETERRNO(0, SS_NORMAL);
3346 /* Assume success here to prevent recursive requirement. */
3347 /* name is never assigned to again, so len is still strlen(name) */
3348 /* Check whether a hook in @INC has already filled %INC */
3350 (void)hv_store(GvHVn(PL_incgv), name, len, newSVpv(CopFILE(&PL_compiling),0),0);
3352 SV** const svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3354 (void)hv_store(GvHVn(PL_incgv), name, len, SvREFCNT_inc_simple(hook_sv), 0 );
3359 lex_start(sv_2mortal(newSVpvs("")));
3360 SAVEGENERICSV(PL_rsfp_filters);
3361 PL_rsfp_filters = NULL;
3366 SAVECOMPILEWARNINGS();
3367 if (PL_dowarn & G_WARN_ALL_ON)
3368 PL_compiling.cop_warnings = pWARN_ALL ;
3369 else if (PL_dowarn & G_WARN_ALL_OFF)
3370 PL_compiling.cop_warnings = pWARN_NONE ;
3371 else if (PL_taint_warn) {
3372 PL_compiling.cop_warnings
3373 = Perl_new_warnings_bitfield(aTHX_ NULL, WARN_TAINTstring, WARNsize);
3376 PL_compiling.cop_warnings = pWARN_STD ;
3377 SAVESPTR(PL_compiling.cop_io);
3378 PL_compiling.cop_io = NULL;
3380 if (filter_sub || filter_cache) {
3381 SV * const datasv = filter_add(S_run_user_filter, NULL);
3382 IoLINES(datasv) = filter_has_file;
3383 IoTOP_GV(datasv) = (GV *)filter_state;
3384 IoBOTTOM_GV(datasv) = (GV *)filter_sub;
3385 IoFMT_GV(datasv) = (GV *)filter_cache;
3388 /* switch to eval mode */
3389 PUSHBLOCK(cx, CXt_EVAL, SP);
3390 PUSHEVAL(cx, name, NULL);
3391 cx->blk_eval.retop = PL_op->op_next;
3393 SAVECOPLINE(&PL_compiling);
3394 CopLINE_set(&PL_compiling, 0);
3398 /* Store and reset encoding. */
3399 encoding = PL_encoding;
3402 op = DOCATCH(doeval(gimme, NULL, NULL, PL_curcop->cop_seq));
3404 /* Restore encoding. */
3405 PL_encoding = encoding;
3413 register PERL_CONTEXT *cx;
3415 const I32 gimme = GIMME_V;
3416 const I32 was = PL_sub_generation;
3417 char tbuf[TYPE_DIGITS(long) + 12];
3418 char *tmpbuf = tbuf;
3424 HV *saved_hh = NULL;
3425 const char * const fakestr = "_<(eval )";
3427 const int fakelen = 9 + 1;
3430 if (PL_op->op_private & OPpEVAL_HAS_HH) {
3431 saved_hh = (HV*) SvREFCNT_inc(POPs);
3435 if (!SvPV_nolen_const(sv))
3437 TAINT_PROPER("eval");
3443 /* switch to eval mode */
3445 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
3446 SV * const temp_sv = sv_newmortal();
3447 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
3448 (unsigned long)++PL_evalseq,
3449 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3450 tmpbuf = SvPVX(temp_sv);
3451 len = SvCUR(temp_sv);
3454 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
3455 SAVECOPFILE_FREE(&PL_compiling);
3456 CopFILE_set(&PL_compiling, tmpbuf+2);
3457 SAVECOPLINE(&PL_compiling);
3458 CopLINE_set(&PL_compiling, 1);
3459 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3460 deleting the eval's FILEGV from the stash before gv_check() runs
3461 (i.e. before run-time proper). To work around the coredump that
3462 ensues, we always turn GvMULTI_on for any globals that were
3463 introduced within evals. See force_ident(). GSAR 96-10-12 */
3464 safestr = savepvn(tmpbuf, len);
3465 SAVEDELETE(PL_defstash, safestr, len);
3467 PL_hints = PL_op->op_targ;
3469 GvHV(PL_hintgv) = saved_hh;
3470 SAVECOMPILEWARNINGS();
3471 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3472 SAVESPTR(PL_compiling.cop_io);
3473 if (specialCopIO(PL_curcop->cop_io))
3474 PL_compiling.cop_io = PL_curcop->cop_io;
3476 PL_compiling.cop_io = newSVsv(PL_curcop->cop_io);
3477 SAVEFREESV(PL_compiling.cop_io);
3479 if (PL_compiling.cop_hints) {
3480 Perl_refcounted_he_free(aTHX_ PL_compiling.cop_hints);
3482 PL_compiling.cop_hints = PL_curcop->cop_hints;
3483 if (PL_compiling.cop_hints) {
3485 PL_compiling.cop_hints->refcounted_he_refcnt++;
3486 HINTS_REFCNT_UNLOCK;
3488 /* special case: an eval '' executed within the DB package gets lexically
3489 * placed in the first non-DB CV rather than the current CV - this
3490 * allows the debugger to execute code, find lexicals etc, in the
3491 * scope of the code being debugged. Passing &seq gets find_runcv
3492 * to do the dirty work for us */
3493 runcv = find_runcv(&seq);
3495 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
3496 PUSHEVAL(cx, 0, NULL);
3497 cx->blk_eval.retop = PL_op->op_next;
3499 /* prepare to compile string */
3501 if (PERLDB_LINE && PL_curstash != PL_debstash)
3502 save_lines(CopFILEAV(&PL_compiling), PL_linestr);
3504 ret = doeval(gimme, NULL, runcv, seq);
3505 if (PERLDB_INTER && was != (I32)PL_sub_generation /* Some subs defined here. */
3506 && ret != PL_op->op_next) { /* Successive compilation. */
3507 /* Copy in anything fake and short. */
3509 strlcpy(safestr, fakestr, fakelen);
3511 strcpy(safestr, fakestr);
3512 #endif /* #ifdef HAS_STRLCPY */
3514 return DOCATCH(ret);
3524 register PERL_CONTEXT *cx;
3526 const U8 save_flags = PL_op -> op_flags;
3531 retop = cx->blk_eval.retop;
3534 if (gimme == G_VOID)
3536 else if (gimme == G_SCALAR) {
3539 if (SvFLAGS(TOPs) & SVs_TEMP)
3542 *MARK = sv_mortalcopy(TOPs);
3546 *MARK = &PL_sv_undef;
3551 /* in case LEAVE wipes old return values */
3552 for (mark = newsp + 1; mark <= SP; mark++) {
3553 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
3554 *mark = sv_mortalcopy(*mark);
3555 TAINT_NOT; /* Each item is independent */
3559 PL_curpm = newpm; /* Don't pop $1 et al till now */
3562 assert(CvDEPTH(PL_compcv) == 1);
3564 CvDEPTH(PL_compcv) = 0;
3567 if (optype == OP_REQUIRE &&
3568 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
3570 /* Unassume the success we assumed earlier. */
3571 SV * const nsv = cx->blk_eval.old_namesv;
3572 (void)hv_delete(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv), G_DISCARD);
3573 retop = Perl_die(aTHX_ "%"SVf" did not return a true value", (void*)nsv);
3574 /* die_where() did LEAVE, or we won't be here */
3578 if (!(save_flags & OPf_SPECIAL))
3579 sv_setpvn(ERRSV,"",0);
3585 /* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
3586 close to the related Perl_create_eval_scope. */
3588 Perl_delete_eval_scope(pTHX)
3593 register PERL_CONTEXT *cx;
3600 PERL_UNUSED_VAR(newsp);
3601 PERL_UNUSED_VAR(gimme);
3602 PERL_UNUSED_VAR(optype);
3605 /* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
3606 also needed by Perl_fold_constants. */
3608 Perl_create_eval_scope(pTHX_ U32 flags)
3611 const I32 gimme = GIMME_V;
3616 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
3618 PL_eval_root = PL_op; /* Only needed so that goto works right. */
3620 PL_in_eval = EVAL_INEVAL;
3621 if (flags & G_KEEPERR)
3622 PL_in_eval |= EVAL_KEEPERR;
3624 sv_setpvn(ERRSV,"",0);
3625 if (flags & G_FAKINGEVAL) {
3626 PL_eval_root = PL_op; /* Only needed so that goto works right. */
3634 PERL_CONTEXT * const cx = create_eval_scope(0);
3635 cx->blk_eval.retop = cLOGOP->op_other->op_next;
3636 return DOCATCH(PL_op->op_next);
3645 register PERL_CONTEXT *cx;
3650 PERL_UNUSED_VAR(optype);
3653 if (gimme == G_VOID)
3655 else if (gimme == G_SCALAR) {
3659 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
3662 *MARK = sv_mortalcopy(TOPs);
3666 *MARK = &PL_sv_undef;
3671 /* in case LEAVE wipes old return values */
3673 for (mark = newsp + 1; mark <= SP; mark++) {
3674 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
3675 *mark = sv_mortalcopy(*mark);
3676 TAINT_NOT; /* Each item is independent */
3680 PL_curpm = newpm; /* Don't pop $1 et al till now */
3683 sv_setpvn(ERRSV,"",0);
3690 register PERL_CONTEXT *cx;
3691 const I32 gimme = GIMME_V;
3696 if (PL_op->op_targ == 0) {
3697 SV ** const defsv_p = &GvSV(PL_defgv);
3698 *defsv_p = newSVsv(POPs);
3699 SAVECLEARSV(*defsv_p);
3702 sv_setsv(PAD_SV(PL_op->op_targ), POPs);
3704 PUSHBLOCK(cx, CXt_GIVEN, SP);
3713 register PERL_CONTEXT *cx;
3717 PERL_UNUSED_CONTEXT;
3720 assert(CxTYPE(cx) == CXt_GIVEN);
3725 PL_curpm = newpm; /* pop $1 et al */
3732 /* Helper routines used by pp_smartmatch */
3735 S_make_matcher(pTHX_ regexp *re)
3738 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
3739 PM_SETRE(matcher, ReREFCNT_inc(re));
3741 SAVEFREEOP((OP *) matcher);
3749 S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
3754 PL_op = (OP *) matcher;
3759 return (SvTRUEx(POPs));
3764 S_destroy_matcher(pTHX_ PMOP *matcher)
3767 PERL_UNUSED_ARG(matcher);
3772 /* Do a smart match */
3775 return do_smartmatch(NULL, NULL);
3778 /* This version of do_smartmatch() implements the following
3779 table of smart matches:
3781 $a $b Type of Match Implied Matching Code
3782 ====== ===== ===================== =============
3783 (overloading trumps everything)
3785 Code[+] Code[+] referential equality match if refaddr($a) == refaddr($b)
3786 Any Code[+] scalar sub truth match if $b->($a)
3788 Hash Hash hash keys identical match if sort(keys(%$a)) ÈeqÇ sort(keys(%$b))
3789 Hash Array hash value slice truth match if $a->{any(@$b)}
3790 Hash Regex hash key grep match if any(keys(%$a)) =~ /$b/
3791 Hash Any hash entry existence match if exists $a->{$b}
3793 Array Array arrays are identical[*] match if $a È~~Ç $b
3794 Array Regex array grep match if any(@$a) =~ /$b/
3795 Array Num array contains number match if any($a) == $b
3796 Array Any array contains string match if any($a) eq $b
3798 Any undef undefined match if !defined $a
3799 Any Regex pattern match match if $a =~ /$b/
3800 Code() Code() results are equal match if $a->() eq $b->()
3801 Any Code() simple closure truth match if $b->() (ignoring $a)
3802 Num numish[!] numeric equality match if $a == $b
3803 Any Str string equality match if $a eq $b
3804 Any Num numeric equality match if $a == $b
3806 Any Any string equality match if $a eq $b
3809 + - this must be a code reference whose prototype (if present) is not ""
3810 (subs with a "" prototype are dealt with by the 'Code()' entry lower down)
3811 * - if a circular reference is found, we fall back to referential equality
3812 ! - either a real number, or a string that looks_like_number()
3817 S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
3822 SV *e = TOPs; /* e is for 'expression' */
3823 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
3826 regexp *this_regex, *other_regex;
3828 # define NOT_EMPTY_PROTO(cv) (!SvPOK(cv) || SvCUR(cv) == 0)
3830 # define SM_REF(type) ( \
3831 (SvROK(d) && (SvTYPE(this = SvRV(d)) == SVt_##type) && (other = e)) \
3832 || (SvROK(e) && (SvTYPE(this = SvRV(e)) == SVt_##type) && (other = d)))
3834 # define SM_CV_NEP /* Find a code ref without an empty prototype */ \
3835 ((SvROK(d) && (SvTYPE(this = SvRV(d)) == SVt_PVCV) \
3836 && NOT_EMPTY_PROTO(this) && (other = e)) \
3837 || (SvROK(e) && (SvTYPE(this = SvRV(e)) == SVt_PVCV) \
3838 && NOT_EMPTY_PROTO(this) && (other = d)))
3840 # define SM_REGEX ( \
3841 (SvROK(d) && SvMAGICAL(this = SvRV(d)) \
3842 && (mg = mg_find(this, PERL_MAGIC_qr)) \
3843 && (this_regex = (regexp *)mg->mg_obj) \
3846 (SvROK(e) && SvMAGICAL(this = SvRV(e)) \
3847 && (mg = mg_find(this, PERL_MAGIC_qr)) \
3848 && (this_regex = (regexp *)mg->mg_obj) \
3852 # define SM_OTHER_REF(type) \
3853 (SvROK(other) && SvTYPE(SvRV(other)) == SVt_##type)
3855 # define SM_OTHER_REGEX (SvROK(other) && SvMAGICAL(SvRV(other)) \
3856 && (mg = mg_find(SvRV(other), PERL_MAGIC_qr)) \
3857 && (other_regex = (regexp *)mg->mg_obj))
3860 # define SM_SEEN_THIS(sv) hv_exists_ent(seen_this, \
3861 sv_2mortal(newSViv(PTR2IV(sv))), 0)
3863 # define SM_SEEN_OTHER(sv) hv_exists_ent(seen_other, \
3864 sv_2mortal(newSViv(PTR2IV(sv))), 0)
3866 tryAMAGICbinSET(smart, 0);
3868 SP -= 2; /* Pop the values */
3870 /* Take care only to invoke mg_get() once for each argument.
3871 * Currently we do this by copying the SV if it's magical. */
3874 d = sv_mortalcopy(d);
3881 e = sv_mortalcopy(e);
3886 if ( SM_OTHER_REF(PVCV) && NOT_EMPTY_PROTO(SvRV(other)) )
3888 if (this == SvRV(other))
3899 c = call_sv(this, G_SCALAR);
3903 else if (SvTEMP(TOPs))
3904 SvREFCNT_inc_void(TOPs);
3909 else if (SM_REF(PVHV)) {
3910 if (SM_OTHER_REF(PVHV)) {
3911 /* Check that the key-sets are identical */
3913 HV *other_hv = (HV *) SvRV(other);
3915 bool other_tied = FALSE;
3916 U32 this_key_count = 0,
3917 other_key_count = 0;
3919 /* Tied hashes don't know how many keys they have. */
3920 if (SvTIED_mg(this, PERL_MAGIC_tied)) {
3923 else if (SvTIED_mg((SV *) other_hv, PERL_MAGIC_tied)) {
3924 HV * const temp = other_hv;
3925 other_hv = (HV *) this;
3929 if (SvTIED_mg((SV *) other_hv, PERL_MAGIC_tied))
3932 if (!tied && HvUSEDKEYS((HV *) this) != HvUSEDKEYS(other_hv))
3935 /* The hashes have the same number of keys, so it suffices
3936 to check that one is a subset of the other. */
3937 (void) hv_iterinit((HV *) this);
3938 while ( (he = hv_iternext((HV *) this)) ) {
3940 char * const key = hv_iterkey(he, &key_len);
3944 if(!hv_exists(other_hv, key, key_len)) {
3945 (void) hv_iterinit((HV *) this); /* reset iterator */
3951 (void) hv_iterinit(other_hv);
3952 while ( hv_iternext(other_hv) )
3956 other_key_count = HvUSEDKEYS(other_hv);
3958 if (this_key_count != other_key_count)
3963 else if (SM_OTHER_REF(PVAV)) {
3964 AV * const other_av = (AV *) SvRV(other);
3965 const I32 other_len = av_len(other_av) + 1;
3968 if (HvUSEDKEYS((HV *) this) != other_len)
3971 for(i = 0; i < other_len; ++i) {
3972 SV ** const svp = av_fetch(other_av, i, FALSE);
3976 if (!svp) /* ??? When can this happen? */
3979 key = SvPV(*svp, key_len);
3980 if(!hv_exists((HV *) this, key, key_len))
3985 else if (SM_OTHER_REGEX) {
3986 PMOP * const matcher = make_matcher(other_regex);
3989 (void) hv_iterinit((HV *) this);
3990 while ( (he = hv_iternext((HV *) this)) ) {
3991 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
3992 (void) hv_iterinit((HV *) this);
3993 destroy_matcher(matcher);
3997 destroy_matcher(matcher);
4001 if (hv_exists_ent((HV *) this, other, 0))
4007 else if (SM_REF(PVAV)) {
4008 if (SM_OTHER_REF(PVAV)) {
4009 AV *other_av = (AV *) SvRV(other);
4010 if (av_len((AV *) this) != av_len(other_av))
4014 const I32 other_len = av_len(other_av);
4016 if (NULL == seen_this) {
4017 seen_this = newHV();
4018 (void) sv_2mortal((SV *) seen_this);
4020 if (NULL == seen_other) {
4021 seen_this = newHV();
4022 (void) sv_2mortal((SV *) seen_other);
4024 for(i = 0; i <= other_len; ++i) {
4025 SV * const * const this_elem = av_fetch((AV *)this, i, FALSE);
4026 SV * const * const other_elem = av_fetch(other_av, i, FALSE);
4028 if (!this_elem || !other_elem) {
4029 if (this_elem || other_elem)
4032 else if (SM_SEEN_THIS(*this_elem)
4033 || SM_SEEN_OTHER(*other_elem))
4035 if (*this_elem != *other_elem)
4039 hv_store_ent(seen_this,
4040 sv_2mortal(newSViv(PTR2IV(*this_elem))),
4042 hv_store_ent(seen_other,
4043 sv_2mortal(newSViv(PTR2IV(*other_elem))),
4049 (void) do_smartmatch(seen_this, seen_other);
4059 else if (SM_OTHER_REGEX) {
4060 PMOP * const matcher = make_matcher(other_regex);
4061 const I32 this_len = av_len((AV *) this);
4064 for(i = 0; i <= this_len; ++i) {
4065 SV * const * const svp = av_fetch((AV *)this, i, FALSE);
4066 if (svp && matcher_matches_sv(matcher, *svp)) {
4067 destroy_matcher(matcher);
4071 destroy_matcher(matcher);
4074 else if (SvIOK(other) || SvNOK(other)) {
4077 for(i = 0; i <= AvFILL((AV *) this); ++i) {
4078 SV * const * const svp = av_fetch((AV *)this, i, FALSE);
4085 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
4095 else if (SvPOK(other)) {
4096 const I32 this_len = av_len((AV *) this);
4099 for(i = 0; i <= this_len; ++i) {
4100 SV * const * const svp = av_fetch((AV *)this, i, FALSE);
4115 else if (!SvOK(d) || !SvOK(e)) {
4116 if (!SvOK(d) && !SvOK(e))
4121 else if (SM_REGEX) {
4122 PMOP * const matcher = make_matcher(this_regex);
4125 PUSHs(matcher_matches_sv(matcher, other)
4128 destroy_matcher(matcher);
4131 else if (SM_REF(PVCV)) {
4133 /* This must be a null-prototyped sub, because we
4134 already checked for the other kind. */
4140 c = call_sv(this, G_SCALAR);
4143 PUSHs(&PL_sv_undef);
4144 else if (SvTEMP(TOPs))
4145 SvREFCNT_inc_void(TOPs);
4147 if (SM_OTHER_REF(PVCV)) {
4148 /* This one has to be null-proto'd too.
4149 Call both of 'em, and compare the results */
4151 c = call_sv(SvRV(other), G_SCALAR);
4154 PUSHs(&PL_sv_undef);
4155 else if (SvTEMP(TOPs))
4156 SvREFCNT_inc_void(TOPs);
4167 else if ( ((SvIOK(d) || SvNOK(d)) && (this = d) && (other = e))
4168 || ((SvIOK(e) || SvNOK(e)) && (this = e) && (other = d)) )
4170 if (SvPOK(other) && !looks_like_number(other)) {
4171 /* String comparison */
4176 /* Otherwise, numeric comparison */
4179 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
4190 /* As a last resort, use string comparison */
4199 register PERL_CONTEXT *cx;
4200 const I32 gimme = GIMME_V;
4202 /* This is essentially an optimization: if the match
4203 fails, we don't want to push a context and then
4204 pop it again right away, so we skip straight
4205 to the op that follows the leavewhen.
4207 if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs))
4208 return cLOGOP->op_other->op_next;
4213 PUSHBLOCK(cx, CXt_WHEN, SP);
4222 register PERL_CONTEXT *cx;
4228 assert(CxTYPE(cx) == CXt_WHEN);
4233 PL_curpm = newpm; /* pop $1 et al */
4243 register PERL_CONTEXT *cx;
4246 cxix = dopoptowhen(cxstack_ix);
4248 DIE(aTHX_ "Can't \"continue\" outside a when block");
4249 if (cxix < cxstack_ix)
4252 /* clear off anything above the scope we're re-entering */
4253 inner = PL_scopestack_ix;
4255 if (PL_scopestack_ix < inner)
4256 leave_scope(PL_scopestack[PL_scopestack_ix]);
4257 PL_curcop = cx->blk_oldcop;
4258 return cx->blk_givwhen.leave_op;
4265 register PERL_CONTEXT *cx;
4268 cxix = dopoptogiven(cxstack_ix);
4270 if (PL_op->op_flags & OPf_SPECIAL)
4271 DIE(aTHX_ "Can't use when() outside a topicalizer");
4273 DIE(aTHX_ "Can't \"break\" outside a given block");
4275 if (CxFOREACH(&cxstack[cxix]) && (0 == (PL_op->op_flags & OPf_SPECIAL)))
4276 DIE(aTHX_ "Can't \"break\" in a loop topicalizer");
4278 if (cxix < cxstack_ix)
4281 /* clear off anything above the scope we're re-entering */
4282 inner = PL_scopestack_ix;
4284 if (PL_scopestack_ix < inner)
4285 leave_scope(PL_scopestack[PL_scopestack_ix]);
4286 PL_curcop = cx->blk_oldcop;
4289 return cx->blk_loop.next_op;
4291 return cx->blk_givwhen.leave_op;
4295 S_doparseform(pTHX_ SV *sv)
4298 register char *s = SvPV_force(sv, len);
4299 register char * const send = s + len;
4300 register char *base = NULL;
4301 register I32 skipspaces = 0;
4302 bool noblank = FALSE;
4303 bool repeat = FALSE;
4304 bool postspace = FALSE;
4310 bool unchopnum = FALSE;
4311 int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */
4314 Perl_croak(aTHX_ "Null picture in formline");
4316 /* estimate the buffer size needed */
4317 for (base = s; s <= send; s++) {
4318 if (*s == '\n' || *s == '@' || *s == '^')
4324 Newx(fops, maxops, U32);
4329 *fpc++ = FF_LINEMARK;
4330 noblank = repeat = FALSE;
4348 case ' ': case '\t':
4355 } /* else FALL THROUGH */
4363 *fpc++ = FF_LITERAL;
4371 *fpc++ = (U16)skipspaces;
4375 *fpc++ = FF_NEWLINE;
4379 arg = fpc - linepc + 1;
4386 *fpc++ = FF_LINEMARK;
4387 noblank = repeat = FALSE;
4396 ischop = s[-1] == '^';
4402 arg = (s - base) - 1;
4404 *fpc++ = FF_LITERAL;
4412 *fpc++ = 2; /* skip the @* or ^* */
4414 *fpc++ = FF_LINESNGL;
4417 *fpc++ = FF_LINEGLOB;
4419 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
4420 arg = ischop ? 512 : 0;
4425 const char * const f = ++s;
4428 arg |= 256 + (s - f);
4430 *fpc++ = s - base; /* fieldsize for FETCH */
4431 *fpc++ = FF_DECIMAL;
4433 unchopnum |= ! ischop;
4435 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
4436 arg = ischop ? 512 : 0;
4438 s++; /* skip the '0' first */
4442 const char * const f = ++s;
4445 arg |= 256 + (s - f);
4447 *fpc++ = s - base; /* fieldsize for FETCH */
4448 *fpc++ = FF_0DECIMAL;
4450 unchopnum |= ! ischop;
4454 bool ismore = FALSE;
4457 while (*++s == '>') ;
4458 prespace = FF_SPACE;
4460 else if (*s == '|') {
4461 while (*++s == '|') ;
4462 prespace = FF_HALFSPACE;
4467 while (*++s == '<') ;
4470 if (*s == '.' && s[1] == '.' && s[2] == '.') {
4474 *fpc++ = s - base; /* fieldsize for FETCH */
4476 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
4479 *fpc++ = (U16)prespace;
4493 assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */
4495 { /* need to jump to the next word */
4497 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
4498 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U32) + 4);
4499 s = SvPVX(sv) + SvCUR(sv) + z;
4501 Copy(fops, s, arg, U32);
4503 sv_magic(sv, NULL, PERL_MAGIC_fm, NULL, 0);
4506 if (unchopnum && repeat)
4507 DIE(aTHX_ "Repeated format line will never terminate (~~ and @#)");
4513 S_num_overflow(NV value, I32 fldsize, I32 frcsize)
4515 /* Can value be printed in fldsize chars, using %*.*f ? */
4519 int intsize = fldsize - (value < 0 ? 1 : 0);
4526 while (intsize--) pwr *= 10.0;
4527 while (frcsize--) eps /= 10.0;
4530 if (value + eps >= pwr)
4533 if (value - eps <= -pwr)
4540 S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
4543 SV * const datasv = FILTER_DATA(idx);
4544 const int filter_has_file = IoLINES(datasv);
4545 SV * const filter_state = (SV *)IoTOP_GV(datasv);
4546 SV * const filter_sub = (SV *)IoBOTTOM_GV(datasv);
4550 const char *got_p = NULL;
4551 const char *prune_from = NULL;
4552 bool read_from_cache = FALSE;
4555 assert(maxlen >= 0);
4558 /* I was having segfault trouble under Linux 2.2.5 after a
4559 parse error occured. (Had to hack around it with a test
4560 for PL_error_count == 0.) Solaris doesn't segfault --
4561 not sure where the trouble is yet. XXX */
4563 if (IoFMT_GV(datasv)) {
4564 SV *const cache = (SV *)IoFMT_GV(datasv);
4567 const char *cache_p = SvPV(cache, cache_len);
4571 /* Running in block mode and we have some cached data already.
4573 if (cache_len >= umaxlen) {
4574 /* In fact, so much data we don't even need to call
4579 const char *const first_nl = memchr(cache_p, '\n', cache_len);
4581 take = first_nl + 1 - cache_p;
4585 sv_catpvn(buf_sv, cache_p, take);
4586 sv_chop(cache, cache_p + take);
4587 /* Definately not EOF */
4591 sv_catsv(buf_sv, cache);
4593 umaxlen -= cache_len;
4596 read_from_cache = TRUE;
4600 /* Filter API says that the filter appends to the contents of the buffer.
4601 Usually the buffer is "", so the details don't matter. But if it's not,
4602 then clearly what it contains is already filtered by this filter, so we
4603 don't want to pass it in a second time.
4604 I'm going to use a mortal in case the upstream filter croaks. */
4605 upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv))
4606 ? sv_newmortal() : buf_sv;
4607 SvUPGRADE(upstream, SVt_PV);
4609 if (filter_has_file) {
4610 status = FILTER_READ(idx+1, upstream, 0);
4613 if (filter_sub && status >= 0) {
4624 PUSHs(sv_2mortal(newSViv(0)));
4626 PUSHs(filter_state);
4629 count = call_sv(filter_sub, G_SCALAR);
4644 if(SvOK(upstream)) {
4645 got_p = SvPV(upstream, got_len);
4647 if (got_len > umaxlen) {
4648 prune_from = got_p + umaxlen;
4651 const char *const first_nl = memchr(got_p, '\n', got_len);
4652 if (first_nl && first_nl + 1 < got_p + got_len) {
4653 /* There's a second line here... */
4654 prune_from = first_nl + 1;
4659 /* Oh. Too long. Stuff some in our cache. */
4660 STRLEN cached_len = got_p + got_len - prune_from;
4661 SV *cache = (SV *)IoFMT_GV(datasv);
4664 IoFMT_GV(datasv) = (GV*) (cache = newSV(got_len - umaxlen));
4665 } else if (SvOK(cache)) {
4666 /* Cache should be empty. */
4667 assert(!SvCUR(cache));
4670 sv_setpvn(cache, prune_from, cached_len);
4671 /* If you ask for block mode, you may well split UTF-8 characters.
4672 "If it breaks, you get to keep both parts"
4673 (Your code is broken if you don't put them back together again
4674 before something notices.) */
4675 if (SvUTF8(upstream)) {
4678 SvCUR_set(upstream, got_len - cached_len);
4679 /* Can't yet be EOF */
4684 /* If they are at EOF but buf_sv has something in it, then they may never
4685 have touched the SV upstream, so it may be undefined. If we naively
4686 concatenate it then we get a warning about use of uninitialised value.
4688 if (upstream != buf_sv && (SvOK(upstream) || SvGMAGICAL(upstream))) {
4689 sv_catsv(buf_sv, upstream);
4693 IoLINES(datasv) = 0;
4694 SvREFCNT_dec(IoFMT_GV(datasv));
4696 SvREFCNT_dec(filter_state);
4697 IoTOP_GV(datasv) = NULL;
4700 SvREFCNT_dec(filter_sub);
4701 IoBOTTOM_GV(datasv) = NULL;
4703 filter_del(S_run_user_filter);
4705 if (status == 0 && read_from_cache) {
4706 /* If we read some data from the cache (and by getting here it implies
4707 that we emptied the cache) then we aren't yet at EOF, and mustn't
4708 report that to our caller. */
4714 /* perhaps someone can come up with a better name for
4715 this? it is not really "absolute", per se ... */
4717 S_path_is_absolute(const char *name)
4719 if (PERL_FILE_IS_ABSOLUTE(name)
4720 #ifdef MACOS_TRADITIONAL
4723 || (*name == '.' && (name[1] == '/' ||
4724 (name[1] == '.' && name[2] == '/')))
4736 * c-indentation-style: bsd
4738 * indent-tabs-mode: t
4741 * ex: set ts=8 sts=4 sw=4 noet: