This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
PL_defoutgv isn't always a GV.
[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) ) {
cea2e8a9 1342 DEBUG_l(Perl_deb(aTHX_ "(Skipping label #%ld %s)\n",
7e8f1eac 1343 (long)i, cx_label));
a0d0e21e
LW
1344 continue;
1345 }
cea2e8a9 1346 DEBUG_l( Perl_deb(aTHX_ "(Found label #%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:
cea2e8a9 1415 DEBUG_l( Perl_deb(aTHX_ "(Found sub #%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:
cea2e8a9 1433 DEBUG_l( Perl_deb(aTHX_ "(Found eval #%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:
cea2e8a9 1462 DEBUG_l( Perl_deb(aTHX_ "(Found loop #%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:
1480 DEBUG_l( Perl_deb(aTHX_ "(Found given #%ld)\n", (long)i));
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
RH
1488 if (CxFOREACHDEF(cx)) {
1489 DEBUG_l( Perl_deb(aTHX_ "(Found foreach #%ld)\n", (long)i));
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:
1508 DEBUG_l( Perl_deb(aTHX_ "(Found when #%ld)\n", (long)i));
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];
c90c0ff4 1524 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
22c35a8c 1525 (long) cxstack_ix, PL_block_type[CxTYPE(cx)]));
a0d0e21e 1526 /* Note: we don't need to restore the base context info till the end. */
6b35e009 1527 switch (CxTYPE(cx)) {
c90c0ff4
PP
1528 case CXt_SUBST:
1529 POPSUBST(cx);
1530 continue; /* not break */
a0d0e21e 1531 case CXt_SUB:
b0d9ce38
GS
1532 POPSUB(cx,sv);
1533 LEAVESUB(sv);
a0d0e21e
LW
1534 break;
1535 case CXt_EVAL:
1536 POPEVAL(cx);
1537 break;
c6fdafd0 1538 case CXt_LOOP_LAZYIV:
d01136d6 1539 case CXt_LOOP_LAZYSV:
3b719c58
NC
1540 case CXt_LOOP_FOR:
1541 case CXt_LOOP_PLAIN:
a0d0e21e
LW
1542 POPLOOP(cx);
1543 break;
0a753a76 1544 case CXt_NULL:
a0d0e21e 1545 break;
7766f137
GS
1546 case CXt_FORMAT:
1547 POPFORMAT(cx);
1548 break;
a0d0e21e 1549 }
c90c0ff4 1550 cxstack_ix--;
a0d0e21e 1551 }
1b6737cc 1552 PERL_UNUSED_VAR(optype);
a0d0e21e
LW
1553}
1554
5a844595
GS
1555void
1556Perl_qerror(pTHX_ SV *err)
1557{
97aff369 1558 dVAR;
7918f24d
NC
1559
1560 PERL_ARGS_ASSERT_QERROR;
1561
5a844595
GS
1562 if (PL_in_eval)
1563 sv_catsv(ERRSV, err);
1564 else if (PL_errors)
1565 sv_catsv(PL_errors, err);
1566 else
be2597df 1567 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
13765c85
DM
1568 if (PL_parser)
1569 ++PL_parser->error_count;
5a844595
GS
1570}
1571
bb4c52e0 1572void
7d0994e0 1573Perl_die_where(pTHX_ SV *msv)
a0d0e21e 1574{
27da23d5 1575 dVAR;
87582a92 1576
3280af22 1577 if (PL_in_eval) {
a0d0e21e 1578 I32 cxix;
a0d0e21e 1579 I32 gimme;
a0d0e21e 1580
7d0994e0 1581 if (msv) {
faef0170 1582 if (PL_in_eval & EVAL_KEEPERR) {
bfed75c6 1583 static const char prefix[] = "\t(in cleanup) ";
2d03de9c 1584 SV * const err = ERRSV;
c445ea15 1585 const char *e = NULL;
98eae8f5 1586 if (!SvPOK(err))
76f68e9b 1587 sv_setpvs(err,"");
7d0994e0 1588 else if (SvCUR(err) >= sizeof(prefix)+SvCUR(msv)-1) {
0510663f 1589 STRLEN len;
7d0994e0
GG
1590 STRLEN msglen;
1591 const char* message = SvPV_const(msv, msglen);
349d4f2f 1592 e = SvPV_const(err, len);
0510663f 1593 e += len - msglen;
98eae8f5 1594 if (*e != *message || strNE(e,message))
c445ea15 1595 e = NULL;
98eae8f5
GS
1596 }
1597 if (!e) {
a2a5de95 1598 STRLEN start;
7d0994e0 1599 SvGROW(err, SvCUR(err)+sizeof(prefix)+SvCUR(msv));
98eae8f5 1600 sv_catpvn(err, prefix, sizeof(prefix)-1);
7d0994e0
GG
1601 sv_catsv(err, msv);
1602 start = SvCUR(err)-SvCUR(msv)-sizeof(prefix)+1;
a2a5de95
NC
1603 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "%s",
1604 SvPVX_const(err)+start);
4633a7c4 1605 }
4633a7c4 1606 }
1aa99e6b 1607 else {
7d0994e0
GG
1608 STRLEN msglen;
1609 const char* message = SvPV_const(msv, msglen);
06bf62c7 1610 sv_setpvn(ERRSV, message, msglen);
7d0994e0 1611 SvFLAGS(ERRSV) |= SvFLAGS(msv) & SVf_UTF8;
1aa99e6b 1612 }
4633a7c4 1613 }
4e6ea2c3 1614
5a844595
GS
1615 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1616 && PL_curstackinfo->si_prev)
1617 {
bac4b2ad 1618 dounwind(-1);
d3acc0f7 1619 POPSTACK;
bac4b2ad 1620 }
e336de0d 1621
a0d0e21e
LW
1622 if (cxix >= 0) {
1623 I32 optype;
35a4481c 1624 register PERL_CONTEXT *cx;
901017d6 1625 SV **newsp;
a0d0e21e
LW
1626
1627 if (cxix < cxstack_ix)
1628 dounwind(cxix);
1629
3280af22 1630 POPBLOCK(cx,PL_curpm);
6b35e009 1631 if (CxTYPE(cx) != CXt_EVAL) {
7d0994e0
GG
1632 STRLEN msglen;
1633 const char* message = SvPVx_const( msv ? msv : ERRSV, msglen);
10edeb5d 1634 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
bf49b057 1635 PerlIO_write(Perl_error_log, message, msglen);
a0d0e21e
LW
1636 my_exit(1);
1637 }
1638 POPEVAL(cx);
1639
1640 if (gimme == G_SCALAR)
3280af22
NIS
1641 *++newsp = &PL_sv_undef;
1642 PL_stack_sp = newsp;
a0d0e21e
LW
1643
1644 LEAVE;
748a9306 1645
7fb6a879
GS
1646 /* LEAVE could clobber PL_curcop (see save_re_context())
1647 * XXX it might be better to find a way to avoid messing with
1648 * PL_curcop in save_re_context() instead, but this is a more
1649 * minimal fix --GSAR */
1650 PL_curcop = cx->blk_oldcop;
1651
7a2e2cd6 1652 if (optype == OP_REQUIRE) {
44f8325f 1653 const char* const msg = SvPVx_nolen_const(ERRSV);
901017d6 1654 SV * const nsv = cx->blk_eval.old_namesv;
b15aece3 1655 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
27bcc0a7 1656 &PL_sv_undef, 0);
5a844595
GS
1657 DIE(aTHX_ "%sCompilation failed in require",
1658 *msg ? msg : "Unknown error\n");
7a2e2cd6 1659 }
f39bc417 1660 assert(CxTYPE(cx) == CXt_EVAL);
bb4c52e0
GG
1661 PL_restartop = cx->blk_eval.retop;
1662 JMPENV_JUMP(3);
1663 /* NOTREACHED */
a0d0e21e
LW
1664 }
1665 }
87582a92 1666
7d0994e0 1667 write_to_stderr( msv ? msv : ERRSV );
f86702cc
PP
1668 my_failure_exit();
1669 /* NOTREACHED */
a0d0e21e
LW
1670}
1671
1672PP(pp_xor)
1673{
97aff369 1674 dVAR; dSP; dPOPTOPssrl;
a0d0e21e
LW
1675 if (SvTRUE(left) != SvTRUE(right))
1676 RETSETYES;
1677 else
1678 RETSETNO;
1679}
1680
a0d0e21e
LW
1681PP(pp_caller)
1682{
97aff369 1683 dVAR;
39644a26 1684 dSP;
a0d0e21e 1685 register I32 cxix = dopoptosub(cxstack_ix);
901017d6
AL
1686 register const PERL_CONTEXT *cx;
1687 register const PERL_CONTEXT *ccstack = cxstack;
1688 const PERL_SI *top_si = PL_curstackinfo;
54310121 1689 I32 gimme;
06b5626a 1690 const char *stashname;
a0d0e21e
LW
1691 I32 count = 0;
1692
1693 if (MAXARG)
1694 count = POPi;
27d41816 1695
a0d0e21e 1696 for (;;) {
2c375eb9
GS
1697 /* we may be in a higher stacklevel, so dig down deeper */
1698 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1699 top_si = top_si->si_prev;
1700 ccstack = top_si->si_cxstack;
1701 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1702 }
a0d0e21e 1703 if (cxix < 0) {
27d41816
DM
1704 if (GIMME != G_ARRAY) {
1705 EXTEND(SP, 1);
a0d0e21e 1706 RETPUSHUNDEF;
27d41816 1707 }
a0d0e21e
LW
1708 RETURN;
1709 }
f2a7f298 1710 /* caller() should not report the automatic calls to &DB::sub */
1711 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
3280af22 1712 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
a0d0e21e
LW
1713 count++;
1714 if (!count--)
1715 break;
2c375eb9 1716 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1717 }
2c375eb9
GS
1718
1719 cx = &ccstack[cxix];
7766f137 1720 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
f54cb97a 1721 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
2c375eb9 1722 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
06a5b730 1723 field below is defined for any cx. */
f2a7f298 1724 /* caller() should not report the automatic calls to &DB::sub */
1725 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
2c375eb9 1726 cx = &ccstack[dbcxix];
06a5b730
PP
1727 }
1728
ed094faf 1729 stashname = CopSTASHPV(cx->blk_oldcop);
a0d0e21e 1730 if (GIMME != G_ARRAY) {
27d41816 1731 EXTEND(SP, 1);
ed094faf 1732 if (!stashname)
3280af22 1733 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1734 else {
1735 dTARGET;
ed094faf 1736 sv_setpv(TARG, stashname);
49d8d3a1
MB
1737 PUSHs(TARG);
1738 }
a0d0e21e
LW
1739 RETURN;
1740 }
a0d0e21e 1741
b3ca2e83 1742 EXTEND(SP, 11);
27d41816 1743
ed094faf 1744 if (!stashname)
3280af22 1745 PUSHs(&PL_sv_undef);
49d8d3a1 1746 else
6e449a3a
MHM
1747 mPUSHs(newSVpv(stashname, 0));
1748 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1749 mPUSHi((I32)CopLINE(cx->blk_oldcop));
a0d0e21e
LW
1750 if (!MAXARG)
1751 RETURN;
7766f137 1752 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
0bd48802 1753 GV * const cvgv = CvGV(ccstack[cxix].blk_sub.cv);
7766f137 1754 /* So is ccstack[dbcxix]. */
07b8c804 1755 if (isGV(cvgv)) {
561b68a9 1756 SV * const sv = newSV(0);
c445ea15 1757 gv_efullname3(sv, cvgv, NULL);
6e449a3a 1758 mPUSHs(sv);
bf38a478 1759 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804
RGS
1760 }
1761 else {
84bafc02 1762 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
bf38a478 1763 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804 1764 }
a0d0e21e
LW
1765 }
1766 else {
84bafc02 1767 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
6e449a3a 1768 mPUSHi(0);
a0d0e21e 1769 }
54310121
PP
1770 gimme = (I32)cx->blk_gimme;
1771 if (gimme == G_VOID)
3280af22 1772 PUSHs(&PL_sv_undef);
54310121 1773 else
98625aca 1774 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
6b35e009 1775 if (CxTYPE(cx) == CXt_EVAL) {
811a4de9 1776 /* eval STRING */
85a64632 1777 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
4633a7c4 1778 PUSHs(cx->blk_eval.cur_text);
3280af22 1779 PUSHs(&PL_sv_no);
0f79a09d 1780 }
811a4de9 1781 /* require */
0f79a09d 1782 else if (cx->blk_eval.old_namesv) {
6e449a3a 1783 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
3280af22 1784 PUSHs(&PL_sv_yes);
06a5b730 1785 }
811a4de9
GS
1786 /* eval BLOCK (try blocks have old_namesv == 0) */
1787 else {
1788 PUSHs(&PL_sv_undef);
1789 PUSHs(&PL_sv_undef);
1790 }
4633a7c4 1791 }
a682de96
GS
1792 else {
1793 PUSHs(&PL_sv_undef);
1794 PUSHs(&PL_sv_undef);
1795 }
bafb2adc 1796 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
ed094faf 1797 && CopSTASH_eq(PL_curcop, PL_debstash))
4633a7c4 1798 {
66a1b24b
AL
1799 AV * const ary = cx->blk_sub.argarray;
1800 const int off = AvARRAY(ary) - AvALLOC(ary);
a0d0e21e 1801
3280af22 1802 if (!PL_dbargs) {
af3885a0
NC
1803 PL_dbargs = GvAV(gv_AVadd(gv_fetchpvs("DB::args", GV_ADDMULTI,
1804 SVt_PVAV)));
3ddcf04c 1805 AvREAL_off(PL_dbargs); /* XXX should be REIFY (see av.h) */
a0d0e21e
LW
1806 }
1807
3280af22
NIS
1808 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1809 av_extend(PL_dbargs, AvFILLp(ary) + off);
1810 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1811 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e 1812 }
f3aa04c2
GS
1813 /* XXX only hints propagated via op_private are currently
1814 * visible (others are not easily accessible, since they
1815 * use the global PL_hints) */
6e449a3a 1816 mPUSHi(CopHINTS_get(cx->blk_oldcop));
e476b1b5
GS
1817 {
1818 SV * mask ;
72dc9ed5 1819 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
114bafba 1820
ac27b0f5 1821 if (old_warnings == pWARN_NONE ||
114bafba 1822 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
e476b1b5 1823 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
ac27b0f5 1824 else if (old_warnings == pWARN_ALL ||
75b6c4ca
RGS
1825 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1826 /* Get the bit mask for $warnings::Bits{all}, because
1827 * it could have been extended by warnings::register */
1828 SV **bits_all;
6673a63c 1829 HV * const bits = get_hv("warnings::Bits", 0);
017a3ce5 1830 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
1831 mask = newSVsv(*bits_all);
1832 }
1833 else {
1834 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1835 }
1836 }
e476b1b5 1837 else
72dc9ed5 1838 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
6e449a3a 1839 mPUSHs(mask);
e476b1b5 1840 }
b3ca2e83 1841
c28fe1ec 1842 PUSHs(cx->blk_oldcop->cop_hints_hash ?
b3ca2e83 1843 sv_2mortal(newRV_noinc(
ad64d0ec
NC
1844 MUTABLE_SV(Perl_refcounted_he_chain_2hv(aTHX_
1845 cx->blk_oldcop->cop_hints_hash))))
b3ca2e83 1846 : &PL_sv_undef);
a0d0e21e
LW
1847 RETURN;
1848}
1849
a0d0e21e
LW
1850PP(pp_reset)
1851{
97aff369 1852 dVAR;
39644a26 1853 dSP;
10edeb5d 1854 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
11faa288 1855 sv_reset(tmps, CopSTASH(PL_curcop));
3280af22 1856 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1857 RETURN;
1858}
1859
dd2155a4
DM
1860/* like pp_nextstate, but used instead when the debugger is active */
1861
a0d0e21e
LW
1862PP(pp_dbstate)
1863{
27da23d5 1864 dVAR;
533c011a 1865 PL_curcop = (COP*)PL_op;
a0d0e21e 1866 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 1867 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1868 FREETMPS;
1869
5df8de69
DM
1870 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1871 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
a0d0e21e 1872 {
39644a26 1873 dSP;
c09156bb 1874 register PERL_CONTEXT *cx;
f54cb97a 1875 const I32 gimme = G_ARRAY;
eb160463 1876 U8 hasargs;
0bd48802
AL
1877 GV * const gv = PL_DBgv;
1878 register CV * const cv = GvCV(gv);
a0d0e21e 1879
a0d0e21e 1880 if (!cv)
cea2e8a9 1881 DIE(aTHX_ "No DB::DB routine defined");
a0d0e21e 1882
aea4f609
DM
1883 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1884 /* don't do recursive DB::DB call */
a0d0e21e 1885 return NORMAL;
748a9306 1886
a57c6685 1887 ENTER;
4633a7c4
LW
1888 SAVETMPS;
1889
3280af22 1890 SAVEI32(PL_debug);
55497cff 1891 SAVESTACK_POS();
3280af22 1892 PL_debug = 0;
748a9306 1893 hasargs = 0;
924508f0 1894 SPAGAIN;
748a9306 1895
aed2304a 1896 if (CvISXSUB(cv)) {
c127bd3a
SF
1897 CvDEPTH(cv)++;
1898 PUSHMARK(SP);
1899 (void)(*CvXSUB(cv))(aTHX_ cv);
1900 CvDEPTH(cv)--;
1901 FREETMPS;
a57c6685 1902 LEAVE;
c127bd3a
SF
1903 return NORMAL;
1904 }
1905 else {
1906 PUSHBLOCK(cx, CXt_SUB, SP);
1907 PUSHSUB_DB(cx);
1908 cx->blk_sub.retop = PL_op->op_next;
1909 CvDEPTH(cv)++;
1910 SAVECOMPPAD();
1911 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1912 RETURNOP(CvSTART(cv));
1913 }
a0d0e21e
LW
1914 }
1915 else
1916 return NORMAL;
1917}
1918
a0d0e21e
LW
1919PP(pp_enteriter)
1920{
27da23d5 1921 dVAR; dSP; dMARK;
c09156bb 1922 register PERL_CONTEXT *cx;
f54cb97a 1923 const I32 gimme = GIMME_V;
a0d0e21e 1924 SV **svp;
840fe433 1925 U8 cxtype = CXt_LOOP_FOR;
7766f137 1926#ifdef USE_ITHREADS
e846cb92 1927 PAD *iterdata;
7766f137 1928#endif
a0d0e21e 1929
d343c3ef 1930 ENTER_with_name("loop1");
4633a7c4
LW
1931 SAVETMPS;
1932
533c011a 1933 if (PL_op->op_targ) {
14f338dc
DM
1934 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
1935 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
1936 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
1937 SVs_PADSTALE, SVs_PADSTALE);
1938 }
09edbca0 1939 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
c3564e5c 1940#ifndef USE_ITHREADS
dd2155a4 1941 svp = &PAD_SVl(PL_op->op_targ); /* "my" variable */
c3564e5c 1942#else
e846cb92 1943 iterdata = NULL;
7766f137 1944#endif
54b9620d
MB
1945 }
1946 else {
159b6efe 1947 GV * const gv = MUTABLE_GV(POPs);
7766f137 1948 svp = &GvSV(gv); /* symbol table variable */
0214ae40 1949 SAVEGENERICSV(*svp);
561b68a9 1950 *svp = newSV(0);
7766f137 1951#ifdef USE_ITHREADS
e846cb92 1952 iterdata = (PAD*)gv;
7766f137 1953#endif
54b9620d 1954 }
4633a7c4 1955
0d863452
RH
1956 if (PL_op->op_private & OPpITER_DEF)
1957 cxtype |= CXp_FOR_DEF;
1958
d343c3ef 1959 ENTER_with_name("loop2");
a0d0e21e 1960
7766f137
GS
1961 PUSHBLOCK(cx, cxtype, SP);
1962#ifdef USE_ITHREADS
e846cb92 1963 PUSHLOOP_FOR(cx, iterdata, MARK, PL_op->op_targ);
7766f137 1964#else
52d1f6fb 1965 PUSHLOOP_FOR(cx, svp, MARK, 0);
7766f137 1966#endif
533c011a 1967 if (PL_op->op_flags & OPf_STACKED) {
d01136d6
BS
1968 SV *maybe_ary = POPs;
1969 if (SvTYPE(maybe_ary) != SVt_PVAV) {
89ea2908 1970 dPOPss;
d01136d6 1971 SV * const right = maybe_ary;
984a4bea
RD
1972 SvGETMAGIC(sv);
1973 SvGETMAGIC(right);
4fe3f0fa 1974 if (RANGE_IS_NUMERIC(sv,right)) {
d01136d6 1975 cx->cx_type &= ~CXTYPEMASK;
c6fdafd0
NC
1976 cx->cx_type |= CXt_LOOP_LAZYIV;
1977 /* Make sure that no-one re-orders cop.h and breaks our
1978 assumptions */
1979 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
a2309040
JH
1980#ifdef NV_PRESERVES_UV
1981 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
1982 (SvNV(sv) > (NV)IV_MAX)))
1983 ||
1984 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
1985 (SvNV(right) < (NV)IV_MIN))))
1986#else
1987 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
1988 ||
1989 ((SvNV(sv) > 0) &&
1990 ((SvUV(sv) > (UV)IV_MAX) ||
1991 (SvNV(sv) > (NV)UV_MAX)))))
1992 ||
1993 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
1994 ||
1995 ((SvNV(right) > 0) &&
1996 ((SvUV(right) > (UV)IV_MAX) ||
1997 (SvNV(right) > (NV)UV_MAX))))))
1998#endif
076d9a11 1999 DIE(aTHX_ "Range iterator outside integer range");
d01136d6
BS
2000 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
2001 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
d4665a05
DM
2002#ifdef DEBUGGING
2003 /* for correct -Dstv display */
2004 cx->blk_oldsp = sp - PL_stack_base;
2005#endif
89ea2908 2006 }
3f63a782 2007 else {
d01136d6
BS
2008 cx->cx_type &= ~CXTYPEMASK;
2009 cx->cx_type |= CXt_LOOP_LAZYSV;
2010 /* Make sure that no-one re-orders cop.h and breaks our
2011 assumptions */
2012 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2013 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2014 cx->blk_loop.state_u.lazysv.end = right;
2015 SvREFCNT_inc(right);
2016 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
267cc4a8
NC
2017 /* This will do the upgrade to SVt_PV, and warn if the value
2018 is uninitialised. */
10516c54 2019 (void) SvPV_nolen_const(right);
267cc4a8
NC
2020 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2021 to replace !SvOK() with a pointer to "". */
2022 if (!SvOK(right)) {
2023 SvREFCNT_dec(right);
d01136d6 2024 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
267cc4a8 2025 }
3f63a782 2026 }
89ea2908 2027 }
d01136d6 2028 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
502c6561 2029 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
d01136d6
BS
2030 SvREFCNT_inc(maybe_ary);
2031 cx->blk_loop.state_u.ary.ix =
2032 (PL_op->op_private & OPpITER_REVERSED) ?
2033 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2034 -1;
ef3e5ea9 2035 }
89ea2908 2036 }
d01136d6
BS
2037 else { /* iterating over items on the stack */
2038 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
ef3e5ea9 2039 if (PL_op->op_private & OPpITER_REVERSED) {
d01136d6 2040 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
ef3e5ea9
NC
2041 }
2042 else {
d01136d6 2043 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
ef3e5ea9 2044 }
4633a7c4 2045 }
a0d0e21e
LW
2046
2047 RETURN;
2048}
2049
2050PP(pp_enterloop)
2051{
27da23d5 2052 dVAR; dSP;
c09156bb 2053 register PERL_CONTEXT *cx;
f54cb97a 2054 const I32 gimme = GIMME_V;
a0d0e21e 2055
d343c3ef 2056 ENTER_with_name("loop1");
a0d0e21e 2057 SAVETMPS;
d343c3ef 2058 ENTER_with_name("loop2");
a0d0e21e 2059
3b719c58
NC
2060 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2061 PUSHLOOP_PLAIN(cx, SP);
a0d0e21e
LW
2062
2063 RETURN;
2064}
2065
2066PP(pp_leaveloop)
2067{
27da23d5 2068 dVAR; dSP;
c09156bb 2069 register PERL_CONTEXT *cx;
a0d0e21e
LW
2070 I32 gimme;
2071 SV **newsp;
2072 PMOP *newpm;
2073 SV **mark;
2074
2075 POPBLOCK(cx,newpm);
3b719c58 2076 assert(CxTYPE_is_LOOP(cx));
4fdae800 2077 mark = newsp;
a8bba7fa 2078 newsp = PL_stack_base + cx->blk_loop.resetsp;
f86702cc 2079
a1f49e72 2080 TAINT_NOT;
54310121 2081 if (gimme == G_VOID)
6f207bd3 2082 NOOP;
54310121
PP
2083 else if (gimme == G_SCALAR) {
2084 if (mark < SP)
2085 *++newsp = sv_mortalcopy(*SP);
2086 else
3280af22 2087 *++newsp = &PL_sv_undef;
a0d0e21e
LW
2088 }
2089 else {
a1f49e72 2090 while (mark < SP) {
a0d0e21e 2091 *++newsp = sv_mortalcopy(*++mark);
a1f49e72
CS
2092 TAINT_NOT; /* Each item is independent */
2093 }
a0d0e21e 2094 }
f86702cc
PP
2095 SP = newsp;
2096 PUTBACK;
2097
a8bba7fa 2098 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
3280af22 2099 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2100
d343c3ef
GG
2101 LEAVE_with_name("loop2");
2102 LEAVE_with_name("loop1");
a0d0e21e 2103
f86702cc 2104 return NORMAL;
a0d0e21e
LW
2105}
2106
2107PP(pp_return)
2108{
27da23d5 2109 dVAR; dSP; dMARK;
c09156bb 2110 register PERL_CONTEXT *cx;
f86702cc 2111 bool popsub2 = FALSE;
b45de488 2112 bool clear_errsv = FALSE;
a0d0e21e
LW
2113 I32 gimme;
2114 SV **newsp;
2115 PMOP *newpm;
2116 I32 optype = 0;
b0d9ce38 2117 SV *sv;
b263a1ad 2118 OP *retop = NULL;
a0d0e21e 2119
0bd48802
AL
2120 const I32 cxix = dopoptosub(cxstack_ix);
2121
9850bf21
RH
2122 if (cxix < 0) {
2123 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2124 * sort block, which is a CXt_NULL
2125 * not a CXt_SUB */
2126 dounwind(0);
d7507f74
RH
2127 PL_stack_base[1] = *PL_stack_sp;
2128 PL_stack_sp = PL_stack_base + 1;
a0d0e21e
LW
2129 return 0;
2130 }
9850bf21
RH
2131 else
2132 DIE(aTHX_ "Can't return outside a subroutine");
a0d0e21e 2133 }
a0d0e21e
LW
2134 if (cxix < cxstack_ix)
2135 dounwind(cxix);
2136
d7507f74
RH
2137 if (CxMULTICALL(&cxstack[cxix])) {
2138 gimme = cxstack[cxix].blk_gimme;
2139 if (gimme == G_VOID)
2140 PL_stack_sp = PL_stack_base;
2141 else if (gimme == G_SCALAR) {
2142 PL_stack_base[1] = *PL_stack_sp;
2143 PL_stack_sp = PL_stack_base + 1;
2144 }
9850bf21 2145 return 0;
d7507f74 2146 }
9850bf21 2147
a0d0e21e 2148 POPBLOCK(cx,newpm);
6b35e009 2149 switch (CxTYPE(cx)) {
a0d0e21e 2150 case CXt_SUB:
f86702cc 2151 popsub2 = TRUE;
f39bc417 2152 retop = cx->blk_sub.retop;
5dd42e15 2153 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
a0d0e21e
LW
2154 break;
2155 case CXt_EVAL:
b45de488
GS
2156 if (!(PL_in_eval & EVAL_KEEPERR))
2157 clear_errsv = TRUE;
a0d0e21e 2158 POPEVAL(cx);
f39bc417 2159 retop = cx->blk_eval.retop;
1d76a5c3
GS
2160 if (CxTRYBLOCK(cx))
2161 break;
067f92a0 2162 lex_end();
748a9306
LW
2163 if (optype == OP_REQUIRE &&
2164 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2165 {
54310121 2166 /* Unassume the success we assumed earlier. */
901017d6 2167 SV * const nsv = cx->blk_eval.old_namesv;
b15aece3 2168 (void)hv_delete(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv), G_DISCARD);
be2597df 2169 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(nsv));
748a9306 2170 }
a0d0e21e 2171 break;
7766f137
GS
2172 case CXt_FORMAT:
2173 POPFORMAT(cx);
f39bc417 2174 retop = cx->blk_sub.retop;
7766f137 2175 break;
a0d0e21e 2176 default:
cea2e8a9 2177 DIE(aTHX_ "panic: return");
a0d0e21e
LW
2178 }
2179
a1f49e72 2180 TAINT_NOT;
a0d0e21e 2181 if (gimme == G_SCALAR) {
a29cdaf0
IZ
2182 if (MARK < SP) {
2183 if (popsub2) {
a8bba7fa 2184 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
2185 if (SvTEMP(TOPs)) {
2186 *++newsp = SvREFCNT_inc(*SP);
2187 FREETMPS;
2188 sv_2mortal(*newsp);
959e3673
GS
2189 }
2190 else {
2191 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
a29cdaf0 2192 FREETMPS;
959e3673
GS
2193 *++newsp = sv_mortalcopy(sv);
2194 SvREFCNT_dec(sv);
a29cdaf0 2195 }
959e3673
GS
2196 }
2197 else
a29cdaf0 2198 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
959e3673
GS
2199 }
2200 else
a29cdaf0 2201 *++newsp = sv_mortalcopy(*SP);
959e3673
GS
2202 }
2203 else
3280af22 2204 *++newsp = &PL_sv_undef;
a0d0e21e 2205 }
54310121 2206 else if (gimme == G_ARRAY) {
a1f49e72 2207 while (++MARK <= SP) {
f86702cc
PP
2208 *++newsp = (popsub2 && SvTEMP(*MARK))
2209 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2210 TAINT_NOT; /* Each item is independent */
2211 }
a0d0e21e 2212 }
3280af22 2213 PL_stack_sp = newsp;
a0d0e21e 2214
5dd42e15 2215 LEAVE;
f86702cc
PP
2216 /* Stack values are safe: */
2217 if (popsub2) {
5dd42e15 2218 cxstack_ix--;
b0d9ce38 2219 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2220 }
b0d9ce38 2221 else
c445ea15 2222 sv = NULL;
3280af22 2223 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2224
b0d9ce38 2225 LEAVESUB(sv);
8433848b 2226 if (clear_errsv) {
ab69dbc2 2227 CLEAR_ERRSV();
8433848b 2228 }
f39bc417 2229 return retop;
a0d0e21e
LW
2230}
2231
2232PP(pp_last)
2233{
27da23d5 2234 dVAR; dSP;
a0d0e21e 2235 I32 cxix;
c09156bb 2236 register PERL_CONTEXT *cx;
f86702cc 2237 I32 pop2 = 0;
a0d0e21e 2238 I32 gimme;
8772537c 2239 I32 optype;
b263a1ad 2240 OP *nextop = NULL;
a0d0e21e
LW
2241 SV **newsp;
2242 PMOP *newpm;
a8bba7fa 2243 SV **mark;
c445ea15 2244 SV *sv = NULL;
9d4ba2ae 2245
a0d0e21e 2246
533c011a 2247 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2248 cxix = dopoptoloop(cxstack_ix);
2249 if (cxix < 0)
a651a37d 2250 DIE(aTHX_ "Can't \"last\" outside a loop block");
a0d0e21e
LW
2251 }
2252 else {
2253 cxix = dopoptolabel(cPVOP->op_pv);
2254 if (cxix < 0)
cea2e8a9 2255 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
a0d0e21e
LW
2256 }
2257 if (cxix < cxstack_ix)
2258 dounwind(cxix);
2259
2260 POPBLOCK(cx,newpm);
5dd42e15 2261 cxstack_ix++; /* temporarily protect top context */
a8bba7fa 2262 mark = newsp;
6b35e009 2263 switch (CxTYPE(cx)) {
c6fdafd0 2264 case CXt_LOOP_LAZYIV:
d01136d6 2265 case CXt_LOOP_LAZYSV:
3b719c58
NC
2266 case CXt_LOOP_FOR:
2267 case CXt_LOOP_PLAIN:
2268 pop2 = CxTYPE(cx);
a8bba7fa 2269 newsp = PL_stack_base + cx->blk_loop.resetsp;
022eaa24 2270 nextop = cx->blk_loop.my_op->op_lastop->op_next;
a0d0e21e 2271 break;
f86702cc 2272 case CXt_SUB:
f86702cc 2273 pop2 = CXt_SUB;
f39bc417 2274 nextop = cx->blk_sub.retop;
a0d0e21e 2275 break;
f86702cc
PP
2276 case CXt_EVAL:
2277 POPEVAL(cx);
f39bc417 2278 nextop = cx->blk_eval.retop;
a0d0e21e 2279 break;
7766f137
GS
2280 case CXt_FORMAT:
2281 POPFORMAT(cx);
f39bc417 2282 nextop = cx->blk_sub.retop;
7766f137 2283 break;
a0d0e21e 2284 default:
cea2e8a9 2285 DIE(aTHX_ "panic: last");
a0d0e21e
LW
2286 }
2287
a1f49e72 2288 TAINT_NOT;
a0d0e21e 2289 if (gimme == G_SCALAR) {
f86702cc
PP
2290 if (MARK < SP)
2291 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2292 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 2293 else
3280af22 2294 *++newsp = &PL_sv_undef;
a0d0e21e 2295 }
54310121 2296 else if (gimme == G_ARRAY) {
a1f49e72 2297 while (++MARK <= SP) {
f86702cc
PP
2298 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2299 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2300 TAINT_NOT; /* Each item is independent */
2301 }
f86702cc
PP
2302 }
2303 SP = newsp;
2304 PUTBACK;
2305
5dd42e15
DM
2306 LEAVE;
2307 cxstack_ix--;
f86702cc
PP
2308 /* Stack values are safe: */
2309 switch (pop2) {
c6fdafd0 2310 case CXt_LOOP_LAZYIV:
3b719c58 2311 case CXt_LOOP_PLAIN:
d01136d6 2312 case CXt_LOOP_LAZYSV:
3b719c58 2313 case CXt_LOOP_FOR:
a8bba7fa 2314 POPLOOP(cx); /* release loop vars ... */
4fdae800 2315 LEAVE;
f86702cc
PP
2316 break;
2317 case CXt_SUB:
b0d9ce38 2318 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2319 break;
a0d0e21e 2320 }
3280af22 2321 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e 2322
b0d9ce38 2323 LEAVESUB(sv);
9d4ba2ae
AL
2324 PERL_UNUSED_VAR(optype);
2325 PERL_UNUSED_VAR(gimme);
f86702cc 2326 return nextop;
a0d0e21e
LW
2327}
2328
2329PP(pp_next)
2330{
27da23d5 2331 dVAR;
a0d0e21e 2332 I32 cxix;
c09156bb 2333 register PERL_CONTEXT *cx;
85538317 2334 I32 inner;
a0d0e21e 2335
533c011a 2336 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2337 cxix = dopoptoloop(cxstack_ix);
2338 if (cxix < 0)
a651a37d 2339 DIE(aTHX_ "Can't \"next\" outside a loop block");
a0d0e21e
LW
2340 }
2341 else {
2342 cxix = dopoptolabel(cPVOP->op_pv);
2343 if (cxix < 0)
cea2e8a9 2344 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
a0d0e21e
LW
2345 }
2346 if (cxix < cxstack_ix)
2347 dounwind(cxix);
2348
85538317
GS
2349 /* clear off anything above the scope we're re-entering, but
2350 * save the rest until after a possible continue block */
2351 inner = PL_scopestack_ix;
1ba6ee2b 2352 TOPBLOCK(cx);
85538317
GS
2353 if (PL_scopestack_ix < inner)
2354 leave_scope(PL_scopestack[PL_scopestack_ix]);
3a1b2b9e 2355 PL_curcop = cx->blk_oldcop;
022eaa24 2356 return CX_LOOP_NEXTOP_GET(cx);
a0d0e21e
LW
2357}
2358
2359PP(pp_redo)
2360{
27da23d5 2361 dVAR;
a0d0e21e 2362 I32 cxix;
c09156bb 2363 register PERL_CONTEXT *cx;
a0d0e21e 2364 I32 oldsave;
a034e688 2365 OP* redo_op;
a0d0e21e 2366
533c011a 2367 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2368 cxix = dopoptoloop(cxstack_ix);
2369 if (cxix < 0)
a651a37d 2370 DIE(aTHX_ "Can't \"redo\" outside a loop block");
a0d0e21e
LW
2371 }
2372 else {
2373 cxix = dopoptolabel(cPVOP->op_pv);
2374 if (cxix < 0)
cea2e8a9 2375 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
a0d0e21e
LW
2376 }
2377 if (cxix < cxstack_ix)
2378 dounwind(cxix);
2379
022eaa24 2380 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
a034e688
DM
2381 if (redo_op->op_type == OP_ENTER) {
2382 /* pop one less context to avoid $x being freed in while (my $x..) */
2383 cxstack_ix++;
2384 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2385 redo_op = redo_op->op_next;
2386 }
2387
a0d0e21e 2388 TOPBLOCK(cx);
3280af22 2389 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e 2390 LEAVE_SCOPE(oldsave);
936c78b5 2391 FREETMPS;
3a1b2b9e 2392 PL_curcop = cx->blk_oldcop;
a034e688 2393 return redo_op;
a0d0e21e
LW
2394}
2395
0824fdcb 2396STATIC OP *
bfed75c6 2397S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
a0d0e21e 2398{
97aff369 2399 dVAR;
a0d0e21e 2400 OP **ops = opstack;
bfed75c6 2401 static const char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 2402
7918f24d
NC
2403 PERL_ARGS_ASSERT_DOFINDLABEL;
2404
fc36a67e 2405 if (ops >= oplimit)
cea2e8a9 2406 Perl_croak(aTHX_ too_deep);
11343788
MB
2407 if (o->op_type == OP_LEAVE ||
2408 o->op_type == OP_SCOPE ||
2409 o->op_type == OP_LEAVELOOP ||
33d34e4c 2410 o->op_type == OP_LEAVESUB ||
11343788 2411 o->op_type == OP_LEAVETRY)
fc36a67e 2412 {
5dc0d613 2413 *ops++ = cUNOPo->op_first;
fc36a67e 2414 if (ops >= oplimit)
cea2e8a9 2415 Perl_croak(aTHX_ too_deep);
fc36a67e 2416 }
c4aa4e48 2417 *ops = 0;
11343788 2418 if (o->op_flags & OPf_KIDS) {
aec46f14 2419 OP *kid;
a0d0e21e 2420 /* First try all the kids at this level, since that's likeliest. */
11343788 2421 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
7e8f1eac
AD
2422 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2423 const char *kid_label = CopLABEL(kCOP);
2424 if (kid_label && strEQ(kid_label, label))
2425 return kid;
2426 }
a0d0e21e 2427 }
11343788 2428 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
3280af22 2429 if (kid == PL_lastgotoprobe)
a0d0e21e 2430 continue;
ed8d0fe2
SM
2431 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2432 if (ops == opstack)
2433 *ops++ = kid;
2434 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2435 ops[-1]->op_type == OP_DBSTATE)
2436 ops[-1] = kid;
2437 else
2438 *ops++ = kid;
2439 }
155aba94 2440 if ((o = dofindlabel(kid, label, ops, oplimit)))
11343788 2441 return o;
a0d0e21e
LW
2442 }
2443 }
c4aa4e48 2444 *ops = 0;
a0d0e21e
LW
2445 return 0;
2446}
2447
a0d0e21e
LW
2448PP(pp_goto)
2449{
27da23d5 2450 dVAR; dSP;
cbbf8932 2451 OP *retop = NULL;
a0d0e21e 2452 I32 ix;
c09156bb 2453 register PERL_CONTEXT *cx;
fc36a67e
PP
2454#define GOTO_DEPTH 64
2455 OP *enterops[GOTO_DEPTH];
cbbf8932 2456 const char *label = NULL;
bfed75c6
AL
2457 const bool do_dump = (PL_op->op_type == OP_DUMP);
2458 static const char must_have_label[] = "goto must have label";
a0d0e21e 2459
533c011a 2460 if (PL_op->op_flags & OPf_STACKED) {
9d4ba2ae 2461 SV * const sv = POPs;
a0d0e21e
LW
2462
2463 /* This egregious kludge implements goto &subroutine */
2464 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2465 I32 cxix;
c09156bb 2466 register PERL_CONTEXT *cx;
ea726b52 2467 CV *cv = MUTABLE_CV(SvRV(sv));
a0d0e21e
LW
2468 SV** mark;
2469 I32 items = 0;
2470 I32 oldsave;
b1464ded 2471 bool reified = 0;
a0d0e21e 2472
e8f7dd13 2473 retry:
4aa0a1f7 2474 if (!CvROOT(cv) && !CvXSUB(cv)) {
7fc63493 2475 const GV * const gv = CvGV(cv);
e8f7dd13 2476 if (gv) {
7fc63493 2477 GV *autogv;
e8f7dd13
GS
2478 SV *tmpstr;
2479 /* autoloaded stub? */
2480 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2481 goto retry;
2482 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2483 GvNAMELEN(gv), FALSE);
2484 if (autogv && (cv = GvCV(autogv)))
2485 goto retry;
2486 tmpstr = sv_newmortal();
c445ea15 2487 gv_efullname3(tmpstr, gv, NULL);
be2597df 2488 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
4aa0a1f7 2489 }
cea2e8a9 2490 DIE(aTHX_ "Goto undefined subroutine");
4aa0a1f7
AD
2491 }
2492
a0d0e21e 2493 /* First do some returnish stuff. */
b37c2d43 2494 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
71fc2216 2495 FREETMPS;
a0d0e21e
LW
2496 cxix = dopoptosub(cxstack_ix);
2497 if (cxix < 0)
cea2e8a9 2498 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
a0d0e21e
LW
2499 if (cxix < cxstack_ix)
2500 dounwind(cxix);
2501 TOPBLOCK(cx);
2d43a17f 2502 SPAGAIN;
564abe23 2503 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2d43a17f 2504 if (CxTYPE(cx) == CXt_EVAL) {
c74ace89
DM
2505 if (CxREALEVAL(cx))
2506 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2507 else
2508 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2d43a17f 2509 }
9850bf21
RH
2510 else if (CxMULTICALL(cx))
2511 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
bafb2adc 2512 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
d8b46c1b 2513 /* put @_ back onto stack */
a0d0e21e 2514 AV* av = cx->blk_sub.argarray;
bfed75c6 2515
93965878 2516 items = AvFILLp(av) + 1;
a45cdc79
DM
2517 EXTEND(SP, items+1); /* @_ could have been extended. */
2518 Copy(AvARRAY(av), SP + 1, items, SV*);
3280af22
NIS
2519 SvREFCNT_dec(GvAV(PL_defgv));
2520 GvAV(PL_defgv) = cx->blk_sub.savearray;
b1464ded 2521 CLEAR_ARGARRAY(av);
d8b46c1b 2522 /* abandon @_ if it got reified */
62b1ebc2 2523 if (AvREAL(av)) {
b1464ded
DM
2524 reified = 1;
2525 SvREFCNT_dec(av);
d8b46c1b
GS
2526 av = newAV();
2527 av_extend(av, items-1);
11ca45c0 2528 AvREIFY_only(av);
ad64d0ec 2529 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
62b1ebc2 2530 }
a0d0e21e 2531 }
aed2304a 2532 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
890ce7af 2533 AV* const av = GvAV(PL_defgv);
1fa4e549 2534 items = AvFILLp(av) + 1;
a45cdc79
DM
2535 EXTEND(SP, items+1); /* @_ could have been extended. */
2536 Copy(AvARRAY(av), SP + 1, items, SV*);
1fa4e549 2537 }
a45cdc79
DM
2538 mark = SP;
2539 SP += items;
6b35e009 2540 if (CxTYPE(cx) == CXt_SUB &&
b150fb22 2541 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
a0d0e21e 2542 SvREFCNT_dec(cx->blk_sub.cv);
3280af22 2543 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
2544 LEAVE_SCOPE(oldsave);
2545
2546 /* Now do some callish stuff. */
2547 SAVETMPS;
5023d17a 2548 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
aed2304a 2549 if (CvISXSUB(cv)) {
b37c2d43 2550 OP* const retop = cx->blk_sub.retop;
f73ef291
NC
2551 SV **newsp;
2552 I32 gimme;
b1464ded
DM
2553 if (reified) {
2554 I32 index;
2555 for (index=0; index<items; index++)
2556 sv_2mortal(SP[-index]);
2557 }
1fa4e549 2558
b37c2d43
AL
2559 /* XS subs don't have a CxSUB, so pop it */
2560 POPBLOCK(cx, PL_curpm);
2561 /* Push a mark for the start of arglist */
2562 PUSHMARK(mark);
2563 PUTBACK;
2564 (void)(*CvXSUB(cv))(aTHX_ cv);
a57c6685 2565 LEAVE;
5eff7df7 2566 return retop;
a0d0e21e
LW
2567 }
2568 else {
b37c2d43 2569 AV* const padlist = CvPADLIST(cv);
6b35e009 2570 if (CxTYPE(cx) == CXt_EVAL) {
85a64632 2571 PL_in_eval = CxOLD_IN_EVAL(cx);
3280af22 2572 PL_eval_root = cx->blk_eval.old_eval_root;
b150fb22 2573 cx->cx_type = CXt_SUB;
b150fb22 2574 }
a0d0e21e 2575 cx->blk_sub.cv = cv;
1a5b3db4 2576 cx->blk_sub.olddepth = CvDEPTH(cv);
dd2155a4 2577
a0d0e21e
LW
2578 CvDEPTH(cv)++;
2579 if (CvDEPTH(cv) < 2)
74c765eb 2580 SvREFCNT_inc_simple_void_NN(cv);
dd2155a4 2581 else {
2b9dff67 2582 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
44a8e56a 2583 sub_crush_depth(cv);
26019298 2584 pad_push(padlist, CvDEPTH(cv));
a0d0e21e 2585 }
fd617465
DM
2586 SAVECOMPPAD();
2587 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
bafb2adc 2588 if (CxHASARGS(cx))
6d4ff0d2 2589 {
502c6561 2590 AV *const av = MUTABLE_AV(PAD_SVl(0));
a0d0e21e 2591
3280af22 2592 cx->blk_sub.savearray = GvAV(PL_defgv);
502c6561 2593 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
dd2155a4 2594 CX_CURPAD_SAVE(cx->blk_sub);
6d4ff0d2 2595 cx->blk_sub.argarray = av;
a0d0e21e
LW
2596
2597 if (items >= AvMAX(av) + 1) {
b37c2d43 2598 SV **ary = AvALLOC(av);
a0d0e21e
LW
2599 if (AvARRAY(av) != ary) {
2600 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
9c6bc640 2601 AvARRAY(av) = ary;
a0d0e21e
LW
2602 }
2603 if (items >= AvMAX(av) + 1) {
2604 AvMAX(av) = items - 1;
2605 Renew(ary,items+1,SV*);
2606 AvALLOC(av) = ary;
9c6bc640 2607 AvARRAY(av) = ary;
a0d0e21e
LW
2608 }
2609 }
a45cdc79 2610 ++mark;
a0d0e21e 2611 Copy(mark,AvARRAY(av),items,SV*);
93965878 2612 AvFILLp(av) = items - 1;
d8b46c1b 2613 assert(!AvREAL(av));
b1464ded
DM
2614 if (reified) {
2615 /* transfer 'ownership' of refcnts to new @_ */
2616 AvREAL_on(av);
2617 AvREIFY_off(av);
2618 }
a0d0e21e
LW
2619 while (items--) {
2620 if (*mark)
2621 SvTEMP_off(*mark);
2622 mark++;
2623 }
2624 }
491527d0 2625 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
005a8a35 2626 Perl_get_db_sub(aTHX_ NULL, cv);
b37c2d43 2627 if (PERLDB_GOTO) {
b96d8cd9 2628 CV * const gotocv = get_cvs("DB::goto", 0);
b37c2d43
AL
2629 if (gotocv) {
2630 PUSHMARK( PL_stack_sp );
ad64d0ec 2631 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
b37c2d43
AL
2632 PL_stack_sp--;
2633 }
491527d0 2634 }
1ce6579f 2635 }
a0d0e21e
LW
2636 RETURNOP(CvSTART(cv));
2637 }
2638 }
1614b0e3 2639 else {
0510663f 2640 label = SvPV_nolen_const(sv);
1614b0e3 2641 if (!(do_dump || *label))
cea2e8a9 2642 DIE(aTHX_ must_have_label);
1614b0e3 2643 }
a0d0e21e 2644 }
533c011a 2645 else if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 2646 if (! do_dump)
cea2e8a9 2647 DIE(aTHX_ must_have_label);
a0d0e21e
LW
2648 }
2649 else
2650 label = cPVOP->op_pv;
2651
2652 if (label && *label) {
cbbf8932 2653 OP *gotoprobe = NULL;
3b2447bc 2654 bool leaving_eval = FALSE;
33d34e4c 2655 bool in_block = FALSE;
cbbf8932 2656 PERL_CONTEXT *last_eval_cx = NULL;
a0d0e21e
LW
2657
2658 /* find label */
2659
d4c19fe8 2660 PL_lastgotoprobe = NULL;
a0d0e21e
LW
2661 *enterops = 0;
2662 for (ix = cxstack_ix; ix >= 0; ix--) {
2663 cx = &cxstack[ix];
6b35e009 2664 switch (CxTYPE(cx)) {
a0d0e21e 2665 case CXt_EVAL:
3b2447bc 2666 leaving_eval = TRUE;
971ecbe6 2667 if (!CxTRYBLOCK(cx)) {
a4f3a277
RH
2668 gotoprobe = (last_eval_cx ?
2669 last_eval_cx->blk_eval.old_eval_root :
2670 PL_eval_root);
2671 last_eval_cx = cx;
9c5794fe
RH
2672 break;
2673 }
2674 /* else fall through */
c6fdafd0 2675 case CXt_LOOP_LAZYIV:
d01136d6 2676 case CXt_LOOP_LAZYSV:
3b719c58
NC
2677 case CXt_LOOP_FOR:
2678 case CXt_LOOP_PLAIN:
bb5aedc1
VP
2679 case CXt_GIVEN:
2680 case CXt_WHEN:
a0d0e21e
LW
2681 gotoprobe = cx->blk_oldcop->op_sibling;
2682 break;
2683 case CXt_SUBST:
2684 continue;
2685 case CXt_BLOCK:
33d34e4c 2686 if (ix) {
a0d0e21e 2687 gotoprobe = cx->blk_oldcop->op_sibling;
33d34e4c
AE
2688 in_block = TRUE;
2689 } else
3280af22 2690 gotoprobe = PL_main_root;
a0d0e21e 2691 break;
b3933176 2692 case CXt_SUB:
9850bf21 2693 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
b3933176
CS
2694 gotoprobe = CvROOT(cx->blk_sub.cv);
2695 break;
2696 }
2697 /* FALL THROUGH */
7766f137 2698 case CXt_FORMAT:
0a753a76 2699 case CXt_NULL:
a651a37d 2700 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
a0d0e21e
LW
2701 default:
2702 if (ix)
cea2e8a9 2703 DIE(aTHX_ "panic: goto");
3280af22 2704 gotoprobe = PL_main_root;
a0d0e21e
LW
2705 break;
2706 }
2b597662
GS
2707 if (gotoprobe) {
2708 retop = dofindlabel(gotoprobe, label,
2709 enterops, enterops + GOTO_DEPTH);
2710 if (retop)
2711 break;
2712 }
3280af22 2713 PL_lastgotoprobe = gotoprobe;
a0d0e21e
LW
2714 }
2715 if (!retop)
cea2e8a9 2716 DIE(aTHX_ "Can't find label %s", label);
a0d0e21e 2717
3b2447bc
RH
2718 /* if we're leaving an eval, check before we pop any frames
2719 that we're not going to punt, otherwise the error
2720 won't be caught */
2721
2722 if (leaving_eval && *enterops && enterops[1]) {
2723 I32 i;
2724 for (i = 1; enterops[i]; i++)
2725 if (enterops[i]->op_type == OP_ENTERITER)
2726 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2727 }
2728
b500e03b
GG
2729 if (*enterops && enterops[1]) {
2730 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2731 if (enterops[i])
2732 deprecate("\"goto\" to jump into a construct");
2733 }
2734
a0d0e21e
LW
2735 /* pop unwanted frames */
2736
2737 if (ix < cxstack_ix) {
2738 I32 oldsave;
2739
2740 if (ix < 0)
2741 ix = 0;
2742 dounwind(ix);
2743 TOPBLOCK(cx);
3280af22 2744 oldsave = PL_scopestack[PL_scopestack_ix];
a0d0e21e
LW
2745 LEAVE_SCOPE(oldsave);
2746 }
2747
2748 /* push wanted frames */
2749
748a9306 2750 if (*enterops && enterops[1]) {
0bd48802 2751 OP * const oldop = PL_op;
33d34e4c
AE
2752 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2753 for (; enterops[ix]; ix++) {
533c011a 2754 PL_op = enterops[ix];
84902520
TB
2755 /* Eventually we may want to stack the needed arguments
2756 * for each op. For now, we punt on the hard ones. */
533c011a 2757 if (PL_op->op_type == OP_ENTERITER)
894356b3 2758 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
fc0dc3b3 2759 CALL_FPTR(PL_op->op_ppaddr)(aTHX);
a0d0e21e 2760 }
533c011a 2761 PL_op = oldop;
a0d0e21e
LW
2762 }
2763 }
2764
2765 if (do_dump) {
a5f75d66 2766#ifdef VMS
6b88bc9c 2767 if (!retop) retop = PL_main_start;
a5f75d66 2768#endif
3280af22
NIS
2769 PL_restartop = retop;
2770 PL_do_undump = TRUE;
a0d0e21e
LW
2771
2772 my_unexec();
2773
3280af22
NIS
2774 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2775 PL_do_undump = FALSE;
a0d0e21e
LW
2776 }
2777
2778 RETURNOP(retop);
2779}
2780
2781PP(pp_exit)
2782{
97aff369 2783 dVAR;
39644a26 2784 dSP;
a0d0e21e
LW
2785 I32 anum;
2786
2787 if (MAXARG < 1)
2788 anum = 0;
ff0cee69 2789 else {
a0d0e21e 2790 anum = SvIVx(POPs);
d98f61e7
GS
2791#ifdef VMS
2792 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
ff0cee69 2793 anum = 0;
96e176bf 2794 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
ff0cee69
PP
2795#endif
2796 }
cc3604b1 2797 PL_exit_flags |= PERL_EXIT_EXPECTED;
81d86705
NC
2798#ifdef PERL_MAD
2799 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2800 if (anum || !(PL_minus_c && PL_madskills))
2801 my_exit(anum);
2802#else
a0d0e21e 2803 my_exit(anum);
81d86705 2804#endif
3280af22 2805 PUSHs(&PL_sv_undef);
a0d0e21e
LW
2806 RETURN;
2807}
2808
a0d0e21e
LW
2809/* Eval. */
2810
0824fdcb 2811STATIC void
cea2e8a9 2812S_save_lines(pTHX_ AV *array, SV *sv)
a0d0e21e 2813{
504618e9 2814 const char *s = SvPVX_const(sv);
890ce7af 2815 const char * const send = SvPVX_const(sv) + SvCUR(sv);
504618e9 2816 I32 line = 1;
a0d0e21e 2817
7918f24d
NC
2818 PERL_ARGS_ASSERT_SAVE_LINES;
2819
a0d0e21e 2820 while (s && s < send) {
f54cb97a 2821 const char *t;
b9f83d2f 2822 SV * const tmpstr = newSV_type(SVt_PVMG);
a0d0e21e 2823
1d963ff3 2824 t = (const char *)memchr(s, '\n', send - s);
a0d0e21e
LW
2825 if (t)
2826 t++;
2827 else
2828 t = send;
2829
2830 sv_setpvn(tmpstr, s, t - s);
2831 av_store(array, line++, tmpstr);
2832 s = t;
2833 }
2834}
2835
22f16304
RU
2836/*
2837=for apidoc docatch
2838
2839Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
2840
28410 is used as continue inside eval,
2842
28433 is used for a die caught by an inner eval - continue inner loop
2844
2845See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
2846establish a local jmpenv to handle exception traps.
2847
2848=cut
2849*/
0824fdcb 2850STATIC OP *
cea2e8a9 2851S_docatch(pTHX_ OP *o)
1e422769 2852{
97aff369 2853 dVAR;
6224f72b 2854 int ret;
06b5626a 2855 OP * const oldop = PL_op;
db36c5a1 2856 dJMPENV;
1e422769 2857
1e422769 2858#ifdef DEBUGGING
54310121 2859 assert(CATCH_GET == TRUE);
1e422769 2860#endif
312caa8e 2861 PL_op = o;
8bffa5f8 2862
14dd3ad8 2863 JMPENV_PUSH(ret);
6224f72b 2864 switch (ret) {
312caa8e 2865 case 0:
abd70938
DM
2866 assert(cxstack_ix >= 0);
2867 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2868 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
14dd3ad8 2869 redo_body:
85aaa934 2870 CALLRUNOPS(aTHX);
312caa8e
CS
2871 break;
2872 case 3:
8bffa5f8 2873 /* die caught by an inner eval - continue inner loop */
abd70938
DM
2874
2875 /* NB XXX we rely on the old popped CxEVAL still being at the top
2876 * of the stack; the way die_where() currently works, this
2877 * assumption is valid. In theory The cur_top_env value should be
2878 * returned in another global, the way retop (aka PL_restartop)
2879 * is. */
2880 assert(CxTYPE(&cxstack[cxstack_ix+1]) == CXt_EVAL);
2881
2882 if (PL_restartop
2883 && cxstack[cxstack_ix+1].blk_eval.cur_top_env == PL_top_env)
2884 {
312caa8e
CS
2885 PL_op = PL_restartop;
2886 PL_restartop = 0;
2887 goto redo_body;
2888 }
2889 /* FALL THROUGH */
2890 default:
14dd3ad8 2891 JMPENV_POP;
533c011a 2892 PL_op = oldop;
6224f72b 2893 JMPENV_JUMP(ret);
1e422769 2894 /* NOTREACHED */
1e422769 2895 }
14dd3ad8 2896 JMPENV_POP;
533c011a 2897 PL_op = oldop;
5f66b61c 2898 return NULL;
1e422769
PP
2899}
2900
ee23ad3b
NC
2901/* James Bond: Do you expect me to talk?
2902 Auric Goldfinger: No, Mr. Bond. I expect you to die.
2903
2904 This code is an ugly hack, doesn't work with lexicals in subroutines that are
2905 called more than once, and is only used by regcomp.c, for (?{}) blocks.
2906
2907 Currently it is not used outside the core code. Best if it stays that way.
2908*/
c277df42 2909OP *
bfed75c6 2910Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
c277df42
IZ
2911/* sv Text to convert to OP tree. */
2912/* startop op_free() this to undo. */
2913/* code Short string id of the caller. */
2914{
27da23d5 2915 dVAR; dSP; /* Make POPBLOCK work. */
c277df42
IZ
2916 PERL_CONTEXT *cx;
2917 SV **newsp;
b094c71d 2918 I32 gimme = G_VOID;
c277df42
IZ
2919 I32 optype;
2920 OP dummy;
83ee9e09
GS
2921 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2922 char *tmpbuf = tbuf;
c277df42 2923 char *safestr;
a3985cdc 2924 int runtime;
601f1833 2925 CV* runcv = NULL; /* initialise to avoid compiler warnings */
f7997f86 2926 STRLEN len;
c277df42 2927
7918f24d
NC
2928 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
2929
d343c3ef 2930 ENTER_with_name("eval");
5486870f 2931 lex_start(sv, NULL, FALSE);
c277df42
IZ
2932 SAVETMPS;
2933 /* switch to eval mode */
2934
923e4eb5 2935 if (IN_PERL_COMPILETIME) {
f4dd75d9 2936 SAVECOPSTASH_FREE(&PL_compiling);
11faa288 2937 CopSTASH_set(&PL_compiling, PL_curstash);
cbce877f 2938 }
83ee9e09 2939 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
9d4ba2ae 2940 SV * const sv = sv_newmortal();
83ee9e09
GS
2941 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
2942 code, (unsigned long)++PL_evalseq,
2943 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
2944 tmpbuf = SvPVX(sv);
fc009855 2945 len = SvCUR(sv);
83ee9e09
GS
2946 }
2947 else
d9fad198
JH
2948 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
2949 (unsigned long)++PL_evalseq);
f4dd75d9 2950 SAVECOPFILE_FREE(&PL_compiling);
57843af0 2951 CopFILE_set(&PL_compiling, tmpbuf+2);
f4dd75d9 2952 SAVECOPLINE(&PL_compiling);
57843af0 2953 CopLINE_set(&PL_compiling, 1);
c277df42
IZ
2954 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2955 deleting the eval's FILEGV from the stash before gv_check() runs
2956 (i.e. before run-time proper). To work around the coredump that
2957 ensues, we always turn GvMULTI_on for any globals that were
2958 introduced within evals. See force_ident(). GSAR 96-10-12 */
f7997f86
NC
2959 safestr = savepvn(tmpbuf, len);
2960 SAVEDELETE(PL_defstash, safestr, len);
b3ac6de7 2961 SAVEHINTS();
d1ca3daa 2962#ifdef OP_IN_REGISTER
6b88bc9c 2963 PL_opsave = op;
d1ca3daa 2964#else
7766f137 2965 SAVEVPTR(PL_op);
d1ca3daa 2966#endif
c277df42 2967
a3985cdc 2968 /* we get here either during compilation, or via pp_regcomp at runtime */
923e4eb5 2969 runtime = IN_PERL_RUNTIME;
a3985cdc 2970 if (runtime)
d819b83a 2971 runcv = find_runcv(NULL);
a3985cdc 2972
533c011a 2973 PL_op = &dummy;
13b51b79 2974 PL_op->op_type = OP_ENTEREVAL;
533c011a 2975 PL_op->op_flags = 0; /* Avoid uninit warning. */
923e4eb5 2976 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
6b75f042 2977 PUSHEVAL(cx, 0);
a3985cdc
DM
2978
2979 if (runtime)
410be5db 2980 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
a3985cdc 2981 else
410be5db 2982 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
13b51b79 2983 POPBLOCK(cx,PL_curpm);
e84b9f1f 2984 POPEVAL(cx);
c277df42
IZ
2985
2986 (*startop)->op_type = OP_NULL;
22c35a8c 2987 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
c277df42 2988 lex_end();
f3548bdc 2989 /* XXX DAPM do this properly one year */
502c6561 2990 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
d343c3ef 2991 LEAVE_with_name("eval");
923e4eb5 2992 if (IN_PERL_COMPILETIME)
623e6609 2993 CopHINTS_set(&PL_compiling, PL_hints);
d1ca3daa 2994#ifdef OP_IN_REGISTER
6b88bc9c 2995 op = PL_opsave;
d1ca3daa 2996#endif
9d4ba2ae
AL
2997 PERL_UNUSED_VAR(newsp);
2998 PERL_UNUSED_VAR(optype);
2999
410be5db 3000 return PL_eval_start;
c277df42
IZ
3001}
3002
a3985cdc
DM
3003
3004/*
3005=for apidoc find_runcv
3006
3007Locate the CV corresponding to the currently executing sub or eval.
d819b83a
DM
3008If db_seqp is non_null, skip CVs that are in the DB package and populate
3009*db_seqp with the cop sequence number at the point that the DB:: code was
3010entered. (allows debuggers to eval in the scope of the breakpoint rather
cf525c36 3011than in the scope of the debugger itself).
a3985cdc
DM
3012
3013=cut
3014*/
3015
3016CV*
d819b83a 3017Perl_find_runcv(pTHX_ U32 *db_seqp)
a3985cdc 3018{
97aff369 3019 dVAR;
a3985cdc 3020 PERL_SI *si;
a3985cdc 3021
d819b83a
DM
3022 if (db_seqp)
3023 *db_seqp = PL_curcop->cop_seq;
a3985cdc 3024 for (si = PL_curstackinfo; si; si = si->si_prev) {
06b5626a 3025 I32 ix;
a3985cdc 3026 for (ix = si->si_cxix; ix >= 0; ix--) {
06b5626a 3027 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
d819b83a 3028 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1b6737cc 3029 CV * const cv = cx->blk_sub.cv;
d819b83a
DM
3030 /* skip DB:: code */
3031 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3032 *db_seqp = cx->blk_oldcop->cop_seq;
3033 continue;
3034 }
3035 return cv;
3036 }
a3985cdc
DM
3037 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
3038 return PL_compcv;
3039 }
3040 }
3041 return PL_main_cv;
3042}
3043
3044
3045/* Compile a require/do, an eval '', or a /(?{...})/.
3046 * In the last case, startop is non-null, and contains the address of
3047 * a pointer that should be set to the just-compiled code.
3048 * outside is the lexically enclosing CV (if any) that invoked us.
410be5db
DM
3049 * Returns a bool indicating whether the compile was successful; if so,
3050 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3051 * pushes undef (also croaks if startop != NULL).
a3985cdc
DM
3052 */
3053
410be5db 3054STATIC bool
a3985cdc 3055S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
a0d0e21e 3056{
27da23d5 3057 dVAR; dSP;
46c461b5 3058 OP * const saveop = PL_op;
a0d0e21e 3059
6dc8a9e4
IZ
3060 PL_in_eval = ((saveop && saveop->op_type == OP_REQUIRE)
3061 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3062 : EVAL_INEVAL);
a0d0e21e 3063
1ce6579f
PP
3064 PUSHMARK(SP);
3065
3280af22 3066 SAVESPTR(PL_compcv);
ea726b52 3067 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
1aff0e91 3068 CvEVAL_on(PL_compcv);
2090ab20
JH
3069 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3070 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
3071
a3985cdc 3072 CvOUTSIDE_SEQ(PL_compcv) = seq;
ea726b52 3073 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
a3985cdc 3074
dd2155a4 3075 /* set up a scratch pad */
a0d0e21e 3076
dd2155a4 3077 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
cecbe010 3078 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
2c05e328 3079
07055b4c 3080
81d86705
NC
3081 if (!PL_madskills)
3082 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
748a9306 3083
a0d0e21e
LW
3084 /* make sure we compile in the right package */
3085
ed094faf 3086 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3280af22 3087 SAVESPTR(PL_curstash);
ed094faf 3088 PL_curstash = CopSTASH(PL_curcop);
a0d0e21e 3089 }
3c10abe3 3090 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3280af22
NIS
3091 SAVESPTR(PL_beginav);
3092 PL_beginav = newAV();
3093 SAVEFREESV(PL_beginav);
3c10abe3
AG
3094 SAVESPTR(PL_unitcheckav);
3095 PL_unitcheckav = newAV();
3096 SAVEFREESV(PL_unitcheckav);
a0d0e21e 3097
81d86705 3098#ifdef PERL_MAD
9da243ce 3099 SAVEBOOL(PL_madskills);
81d86705
NC
3100 PL_madskills = 0;
3101#endif
3102
a0d0e21e
LW
3103 /* try to compile it */
3104
5f66b61c 3105 PL_eval_root = NULL;
3280af22 3106 PL_curcop = &PL_compiling;
fc15ae8f 3107 CopARYBASE_set(PL_curcop, 0);
5f66b61c 3108 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
faef0170 3109 PL_in_eval |= EVAL_KEEPERR;
ab69dbc2
RGS
3110 else
3111 CLEAR_ERRSV();
13765c85 3112 if (yyparse() || PL_parser->error_count || !PL_eval_root) {
0c58d367 3113 SV **newsp; /* Used by POPBLOCK. */
9d4ba2ae 3114 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
c277df42 3115 I32 optype = 0; /* Might be reset by POPEVAL. */
9d4ba2ae 3116 const char *msg;
bfed75c6 3117
533c011a 3118 PL_op = saveop;
3280af22
NIS
3119 if (PL_eval_root) {
3120 op_free(PL_eval_root);
5f66b61c 3121 PL_eval_root = NULL;
a0d0e21e 3122 }
3280af22 3123 SP = PL_stack_base + POPMARK; /* pop original mark */
c277df42 3124 if (!startop) {
3280af22 3125 POPBLOCK(cx,PL_curpm);
c277df42 3126 POPEVAL(cx);
c277df42 3127 }
a0d0e21e 3128 lex_end();
d343c3ef 3129 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
9d4ba2ae
AL
3130
3131 msg = SvPVx_nolen_const(ERRSV);
7a2e2cd6 3132 if (optype == OP_REQUIRE) {
b464bac0 3133 const SV * const nsv = cx->blk_eval.old_namesv;
504618e9 3134 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
27bcc0a7 3135 &PL_sv_undef, 0);
58d3fd3b
SH
3136 Perl_croak(aTHX_ "%sCompilation failed in require",
3137 *msg ? msg : "Unknown error\n");
5a844595
GS
3138 }
3139 else if (startop) {
3280af22 3140 POPBLOCK(cx,PL_curpm);
c277df42 3141 POPEVAL(cx);
5a844595
GS
3142 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3143 (*msg ? msg : "Unknown error\n"));
7a2e2cd6 3144 }
9d7f88dd 3145 else {
9d7f88dd 3146 if (!*msg) {
6502358f 3147 sv_setpvs(ERRSV, "Compilation error");
9d7f88dd
SR
3148 }
3149 }
9d4ba2ae 3150 PERL_UNUSED_VAR(newsp);
410be5db
DM
3151 PUSHs(&PL_sv_undef);
3152 PUTBACK;
3153 return FALSE;
a0d0e21e 3154 }
57843af0 3155 CopLINE_set(&PL_compiling, 0);
c277df42 3156 if (startop) {
3280af22 3157 *startop = PL_eval_root;
c277df42 3158 } else
3280af22 3159 SAVEFREEOP(PL_eval_root);
0c58d367
RGS
3160
3161 /* Set the context for this new optree.
021f53de
GG
3162 * Propagate the context from the eval(). */
3163 if ((gimme & G_WANT) == G_VOID)
3280af22 3164 scalarvoid(PL_eval_root);
7df0357e 3165 else if ((gimme & G_WANT) == G_ARRAY)
3280af22 3166 list(PL_eval_root);
a0d0e21e 3167 else
3280af22 3168 scalar(PL_eval_root);
a0d0e21e
LW
3169
3170 DEBUG_x(dump_eval());
3171
55497cff 3172 /* Register with debugger: */
6482a30d 3173 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
b96d8cd9 3174 CV * const cv = get_cvs("DB::postponed", 0);
55497cff
PP
3175 if (cv) {
3176 dSP;
924508f0 3177 PUSHMARK(SP);
ad64d0ec 3178 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
55497cff 3179 PUTBACK;
ad64d0ec 3180 call_sv(MUTABLE_SV(cv), G_DISCARD);
55497cff
PP
3181 }
3182 }
3183
3c10abe3
AG
3184 if (PL_unitcheckav)
3185 call_list(PL_scopestack_ix, PL_unitcheckav);
3186
a0d0e21e
LW
3187 /* compiled okay, so do it */
3188
3280af22
NIS
3189 CvDEPTH(PL_compcv) = 1;
3190 SP = PL_stack_base + POPMARK; /* pop original mark */
533c011a 3191 PL_op = saveop; /* The caller may need it. */
bc177e6b 3192 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
5dc0d613 3193
410be5db
DM
3194 PUTBACK;
3195 return TRUE;
a0d0e21e
LW
3196}
3197
a6c40364 3198STATIC PerlIO *
0786552a 3199S_check_type_and_open(pTHX_ const char *name)
ce8abf5f
SP
3200{
3201 Stat_t st;
c445ea15 3202 const int st_rc = PerlLIO_stat(name, &st);
df528165 3203
7918f24d
NC
3204 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3205
6b845e56 3206 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
4608196e 3207 return NULL;
ce8abf5f
SP
3208 }
3209
0786552a 3210 return PerlIO_open(name, PERL_SCRIPT_MODE);
ce8abf5f
SP
3211}
3212
75c20bac 3213#ifndef PERL_DISABLE_PMC
ce8abf5f 3214STATIC PerlIO *
0786552a 3215S_doopen_pm(pTHX_ const char *name, const STRLEN namelen)
b295d113 3216{
b295d113
TH
3217 PerlIO *fp;
3218
7918f24d
NC
3219 PERL_ARGS_ASSERT_DOOPEN_PM;
3220
ce9440c8 3221 if (namelen > 3 && memEQs(name + namelen - 3, 3, ".pm")) {
50b8ed39
NC
3222 SV *const pmcsv = newSV(namelen + 2);
3223 char *const pmc = SvPVX(pmcsv);
a6c40364 3224 Stat_t pmcstat;
50b8ed39
NC
3225
3226 memcpy(pmc, name, namelen);
3227 pmc[namelen] = 'c';
3228 pmc[namelen + 1] = '\0';
3229
a6c40364 3230 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
0786552a 3231 fp = check_type_and_open(name);
a6c40364
GS
3232 }
3233 else {
0786552a 3234 fp = check_type_and_open(pmc);
b295d113 3235 }
a6c40364
GS
3236 SvREFCNT_dec(pmcsv);
3237 }
3238 else {
0786552a 3239 fp = check_type_and_open(name);
b295d113 3240 }
b295d113 3241 return fp;
75c20bac 3242}
7925835c 3243#else
75c20bac 3244# define doopen_pm(name, namelen) check_type_and_open(name)
7925835c 3245#endif /* !PERL_DISABLE_PMC */
b295d113 3246
a0d0e21e
LW
3247PP(pp_require)
3248{
27da23d5 3249 dVAR; dSP;
c09156bb 3250 register PERL_CONTEXT *cx;
a0d0e21e 3251 SV *sv;
5c144d81 3252 const char *name;
6132ea6c 3253 STRLEN len;
4492be7a
JM
3254 char * unixname;
3255 STRLEN unixlen;
62f5ad7a 3256#ifdef VMS
4492be7a 3257 int vms_unixname = 0;
62f5ad7a 3258#endif
c445ea15
AL
3259 const char *tryname = NULL;
3260 SV *namesv = NULL;
f54cb97a 3261 const I32 gimme = GIMME_V;
bbed91b5 3262 int filter_has_file = 0;
c445ea15 3263 PerlIO *tryrsfp = NULL;
34113e50 3264 SV *filter_cache = NULL;
c445ea15
AL
3265 SV *filter_state = NULL;
3266 SV *filter_sub = NULL;
3267 SV *hook_sv = NULL;
6ec9efec
JH
3268 SV *encoding;
3269 OP *op;
a0d0e21e
LW
3270
3271 sv = POPs;
d7aa5382 3272 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
d7aa5382
JP
3273 sv = new_version(sv);
3274 if (!sv_derived_from(PL_patchlevel, "version"))
ac0e6a2f 3275 upg_version(PL_patchlevel, TRUE);
149c1637 3276 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3cacfbb9 3277 if ( vcmp(sv,PL_patchlevel) <= 0 )
468aa647 3278 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
be2597df 3279 SVfARG(vnormal(sv)), SVfARG(vnormal(PL_patchlevel)));
468aa647
RGS
3280 }
3281 else {
d1029faa
JP
3282 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3283 I32 first = 0;
3284 AV *lav;
3285 SV * const req = SvRV(sv);
85fbaab2 3286 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
d1029faa
JP
3287
3288 /* get the left hand term */
502c6561 3289 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
d1029faa
JP
3290
3291 first = SvIV(*av_fetch(lav,0,0));
3292 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
85fbaab2 3293 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
d1029faa
JP
3294 || av_len(lav) > 1 /* FP with > 3 digits */
3295 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3296 ) {
3297 DIE(aTHX_ "Perl %"SVf" required--this is only "
3298 "%"SVf", stopped", SVfARG(vnormal(req)),
3299 SVfARG(vnormal(PL_patchlevel)));
3300 }
3301 else { /* probably 'use 5.10' or 'use 5.8' */
af61dbfd 3302 SV *hintsv;
d1029faa
JP
3303 I32 second = 0;
3304
3305 if (av_len(lav)>=1)
3306 second = SvIV(*av_fetch(lav,1,0));
3307
3308 second /= second >= 600 ? 100 : 10;
af61dbfd
NC
3309 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3310 (int)first, (int)second);
d1029faa
JP
3311 upg_version(hintsv, TRUE);
3312
3313 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3314 "--this is only %"SVf", stopped",
3315 SVfARG(vnormal(req)),
af61dbfd 3316 SVfARG(vnormal(sv_2mortal(hintsv))),
d1029faa
JP
3317 SVfARG(vnormal(PL_patchlevel)));
3318 }
3319 }
468aa647 3320 }
d7aa5382 3321
fbc891ce
RB
3322 /* We do this only with use, not require. */
3323 if (PL_compcv &&
fbc891ce
RB
3324 /* If we request a version >= 5.9.5, load feature.pm with the
3325 * feature bundle that corresponds to the required version. */
2e8342de 3326 vcmp(sv, sv_2mortal(upg_version(newSVnv(5.009005), FALSE))) >= 0) {
7dfde25d
RGS
3327 SV *const importsv = vnormal(sv);
3328 *SvPVX_mutable(importsv) = ':';
d343c3ef 3329 ENTER_with_name("load_feature");
7dfde25d 3330 Perl_load_module(aTHX_ 0, newSVpvs("feature"), NULL, importsv, NULL);
d343c3ef 3331 LEAVE_with_name("load_feature");
7dfde25d 3332 }
53eb19dd
SM
3333 /* If a version >= 5.11.0 is requested, strictures are on by default! */
3334 if (PL_compcv &&
3335 vcmp(sv, sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
5cc917d6 3336 PL_hints |= (HINT_STRICT_REFS | HINT_STRICT_SUBS | HINT_STRICT_VARS);
53eb19dd
SM
3337 }
3338
7dfde25d 3339 RETPUSHYES;
a0d0e21e 3340 }
5c144d81 3341 name = SvPV_const(sv, len);
6132ea6c 3342 if (!(name && len > 0 && *name))
cea2e8a9 3343 DIE(aTHX_ "Null filename used");
4633a7c4 3344 TAINT_PROPER("require");
4492be7a
JM
3345
3346
3347#ifdef VMS
3348 /* The key in the %ENV hash is in the syntax of file passed as the argument
3349 * usually this is in UNIX format, but sometimes in VMS format, which
3350 * can result in a module being pulled in more than once.
3351 * To prevent this, the key must be stored in UNIX format if the VMS
3352 * name can be translated to UNIX.
3353 */
3354 if ((unixname = tounixspec(name, NULL)) != NULL) {
3355 unixlen = strlen(unixname);
3356 vms_unixname = 1;
3357 }
3358 else
3359#endif
3360 {
3361 /* if not VMS or VMS name can not be translated to UNIX, pass it
3362 * through.
3363 */
3364 unixname = (char *) name;
3365 unixlen = len;
3366 }
44f8325f 3367 if (PL_op->op_type == OP_REQUIRE) {
4492be7a
JM
3368 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3369 unixname, unixlen, 0);
44f8325f
AL
3370 if ( svp ) {
3371 if (*svp != &PL_sv_undef)
3372 RETPUSHYES;
3373 else
087b5369
RD
3374 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3375 "Compilation failed in require", unixname);
44f8325f 3376 }
4d8b06f1 3377 }
a0d0e21e
LW
3378