This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
more mods to -Dl debugging output
[perl5.git] / pp_ctl.c
CommitLineData
a0d0e21e
LW
1/* pp_ctl.c
2 *
1129b882
NC
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
a0d0e21e
LW
5 *
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.
8 *
9 */
10
11/*
4ac71550
TC
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.
18 *
19 * [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
a0d0e21e
LW
20 */
21
166f8a29
DM
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.
27 *
28 * Control-oriented means things like pp_enteriter() and pp_next(), which
29 * alter the flow of control of the program.
30 */
31
32
a0d0e21e 33#include "EXTERN.h"
864dbfa3 34#define PERL_IN_PP_CTL_C
a0d0e21e
LW
35#include "perl.h"
36
37#ifndef WORD_ALIGN
dea28490 38#define WORD_ALIGN sizeof(U32)
a0d0e21e
LW
39#endif
40
54310121 41#define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
1e422769 42
94fcd414
NC
43#define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
44
a0d0e21e
LW
45PP(pp_wantarray)
46{
97aff369 47 dVAR;
39644a26 48 dSP;
a0d0e21e
LW
49 I32 cxix;
50 EXTEND(SP, 1);
51
52 cxix = dopoptosub(cxstack_ix);
53 if (cxix < 0)
54 RETPUSHUNDEF;
55
54310121
PP
56 switch (cxstack[cxix].blk_gimme) {
57 case G_ARRAY:
a0d0e21e 58 RETPUSHYES;
54310121 59 case G_SCALAR:
a0d0e21e 60 RETPUSHNO;
54310121
PP
61 default:
62 RETPUSHUNDEF;
63 }
a0d0e21e
LW
64}
65
2cd61cdb
IZ
66PP(pp_regcreset)
67{
97aff369 68 dVAR;
2cd61cdb
IZ
69 /* XXXX Should store the old value to allow for tie/overload - and
70 restore in regcomp, where marked with XXXX. */
3280af22 71 PL_reginterp_cnt = 0;
0b4182de 72 TAINT_NOT;
2cd61cdb
IZ
73 return NORMAL;
74}
75
b3eb6a9b
GS
76PP(pp_regcomp)
77{
97aff369 78 dVAR;
39644a26 79 dSP;
a0d0e21e 80 register PMOP *pm = (PMOP*)cLOGOP->op_other;
a0d0e21e 81 SV *tmpstr;
84679df5 82 REGEXP *re = NULL;
bfed75c6 83
4b5a0d1c 84 /* prevent recompiling under /o and ithreads. */
3db8f154 85#if defined(USE_ITHREADS)
131b3ad0
DM
86 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
87 if (PL_op->op_flags & OPf_STACKED) {
88 dMARK;
89 SP = MARK;
90 }
91 else
92 (void)POPs;
93 RETURN;
94 }
513629ba 95#endif
d4b87e75
BM
96
97#define tryAMAGICregexp(rx) \
98 STMT_START { \
99 if (SvROK(rx) && SvAMAGIC(rx)) { \
100 SV *sv = AMG_CALLun(rx, regexp); \
101 if (sv) { \
102 if (SvROK(sv)) \
103 sv = SvRV(sv); \
104 if (SvTYPE(sv) != SVt_REGEXP) \
105 Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \
106 rx = sv; \
107 } \
108 } \
109 } STMT_END
110
111
131b3ad0
DM
112 if (PL_op->op_flags & OPf_STACKED) {
113 /* multiple args; concatentate them */
114 dMARK; dORIGMARK;
115 tmpstr = PAD_SV(ARGTARG);
76f68e9b 116 sv_setpvs(tmpstr, "");
131b3ad0 117 while (++MARK <= SP) {
d4b87e75 118 SV *msv = *MARK;
131b3ad0
DM
119 if (PL_amagic_generation) {
120 SV *sv;
d4b87e75
BM
121
122 tryAMAGICregexp(msv);
123
124 if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) &&
125 (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign)))
131b3ad0
DM
126 {
127 sv_setsv(tmpstr, sv);
128 continue;
129 }
130 }
d4b87e75 131 sv_catsv(tmpstr, msv);
131b3ad0
DM
132 }
133 SvSETMAGIC(tmpstr);
134 SP = ORIGMARK;
135 }
d4b87e75 136 else {
131b3ad0 137 tmpstr = POPs;
d4b87e75
BM
138 tryAMAGICregexp(tmpstr);
139 }
140
141#undef tryAMAGICregexp
513629ba 142
b3eb6a9b 143 if (SvROK(tmpstr)) {
d8f6592e 144 SV * const sv = SvRV(tmpstr);
5c35adbb 145 if (SvTYPE(sv) == SVt_REGEXP)
d2f13c59 146 re = (REGEXP*) sv;
c277df42 147 }
d4b87e75
BM
148 else if (SvTYPE(tmpstr) == SVt_REGEXP)
149 re = (REGEXP*) tmpstr;
150
5c35adbb 151 if (re) {
69dc4b30
FC
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. */
455d9033
FC
158 if(pm->op_type == OP_MATCH) {
159 SV *lhs;
160 const bool was_tainted = PL_tainted;
161 if (pm->op_flags & OPf_STACKED)
69dc4b30 162 lhs = TOPs;
455d9033
FC
163 else if (pm->op_private & OPpTARGET_MY)
164 lhs = PAD_SV(pm->op_targ);
165 else lhs = DEFSV;
166 SvGETMAGIC(lhs);
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;
171 }
69dc4b30 172
f0826785 173 re = reg_temp_copy(NULL, re);
aaa362c4 174 ReREFCNT_dec(PM_GETRE(pm));
28d8d7f4 175 PM_SETRE(pm, re);
c277df42
IZ
176 }
177 else {
e62f0680 178 STRLEN len;
3ab4a224 179 const char *t = SvOK(tmpstr) ? SvPV_const(tmpstr, len) : "";
c737faaf 180 re = PM_GETRE(pm);
14a49a24 181 assert (re != (REGEXP*) &PL_sv_undef);
c277df42 182
20408e3c 183 /* Check against the last compiled regexp. */
a11c8683 184 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
220fc49f 185 memNE(RX_PRECOMP(re), t, len))
85aff577 186 {
07bc277f 187 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
c737faaf 188 U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME;
d8f6592e
AL
189 if (re) {
190 ReREFCNT_dec(re);
14a49a24
NC
191#ifdef USE_ITHREADS
192 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
193#else
4608196e 194 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
14a49a24 195#endif
1e2e3d02
YO
196 } else if (PL_curcop->cop_hints_hash) {
197 SV *ptr = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, 0,
198 "regcomp", 7, 0, 0);
199 if (ptr && SvIOK(ptr) && SvIV(ptr))
200 eng = INT2PTR(regexp_engine*,SvIV(ptr));
c277df42 201 }
664e119d 202
533c011a 203 if (PL_op->op_flags & OPf_SPECIAL)
3280af22 204 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
a0d0e21e 205
b9ad30b4
NC
206 if (DO_UTF8(tmpstr)) {
207 assert (SvUTF8(tmpstr));
208 } else if (SvUTF8(tmpstr)) {
209 /* Not doing UTF-8, despite what the SV says. Is this only if
210 we're trapped in use 'bytes'? */
211 /* Make a copy of the octet sequence, but without the flag on,
212 as the compiler now honours the SvUTF8 flag on tmpstr. */
213 STRLEN len;
214 const char *const p = SvPV(tmpstr, len);
215 tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
216 }
c737faaf 217
3ab4a224
AB
218 if (eng)
219 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
220 else
221 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
c737faaf 222
f86aaa29 223 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
2cd61cdb 224 inside tie/overload accessors. */
c277df42 225 }
4633a7c4 226 }
c737faaf
YO
227
228 re = PM_GETRE(pm);
a0d0e21e 229
72311751 230#ifndef INCOMPLETE_TAINTS
3280af22
NIS
231 if (PL_tainting) {
232 if (PL_tainted)
07bc277f 233 RX_EXTFLAGS(re) |= RXf_TAINTED;
72311751 234 else
07bc277f 235 RX_EXTFLAGS(re) &= ~RXf_TAINTED;
72311751
GS
236 }
237#endif
238
220fc49f 239 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
3280af22 240 pm = PL_curpm;
a0d0e21e 241
c737faaf
YO
242
243#if !defined(USE_ITHREADS)
244 /* can't change the optree at runtime either */
245 /* PMf_KEEP is handled differently under threads to avoid these problems */
a0d0e21e 246 if (pm->op_pmflags & PMf_KEEP) {
c90c0ff4 247 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
533c011a 248 cLOGOP->op_first->op_next = PL_op->op_next;
a0d0e21e 249 }
c737faaf 250#endif
a0d0e21e
LW
251 RETURN;
252}
253
254PP(pp_substcont)
255{
97aff369 256 dVAR;
39644a26 257 dSP;
c09156bb 258 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
901017d6
AL
259 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
260 register SV * const dstr = cx->sb_dstr;
a0d0e21e
LW
261 register char *s = cx->sb_s;
262 register char *m = cx->sb_m;
263 char *orig = cx->sb_orig;
901017d6 264 register REGEXP * const rx = cx->sb_rx;
c445ea15 265 SV *nsv = NULL;
988e6e7e
AE
266 REGEXP *old = PM_GETRE(pm);
267 if(old != rx) {
bfed75c6 268 if(old)
988e6e7e 269 ReREFCNT_dec(old);
d6106309 270 PM_SETRE(pm,ReREFCNT_inc(rx));
d8f2cf8a
AB
271 }
272
d9f97599 273 rxres_restore(&cx->sb_rxres, rx);
01b35787 274 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
c90c0ff4 275
a0d0e21e 276 if (cx->sb_iters++) {
a3b680e6 277 const I32 saviters = cx->sb_iters;
a0d0e21e 278 if (cx->sb_iters > cx->sb_maxiters)
cea2e8a9 279 DIE(aTHX_ "Substitution loop");
a0d0e21e 280
447ee134
DM
281 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
282
48c036b1
GS
283 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
284 cx->sb_rxtainted |= 2;
447ee134 285 sv_catsv_nomg(dstr, POPs);
2c296965
YO
286 /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */
287 s -= RX_GOFS(rx);
a0d0e21e
LW
288
289 /* Are we done */
2c296965
YO
290 if (CxONCE(cx) || s < orig ||
291 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
292 (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
293 ((cx->sb_rflags & REXEC_COPY_STR)
294 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
295 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
a0d0e21e 296 {
823a54a3 297 SV * const targ = cx->sb_targ;
748a9306 298
078c425b
JH
299 assert(cx->sb_strend >= s);
300 if(cx->sb_strend > s) {
301 if (DO_UTF8(dstr) && !SvUTF8(targ))
302 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
303 else
304 sv_catpvn(dstr, s, cx->sb_strend - s);
305 }
48c036b1 306 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
9212bbba 307
f8c7b90f 308#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
309 if (SvIsCOW(targ)) {
310 sv_force_normal_flags(targ, SV_COW_DROP_PV);
311 } else
312#endif
313 {
8bd4d4c5 314 SvPV_free(targ);
ed252734 315 }
f880fe2f 316 SvPV_set(targ, SvPVX(dstr));
748a9306
LW
317 SvCUR_set(targ, SvCUR(dstr));
318 SvLEN_set(targ, SvLEN(dstr));
1aa99e6b
IH
319 if (DO_UTF8(dstr))
320 SvUTF8_on(targ);
6136c704 321 SvPV_set(dstr, NULL);
48c036b1
GS
322
323 TAINT_IF(cx->sb_rxtainted & 1);
6e449a3a 324 mPUSHi(saviters - 1);
48c036b1 325
ffc61ed2 326 (void)SvPOK_only_UTF8(targ);
48c036b1 327 TAINT_IF(cx->sb_rxtainted);
a0d0e21e 328 SvSETMAGIC(targ);
9212bbba 329 SvTAINT(targ);
5cd24f17 330
4633a7c4 331 LEAVE_SCOPE(cx->sb_oldsave);
a0d0e21e
LW
332 POPSUBST(cx);
333 RETURNOP(pm->op_next);
334 }
8e5e9ebe 335 cx->sb_iters = saviters;
a0d0e21e 336 }
07bc277f 337 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
a0d0e21e
LW
338 m = s;
339 s = orig;
07bc277f 340 cx->sb_orig = orig = RX_SUBBEG(rx);
a0d0e21e
LW
341 s = orig + (m - s);
342 cx->sb_strend = s + (cx->sb_strend - m);
343 }
07bc277f 344 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
db79b45b 345 if (m > s) {
bfed75c6 346 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
db79b45b
JH
347 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
348 else
349 sv_catpvn(dstr, s, m-s);
350 }
07bc277f 351 cx->sb_s = RX_OFFS(rx)[0].end + orig;
084916e3 352 { /* Update the pos() information. */
44f8325f 353 SV * const sv = cx->sb_targ;
084916e3 354 MAGIC *mg;
7a7f3e45 355 SvUPGRADE(sv, SVt_PVMG);
14befaf4 356 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
d83f0a82 357#ifdef PERL_OLD_COPY_ON_WRITE
51a9ea20 358 if (SvIsCOW(sv))
d83f0a82
NC
359 sv_force_normal_flags(sv, 0);
360#endif
361 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
362 NULL, 0);
084916e3 363 }
ce474962 364 mg->mg_len = m - orig;
084916e3 365 }
988e6e7e 366 if (old != rx)
d6106309 367 (void)ReREFCNT_inc(rx);
d9f97599
GS
368 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
369 rxres_save(&cx->sb_rxres, rx);
29f2e912 370 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
a0d0e21e
LW
371}
372
c90c0ff4 373void
864dbfa3 374Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4
PP
375{
376 UV *p = (UV*)*rsp;
377 U32 i;
7918f24d
NC
378
379 PERL_ARGS_ASSERT_RXRES_SAVE;
96a5add6 380 PERL_UNUSED_CONTEXT;
c90c0ff4 381
07bc277f 382 if (!p || p[1] < RX_NPARENS(rx)) {
f8c7b90f 383#ifdef PERL_OLD_COPY_ON_WRITE
07bc277f 384 i = 7 + RX_NPARENS(rx) * 2;
ed252734 385#else
07bc277f 386 i = 6 + RX_NPARENS(rx) * 2;
ed252734 387#endif
c90c0ff4 388 if (!p)
a02a5408 389 Newx(p, i, UV);
c90c0ff4
PP
390 else
391 Renew(p, i, UV);
392 *rsp = (void*)p;
393 }
394
07bc277f 395 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
cf93c79d 396 RX_MATCH_COPIED_off(rx);
c90c0ff4 397
f8c7b90f 398#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1
NC
399 *p++ = PTR2UV(RX_SAVED_COPY(rx));
400 RX_SAVED_COPY(rx) = NULL;
ed252734
NC
401#endif
402
07bc277f 403 *p++ = RX_NPARENS(rx);
c90c0ff4 404
07bc277f
NC
405 *p++ = PTR2UV(RX_SUBBEG(rx));
406 *p++ = (UV)RX_SUBLEN(rx);
407 for (i = 0; i <= RX_NPARENS(rx); ++i) {
408 *p++ = (UV)RX_OFFS(rx)[i].start;
409 *p++ = (UV)RX_OFFS(rx)[i].end;
c90c0ff4
PP
410 }
411}
412
9c105995
NC
413static void
414S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4
PP
415{
416 UV *p = (UV*)*rsp;
417 U32 i;
7918f24d
NC
418
419 PERL_ARGS_ASSERT_RXRES_RESTORE;
96a5add6 420 PERL_UNUSED_CONTEXT;
c90c0ff4 421
ed252734 422 RX_MATCH_COPY_FREE(rx);
cf93c79d 423 RX_MATCH_COPIED_set(rx, *p);
c90c0ff4
PP
424 *p++ = 0;
425
f8c7b90f 426#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1
NC
427 if (RX_SAVED_COPY(rx))
428 SvREFCNT_dec (RX_SAVED_COPY(rx));
429 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
ed252734
NC
430 *p++ = 0;
431#endif
432
07bc277f 433 RX_NPARENS(rx) = *p++;
c90c0ff4 434
07bc277f
NC
435 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
436 RX_SUBLEN(rx) = (I32)(*p++);
437 for (i = 0; i <= RX_NPARENS(rx); ++i) {
438 RX_OFFS(rx)[i].start = (I32)(*p++);
439 RX_OFFS(rx)[i].end = (I32)(*p++);
c90c0ff4
PP
440 }
441}
442
9c105995
NC
443static void
444S_rxres_free(pTHX_ void **rsp)
c90c0ff4 445{
44f8325f 446 UV * const p = (UV*)*rsp;
7918f24d
NC
447
448 PERL_ARGS_ASSERT_RXRES_FREE;
96a5add6 449 PERL_UNUSED_CONTEXT;
c90c0ff4
PP
450
451 if (p) {
94010e71
NC
452#ifdef PERL_POISON
453 void *tmp = INT2PTR(char*,*p);
454 Safefree(tmp);
455 if (*p)
7e337ee0 456 PoisonFree(*p, 1, sizeof(*p));
94010e71 457#else
56431972 458 Safefree(INT2PTR(char*,*p));
94010e71 459#endif
f8c7b90f 460#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
461 if (p[1]) {
462 SvREFCNT_dec (INT2PTR(SV*,p[1]));
463 }
464#endif
c90c0ff4 465 Safefree(p);
4608196e 466 *rsp = NULL;
c90c0ff4
PP
467 }
468}
469
a0d0e21e
LW
470PP(pp_formline)
471{
97aff369 472 dVAR; dSP; dMARK; dORIGMARK;
823a54a3 473 register SV * const tmpForm = *++MARK;
dea28490 474 register U32 *fpc;
a0d0e21e 475 register char *t;
245d4a47 476 const char *f;
a0d0e21e 477 register I32 arg;
c445ea15
AL
478 register SV *sv = NULL;
479 const char *item = NULL;
9c5ffd7c
JH
480 I32 itemsize = 0;
481 I32 fieldsize = 0;
a0d0e21e 482 I32 lines = 0;
c445ea15
AL
483 bool chopspace = (strchr(PL_chopset, ' ') != NULL);
484 const char *chophere = NULL;
485 char *linemark = NULL;
65202027 486 NV value;
9c5ffd7c 487 bool gotsome = FALSE;
a0d0e21e 488 STRLEN len;
823a54a3 489 const STRLEN fudge = SvPOK(tmpForm)
24c89738 490 ? (SvCUR(tmpForm) * (IN_BYTES ? 1 : 3) + 1) : 0;
1bd51a4c
IH
491 bool item_is_utf8 = FALSE;
492 bool targ_is_utf8 = FALSE;
c445ea15 493 SV * nsv = NULL;
cbbf8932 494 OP * parseres = NULL;
bfed75c6 495 const char *fmt;
a0d0e21e 496
76e3520e 497 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
445b3f51
GS
498 if (SvREADONLY(tmpForm)) {
499 SvREADONLY_off(tmpForm);
a1b95068 500 parseres = doparseform(tmpForm);
445b3f51
GS
501 SvREADONLY_on(tmpForm);
502 }
503 else
a1b95068
LW
504 parseres = doparseform(tmpForm);
505 if (parseres)
506 return parseres;
a0d0e21e 507 }
3280af22 508 SvPV_force(PL_formtarget, len);
1bd51a4c
IH
509 if (DO_UTF8(PL_formtarget))
510 targ_is_utf8 = TRUE;
a0ed51b3 511 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
a0d0e21e 512 t += len;
245d4a47 513 f = SvPV_const(tmpForm, len);
a0d0e21e 514 /* need to jump to the next word */
245d4a47 515 fpc = (U32*)(f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN);
a0d0e21e
LW
516
517 for (;;) {
518 DEBUG_f( {
bfed75c6 519 const char *name = "???";
a0d0e21e
LW
520 arg = -1;
521 switch (*fpc) {
522 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
523 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
524 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
525 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
526 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
527
528 case FF_CHECKNL: name = "CHECKNL"; break;
529 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
530 case FF_SPACE: name = "SPACE"; break;
531 case FF_HALFSPACE: name = "HALFSPACE"; break;
532 case FF_ITEM: name = "ITEM"; break;
533 case FF_CHOP: name = "CHOP"; break;
534 case FF_LINEGLOB: name = "LINEGLOB"; break;
535 case FF_NEWLINE: name = "NEWLINE"; break;
536 case FF_MORE: name = "MORE"; break;
537 case FF_LINEMARK: name = "LINEMARK"; break;
538 case FF_END: name = "END"; break;
bfed75c6 539 case FF_0DECIMAL: name = "0DECIMAL"; break;
a1b95068 540 case FF_LINESNGL: name = "LINESNGL"; break;
a0d0e21e
LW
541 }
542 if (arg >= 0)
bf49b057 543 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
a0d0e21e 544 else
bf49b057 545 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
5f80b19c 546 } );
a0d0e21e
LW
547 switch (*fpc++) {
548 case FF_LINEMARK:
549 linemark = t;
a0d0e21e
LW
550 lines++;
551 gotsome = FALSE;
552 break;
553
554 case FF_LITERAL:
555 arg = *fpc++;
1bd51a4c 556 if (targ_is_utf8 && !SvUTF8(tmpForm)) {
b15aece3 557 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
78da4d13
JH
558 *t = '\0';
559 sv_catpvn_utf8_upgrade(PL_formtarget, f, arg, nsv);
560 t = SvEND(PL_formtarget);
f3f2f1a3 561 f += arg;
1bd51a4c
IH
562 break;
563 }
564 if (!targ_is_utf8 && DO_UTF8(tmpForm)) {
b15aece3 565 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1bd51a4c 566 *t = '\0';
7bf79863 567 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC, fudge + 1);
1bd51a4c
IH
568 t = SvEND(PL_formtarget);
569 targ_is_utf8 = TRUE;
570 }
a0d0e21e
LW
571 while (arg--)
572 *t++ = *f++;
573 break;
574
575 case FF_SKIP:
576 f += *fpc++;
577 break;
578
579 case FF_FETCH:
580 arg = *fpc++;
581 f += arg;
582 fieldsize = arg;
583
584 if (MARK < SP)
585 sv = *++MARK;
586 else {
3280af22 587 sv = &PL_sv_no;
a2a5de95 588 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
a0d0e21e
LW
589 }
590 break;
591
592 case FF_CHECKNL:
5a34cab7
NC
593 {
594 const char *send;
595 const char *s = item = SvPV_const(sv, len);
596 itemsize = len;
597 if (DO_UTF8(sv)) {
598 itemsize = sv_len_utf8(sv);
599 if (itemsize != (I32)len) {
600 I32 itembytes;
601 if (itemsize > fieldsize) {
602 itemsize = fieldsize;
603 itembytes = itemsize;
604 sv_pos_u2b(sv, &itembytes, 0);
605 }
606 else
607 itembytes = len;
608 send = chophere = s + itembytes;
609 while (s < send) {
610 if (*s & ~31)
611 gotsome = TRUE;
612 else if (*s == '\n')
613 break;
614 s++;
615 }
616 item_is_utf8 = TRUE;
617 itemsize = s - item;
618 sv_pos_b2u(sv, &itemsize);
619 break;
a0ed51b3 620 }
a0ed51b3 621 }
5a34cab7
NC
622 item_is_utf8 = FALSE;
623 if (itemsize > fieldsize)
624 itemsize = fieldsize;
625 send = chophere = s + itemsize;
626 while (s < send) {
627 if (*s & ~31)
628 gotsome = TRUE;
629 else if (*s == '\n')
630 break;
631 s++;
632 }
633 itemsize = s - item;
634 break;
a0ed51b3 635 }
a0d0e21e
LW
636
637 case FF_CHECKCHOP:
5a34cab7
NC
638 {
639 const char *s = item = SvPV_const(sv, len);
640 itemsize = len;
641 if (DO_UTF8(sv)) {
642 itemsize = sv_len_utf8(sv);
643 if (itemsize != (I32)len) {
644 I32 itembytes;
645 if (itemsize <= fieldsize) {
646 const char *send = chophere = s + itemsize;
647 while (s < send) {
648 if (*s == '\r') {
649 itemsize = s - item;
a0ed51b3 650 chophere = s;
a0ed51b3 651 break;
5a34cab7
NC
652 }
653 if (*s++ & ~31)
a0ed51b3 654 gotsome = TRUE;
a0ed51b3 655 }
a0ed51b3 656 }
5a34cab7
NC
657 else {
658 const char *send;
659 itemsize = fieldsize;
660 itembytes = itemsize;
661 sv_pos_u2b(sv, &itembytes, 0);
662 send = chophere = s + itembytes;
663 while (s < send || (s == send && isSPACE(*s))) {
664 if (isSPACE(*s)) {
665 if (chopspace)
666 chophere = s;
667 if (*s == '\r')
668 break;
669 }
670 else {
671 if (*s & ~31)
672 gotsome = TRUE;
673 if (strchr(PL_chopset, *s))
674 chophere = s + 1;
675 }
676 s++;
677 }
678 itemsize = chophere - item;
679 sv_pos_b2u(sv, &itemsize);
680 }
681 item_is_utf8 = TRUE;
a0d0e21e
LW
682 break;
683 }
a0d0e21e 684 }
5a34cab7
NC
685 item_is_utf8 = FALSE;
686 if (itemsize <= fieldsize) {
687 const char *const send = chophere = s + itemsize;
688 while (s < send) {
689 if (*s == '\r') {
690 itemsize = s - item;
a0d0e21e 691 chophere = s;
a0d0e21e 692 break;
5a34cab7
NC
693 }
694 if (*s++ & ~31)
a0d0e21e 695 gotsome = TRUE;
a0d0e21e 696 }
a0d0e21e 697 }
5a34cab7
NC
698 else {
699 const char *send;
700 itemsize = fieldsize;
701 send = chophere = s + itemsize;
702 while (s < send || (s == send && isSPACE(*s))) {
703 if (isSPACE(*s)) {
704 if (chopspace)
705 chophere = s;
706 if (*s == '\r')
707 break;
708 }
709 else {
710 if (*s & ~31)
711 gotsome = TRUE;
712 if (strchr(PL_chopset, *s))
713 chophere = s + 1;
714 }
715 s++;
716 }
717 itemsize = chophere - item;
718 }
719 break;
a0d0e21e 720 }
a0d0e21e
LW
721
722 case FF_SPACE:
723 arg = fieldsize - itemsize;
724 if (arg) {
725 fieldsize -= arg;
726 while (arg-- > 0)
727 *t++ = ' ';
728 }
729 break;
730
731 case FF_HALFSPACE:
732 arg = fieldsize - itemsize;
733 if (arg) {
734 arg /= 2;
735 fieldsize -= arg;
736 while (arg-- > 0)
737 *t++ = ' ';
738 }
739 break;
740
741 case FF_ITEM:
5a34cab7
NC
742 {
743 const char *s = item;
744 arg = itemsize;
745 if (item_is_utf8) {
746 if (!targ_is_utf8) {
747 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
748 *t = '\0';
7bf79863
KW
749 sv_utf8_upgrade_flags_grow(PL_formtarget, SV_GMAGIC,
750 fudge + 1);
5a34cab7
NC
751 t = SvEND(PL_formtarget);
752 targ_is_utf8 = TRUE;
a0ed51b3 753 }
5a34cab7
NC
754 while (arg--) {
755 if (UTF8_IS_CONTINUED(*s)) {
756 STRLEN skip = UTF8SKIP(s);
757 switch (skip) {
758 default:
759 Move(s,t,skip,char);
760 s += skip;
761 t += skip;
762 break;
763 case 7: *t++ = *s++;
764 case 6: *t++ = *s++;
765 case 5: *t++ = *s++;
766 case 4: *t++ = *s++;
767 case 3: *t++ = *s++;
768 case 2: *t++ = *s++;
769 case 1: *t++ = *s++;
770 }
771 }
772 else {
773 if ( !((*t++ = *s++) & ~31) )
774 t[-1] = ' ';
775 }
a0ed51b3 776 }
5a34cab7 777 break;
a0ed51b3 778 }
5a34cab7
NC
779 if (targ_is_utf8 && !item_is_utf8) {
780 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
781 *t = '\0';
782 sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
783 for (; t < SvEND(PL_formtarget); t++) {
78da4d13 784#ifdef EBCDIC
901017d6 785 const int ch = *t;
5a34cab7 786 if (iscntrl(ch))
78da4d13 787#else
5a34cab7 788 if (!(*t & ~31))
78da4d13 789#endif
5a34cab7
NC
790 *t = ' ';
791 }
792 break;
78da4d13 793 }
5a34cab7 794 while (arg--) {
9d116dd7 795#ifdef EBCDIC
901017d6 796 const int ch = *t++ = *s++;
5a34cab7 797 if (iscntrl(ch))
a0d0e21e 798#else
5a34cab7 799 if ( !((*t++ = *s++) & ~31) )
a0d0e21e 800#endif
5a34cab7
NC
801 t[-1] = ' ';
802 }
803 break;
a0d0e21e 804 }
a0d0e21e
LW
805
806 case FF_CHOP:
5a34cab7
NC
807 {
808 const char *s = chophere;
809 if (chopspace) {
af68e756 810 while (isSPACE(*s))
5a34cab7
NC
811 s++;
812 }
813 sv_chop(sv,s);
814 SvSETMAGIC(sv);
815 break;
a0d0e21e 816 }
a0d0e21e 817
a1b95068
LW
818 case FF_LINESNGL:
819 chopspace = 0;
a0d0e21e 820 case FF_LINEGLOB:
5a34cab7 821 {
e32383e2 822 const bool oneline = fpc[-1] == FF_LINESNGL;
5a34cab7 823 const char *s = item = SvPV_const(sv, len);
f3f2f1a3 824 item_is_utf8 = DO_UTF8(sv);
5a34cab7 825 itemsize = len;
5a34cab7 826 if (itemsize) {
e8e72d41 827 STRLEN to_copy = itemsize;
5a34cab7 828 const char *const send = s + len;
35c6393c 829 const U8 *source = (const U8 *) s;
e8e72d41
NC
830 U8 *tmp = NULL;
831
5a34cab7
NC
832 gotsome = TRUE;
833 chophere = s + itemsize;
834 while (s < send) {
835 if (*s++ == '\n') {
836 if (oneline) {
e8e72d41 837 to_copy = s - SvPVX_const(sv) - 1;
5a34cab7
NC
838 chophere = s;
839 break;
840 } else {
841 if (s == send) {
842 itemsize--;
e8e72d41 843 to_copy--;
5a34cab7
NC
844 } else
845 lines++;
846 }
1bd51a4c 847 }
a0d0e21e 848 }
e8e72d41 849 if (targ_is_utf8 && !item_is_utf8) {
35c6393c 850 source = tmp = bytes_to_utf8(source, &to_copy);
e8e72d41
NC
851 SvCUR_set(PL_formtarget,
852 t - SvPVX_const(PL_formtarget));
853 } else {
854 if (item_is_utf8 && !targ_is_utf8) {
855 /* Upgrade targ to UTF8, and then we reduce it to
856 a problem we have a simple solution for. */
857 SvCUR_set(PL_formtarget,
858 t - SvPVX_const(PL_formtarget));
859 targ_is_utf8 = TRUE;
860 /* Don't need get magic. */
7bf79863 861 sv_utf8_upgrade_nomg(PL_formtarget);
e8e72d41
NC
862 } else {
863 SvCUR_set(PL_formtarget,
864 t - SvPVX_const(PL_formtarget));
865 }
e8e72d41
NC
866
867 /* Easy. They agree. */
868 assert (item_is_utf8 == targ_is_utf8);
869 }
870 SvGROW(PL_formtarget,
871 SvCUR(PL_formtarget) + to_copy + fudge + 1);
5a34cab7 872 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
e8e72d41
NC
873
874 Copy(source, t, to_copy, char);
875 t += to_copy;
876 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
f3f2f1a3 877 if (item_is_utf8) {
e8e72d41
NC
878 if (SvGMAGICAL(sv)) {
879 /* Mustn't call sv_pos_b2u() as it does a second
880 mg_get(). Is this a bug? Do we need a _flags()
881 variant? */
882 itemsize = utf8_length(source, source + itemsize);
883 } else {
884 sv_pos_b2u(sv, &itemsize);
885 }
886 assert(!tmp);
887 } else if (tmp) {
888 Safefree(tmp);
f3f2f1a3 889 }
a0d0e21e 890 }
5a34cab7 891 break;
a0d0e21e 892 }
a0d0e21e 893
a1b95068
LW
894 case FF_0DECIMAL:
895 arg = *fpc++;
896#if defined(USE_LONG_DOUBLE)
10edeb5d
JH
897 fmt = (const char *)
898 ((arg & 256) ?
899 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
a1b95068 900#else
10edeb5d
JH
901 fmt = (const char *)
902 ((arg & 256) ?
903 "%#0*.*f" : "%0*.*f");
a1b95068
LW
904#endif
905 goto ff_dec;
a0d0e21e 906 case FF_DECIMAL:
a0d0e21e 907 arg = *fpc++;
65202027 908#if defined(USE_LONG_DOUBLE)
10edeb5d
JH
909 fmt = (const char *)
910 ((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
65202027 911#else
10edeb5d
JH
912 fmt = (const char *)
913 ((arg & 256) ? "%#*.*f" : "%*.*f");
65202027 914#endif
a1b95068 915 ff_dec:
784707d5
JP
916 /* If the field is marked with ^ and the value is undefined,
917 blank it out. */
784707d5
JP
918 if ((arg & 512) && !SvOK(sv)) {
919 arg = fieldsize;
920 while (arg--)
921 *t++ = ' ';
922 break;
923 }
924 gotsome = TRUE;
925 value = SvNV(sv);
a1b95068 926 /* overflow evidence */
bfed75c6 927 if (num_overflow(value, fieldsize, arg)) {
a1b95068
LW
928 arg = fieldsize;
929 while (arg--)
930 *t++ = '#';
931 break;
932 }
784707d5
JP
933 /* Formats aren't yet marked for locales, so assume "yes". */
934 {
935 STORE_NUMERIC_STANDARD_SET_LOCAL();
d9fad198 936 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
784707d5
JP
937 RESTORE_NUMERIC_STANDARD();
938 }
939 t += fieldsize;
940 break;
a1b95068 941
a0d0e21e
LW
942 case FF_NEWLINE:
943 f++;
944 while (t-- > linemark && *t == ' ') ;
945 t++;
946 *t++ = '\n';
947 break;
948
949 case FF_BLANK:
950 arg = *fpc++;
951 if (gotsome) {
952 if (arg) { /* repeat until fields exhausted? */
953 *t = '\0';
b15aece3 954 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
3280af22 955 lines += FmLINES(PL_formtarget);
1bd51a4c
IH
956 if (targ_is_utf8)
957 SvUTF8_on(PL_formtarget);
3280af22 958 FmLINES(PL_formtarget) = lines;
a0d0e21e
LW
959 SP = ORIGMARK;
960 RETURNOP(cLISTOP->op_first);
961 }
962 }
963 else {
964 t = linemark;
965 lines--;
966 }
967 break;
968
969 case FF_MORE:
5a34cab7
NC
970 {
971 const char *s = chophere;
972 const char *send = item + len;
973 if (chopspace) {
af68e756 974 while (isSPACE(*s) && (s < send))
5a34cab7 975 s++;
a0d0e21e 976 }
5a34cab7
NC
977 if (s < send) {
978 char *s1;
979 arg = fieldsize - itemsize;
980 if (arg) {
981 fieldsize -= arg;
982 while (arg-- > 0)
983 *t++ = ' ';
984 }
985 s1 = t - 3;
986 if (strnEQ(s1," ",3)) {
987 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
988 s1--;
989 }
990 *s1++ = '.';
991 *s1++ = '.';
992 *s1++ = '.';
a0d0e21e 993 }
5a34cab7 994 break;
a0d0e21e 995 }
a0d0e21e
LW
996 case FF_END:
997 *t = '\0';
b15aece3 998 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1bd51a4c
IH
999 if (targ_is_utf8)
1000 SvUTF8_on(PL_formtarget);
3280af22 1001 FmLINES(PL_formtarget) += lines;
a0d0e21e
LW
1002 SP = ORIGMARK;
1003 RETPUSHYES;
1004 }
1005 }
1006}
1007
1008PP(pp_grepstart)
1009{
27da23d5 1010 dVAR; dSP;
a0d0e21e
LW
1011 SV *src;
1012
3280af22 1013 if (PL_stack_base + *PL_markstack_ptr == SP) {
a0d0e21e 1014 (void)POPMARK;
54310121 1015 if (GIMME_V == G_SCALAR)
6e449a3a 1016 mXPUSHi(0);
533c011a 1017 RETURNOP(PL_op->op_next->op_next);
a0d0e21e 1018 }
3280af22 1019 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
cea2e8a9
GS
1020 pp_pushmark(); /* push dst */
1021 pp_pushmark(); /* push src */
d343c3ef 1022 ENTER_with_name("grep"); /* enter outer scope */
a0d0e21e
LW
1023
1024 SAVETMPS;
59f00321
RGS
1025 if (PL_op->op_private & OPpGREP_LEX)
1026 SAVESPTR(PAD_SVl(PL_op->op_targ));
1027 else
1028 SAVE_DEFSV;
d343c3ef 1029 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1030 SAVEVPTR(PL_curpm);
a0d0e21e 1031
3280af22 1032 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1033 SvTEMP_off(src);
59f00321
RGS
1034 if (PL_op->op_private & OPpGREP_LEX)
1035 PAD_SVl(PL_op->op_targ) = src;
1036 else
414bf5ae 1037 DEFSV_set(src);
a0d0e21e
LW
1038
1039 PUTBACK;
533c011a 1040 if (PL_op->op_type == OP_MAPSTART)
cea2e8a9 1041 pp_pushmark(); /* push top */
533c011a 1042 return ((LOGOP*)PL_op->op_next)->op_other;
a0d0e21e
LW
1043}
1044
a0d0e21e
LW
1045PP(pp_mapwhile)
1046{
27da23d5 1047 dVAR; dSP;
f54cb97a 1048 const I32 gimme = GIMME_V;
544f3153 1049 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
a0d0e21e
LW
1050 I32 count;
1051 I32 shift;
1052 SV** src;
ac27b0f5 1053 SV** dst;
a0d0e21e 1054
544f3153 1055 /* first, move source pointer to the next item in the source list */
3280af22 1056 ++PL_markstack_ptr[-1];
544f3153
GS
1057
1058 /* if there are new items, push them into the destination list */
4c90a460 1059 if (items && gimme != G_VOID) {
544f3153
GS
1060 /* might need to make room back there first */
1061 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1062 /* XXX this implementation is very pessimal because the stack
1063 * is repeatedly extended for every set of items. Is possible
1064 * to do this without any stack extension or copying at all
1065 * by maintaining a separate list over which the map iterates
18ef8bea 1066 * (like foreach does). --gsar */
544f3153
GS
1067
1068 /* everything in the stack after the destination list moves
1069 * towards the end the stack by the amount of room needed */
1070 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1071
1072 /* items to shift up (accounting for the moved source pointer) */
1073 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
18ef8bea
BT
1074
1075 /* This optimization is by Ben Tilly and it does
1076 * things differently from what Sarathy (gsar)
1077 * is describing. The downside of this optimization is
1078 * that leaves "holes" (uninitialized and hopefully unused areas)
1079 * to the Perl stack, but on the other hand this
1080 * shouldn't be a problem. If Sarathy's idea gets
1081 * implemented, this optimization should become
1082 * irrelevant. --jhi */
1083 if (shift < count)
1084 shift = count; /* Avoid shifting too often --Ben Tilly */
bfed75c6 1085
924508f0
GS
1086 EXTEND(SP,shift);
1087 src = SP;
1088 dst = (SP += shift);
3280af22
NIS
1089 PL_markstack_ptr[-1] += shift;
1090 *PL_markstack_ptr += shift;
544f3153 1091 while (count--)
a0d0e21e
LW
1092 *dst-- = *src--;
1093 }
544f3153 1094 /* copy the new items down to the destination list */
ac27b0f5 1095 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
22023b26
TP
1096 if (gimme == G_ARRAY) {
1097 while (items-- > 0)
1098 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
1099 }
bfed75c6 1100 else {
22023b26
TP
1101 /* scalar context: we don't care about which values map returns
1102 * (we use undef here). And so we certainly don't want to do mortal
1103 * copies of meaningless values. */
1104 while (items-- > 0) {
b988aa42 1105 (void)POPs;
22023b26
TP
1106 *dst-- = &PL_sv_undef;
1107 }
1108 }
a0d0e21e 1109 }
d343c3ef 1110 LEAVE_with_name("grep_item"); /* exit inner scope */
a0d0e21e
LW
1111
1112 /* All done yet? */
3280af22 1113 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
a0d0e21e
LW
1114
1115 (void)POPMARK; /* pop top */
d343c3ef 1116 LEAVE_with_name("grep"); /* exit outer scope */
a0d0e21e 1117 (void)POPMARK; /* pop src */
3280af22 1118 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1119 (void)POPMARK; /* pop dst */
3280af22 1120 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1121 if (gimme == G_SCALAR) {
7cc47870
RGS
1122 if (PL_op->op_private & OPpGREP_LEX) {
1123 SV* sv = sv_newmortal();
1124 sv_setiv(sv, items);
1125 PUSHs(sv);
1126 }
1127 else {
1128 dTARGET;
1129 XPUSHi(items);
1130 }
a0d0e21e 1131 }
54310121
PP
1132 else if (gimme == G_ARRAY)
1133 SP += items;
a0d0e21e
LW
1134 RETURN;
1135 }
1136 else {
1137 SV *src;
1138
d343c3ef 1139 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1140 SAVEVPTR(PL_curpm);
a0d0e21e 1141
544f3153 1142 /* set $_ to the new source item */
3280af22 1143 src = PL_stack_base[PL_markstack_ptr[-1]];
a0d0e21e 1144 SvTEMP_off(src);
59f00321
RGS
1145 if (PL_op->op_private & OPpGREP_LEX)
1146 PAD_SVl(PL_op->op_targ) = src;
1147 else
414bf5ae 1148 DEFSV_set(src);
a0d0e21e
LW
1149
1150 RETURNOP(cLOGOP->op_other);
1151 }
1152}
1153
a0d0e21e
LW
1154/* Range stuff. */
1155
1156PP(pp_range)
1157{
97aff369 1158 dVAR;
a0d0e21e 1159 if (GIMME == G_ARRAY)
1a67a97c 1160 return NORMAL;
538573f7 1161 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1a67a97c 1162 return cLOGOP->op_other;
538573f7 1163 else
1a67a97c 1164 return NORMAL;
a0d0e21e
LW
1165}
1166
1167PP(pp_flip)
1168{
97aff369 1169 dVAR;
39644a26 1170 dSP;
a0d0e21e
LW
1171
1172 if (GIMME == G_ARRAY) {
1a67a97c 1173 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1174 }
1175 else {
1176 dTOPss;
44f8325f 1177 SV * const targ = PAD_SV(PL_op->op_targ);
bfed75c6 1178 int flip = 0;
790090df 1179
bfed75c6 1180 if (PL_op->op_private & OPpFLIP_LINENUM) {
4e3399f9
YST
1181 if (GvIO(PL_last_in_gv)) {
1182 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1183 }
1184 else {
fafc274c 1185 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
44f8325f
AL
1186 if (gv && GvSV(gv))
1187 flip = SvIV(sv) == SvIV(GvSV(gv));
4e3399f9 1188 }
bfed75c6
AL
1189 } else {
1190 flip = SvTRUE(sv);
1191 }
1192 if (flip) {
a0d0e21e 1193 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
533c011a 1194 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1195 sv_setiv(targ, 1);
3e3baf6d 1196 SETs(targ);
a0d0e21e
LW
1197 RETURN;
1198 }
1199 else {
1200 sv_setiv(targ, 0);
924508f0 1201 SP--;
1a67a97c 1202 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1203 }
1204 }
76f68e9b 1205 sv_setpvs(TARG, "");
a0d0e21e
LW
1206 SETs(targ);
1207 RETURN;
1208 }
1209}
1210
8e9bbdb9
RGS
1211/* This code tries to decide if "$left .. $right" should use the
1212 magical string increment, or if the range is numeric (we make
1213 an exception for .."0" [#18165]). AMS 20021031. */
1214
1215#define RANGE_IS_NUMERIC(left,right) ( \
b0e74086
RGS
1216 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1217 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
e0ab1c0e 1218 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
b15aece3 1219 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
e0ab1c0e 1220 && (!SvOK(right) || looks_like_number(right))))
8e9bbdb9 1221
a0d0e21e
LW
1222PP(pp_flop)
1223{
97aff369 1224 dVAR; dSP;
a0d0e21e
LW
1225
1226 if (GIMME == G_ARRAY) {
1227 dPOPPOPssrl;
86cb7173 1228
5b295bef
RD
1229 SvGETMAGIC(left);
1230 SvGETMAGIC(right);
a0d0e21e 1231
8e9bbdb9 1232 if (RANGE_IS_NUMERIC(left,right)) {
901017d6
AL
1233 register IV i, j;
1234 IV max;
4fe3f0fa
MHM
1235 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1236 (SvOK(right) && SvNV(right) > IV_MAX))
d470f89e 1237 DIE(aTHX_ "Range iterator outside integer range");
a0d0e21e
LW
1238 i = SvIV(left);
1239 max = SvIV(right);
bbce6d69 1240 if (max >= i) {
c1ab3db2
AK
1241 j = max - i + 1;
1242 EXTEND_MORTAL(j);
1243 EXTEND(SP, j);
bbce6d69 1244 }
c1ab3db2
AK
1245 else
1246 j = 0;
1247 while (j--) {
901017d6 1248 SV * const sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
1249 PUSHs(sv);
1250 }
1251 }
1252 else {
44f8325f 1253 SV * const final = sv_mortalcopy(right);
13c5b33c 1254 STRLEN len;
823a54a3 1255 const char * const tmps = SvPV_const(final, len);
a0d0e21e 1256
901017d6 1257 SV *sv = sv_mortalcopy(left);
13c5b33c 1258 SvPV_force_nolen(sv);
89ea2908 1259 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
a0d0e21e 1260 XPUSHs(sv);
b15aece3 1261 if (strEQ(SvPVX_const(sv),tmps))
89ea2908 1262 break;
a0d0e21e
LW
1263 sv = sv_2mortal(newSVsv(sv));
1264 sv_inc(sv);
1265 }
a0d0e21e
LW
1266 }
1267 }
1268 else {
1269 dTOPss;
901017d6 1270 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
4e3399f9 1271 int flop = 0;
a0d0e21e 1272 sv_inc(targ);
4e3399f9
YST
1273
1274 if (PL_op->op_private & OPpFLIP_LINENUM) {
1275 if (GvIO(PL_last_in_gv)) {
1276 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1277 }
1278 else {
fafc274c 1279 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
4e3399f9
YST
1280 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1281 }
1282 }
1283 else {
1284 flop = SvTRUE(sv);
1285 }
1286
1287 if (flop) {
a0d0e21e 1288 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
396482e1 1289 sv_catpvs(targ, "E0");
a0d0e21e
LW
1290 }
1291 SETs(targ);
1292 }
1293
1294 RETURN;
1295}
1296
1297/* Control. */
1298
27da23d5 1299static const char * const context_name[] = {
515afda2 1300 "pseudo-block",
f31522f3 1301 NULL, /* CXt_WHEN never actually needs "block" */
76753e7f 1302 NULL, /* CXt_BLOCK never actually needs "block" */
f31522f3 1303 NULL, /* CXt_GIVEN never actually needs "block" */
76753e7f
NC
1304 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1305 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1306 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1307 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
515afda2 1308 "subroutine",
76753e7f 1309 "format",
515afda2 1310 "eval",
515afda2 1311 "substitution",
515afda2
NC
1312};
1313
76e3520e 1314STATIC I32
06b5626a 1315S_dopoptolabel(pTHX_ const char *label)
a0d0e21e 1316{
97aff369 1317 dVAR;
a0d0e21e 1318 register I32 i;
a0d0e21e 1319
7918f24d
NC
1320 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1321
a0d0e21e 1322 for (i = cxstack_ix; i >= 0; i--) {
901017d6 1323 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1324 switch (CxTYPE(cx)) {
a0d0e21e 1325 case CXt_SUBST:
a0d0e21e 1326 case CXt_SUB:
7766f137 1327 case CXt_FORMAT:
a0d0e21e 1328 case CXt_EVAL:
0a753a76 1329 case CXt_NULL:
a2a5de95
NC
1330 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1331 context_name[CxTYPE(cx)], OP_NAME(PL_op));
515afda2
NC
1332 if (CxTYPE(cx) == CXt_NULL)
1333 return -1;
1334 break;
c6fdafd0 1335 case CXt_LOOP_LAZYIV:
d01136d6 1336 case CXt_LOOP_LAZYSV:
3b719c58
NC
1337 case CXt_LOOP_FOR:
1338 case CXt_LOOP_PLAIN:
7e8f1eac
AD
1339 {
1340 const char *cx_label = CxLABEL(cx);
1341 if (!cx_label || strNE(label, cx_label) ) {
1c98cc53 1342 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
7e8f1eac 1343 (long)i, cx_label));
a0d0e21e
LW
1344 continue;
1345 }
1c98cc53 1346 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
a0d0e21e 1347 return i;
7e8f1eac 1348 }
a0d0e21e
LW
1349 }
1350 }
1351 return i;
1352}
1353
0d863452
RH
1354
1355
e50aee73 1356I32
864dbfa3 1357Perl_dowantarray(pTHX)
e50aee73 1358{
97aff369 1359 dVAR;
f54cb97a 1360 const I32 gimme = block_gimme();
54310121
PP
1361 return (gimme == G_VOID) ? G_SCALAR : gimme;
1362}
1363
1364I32
864dbfa3 1365Perl_block_gimme(pTHX)
54310121 1366{
97aff369 1367 dVAR;
06b5626a 1368 const I32 cxix = dopoptosub(cxstack_ix);
e50aee73 1369 if (cxix < 0)
46fc3d4c 1370 return G_VOID;
e50aee73 1371
54310121 1372 switch (cxstack[cxix].blk_gimme) {
d2719217
GS
1373 case G_VOID:
1374 return G_VOID;
54310121 1375 case G_SCALAR:
e50aee73 1376 return G_SCALAR;
54310121
PP
1377 case G_ARRAY:
1378 return G_ARRAY;
1379 default:
cea2e8a9 1380 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
d2719217
GS
1381 /* NOTREACHED */
1382 return 0;
54310121 1383 }
e50aee73
AD
1384}
1385
78f9721b
SM
1386I32
1387Perl_is_lvalue_sub(pTHX)
1388{
97aff369 1389 dVAR;
06b5626a 1390 const I32 cxix = dopoptosub(cxstack_ix);
78f9721b
SM
1391 assert(cxix >= 0); /* We should only be called from inside subs */
1392
bafb2adc
NC
1393 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1394 return CxLVAL(cxstack + cxix);
78f9721b
SM
1395 else
1396 return 0;
1397}
1398
76e3520e 1399STATIC I32
901017d6 1400S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
2c375eb9 1401{
97aff369 1402 dVAR;
a0d0e21e 1403 I32 i;
7918f24d
NC
1404
1405 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1406
a0d0e21e 1407 for (i = startingblock; i >= 0; i--) {
901017d6 1408 register const PERL_CONTEXT * const cx = &cxstk[i];
6b35e009 1409 switch (CxTYPE(cx)) {
a0d0e21e
LW
1410 default:
1411 continue;
1412 case CXt_EVAL:
1413 case CXt_SUB:
7766f137 1414 case CXt_FORMAT:
1c98cc53 1415 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
a0d0e21e
LW
1416 return i;
1417 }
1418 }
1419 return i;
1420}
1421
76e3520e 1422STATIC I32
cea2e8a9 1423S_dopoptoeval(pTHX_ I32 startingblock)
a0d0e21e 1424{
97aff369 1425 dVAR;
a0d0e21e 1426 I32 i;
a0d0e21e 1427 for (i = startingblock; i >= 0; i--) {
06b5626a 1428 register const PERL_CONTEXT *cx = &cxstack[i];
6b35e009 1429 switch (CxTYPE(cx)) {
a0d0e21e
LW
1430 default:
1431 continue;
1432 case CXt_EVAL:
1c98cc53 1433 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
a0d0e21e
LW
1434 return i;
1435 }
1436 }
1437 return i;
1438}
1439
76e3520e 1440STATIC I32
cea2e8a9 1441S_dopoptoloop(pTHX_ I32 startingblock)
a0d0e21e 1442{
97aff369 1443 dVAR;
a0d0e21e 1444 I32 i;
a0d0e21e 1445 for (i = startingblock; i >= 0; i--) {
901017d6 1446 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1447 switch (CxTYPE(cx)) {
a0d0e21e 1448 case CXt_SUBST:
a0d0e21e 1449 case CXt_SUB:
7766f137 1450 case CXt_FORMAT:
a0d0e21e 1451 case CXt_EVAL:
0a753a76 1452 case CXt_NULL:
a2a5de95
NC
1453 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1454 context_name[CxTYPE(cx)], OP_NAME(PL_op));
515afda2
NC
1455 if ((CxTYPE(cx)) == CXt_NULL)
1456 return -1;
1457 break;
c6fdafd0 1458 case CXt_LOOP_LAZYIV:
d01136d6 1459 case CXt_LOOP_LAZYSV:
3b719c58
NC
1460 case CXt_LOOP_FOR:
1461 case CXt_LOOP_PLAIN:
1c98cc53 1462 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
a0d0e21e
LW
1463 return i;
1464 }
1465 }
1466 return i;
1467}
1468
0d863452
RH
1469STATIC I32
1470S_dopoptogiven(pTHX_ I32 startingblock)
1471{
97aff369 1472 dVAR;
0d863452
RH
1473 I32 i;
1474 for (i = startingblock; i >= 0; i--) {
1475 register const PERL_CONTEXT *cx = &cxstack[i];
1476 switch (CxTYPE(cx)) {
1477 default:
1478 continue;
1479 case CXt_GIVEN:
1c98cc53 1480 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
0d863452 1481 return i;
3b719c58
NC
1482 case CXt_LOOP_PLAIN:
1483 assert(!CxFOREACHDEF(cx));
1484 break;
c6fdafd0 1485 case CXt_LOOP_LAZYIV:
d01136d6 1486 case CXt_LOOP_LAZYSV:
3b719c58 1487 case CXt_LOOP_FOR:
0d863452 1488 if (CxFOREACHDEF(cx)) {
1c98cc53 1489 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
0d863452
RH
1490 return i;
1491 }
1492 }
1493 }
1494 return i;
1495}
1496
1497STATIC I32
1498S_dopoptowhen(pTHX_ I32 startingblock)
1499{
97aff369 1500 dVAR;
0d863452
RH
1501 I32 i;
1502 for (i = startingblock; i >= 0; i--) {
1503 register const PERL_CONTEXT *cx = &cxstack[i];
1504 switch (CxTYPE(cx)) {
1505 default:
1506 continue;
1507 case CXt_WHEN:
1c98cc53 1508 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
0d863452
RH
1509 return i;
1510 }
1511 }
1512 return i;
1513}
1514
a0d0e21e 1515void
864dbfa3 1516Perl_dounwind(pTHX_ I32 cxix)
a0d0e21e 1517{
97aff369 1518 dVAR;
a0d0e21e
LW
1519 I32 optype;
1520
1521 while (cxstack_ix > cxix) {
b0d9ce38 1522 SV *sv;
06b5626a 1523 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1c98cc53 1524 DEBUG_CX("UNWIND"); \
a0d0e21e 1525 /* Note: we don't need to restore the base context info till the end. */
6b35e009 1526 switch (CxTYPE(cx)) {
c90c0ff4
PP
1527 case CXt_SUBST:
1528 POPSUBST(cx);
1529 continue; /* not break */
a0d0e21e 1530 case CXt_SUB:
b0d9ce38
GS
1531 POPSUB(cx,sv);
1532 LEAVESUB(sv);
a0d0e21e
LW
1533 break;
1534 case CXt_EVAL:
1535 POPEVAL(cx);
1536 break;
c6fdafd0 1537 case CXt_LOOP_LAZYIV:
d01136d6 1538 case CXt_LOOP_LAZYSV:
3b719c58
NC
1539 case CXt_LOOP_FOR:
1540 case CXt_LOOP_PLAIN:
a0d0e21e
LW
1541 POPLOOP(cx);
1542 break;
0a753a76 1543 case CXt_NULL:
a0d0e21e 1544 break;
7766f137
GS
1545 case CXt_FORMAT:
1546 POPFORMAT(cx);
1547 break;
a0d0e21e 1548 }
c90c0ff4 1549 cxstack_ix--;
a0d0e21e 1550 }
1b6737cc 1551 PERL_UNUSED_VAR(optype);
a0d0e21e
LW
1552}
1553
5a844595
GS
1554void
1555Perl_qerror(pTHX_ SV *err)
1556{
97aff369 1557 dVAR;
7918f24d
NC
1558
1559 PERL_ARGS_ASSERT_QERROR;
1560
5a844595
GS
1561 if (PL_in_eval)
1562 sv_catsv(ERRSV, err);
1563 else if (PL_errors)
1564 sv_catsv(PL_errors, err);
1565 else
be2597df 1566 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
13765c85
DM
1567 if (PL_parser)
1568 ++PL_parser->error_count;
5a844595
GS
1569}
1570
bb4c52e0 1571void
7d0994e0 1572Perl_die_where(pTHX_ SV *msv)
a0d0e21e 1573{
27da23d5 1574 dVAR;
87582a92 1575
3280af22 1576 if (PL_in_eval) {
a0d0e21e 1577 I32 cxix;
a0d0e21e 1578 I32 gimme;
a0d0e21e 1579
7d0994e0 1580 if (msv) {
faef0170 1581 if (PL_in_eval & EVAL_KEEPERR) {
bfed75c6 1582 static const char prefix[] = "\t(in cleanup) ";
2d03de9c 1583 SV * const err = ERRSV;
c445ea15 1584 const char *e = NULL;
98eae8f5 1585 if (!SvPOK(err))
76f68e9b 1586 sv_setpvs(err,"");
7d0994e0 1587 else if (SvCUR(err) >= sizeof(prefix)+SvCUR(msv)-1) {
0510663f 1588 STRLEN len;
7d0994e0
GG
1589 STRLEN msglen;
1590 const char* message = SvPV_const(msv, msglen);
349d4f2f 1591 e = SvPV_const(err, len);
0510663f 1592 e += len - msglen;
98eae8f5 1593 if (*e != *message || strNE(e,message))
c445ea15 1594 e = NULL;
98eae8f5
GS
1595 }
1596 if (!e) {
a2a5de95 1597 STRLEN start;
7d0994e0 1598 SvGROW(err, SvCUR(err)+sizeof(prefix)+SvCUR(msv));
98eae8f5 1599 sv_catpvn(err, prefix, sizeof(prefix)-1);
7d0994e0
GG
1600 sv_catsv(err, msv);
1601 start = SvCUR(err)-SvCUR(msv)-sizeof(prefix)+1;
a2a5de95
NC
1602 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "%s",
1603 SvPVX_const(err)+start);
4633a7c4 1604 }
4633a7c4 1605 }
1aa99e6b 1606 else {
7d0994e0
GG
1607 STRLEN msglen;
1608 const char* message = SvPV_const(msv, msglen);
06bf62c7 1609 sv_setpvn(ERRSV, message, msglen);
7d0994e0 1610 SvFLAGS(ERRSV) |= SvFLAGS(msv) & SVf_UTF8;
1aa99e6b 1611 }
4633a7c4 1612 }
4e6ea2c3 1613
5a844595
GS
1614 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1615 && PL_curstackinfo->si_prev)
1616 {
bac4b2ad 1617 dounwind(-1);
d3acc0f7 1618 POPSTACK;
bac4b2ad 1619 }
e336de0d 1620
a0d0e21e
LW
1621 if (cxix >= 0) {
1622 I32 optype;
35a4481c 1623 register PERL_CONTEXT *cx;
901017d6 1624 SV **newsp;
a0d0e21e
LW
1625
1626 if (cxix < cxstack_ix)
1627 dounwind(cxix);
1628
3280af22 1629 POPBLOCK(cx,PL_curpm);
6b35e009 1630 if (CxTYPE(cx) != CXt_EVAL) {
7d0994e0
GG
1631 STRLEN msglen;
1632 const char* message = SvPVx_const( msv ? msv : ERRSV, msglen);
10edeb5d 1633 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
bf49b057 1634 PerlIO_write(Perl_error_log, message, msglen);
a0d0e21e
LW
1635 my_exit(1);
1636 }
1637 POPEVAL(cx);
1638
1639 if (gimme == G_SCALAR)
3280af22
NIS
1640 *++newsp = &PL_sv_undef;
1641 PL_stack_sp = newsp;
a0d0e21e
LW
1642
1643 LEAVE;
748a9306 1644
7fb6a879
GS
1645 /* LEAVE could clobber PL_curcop (see save_re_context())
1646 * XXX it might be better to find a way to avoid messing with
1647 * PL_curcop in save_re_context() instead, but this is a more
1648 * minimal fix --GSAR */
1649 PL_curcop = cx->blk_oldcop;
1650
7a2e2cd6 1651 if (optype == OP_REQUIRE) {
44f8325f 1652 const char* const msg = SvPVx_nolen_const(ERRSV);
901017d6 1653 SV * const nsv = cx->blk_eval.old_namesv;
b15aece3 1654 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
27bcc0a7 1655 &PL_sv_undef, 0);
5a844595
GS
1656 DIE(aTHX_ "%sCompilation failed in require",
1657 *msg ? msg : "Unknown error\n");
7a2e2cd6 1658 }
f39bc417 1659 assert(CxTYPE(cx) == CXt_EVAL);
bb4c52e0
GG
1660 PL_restartop = cx->blk_eval.retop;
1661 JMPENV_JUMP(3);
1662 /* NOTREACHED */
a0d0e21e
LW
1663 }
1664 }
87582a92 1665
7d0994e0 1666 write_to_stderr( msv ? msv : ERRSV );
f86702cc
PP
1667 my_failure_exit();
1668 /* NOTREACHED */
a0d0e21e
LW
1669}
1670
1671PP(pp_xor)
1672{
97aff369 1673 dVAR; dSP; dPOPTOPssrl;
a0d0e21e
LW
1674 if (SvTRUE(left) != SvTRUE(right))
1675 RETSETYES;
1676 else
1677 RETSETNO;
1678}
1679
a0d0e21e
LW
1680PP(pp_caller)
1681{
97aff369 1682 dVAR;
39644a26 1683 dSP;
a0d0e21e 1684 register I32 cxix = dopoptosub(cxstack_ix);
901017d6
AL
1685 register const PERL_CONTEXT *cx;
1686 register const PERL_CONTEXT *ccstack = cxstack;
1687 const PERL_SI *top_si = PL_curstackinfo;
54310121 1688 I32 gimme;
06b5626a 1689 const char *stashname;
a0d0e21e
LW
1690 I32 count = 0;
1691
1692 if (MAXARG)
1693 count = POPi;
27d41816 1694
a0d0e21e 1695 for (;;) {
2c375eb9
GS
1696 /* we may be in a higher stacklevel, so dig down deeper */
1697 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1698 top_si = top_si->si_prev;
1699 ccstack = top_si->si_cxstack;
1700 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1701 }
a0d0e21e 1702 if (cxix < 0) {
27d41816
DM
1703 if (GIMME != G_ARRAY) {
1704 EXTEND(SP, 1);
a0d0e21e 1705 RETPUSHUNDEF;
27d41816 1706 }
a0d0e21e
LW
1707 RETURN;
1708 }
f2a7f298 1709 /* caller() should not report the automatic calls to &DB::sub */
1710 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
3280af22 1711 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
a0d0e21e
LW
1712 count++;
1713 if (!count--)
1714 break;
2c375eb9 1715 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1716 }
2c375eb9
GS
1717
1718 cx = &ccstack[cxix];
7766f137 1719 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
f54cb97a 1720 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
2c375eb9 1721 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
06a5b730 1722 field below is defined for any cx. */
f2a7f298 1723 /* caller() should not report the automatic calls to &DB::sub */
1724 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
2c375eb9 1725 cx = &ccstack[dbcxix];
06a5b730
PP
1726 }
1727
ed094faf 1728 stashname = CopSTASHPV(cx->blk_oldcop);
a0d0e21e 1729 if (GIMME != G_ARRAY) {
27d41816 1730 EXTEND(SP, 1);
ed094faf 1731 if (!stashname)
3280af22 1732 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1733 else {
1734 dTARGET;
ed094faf 1735 sv_setpv(TARG, stashname);
49d8d3a1
MB
1736 PUSHs(TARG);
1737 }
a0d0e21e
LW
1738 RETURN;
1739 }
a0d0e21e 1740
b3ca2e83 1741 EXTEND(SP, 11);
27d41816 1742
ed094faf 1743 if (!stashname)
3280af22 1744 PUSHs(&PL_sv_undef);
49d8d3a1 1745 else
6e449a3a
MHM
1746 mPUSHs(newSVpv(stashname, 0));
1747 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1748 mPUSHi((I32)CopLINE(cx->blk_oldcop));
a0d0e21e
LW
1749 if (!MAXARG)
1750 RETURN;
7766f137 1751 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
0bd48802 1752 GV * const cvgv = CvGV(ccstack[cxix].blk_sub.cv);
7766f137 1753 /* So is ccstack[dbcxix]. */
07b8c804 1754 if (isGV(cvgv)) {
561b68a9 1755 SV * const sv = newSV(0);
c445ea15 1756 gv_efullname3(sv, cvgv, NULL);
6e449a3a 1757 mPUSHs(sv);
bf38a478 1758 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804
RGS
1759 }
1760 else {
84bafc02 1761 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
bf38a478 1762 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804 1763 }
a0d0e21e
LW
1764 }
1765 else {
84bafc02 1766 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
6e449a3a 1767 mPUSHi(0);
a0d0e21e 1768 }
54310121
PP
1769 gimme = (I32)cx->blk_gimme;
1770 if (gimme == G_VOID)
3280af22 1771 PUSHs(&PL_sv_undef);
54310121 1772 else
98625aca 1773 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
6b35e009 1774 if (CxTYPE(cx) == CXt_EVAL) {
811a4de9 1775 /* eval STRING */
85a64632 1776 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
4633a7c4 1777 PUSHs(cx->blk_eval.cur_text);
3280af22 1778 PUSHs(&PL_sv_no);
0f79a09d 1779 }
811a4de9 1780 /* require */
0f79a09d 1781 else if (cx->blk_eval.old_namesv) {
6e449a3a 1782 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
3280af22 1783 PUSHs(&PL_sv_yes);
06a5b730 1784 }
811a4de9
GS
1785 /* eval BLOCK (try blocks have old_namesv == 0) */
1786 else {
1787 PUSHs(&PL_sv_undef);
1788 PUSHs(&PL_sv_undef);
1789 }
4633a7c4 1790 }
a682de96
GS
1791 else {
1792 PUSHs(&PL_sv_undef);
1793 PUSHs(&PL_sv_undef);
1794 }
bafb2adc 1795 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
ed094faf 1796 && CopSTASH_eq(PL_curcop, PL_debstash))
4633a7c4 1797 {
66a1b24b
AL
1798 AV * const ary = cx->blk_sub.argarray;
1799 const int off = AvARRAY(ary) - AvALLOC(ary);
a0d0e21e 1800
3280af22 1801 if (!PL_dbargs) {
af3885a0
NC
1802 PL_dbargs = GvAV(gv_AVadd(gv_fetchpvs("DB::args", GV_ADDMULTI,
1803 SVt_PVAV)));
3ddcf04c 1804 AvREAL_off(PL_dbargs); /* XXX should be REIFY (see av.h) */
a0d0e21e
LW
1805 }
1806
3280af22
NIS
1807 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1808 av_extend(PL_dbargs, AvFILLp(ary) + off);
1809 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1810 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e 1811 }
f3aa04c2
GS
1812 /* XXX only hints propagated via op_private are currently
1813 * visible (others are not easily accessible, since they
1814 * use the global PL_hints) */
6e449a3a 1815 mPUSHi(CopHINTS_get(cx->blk_oldcop));
e476b1b5
GS
1816 {
1817 SV * mask ;
72dc9ed5 1818 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
114bafba 1819
ac27b0f5 1820 if (old_warnings == pWARN_NONE ||
114bafba 1821 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
e476b1b5 1822 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
ac27b0f5 1823 else if (old_warnings == pWARN_ALL ||
75b6c4ca
RGS
1824 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1825 /* Get the bit mask for $warnings::Bits{all}, because
1826 * it could have been extended by warnings::register */
1827 SV **bits_all;
6673a63c 1828 HV * const bits = get_hv("warnings::Bits", 0);
017a3ce5 1829 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
1830 mask = newSVsv(*bits_all);
1831 }
1832 else {
1833 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1834 }
1835 }
e476b1b5 1836 else
72dc9ed5 1837 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
6e449a3a 1838 mPUSHs(mask);
e476b1b5 1839 }
b3ca2e83 1840
c28fe1ec 1841 PUSHs(cx->blk_oldcop->cop_hints_hash ?
b3ca2e83 1842 sv_2mortal(newRV_noinc(
ad64d0ec
NC
1843 MUTABLE_SV(Perl_refcounted_he_chain_2hv(aTHX_
1844 cx->blk_oldcop->cop_hints_hash))))
b3ca2e83 1845 : &PL_sv_undef);
a0d0e21e
LW
1846 RETURN;
1847}
1848
a0d0e21e
LW
1849PP(pp_reset)
1850{
97aff369 1851 dVAR;
39644a26 1852 dSP;
10edeb5d 1853 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
11faa288 1854 sv_reset(tmps, CopSTASH(PL_curcop));
3280af22 1855 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1856 RETURN;
1857}
1858
dd2155a4
DM
1859/* like pp_nextstate, but used instead when the debugger is active */
1860
a0d0e21e
LW
1861PP(pp_dbstate)
1862{
27da23d5 1863 dVAR;
533c011a 1864 PL_curcop = (COP*)PL_op;
a0d0e21e 1865 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 1866 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1867 FREETMPS;
1868
5df8de69
DM
1869 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1870 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
a0d0e21e 1871 {
39644a26 1872 dSP;
c09156bb 1873 register PERL_CONTEXT *cx;
f54cb97a 1874 const I32 gimme = G_ARRAY;
eb160463 1875 U8 hasargs;
0bd48802
AL
1876 GV * const gv = PL_DBgv;
1877 register CV * const cv = GvCV(gv);
a0d0e21e 1878
a0d0e21e 1879 if (!cv)
cea2e8a9 1880 DIE(aTHX_ "No DB::DB routine defined");
a0d0e21e 1881
aea4f609
DM
1882 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1883 /* don't do recursive DB::DB call */
a0d0e21e 1884 return NORMAL;
748a9306 1885
a57c6685 1886 ENTER;
4633a7c4
LW
1887 SAVETMPS;
1888
3280af22 1889 SAVEI32(PL_debug);
55497cff 1890 SAVESTACK_POS();
3280af22 1891 PL_debug = 0;
748a9306 1892 hasargs = 0;
924508f0 1893 SPAGAIN;
748a9306 1894
aed2304a 1895 if (CvISXSUB(cv)) {
c127bd3a
SF
1896 CvDEPTH(cv)++;
1897 PUSHMARK(SP);
1898 (void)(*CvXSUB(cv))(aTHX_ cv);
1899 CvDEPTH(cv)--;
1900 FREETMPS;
a57c6685 1901 LEAVE;
c127bd3a
SF
1902 return NORMAL;
1903 }
1904 else {
1905 PUSHBLOCK(cx, CXt_SUB, SP);
1906 PUSHSUB_DB(cx);
1907 cx->blk_sub.retop = PL_op->op_next;
1908 CvDEPTH(cv)++;
1909 SAVECOMPPAD();
1910 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1911 RETURNOP(CvSTART(cv));
1912 }
a0d0e21e
LW
1913 }
1914 else
1915 return NORMAL;
1916}
1917
a0d0e21e
LW
1918PP(pp_enteriter)
1919{
27da23d5 1920 dVAR; dSP; dMARK;
c09156bb 1921 register PERL_CONTEXT *cx;
f54cb97a 1922 const I32 gimme = GIMME_V;
a0d0e21e 1923 SV **svp;
840fe433 1924 U8 cxtype = CXt_LOOP_FOR;
7766f137 1925#ifdef USE_ITHREADS
e846cb92 1926 PAD *iterdata;
7766f137 1927#endif
a0d0e21e 1928
d343c3ef 1929 ENTER_with_name("loop1");
4633a7c4
LW
1930 SAVETMPS;
1931
533c011a 1932 if (PL_op->op_targ) {
14f338dc
DM
1933 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
1934 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
1935 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
1936 SVs_PADSTALE, SVs_PADSTALE);
1937 }
09edbca0 1938 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
c3564e5c 1939#ifndef USE_ITHREADS
dd2155a4 1940 svp = &PAD_SVl(PL_op->op_targ); /* "my" variable */
c3564e5c 1941#else
e846cb92 1942 iterdata = NULL;
7766f137 1943#endif
54b9620d
MB
1944 }
1945 else {
159b6efe 1946 GV * const gv = MUTABLE_GV(POPs);
7766f137 1947 svp = &GvSV(gv); /* symbol table variable */
0214ae40 1948 SAVEGENERICSV(*svp);
561b68a9 1949 *svp = newSV(0);
7766f137 1950#ifdef USE_ITHREADS
e846cb92 1951 iterdata = (PAD*)gv;
7766f137 1952#endif
54b9620d 1953 }
4633a7c4 1954
0d863452
RH
1955 if (PL_op->op_private & OPpITER_DEF)
1956 cxtype |= CXp_FOR_DEF;
1957
d343c3ef 1958 ENTER_with_name("loop2");
a0d0e21e 1959
7766f137
GS
1960 PUSHBLOCK(cx, cxtype, SP);
1961#ifdef USE_ITHREADS
e846cb92 1962 PUSHLOOP_FOR(cx, iterdata, MARK, PL_op->op_targ);
7766f137 1963#else
52d1f6fb 1964 PUSHLOOP_FOR(cx, svp, MARK, 0);
7766f137 1965#endif
533c011a 1966 if (PL_op->op_flags & OPf_STACKED) {
d01136d6
BS
1967 SV *maybe_ary = POPs;
1968 if (SvTYPE(maybe_ary) != SVt_PVAV) {
89ea2908 1969 dPOPss;
d01136d6 1970 SV * const right = maybe_ary;
984a4bea
RD
1971 SvGETMAGIC(sv);
1972 SvGETMAGIC(right);
4fe3f0fa 1973 if (RANGE_IS_NUMERIC(sv,right)) {
d01136d6 1974 cx->cx_type &= ~CXTYPEMASK;
c6fdafd0
NC
1975 cx->cx_type |= CXt_LOOP_LAZYIV;
1976 /* Make sure that no-one re-orders cop.h and breaks our
1977 assumptions */
1978 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
a2309040
JH
1979#ifdef NV_PRESERVES_UV
1980 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
1981 (SvNV(sv) > (NV)IV_MAX)))
1982 ||
1983 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
1984 (SvNV(right) < (NV)IV_MIN))))
1985#else
1986 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
1987 ||
1988 ((SvNV(sv) > 0) &&
1989 ((SvUV(sv) > (UV)IV_MAX) ||
1990 (SvNV(sv) > (NV)UV_MAX)))))
1991 ||
1992 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
1993 ||
1994 ((SvNV(right) > 0) &&
1995 ((SvUV(right) > (UV)IV_MAX) ||
1996 (SvNV(right) > (NV)UV_MAX))))))
1997#endif
076d9a11 1998 DIE(aTHX_ "Range iterator outside integer range");
d01136d6
BS
1999 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2000 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
d4665a05
DM
2001#ifdef DEBUGGING
2002 /* for correct -Dstv display */
2003 cx->blk_oldsp = sp - PL_stack_base;
2004#endif
89ea2908 2005 }
3f63a782 2006 else {
d01136d6
BS
2007 cx->cx_type &= ~CXTYPEMASK;
2008 cx->cx_type |= CXt_LOOP_LAZYSV;
2009 /* Make sure that no-one re-orders cop.h and breaks our
2010 assumptions */
2011 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2012 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2013 cx->blk_loop.state_u.lazysv.end = right;
2014 SvREFCNT_inc(right);
2015 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
267cc4a8
NC
2016 /* This will do the upgrade to SVt_PV, and warn if the value
2017 is uninitialised. */
10516c54 2018 (void) SvPV_nolen_const(right);
267cc4a8
NC
2019 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2020 to replace !SvOK() with a pointer to "". */
2021 if (!SvOK(right)) {
2022 SvREFCNT_dec(right);
d01136d6 2023 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
267cc4a8 2024 }
3f63a782 2025 }
89ea2908 2026 }
d01136d6 2027 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
502c6561 2028 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
d01136d6
BS
2029 SvREFCNT_inc(maybe_ary);
2030 cx->blk_loop.state_u.ary.ix =
2031 (PL_op->op_private & OPpITER_REVERSED) ?
2032 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2033 -1;
ef3e5ea9 2034 }
89ea2908 2035 }
d01136d6
BS
2036 else { /* iterating over items on the stack */
2037 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
ef3e5ea9 2038 if (PL_op->op_private & OPpITER_REVERSED) {
d01136d6 2039 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
ef3e5ea9
NC
2040 }
2041 else {
d01136d6 2042 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
ef3e5ea9 2043 }
4633a7c4 2044 }
a0d0e21e
LW
2045
2046 RETURN;
2047}
2048
2049PP(pp_enterloop)
2050{
27da23d5 2051 dVAR; dSP;
c09156bb 2052 register PERL_CONTEXT *cx;
f54cb97a 2053 const I32 gimme = GIMME_V;
a0d0e21e 2054
d343c3ef 2055 ENTER_with_name("loop1");
a0d0e21e 2056 SAVETMPS;
d343c3ef 2057 ENTER_with_name("loop2");
a0d0e21e 2058
3b719c58
NC
2059 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2060 PUSHLOOP_PLAIN(cx, SP);
a0d0e21e
LW
2061
2062 RETURN;
2063}
2064
2065PP(pp_leaveloop)
2066{
27da23d5 2067 dVAR; dSP;
c09156bb 2068 register PERL_CONTEXT *cx;
a0d0e21e
LW
2069 I32 gimme;
2070 SV **newsp;
2071 PMOP *newpm;
2072 SV **mark;
2073
2074 POPBLOCK(cx,newpm);
3b719c58 2075 assert(CxTYPE_is_LOOP(cx));
4fdae800 2076 mark = newsp;
a8bba7fa 2077 newsp = PL_stack_base + cx->blk_loop.resetsp;
f86702cc 2078
a1f49e72 2079 TAINT_NOT;
54310121 2080 if (gimme == G_VOID)
6f207bd3 2081 NOOP;
54310121
PP
2082 else if (gimme == G_SCALAR) {
2083 if (mark < SP)
2084 *++newsp = sv_mortalcopy(*SP);
2085 else
3280af22 2086 *++newsp = &PL_sv_undef;
a0d0e21e
LW
2087 }
2088 else {
a1f49e72 2089 while (mark < SP) {
a0d0e21e 2090 *++newsp = sv_mortalcopy(*++mark);
a1f49e72
CS
2091 TAINT_NOT; /* Each item is independent */
2092 }
a0d0e21e 2093 }
f86702cc
PP
2094 SP = newsp;
2095 PUTBACK;
2096
a8bba7fa 2097 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
3280af22 2098 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2099
d343c3ef
GG
2100 LEAVE_with_name("loop2");
2101 LEAVE_with_name("loop1");
a0d0e21e 2102
f86702cc 2103 return NORMAL;
a0d0e21e
LW
2104}
2105
2106PP(pp_return)
2107{
27da23d5 2108 dVAR; dSP; dMARK;
c09156bb 2109 register PERL_CONTEXT *cx;
f86702cc 2110 bool popsub2 = FALSE;
b45de488 2111 bool clear_errsv = FALSE;
a0d0e21e
LW
2112 I32 gimme;
2113 SV **newsp;
2114 PMOP *newpm;
2115 I32 optype = 0;
b0d9ce38 2116 SV *sv;
b263a1ad 2117 OP *retop = NULL;
a0d0e21e 2118
0bd48802
AL
2119 const I32 cxix = dopoptosub(cxstack_ix);
2120
9850bf21
RH
2121 if (cxix < 0) {
2122 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2123 * sort block, which is a CXt_NULL
2124 * not a CXt_SUB */
2125 dounwind(0);
d7507f74
RH
2126 PL_stack_base[1] = *PL_stack_sp;
2127 PL_stack_sp = PL_stack_base + 1;
a0d0e21e
LW
2128 return 0;
2129 }
9850bf21
RH
2130 else
2131 DIE(aTHX_ "Can't return outside a subroutine");
a0d0e21e 2132 }
a0d0e21e
LW
2133 if (cxix < cxstack_ix)
2134 dounwind(cxix);
2135
d7507f74
RH
2136 if (CxMULTICALL(&cxstack[cxix])) {
2137 gimme = cxstack[cxix].blk_gimme;
2138 if (gimme == G_VOID)
2139 PL_stack_sp = PL_stack_base;
2140 else if (gimme == G_SCALAR) {
2141 PL_stack_base[1] = *PL_stack_sp;
2142 PL_stack_sp = PL_stack_base + 1;
2143 }
9850bf21 2144 return 0;
d7507f74 2145 }
9850bf21 2146
a0d0e21e 2147 POPBLOCK(cx,newpm);
6b35e009 2148 switch (CxTYPE(cx)) {
a0d0e21e 2149 case CXt_SUB:
f86702cc 2150 popsub2 = TRUE;
f39bc417 2151 retop = cx->blk_sub.retop;
5dd42e15 2152 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
a0d0e21e
LW
2153 break;
2154 case CXt_EVAL:
b45de488
GS
2155 if (!(PL_in_eval & EVAL_KEEPERR))
2156 clear_errsv = TRUE;
a0d0e21e 2157 POPEVAL(cx);
f39bc417 2158 retop = cx->blk_eval.retop;
1d76a5c3
GS
2159 if (CxTRYBLOCK(cx))
2160 break;
067f92a0 2161 lex_end();
748a9306
LW
2162 if (optype == OP_REQUIRE &&
2163 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2164 {
54310121 2165 /* Unassume the success we assumed earlier. */
901017d6 2166 SV * const nsv = cx->blk_eval.old_namesv;
b15aece3 2167 (void)hv_delete(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv), G_DISCARD);
be2597df 2168 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(nsv));
748a9306 2169 }
a0d0e21e 2170 break;
7766f137
GS
2171 case CXt_FORMAT:
2172 POPFORMAT(cx);
f39bc417 2173 retop = cx->blk_sub.retop;
7766f137 2174 break;
a0d0e21e 2175 default:
cea2e8a9 2176 DIE(aTHX_ "panic: return");
a0d0e21e
LW
2177 }
2178
a1f49e72 2179 TAINT_NOT;
a0d0e21e 2180 if (gimme == G_SCALAR) {
a29cdaf0
IZ
2181 if (MARK < SP) {
2182 if (popsub2) {
a8bba7fa 2183 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
2184 if (SvTEMP(TOPs)) {
2185 *++newsp = SvREFCNT_inc(*SP);
2186 FREETMPS;
2187 sv_2mortal(*newsp);
959e3673
GS
2188 }
2189 else {
2190 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
a29cdaf0 2191 FREETMPS;
959e3673
GS
2192 *++newsp = sv_mortalcopy(sv);
2193 SvREFCNT_dec(sv);
a29cdaf0 2194 }
959e3673
GS
2195 }
2196 else
a29cdaf0 2197 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
959e3673
GS
2198 }
2199 else
a29cdaf0 2200 *++newsp = sv_mortalcopy(*SP);
959e3673
GS
2201 }
2202 else
3280af22 2203 *++newsp = &PL_sv_undef;
a0d0e21e 2204 }
54310121 2205 else if (gimme == G_ARRAY) {
a1f49e72 2206 while (++MARK <= SP) {
f86702cc
PP
2207 *++newsp = (popsub2 && SvTEMP(*MARK))
2208 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2209 TAINT_NOT; /* Each item is independent */
2210 }
a0d0e21e 2211 }
3280af22 2212 PL_stack_sp = newsp;
a0d0e21e 2213
5dd42e15 2214 LEAVE;
f86702cc
PP
2215 /* Stack values are safe: */
2216 if (popsub2) {
5dd42e15 2217 cxstack_ix--;
b0d9ce38 2218 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2219 }
b0d9ce38 2220 else
c445ea15 2221 sv = NULL;
3280af22 2222 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2223
b0d9ce38 2224 LEAVESUB(sv);
8433848b 2225 if (clear_errsv) {
ab69dbc2 2226 CLEAR_ERRSV();
8433848b 2227 }
f39bc417 2228 return retop;
a0d0e21e
LW
2229}
2230
2231PP(pp_last)
2232{
27da23d5 2233 dVAR; dSP;
a0d0e21e 2234 I32 cxix;
c09156bb 2235 register PERL_CONTEXT *cx;
f86702cc 2236 I32 pop2 = 0;
a0d0e21e 2237 I32 gimme;
8772537c 2238 I32 optype;
b263a1ad 2239 OP *nextop = NULL;
a0d0e21e
LW
2240 SV **newsp;
2241 PMOP *newpm;
a8bba7fa 2242 SV **mark;
c445ea15 2243 SV *sv = NULL;
9d4ba2ae 2244
a0d0e21e 2245
533c011a 2246 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2247 cxix = dopoptoloop(cxstack_ix);
2248 if (cxix < 0)
a651a37d 2249 DIE(aTHX_ "Can't \"last\" outside a loop block");
a0d0e21e
LW
2250 }
2251 else {
2252 cxix = dopoptolabel(cPVOP->op_pv);
2253 if (cxix < 0)
cea2e8a9 2254 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
a0d0e21e
LW
2255 }
2256 if (cxix < cxstack_ix)
2257 dounwind(cxix);
2258
2259 POPBLOCK(cx,newpm);
5dd42e15 2260 cxstack_ix++; /* temporarily protect top context */
a8bba7fa 2261 mark = newsp;
6b35e009 2262 switch (CxTYPE(cx)) {
c6fdafd0 2263 case CXt_LOOP_LAZYIV:
d01136d6 2264 case CXt_LOOP_LAZYSV:
3b719c58
NC
2265 case CXt_LOOP_FOR:
2266 case CXt_LOOP_PLAIN:
2267 pop2 = CxTYPE(cx);
a8bba7fa 2268 newsp = PL_stack_base + cx->blk_loop.resetsp;
022eaa24 2269 nextop = cx->blk_loop.my_op->op_lastop->op_next;
a0d0e21e 2270 break;
f86702cc 2271 case CXt_SUB:
f86702cc 2272 pop2 = CXt_SUB;
f39bc417 2273 nextop = cx->blk_sub.retop;
a0d0e21e 2274 break;
f86702cc
PP
2275 case CXt_EVAL:
2276 POPEVAL(cx);
f39bc417 2277 nextop = cx->blk_eval.retop;
a0d0e21e 2278 break;
7766f137
GS
2279 case CXt_FORMAT:
2280 POPFORMAT(cx);
f39bc417 2281 nextop = cx->blk_sub.retop;
7766f137 2282 break;
a0d0e21e 2283 default:
cea2e8a9 2284 DIE(aTHX_ "panic: last");
a0d0e21e
LW
2285 }
2286
a1f49e72 2287 TAINT_NOT;
a0d0e21e 2288 if (gimme == G_SCALAR) {
f86702cc
PP
2289 if (MARK < SP)
2290 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2291 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 2292 else
3280af22 2293 *++newsp = &PL_sv_undef;
a0d0e21e 2294 }
54310121 2295 else if (gimme == G_ARRAY) {
a1f49e72 2296 while (++MARK <= SP) {
f86702cc
PP
2297 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2298 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2299 TAINT_NOT; /* Each item is independent */
2300 }
f86702cc
PP
2301 }
2302 SP = newsp;
2303 PUTBACK;
2304
5dd42e15
DM
2305 LEAVE;
2306 cxstack_ix--;
f86702cc
PP
2307 /* Stack values are safe: */
2308 switch (pop2) {
c6fdafd0 2309 case CXt_LOOP_LAZYIV:
3b719c58 2310 case CXt_LOOP_PLAIN:
d01136d6 2311 case CXt_LOOP_LAZYSV:
3b719c58 2312 case CXt_LOOP_FOR:
a8bba7fa 2313 POPLOOP(cx); /* release loop vars ... */
4fdae800 2314 LEAVE;
f86702cc
PP
2315 break;
2316 case CXt_SUB:
b0d9ce38 2317 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2318 break;
a0d0e21e 2319 }
3280af22 2320 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e 2321
b0d9ce38 2322 LEAVESUB(sv);
9d4ba2ae
AL
2323 PERL_UNUSED_VAR(optype);
2324 PERL_UNUSED_VAR(gimme);
f86702cc 2325 return nextop;
a0d0e21e
LW
2326}
2327
2328PP(pp_next)
2329{
27da23d5 2330 dVAR;
a0d0e21e 2331 I32 cxix;
c09156bb 2332 register PERL_CONTEXT *cx;
85538317 2333 I32 inner;
a0d0e21e 2334
533c011a 2335 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2336 cxix = dopoptoloop(cxstack_ix);
2337 if (cxix < 0)
a651a37d 2338 DIE(aTHX_ "Can't \"next\" outside a loop block");
a0d0e21e
LW
2339 }
2340 else {
2341 cxix = dopoptolabel(cPVOP->op_pv);
2342 if (cxix < 0)
cea2e8a9 2343 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
a0d0e21e
LW
2344 }
2345 if (cxix < cxstack_ix)
2346 dounwind(cxix);
2347
85538317
GS
2348 /* clear off anything above the scope we're re-entering, but
2349 * save the rest until after a possible continue block */
2350 inner = PL_scopestack_ix;
1ba6ee2b 2351 TOPBLOCK(cx);
85538317
GS
2352 if (PL_scopestack_ix < inner)
2353 leave_scope(PL_scopestack[PL_scopestack_ix]);
3a1b2b9e 2354 PL_curcop = cx->blk_oldcop;
022eaa24 2355 return CX_LOOP_NEXTOP_GET(cx);
a0d0e21e
LW
2356}
2357
2358PP(pp_redo)
2359{
27da23d5 2360 dVAR;
a0d0e21e 2361 I32 cxix;
c09156bb 2362 register PERL_CONTEXT *cx;
a0d0e21e 2363 I32 oldsave;
a034e688 2364 OP* redo_op;
a0d0e21e 2365
533c011a 2366 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2367 cxix = dopoptoloop(cxstack_ix);
2368 if (cxix < 0)
a651a37d 2369 DIE(aTHX_ "Can't \"redo\" outside a loop block");
a0d0e21e
LW
2370 }
2371 else {
2372 cxix = dopoptolabel(cPVOP->op_pv);
2373 if (cxix < 0)
cea2e8a9 2374 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
a0d0e21e
LW
2375 }
2376 if (cxix < cxstack_ix)
2377 dounwind(cxix);
2378
022eaa24 2379 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
a034e688
DM
2380 if (redo_op->op_type == OP_ENTER) {
2381 /* pop one less context to avoid $x being freed in while (my $x..) */
2382 cxstack_ix++;
2383 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2384 redo_op = redo_op->op_next;
2385 }
2386
a0d0e21e 2387 TOPBLOCK(cx);
3280af22 2388 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e 2389 LEAVE_SCOPE(oldsave);
936c78b5 2390 FREETMPS;
3a1b2b9e 2391 PL_curcop = cx->blk_oldcop;
a034e688 2392 return redo_op;
a0d0e21e
LW
2393}
2394
0824fdcb 2395STATIC OP *
bfed75c6 2396S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
a0d0e21e 2397{
97aff369 2398 dVAR;
a0d0e21e 2399 OP **ops = opstack;
bfed75c6 2400 static const char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 2401
7918f24d
NC
2402 PERL_ARGS_ASSERT_DOFINDLABEL;
2403
fc36a67e 2404 if (ops >= oplimit)
cea2e8a9 2405 Perl_croak(aTHX_ too_deep);
11343788
MB
2406 if (o->op_type == OP_LEAVE ||
2407 o->op_type == OP_SCOPE ||
2408 o->op_type == OP_LEAVELOOP ||
33d34e4c 2409 o->op_type == OP_LEAVESUB ||
11343788 2410 o->op_type == OP_LEAVETRY)
fc36a67e 2411 {
5dc0d613 2412 *ops++ = cUNOPo->op_first;
fc36a67e 2413 if (ops >= oplimit)
cea2e8a9 2414 Perl_croak(aTHX_ too_deep);
fc36a67e 2415 }
c4aa4e48 2416 *ops = 0;
11343788 2417 if (o->op_flags & OPf_KIDS) {
aec46f14 2418 OP *kid;
a0d0e21e 2419 /* First try all the kids at this level, since that's likeliest. */
11343788 2420 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
7e8f1eac
AD
2421 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2422 const char *kid_label = CopLABEL(kCOP);
2423 if (kid_label && strEQ(kid_label, label))
2424 return kid;
2425 }
a0d0e21e 2426 }
11343788 2427 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
3280af22 2428 if (kid == PL_lastgotoprobe)
a0d0e21e 2429 continue;
ed8d0fe2
SM
2430 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2431 if (ops == opstack)
2432 *ops++ = kid;
2433 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2434 ops[-1]->op_type == OP_DBSTATE)
2435 ops[-1] = kid;
2436 else
2437 *ops++ = kid;
2438 }
155aba94 2439 if ((o = dofindlabel(kid, label, ops, oplimit)))
11343788 2440 return o;
a0d0e21e
LW
2441 }
2442 }
c4aa4e48 2443 *ops = 0;
a0d0e21e
LW
2444 return 0;
2445}
2446
a0d0e21e
LW
2447PP(pp_goto)
2448{
27da23d5 2449 dVAR; dSP;
cbbf8932 2450 OP *retop = NULL;
a0d0e21e 2451 I32 ix;
c09156bb 2452 register PERL_CONTEXT *cx;
fc36a67e
PP
2453#define GOTO_DEPTH 64
2454 OP *enterops[GOTO_DEPTH];
cbbf8932 2455 const char *label = NULL;
bfed75c6
AL
2456 const bool do_dump = (PL_op->op_type == OP_DUMP);
2457 static const char must_have_label[] = "goto must have label";
a0d0e21e 2458
533c011a 2459 if (PL_op->op_flags & OPf_STACKED) {
9d4ba2ae 2460 SV * const sv = POPs;
a0d0e21e
LW
2461
2462 /* This egregious kludge implements goto &subroutine */
2463 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2464 I32 cxix;
c09156bb 2465 register PERL_CONTEXT *cx;
ea726b52 2466 CV *cv = MUTABLE_CV(SvRV(sv));
a0d0e21e
LW
2467 SV** mark;
2468 I32 items = 0;
2469 I32 oldsave;
b1464ded 2470 bool reified = 0;
a0d0e21e 2471
e8f7dd13 2472 retry:
4aa0a1f7 2473 if (!CvROOT(cv) && !CvXSUB(cv)) {
7fc63493 2474 const GV * const gv = CvGV(cv);
e8f7dd13 2475 if (gv) {
7fc63493 2476 GV *autogv;
e8f7dd13
GS
2477 SV *tmpstr;
2478 /* autoloaded stub? */
2479 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2480 goto retry;
2481 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2482 GvNAMELEN(gv), FALSE);
2483 if (autogv && (cv = GvCV(autogv)))
2484 goto retry;
2485 tmpstr = sv_newmortal();
c445ea15 2486 gv_efullname3(tmpstr, gv, NULL);
be2597df 2487 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
4aa0a1f7 2488 }
cea2e8a9 2489 DIE(aTHX_ "Goto undefined subroutine");
4aa0a1f7
AD
2490 }
2491
a0d0e21e 2492 /* First do some returnish stuff. */
b37c2d43 2493 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
71fc2216 2494 FREETMPS;
a0d0e21e
LW
2495 cxix = dopoptosub(cxstack_ix);
2496 if (cxix < 0)
cea2e8a9 2497 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
a0d0e21e
LW
2498 if (cxix < cxstack_ix)
2499 dounwind(cxix);
2500 TOPBLOCK(cx);
2d43a17f 2501 SPAGAIN;
564abe23 2502 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2d43a17f 2503 if (CxTYPE(cx) == CXt_EVAL) {
c74ace89
DM
2504 if (CxREALEVAL(cx))
2505 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2506 else
2507 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2d43a17f 2508 }
9850bf21
RH
2509 else if (CxMULTICALL(cx))
2510 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
bafb2adc 2511 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
d8b46c1b 2512 /* put @_ back onto stack */
a0d0e21e 2513 AV* av = cx->blk_sub.argarray;
bfed75c6 2514
93965878 2515 items = AvFILLp(av) + 1;
a45cdc79
DM
2516 EXTEND(SP, items+1); /* @_ could have been extended. */
2517 Copy(AvARRAY(av), SP + 1, items, SV*);
3280af22
NIS
2518 SvREFCNT_dec(GvAV(PL_defgv));
2519 GvAV(PL_defgv) = cx->blk_sub.savearray;
b1464ded 2520 CLEAR_ARGARRAY(av);
d8b46c1b 2521 /* abandon @_ if it got reified */
62b1ebc2 2522 if (AvREAL(av)) {
b1464ded
DM
2523 reified = 1;
2524 SvREFCNT_dec(av);
d8b46c1b
GS
2525 av = newAV();
2526 av_extend(av, items-1);
11ca45c0 2527 AvREIFY_only(av);
ad64d0ec 2528 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
62b1ebc2 2529 }
a0d0e21e 2530 }
aed2304a 2531 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
890ce7af 2532 AV* const av = GvAV(PL_defgv);
1fa4e549 2533 items = AvFILLp(av) + 1;
a45cdc79
DM
2534 EXTEND(SP, items+1); /* @_ could have been extended. */
2535 Copy(AvARRAY(av), SP + 1, items, SV*);
1fa4e549 2536 }
a45cdc79
DM
2537 mark = SP;
2538 SP += items;
6b35e009 2539 if (CxTYPE(cx) == CXt_SUB &&
b150fb22 2540 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
a0d0e21e 2541 SvREFCNT_dec(cx->blk_sub.cv);
3280af22 2542 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
2543 LEAVE_SCOPE(oldsave);
2544
2545 /* Now do some callish stuff. */
2546 SAVETMPS;
5023d17a 2547 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
aed2304a 2548 if (CvISXSUB(cv)) {
b37c2d43 2549 OP* const retop = cx->blk_sub.retop;
f73ef291
NC
2550 SV **newsp;
2551 I32 gimme;
b1464ded
DM
2552 if (reified) {
2553 I32 index;
2554 for (index=0; index<items; index++)
2555 sv_2mortal(SP[-index]);
2556 }
1fa4e549 2557
b37c2d43
AL
2558 /* XS subs don't have a CxSUB, so pop it */
2559 POPBLOCK(cx, PL_curpm);
2560 /* Push a mark for the start of arglist */
2561 PUSHMARK(mark);
2562 PUTBACK;
2563 (void)(*CvXSUB(cv))(aTHX_ cv);
a57c6685 2564 LEAVE;
5eff7df7 2565 return retop;
a0d0e21e
LW
2566 }
2567 else {
b37c2d43 2568 AV* const padlist = CvPADLIST(cv);
6b35e009 2569 if (CxTYPE(cx) == CXt_EVAL) {
85a64632 2570 PL_in_eval = CxOLD_IN_EVAL(cx);
3280af22 2571 PL_eval_root = cx->blk_eval.old_eval_root;
b150fb22 2572 cx->cx_type = CXt_SUB;
b150fb22 2573 }
a0d0e21e 2574 cx->blk_sub.cv = cv;
1a5b3db4 2575 cx->blk_sub.olddepth = CvDEPTH(cv);
dd2155a4 2576
a0d0e21e
LW
2577 CvDEPTH(cv)++;
2578 if (CvDEPTH(cv) < 2)
74c765eb 2579 SvREFCNT_inc_simple_void_NN(cv);
dd2155a4 2580 else {
2b9dff67 2581 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
44a8e56a 2582 sub_crush_depth(cv);
26019298 2583 pad_push(padlist, CvDEPTH(cv));
a0d0e21e 2584 }
fd617465
DM
2585 SAVECOMPPAD();
2586 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
bafb2adc 2587 if (CxHASARGS(cx))
6d4ff0d2 2588 {
502c6561 2589 AV *const av = MUTABLE_AV(PAD_SVl(0));
a0d0e21e 2590
3280af22 2591 cx->blk_sub.savearray = GvAV(PL_defgv);
502c6561 2592 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
dd2155a4 2593 CX_CURPAD_SAVE(cx->blk_sub);
6d4ff0d2 2594 cx->blk_sub.argarray = av;
a0d0e21e
LW
2595
2596 if (items >= AvMAX(av) + 1) {
b37c2d43 2597 SV **ary = AvALLOC(av);
a0d0e21e
LW
2598 if (AvARRAY(av) != ary) {
2599 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
9c6bc640 2600 AvARRAY(av) = ary;
a0d0e21e
LW
2601 }
2602 if (items >= AvMAX(av) + 1) {
2603 AvMAX(av) = items - 1;
2604 Renew(ary,items+1,SV*);
2605 AvALLOC(av) = ary;
9c6bc640 2606 AvARRAY(av) = ary;
a0d0e21e
LW
2607 }
2608 }
a45cdc79 2609 ++mark;
a0d0e21e 2610 Copy(mark,AvARRAY(av),items,SV*);
93965878 2611 AvFILLp(av) = items - 1;
d8b46c1b 2612 assert(!AvREAL(av));
b1464ded
DM
2613 if (reified) {
2614 /* transfer 'ownership' of refcnts to new @_ */
2615 AvREAL_on(av);
2616 AvREIFY_off(av);
2617 }
a0d0e21e
LW
2618 while (items--) {
2619 if (*mark)
2620 SvTEMP_off(*mark);
2621 mark++;
2622 }
2623 }
491527d0 2624 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
005a8a35 2625 Perl_get_db_sub(aTHX_ NULL, cv);
b37c2d43 2626 if (PERLDB_GOTO) {
b96d8cd9 2627 CV * const gotocv = get_cvs("DB::goto", 0);
b37c2d43
AL
2628 if (gotocv) {
2629 PUSHMARK( PL_stack_sp );
ad64d0ec 2630 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
b37c2d43
AL
2631 PL_stack_sp--;
2632 }
491527d0 2633 }
1ce6579f 2634 }
a0d0e21e
LW
2635 RETURNOP(CvSTART(cv));
2636 }
2637 }
1614b0e3 2638 else {
0510663f 2639 label = SvPV_nolen_const(sv);
1614b0e3 2640 if (!(do_dump || *label))
cea2e8a9 2641 DIE(aTHX_ must_have_label);
1614b0e3 2642 }
a0d0e21e 2643 }
533c011a 2644 else if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 2645 if (! do_dump)
cea2e8a9 2646 DIE(aTHX_ must_have_label);
a0d0e21e
LW
2647 }
2648 else
2649 label = cPVOP->op_pv;
2650
2651 if (label && *label) {
cbbf8932 2652 OP *gotoprobe = NULL;
3b2447bc 2653 bool leaving_eval = FALSE;
33d34e4c 2654 bool in_block = FALSE;
cbbf8932 2655 PERL_CONTEXT *last_eval_cx = NULL;
a0d0e21e
LW
2656
2657 /* find label */
2658
d4c19fe8 2659 PL_lastgotoprobe = NULL;
a0d0e21e
LW
2660 *enterops = 0;
2661 for (ix = cxstack_ix; ix >= 0; ix--) {
2662 cx = &cxstack[ix];
6b35e009 2663 switch (CxTYPE(cx)) {
a0d0e21e 2664 case CXt_EVAL:
3b2447bc 2665 leaving_eval = TRUE;
971ecbe6 2666 if (!CxTRYBLOCK(cx)) {
a4f3a277
RH
2667 gotoprobe = (last_eval_cx ?
2668 last_eval_cx->blk_eval.old_eval_root :
2669 PL_eval_root);
2670 last_eval_cx = cx;
9c5794fe
RH
2671 break;
2672 }
2673 /* else fall through */
c6fdafd0 2674 case CXt_LOOP_LAZYIV:
d01136d6 2675 case CXt_LOOP_LAZYSV:
3b719c58
NC
2676 case CXt_LOOP_FOR:
2677 case CXt_LOOP_PLAIN:
bb5aedc1
VP
2678 case CXt_GIVEN:
2679 case CXt_WHEN:
a0d0e21e
LW
2680 gotoprobe = cx->blk_oldcop->op_sibling;
2681 break;
2682 case CXt_SUBST:
2683 continue;
2684 case CXt_BLOCK:
33d34e4c 2685 if (ix) {
a0d0e21e 2686 gotoprobe = cx->blk_oldcop->op_sibling;
33d34e4c
AE
2687 in_block = TRUE;
2688 } else
3280af22 2689 gotoprobe = PL_main_root;
a0d0e21e 2690 break;
b3933176 2691 case CXt_SUB:
9850bf21 2692 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
b3933176
CS
2693 gotoprobe = CvROOT(cx->blk_sub.cv);
2694 break;
2695 }
2696 /* FALL THROUGH */
7766f137 2697 case CXt_FORMAT:
0a753a76 2698 case CXt_NULL:
a651a37d 2699 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
a0d0e21e
LW
2700 default:
2701 if (ix)
cea2e8a9 2702 DIE(aTHX_ "panic: goto");
3280af22 2703 gotoprobe = PL_main_root;
a0d0e21e
LW
2704 break;
2705 }
2b597662
GS
2706 if (gotoprobe) {
2707 retop = dofindlabel(gotoprobe, label,
2708 enterops, enterops + GOTO_DEPTH);
2709 if (retop)
2710 break;
2711 }
3280af22 2712 PL_lastgotoprobe = gotoprobe;
a0d0e21e
LW
2713 }
2714 if (!retop)
cea2e8a9 2715 DIE(aTHX_ "Can't find label %s", label);
a0d0e21e 2716
3b2447bc
RH
2717 /* if we're leaving an eval, check before we pop any frames
2718 that we're not going to punt, otherwise the error
2719 won't be caught */
2720
2721 if (leaving_eval && *enterops && enterops[1]) {
2722 I32 i;
2723 for (i = 1; enterops[i]; i++)
2724 if (enterops[i]->op_type == OP_ENTERITER)
2725 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2726 }
2727
b500e03b
GG
2728 if (*enterops && enterops[1]) {
2729 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2730 if (enterops[i])
2731 deprecate("\"goto\" to jump into a construct");
2732 }
2733
a0d0e21e
LW
2734 /* pop unwanted frames */
2735
2736 if (ix < cxstack_ix) {
2737 I32 oldsave;
2738
2739 if (ix < 0)
2740 ix = 0;
2741 dounwind(ix);
2742 TOPBLOCK(cx);
3280af22 2743 oldsave = PL_scopestack[PL_scopestack_ix];
a0d0e21e
LW
2744 LEAVE_SCOPE(oldsave);
2745 }
2746
2747 /* push wanted frames */
2748
748a9306 2749 if (*enterops && enterops[1]) {
0bd48802 2750 OP * const oldop = PL_op;
33d34e4c
AE
2751 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2752 for (; enterops[ix]; ix++) {
533c011a 2753 PL_op = enterops[ix];
84902520
TB
2754 /* Eventually we may want to stack the needed arguments
2755 * for each op. For now, we punt on the hard ones. */
533c011a 2756 if (PL_op->op_type == OP_ENTERITER)
894356b3 2757 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
fc0dc3b3 2758 CALL_FPTR(PL_op->op_ppaddr)(aTHX);
a0d0e21e 2759 }
533c011a 2760 PL_op = oldop;
a0d0e21e
LW
2761 }
2762 }
2763
2764 if (do_dump) {
a5f75d66 2765#ifdef VMS
6b88bc9c 2766 if (!retop) retop = PL_main_start;
a5f75d66 2767#endif
3280af22
NIS
2768 PL_restartop = retop;
2769 PL_do_undump = TRUE;
a0d0e21e
LW
2770
2771 my_unexec();
2772
3280af22
NIS
2773 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2774 PL_do_undump = FALSE;
a0d0e21e
LW
2775 }
2776
2777 RETURNOP(retop);
2778}
2779
2780PP(pp_exit)
2781{
97aff369 2782 dVAR;
39644a26 2783 dSP;
a0d0e21e
LW
2784 I32 anum;
2785
2786 if (MAXARG < 1)
2787 anum = 0;
ff0cee69 2788 else {
a0d0e21e 2789 anum = SvIVx(POPs);
d98f61e7
GS
2790#ifdef VMS
2791 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
ff0cee69 2792 anum = 0;
96e176bf 2793 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
ff0cee69
PP
2794#endif
2795 }
cc3604b1 2796 PL_exit_flags |= PERL_EXIT_EXPECTED;
81d86705
NC
2797#ifdef PERL_MAD
2798 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2799 if (anum || !(PL_minus_c && PL_madskills))
2800 my_exit(anum);
2801#else
a0d0e21e 2802 my_exit(anum);
81d86705 2803#endif
3280af22 2804 PUSHs(&PL_sv_undef);
a0d0e21e
LW
2805 RETURN;
2806}
2807
a0d0e21e
LW
2808/* Eval. */
2809
0824fdcb 2810STATIC void
cea2e8a9 2811S_save_lines(pTHX_ AV *array, SV *sv)
a0d0e21e 2812{
504618e9 2813 const char *s = SvPVX_const(sv);
890ce7af 2814 const char * const send = SvPVX_const(sv) + SvCUR(sv);
504618e9 2815 I32 line = 1;
a0d0e21e 2816
7918f24d
NC
2817 PERL_ARGS_ASSERT_SAVE_LINES;
2818
a0d0e21e 2819 while (s && s < send) {
f54cb97a 2820 const char *t;
b9f83d2f 2821 SV * const tmpstr = newSV_type(SVt_PVMG);
a0d0e21e 2822
1d963ff3 2823 t = (const char *)memchr(s, '\n', send - s);
a0d0e21e
LW
2824 if (t)
2825 t++;
2826 else
2827 t = send;
2828
2829 sv_setpvn(tmpstr, s, t - s);
2830 av_store(array, line++, tmpstr);
2831 s = t;
2832 }
2833}
2834
22f16304
RU
2835/*
2836=for apidoc docatch
2837
2838Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
2839
28400 is used as continue inside eval,
2841
28423 is used for a die caught by an inner eval - continue inner loop
2843
2844See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
2845establish a local jmpenv to handle exception traps.
2846
2847=cut
2848*/
0824fdcb 2849STATIC OP *
cea2e8a9 2850S_docatch(pTHX_ OP *o)
1e422769 2851{
97aff369 2852 dVAR;
6224f72b 2853 int ret;
06b5626a 2854 OP * const oldop = PL_op;
db36c5a1 2855 dJMPENV;
1e422769 2856
1e422769 2857#ifdef DEBUGGING
54310121 2858 assert(CATCH_GET == TRUE);
1e422769 2859#endif
312caa8e 2860 PL_op = o;
8bffa5f8 2861
14dd3ad8 2862 JMPENV_PUSH(ret);
6224f72b 2863 switch (ret) {
312caa8e 2864 case 0:
abd70938
DM
2865 assert(cxstack_ix >= 0);
2866 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2867 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
14dd3ad8 2868 redo_body:
85aaa934 2869 CALLRUNOPS(aTHX);
312caa8e
CS
2870 break;
2871 case 3:
8bffa5f8 2872 /* die caught by an inner eval - continue inner loop */
abd70938
DM
2873
2874 /* NB XXX we rely on the old popped CxEVAL still being at the top
2875 * of the stack; the way die_where() currently works, this
2876 * assumption is valid. In theory The cur_top_env value should be
2877 * returned in another global, the way retop (aka PL_restartop)
2878 * is. */
2879 assert(CxTYPE(&cxstack[cxstack_ix+1]) == CXt_EVAL);
2880
2881 if (PL_restartop
2882 && cxstack[cxstack_ix+1].blk_eval.cur_top_env == PL_top_env)
2883 {
312caa8e
CS
2884 PL_op = PL_restartop;
2885 PL_restartop = 0;
2886 goto redo_body;
2887 }
2888 /* FALL THROUGH */
2889 default:
14dd3ad8 2890 JMPENV_POP;
533c011a 2891 PL_op = oldop;
6224f72b 2892 JMPENV_JUMP(ret);
1e422769 2893 /* NOTREACHED */
1e422769 2894 }
14dd3ad8 2895 JMPENV_POP;
533c011a 2896 PL_op = oldop;
5f66b61c 2897 return NULL;
1e422769
PP
2898}
2899
ee23ad3b
NC
2900/* James Bond: Do you expect me to talk?
2901 Auric Goldfinger: No, Mr. Bond. I expect you to die.
2902
2903 This code is an ugly hack, doesn't work with lexicals in subroutines that are
2904 called more than once, and is only used by regcomp.c, for (?{}) blocks.
2905
2906 Currently it is not used outside the core code. Best if it stays that way.
2907*/
c277df42 2908OP *
bfed75c6 2909Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
c277df42
IZ
2910/* sv Text to convert to OP tree. */
2911/* startop op_free() this to undo. */
2912/* code Short string id of the caller. */
2913{
27da23d5 2914 dVAR; dSP; /* Make POPBLOCK work. */
c277df42
IZ
2915 PERL_CONTEXT *cx;
2916 SV **newsp;
b094c71d 2917 I32 gimme = G_VOID;
c277df42
IZ
2918 I32 optype;
2919 OP dummy;
83ee9e09
GS
2920 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2921 char *tmpbuf = tbuf;
c277df42 2922 char *safestr;
a3985cdc 2923 int runtime;
601f1833 2924 CV* runcv = NULL; /* initialise to avoid compiler warnings */
f7997f86 2925 STRLEN len;
c277df42 2926
7918f24d
NC
2927 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
2928
d343c3ef 2929 ENTER_with_name("eval");
5486870f 2930 lex_start(sv, NULL, FALSE);
c277df42
IZ
2931 SAVETMPS;
2932 /* switch to eval mode */
2933
923e4eb5 2934 if (IN_PERL_COMPILETIME) {
f4dd75d9 2935 SAVECOPSTASH_FREE(&PL_compiling);
11faa288 2936 CopSTASH_set(&PL_compiling, PL_curstash);
cbce877f 2937 }
83ee9e09 2938 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
9d4ba2ae 2939 SV * const sv = sv_newmortal();
83ee9e09
GS
2940 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
2941 code, (unsigned long)++PL_evalseq,
2942 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
2943 tmpbuf = SvPVX(sv);
fc009855 2944 len = SvCUR(sv);
83ee9e09
GS
2945 }
2946 else
d9fad198
JH
2947 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
2948 (unsigned long)++PL_evalseq);
f4dd75d9 2949 SAVECOPFILE_FREE(&PL_compiling);
57843af0 2950 CopFILE_set(&PL_compiling, tmpbuf+2);
f4dd75d9 2951 SAVECOPLINE(&PL_compiling);
57843af0 2952 CopLINE_set(&PL_compiling, 1);
c277df42
IZ
2953 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2954 deleting the eval's FILEGV from the stash before gv_check() runs
2955 (i.e. before run-time proper). To work around the coredump that
2956 ensues, we always turn GvMULTI_on for any globals that were
2957 introduced within evals. See force_ident(). GSAR 96-10-12 */
f7997f86
NC
2958 safestr = savepvn(tmpbuf, len);
2959 SAVEDELETE(PL_defstash, safestr, len);
b3ac6de7 2960 SAVEHINTS();
d1ca3daa 2961#ifdef OP_IN_REGISTER
6b88bc9c 2962 PL_opsave = op;
d1ca3daa 2963#else
7766f137 2964 SAVEVPTR(PL_op);
d1ca3daa 2965#endif
c277df42 2966
a3985cdc 2967 /* we get here either during compilation, or via pp_regcomp at runtime */
923e4eb5 2968 runtime = IN_PERL_RUNTIME;
a3985cdc 2969 if (runtime)
d819b83a 2970 runcv = find_runcv(NULL);
a3985cdc 2971
533c011a 2972 PL_op = &dummy;
13b51b79 2973 PL_op->op_type = OP_ENTEREVAL;
533c011a 2974 PL_op->op_flags = 0; /* Avoid uninit warning. */
923e4eb5 2975 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
6b75f042 2976 PUSHEVAL(cx, 0);
a3985cdc
DM
2977
2978 if (runtime)
410be5db 2979 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
a3985cdc 2980 else
410be5db 2981 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
13b51b79 2982 POPBLOCK(cx,PL_curpm);
e84b9f1f 2983 POPEVAL(cx);
c277df42
IZ
2984
2985 (*startop)->op_type = OP_NULL;
22c35a8c 2986 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
c277df42 2987 lex_end();
f3548bdc 2988 /* XXX DAPM do this properly one year */
502c6561 2989 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
d343c3ef 2990 LEAVE_with_name("eval");
923e4eb5 2991 if (IN_PERL_COMPILETIME)
623e6609 2992 CopHINTS_set(&PL_compiling, PL_hints);
d1ca3daa 2993#ifdef OP_IN_REGISTER
6b88bc9c 2994 op = PL_opsave;
d1ca3daa 2995#endif
9d4ba2ae
AL
2996 PERL_UNUSED_VAR(newsp);
2997 PERL_UNUSED_VAR(optype);
2998
410be5db 2999 return PL_eval_start;
c277df42
IZ
3000}
3001
a3985cdc
DM
3002
3003/*
3004=for apidoc find_runcv
3005
3006Locate the CV corresponding to the currently executing sub or eval.
d819b83a
DM
3007If db_seqp is non_null, skip CVs that are in the DB package and populate
3008*db_seqp with the cop sequence number at the point that the DB:: code was
3009entered. (allows debuggers to eval in the scope of the breakpoint rather
cf525c36 3010than in the scope of the debugger itself).
a3985cdc
DM
3011
3012=cut
3013*/
3014
3015CV*
d819b83a 3016Perl_find_runcv(pTHX_ U32 *db_seqp)
a3985cdc 3017{
97aff369 3018 dVAR;
a3985cdc 3019 PERL_SI *si;
a3985cdc 3020
d819b83a
DM
3021 if (db_seqp)
3022 *db_seqp = PL_curcop->cop_seq;
a3985cdc 3023 for (si = PL_curstackinfo; si; si = si->si_prev) {
06b5626a 3024 I32 ix;
a3985cdc 3025 for (ix = si->si_cxix; ix >= 0; ix--) {
06b5626a 3026 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
d819b83a 3027 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1b6737cc 3028 CV * const cv = cx->blk_sub.cv;
d819b83a
DM
3029 /* skip DB:: code */
3030 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3031 *db_seqp = cx->blk_oldcop->cop_seq;
3032 continue;
3033 }
3034 return cv;
3035 }
a3985cdc
DM
3036 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3037 return PL_compcv;
3038 }
3039 }
3040 return PL_main_cv;
3041}
3042
3043
3044/* Compile a require/do, an eval '', or a /(?{...})/.
3045 * In the last case, startop is non-null, and contains the address of
3046 * a pointer that should be set to the just-compiled code.
3047 * outside is the lexically enclosing CV (if any) that invoked us.
410be5db
DM
3048 * Returns a bool indicating whether the compile was successful; if so,
3049 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3050 * pushes undef (also croaks if startop != NULL).
a3985cdc
DM
3051 */
3052
410be5db 3053STATIC bool
a3985cdc 3054S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
a0d0e21e 3055{
27da23d5 3056 dVAR; dSP;
46c461b5 3057 OP * const saveop = PL_op;
a0d0e21e 3058
6dc8a9e4
IZ
3059 PL_in_eval = ((saveop && saveop->op_type == OP_REQUIRE)
3060 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3061 : EVAL_INEVAL);
a0d0e21e 3062
1ce6579f
PP
3063 PUSHMARK(SP);
3064
3280af22 3065 SAVESPTR(PL_compcv);
ea726b52 3066 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
1aff0e91 3067 CvEVAL_on(PL_compcv);
2090ab20
JH
3068 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3069 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3070
a3985cdc 3071 CvOUTSIDE_SEQ(PL_compcv) = seq;
ea726b52 3072 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
a3985cdc 3073
dd2155a4 3074 /* set up a scratch pad */
a0d0e21e 3075
dd2155a4 3076 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
cecbe010 3077 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
2c05e328 3078
07055b4c 3079
81d86705
NC
3080 if (!PL_madskills)
3081 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
748a9306 3082
a0d0e21e
LW
3083 /* make sure we compile in the right package */
3084
ed094faf 3085 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3280af22 3086 SAVESPTR(PL_curstash);
ed094faf 3087 PL_curstash = CopSTASH(PL_curcop);
a0d0e21e 3088 }
3c10abe3 3089 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3280af22
NIS
3090 SAVESPTR(PL_beginav);
3091 PL_beginav = newAV();
3092 SAVEFREESV(PL_beginav);
3c10abe3
AG
3093 SAVESPTR(PL_unitcheckav);
3094 PL_unitcheckav = newAV();
3095 SAVEFREESV(PL_unitcheckav);
a0d0e21e 3096
81d86705 3097#ifdef PERL_MAD
9da243ce 3098 SAVEBOOL(PL_madskills);
81d86705
NC
3099 PL_madskills = 0;
3100#endif
3101
a0d0e21e
LW
3102 /* try to compile it */
3103
5f66b61c 3104 PL_eval_root = NULL;
3280af22 3105 PL_curcop = &PL_compiling;
fc15ae8f 3106 CopARYBASE_set(PL_curcop, 0);
5f66b61c 3107 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
faef0170 3108 PL_in_eval |= EVAL_KEEPERR;
ab69dbc2
RGS
3109 else
3110 CLEAR_ERRSV();
13765c85 3111 if (yyparse() || PL_parser->error_count || !PL_eval_root) {
0c58d367 3112 SV **newsp; /* Used by POPBLOCK. */
9d4ba2ae 3113 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
c277df42 3114 I32 optype = 0; /* Might be reset by POPEVAL. */
9d4ba2ae 3115 const char *msg;
bfed75c6 3116
533c011a 3117 PL_op = saveop;
3280af22
NIS
3118 if (PL_eval_root) {
3119 op_free(PL_eval_root);
5f66b61c 3120 PL_eval_root = NULL;
a0d0e21e 3121 }
3280af22 3122 SP = PL_stack_base + POPMARK; /* pop original mark */
c277df42 3123 if (!startop) {
3280af22 3124 POPBLOCK(cx,PL_curpm);
c277df42 3125 POPEVAL(cx);
c277df42 3126 }
a0d0e21e 3127 lex_end();
d343c3ef 3128 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
9d4ba2ae
AL
3129
3130 msg = SvPVx_nolen_const(ERRSV);
7a2e2cd6 3131 if (optype == OP_REQUIRE) {
b464bac0 3132 const SV * const nsv = cx->blk_eval.old_namesv;
504618e9 3133 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
27bcc0a7 3134 &PL_sv_undef, 0);
58d3fd3b
SH
3135 Perl_croak(aTHX_ "%sCompilation failed in require",
3136 *msg ? msg : "Unknown error\n");
5a844595
GS
3137 }
3138 else if (startop) {
3280af22 3139 POPBLOCK(cx,PL_curpm);
c277df42 3140 POPEVAL(cx);
5a844595
GS
3141 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3142 (*msg ? msg : "Unknown error\n"));
7a2e2cd6 3143 }
9d7f88dd 3144 else {
9d7f88dd 3145 if (!*msg) {
6502358f 3146 sv_setpvs(ERRSV, "Compilation error");
9d7f88dd
SR
3147 }
3148 }
9d4ba2ae 3149 PERL_UNUSED_VAR(newsp);
410be5db
DM
3150 PUSHs(&PL_sv_undef);
3151 PUTBACK;
3152 return FALSE;
a0d0e21e 3153 }
57843af0 3154 CopLINE_set(&PL_compiling, 0);
c277df42 3155 if (startop) {
3280af22 3156 *startop = PL_eval_root;
c277df42 3157 } else
3280af22 3158 SAVEFREEOP(PL_eval_root);
0c58d367
RGS
3159
3160 /* Set the context for this new optree.
021f53de
GG
3161 * Propagate the context from the eval(). */
3162 if ((gimme & G_WANT) == G_VOID)
3280af22 3163 scalarvoid(PL_eval_root);
7df0357e 3164 else if ((gimme & G_WANT) == G_ARRAY)
3280af22 3165 list(PL_eval_root);
a0d0e21e 3166 else
3280af22 3167 scalar(PL_eval_root);
a0d0e21e
LW
3168
3169 DEBUG_x(dump_eval());
3170
55497cff 3171 /* Register with debugger: */
6482a30d 3172 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
b96d8cd9 3173 CV * const cv = get_cvs("DB::postponed", 0);
55497cff
PP
3174 if (cv) {
3175 dSP;
924508f0 3176 PUSHMARK(SP);
ad64d0ec 3177 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
55497cff 3178 PUTBACK;
ad64d0ec 3179 call_sv(MUTABLE_SV(cv), G_DISCARD);
55497cff
PP
3180 }
3181 }
3182
3c10abe3
AG
3183 if (PL_unitcheckav)
3184 call_list(PL_scopestack_ix, PL_unitcheckav);
3185
a0d0e21e
LW
3186 /* compiled okay, so do it */
3187
3280af22
NIS
3188 CvDEPTH(PL_compcv) = 1;
3189 SP = PL_stack_base + POPMARK; /* pop original mark */
533c011a 3190 PL_op = saveop; /* The caller may need it. */
bc177e6b 3191 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
5dc0d613 3192
410be5db
DM
3193 PUTBACK;
3194 return TRUE;
a0d0e21e
LW
3195}
3196
a6c40364 3197STATIC PerlIO *
0786552a 3198S_check_type_and_open(pTHX_ const char *name)
ce8abf5f
SP
3199{
3200 Stat_t st;
c445ea15 3201 const int st_rc = PerlLIO_stat(name, &st);
df528165 3202
7918f24d
NC
3203 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3204
6b845e56 3205 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
4608196e 3206 return NULL;
ce8abf5f
SP
3207 }
3208
0786552a 3209 return PerlIO_open(name, PERL_SCRIPT_MODE);
ce8abf5f
SP
3210}
3211
75c20bac 3212#ifndef PERL_DISABLE_PMC
ce8abf5f 3213STATIC PerlIO *
0786552a 3214S_doopen_pm(pTHX_ const char *name, const STRLEN namelen)
b295d113 3215{
b295d113
TH
3216 PerlIO *fp;
3217
7918f24d
NC
3218 PERL_ARGS_ASSERT_DOOPEN_PM;
3219
ce9440c8 3220 if (namelen > 3 && memEQs(name + namelen - 3, 3, ".pm")) {
50b8ed39
NC
3221 SV *const pmcsv = newSV(namelen + 2);
3222 char *const pmc = SvPVX(pmcsv);
a6c40364 3223 Stat_t pmcstat;
50b8ed39
NC
3224
3225 memcpy(pmc, name, namelen);
3226 pmc[namelen] = 'c';
3227 pmc[namelen + 1] = '\0';
3228
a6c40364 3229 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
0786552a 3230 fp = check_type_and_open(name);
a6c40364
GS
3231 }
3232 else {
0786552a 3233 fp = check_type_and_open(pmc);
b295d113 3234 }
a6c40364
GS
3235 SvREFCNT_dec(pmcsv);
3236 }
3237 else {
0786552a 3238 fp = check_type_and_open(name);
b295d113 3239 }
b295d113 3240 return fp;
75c20bac 3241}
7925835c 3242#else
75c20bac 3243# define doopen_pm(name, namelen) check_type_and_open(name)
7925835c 3244#endif /* !PERL_DISABLE_PMC */
b295d113 3245
a0d0e21e
LW
3246PP(pp_require)
3247{
27da23d5 3248 dVAR; dSP;
c09156bb 3249 register PERL_CONTEXT *cx;
a0d0e21e 3250 SV *sv;
5c144d81 3251 const char *name;
6132ea6c 3252 STRLEN len;
4492be7a
JM
3253 char * unixname;
3254 STRLEN unixlen;
62f5ad7a 3255#ifdef VMS
4492be7a 3256 int vms_unixname = 0;
62f5ad7a 3257#endif
c445ea15
AL
3258 const char *tryname = NULL;
3259 SV *namesv = NULL;
f54cb97a 3260 const I32 gimme = GIMME_V;
bbed91b5 3261 int filter_has_file = 0;
c445ea15 3262 PerlIO *tryrsfp = NULL;
34113e50 3263 SV *filter_cache = NULL;
c445ea15
AL
3264 SV *filter_state = NULL;
3265 SV *filter_sub = NULL;
3266 SV *hook_sv = NULL;
6ec9efec
JH
3267 SV *encoding;
3268 OP *op;
a0d0e21e
LW
3269
3270 sv = POPs;
d7aa5382 3271 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
d7aa5382
JP
3272 sv = new_version(sv);
3273 if (!sv_derived_from(PL_patchlevel, "version"))
ac0e6a2f 3274 upg_version(PL_patchlevel, TRUE);
149c1637 3275 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3cacfbb9 3276 if ( vcmp(sv,PL_patchlevel) <= 0 )
468aa647 3277 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
be2597df 3278 SVfARG(vnormal(sv)), SVfARG(vnormal(PL_patchlevel)));
468aa647
RGS
3279 }
3280 else {
d1029faa
JP
3281 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3282 I32 first = 0;
3283 AV *lav;
3284 SV * const req = SvRV(sv);
85fbaab2 3285 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
d1029faa
JP
3286
3287 /* get the left hand term */
502c6561 3288 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
d1029faa
JP
3289
3290 first = SvIV(*av_fetch(lav,0,0));
3291 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
85fbaab2 3292 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
d1029faa
JP
3293 || av_len(lav) > 1 /* FP with > 3 digits */
3294 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3295 ) {
3296 DIE(aTHX_ "Perl %"SVf" required--this is only "
3297 "%"SVf", stopped", SVfARG(vnormal(req)),
3298 SVfARG(vnormal(PL_patchlevel)));
3299 }
3300 else { /* probably 'use 5.10' or 'use 5.8' */
af61dbfd 3301 SV *hintsv;
d1029faa
JP
3302 I32 second = 0;
3303
3304 if (av_len(lav)>=1)
3305 second = SvIV(*av_fetch(lav,1,0));
3306
3307 second /= second >= 600 ? 100 : 10;
af61dbfd
NC
3308 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3309 (int)first, (int)second);
d1029faa
JP
3310 upg_version(hintsv, TRUE);
3311
3312 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3313 "--this is only %"SVf", stopped",
3314 SVfARG(vnormal(req)),
af61dbfd 3315 SVfARG(vnormal(sv_2mortal(hintsv))),
d1029faa
JP
3316 SVfARG(vnormal(PL_patchlevel)));
3317 }
3318 }
468aa647 3319 }
d7aa5382 3320
fbc891ce
RB
3321 /* We do this only with use, not require. */
3322 if (PL_compcv &&
fbc891ce
RB
3323 /* If we request a version >= 5.9.5, load feature.pm with the
3324 * feature bundle that corresponds to the required version. */
2e8342de 3325 vcmp(sv, sv_2mortal(upg_version(newSVnv(5.009005), FALSE))) >= 0) {
7dfde25d
RGS
3326 SV *const importsv = vnormal(sv);
3327 *SvPVX_mutable(importsv) = ':';
d343c3ef 3328 ENTER_with_name("load_feature");
7dfde25d 3329 Perl_load_module(aTHX_ 0, newSVpvs("feature"), NULL, importsv, NULL);
d343c3ef 3330 LEAVE_with_name("load_feature");
7dfde25d 3331 }
53eb19dd
SM
3332 /* If a version >= 5.11.0 is requested, strictures are on by default! */
3333 if (PL_compcv &&
3334 vcmp(sv, sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
5cc917d6 3335 PL_hints |= (HINT_STRICT_REFS | HINT_STRICT_SUBS | HINT_STRICT_VARS);
53eb19dd
SM
3336 }
3337
7dfde25d 3338 RETPUSHYES;
a0d0e21e 3339 }
5c144d81 3340 name = SvPV_const(sv, len);
6132ea6c 3341 if (!(name && len > 0 && *name))
cea2e8a9 3342 DIE(aTHX_ "Null filename used");
4633a7c4 3343 TAINT_PROPER("require");
4492be7a
JM
3344
3345
3346#ifdef VMS
3347 /* The key in the %ENV hash is in the syntax of file passed as the argument
3348 * usually this is in UNIX format, but sometimes in VMS format, which
3349 * can result in a module being pulled in more than once.
3350 * To prevent this, the key must be stored in UNIX format if the VMS
3351 * name can be translated to UNIX.
3352 */
3353 if ((unixname = tounixspec(name, NULL)) != NULL) {
3354 unixlen = strlen(unixname);
3355 vms_unixname = 1;
3356 }
3357 else
3358#endif
3359 {
3360 /* if not VMS or VMS name can not be translated to UNIX, pass it
3361 * through.
3362 */
3363 unixname = (char *) name;
3364 unixlen = len;
3365 }
44f8325f 3366 if (PL_op->op_type == OP_REQUIRE) {
4492be7a
JM
3367 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3368 unixname, unixlen, 0);
44f8325f
AL
3369 if ( svp ) {
3370 if (*svp != &PL_sv_undef)
3371 RETPUSHYES;
3372 else
087b5369
RD
3373 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3374 "Compilation failed in require", unixname);
44f8325f 3375 }