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