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