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