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