This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Consistent indentation in perlbook
[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
54310121 37#define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
1e422769 38
94fcd414
NC
39#define dopoptosub(plop) dopoptosub_at(cxstack, (plop))
40
a0d0e21e
LW
41PP(pp_wantarray)
42{
97aff369 43 dVAR;
39644a26 44 dSP;
a0d0e21e 45 I32 cxix;
93f0bc49 46 const PERL_CONTEXT *cx;
a0d0e21e
LW
47 EXTEND(SP, 1);
48
93f0bc49
FC
49 if (PL_op->op_private & OPpOFFBYONE) {
50 if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF;
51 }
52 else {
53 cxix = dopoptosub(cxstack_ix);
54 if (cxix < 0)
a0d0e21e 55 RETPUSHUNDEF;
93f0bc49
FC
56 cx = &cxstack[cxix];
57 }
a0d0e21e 58
93f0bc49 59 switch (cx->blk_gimme) {
54310121 60 case G_ARRAY:
a0d0e21e 61 RETPUSHYES;
54310121 62 case G_SCALAR:
a0d0e21e 63 RETPUSHNO;
54310121 64 default:
65 RETPUSHUNDEF;
66 }
a0d0e21e
LW
67}
68
2cd61cdb
IZ
69PP(pp_regcreset)
70{
97aff369 71 dVAR;
2cd61cdb
IZ
72 /* XXXX Should store the old value to allow for tie/overload - and
73 restore in regcomp, where marked with XXXX. */
3280af22 74 PL_reginterp_cnt = 0;
0b4182de 75 TAINT_NOT;
2cd61cdb
IZ
76 return NORMAL;
77}
78
b3eb6a9b
GS
79PP(pp_regcomp)
80{
97aff369 81 dVAR;
39644a26 82 dSP;
a0d0e21e 83 register PMOP *pm = (PMOP*)cLOGOP->op_other;
a0d0e21e 84 SV *tmpstr;
84679df5 85 REGEXP *re = NULL;
bfed75c6 86
4b5a0d1c 87 /* prevent recompiling under /o and ithreads. */
3db8f154 88#if defined(USE_ITHREADS)
131b3ad0
DM
89 if (pm->op_pmflags & PMf_KEEP && PM_GETRE(pm)) {
90 if (PL_op->op_flags & OPf_STACKED) {
91 dMARK;
92 SP = MARK;
93 }
94 else
95 (void)POPs;
96 RETURN;
97 }
513629ba 98#endif
d4b87e75
BM
99
100#define tryAMAGICregexp(rx) \
101 STMT_START { \
6f1401dc 102 SvGETMAGIC(rx); \
d4b87e75 103 if (SvROK(rx) && SvAMAGIC(rx)) { \
31d632c3 104 SV *sv = AMG_CALLunary(rx, regexp_amg); \
d4b87e75
BM
105 if (sv) { \
106 if (SvROK(sv)) \
107 sv = SvRV(sv); \
108 if (SvTYPE(sv) != SVt_REGEXP) \
109 Perl_croak(aTHX_ "Overloaded qr did not return a REGEXP"); \
110 rx = sv; \
111 } \
112 } \
113 } STMT_END
114
115
131b3ad0 116 if (PL_op->op_flags & OPf_STACKED) {
486ec47a 117 /* multiple args; concatenate them */
131b3ad0
DM
118 dMARK; dORIGMARK;
119 tmpstr = PAD_SV(ARGTARG);
76f68e9b 120 sv_setpvs(tmpstr, "");
131b3ad0 121 while (++MARK <= SP) {
d4b87e75 122 SV *msv = *MARK;
79a8d529 123 SV *sv;
d4b87e75 124
79a8d529 125 tryAMAGICregexp(msv);
d4b87e75 126
79a8d529
DM
127 if ((SvAMAGIC(tmpstr) || SvAMAGIC(msv)) &&
128 (sv = amagic_call(tmpstr, msv, concat_amg, AMGf_assign)))
129 {
130 sv_setsv(tmpstr, sv);
131 continue;
131b3ad0 132 }
37c07a4b
FC
133
134 if (SvROK(msv) && SvTYPE(SvRV(msv)) == SVt_REGEXP) {
135 msv = SvRV(msv);
136 PL_reginterp_cnt +=
137 RX_SEEN_EVALS((REGEXP *)MUTABLE_PTR(msv));
138 }
139
a9984b10 140 sv_catsv_nomg(tmpstr, msv);
131b3ad0
DM
141 }
142 SvSETMAGIC(tmpstr);
143 SP = ORIGMARK;
144 }
d4b87e75 145 else {
131b3ad0 146 tmpstr = POPs;
d4b87e75
BM
147 tryAMAGICregexp(tmpstr);
148 }
149
150#undef tryAMAGICregexp
513629ba 151
b3eb6a9b 152 if (SvROK(tmpstr)) {
d8f6592e 153 SV * const sv = SvRV(tmpstr);
5c35adbb 154 if (SvTYPE(sv) == SVt_REGEXP)
d2f13c59 155 re = (REGEXP*) sv;
c277df42 156 }
d4b87e75
BM
157 else if (SvTYPE(tmpstr) == SVt_REGEXP)
158 re = (REGEXP*) tmpstr;
159
5c35adbb 160 if (re) {
69dc4b30
FC
161 /* The match's LHS's get-magic might need to access this op's reg-
162 exp (as is sometimes the case with $'; see bug 70764). So we
163 must call get-magic now before we replace the regexp. Hopeful-
164 ly this hack can be replaced with the approach described at
165 http://www.nntp.perl.org/group/perl.perl5.porters/2007/03
166 /msg122415.html some day. */
455d9033
FC
167 if(pm->op_type == OP_MATCH) {
168 SV *lhs;
169 const bool was_tainted = PL_tainted;
170 if (pm->op_flags & OPf_STACKED)
69dc4b30 171 lhs = TOPs;
455d9033
FC
172 else if (pm->op_private & OPpTARGET_MY)
173 lhs = PAD_SV(pm->op_targ);
174 else lhs = DEFSV;
175 SvGETMAGIC(lhs);
176 /* Restore the previous value of PL_tainted (which may have been
177 modified by get-magic), to avoid incorrectly setting the
178 RXf_TAINTED flag further down. */
179 PL_tainted = was_tainted;
180 }
69dc4b30 181
f0826785 182 re = reg_temp_copy(NULL, re);
aaa362c4 183 ReREFCNT_dec(PM_GETRE(pm));
28d8d7f4 184 PM_SETRE(pm, re);
c277df42
IZ
185 }
186 else {
f3ec07c7
DM
187 STRLEN len = 0;
188 const char *t = SvOK(tmpstr) ? SvPV_nomg_const(tmpstr, len) : "";
189
c737faaf 190 re = PM_GETRE(pm);
14a49a24 191 assert (re != (REGEXP*) &PL_sv_undef);
c277df42 192
20408e3c 193 /* Check against the last compiled regexp. */
a11c8683 194 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
220fc49f 195 memNE(RX_PRECOMP(re), t, len))
85aff577 196 {
07bc277f 197 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
73134a2e 198 U32 pm_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
d8f6592e
AL
199 if (re) {
200 ReREFCNT_dec(re);
14a49a24
NC
201#ifdef USE_ITHREADS
202 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
203#else
4608196e 204 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
14a49a24 205#endif
1e2e3d02 206 } else if (PL_curcop->cop_hints_hash) {
20439bc7 207 SV *ptr = cop_hints_fetch_pvs(PL_curcop, "regcomp", 0);
1e2e3d02
YO
208 if (ptr && SvIOK(ptr) && SvIV(ptr))
209 eng = INT2PTR(regexp_engine*,SvIV(ptr));
c277df42 210 }
664e119d 211
533c011a 212 if (PL_op->op_flags & OPf_SPECIAL)
3280af22 213 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
a0d0e21e 214
15d9c083 215 if (!DO_UTF8(tmpstr) && SvUTF8(tmpstr)) {
b9ad30b4
NC
216 /* Not doing UTF-8, despite what the SV says. Is this only if
217 we're trapped in use 'bytes'? */
218 /* Make a copy of the octet sequence, but without the flag on,
219 as the compiler now honours the SvUTF8 flag on tmpstr. */
220 STRLEN len;
221 const char *const p = SvPV(tmpstr, len);
222 tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
223 }
3e102237 224 else if (SvAMAGIC(tmpstr) || SvGMAGICAL(tmpstr)) {
f3ec07c7 225 /* make a copy to avoid extra stringifies */
0479a84a 226 tmpstr = newSVpvn_flags(t, len, SVs_TEMP | SvUTF8(tmpstr));
f3ec07c7 227 }
c737faaf 228
5a8697a7 229 if (eng)
3ab4a224 230 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
5a8697a7 231 else
3ab4a224 232 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
c737faaf 233
f86aaa29 234 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
2cd61cdb 235 inside tie/overload accessors. */
c277df42 236 }
4633a7c4 237 }
c737faaf
YO
238
239 re = PM_GETRE(pm);
a0d0e21e 240
72311751 241#ifndef INCOMPLETE_TAINTS
3280af22 242 if (PL_tainting) {
9274aefd
DM
243 if (PL_tainted) {
244 SvTAINTED_on((SV*)re);
07bc277f 245 RX_EXTFLAGS(re) |= RXf_TAINTED;
9274aefd 246 }
72311751
GS
247 }
248#endif
249
220fc49f 250 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
3280af22 251 pm = PL_curpm;
a0d0e21e 252
c737faaf
YO
253
254#if !defined(USE_ITHREADS)
255 /* can't change the optree at runtime either */
256 /* PMf_KEEP is handled differently under threads to avoid these problems */
a0d0e21e 257 if (pm->op_pmflags & PMf_KEEP) {
c90c0ff4 258 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
533c011a 259 cLOGOP->op_first->op_next = PL_op->op_next;
a0d0e21e 260 }
c737faaf 261#endif
a0d0e21e
LW
262 RETURN;
263}
264
265PP(pp_substcont)
266{
97aff369 267 dVAR;
39644a26 268 dSP;
c09156bb 269 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
901017d6
AL
270 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
271 register SV * const dstr = cx->sb_dstr;
a0d0e21e
LW
272 register char *s = cx->sb_s;
273 register char *m = cx->sb_m;
274 char *orig = cx->sb_orig;
901017d6 275 register REGEXP * const rx = cx->sb_rx;
c445ea15 276 SV *nsv = NULL;
988e6e7e 277 REGEXP *old = PM_GETRE(pm);
f410a211
NC
278
279 PERL_ASYNC_CHECK();
280
988e6e7e 281 if(old != rx) {
bfed75c6 282 if(old)
988e6e7e 283 ReREFCNT_dec(old);
d6106309 284 PM_SETRE(pm,ReREFCNT_inc(rx));
d8f2cf8a
AB
285 }
286
d9f97599 287 rxres_restore(&cx->sb_rxres, rx);
01b35787 288 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
c90c0ff4 289
a0d0e21e 290 if (cx->sb_iters++) {
a3b680e6 291 const I32 saviters = cx->sb_iters;
a0d0e21e 292 if (cx->sb_iters > cx->sb_maxiters)
cea2e8a9 293 DIE(aTHX_ "Substitution loop");
a0d0e21e 294
447ee134
DM
295 SvGETMAGIC(TOPs); /* possibly clear taint on $1 etc: #67962 */
296
ef07e810 297 /* See "how taint works" above pp_subst() */
20be6587
DM
298 if (SvTAINTED(TOPs))
299 cx->sb_rxtainted |= SUBST_TAINT_REPL;
447ee134 300 sv_catsv_nomg(dstr, POPs);
2c296965
YO
301 /* XXX: adjust for positive offsets of \G for instance s/(.)\G//g with positive pos() */
302 s -= RX_GOFS(rx);
a0d0e21e
LW
303
304 /* Are we done */
2c296965
YO
305 if (CxONCE(cx) || s < orig ||
306 !CALLREGEXEC(rx, s, cx->sb_strend, orig,
307 (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
308 ((cx->sb_rflags & REXEC_COPY_STR)
309 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
310 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
a0d0e21e 311 {
8ca8a454 312 SV *targ = cx->sb_targ;
748a9306 313
078c425b
JH
314 assert(cx->sb_strend >= s);
315 if(cx->sb_strend > s) {
316 if (DO_UTF8(dstr) && !SvUTF8(targ))
317 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
318 else
319 sv_catpvn(dstr, s, cx->sb_strend - s);
320 }
20be6587
DM
321 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
322 cx->sb_rxtainted |= SUBST_TAINT_PAT;
9212bbba 323
8ca8a454
NC
324 if (pm->op_pmflags & PMf_NONDESTRUCT) {
325 PUSHs(dstr);
326 /* From here on down we're using the copy, and leaving the
327 original untouched. */
328 targ = dstr;
329 }
330 else {
8ca8a454
NC
331 if (SvIsCOW(targ)) {
332 sv_force_normal_flags(targ, SV_COW_DROP_PV);
333 } else
8ca8a454
NC
334 {
335 SvPV_free(targ);
336 }
337 SvPV_set(targ, SvPVX(dstr));
338 SvCUR_set(targ, SvCUR(dstr));
339 SvLEN_set(targ, SvLEN(dstr));
340 if (DO_UTF8(dstr))
341 SvUTF8_on(targ);
342 SvPV_set(dstr, NULL);
343
4f4d7508 344 mPUSHi(saviters - 1);
48c036b1 345
8ca8a454
NC
346 (void)SvPOK_only_UTF8(targ);
347 }
5cd24f17 348
20be6587 349 /* update the taint state of various various variables in
ef07e810
DM
350 * preparation for final exit.
351 * See "how taint works" above pp_subst() */
20be6587
DM
352 if (PL_tainting) {
353 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
354 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
355 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
356 )
357 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
358
359 if (!(cx->sb_rxtainted & SUBST_TAINT_BOOLRET)
360 && (cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_PAT))
361 )
362 SvTAINTED_on(TOPs); /* taint return value */
363 /* needed for mg_set below */
364 PL_tainted = cBOOL(cx->sb_rxtainted &
365 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL));
366 SvTAINT(TARG);
367 }
368 /* PL_tainted must be correctly set for this mg_set */
369 SvSETMAGIC(TARG);
370 TAINT_NOT;
4633a7c4 371 LEAVE_SCOPE(cx->sb_oldsave);
a0d0e21e
LW
372 POPSUBST(cx);
373 RETURNOP(pm->op_next);
20be6587 374 /* NOTREACHED */
a0d0e21e 375 }
8e5e9ebe 376 cx->sb_iters = saviters;
a0d0e21e 377 }
07bc277f 378 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
a0d0e21e
LW
379 m = s;
380 s = orig;
07bc277f 381 cx->sb_orig = orig = RX_SUBBEG(rx);
a0d0e21e
LW
382 s = orig + (m - s);
383 cx->sb_strend = s + (cx->sb_strend - m);
384 }
07bc277f 385 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
db79b45b 386 if (m > s) {
bfed75c6 387 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
db79b45b
JH
388 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
389 else
390 sv_catpvn(dstr, s, m-s);
391 }
07bc277f 392 cx->sb_s = RX_OFFS(rx)[0].end + orig;
084916e3 393 { /* Update the pos() information. */
8ca8a454
NC
394 SV * const sv
395 = (pm->op_pmflags & PMf_NONDESTRUCT) ? cx->sb_dstr : cx->sb_targ;
084916e3 396 MAGIC *mg;
7a7f3e45 397 SvUPGRADE(sv, SVt_PVMG);
14befaf4 398 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
d83f0a82 399#ifdef PERL_OLD_COPY_ON_WRITE
51a9ea20 400 if (SvIsCOW(sv))
d83f0a82
NC
401 sv_force_normal_flags(sv, 0);
402#endif
403 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
404 NULL, 0);
084916e3 405 }
ce474962 406 mg->mg_len = m - orig;
084916e3 407 }
988e6e7e 408 if (old != rx)
d6106309 409 (void)ReREFCNT_inc(rx);
20be6587 410 /* update the taint state of various various variables in preparation
ef07e810
DM
411 * for calling the code block.
412 * See "how taint works" above pp_subst() */
20be6587
DM
413 if (PL_tainting) {
414 if (RX_MATCH_TAINTED(rx)) /* run time pattern taint, eg locale */
415 cx->sb_rxtainted |= SUBST_TAINT_PAT;
416
417 if ((cx->sb_rxtainted & SUBST_TAINT_PAT) ||
418 ((cx->sb_rxtainted & (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
419 == (SUBST_TAINT_STR|SUBST_TAINT_RETAINT))
420 )
421 (RX_MATCH_TAINTED_on(rx)); /* taint $1 et al */
422
423 if (cx->sb_iters > 1 && (cx->sb_rxtainted &
424 (SUBST_TAINT_STR|SUBST_TAINT_PAT|SUBST_TAINT_REPL)))
8ca8a454
NC
425 SvTAINTED_on((pm->op_pmflags & PMf_NONDESTRUCT)
426 ? cx->sb_dstr : cx->sb_targ);
20be6587
DM
427 TAINT_NOT;
428 }
d9f97599 429 rxres_save(&cx->sb_rxres, rx);
af9838cc 430 PL_curpm = pm;
29f2e912 431 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
a0d0e21e
LW
432}
433
c90c0ff4 434void
864dbfa3 435Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4 436{
437 UV *p = (UV*)*rsp;
438 U32 i;
7918f24d
NC
439
440 PERL_ARGS_ASSERT_RXRES_SAVE;
96a5add6 441 PERL_UNUSED_CONTEXT;
c90c0ff4 442
07bc277f 443 if (!p || p[1] < RX_NPARENS(rx)) {
f8c7b90f 444#ifdef PERL_OLD_COPY_ON_WRITE
07bc277f 445 i = 7 + RX_NPARENS(rx) * 2;
ed252734 446#else
07bc277f 447 i = 6 + RX_NPARENS(rx) * 2;
ed252734 448#endif
c90c0ff4 449 if (!p)
a02a5408 450 Newx(p, i, UV);
c90c0ff4 451 else
452 Renew(p, i, UV);
453 *rsp = (void*)p;
454 }
455
07bc277f 456 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
cf93c79d 457 RX_MATCH_COPIED_off(rx);
c90c0ff4 458
f8c7b90f 459#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1
NC
460 *p++ = PTR2UV(RX_SAVED_COPY(rx));
461 RX_SAVED_COPY(rx) = NULL;
ed252734
NC
462#endif
463
07bc277f 464 *p++ = RX_NPARENS(rx);
c90c0ff4 465
07bc277f
NC
466 *p++ = PTR2UV(RX_SUBBEG(rx));
467 *p++ = (UV)RX_SUBLEN(rx);
468 for (i = 0; i <= RX_NPARENS(rx); ++i) {
469 *p++ = (UV)RX_OFFS(rx)[i].start;
470 *p++ = (UV)RX_OFFS(rx)[i].end;
c90c0ff4 471 }
472}
473
9c105995
NC
474static void
475S_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4 476{
477 UV *p = (UV*)*rsp;
478 U32 i;
7918f24d
NC
479
480 PERL_ARGS_ASSERT_RXRES_RESTORE;
96a5add6 481 PERL_UNUSED_CONTEXT;
c90c0ff4 482
ed252734 483 RX_MATCH_COPY_FREE(rx);
cf93c79d 484 RX_MATCH_COPIED_set(rx, *p);
c90c0ff4 485 *p++ = 0;
486
f8c7b90f 487#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1
NC
488 if (RX_SAVED_COPY(rx))
489 SvREFCNT_dec (RX_SAVED_COPY(rx));
490 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
ed252734
NC
491 *p++ = 0;
492#endif
493
07bc277f 494 RX_NPARENS(rx) = *p++;
c90c0ff4 495
07bc277f
NC
496 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
497 RX_SUBLEN(rx) = (I32)(*p++);
498 for (i = 0; i <= RX_NPARENS(rx); ++i) {
499 RX_OFFS(rx)[i].start = (I32)(*p++);
500 RX_OFFS(rx)[i].end = (I32)(*p++);
c90c0ff4 501 }
502}
503
9c105995
NC
504static void
505S_rxres_free(pTHX_ void **rsp)
c90c0ff4 506{
44f8325f 507 UV * const p = (UV*)*rsp;
7918f24d
NC
508
509 PERL_ARGS_ASSERT_RXRES_FREE;
96a5add6 510 PERL_UNUSED_CONTEXT;
c90c0ff4 511
512 if (p) {
94010e71
NC
513#ifdef PERL_POISON
514 void *tmp = INT2PTR(char*,*p);
515 Safefree(tmp);
516 if (*p)
7e337ee0 517 PoisonFree(*p, 1, sizeof(*p));
94010e71 518#else
56431972 519 Safefree(INT2PTR(char*,*p));
94010e71 520#endif
f8c7b90f 521#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
522 if (p[1]) {
523 SvREFCNT_dec (INT2PTR(SV*,p[1]));
524 }
525#endif
c90c0ff4 526 Safefree(p);
4608196e 527 *rsp = NULL;
c90c0ff4 528 }
529}
530
a701009a
DM
531#define FORM_NUM_BLANK (1<<30)
532#define FORM_NUM_POINT (1<<29)
533
a0d0e21e
LW
534PP(pp_formline)
535{
97aff369 536 dVAR; dSP; dMARK; dORIGMARK;
823a54a3 537 register SV * const tmpForm = *++MARK;
086b26f3
DM
538 SV *formsv; /* contains text of original format */
539 register U32 *fpc; /* format ops program counter */
540 register char *t; /* current append position in target string */
541 const char *f; /* current position in format string */
a0d0e21e 542 register I32 arg;
086b26f3
DM
543 register SV *sv = NULL; /* current item */
544 const char *item = NULL;/* string value of current item */
545 I32 itemsize = 0; /* length of current item, possibly truncated */
546 I32 fieldsize = 0; /* width of current field */
547 I32 lines = 0; /* number of lines that have been output */
548 bool chopspace = (strchr(PL_chopset, ' ') != NULL); /* does $: have space */
549 const char *chophere = NULL; /* where to chop current item */
f5ada144 550 STRLEN linemark = 0; /* pos of start of line in output */
65202027 551 NV value;
086b26f3 552 bool gotsome = FALSE; /* seen at least one non-blank item on this line */
a0d0e21e 553 STRLEN len;
26e935cf 554 STRLEN linemax; /* estimate of output size in bytes */
1bd51a4c
IH
555 bool item_is_utf8 = FALSE;
556 bool targ_is_utf8 = FALSE;
bfed75c6 557 const char *fmt;
74e0ddf7 558 MAGIC *mg = NULL;
4ff700b9
DM
559 U8 *source; /* source of bytes to append */
560 STRLEN to_copy; /* how may bytes to append */
ea60cfe8 561 char trans; /* what chars to translate */
74e0ddf7 562
3808a683 563 mg = doparseform(tmpForm);
a0d0e21e 564
74e0ddf7 565 fpc = (U32*)mg->mg_ptr;
3808a683
DM
566 /* the actual string the format was compiled from.
567 * with overload etc, this may not match tmpForm */
568 formsv = mg->mg_obj;
569
74e0ddf7 570
3280af22 571 SvPV_force(PL_formtarget, len);
3808a683 572 if (SvTAINTED(tmpForm) || SvTAINTED(formsv))
125b9982 573 SvTAINTED_on(PL_formtarget);
1bd51a4c
IH
574 if (DO_UTF8(PL_formtarget))
575 targ_is_utf8 = TRUE;
26e935cf
DM
576 linemax = (SvCUR(formsv) * (IN_BYTES ? 1 : 3) + 1);
577 t = SvGROW(PL_formtarget, len + linemax + 1);
578 /* XXX from now onwards, SvCUR(PL_formtarget) is invalid */
a0d0e21e 579 t += len;
3808a683 580 f = SvPV_const(formsv, len);
a0d0e21e
LW
581
582 for (;;) {
583 DEBUG_f( {
bfed75c6 584 const char *name = "???";
a0d0e21e
LW
585 arg = -1;
586 switch (*fpc) {
587 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
588 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
589 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
590 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
591 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
592
593 case FF_CHECKNL: name = "CHECKNL"; break;
594 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
595 case FF_SPACE: name = "SPACE"; break;
596 case FF_HALFSPACE: name = "HALFSPACE"; break;
597 case FF_ITEM: name = "ITEM"; break;
598 case FF_CHOP: name = "CHOP"; break;
599 case FF_LINEGLOB: name = "LINEGLOB"; break;
600 case FF_NEWLINE: name = "NEWLINE"; break;
601 case FF_MORE: name = "MORE"; break;
602 case FF_LINEMARK: name = "LINEMARK"; break;
603 case FF_END: name = "END"; break;
bfed75c6 604 case FF_0DECIMAL: name = "0DECIMAL"; break;
a1b95068 605 case FF_LINESNGL: name = "LINESNGL"; break;
a0d0e21e
LW
606 }
607 if (arg >= 0)
bf49b057 608 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
a0d0e21e 609 else
bf49b057 610 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
5f80b19c 611 } );
a0d0e21e
LW
612 switch (*fpc++) {
613 case FF_LINEMARK:
f5ada144 614 linemark = t - SvPVX(PL_formtarget);
a0d0e21e
LW
615 lines++;
616 gotsome = FALSE;
617 break;
618
619 case FF_LITERAL:
ea60cfe8
DM
620 to_copy = *fpc++;
621 source = (U8 *)f;
622 f += to_copy;
623 trans = '~';
75645721 624 item_is_utf8 = targ_is_utf8 ? !!DO_UTF8(formsv) : !!SvUTF8(formsv);
ea60cfe8 625 goto append;
a0d0e21e
LW
626
627 case FF_SKIP:
628 f += *fpc++;
629 break;
630
631 case FF_FETCH:
632 arg = *fpc++;
633 f += arg;
634 fieldsize = arg;
635
636 if (MARK < SP)
637 sv = *++MARK;
638 else {
3280af22 639 sv = &PL_sv_no;
a2a5de95 640 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
a0d0e21e 641 }
125b9982
NT
642 if (SvTAINTED(sv))
643 SvTAINTED_on(PL_formtarget);
a0d0e21e
LW
644 break;
645
646 case FF_CHECKNL:
5a34cab7
NC
647 {
648 const char *send;
649 const char *s = item = SvPV_const(sv, len);
650 itemsize = len;
651 if (DO_UTF8(sv)) {
652 itemsize = sv_len_utf8(sv);
653 if (itemsize != (I32)len) {
654 I32 itembytes;
655 if (itemsize > fieldsize) {
656 itemsize = fieldsize;
657 itembytes = itemsize;
658 sv_pos_u2b(sv, &itembytes, 0);
659 }
660 else
661 itembytes = len;
662 send = chophere = s + itembytes;
663 while (s < send) {
664 if (*s & ~31)
665 gotsome = TRUE;
666 else if (*s == '\n')
667 break;
668 s++;
669 }
670 item_is_utf8 = TRUE;
671 itemsize = s - item;
672 sv_pos_b2u(sv, &itemsize);
673 break;
a0ed51b3 674 }
a0ed51b3 675 }
5a34cab7
NC
676 item_is_utf8 = FALSE;
677 if (itemsize > fieldsize)
678 itemsize = fieldsize;
679 send = chophere = s + itemsize;
680 while (s < send) {
681 if (*s & ~31)
682 gotsome = TRUE;
683 else if (*s == '\n')
684 break;
685 s++;
686 }
687 itemsize = s - item;
688 break;
a0ed51b3 689 }
a0d0e21e
LW
690
691 case FF_CHECKCHOP:
5a34cab7
NC
692 {
693 const char *s = item = SvPV_const(sv, len);
694 itemsize = len;
695 if (DO_UTF8(sv)) {
696 itemsize = sv_len_utf8(sv);
697 if (itemsize != (I32)len) {
698 I32 itembytes;
699 if (itemsize <= fieldsize) {
700 const char *send = chophere = s + itemsize;
701 while (s < send) {
702 if (*s == '\r') {
703 itemsize = s - item;
a0ed51b3 704 chophere = s;
a0ed51b3 705 break;
5a34cab7
NC
706 }
707 if (*s++ & ~31)
a0ed51b3 708 gotsome = TRUE;
a0ed51b3 709 }
a0ed51b3 710 }
5a34cab7
NC
711 else {
712 const char *send;
713 itemsize = fieldsize;
714 itembytes = itemsize;
715 sv_pos_u2b(sv, &itembytes, 0);
716 send = chophere = s + itembytes;
717 while (s < send || (s == send && isSPACE(*s))) {
718 if (isSPACE(*s)) {
719 if (chopspace)
720 chophere = s;
721 if (*s == '\r')
722 break;
723 }
724 else {
725 if (*s & ~31)
726 gotsome = TRUE;
727 if (strchr(PL_chopset, *s))
728 chophere = s + 1;
729 }
730 s++;
731 }
732 itemsize = chophere - item;
733 sv_pos_b2u(sv, &itemsize);
734 }
735 item_is_utf8 = TRUE;
a0d0e21e
LW
736 break;
737 }
a0d0e21e 738 }
5a34cab7
NC
739 item_is_utf8 = FALSE;
740 if (itemsize <= fieldsize) {
741 const char *const send = chophere = s + itemsize;
742 while (s < send) {
743 if (*s == '\r') {
744 itemsize = s - item;
a0d0e21e 745 chophere = s;
a0d0e21e 746 break;
5a34cab7
NC
747 }
748 if (*s++ & ~31)
a0d0e21e 749 gotsome = TRUE;
a0d0e21e 750 }
a0d0e21e 751 }
5a34cab7
NC
752 else {
753 const char *send;
754 itemsize = fieldsize;
755 send = chophere = s + itemsize;
756 while (s < send || (s == send && isSPACE(*s))) {
757 if (isSPACE(*s)) {
758 if (chopspace)
759 chophere = s;
760 if (*s == '\r')
761 break;
762 }
763 else {
764 if (*s & ~31)
765 gotsome = TRUE;
766 if (strchr(PL_chopset, *s))
767 chophere = s + 1;
768 }
769 s++;
770 }
771 itemsize = chophere - item;
772 }
773 break;
a0d0e21e 774 }
a0d0e21e
LW
775
776 case FF_SPACE:
777 arg = fieldsize - itemsize;
778 if (arg) {
779 fieldsize -= arg;
780 while (arg-- > 0)
781 *t++ = ' ';
782 }
783 break;
784
785 case FF_HALFSPACE:
786 arg = fieldsize - itemsize;
787 if (arg) {
788 arg /= 2;
789 fieldsize -= arg;
790 while (arg-- > 0)
791 *t++ = ' ';
792 }
793 break;
794
795 case FF_ITEM:
8aa7beb6
DM
796 to_copy = itemsize;
797 source = (U8 *)item;
798 trans = 1;
799 if (item_is_utf8) {
800 /* convert to_copy from chars to bytes */
801 U8 *s = source;
802 while (to_copy--)
803 s += UTF8SKIP(s);
804 to_copy = s - source;
a0d0e21e 805 }
8aa7beb6 806 goto append;
a0d0e21e
LW
807
808 case FF_CHOP:
5a34cab7
NC
809 {
810 const char *s = chophere;
811 if (chopspace) {
af68e756 812 while (isSPACE(*s))
5a34cab7
NC
813 s++;
814 }
815 sv_chop(sv,s);
816 SvSETMAGIC(sv);
817 break;
a0d0e21e 818 }
a0d0e21e 819
a1b95068
WL
820 case FF_LINESNGL:
821 chopspace = 0;
a0d0e21e 822 case FF_LINEGLOB:
5a34cab7 823 {
e32383e2 824 const bool oneline = fpc[-1] == FF_LINESNGL;
5a34cab7 825 const char *s = item = SvPV_const(sv, len);
7440a75b 826 const char *const send = s + len;
7440a75b 827
f3f2f1a3 828 item_is_utf8 = DO_UTF8(sv);
a1137ee5 829 if (!len)
7440a75b 830 break;
ea60cfe8 831 trans = 0;
0d21cefe 832 gotsome = TRUE;
a1137ee5 833 chophere = s + len;
4ff700b9
DM
834 source = (U8 *) s;
835 to_copy = len;
0d21cefe
DM
836 while (s < send) {
837 if (*s++ == '\n') {
838 if (oneline) {
839 to_copy = s - SvPVX_const(sv) - 1;
840 chophere = s;
841 break;
842 } else {
843 if (s == send) {
0d21cefe
DM
844 to_copy--;
845 } else
846 lines++;
1bd51a4c 847 }
a0d0e21e 848 }
0d21cefe 849 }
a2c0032b
DM
850 }
851
ea60cfe8
DM
852 append:
853 /* append to_copy bytes from source to PL_formstring.
854 * item_is_utf8 implies source is utf8.
855 * if trans, translate certain characters during the copy */
a2c0032b
DM
856 {
857 U8 *tmp = NULL;
26e935cf 858 STRLEN grow = 0;
0325ce87
DM
859
860 SvCUR_set(PL_formtarget,
861 t - SvPVX_const(PL_formtarget));
862
0d21cefe
DM
863 if (targ_is_utf8 && !item_is_utf8) {
864 source = tmp = bytes_to_utf8(source, &to_copy);
0d21cefe
DM
865 } else {
866 if (item_is_utf8 && !targ_is_utf8) {
f5ada144 867 U8 *s;
0d21cefe 868 /* Upgrade targ to UTF8, and then we reduce it to
0325ce87
DM
869 a problem we have a simple solution for.
870 Don't need get magic. */
0d21cefe 871 sv_utf8_upgrade_nomg(PL_formtarget);
0325ce87 872 targ_is_utf8 = TRUE;
f5ada144
DM
873 /* re-calculate linemark */
874 s = (U8*)SvPVX(PL_formtarget);
26e935cf
DM
875 /* the bytes we initially allocated to append the
876 * whole line may have been gobbled up during the
877 * upgrade, so allocate a whole new line's worth
878 * for safety */
879 grow = linemax;
f5ada144
DM
880 while (linemark--)
881 s += UTF8SKIP(s);
882 linemark = s - (U8*)SvPVX(PL_formtarget);
e8e72d41 883 }
0d21cefe
DM
884 /* Easy. They agree. */
885 assert (item_is_utf8 == targ_is_utf8);
886 }
26e935cf
DM
887 if (!trans)
888 /* @* and ^* are the only things that can exceed
889 * the linemax, so grow by the output size, plus
890 * a whole new form's worth in case of any further
891 * output */
892 grow = linemax + to_copy;
893 if (grow)
894 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + grow + 1);
0d21cefe
DM
895 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
896
897 Copy(source, t, to_copy, char);
ea60cfe8 898 if (trans) {
8aa7beb6
DM
899 /* blank out ~ or control chars, depending on trans.
900 * works on bytes not chars, so relies on not
901 * matching utf8 continuation bytes */
ea60cfe8
DM
902 U8 *s = (U8*)t;
903 U8 *send = s + to_copy;
904 while (s < send) {
8aa7beb6
DM
905 const int ch = *s;
906 if (trans == '~' ? (ch == '~') :
907#ifdef EBCDIC
908 iscntrl(ch)
909#else
910 (!(ch & ~31))
911#endif
912 )
ea60cfe8
DM
913 *s = ' ';
914 s++;
915 }
916 }
917
0d21cefe
DM
918 t += to_copy;
919 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
a1137ee5 920 if (tmp)
0d21cefe 921 Safefree(tmp);
5a34cab7 922 break;
a0d0e21e 923 }
a0d0e21e 924
a1b95068
WL
925 case FF_0DECIMAL:
926 arg = *fpc++;
927#if defined(USE_LONG_DOUBLE)
10edeb5d 928 fmt = (const char *)
a701009a 929 ((arg & FORM_NUM_POINT) ?
10edeb5d 930 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
a1b95068 931#else
10edeb5d 932 fmt = (const char *)
a701009a 933 ((arg & FORM_NUM_POINT) ?
10edeb5d 934 "%#0*.*f" : "%0*.*f");
a1b95068
WL
935#endif
936 goto ff_dec;
a0d0e21e 937 case FF_DECIMAL:
a0d0e21e 938 arg = *fpc++;
65202027 939#if defined(USE_LONG_DOUBLE)
10edeb5d 940 fmt = (const char *)
a701009a 941 ((arg & FORM_NUM_POINT) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
65202027 942#else
10edeb5d 943 fmt = (const char *)
a701009a 944 ((arg & FORM_NUM_POINT) ? "%#*.*f" : "%*.*f");
65202027 945#endif
a1b95068 946 ff_dec:
784707d5
JP
947 /* If the field is marked with ^ and the value is undefined,
948 blank it out. */
a701009a 949 if ((arg & FORM_NUM_BLANK) && !SvOK(sv)) {
784707d5
JP
950 arg = fieldsize;
951 while (arg--)
952 *t++ = ' ';
953 break;
954 }
955 gotsome = TRUE;
956 value = SvNV(sv);
a1b95068 957 /* overflow evidence */
bfed75c6 958 if (num_overflow(value, fieldsize, arg)) {
a1b95068
WL
959 arg = fieldsize;
960 while (arg--)
961 *t++ = '#';
962 break;
963 }
784707d5
JP
964 /* Formats aren't yet marked for locales, so assume "yes". */
965 {
966 STORE_NUMERIC_STANDARD_SET_LOCAL();
a701009a
DM
967 arg &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
968 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg, value);
784707d5
JP
969 RESTORE_NUMERIC_STANDARD();
970 }
971 t += fieldsize;
972 break;
a1b95068 973
a0d0e21e
LW
974 case FF_NEWLINE:
975 f++;
f5ada144 976 while (t-- > (SvPVX(PL_formtarget) + linemark) && *t == ' ') ;
a0d0e21e
LW
977 t++;
978 *t++ = '\n';
979 break;
980
981 case FF_BLANK:
982 arg = *fpc++;
983 if (gotsome) {
984 if (arg) { /* repeat until fields exhausted? */
11f9eeaf
DM
985 fpc--;
986 goto end;
a0d0e21e
LW
987 }
988 }
989 else {
f5ada144 990 t = SvPVX(PL_formtarget) + linemark;
a0d0e21e
LW
991 lines--;
992 }
993 break;
994
995 case FF_MORE:
5a34cab7
NC
996 {
997 const char *s = chophere;
998 const char *send = item + len;
999 if (chopspace) {
af68e756 1000 while (isSPACE(*s) && (s < send))
5a34cab7 1001 s++;
a0d0e21e 1002 }
5a34cab7
NC
1003 if (s < send) {
1004 char *s1;
1005 arg = fieldsize - itemsize;
1006 if (arg) {
1007 fieldsize -= arg;
1008 while (arg-- > 0)
1009 *t++ = ' ';
1010 }
1011 s1 = t - 3;
1012 if (strnEQ(s1," ",3)) {
1013 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
1014 s1--;
1015 }
1016 *s1++ = '.';
1017 *s1++ = '.';
1018 *s1++ = '.';
a0d0e21e 1019 }
5a34cab7 1020 break;
a0d0e21e 1021 }
a0d0e21e 1022 case FF_END:
11f9eeaf 1023 end:
bf2bec63 1024 assert(t < SvPVX_const(PL_formtarget) + SvLEN(PL_formtarget));
a0d0e21e 1025 *t = '\0';
b15aece3 1026 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1bd51a4c
IH
1027 if (targ_is_utf8)
1028 SvUTF8_on(PL_formtarget);
3280af22 1029 FmLINES(PL_formtarget) += lines;
a0d0e21e 1030 SP = ORIGMARK;
11f9eeaf
DM
1031 if (fpc[-1] == FF_BLANK)
1032 RETURNOP(cLISTOP->op_first);
1033 else
1034 RETPUSHYES;
a0d0e21e
LW
1035 }
1036 }
1037}
1038
1039PP(pp_grepstart)
1040{
27da23d5 1041 dVAR; dSP;
a0d0e21e
LW
1042 SV *src;
1043
3280af22 1044 if (PL_stack_base + *PL_markstack_ptr == SP) {
a0d0e21e 1045 (void)POPMARK;
54310121 1046 if (GIMME_V == G_SCALAR)
6e449a3a 1047 mXPUSHi(0);
533c011a 1048 RETURNOP(PL_op->op_next->op_next);
a0d0e21e 1049 }
3280af22 1050 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
897d3989
NC
1051 Perl_pp_pushmark(aTHX); /* push dst */
1052 Perl_pp_pushmark(aTHX); /* push src */
d343c3ef 1053 ENTER_with_name("grep"); /* enter outer scope */
a0d0e21e
LW
1054
1055 SAVETMPS;
59f00321
RGS
1056 if (PL_op->op_private & OPpGREP_LEX)
1057 SAVESPTR(PAD_SVl(PL_op->op_targ));
1058 else
1059 SAVE_DEFSV;
d343c3ef 1060 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1061 SAVEVPTR(PL_curpm);
a0d0e21e 1062
3280af22 1063 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 1064 SvTEMP_off(src);
59f00321
RGS
1065 if (PL_op->op_private & OPpGREP_LEX)
1066 PAD_SVl(PL_op->op_targ) = src;
1067 else
414bf5ae 1068 DEFSV_set(src);
a0d0e21e
LW
1069
1070 PUTBACK;
533c011a 1071 if (PL_op->op_type == OP_MAPSTART)
897d3989 1072 Perl_pp_pushmark(aTHX); /* push top */
533c011a 1073 return ((LOGOP*)PL_op->op_next)->op_other;
a0d0e21e
LW
1074}
1075
a0d0e21e
LW
1076PP(pp_mapwhile)
1077{
27da23d5 1078 dVAR; dSP;
f54cb97a 1079 const I32 gimme = GIMME_V;
544f3153 1080 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
a0d0e21e
LW
1081 I32 count;
1082 I32 shift;
1083 SV** src;
ac27b0f5 1084 SV** dst;
a0d0e21e 1085
544f3153 1086 /* first, move source pointer to the next item in the source list */
3280af22 1087 ++PL_markstack_ptr[-1];
544f3153
GS
1088
1089 /* if there are new items, push them into the destination list */
4c90a460 1090 if (items && gimme != G_VOID) {
544f3153
GS
1091 /* might need to make room back there first */
1092 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1093 /* XXX this implementation is very pessimal because the stack
1094 * is repeatedly extended for every set of items. Is possible
1095 * to do this without any stack extension or copying at all
1096 * by maintaining a separate list over which the map iterates
18ef8bea 1097 * (like foreach does). --gsar */
544f3153
GS
1098
1099 /* everything in the stack after the destination list moves
1100 * towards the end the stack by the amount of room needed */
1101 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1102
1103 /* items to shift up (accounting for the moved source pointer) */
1104 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
18ef8bea
BT
1105
1106 /* This optimization is by Ben Tilly and it does
1107 * things differently from what Sarathy (gsar)
1108 * is describing. The downside of this optimization is
1109 * that leaves "holes" (uninitialized and hopefully unused areas)
1110 * to the Perl stack, but on the other hand this
1111 * shouldn't be a problem. If Sarathy's idea gets
1112 * implemented, this optimization should become
1113 * irrelevant. --jhi */
1114 if (shift < count)
1115 shift = count; /* Avoid shifting too often --Ben Tilly */
bfed75c6 1116
924508f0
GS
1117 EXTEND(SP,shift);
1118 src = SP;
1119 dst = (SP += shift);
3280af22
NIS
1120 PL_markstack_ptr[-1] += shift;
1121 *PL_markstack_ptr += shift;
544f3153 1122 while (count--)
a0d0e21e
LW
1123 *dst-- = *src--;
1124 }
544f3153 1125 /* copy the new items down to the destination list */
ac27b0f5 1126 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
22023b26 1127 if (gimme == G_ARRAY) {
b2a2a901
DM
1128 /* add returned items to the collection (making mortal copies
1129 * if necessary), then clear the current temps stack frame
1130 * *except* for those items. We do this splicing the items
1131 * into the start of the tmps frame (so some items may be on
59d53fd6 1132 * the tmps stack twice), then moving PL_tmps_floor above
b2a2a901
DM
1133 * them, then freeing the frame. That way, the only tmps that
1134 * accumulate over iterations are the return values for map.
1135 * We have to do to this way so that everything gets correctly
1136 * freed if we die during the map.
1137 */
1138 I32 tmpsbase;
1139 I32 i = items;
1140 /* make space for the slice */
1141 EXTEND_MORTAL(items);
1142 tmpsbase = PL_tmps_floor + 1;
1143 Move(PL_tmps_stack + tmpsbase,
1144 PL_tmps_stack + tmpsbase + items,
1145 PL_tmps_ix - PL_tmps_floor,
1146 SV*);
1147 PL_tmps_ix += items;
1148
1149 while (i-- > 0) {
1150 SV *sv = POPs;
1151 if (!SvTEMP(sv))
1152 sv = sv_mortalcopy(sv);
1153 *dst-- = sv;
1154 PL_tmps_stack[tmpsbase++] = SvREFCNT_inc_simple(sv);
1155 }
1156 /* clear the stack frame except for the items */
1157 PL_tmps_floor += items;
1158 FREETMPS;
1159 /* FREETMPS may have cleared the TEMP flag on some of the items */
1160 i = items;
1161 while (i-- > 0)
1162 SvTEMP_on(PL_tmps_stack[--tmpsbase]);
22023b26 1163 }
bfed75c6 1164 else {
22023b26
TP
1165 /* scalar context: we don't care about which values map returns
1166 * (we use undef here). And so we certainly don't want to do mortal
1167 * copies of meaningless values. */
1168 while (items-- > 0) {
b988aa42 1169 (void)POPs;
22023b26
TP
1170 *dst-- = &PL_sv_undef;
1171 }
b2a2a901 1172 FREETMPS;
22023b26 1173 }
a0d0e21e 1174 }
b2a2a901
DM
1175 else {
1176 FREETMPS;
1177 }
d343c3ef 1178 LEAVE_with_name("grep_item"); /* exit inner scope */
a0d0e21e
LW
1179
1180 /* All done yet? */
3280af22 1181 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
a0d0e21e
LW
1182
1183 (void)POPMARK; /* pop top */
d343c3ef 1184 LEAVE_with_name("grep"); /* exit outer scope */
a0d0e21e 1185 (void)POPMARK; /* pop src */
3280af22 1186 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1187 (void)POPMARK; /* pop dst */
3280af22 1188 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1189 if (gimme == G_SCALAR) {
7cc47870
RGS
1190 if (PL_op->op_private & OPpGREP_LEX) {
1191 SV* sv = sv_newmortal();
1192 sv_setiv(sv, items);
1193 PUSHs(sv);
1194 }
1195 else {
1196 dTARGET;
1197 XPUSHi(items);
1198 }
a0d0e21e 1199 }
54310121 1200 else if (gimme == G_ARRAY)
1201 SP += items;
a0d0e21e
LW
1202 RETURN;
1203 }
1204 else {
1205 SV *src;
1206
d343c3ef 1207 ENTER_with_name("grep_item"); /* enter inner scope */
7766f137 1208 SAVEVPTR(PL_curpm);
a0d0e21e 1209
544f3153 1210 /* set $_ to the new source item */
3280af22 1211 src = PL_stack_base[PL_markstack_ptr[-1]];
a0d0e21e 1212 SvTEMP_off(src);
59f00321
RGS
1213 if (PL_op->op_private & OPpGREP_LEX)
1214 PAD_SVl(PL_op->op_targ) = src;
1215 else
414bf5ae 1216 DEFSV_set(src);
a0d0e21e
LW
1217
1218 RETURNOP(cLOGOP->op_other);
1219 }
1220}
1221
a0d0e21e
LW
1222/* Range stuff. */
1223
1224PP(pp_range)
1225{
97aff369 1226 dVAR;
a0d0e21e 1227 if (GIMME == G_ARRAY)
1a67a97c 1228 return NORMAL;
538573f7 1229 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1a67a97c 1230 return cLOGOP->op_other;
538573f7 1231 else
1a67a97c 1232 return NORMAL;
a0d0e21e
LW
1233}
1234
1235PP(pp_flip)
1236{
97aff369 1237 dVAR;
39644a26 1238 dSP;
a0d0e21e
LW
1239
1240 if (GIMME == G_ARRAY) {
1a67a97c 1241 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1242 }
1243 else {
1244 dTOPss;
44f8325f 1245 SV * const targ = PAD_SV(PL_op->op_targ);
bfed75c6 1246 int flip = 0;
790090df 1247
bfed75c6 1248 if (PL_op->op_private & OPpFLIP_LINENUM) {
4e3399f9
YST
1249 if (GvIO(PL_last_in_gv)) {
1250 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1251 }
1252 else {
fafc274c 1253 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
44f8325f
AL
1254 if (gv && GvSV(gv))
1255 flip = SvIV(sv) == SvIV(GvSV(gv));
4e3399f9 1256 }
bfed75c6
AL
1257 } else {
1258 flip = SvTRUE(sv);
1259 }
1260 if (flip) {
a0d0e21e 1261 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
533c011a 1262 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1263 sv_setiv(targ, 1);
3e3baf6d 1264 SETs(targ);
a0d0e21e
LW
1265 RETURN;
1266 }
1267 else {
1268 sv_setiv(targ, 0);
924508f0 1269 SP--;
1a67a97c 1270 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1271 }
1272 }
76f68e9b 1273 sv_setpvs(TARG, "");
a0d0e21e
LW
1274 SETs(targ);
1275 RETURN;
1276 }
1277}
1278
8e9bbdb9
RGS
1279/* This code tries to decide if "$left .. $right" should use the
1280 magical string increment, or if the range is numeric (we make
1281 an exception for .."0" [#18165]). AMS 20021031. */
1282
1283#define RANGE_IS_NUMERIC(left,right) ( \
b0e74086
RGS
1284 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1285 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
e0ab1c0e 1286 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
b15aece3 1287 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
e0ab1c0e 1288 && (!SvOK(right) || looks_like_number(right))))
8e9bbdb9 1289
a0d0e21e
LW
1290PP(pp_flop)
1291{
97aff369 1292 dVAR; dSP;
a0d0e21e
LW
1293
1294 if (GIMME == G_ARRAY) {
1295 dPOPPOPssrl;
86cb7173 1296
5b295bef
RD
1297 SvGETMAGIC(left);
1298 SvGETMAGIC(right);
a0d0e21e 1299
8e9bbdb9 1300 if (RANGE_IS_NUMERIC(left,right)) {
901017d6
AL
1301 register IV i, j;
1302 IV max;
f52e41ad
FC
1303 if ((SvOK(left) && SvNV_nomg(left) < IV_MIN) ||
1304 (SvOK(right) && SvNV_nomg(right) > IV_MAX))
d470f89e 1305 DIE(aTHX_ "Range iterator outside integer range");
f52e41ad
FC
1306 i = SvIV_nomg(left);
1307 max = SvIV_nomg(right);
bbce6d69 1308 if (max >= i) {
c1ab3db2
AK
1309 j = max - i + 1;
1310 EXTEND_MORTAL(j);
1311 EXTEND(SP, j);
bbce6d69 1312 }
c1ab3db2
AK
1313 else
1314 j = 0;
1315 while (j--) {
901017d6 1316 SV * const sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
1317 PUSHs(sv);
1318 }
1319 }
1320 else {
3c323193
FC
1321 STRLEN len, llen;
1322 const char * const lpv = SvPV_nomg_const(left, llen);
f52e41ad 1323 const char * const tmps = SvPV_nomg_const(right, len);
a0d0e21e 1324
3c323193 1325 SV *sv = newSVpvn_flags(lpv, llen, SvUTF8(left)|SVs_TEMP);
89ea2908 1326 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
a0d0e21e 1327 XPUSHs(sv);
b15aece3 1328 if (strEQ(SvPVX_const(sv),tmps))
89ea2908 1329 break;
a0d0e21e
LW
1330 sv = sv_2mortal(newSVsv(sv));
1331 sv_inc(sv);
1332 }
a0d0e21e
LW
1333 }
1334 }
1335 else {
1336 dTOPss;
901017d6 1337 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
4e3399f9 1338 int flop = 0;
a0d0e21e 1339 sv_inc(targ);
4e3399f9
YST
1340
1341 if (PL_op->op_private & OPpFLIP_LINENUM) {
1342 if (GvIO(PL_last_in_gv)) {
1343 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1344 }
1345 else {
fafc274c 1346 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
4e3399f9
YST
1347 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1348 }
1349 }
1350 else {
1351 flop = SvTRUE(sv);
1352 }
1353
1354 if (flop) {
a0d0e21e 1355 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
396482e1 1356 sv_catpvs(targ, "E0");
a0d0e21e
LW
1357 }
1358 SETs(targ);
1359 }
1360
1361 RETURN;
1362}
1363
1364/* Control. */
1365
27da23d5 1366static const char * const context_name[] = {
515afda2 1367 "pseudo-block",
f31522f3 1368 NULL, /* CXt_WHEN never actually needs "block" */
76753e7f 1369 NULL, /* CXt_BLOCK never actually needs "block" */
f31522f3 1370 NULL, /* CXt_GIVEN never actually needs "block" */
76753e7f
NC
1371 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1372 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1373 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1374 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
515afda2 1375 "subroutine",
76753e7f 1376 "format",
515afda2 1377 "eval",
515afda2 1378 "substitution",
515afda2
NC
1379};
1380
76e3520e 1381STATIC I32
5db1eb8d 1382S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags)
a0d0e21e 1383{
97aff369 1384 dVAR;
a0d0e21e 1385 register I32 i;
a0d0e21e 1386
7918f24d
NC
1387 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1388
a0d0e21e 1389 for (i = cxstack_ix; i >= 0; i--) {
901017d6 1390 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1391 switch (CxTYPE(cx)) {
a0d0e21e 1392 case CXt_SUBST:
a0d0e21e 1393 case CXt_SUB:
7766f137 1394 case CXt_FORMAT:
a0d0e21e 1395 case CXt_EVAL:
0a753a76 1396 case CXt_NULL:
dcbac5bb 1397 /* diag_listed_as: Exiting subroutine via %s */
a2a5de95
NC
1398 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1399 context_name[CxTYPE(cx)], OP_NAME(PL_op));
515afda2
NC
1400 if (CxTYPE(cx) == CXt_NULL)
1401 return -1;
1402 break;
c6fdafd0 1403 case CXt_LOOP_LAZYIV:
d01136d6 1404 case CXt_LOOP_LAZYSV:
3b719c58
NC
1405 case CXt_LOOP_FOR:
1406 case CXt_LOOP_PLAIN:
7e8f1eac 1407 {
5db1eb8d
BF
1408 STRLEN cx_label_len = 0;
1409 U32 cx_label_flags = 0;
1410 const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags);
1411 if (!cx_label || !(
1412 ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
1413 (flags & SVf_UTF8)
1414 ? (bytes_cmp_utf8(
1415 (const U8*)cx_label, cx_label_len,
1416 (const U8*)label, len) == 0)
1417 : (bytes_cmp_utf8(
1418 (const U8*)label, len,
1419 (const U8*)cx_label, cx_label_len) == 0)
eade7155
BF
1420 : (len == cx_label_len && ((cx_label == label)
1421 || memEQ(cx_label, label, len))) )) {
1c98cc53 1422 DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
7e8f1eac 1423 (long)i, cx_label));
a0d0e21e
LW
1424 continue;
1425 }
1c98cc53 1426 DEBUG_l( Perl_deb(aTHX_ "(poptolabel(): found label at cx=%ld %s)\n", (long)i, label));
a0d0e21e 1427 return i;
7e8f1eac 1428 }
a0d0e21e
LW
1429 }
1430 }
1431 return i;
1432}
1433
0d863452
RH
1434
1435
e50aee73 1436I32
864dbfa3 1437Perl_dowantarray(pTHX)
e50aee73 1438{
97aff369 1439 dVAR;
f54cb97a 1440 const I32 gimme = block_gimme();
54310121 1441 return (gimme == G_VOID) ? G_SCALAR : gimme;
1442}
1443
1444I32
864dbfa3 1445Perl_block_gimme(pTHX)
54310121 1446{
97aff369 1447 dVAR;
06b5626a 1448 const I32 cxix = dopoptosub(cxstack_ix);
e50aee73 1449 if (cxix < 0)
46fc3d4c 1450 return G_VOID;
e50aee73 1451
54310121 1452 switch (cxstack[cxix].blk_gimme) {
d2719217
GS
1453 case G_VOID:
1454 return G_VOID;
54310121 1455 case G_SCALAR:
e50aee73 1456 return G_SCALAR;
54310121 1457 case G_ARRAY:
1458 return G_ARRAY;
1459 default:
cea2e8a9 1460 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
d2719217
GS
1461 /* NOTREACHED */
1462 return 0;
54310121 1463 }
e50aee73
AD
1464}
1465
78f9721b
SM
1466I32
1467Perl_is_lvalue_sub(pTHX)
1468{
97aff369 1469 dVAR;
06b5626a 1470 const I32 cxix = dopoptosub(cxstack_ix);
78f9721b
SM
1471 assert(cxix >= 0); /* We should only be called from inside subs */
1472
bafb2adc
NC
1473 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1474 return CxLVAL(cxstack + cxix);
78f9721b
SM
1475 else
1476 return 0;
1477}
1478
777d9014
FC
1479/* only used by PUSHSUB */
1480I32
1481Perl_was_lvalue_sub(pTHX)
1482{
1483 dVAR;
1484 const I32 cxix = dopoptosub(cxstack_ix-1);
1485 assert(cxix >= 0); /* We should only be called from inside subs */
1486
1487 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1488 return CxLVAL(cxstack + cxix);
1489 else
1490 return 0;
1491}
1492
76e3520e 1493STATIC I32
901017d6 1494S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
2c375eb9 1495{
97aff369 1496 dVAR;
a0d0e21e 1497 I32 i;
7918f24d
NC
1498
1499 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1500
a0d0e21e 1501 for (i = startingblock; i >= 0; i--) {
901017d6 1502 register const PERL_CONTEXT * const cx = &cxstk[i];
6b35e009 1503 switch (CxTYPE(cx)) {
a0d0e21e
LW
1504 default:
1505 continue;
1506 case CXt_EVAL:
1507 case CXt_SUB:
7766f137 1508 case CXt_FORMAT:
1c98cc53 1509 DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i));
a0d0e21e
LW
1510 return i;
1511 }
1512 }
1513 return i;
1514}
1515
76e3520e 1516STATIC I32
cea2e8a9 1517S_dopoptoeval(pTHX_ I32 startingblock)
a0d0e21e 1518{
97aff369 1519 dVAR;
a0d0e21e 1520 I32 i;
a0d0e21e 1521 for (i = startingblock; i >= 0; i--) {
06b5626a 1522 register const PERL_CONTEXT *cx = &cxstack[i];
6b35e009 1523 switch (CxTYPE(cx)) {
a0d0e21e
LW
1524 default:
1525 continue;
1526 case CXt_EVAL:
1c98cc53 1527 DEBUG_l( Perl_deb(aTHX_ "(dopoptoeval(): found eval at cx=%ld)\n", (long)i));
a0d0e21e
LW
1528 return i;
1529 }
1530 }
1531 return i;
1532}
1533
76e3520e 1534STATIC I32
cea2e8a9 1535S_dopoptoloop(pTHX_ I32 startingblock)
a0d0e21e 1536{
97aff369 1537 dVAR;
a0d0e21e 1538 I32 i;
a0d0e21e 1539 for (i = startingblock; i >= 0; i--) {
901017d6 1540 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1541 switch (CxTYPE(cx)) {
a0d0e21e 1542 case CXt_SUBST:
a0d0e21e 1543 case CXt_SUB:
7766f137 1544 case CXt_FORMAT:
a0d0e21e 1545 case CXt_EVAL:
0a753a76 1546 case CXt_NULL:
dcbac5bb 1547 /* diag_listed_as: Exiting subroutine via %s */
a2a5de95
NC
1548 Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1549 context_name[CxTYPE(cx)], OP_NAME(PL_op));
515afda2
NC
1550 if ((CxTYPE(cx)) == CXt_NULL)
1551 return -1;
1552 break;
c6fdafd0 1553 case CXt_LOOP_LAZYIV:
d01136d6 1554 case CXt_LOOP_LAZYSV:
3b719c58
NC
1555 case CXt_LOOP_FOR:
1556 case CXt_LOOP_PLAIN:
1c98cc53 1557 DEBUG_l( Perl_deb(aTHX_ "(dopoptoloop(): found loop at cx=%ld)\n", (long)i));
a0d0e21e
LW
1558 return i;
1559 }
1560 }
1561 return i;
1562}
1563
0d863452
RH
1564STATIC I32
1565S_dopoptogiven(pTHX_ I32 startingblock)
1566{
97aff369 1567 dVAR;
0d863452
RH
1568 I32 i;
1569 for (i = startingblock; i >= 0; i--) {
1570 register const PERL_CONTEXT *cx = &cxstack[i];
1571 switch (CxTYPE(cx)) {
1572 default:
1573 continue;
1574 case CXt_GIVEN:
1c98cc53 1575 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found given at cx=%ld)\n", (long)i));
0d863452 1576 return i;
3b719c58
NC
1577 case CXt_LOOP_PLAIN:
1578 assert(!CxFOREACHDEF(cx));
1579 break;
c6fdafd0 1580 case CXt_LOOP_LAZYIV:
d01136d6 1581 case CXt_LOOP_LAZYSV:
3b719c58 1582 case CXt_LOOP_FOR:
0d863452 1583 if (CxFOREACHDEF(cx)) {
1c98cc53 1584 DEBUG_l( Perl_deb(aTHX_ "(dopoptogiven(): found foreach at cx=%ld)\n", (long)i));
0d863452
RH
1585 return i;
1586 }
1587 }
1588 }
1589 return i;
1590}
1591
1592STATIC I32
1593S_dopoptowhen(pTHX_ I32 startingblock)
1594{
97aff369 1595 dVAR;
0d863452
RH
1596 I32 i;
1597 for (i = startingblock; i >= 0; i--) {
1598 register const PERL_CONTEXT *cx = &cxstack[i];
1599 switch (CxTYPE(cx)) {
1600 default:
1601 continue;
1602 case CXt_WHEN:
1c98cc53 1603 DEBUG_l( Perl_deb(aTHX_ "(dopoptowhen(): found when at cx=%ld)\n", (long)i));
0d863452
RH
1604 return i;
1605 }
1606 }
1607 return i;
1608}
1609
a0d0e21e 1610void
864dbfa3 1611Perl_dounwind(pTHX_ I32 cxix)
a0d0e21e 1612{
97aff369 1613 dVAR;
a0d0e21e
LW
1614 I32 optype;
1615
f144f1e3
DM
1616 if (!PL_curstackinfo) /* can happen if die during thread cloning */
1617 return;
1618
a0d0e21e 1619 while (cxstack_ix > cxix) {
b0d9ce38 1620 SV *sv;
06b5626a 1621 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1c98cc53 1622 DEBUG_CX("UNWIND"); \
a0d0e21e 1623 /* Note: we don't need to restore the base context info till the end. */
6b35e009 1624 switch (CxTYPE(cx)) {
c90c0ff4 1625 case CXt_SUBST:
1626 POPSUBST(cx);
1627 continue; /* not break */
a0d0e21e 1628 case CXt_SUB:
b0d9ce38
GS
1629 POPSUB(cx,sv);
1630 LEAVESUB(sv);
a0d0e21e
LW
1631 break;
1632 case CXt_EVAL:
1633 POPEVAL(cx);
1634 break;
c6fdafd0 1635 case CXt_LOOP_LAZYIV:
d01136d6 1636 case CXt_LOOP_LAZYSV:
3b719c58
NC
1637 case CXt_LOOP_FOR:
1638 case CXt_LOOP_PLAIN:
a0d0e21e
LW
1639 POPLOOP(cx);
1640 break;
0a753a76 1641 case CXt_NULL:
a0d0e21e 1642 break;
7766f137
GS
1643 case CXt_FORMAT:
1644 POPFORMAT(cx);
1645 break;
a0d0e21e 1646 }
c90c0ff4 1647 cxstack_ix--;
a0d0e21e 1648 }
1b6737cc 1649 PERL_UNUSED_VAR(optype);
a0d0e21e
LW
1650}
1651
5a844595
GS
1652void
1653Perl_qerror(pTHX_ SV *err)
1654{
97aff369 1655 dVAR;
7918f24d
NC
1656
1657 PERL_ARGS_ASSERT_QERROR;
1658
6b2fb389
DM
1659 if (PL_in_eval) {
1660 if (PL_in_eval & EVAL_KEEPERR) {
ecad31f0
BF
1661 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
1662 SVfARG(err));
6b2fb389
DM
1663 }
1664 else
1665 sv_catsv(ERRSV, err);
1666 }
5a844595
GS
1667 else if (PL_errors)
1668 sv_catsv(PL_errors, err);
1669 else
be2597df 1670 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
13765c85
DM
1671 if (PL_parser)
1672 ++PL_parser->error_count;
5a844595
GS
1673}
1674
bb4c52e0 1675void
c5df3096 1676Perl_die_unwind(pTHX_ SV *msv)
a0d0e21e 1677{
27da23d5 1678 dVAR;
c5df3096 1679 SV *exceptsv = sv_mortalcopy(msv);
96d9b9cd 1680 U8 in_eval = PL_in_eval;
c5df3096 1681 PERL_ARGS_ASSERT_DIE_UNWIND;
87582a92 1682
96d9b9cd 1683 if (in_eval) {
a0d0e21e 1684 I32 cxix;
a0d0e21e 1685 I32 gimme;
a0d0e21e 1686
22a30693
Z
1687 /*
1688 * Historically, perl used to set ERRSV ($@) early in the die
1689 * process and rely on it not getting clobbered during unwinding.
1690 * That sucked, because it was liable to get clobbered, so the
1691 * setting of ERRSV used to emit the exception from eval{} has
1692 * been moved to much later, after unwinding (see just before
1693 * JMPENV_JUMP below). However, some modules were relying on the
1694 * early setting, by examining $@ during unwinding to use it as
1695 * a flag indicating whether the current unwinding was caused by
1696 * an exception. It was never a reliable flag for that purpose,
1697 * being totally open to false positives even without actual
1698 * clobberage, but was useful enough for production code to
1699 * semantically rely on it.
1700 *
1701 * We'd like to have a proper introspective interface that
1702 * explicitly describes the reason for whatever unwinding
1703 * operations are currently in progress, so that those modules
1704 * work reliably and $@ isn't further overloaded. But we don't
1705 * have one yet. In its absence, as a stopgap measure, ERRSV is
1706 * now *additionally* set here, before unwinding, to serve as the
1707 * (unreliable) flag that it used to.
1708 *
1709 * This behaviour is temporary, and should be removed when a
1710 * proper way to detect exceptional unwinding has been developed.
1711 * As of 2010-12, the authors of modules relying on the hack
1712 * are aware of the issue, because the modules failed on
1713 * perls 5.13.{1..7} which had late setting of $@ without this
1714 * early-setting hack.
1715 */
1716 if (!(in_eval & EVAL_KEEPERR)) {
1717 SvTEMP_off(exceptsv);
1718 sv_setsv(ERRSV, exceptsv);
1719 }
1720
5a844595
GS
1721 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1722 && PL_curstackinfo->si_prev)
1723 {
bac4b2ad 1724 dounwind(-1);
d3acc0f7 1725 POPSTACK;
bac4b2ad 1726 }
e336de0d 1727
a0d0e21e
LW
1728 if (cxix >= 0) {
1729 I32 optype;
b6494f15 1730 SV *namesv;
35a4481c 1731 register PERL_CONTEXT *cx;
901017d6 1732 SV **newsp;
8f89e5a9
Z
1733 COP *oldcop;
1734 JMPENV *restartjmpenv;
1735 OP *restartop;
a0d0e21e
LW
1736
1737 if (cxix < cxstack_ix)
1738 dounwind(cxix);
1739
3280af22 1740 POPBLOCK(cx,PL_curpm);
6b35e009 1741 if (CxTYPE(cx) != CXt_EVAL) {
7d0994e0 1742 STRLEN msglen;
96d9b9cd 1743 const char* message = SvPVx_const(exceptsv, msglen);
10edeb5d 1744 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
bf49b057 1745 PerlIO_write(Perl_error_log, message, msglen);
a0d0e21e
LW
1746 my_exit(1);
1747 }
1748 POPEVAL(cx);
b6494f15 1749 namesv = cx->blk_eval.old_namesv;
8f89e5a9
Z
1750 oldcop = cx->blk_oldcop;
1751 restartjmpenv = cx->blk_eval.cur_top_env;
1752 restartop = cx->blk_eval.retop;
a0d0e21e
LW
1753
1754 if (gimme == G_SCALAR)
3280af22
NIS
1755 *++newsp = &PL_sv_undef;
1756 PL_stack_sp = newsp;
a0d0e21e
LW
1757
1758 LEAVE;
748a9306 1759
7fb6a879
GS
1760 /* LEAVE could clobber PL_curcop (see save_re_context())
1761 * XXX it might be better to find a way to avoid messing with
1762 * PL_curcop in save_re_context() instead, but this is a more
1763 * minimal fix --GSAR */
8f89e5a9 1764 PL_curcop = oldcop;
7fb6a879 1765
7a2e2cd6 1766 if (optype == OP_REQUIRE) {
b6494f15 1767 (void)hv_store(GvHVn(PL_incgv),
ecad31f0 1768 SvPVX_const(namesv),
c60dbbc3 1769 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
27bcc0a7 1770 &PL_sv_undef, 0);
27e90453
DM
1771 /* note that unlike pp_entereval, pp_require isn't
1772 * supposed to trap errors. So now that we've popped the
1773 * EVAL that pp_require pushed, and processed the error
1774 * message, rethrow the error */
ecad31f0
BF
1775 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
1776 SVfARG(exceptsv ? exceptsv : newSVpvs_flags("Unknown error\n",
1777 SVs_TEMP)));
7a2e2cd6 1778 }
c5df3096 1779 if (in_eval & EVAL_KEEPERR) {
ecad31f0
BF
1780 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
1781 SVfARG(exceptsv));
96d9b9cd
Z
1782 }
1783 else {
1784 sv_setsv(ERRSV, exceptsv);
1785 }
8f89e5a9
Z
1786 PL_restartjmpenv = restartjmpenv;
1787 PL_restartop = restartop;
bb4c52e0
GG
1788 JMPENV_JUMP(3);
1789 /* NOTREACHED */
a0d0e21e
LW
1790 }
1791 }
87582a92 1792
96d9b9cd 1793 write_to_stderr(exceptsv);
f86702cc 1794 my_failure_exit();
1795 /* NOTREACHED */
a0d0e21e
LW
1796}
1797
1798PP(pp_xor)
1799{
97aff369 1800 dVAR; dSP; dPOPTOPssrl;
a0d0e21e
LW
1801 if (SvTRUE(left) != SvTRUE(right))
1802 RETSETYES;
1803 else
1804 RETSETNO;
1805}
1806
8dff4fc5
BM
1807/*
1808=for apidoc caller_cx
1809
1810The XSUB-writer's equivalent of L<caller()|perlfunc/caller>. The
1811returned C<PERL_CONTEXT> structure can be interrogated to find all the
1812information returned to Perl by C<caller>. Note that XSUBs don't get a
1813stack frame, so C<caller_cx(0, NULL)> will return information for the
1814immediately-surrounding Perl code.
1815
1816This function skips over the automatic calls to C<&DB::sub> made on the
1817behalf of the debugger. If the stack frame requested was a sub called by
1818C<DB::sub>, the return value will be the frame for the call to
1819C<DB::sub>, since that has the correct line number/etc. for the call
1820site. If I<dbcxp> is non-C<NULL>, it will be set to a pointer to the
1821frame for the sub call itself.
1822
1823=cut
1824*/
1825
1826const PERL_CONTEXT *
1827Perl_caller_cx(pTHX_ I32 count, const PERL_CONTEXT **dbcxp)
a0d0e21e 1828{
a0d0e21e 1829 register I32 cxix = dopoptosub(cxstack_ix);
901017d6
AL
1830 register const PERL_CONTEXT *cx;
1831 register const PERL_CONTEXT *ccstack = cxstack;
1832 const PERL_SI *top_si = PL_curstackinfo;
27d41816 1833
a0d0e21e 1834 for (;;) {
2c375eb9
GS
1835 /* we may be in a higher stacklevel, so dig down deeper */
1836 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1837 top_si = top_si->si_prev;
1838 ccstack = top_si->si_cxstack;
1839 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1840 }
8dff4fc5
BM
1841 if (cxix < 0)
1842 return NULL;
f2a7f298
DG
1843 /* caller() should not report the automatic calls to &DB::sub */
1844 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
3280af22 1845 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
a0d0e21e
LW
1846 count++;
1847 if (!count--)
1848 break;
2c375eb9 1849 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1850 }
2c375eb9
GS
1851
1852 cx = &ccstack[cxix];
8dff4fc5
BM
1853 if (dbcxp) *dbcxp = cx;
1854
7766f137 1855 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
f54cb97a 1856 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
2c375eb9 1857 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
06a5b730 1858 field below is defined for any cx. */
f2a7f298
DG
1859 /* caller() should not report the automatic calls to &DB::sub */
1860 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
2c375eb9 1861 cx = &ccstack[dbcxix];
06a5b730 1862 }
1863
8dff4fc5
BM
1864 return cx;
1865}
1866
1867PP(pp_caller)
1868{
1869 dVAR;
1870 dSP;
1871 register const PERL_CONTEXT *cx;
1872 const PERL_CONTEXT *dbcx;
1873 I32 gimme;
d527ce7c 1874 const HEK *stash_hek;
8dff4fc5 1875 I32 count = 0;
ce0b554b 1876 bool has_arg = MAXARG && TOPs;
8dff4fc5 1877
ce0b554b
FC
1878 if (MAXARG) {
1879 if (has_arg)
8dff4fc5 1880 count = POPi;
ce0b554b
FC
1881 else (void)POPs;
1882 }
8dff4fc5 1883
ce0b554b 1884 cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx);
8dff4fc5
BM
1885 if (!cx) {
1886 if (GIMME != G_ARRAY) {
1887 EXTEND(SP, 1);
1888 RETPUSHUNDEF;
1889 }
1890 RETURN;
1891 }
1892
fb55feef 1893 DEBUG_CX("CALLER");
d0279c7c 1894 assert(CopSTASH(cx->blk_oldcop));
e7886211
FC
1895 stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV
1896 ? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop))
1897 : NULL;
a0d0e21e 1898 if (GIMME != G_ARRAY) {
27d41816 1899 EXTEND(SP, 1);
d527ce7c 1900 if (!stash_hek)
3280af22 1901 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1902 else {
1903 dTARGET;
d527ce7c 1904 sv_sethek(TARG, stash_hek);
49d8d3a1
MB
1905 PUSHs(TARG);
1906 }
a0d0e21e
LW
1907 RETURN;
1908 }
a0d0e21e 1909
b3ca2e83 1910 EXTEND(SP, 11);
27d41816 1911
d527ce7c 1912 if (!stash_hek)
3280af22 1913 PUSHs(&PL_sv_undef);
d527ce7c
BF
1914 else {
1915 dTARGET;
1916 sv_sethek(TARG, stash_hek);
1917 PUSHTARG;
1918 }
6e449a3a
MHM
1919 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1920 mPUSHi((I32)CopLINE(cx->blk_oldcop));
ce0b554b 1921 if (!has_arg)
a0d0e21e 1922 RETURN;
7766f137 1923 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
8dff4fc5 1924 GV * const cvgv = CvGV(dbcx->blk_sub.cv);
7766f137 1925 /* So is ccstack[dbcxix]. */
07b8c804 1926 if (isGV(cvgv)) {
561b68a9 1927 SV * const sv = newSV(0);
c445ea15 1928 gv_efullname3(sv, cvgv, NULL);
6e449a3a 1929 mPUSHs(sv);
bf38a478 1930 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804
RGS
1931 }
1932 else {
84bafc02 1933 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
bf38a478 1934 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804 1935 }
a0d0e21e
LW
1936 }
1937 else {
84bafc02 1938 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
6e449a3a 1939 mPUSHi(0);
a0d0e21e 1940 }
54310121 1941 gimme = (I32)cx->blk_gimme;
1942 if (gimme == G_VOID)
3280af22 1943 PUSHs(&PL_sv_undef);
54310121 1944 else
98625aca 1945 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
6b35e009 1946 if (CxTYPE(cx) == CXt_EVAL) {
811a4de9 1947 /* eval STRING */
85a64632 1948 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
4633a7c4 1949 PUSHs(cx->blk_eval.cur_text);
3280af22 1950 PUSHs(&PL_sv_no);
0f79a09d 1951 }
811a4de9 1952 /* require */
0f79a09d 1953 else if (cx->blk_eval.old_namesv) {
6e449a3a 1954 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
3280af22 1955 PUSHs(&PL_sv_yes);
06a5b730 1956 }
811a4de9
GS
1957 /* eval BLOCK (try blocks have old_namesv == 0) */
1958 else {
1959 PUSHs(&PL_sv_undef);
1960 PUSHs(&PL_sv_undef);
1961 }
4633a7c4 1962 }
a682de96
GS
1963 else {
1964 PUSHs(&PL_sv_undef);
1965 PUSHs(&PL_sv_undef);
1966 }
bafb2adc 1967 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
ed094faf 1968 && CopSTASH_eq(PL_curcop, PL_debstash))
4633a7c4 1969 {
66a1b24b
AL
1970 AV * const ary = cx->blk_sub.argarray;
1971 const int off = AvARRAY(ary) - AvALLOC(ary);
a0d0e21e 1972
e1a80902 1973 Perl_init_dbargs(aTHX);
a0d0e21e 1974
3280af22
NIS
1975 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1976 av_extend(PL_dbargs, AvFILLp(ary) + off);
1977 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1978 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e 1979 }
f3aa04c2
GS
1980 /* XXX only hints propagated via op_private are currently
1981 * visible (others are not easily accessible, since they
1982 * use the global PL_hints) */
6e449a3a 1983 mPUSHi(CopHINTS_get(cx->blk_oldcop));
e476b1b5
GS
1984 {
1985 SV * mask ;
72dc9ed5 1986 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
114bafba 1987
ac27b0f5 1988 if (old_warnings == pWARN_NONE ||
114bafba 1989 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
e476b1b5 1990 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
ac27b0f5 1991 else if (old_warnings == pWARN_ALL ||
75b6c4ca
RGS
1992 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1993 /* Get the bit mask for $warnings::Bits{all}, because
1994 * it could have been extended by warnings::register */
1995 SV **bits_all;
6673a63c 1996 HV * const bits = get_hv("warnings::Bits", 0);
017a3ce5 1997 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
1998 mask = newSVsv(*bits_all);
1999 }
2000 else {
2001 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
2002 }
2003 }
e476b1b5 2004 else
72dc9ed5 2005 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
6e449a3a 2006 mPUSHs(mask);
e476b1b5 2007 }
b3ca2e83 2008
c28fe1ec 2009 PUSHs(cx->blk_oldcop->cop_hints_hash ?
20439bc7 2010 sv_2mortal(newRV_noinc(MUTABLE_SV(cop_hints_2hv(cx->blk_oldcop, 0))))
b3ca2e83 2011 : &PL_sv_undef);
a0d0e21e
LW
2012 RETURN;
2013}
2014
a0d0e21e
LW
2015PP(pp_reset)
2016{
97aff369 2017 dVAR;
39644a26 2018 dSP;
f650fa72
FC
2019 const char * const tmps =
2020 (MAXARG < 1 || (!TOPs && !POPs)) ? (const char *)"" : POPpconstx;
11faa288 2021 sv_reset(tmps, CopSTASH(PL_curcop));
3280af22 2022 PUSHs(&PL_sv_yes);
a0d0e21e
LW
2023 RETURN;
2024}
2025
dd2155a4
DM
2026/* like pp_nextstate, but used instead when the debugger is active */
2027
a0d0e21e
LW
2028PP(pp_dbstate)
2029{
27da23d5 2030 dVAR;
533c011a 2031 PL_curcop = (COP*)PL_op;
a0d0e21e 2032 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 2033 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
2034 FREETMPS;
2035
f410a211
NC
2036 PERL_ASYNC_CHECK();
2037
5df8de69
DM
2038 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
2039 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
a0d0e21e 2040 {
39644a26 2041 dSP;
c09156bb 2042 register PERL_CONTEXT *cx;
f54cb97a 2043 const I32 gimme = G_ARRAY;
eb160463 2044 U8 hasargs;
0bd48802
AL
2045 GV * const gv = PL_DBgv;
2046 register CV * const cv = GvCV(gv);
a0d0e21e 2047
a0d0e21e 2048 if (!cv)
cea2e8a9 2049 DIE(aTHX_ "No DB::DB routine defined");
a0d0e21e 2050
aea4f609
DM
2051 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
2052 /* don't do recursive DB::DB call */
a0d0e21e 2053 return NORMAL;
748a9306 2054
a57c6685 2055 ENTER;
4633a7c4
LW
2056 SAVETMPS;
2057
3280af22 2058 SAVEI32(PL_debug);
55497cff 2059 SAVESTACK_POS();
3280af22 2060 PL_debug = 0;
748a9306 2061 hasargs = 0;
924508f0 2062 SPAGAIN;
748a9306 2063
aed2304a 2064 if (CvISXSUB(cv)) {
c127bd3a
SF
2065 CvDEPTH(cv)++;
2066 PUSHMARK(SP);
2067 (void)(*CvXSUB(cv))(aTHX_ cv);
2068 CvDEPTH(cv)--;
2069 FREETMPS;
a57c6685 2070 LEAVE;
c127bd3a
SF
2071 return NORMAL;
2072 }
2073 else {
2074 PUSHBLOCK(cx, CXt_SUB, SP);
2075 PUSHSUB_DB(cx);
2076 cx->blk_sub.retop = PL_op->op_next;
2077 CvDEPTH(cv)++;
2078 SAVECOMPPAD();
2079 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
2080 RETURNOP(CvSTART(cv));
2081 }
a0d0e21e
LW
2082 }
2083 else
2084 return NORMAL;
2085}
2086
b9d76716
VP
2087STATIC SV **
2088S_adjust_stack_on_leave(pTHX_ SV **newsp, SV **sp, SV **mark, I32 gimme, U32 flags)
2089{
9a214eec 2090 bool padtmp = 0;
b9d76716
VP
2091 PERL_ARGS_ASSERT_ADJUST_STACK_ON_LEAVE;
2092
9a214eec
DM
2093 if (flags & SVs_PADTMP) {
2094 flags &= ~SVs_PADTMP;
2095 padtmp = 1;
2096 }
b9d76716
VP
2097 if (gimme == G_SCALAR) {
2098 if (MARK < SP)
9a214eec
DM
2099 *++newsp = ((SvFLAGS(*SP) & flags) || (padtmp && SvPADTMP(*SP)))
2100 ? *SP : sv_mortalcopy(*SP);
b9d76716
VP
2101 else {
2102 /* MEXTEND() only updates MARK, so reuse it instead of newsp. */
2103 MARK = newsp;
2104 MEXTEND(MARK, 1);
2105 *++MARK = &PL_sv_undef;
2106 return MARK;
2107 }
2108 }
2109 else if (gimme == G_ARRAY) {
2110 /* in case LEAVE wipes old return values */
2111 while (++MARK <= SP) {
9a214eec 2112 if ((SvFLAGS(*MARK) & flags) || (padtmp && SvPADTMP(*MARK)))
b9d76716
VP
2113 *++newsp = *MARK;
2114 else {
2115 *++newsp = sv_mortalcopy(*MARK);
2116 TAINT_NOT; /* Each item is independent */
2117 }
2118 }
2119 /* When this function was called with MARK == newsp, we reach this
2120 * point with SP == newsp. */
2121 }
2122
2123 return newsp;
2124}
2125
2b9a6457
VP
2126PP(pp_enter)
2127{
2128 dVAR; dSP;
2129 register PERL_CONTEXT *cx;
7c2d9d03 2130 I32 gimme = GIMME_V;
2b9a6457
VP
2131
2132 ENTER_with_name("block");
2133
2134 SAVETMPS;
2135 PUSHBLOCK(cx, CXt_BLOCK, SP);
2136
2137 RETURN;
2138}
2139
2140PP(pp_leave)
2141{
2142 dVAR; dSP;
2143 register PERL_CONTEXT *cx;
2144 SV **newsp;
2145 PMOP *newpm;
2146 I32 gimme;
2147
2148 if (PL_op->op_flags & OPf_SPECIAL) {
2149 cx = &cxstack[cxstack_ix];
2150 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
2151 }
2152
2153 POPBLOCK(cx,newpm);
2154
2155 gimme = OP_GIMME(PL_op, (cxstack_ix >= 0) ? gimme : G_SCALAR);
2156
2157 TAINT_NOT;
f02ea43c 2158 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
2b9a6457
VP
2159 PL_curpm = newpm; /* Don't pop $1 et al till now */
2160
2161 LEAVE_with_name("block");
2162
2163 RETURN;
2164}
2165
a0d0e21e
LW
2166PP(pp_enteriter)
2167{
27da23d5 2168 dVAR; dSP; dMARK;
c09156bb 2169 register PERL_CONTEXT *cx;
f54cb97a 2170 const I32 gimme = GIMME_V;
df530c37 2171 void *itervar; /* location of the iteration variable */
840fe433 2172 U8 cxtype = CXt_LOOP_FOR;
a0d0e21e 2173
d343c3ef 2174 ENTER_with_name("loop1");
4633a7c4
LW
2175 SAVETMPS;
2176
aafca525
DM
2177 if (PL_op->op_targ) { /* "my" variable */
2178 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
14f338dc
DM
2179 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
2180 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
2181 SVs_PADSTALE, SVs_PADSTALE);
2182 }
09edbca0 2183 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
89e00a7c 2184#ifdef USE_ITHREADS
df530c37 2185 itervar = PL_comppad;
89e00a7c 2186#else
aafca525 2187 itervar = &PAD_SVl(PL_op->op_targ);
7766f137 2188#endif
54b9620d 2189 }
aafca525 2190 else { /* symbol table variable */
159b6efe 2191 GV * const gv = MUTABLE_GV(POPs);
f83b46a0
DM
2192 SV** svp = &GvSV(gv);
2193 save_pushptrptr(gv, SvREFCNT_inc(*svp), SAVEt_GVSV);
561b68a9 2194 *svp = newSV(0);
df530c37 2195 itervar = (void *)gv;
54b9620d 2196 }
4633a7c4 2197
0d863452
RH
2198 if (PL_op->op_private & OPpITER_DEF)
2199 cxtype |= CXp_FOR_DEF;
2200
d343c3ef 2201 ENTER_with_name("loop2");
a0d0e21e 2202
7766f137 2203 PUSHBLOCK(cx, cxtype, SP);
df530c37 2204 PUSHLOOP_FOR(cx, itervar, MARK);
533c011a 2205 if (PL_op->op_flags & OPf_STACKED) {
d01136d6
BS
2206 SV *maybe_ary = POPs;
2207 if (SvTYPE(maybe_ary) != SVt_PVAV) {
89ea2908 2208 dPOPss;
d01136d6 2209 SV * const right = maybe_ary;
984a4bea
RD
2210 SvGETMAGIC(sv);
2211 SvGETMAGIC(right);
4fe3f0fa 2212 if (RANGE_IS_NUMERIC(sv,right)) {
d01136d6 2213 cx->cx_type &= ~CXTYPEMASK;
c6fdafd0
NC
2214 cx->cx_type |= CXt_LOOP_LAZYIV;
2215 /* Make sure that no-one re-orders cop.h and breaks our
2216 assumptions */
2217 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
a2309040 2218#ifdef NV_PRESERVES_UV
f52e41ad
FC
2219 if ((SvOK(sv) && ((SvNV_nomg(sv) < (NV)IV_MIN) ||
2220 (SvNV_nomg(sv) > (NV)IV_MAX)))
a2309040 2221 ||
f52e41ad
FC
2222 (SvOK(right) && ((SvNV_nomg(right) > (NV)IV_MAX) ||
2223 (SvNV_nomg(right) < (NV)IV_MIN))))
a2309040 2224#else
f52e41ad 2225 if ((SvOK(sv) && ((SvNV_nomg(sv) <= (NV)IV_MIN)
a2309040 2226 ||
f52e41ad
FC
2227 ((SvNV_nomg(sv) > 0) &&
2228 ((SvUV_nomg(sv) > (UV)IV_MAX) ||
2229 (SvNV_nomg(sv) > (NV)UV_MAX)))))
a2309040 2230 ||
f52e41ad 2231 (SvOK(right) && ((SvNV_nomg(right) <= (NV)IV_MIN)
a2309040 2232 ||
f52e41ad
FC
2233 ((SvNV_nomg(right) > 0) &&
2234 ((SvUV_nomg(right) > (UV)IV_MAX) ||
2235 (SvNV_nomg(right) > (NV)UV_MAX))
2236 ))))
a2309040 2237#endif
076d9a11 2238 DIE(aTHX_ "Range iterator outside integer range");
f52e41ad
FC
2239 cx->blk_loop.state_u.lazyiv.cur = SvIV_nomg(sv);
2240 cx->blk_loop.state_u.lazyiv.end = SvIV_nomg(right);
d4665a05
DM
2241#ifdef DEBUGGING
2242 /* for correct -Dstv display */
2243 cx->blk_oldsp = sp - PL_stack_base;
2244#endif
89ea2908 2245 }
3f63a782 2246 else {
d01136d6
BS
2247 cx->cx_type &= ~CXTYPEMASK;
2248 cx->cx_type |= CXt_LOOP_LAZYSV;
2249 /* Make sure that no-one re-orders cop.h and breaks our
2250 assumptions */
2251 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
2252 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
2253 cx->blk_loop.state_u.lazysv.end = right;
2254 SvREFCNT_inc(right);
2255 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
267cc4a8
NC
2256 /* This will do the upgrade to SVt_PV, and warn if the value
2257 is uninitialised. */
10516c54 2258 (void) SvPV_nolen_const(right);
267cc4a8
NC
2259 /* Doing this avoids a check every time in pp_iter in pp_hot.c
2260 to replace !SvOK() with a pointer to "". */
2261 if (!SvOK(right)) {
2262 SvREFCNT_dec(right);
d01136d6 2263 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
267cc4a8 2264 }
3f63a782 2265 }
89ea2908 2266 }
d01136d6 2267 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
502c6561 2268 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
d01136d6
BS
2269 SvREFCNT_inc(maybe_ary);
2270 cx->blk_loop.state_u.ary.ix =
2271 (PL_op->op_private & OPpITER_REVERSED) ?
2272 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
2273 -1;
ef3e5ea9 2274 }
89ea2908 2275 }
d01136d6
BS
2276 else { /* iterating over items on the stack */
2277 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
ef3e5ea9 2278 if (PL_op->op_private & OPpITER_REVERSED) {
d01136d6 2279 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
ef3e5ea9
NC
2280 }
2281 else {
d01136d6 2282 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
ef3e5ea9 2283 }
4633a7c4 2284 }
a0d0e21e
LW
2285
2286 RETURN;
2287}
2288
2289PP(pp_enterloop)
2290{
27da23d5 2291 dVAR; dSP;
c09156bb 2292 register PERL_CONTEXT *cx;
f54cb97a 2293 const I32 gimme = GIMME_V;
a0d0e21e 2294
d343c3ef 2295 ENTER_with_name("loop1");
a0d0e21e 2296 SAVETMPS;
d343c3ef 2297 ENTER_with_name("loop2");
a0d0e21e 2298
3b719c58
NC
2299 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2300 PUSHLOOP_PLAIN(cx, SP);
a0d0e21e
LW
2301
2302 RETURN;
2303}
2304
2305PP(pp_leaveloop)
2306{
27da23d5 2307 dVAR; dSP;
c09156bb 2308 register PERL_CONTEXT *cx;
a0d0e21e
LW
2309 I32 gimme;
2310 SV **newsp;
2311 PMOP *newpm;
2312 SV **mark;
2313
2314 POPBLOCK(cx,newpm);
3b719c58 2315 assert(CxTYPE_is_LOOP(cx));
4fdae800 2316 mark = newsp;
a8bba7fa 2317 newsp = PL_stack_base + cx->blk_loop.resetsp;
f86702cc 2318
a1f49e72 2319 TAINT_NOT;
b9d76716 2320 SP = adjust_stack_on_leave(newsp, SP, MARK, gimme, 0);
f86702cc 2321 PUTBACK;
2322
a8bba7fa 2323 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
3280af22 2324 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2325
d343c3ef
GG
2326 LEAVE_with_name("loop2");
2327 LEAVE_with_name("loop1");
a0d0e21e 2328
f86702cc 2329 return NORMAL;
a0d0e21e
LW
2330}
2331
3bdf583b
FC
2332STATIC void
2333S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
d25b0d7b 2334 PERL_CONTEXT *cx, PMOP *newpm)
3bdf583b 2335{
80422e24 2336 const bool ref = !!(CxLVAL(cx) & OPpENTERSUB_INARGS);
3bdf583b 2337 if (gimme == G_SCALAR) {
d25b0d7b
FC
2338 if (CxLVAL(cx) && !ref) { /* Leave it as it is if we can. */
2339 SV *sv;
001de122 2340 const char *what = NULL;
d25b0d7b
FC
2341 if (MARK < SP) {
2342 assert(MARK+1 == SP);
2343 if ((SvPADTMP(TOPs) ||
2344 (SvFLAGS(TOPs) & (SVf_READONLY | SVf_FAKE))
2345 == SVf_READONLY
2346 ) &&
2347 !SvSMAGICAL(TOPs)) {
001de122 2348 what =
d25b0d7b 2349 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
001de122 2350 : "a readonly value" : "a temporary";
d25b0d7b 2351 }
001de122 2352 else goto copy_sv;
d25b0d7b
FC
2353 }
2354 else {
2355 /* sub:lvalue{} will take us here. */
001de122 2356 what = "undef";
d25b0d7b 2357 }
001de122
FC
2358 LEAVE;
2359 cxstack_ix--;
2360 POPSUB(cx,sv);
2361 PL_curpm = newpm;
2362 LEAVESUB(sv);
2363 Perl_croak(aTHX_
2364 "Can't return %s from lvalue subroutine", what
2365 );
d25b0d7b 2366 }
93905212 2367 if (MARK < SP) {
a5ad7a5a 2368 copy_sv:
3bdf583b 2369 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
5811c07e 2370 if (!SvPADTMP(*SP)) {
3bdf583b
FC
2371 *++newsp = SvREFCNT_inc(*SP);
2372 FREETMPS;
2373 sv_2mortal(*newsp);
5811c07e
FC
2374 }
2375 else {
2376 /* FREETMPS could clobber it */
2377 SV *sv = SvREFCNT_inc(*SP);
2378 FREETMPS;
2379 *++newsp = sv_mortalcopy(sv);
2380 SvREFCNT_dec(sv);
2381 }
3bdf583b
FC
2382 }
2383 else
e08be60b 2384 *++newsp =
5811c07e
FC
2385 SvPADTMP(*SP)
2386 ? sv_mortalcopy(*SP)
2387 : !SvTEMP(*SP)
e08be60b
FC
2388 ? sv_2mortal(SvREFCNT_inc_simple_NN(*SP))
2389 : *SP;
3bdf583b 2390 }
0d235c77
FC
2391 else {
2392 EXTEND(newsp,1);
3bdf583b 2393 *++newsp = &PL_sv_undef;
0d235c77 2394 }
0e9700df 2395 if (CxLVAL(cx) & OPpDEREF) {
767eda44
FC
2396 SvGETMAGIC(TOPs);
2397 if (!SvOK(TOPs)) {
0e9700df 2398 TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF);
767eda44
FC
2399 }
2400 }
3bdf583b
FC
2401 }
2402 else if (gimme == G_ARRAY) {
0e9700df 2403 assert (!(CxLVAL(cx) & OPpDEREF));
80422e24 2404 if (ref || !CxLVAL(cx))
e08be60b
FC
2405 while (++MARK <= SP)
2406 *++newsp =
5811c07e 2407 SvFLAGS(*MARK) & SVs_PADTMP
80422e24 2408 ? sv_mortalcopy(*MARK)
5811c07e
FC
2409 : SvTEMP(*MARK)
2410 ? *MARK
80422e24 2411 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
e08be60b 2412 else while (++MARK <= SP) {
d25b0d7b
FC
2413 if (*MARK != &PL_sv_undef
2414 && (SvPADTMP(*MARK)
2415 || (SvFLAGS(*MARK) & (SVf_READONLY|SVf_FAKE))
2416 == SVf_READONLY
2417 )
2418 ) {
2419 SV *sv;
2420 /* Might be flattened array after $#array = */
2421 PUTBACK;
2422 LEAVE;
2423 cxstack_ix--;
2424 POPSUB(cx,sv);
2425 PL_curpm = newpm;
2426 LEAVESUB(sv);
ae917476 2427 /* diag_listed_as: Can't return %s from lvalue subroutine */
d25b0d7b
FC
2428 Perl_croak(aTHX_
2429 "Can't return a %s from lvalue subroutine",
2430 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2431 }
2432 else
4bee03f8
FC
2433 *++newsp =
2434 SvTEMP(*MARK)
2435 ? *MARK
2436 : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
3bdf583b
FC
2437 }
2438 }
2439 PL_stack_sp = newsp;
2440}
2441
a0d0e21e
LW
2442PP(pp_return)
2443{
27da23d5 2444 dVAR; dSP; dMARK;
c09156bb 2445 register PERL_CONTEXT *cx;
f86702cc 2446 bool popsub2 = FALSE;
b45de488 2447 bool clear_errsv = FALSE;
fa1e92c4 2448 bool lval = FALSE;
a0d0e21e
LW
2449 I32 gimme;
2450 SV **newsp;
2451 PMOP *newpm;
2452 I32 optype = 0;
b6494f15 2453 SV *namesv;
b0d9ce38 2454 SV *sv;
b263a1ad 2455 OP *retop = NULL;
a0d0e21e 2456
0bd48802
AL
2457 const I32 cxix = dopoptosub(cxstack_ix);
2458
9850bf21
RH
2459 if (cxix < 0) {
2460 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2461 * sort block, which is a CXt_NULL
2462 * not a CXt_SUB */
2463 dounwind(0);
d7507f74
RH
2464 PL_stack_base[1] = *PL_stack_sp;
2465 PL_stack_sp = PL_stack_base + 1;
a0d0e21e
LW
2466 return 0;
2467 }
9850bf21
RH
2468 else
2469 DIE(aTHX_ "Can't return outside a subroutine");
a0d0e21e 2470 }
a0d0e21e
LW
2471 if (cxix < cxstack_ix)
2472 dounwind(cxix);
2473
d7507f74
RH
2474 if (CxMULTICALL(&cxstack[cxix])) {
2475 gimme = cxstack[cxix].blk_gimme;
2476 if (gimme == G_VOID)
2477 PL_stack_sp = PL_stack_base;
2478 else if (gimme == G_SCALAR) {
2479 PL_stack_base[1] = *PL_stack_sp;
2480 PL_stack_sp = PL_stack_base + 1;
2481 }
9850bf21 2482 return 0;
d7507f74 2483 }
9850bf21 2484
a0d0e21e 2485 POPBLOCK(cx,newpm);
6b35e009 2486 switch (CxTYPE(cx)) {
a0d0e21e 2487 case CXt_SUB:
f86702cc 2488 popsub2 = TRUE;
fa1e92c4 2489 lval = !!CvLVALUE(cx->blk_sub.cv);
f39bc417 2490 retop = cx->blk_sub.retop;
5dd42e15 2491 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
a0d0e21e
LW
2492 break;
2493 case CXt_EVAL:
b45de488
GS
2494 if (!(PL_in_eval & EVAL_KEEPERR))
2495 clear_errsv = TRUE;
a0d0e21e 2496 POPEVAL(cx);
b6494f15 2497 namesv = cx->blk_eval.old_namesv;
f39bc417 2498 retop = cx->blk_eval.retop;
1d76a5c3
GS
2499 if (CxTRYBLOCK(cx))
2500 break;
748a9306
LW
2501 if (optype == OP_REQUIRE &&
2502 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2503 {
54310121 2504 /* Unassume the success we assumed earlier. */
b6494f15 2505 (void)hv_delete(GvHVn(PL_incgv),
ecad31f0 2506 SvPVX_const(namesv),
c60dbbc3 2507 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
b6494f15
VP
2508 G_DISCARD);
2509 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
748a9306 2510 }
a0d0e21e 2511 break;
7766f137
GS
2512 case CXt_FORMAT:
2513 POPFORMAT(cx);
f39bc417 2514 retop = cx->blk_sub.retop;
7766f137 2515 break;
a0d0e21e 2516 default:
5637ef5b 2517 DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx));
a0d0e21e
LW
2518 }
2519
a1f49e72 2520 TAINT_NOT;
d25b0d7b 2521 if (lval) S_return_lvalues(aTHX_ MARK, SP, newsp, gimme, cx, newpm);
3bdf583b
FC
2522 else {
2523 if (gimme == G_SCALAR) {
a29cdaf0
IZ
2524 if (MARK < SP) {
2525 if (popsub2) {
a8bba7fa 2526 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
6f48390a
FC
2527 if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1
2528 && !SvMAGICAL(TOPs)) {
a29cdaf0
IZ
2529 *++newsp = SvREFCNT_inc(*SP);
2530 FREETMPS;
2531 sv_2mortal(*newsp);
959e3673
GS
2532 }
2533 else {
2534 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
a29cdaf0 2535 FREETMPS;
959e3673
GS
2536 *++newsp = sv_mortalcopy(sv);
2537 SvREFCNT_dec(sv);
a29cdaf0 2538 }
959e3673 2539 }
6f48390a
FC
2540 else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1
2541 && !SvMAGICAL(*SP)) {
767eda44 2542 *++newsp = *SP;
767eda44 2543 }
959e3673 2544 else
767eda44 2545 *++newsp = sv_mortalcopy(*SP);
959e3673
GS
2546 }
2547 else
a29cdaf0 2548 *++newsp = sv_mortalcopy(*SP);
959e3673
GS
2549 }
2550 else
3280af22 2551 *++newsp = &PL_sv_undef;
3bdf583b
FC
2552 }
2553 else if (gimme == G_ARRAY) {
a1f49e72 2554 while (++MARK <= SP) {
3ed94dc0 2555 *++newsp = popsub2 && SvTEMP(*MARK) && SvREFCNT(*MARK) == 1
6f48390a 2556 && !SvGMAGICAL(*MARK)
f86702cc 2557 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2558 TAINT_NOT; /* Each item is independent */
2559 }
3bdf583b
FC
2560 }
2561 PL_stack_sp = newsp;
a0d0e21e 2562 }
a0d0e21e 2563
5dd42e15 2564 LEAVE;
f86702cc 2565 /* Stack values are safe: */
2566 if (popsub2) {
5dd42e15 2567 cxstack_ix--;
b0d9ce38 2568 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2569 }
b0d9ce38 2570 else
c445ea15 2571 sv = NULL;
3280af22 2572 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2573
b0d9ce38 2574 LEAVESUB(sv);
8433848b 2575 if (clear_errsv) {
ab69dbc2 2576 CLEAR_ERRSV();
8433848b 2577 }
f39bc417 2578 return retop;
a0d0e21e
LW
2579}
2580
4f443c3d
FC
2581/* This duplicates parts of pp_leavesub, so that it can share code with
2582 * pp_return */
2583PP(pp_leavesublv)
2584{
2585 dVAR; dSP;
4f443c3d
FC
2586 SV **newsp;
2587 PMOP *newpm;
2588 I32 gimme;
2589 register PERL_CONTEXT *cx;
2590 SV *sv;
2591
2592 if (CxMULTICALL(&cxstack[cxstack_ix]))
2593 return 0;
2594
2595 POPBLOCK(cx,newpm);
2596 cxstack_ix++; /* temporarily protect top context */
4f443c3d
FC
2597
2598 TAINT_NOT;
2599
0d235c77 2600 S_return_lvalues(aTHX_ newsp, SP, newsp, gimme, cx, newpm);
4f443c3d
FC
2601
2602 LEAVE;
2603 cxstack_ix--;
2604 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2605 PL_curpm = newpm; /* ... and pop $1 et al */
2606
2607 LEAVESUB(sv);
2608 return cx->blk_sub.retop;
2609}
2610
a0d0e21e
LW
2611PP(pp_last)
2612{
27da23d5 2613 dVAR; dSP;
a0d0e21e 2614 I32 cxix;
c09156bb 2615 register PERL_CONTEXT *cx;
f86702cc 2616 I32 pop2 = 0;
a0d0e21e 2617 I32 gimme;
8772537c 2618 I32 optype;
b263a1ad 2619 OP *nextop = NULL;
a0d0e21e
LW
2620 SV **newsp;
2621 PMOP *newpm;
a8bba7fa 2622 SV **mark;
c445ea15 2623 SV *sv = NULL;
9d4ba2ae 2624
a0d0e21e 2625
533c011a 2626 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2627 cxix = dopoptoloop(cxstack_ix);
2628 if (cxix < 0)
a651a37d 2629 DIE(aTHX_ "Can't \"last\" outside a loop block");
a0d0e21e
LW
2630 }
2631 else {
5db1eb8d
BF
2632 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2633 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
a0d0e21e 2634 if (cxix < 0)
5db1eb8d
BF
2635 DIE(aTHX_ "Label not found for \"last %"SVf"\"",
2636 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2637 strlen(cPVOP->op_pv),
2638 ((cPVOP->op_private & OPpPV_IS_UTF8)
2639 ? SVf_UTF8 : 0) | SVs_TEMP)));
a0d0e21e
LW
2640 }
2641 if (cxix < cxstack_ix)
2642 dounwind(cxix);
2643
2644 POPBLOCK(cx,newpm);
5dd42e15 2645 cxstack_ix++; /* temporarily protect top context */
a8bba7fa 2646 mark = newsp;
6b35e009 2647 switch (CxTYPE(cx)) {
c6fdafd0 2648 case CXt_LOOP_LAZYIV:
d01136d6 2649 case CXt_LOOP_LAZYSV:
3b719c58
NC
2650 case CXt_LOOP_FOR:
2651 case CXt_LOOP_PLAIN:
2652 pop2 = CxTYPE(cx);
a8bba7fa 2653 newsp = PL_stack_base + cx->blk_loop.resetsp;
022eaa24 2654 nextop = cx->blk_loop.my_op->op_lastop->op_next;
a0d0e21e 2655 break;
f86702cc 2656 case CXt_SUB:
f86702cc 2657 pop2 = CXt_SUB;
f39bc417 2658 nextop = cx->blk_sub.retop;
a0d0e21e 2659 break;
f86702cc 2660 case CXt_EVAL:
2661 POPEVAL(cx);
f39bc417 2662 nextop = cx->blk_eval.retop;
a0d0e21e 2663 break;
7766f137
GS
2664 case CXt_FORMAT:
2665 POPFORMAT(cx);
f39bc417 2666 nextop = cx->blk_sub.retop;
7766f137 2667 break;
a0d0e21e 2668 default:
5637ef5b 2669 DIE(aTHX_ "panic: last, type=%u", (unsigned) CxTYPE(cx));
a0d0e21e
LW
2670 }
2671
a1f49e72 2672 TAINT_NOT;
b9d76716
VP
2673 SP = adjust_stack_on_leave(newsp, SP, MARK, gimme,
2674 pop2 == CXt_SUB ? SVs_TEMP : 0);
f86702cc 2675 PUTBACK;
2676
5dd42e15
DM
2677 LEAVE;
2678 cxstack_ix--;
f86702cc 2679 /* Stack values are safe: */
2680 switch (pop2) {
c6fdafd0 2681 case CXt_LOOP_LAZYIV:
3b719c58 2682 case CXt_LOOP_PLAIN:
d01136d6 2683 case CXt_LOOP_LAZYSV:
3b719c58 2684 case CXt_LOOP_FOR:
a8bba7fa 2685 POPLOOP(cx); /* release loop vars ... */
4fdae800 2686 LEAVE;
f86702cc 2687 break;
2688 case CXt_SUB:
b0d9ce38 2689 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2690 break;
a0d0e21e 2691 }
3280af22 2692 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e 2693
b0d9ce38 2694 LEAVESUB(sv);
9d4ba2ae
AL
2695 PERL_UNUSED_VAR(optype);
2696 PERL_UNUSED_VAR(gimme);
f86702cc 2697 return nextop;
a0d0e21e
LW
2698}
2699
2700PP(pp_next)
2701{
27da23d5 2702 dVAR;
a0d0e21e 2703 I32 cxix;
c09156bb 2704 register PERL_CONTEXT *cx;
85538317 2705 I32 inner;
a0d0e21e 2706
533c011a 2707 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2708 cxix = dopoptoloop(cxstack_ix);
2709 if (cxix < 0)
a651a37d 2710 DIE(aTHX_ "Can't \"next\" outside a loop block");
a0d0e21e
LW
2711 }
2712 else {
5db1eb8d
BF
2713 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2714 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
2715 if (cxix < 0)
2716 DIE(aTHX_ "Label not found for \"next %"SVf"\"",
2717 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2718 strlen(cPVOP->op_pv),
2719 ((cPVOP->op_private & OPpPV_IS_UTF8)
2720 ? SVf_UTF8 : 0) | SVs_TEMP)));
a0d0e21e
LW
2721 }
2722 if (cxix < cxstack_ix)
2723 dounwind(cxix);
2724
85538317
GS
2725 /* clear off anything above the scope we're re-entering, but
2726 * save the rest until after a possible continue block */
2727 inner = PL_scopestack_ix;
1ba6ee2b 2728 TOPBLOCK(cx);
85538317
GS
2729 if (PL_scopestack_ix < inner)
2730 leave_scope(PL_scopestack[PL_scopestack_ix]);
3a1b2b9e 2731 PL_curcop = cx->blk_oldcop;
d57ce4df 2732 return (cx)->blk_loop.my_op->op_nextop;
a0d0e21e
LW
2733}
2734
2735PP(pp_redo)
2736{
27da23d5 2737 dVAR;
a0d0e21e 2738 I32 cxix;
c09156bb 2739 register PERL_CONTEXT *cx;
a0d0e21e 2740 I32 oldsave;
a034e688 2741 OP* redo_op;
a0d0e21e 2742
533c011a 2743 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2744 cxix = dopoptoloop(cxstack_ix);
2745 if (cxix < 0)
a651a37d 2746 DIE(aTHX_ "Can't \"redo\" outside a loop block");
a0d0e21e
LW
2747 }
2748 else {
5db1eb8d
BF
2749 cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
2750 (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
2751 if (cxix < 0)
2752 DIE(aTHX_ "Label not found for \"redo %"SVf"\"",
2753 SVfARG(newSVpvn_flags(cPVOP->op_pv,
2754 strlen(cPVOP->op_pv),
2755 ((cPVOP->op_private & OPpPV_IS_UTF8)
2756 ? SVf_UTF8 : 0) | SVs_TEMP)));
a0d0e21e
LW
2757 }
2758 if (cxix < cxstack_ix)
2759 dounwind(cxix);
2760
022eaa24 2761 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
a034e688
DM
2762 if (redo_op->op_type == OP_ENTER) {
2763 /* pop one less context to avoid $x being freed in while (my $x..) */
2764 cxstack_ix++;
2765 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2766 redo_op = redo_op->op_next;
2767 }
2768
a0d0e21e 2769 TOPBLOCK(cx);
3280af22 2770 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e 2771 LEAVE_SCOPE(oldsave);
936c78b5 2772 FREETMPS;
3a1b2b9e 2773 PL_curcop = cx->blk_oldcop;
a034e688 2774 return redo_op;
a0d0e21e
LW
2775}
2776
0824fdcb 2777STATIC OP *
5db1eb8d 2778S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit)
a0d0e21e 2779{
97aff369 2780 dVAR;
a0d0e21e 2781 OP **ops = opstack;
bfed75c6 2782 static const char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 2783
7918f24d
NC
2784 PERL_ARGS_ASSERT_DOFINDLABEL;
2785
fc36a67e 2786 if (ops >= oplimit)
cea2e8a9 2787 Perl_croak(aTHX_ too_deep);
11343788
MB
2788 if (o->op_type == OP_LEAVE ||
2789 o->op_type == OP_SCOPE ||
2790 o->op_type == OP_LEAVELOOP ||
33d34e4c 2791 o->op_type == OP_LEAVESUB ||
11343788 2792 o->op_type == OP_LEAVETRY)
fc36a67e 2793 {
5dc0d613 2794 *ops++ = cUNOPo->op_first;
fc36a67e 2795 if (ops >= oplimit)
cea2e8a9 2796 Perl_croak(aTHX_ too_deep);
fc36a67e 2797 }
c4aa4e48 2798 *ops = 0;
11343788 2799 if (o->op_flags & OPf_KIDS) {
aec46f14 2800 OP *kid;
a0d0e21e 2801 /* First try all the kids at this level, since that's likeliest. */
11343788 2802 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
7e8f1eac 2803 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
5db1eb8d
BF
2804 STRLEN kid_label_len;
2805 U32 kid_label_flags;
2806 const char *kid_label = CopLABEL_len_flags(kCOP,
2807 &kid_label_len, &kid_label_flags);
2808 if (kid_label && (
2809 ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
2810 (flags & SVf_UTF8)
2811 ? (bytes_cmp_utf8(
2812 (const U8*)kid_label, kid_label_len,
2813 (const U8*)label, len) == 0)
2814 : (bytes_cmp_utf8(
2815 (const U8*)label, len,
2816 (const U8*)kid_label, kid_label_len) == 0)
eade7155
BF
2817 : ( len == kid_label_len && ((kid_label == label)
2818 || memEQ(kid_label, label, len)))))
7e8f1eac
AD
2819 return kid;
2820 }
a0d0e21e 2821 }
11343788 2822 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
3280af22 2823 if (kid == PL_lastgotoprobe)
a0d0e21e 2824 continue;
ed8d0fe2
SM
2825 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2826 if (ops == opstack)
2827 *ops++ = kid;
2828 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2829 ops[-1]->op_type == OP_DBSTATE)
2830 ops[-1] = kid;
2831 else
2832 *ops++ = kid;
2833 }
5db1eb8d 2834 if ((o = dofindlabel(kid, label, len, flags, ops, oplimit)))
11343788 2835 return o;
a0d0e21e
LW
2836 }
2837 }
c4aa4e48 2838 *ops = 0;
a0d0e21e
LW
2839 return 0;
2840}
2841
a0d0e21e
LW
2842PP(pp_goto)
2843{
27da23d5 2844 dVAR; dSP;
cbbf8932 2845 OP *retop = NULL;
a0d0e21e 2846 I32 ix;
c09156bb 2847 register PERL_CONTEXT *cx;
fc36a67e 2848#define GOTO_DEPTH 64
2849 OP *enterops[GOTO_DEPTH];
cbbf8932 2850 const char *label = NULL;
5db1eb8d
BF
2851 STRLEN label_len = 0;
2852 U32 label_flags = 0;
bfed75c6
AL
2853 const bool do_dump = (PL_op->op_type == OP_DUMP);
2854 static const char must_have_label[] = "goto must have label";
a0d0e21e 2855
533c011a 2856 if (PL_op->op_flags & OPf_STACKED) {
9d4ba2ae 2857 SV * const sv = POPs;
a0d0e21e
LW
2858
2859 /* This egregious kludge implements goto &subroutine */
2860 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2861 I32 cxix;
c09156bb 2862 register PERL_CONTEXT *cx;
ea726b52 2863 CV *cv = MUTABLE_CV(SvRV(sv));
a0d0e21e
LW
2864 SV** mark;
2865 I32 items = 0;
2866 I32 oldsave;
b1464ded 2867 bool reified = 0;
a0d0e21e 2868
e8f7dd13 2869 retry:
4aa0a1f7 2870 if (!CvROOT(cv) && !CvXSUB(cv)) {
7fc63493 2871 const GV * const gv = CvGV(cv);
e8f7dd13 2872 if (gv) {
7fc63493 2873 GV *autogv;
e8f7dd13
GS
2874 SV *tmpstr;
2875 /* autoloaded stub? */
2876 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2877 goto retry;
c271df94
BF
2878 autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv),
2879 GvNAMELEN(gv),
2880 GvNAMEUTF8(gv) ? SVf_UTF8 : 0);
e8f7dd13
GS
2881 if (autogv && (cv = GvCV(autogv)))
2882 goto retry;
2883 tmpstr = sv_newmortal();
c445ea15 2884 gv_efullname3(tmpstr, gv, NULL);
be2597df 2885 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
4aa0a1f7 2886 }
cea2e8a9 2887 DIE(aTHX_ "Goto undefined subroutine");
4aa0a1f7
AD
2888 }
2889
a0d0e21e 2890 /* First do some returnish stuff. */
b37c2d43 2891 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
71fc2216 2892 FREETMPS;
a0d0e21e
LW
2893 cxix = dopoptosub(cxstack_ix);
2894 if (cxix < 0)
cea2e8a9 2895 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
a0d0e21e
LW
2896 if (cxix < cxstack_ix)
2897 dounwind(cxix);
2898 TOPBLOCK(cx);
2d43a17f 2899 SPAGAIN;
564abe23 2900 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2d43a17f 2901 if (CxTYPE(cx) == CXt_EVAL) {
c74ace89 2902 if (CxREALEVAL(cx))
00455a92 2903 /* diag_listed_as: Can't goto subroutine from an eval-%s */
c74ace89
DM
2904 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2905 else
00455a92 2906 /* diag_listed_as: Can't goto subroutine from an eval-%s */
c74ace89 2907 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2d43a17f 2908 }
9850bf21
RH
2909 else if (CxMULTICALL(cx))
2910 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
bafb2adc 2911 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
d8b46c1b 2912 /* put @_ back onto stack */
a0d0e21e 2913 AV* av = cx->blk_sub.argarray;
bfed75c6 2914
93965878 2915 items = AvFILLp(av) + 1;
a45cdc79
DM
2916 EXTEND(SP, items+1); /* @_ could have been extended. */
2917 Copy(AvARRAY(av), SP + 1, items, SV*);
3280af22
NIS
2918 SvREFCNT_dec(GvAV(PL_defgv));
2919 GvAV(PL_defgv) = cx->blk_sub.savearray;
b1464ded 2920 CLEAR_ARGARRAY(av);
d8b46c1b 2921 /* abandon @_ if it got reified */
62b1ebc2 2922 if (AvREAL(av)) {
b1464ded
DM
2923 reified = 1;
2924 SvREFCNT_dec(av);
d8b46c1b
GS
2925 av = newAV();
2926 av_extend(av, items-1);
11ca45c0 2927 AvREIFY_only(av);
ad64d0ec 2928 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
62b1ebc2 2929 }
a0d0e21e 2930 }
aed2304a 2931 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
890ce7af 2932 AV* const av = GvAV(PL_defgv);
1fa4e549 2933 items = AvFILLp(av) + 1;
a45cdc79
DM
2934 EXTEND(SP, items+1); /* @_ could have been extended. */
2935 Copy(AvARRAY(av), SP + 1, items, SV*);
1fa4e549 2936 }
a45cdc79
DM
2937 mark = SP;
2938 SP += items;
6b35e009 2939 if (CxTYPE(cx) == CXt_SUB &&
b150fb22 2940 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
a0d0e21e 2941 SvREFCNT_dec(cx->blk_sub.cv);
3280af22 2942 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
2943 LEAVE_SCOPE(oldsave);
2944
1d59c038
FC
2945 /* A destructor called during LEAVE_SCOPE could have undefined
2946 * our precious cv. See bug #99850. */
2947 if (!CvROOT(cv) && !CvXSUB(cv)) {
2948 const GV * const gv = CvGV(cv);
2949 if (gv) {
2950 SV * const tmpstr = sv_newmortal();
2951 gv_efullname3(tmpstr, gv, NULL);
2952 DIE(aTHX_ "Goto undefined subroutine &%"SVf"",
2953 SVfARG(tmpstr));
2954 }
2955 DIE(aTHX_ "Goto undefined subroutine");
2956 }
2957
a0d0e21e
LW
2958 /* Now do some callish stuff. */
2959 SAVETMPS;
5023d17a 2960 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
aed2304a 2961 if (CvISXSUB(cv)) {
b37c2d43 2962 OP* const retop = cx->blk_sub.retop;
9d63fa07
KW
2963 SV **newsp PERL_UNUSED_DECL;
2964 I32 gimme PERL_UNUSED_DECL;
b1464ded
DM
2965 if (reified) {
2966 I32 index;
2967 for (index=0; index<items; index++)
2968 sv_2mortal(SP[-index]);
2969 }
1fa4e549 2970
b37c2d43
AL
2971 /* XS subs don't have a CxSUB, so pop it */
2972 POPBLOCK(cx, PL_curpm);
2973 /* Push a mark for the start of arglist */
2974 PUSHMARK(mark);
2975 PUTBACK;
2976 (void)(*CvXSUB(cv))(aTHX_ cv);
a57c6685 2977 LEAVE;
5eff7df7 2978 return retop;
a0d0e21e
LW
2979 }
2980 else {
b37c2d43 2981 AV* const padlist = CvPADLIST(cv);
6b35e009 2982 if (CxTYPE(cx) == CXt_EVAL) {
85a64632 2983 PL_in_eval = CxOLD_IN_EVAL(cx);
3280af22 2984 PL_eval_root = cx->blk_eval.old_eval_root;
b150fb22 2985 cx->cx_type = CXt_SUB;
b150fb22 2986 }
a0d0e21e 2987 cx->blk_sub.cv = cv;
1a5b3db4 2988 cx->blk_sub.olddepth = CvDEPTH(cv);
dd2155a4 2989
a0d0e21e
LW
2990 CvDEPTH(cv)++;
2991 if (CvDEPTH(cv) < 2)
74c765eb 2992 SvREFCNT_inc_simple_void_NN(cv);
dd2155a4 2993 else {
2b9dff67 2994 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
44a8e56a 2995 sub_crush_depth(cv);
26019298 2996 pad_push(padlist, CvDEPTH(cv));
a0d0e21e 2997 }
426a09cd 2998 PL_curcop = cx->blk_oldcop;
fd617465
DM
2999 SAVECOMPPAD();
3000 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
bafb2adc 3001 if (CxHASARGS(cx))
6d4ff0d2 3002 {
502c6561 3003 AV *const av = MUTABLE_AV(PAD_SVl(0));
a0d0e21e 3004
3280af22 3005 cx->blk_sub.savearray = GvAV(PL_defgv);
502c6561 3006 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
dd2155a4 3007 CX_CURPAD_SAVE(cx->blk_sub);
6d4ff0d2 3008 cx->blk_sub.argarray = av;
a0d0e21e
LW
3009
3010 if (items >= AvMAX(av) + 1) {
b37c2d43 3011 SV **ary = AvALLOC(av);
a0d0e21e
LW
3012 if (AvARRAY(av) != ary) {
3013 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
9c6bc640 3014 AvARRAY(av) = ary;
a0d0e21e
LW
3015 }
3016 if (items >= AvMAX(av) + 1) {
3017 AvMAX(av) = items - 1;
3018 Renew(ary,items+1,SV*);
3019 AvALLOC(av) = ary;
9c6bc640 3020 AvARRAY(av) = ary;
a0d0e21e
LW
3021 }
3022 }
a45cdc79 3023 ++mark;
a0d0e21e 3024 Copy(mark,AvARRAY(av),items,SV*);
93965878 3025 AvFILLp(av) = items - 1;
d8b46c1b 3026 assert(!AvREAL(av));
b1464ded
DM
3027 if (reified) {
3028 /* transfer 'ownership' of refcnts to new @_ */
3029 AvREAL_on(av);
3030 AvREIFY_off(av);
3031 }
a0d0e21e
LW
3032 while (items--) {
3033 if (*mark)
3034 SvTEMP_off(*mark);
3035 mark++;
3036 }
3037 }
491527d0 3038 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
005a8a35 3039 Perl_get_db_sub(aTHX_ NULL, cv);
b37c2d43 3040 if (PERLDB_GOTO) {
b96d8cd9 3041 CV * const gotocv = get_cvs("DB::goto", 0);
b37c2d43
AL
3042 if (gotocv) {
3043 PUSHMARK( PL_stack_sp );
ad64d0ec 3044 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
b37c2d43
AL
3045 PL_stack_sp--;
3046 }
491527d0 3047 }
1ce6579f 3048 }
a0d0e21e
LW
3049 RETURNOP(CvSTART(cv));
3050 }
3051 }
1614b0e3 3052 else {
5db1eb8d
BF
3053 label = SvPV_const(sv, label_len);
3054 label_flags = SvUTF8(sv);
1614b0e3 3055 }
a0d0e21e 3056 }
2fc690dc 3057 else if (!(PL_op->op_flags & OPf_SPECIAL)) {
5db1eb8d
BF
3058 label = cPVOP->op_pv;
3059 label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
3060 label_len = strlen(label);
3061 }
2fc690dc 3062 if (!(do_dump || label_len)) DIE(aTHX_ must_have_label);
a0d0e21e 3063
f410a211
NC
3064 PERL_ASYNC_CHECK();
3065
3532f34a 3066 if (label_len) {
cbbf8932 3067 OP *gotoprobe = NULL;
3b2447bc 3068 bool leaving_eval = FALSE;
33d34e4c 3069 bool in_block = FALSE;
cbbf8932 3070 PERL_CONTEXT *last_eval_cx = NULL;
a0d0e21e
LW
3071
3072 /* find label */
3073
d4c19fe8 3074 PL_lastgotoprobe = NULL;
a0d0e21e
LW
3075 *enterops = 0;
3076 for (ix = cxstack_ix; ix >= 0; ix--) {
3077 cx = &cxstack[ix];
6b35e009 3078 switch (CxTYPE(cx)) {
a0d0e21e 3079 case CXt_EVAL:
3b2447bc 3080 leaving_eval = TRUE;
971ecbe6 3081 if (!CxTRYBLOCK(cx)) {
a4f3a277
RH
3082 gotoprobe = (last_eval_cx ?
3083 last_eval_cx->blk_eval.old_eval_root :
3084 PL_eval_root);
3085 last_eval_cx = cx;
9c5794fe
RH
3086 break;
3087 }
3088 /* else fall through */
c6fdafd0 3089 case CXt_LOOP_LAZYIV:
d01136d6 3090 case CXt_LOOP_LAZYSV:
3b719c58
NC
3091 case CXt_LOOP_FOR:
3092 case CXt_LOOP_PLAIN:
bb5aedc1
VP
3093 case CXt_GIVEN:
3094 case CXt_WHEN:
a0d0e21e
LW
3095 gotoprobe = cx->blk_oldcop->op_sibling;
3096 break;
3097 case CXt_SUBST:
3098 continue;
3099 case CXt_BLOCK:
33d34e4c 3100 if (ix) {
a0d0e21e 3101 gotoprobe = cx->blk_oldcop->op_sibling;
33d34e4c
AE
3102 in_block = TRUE;
3103 } else
3280af22 3104 gotoprobe = PL_main_root;
a0d0e21e 3105 break;
b3933176 3106 case CXt_SUB:
9850bf21 3107 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
b3933176
CS
3108 gotoprobe = CvROOT(cx->blk_sub.cv);
3109 break;
3110 }
3111 /* FALL THROUGH */
7766f137 3112 case CXt_FORMAT:
0a753a76 3113 case CXt_NULL:
a651a37d 3114 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
a0d0e21e
LW
3115 default:
3116 if (ix)
5637ef5b
NC
3117 DIE(aTHX_ "panic: goto, type=%u, ix=%ld",
3118 CxTYPE(cx), (long) ix);
3280af22 3119 gotoprobe = PL_main_root;
a0d0e21e
LW
3120 break;
3121 }
2b597662 3122 if (gotoprobe) {
5db1eb8d 3123 retop = dofindlabel(gotoprobe, label, label_len, label_flags,
2b597662
GS
3124 enterops, enterops + GOTO_DEPTH);
3125 if (retop)
3126 break;
eae48c89
Z
3127 if (gotoprobe->op_sibling &&
3128 gotoprobe->op_sibling->op_type == OP_UNSTACK &&
3129 gotoprobe->op_sibling->op_sibling) {
3130 retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
5db1eb8d
BF
3131 label, label_len, label_flags, enterops,
3132 enterops + GOTO_DEPTH);
eae48c89
Z
3133 if (retop)
3134 break;
3135 }
2b597662 3136 }
3280af22 3137 PL_lastgotoprobe = gotoprobe;
a0d0e21e
LW
3138 }
3139 if (!retop)
5db1eb8d
BF
3140 DIE(aTHX_ "Can't find label %"SVf,
3141 SVfARG(newSVpvn_flags(label, label_len,
3142 SVs_TEMP | label_flags)));
a0d0e21e 3143
3b2447bc
RH
3144 /* if we're leaving an eval, check before we pop any frames
3145 that we're not going to punt, otherwise the error
3146 won't be caught */
3147
3148 if (leaving_eval && *enterops && enterops[1]) {
3149 I32 i;
3150 for (i = 1; enterops[i]; i++)
3151 if (enterops[i]->op_type == OP_ENTERITER)
3152 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
3153 }
3154
b500e03b
GG
3155 if (*enterops && enterops[1]) {
3156 I32 i = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3157 if (enterops[i])
3158 deprecate("\"goto\" to jump into a construct");
3159 }
3160
a0d0e21e
LW
3161 /* pop unwanted frames */
3162
3163 if (ix < cxstack_ix) {
3164 I32 oldsave;
3165
3166 if (ix < 0)
3167 ix = 0;
3168 dounwind(ix);
3169 TOPBLOCK(cx);
3280af22 3170 oldsave = PL_scopestack[PL_scopestack_ix];
a0d0e21e
LW
3171 LEAVE_SCOPE(oldsave);
3172 }
3173
3174 /* push wanted frames */
3175
748a9306 3176 if (*enterops && enterops[1]) {
0bd48802 3177 OP * const oldop = PL_op;
33d34e4c
AE
3178 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
3179 for (; enterops[ix]; ix++) {
533c011a 3180 PL_op = enterops[ix];
84902520
TB
3181 /* Eventually we may want to stack the needed arguments
3182 * for each op. For now, we punt on the hard ones. */
533c011a 3183 if (PL_op->op_type == OP_ENTERITER)
894356b3 3184 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
16c91539 3185 PL_op->op_ppaddr(aTHX);
a0d0e21e 3186 }
533c011a 3187 PL_op = oldop;
a0d0e21e
LW
3188 }
3189 }
3190
3191 if (do_dump) {
a5f75d66 3192#ifdef VMS
6b88bc9c 3193 if (!retop) retop = PL_main_start;
a5f75d66 3194#endif
3280af22
NIS
3195 PL_restartop = retop;
3196 PL_do_undump = TRUE;
a0d0e21e
LW
3197
3198 my_unexec();
3199
3280af22
NIS
3200 PL_restartop = 0; /* hmm, must be GNU unexec().. */
3201 PL_do_undump = FALSE;
a0d0e21e
LW
3202 }
3203
3204 RETURNOP(retop);
3205}
3206
3207PP(pp_exit)
3208{
97aff369 3209 dVAR;
39644a26 3210 dSP;
a0d0e21e
LW
3211 I32 anum;
3212
3213 if (MAXARG < 1)
3214 anum = 0;
9d3c658e
FC
3215 else if (!TOPs) {
3216 anum = 0; (void)POPs;
3217 }
ff0cee69 3218 else {
a0d0e21e 3219 anum = SvIVx(POPs);
d98f61e7
GS
3220#ifdef VMS
3221 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
ff0cee69 3222 anum = 0;
96e176bf 3223 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
ff0cee69 3224#endif
3225 }
cc3604b1 3226 PL_exit_flags |= PERL_EXIT_EXPECTED;
81d86705
NC
3227#ifdef PERL_MAD
3228 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
3229 if (anum || !(PL_minus_c && PL_madskills))
3230 my_exit(anum);
3231#else
a0d0e21e 3232 my_exit(anum);
81d86705 3233#endif
3280af22 3234 PUSHs(&PL_sv_undef);
a0d0e21e
LW
3235 RETURN;
3236}
3237
a0d0e21e
LW
3238/* Eval. */
3239
0824fdcb 3240STATIC void
cea2e8a9 3241S_save_lines(pTHX_ AV *array, SV *sv)
a0d0e21e 3242{
504618e9 3243 const char *s = SvPVX_const(sv);
890ce7af 3244 const char * const send = SvPVX_const(sv) + SvCUR(sv);
504618e9 3245 I32 line = 1;
a0d0e21e 3246
7918f24d
NC
3247 PERL_ARGS_ASSERT_SAVE_LINES;
3248
a0d0e21e 3249 while (s && s < send) {
f54cb97a 3250 const char *t;
b9f83d2f 3251 SV * const tmpstr = newSV_type(SVt_PVMG);
a0d0e21e 3252
1d963ff3 3253 t = (const char *)memchr(s, '\n', send - s);
a0d0e21e
LW
3254 if (t)
3255 t++;
3256 else
3257 t = send;
3258
3259 sv_setpvn(tmpstr, s, t - s);
3260 av_store(array, line++, tmpstr);
3261 s = t;
3262 }
3263}
3264
22f16304
RU
3265/*
3266=for apidoc docatch
3267
3268Check for the cases 0 or 3 of cur_env.je_ret, only used inside an eval context.
3269
32700 is used as continue inside eval,
3271
32723 is used for a die caught by an inner eval - continue inner loop
3273
3274See cop.h: je_mustcatch, when set at any runlevel to TRUE, means eval ops must
3275establish a local jmpenv to handle exception traps.
3276
3277=cut
3278*/
0824fdcb 3279STATIC OP *
cea2e8a9 3280S_docatch(pTHX_ OP *o)
1e422769 3281{
97aff369 3282 dVAR;
6224f72b 3283 int ret;
06b5626a 3284 OP * const oldop = PL_op;
db36c5a1 3285 dJMPENV;
1e422769 3286
1e422769 3287#ifdef DEBUGGING
54310121 3288 assert(CATCH_GET == TRUE);
1e422769 3289#endif
312caa8e 3290 PL_op = o;
8bffa5f8 3291
14dd3ad8 3292 JMPENV_PUSH(ret);
6224f72b 3293 switch (ret) {
312caa8e 3294 case 0:
abd70938
DM
3295 assert(cxstack_ix >= 0);
3296 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3297 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
14dd3ad8 3298 redo_body:
85aaa934 3299 CALLRUNOPS(aTHX);
312caa8e
CS
3300 break;
3301 case 3:
8bffa5f8 3302 /* die caught by an inner eval - continue inner loop */
febb3a6d
Z
3303 if (PL_restartop && PL_restartjmpenv == PL_top_env) {
3304 PL_restartjmpenv = NULL;
312caa8e
CS
3305 PL_op = PL_restartop;
3306 PL_restartop = 0;
3307 goto redo_body;
3308 }
3309 /* FALL THROUGH */
3310 default:
14dd3ad8 3311 JMPENV_POP;
533c011a 3312 PL_op = oldop;
6224f72b 3313 JMPENV_JUMP(ret);
1e422769 3314 /* NOTREACHED */
1e422769 3315 }
14dd3ad8 3316 JMPENV_POP;
533c011a 3317 PL_op = oldop;
5f66b61c 3318 return NULL;
1e422769 3319}
3320
ee23ad3b
NC
3321/* James Bond: Do you expect me to talk?
3322 Auric Goldfinger: No, Mr. Bond. I expect you to die.
3323
3324 This code is an ugly hack, doesn't work with lexicals in subroutines that are
3325 called more than once, and is only used by regcomp.c, for (?{}) blocks.
3326
3327 Currently it is not used outside the core code. Best if it stays that way.
d59a8b3e
NC
3328
3329 Hence it's now deprecated, and will be removed.
ee23ad3b 3330*/
c277df42 3331OP *
bfed75c6 3332Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
c277df42
IZ
3333/* sv Text to convert to OP tree. */
3334/* startop op_free() this to undo. */
3335/* code Short string id of the caller. */
3336{
d59a8b3e
NC
3337 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
3338 return Perl_sv_compile_2op_is_broken(aTHX_ sv, startop, code, padp);
3339}
3340
3341/* Don't use this. It will go away without warning once the regexp engine is
3342 refactored not to use it. */
3343OP *
3344Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
3345 PAD **padp)
3346{
27da23d5 3347 dVAR; dSP; /* Make POPBLOCK work. */
c277df42
IZ
3348 PERL_CONTEXT *cx;
3349 SV **newsp;
b094c71d 3350 I32 gimme = G_VOID;
c277df42
IZ
3351 I32 optype;
3352 OP dummy;
83ee9e09
GS
3353 char tbuf[TYPE_DIGITS(long) + 12 + 10];
3354 char *tmpbuf = tbuf;
c277df42 3355 char *safestr;
a3985cdc 3356 int runtime;
601f1833 3357 CV* runcv = NULL; /* initialise to avoid compiler warnings */
f7997f86 3358 STRLEN len;
634d6919 3359 bool need_catch;
c277df42 3360
d59a8b3e 3361 PERL_ARGS_ASSERT_SV_COMPILE_2OP_IS_BROKEN;
7918f24d 3362
d343c3ef 3363 ENTER_with_name("eval");
27fcb6ee 3364 lex_start(sv, NULL, LEX_START_SAME_FILTER);
c277df42
IZ
3365 SAVETMPS;
3366 /* switch to eval mode */
3367
923e4eb5 3368 if (IN_PERL_COMPILETIME) {
f4dd75d9 3369 SAVECOPSTASH_FREE(&PL_compiling);
11faa288 3370 CopSTASH_set(&PL_compiling, PL_curstash);
cbce877f 3371 }
83ee9e09 3372 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
9d4ba2ae 3373 SV * const sv = sv_newmortal();
83ee9e09
GS
3374 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
3375 code, (unsigned long)++PL_evalseq,
3376 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
3377 tmpbuf = SvPVX(sv);
fc009855 3378 len = SvCUR(sv);
83ee9e09
GS
3379 }
3380 else
d9fad198
JH
3381 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
3382 (unsigned long)++PL_evalseq);
f4dd75d9 3383 SAVECOPFILE_FREE(&PL_compiling);
57843af0 3384 CopFILE_set(&PL_compiling, tmpbuf+2);
f4dd75d9 3385 SAVECOPLINE(&PL_compiling);
57843af0 3386 CopLINE_set(&PL_compiling, 1);
c277df42
IZ
3387 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
3388 deleting the eval's FILEGV from the stash before gv_check() runs
3389 (i.e. before run-time proper). To work around the coredump that
3390 ensues, we always turn GvMULTI_on for any globals that were
3391 introduced within evals. See force_ident(). GSAR 96-10-12 */
f7997f86
NC
3392 safestr = savepvn(tmpbuf, len);
3393 SAVEDELETE(PL_defstash, safestr, len);
b3ac6de7 3394 SAVEHINTS();
d1ca3daa 3395#ifdef OP_IN_REGISTER
6b88bc9c 3396 PL_opsave = op;
d1ca3daa 3397#else
7766f137 3398 SAVEVPTR(PL_op);
d1ca3daa 3399#endif
c277df42 3400
a3985cdc 3401 /* we get here either during compilation, or via pp_regcomp at runtime */
923e4eb5 3402 runtime = IN_PERL_RUNTIME;
a3985cdc 3403 if (runtime)
558b4424 3404 {
d819b83a 3405 runcv = find_runcv(NULL);
a3985cdc 3406
558b4424
FC
3407 /* At run time, we have to fetch the hints from PL_curcop. */
3408 PL_hints = PL_curcop->cop_hints;
3409 if (PL_hints & HINT_LOCALIZE_HH) {
3410 /* SAVEHINTS created a new HV in PL_hintgv, which we
3411 need to GC */
3412 SvREFCNT_dec(GvHV(PL_hintgv));
3413 GvHV(PL_hintgv) =
3414 refcounted_he_chain_2hv(PL_curcop->cop_hints_hash, 0);
3415 hv_magic(GvHV(PL_hintgv), NULL, PERL_MAGIC_hints);
3416 }
3417 SAVECOMPILEWARNINGS();
3418 PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
3419 cophh_free(CopHINTHASH_get(&PL_compiling));
3420 /* XXX Does this need to avoid copying a label? */
3421 PL_compiling.cop_hints_hash
3422 = cophh_copy(PL_curcop->cop_hints_hash);
3423 }
3424
533c011a 3425 PL_op = &dummy;
13b51b79 3426 PL_op->op_type = OP_ENTEREVAL;
533c011a 3427 PL_op->op_flags = 0; /* Avoid uninit warning. */
923e4eb5 3428 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
6b75f042 3429 PUSHEVAL(cx, 0);
634d6919
GG
3430 need_catch = CATCH_GET;
3431 CATCH_SET(TRUE);
a3985cdc
DM
3432
3433 if (runtime)
f45b078d 3434 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq, NULL);
a3985cdc 3435 else
f45b078d 3436 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax, NULL);
634d6919 3437 CATCH_SET(need_catch);
13b51b79 3438 POPBLOCK(cx,PL_curpm);
e84b9f1f 3439 POPEVAL(cx);
c277df42
IZ
3440
3441 (*startop)->op_type = OP_NULL;
22c35a8c 3442 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
f3548bdc 3443 /* XXX DAPM do this properly one year */
502c6561 3444 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
d343c3ef 3445 LEAVE_with_name("eval");
923e4eb5 3446 if (IN_PERL_COMPILETIME)
623e6609 3447 CopHINTS_set(&PL_compiling, PL_hints);
d1ca3daa 3448#ifdef OP_IN_REGISTER
6b88bc9c 3449 op = PL_opsave;
d1ca3daa 3450#endif
9d4ba2ae
AL
3451 PERL_UNUSED_VAR(newsp);
3452 PERL_UNUSED_VAR(optype);
3453
410be5db 3454 return PL_eval_start;
c277df42
IZ
3455}
3456
a3985cdc
DM
3457
3458/*
3459=for apidoc find_runcv
3460
3461Locate the CV corresponding to the currently executing sub or eval.
d819b83a
DM
3462If db_seqp is non_null, skip CVs that are in the DB package and populate
3463*db_seqp with the cop sequence number at the point that the DB:: code was
3464entered. (allows debuggers to eval in the scope of the breakpoint rather
cf525c36 3465than in the scope of the debugger itself).
a3985cdc
DM
3466
3467=cut
3468*/
3469
3470CV*
d819b83a 3471Perl_find_runcv(pTHX_ U32 *db_seqp)
a3985cdc 3472{
97aff369 3473 dVAR;
a3985cdc 3474 PERL_SI *si;
a3985cdc 3475
d819b83a
DM
3476 if (db_seqp)
3477 *db_seqp = PL_curcop->cop_seq;
a3985cdc 3478 for (si = PL_curstackinfo; si; si = si->si_prev) {
06b5626a 3479 I32 ix;
a3985cdc 3480 for (ix = si->si_cxix; ix >= 0; ix--) {
06b5626a 3481 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
d819b83a 3482 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1b6737cc 3483 CV * const cv = cx->blk_sub.cv;
d819b83a
DM
3484 /* skip DB:: code */
3485 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
3486 *db_seqp = cx->blk_oldcop->cop_seq;
3487 continue;
3488 }
3489 return cv;
3490 }
a3985cdc 3491 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
676a678a 3492 return cx->blk_eval.cv;
a3985cdc
DM
3493 }
3494 }
3495 return PL_main_cv;
3496}
3497
3498
27e90453
DM
3499/* Run yyparse() in a setjmp wrapper. Returns:
3500 * 0: yyparse() successful
3501 * 1: yyparse() failed
3502 * 3: yyparse() died
3503 */
3504STATIC int
28ac2b49 3505S_try_yyparse(pTHX_ int gramtype)
27e90453
DM
3506{
3507 int ret;
3508 dJMPENV;
3509
3510 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
3511 JMPENV_PUSH(ret);
3512 switch (ret) {
3513 case 0:
28ac2b49 3514 ret = yyparse(gramtype) ? 1 : 0;
27e90453
DM
3515 break;
3516 case 3:
3517 break;
3518 default:
3519 JMPENV_POP;
3520 JMPENV_JUMP(ret);
3521 /* NOTREACHED */
3522 }
3523 JMPENV_POP;
3524 return ret;
3525}
3526
3527
a3985cdc
DM
3528/* Compile a require/do, an eval '', or a /(?{...})/.
3529 * In the last case, startop is non-null, and contains the address of
3530 * a pointer that should be set to the just-compiled code.
3531 * outside is the lexically enclosing CV (if any) that invoked us.
410be5db
DM
3532 * Returns a bool indicating whether the compile was successful; if so,
3533 * PL_eval_start contains the first op of the compiled ocde; otherwise,
3534 * pushes undef (also croaks if startop != NULL).
a3985cdc
DM
3535 */
3536
12a680b7 3537/* This function is called from three places, sv_compile_2op, pp_require
7d116edc
FC
3538 * and pp_entereval. These can be distinguished as follows:
3539 * sv_compile_2op - startop is non-null
3840c57b
FC
3540 * pp_require - startop is null; saveop is not entereval
3541 * pp_entereval - startop is null; saveop is entereval
7d116edc
FC
3542 */
3543
410be5db 3544STATIC bool
f45b078d 3545S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq, HV *hh)
a0d0e21e 3546{
27da23d5 3547 dVAR; dSP;
46c461b5 3548 OP * const saveop = PL_op;
f45b078d 3549 COP * const oldcurcop = PL_curcop;
27e90453
DM
3550 bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
3551 int yystatus;
676a678a 3552 CV *evalcv;
a0d0e21e 3553
27e90453 3554 PL_in_eval = (in_require
6dc8a9e4
IZ
3555 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
3556 : EVAL_INEVAL);
a0d0e21e 3557
1ce6579f 3558 PUSHMARK(SP);
3559
676a678a
Z
3560 evalcv = MUTABLE_CV(newSV_type(SVt_PVCV));
3561 CvEVAL_on(evalcv);
2090ab20 3562 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
676a678a 3563 cxstack[cxstack_ix].blk_eval.cv = evalcv;
86a64801 3564 cxstack[cxstack_ix].blk_gimme = gimme;
2090ab20 3565
676a678a
Z
3566 CvOUTSIDE_SEQ(evalcv) = seq;
3567 CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
a3985cdc 3568
dd2155a4 3569 /* set up a scratch pad */
a0d0e21e 3570
676a678a 3571 CvPADLIST(evalcv) = pad_new(padnew_SAVE);
cecbe010 3572 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
2c05e328 3573
07055b4c 3574
81d86705 3575 if (!PL_madskills)
676a678a 3576 SAVEMORTALIZESV(evalcv); /* must remain until end of current statement */
748a9306 3577
a0d0e21e
LW
3578 /* make sure we compile in the right package */
3579
ed094faf 3580 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
03d9f026
FC
3581 SAVEGENERICSV(PL_curstash);
3582 PL_curstash = (HV *)SvREFCNT_inc_simple(CopSTASH(PL_curcop));
a0d0e21e 3583 }
3c10abe3 3584 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3280af22
NIS
3585 SAVESPTR(PL_beginav);
3586 PL_beginav = newAV();
3587 SAVEFREESV(PL_beginav);
3c10abe3
AG
3588 SAVESPTR(PL_unitcheckav);
3589 PL_unitcheckav = newAV();
3590 SAVEFREESV(PL_unitcheckav);
a0d0e21e 3591
81d86705 3592#ifdef PERL_MAD
9da243ce 3593 SAVEBOOL(PL_madskills);
81d86705
NC
3594 PL_madskills = 0;
3595#endif
3596
676a678a
Z
3597 if (!startop) ENTER_with_name("evalcomp");
3598 SAVESPTR(PL_compcv);
3599 PL_compcv = evalcv;
3600
a0d0e21e
LW
3601 /* try to compile it */
3602
5f66b61c 3603 PL_eval_root = NULL;
3280af22 3604 PL_curcop = &PL_compiling;
5f66b61c 3605 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
faef0170 3606 PL_in_eval |= EVAL_KEEPERR;
ab69dbc2
RGS
3607 else
3608 CLEAR_ERRSV();
27e90453 3609
f45b078d 3610 if (!startop) {
3840c57b 3611 bool clear_hints = saveop->op_type != OP_ENTEREVAL;
f45b078d 3612 SAVEHINTS();
3840c57b 3613 if (clear_hints) {
f45b078d
FC
3614 PL_hints = 0;
3615 hv_clear(GvHV(PL_hintgv));
3616 }
3617 else {
3618 PL_hints = saveop->op_private & OPpEVAL_COPHH
3619 ? oldcurcop->cop_hints : saveop->op_targ;
3620 if (hh) {
3621 /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
3622 SvREFCNT_dec(GvHV(PL_hintgv));
3623 GvHV(PL_hintgv) = hh;
3624 }
3625 }
3626 SAVECOMPILEWARNINGS();
3840c57b 3627 if (clear_hints) {
f45b078d
FC
3628 if (PL_dowarn & G_WARN_ALL_ON)
3629 PL_compiling.cop_warnings = pWARN_ALL ;
3630 else if (PL_dowarn & G_WARN_ALL_OFF)
3631 PL_compiling.cop_warnings = pWARN_NONE ;
3632 else
3633 PL_compiling.cop_warnings = pWARN_STD ;
3634 }
3635 else {
3636 PL_compiling.cop_warnings =
3637 DUP_WARNINGS(oldcurcop->cop_warnings);
3638 cophh_free(CopHINTHASH_get(&PL_compiling));
3639 if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) {
3640 /* The label, if present, is the first entry on the chain. So rather
3641 than writing a blank label in front of it (which involves an
3642 allocation), just use the next entry in the chain. */
3643 PL_compiling.cop_hints_hash
3644 = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next);
3645 /* Check the assumption that this removed the label. */
3646 assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
3647 }
3648 else
3649 PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash);
3650 }
3651 }
3652
a88d97bf 3653 CALL_BLOCK_HOOKS(bhk_eval, saveop);
52db365a 3654
27e90453
DM
3655 /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
3656 * so honour CATCH_GET and trap it here if necessary */
3657
28ac2b49 3658 yystatus = (!in_require && CATCH_GET) ? S_try_yyparse(aTHX_ GRAMPROG) : yyparse(GRAMPROG);
27e90453
DM
3659
3660 if (yystatus || PL_parser->error_count || !PL_eval_root) {
0c58d367 3661 SV **newsp; /* Used by POPBLOCK. */
d164302a 3662 PERL_CONTEXT *cx;
27e90453 3663 I32 optype; /* Used by POPEVAL. */
d164302a 3664 SV *namesv;
bfed75c6 3665
d164302a
GG
3666 cx = NULL;
3667 namesv = NULL;
27e90453
DM
3668 PERL_UNUSED_VAR(newsp);
3669 PERL_UNUSED_VAR(optype);
3670
c86ffc32
DM
3671 /* note that if yystatus == 3, then the EVAL CX block has already
3672 * been popped, and various vars restored */
533c011a 3673 PL_op = saveop;
27e90453 3674 if (yystatus != 3) {
c86ffc32
DM
3675 if (PL_eval_root) {
3676 op_free(PL_eval_root);
3677 PL_eval_root = NULL;
3678 }
27e90453
DM
3679 SP = PL_stack_base + POPMARK; /* pop original mark */
3680 if (!startop) {
3681 POPBLOCK(cx,PL_curpm);
3682 POPEVAL(cx);
b6494f15 3683 namesv = cx->blk_eval.old_namesv;
27e90453 3684 }
bbde7ba3 3685 /* POPBLOCK renders LEAVE_with_name("evalcomp") unnecessary. */
27e90453 3686 LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE. */
cd6472fc 3687 }
9d4ba2ae 3688
27e90453 3689 if (in_require) {
b6494f15
VP
3690 if (!cx) {
3691 /* If cx is still NULL, it means that we didn't go in the
3692 * POPEVAL branch. */
3693 cx = &cxstack[cxstack_ix];
3694 assert(CxTYPE(cx) == CXt_EVAL);
3695 namesv = cx->blk_eval.old_namesv;
3696 }
3697 (void)hv_store(GvHVn(PL_incgv),
ecad31f0 3698 SvPVX_const(namesv),
c60dbbc3 3699 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
b6494f15 3700 &PL_sv_undef, 0);
ecad31f0
BF
3701 Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
3702 SVfARG(ERRSV
3703 ? ERRSV
3704 : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
5a844595
GS
3705 }
3706 else if (startop) {
27e90453
DM
3707 if (yystatus != 3) {
3708 POPBLOCK(cx,PL_curpm);
3709 POPEVAL(cx);
3710 }
ecad31f0
BF
3711 Perl_croak(aTHX_ "%"SVf"Compilation failed in regexp",
3712 SVfARG(ERRSV
3713 ? ERRSV
3714 : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
7a2e2cd6 3715 }
9d7f88dd 3716 else {
ecad31f0 3717 if (!*(SvPVx_nolen_const(ERRSV))) {
6502358f 3718 sv_setpvs(ERRSV, "Compilation error");
9d7f88dd
SR
3719 }
3720 }
2bf54cc6 3721 if (gimme != G_ARRAY) PUSHs(&PL_sv_undef);
410be5db
DM
3722 PUTBACK;
3723 return FALSE;
a0d0e21e 3724 }
bbde7ba3 3725 else if (!startop) LEAVE_with_name("evalcomp");
57843af0 3726 CopLINE_set(&PL_compiling, 0);
c277df42 3727 if (startop) {
3280af22 3728 *startop = PL_eval_root;
c277df42 3729 } else
3280af22 3730 SAVEFREEOP(PL_eval_root);
0c58d367 3731
a0d0e21e
LW
3732 DEBUG_x(dump_eval());
3733
55497cff 3734 /* Register with debugger: */
6482a30d 3735 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
b96d8cd9 3736 CV * const cv = get_cvs("DB::postponed", 0);
55497cff 3737 if (cv) {
3738 dSP;
924508f0 3739 PUSHMARK(SP);
ad64d0ec 3740 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
55497cff 3741 PUTBACK;
ad64d0ec 3742 call_sv(MUTABLE_SV(cv), G_DISCARD);
55497cff 3743 }
3744 }
3745
8ed49485
FC
3746 if (PL_unitcheckav) {
3747 OP *es = PL_eval_start;
3c10abe3 3748 call_list(PL_scopestack_ix, PL_unitcheckav);
8ed49485
FC
3749 PL_eval_start = es;
3750 }
3c10abe3 3751
a0d0e21e
LW
3752 /* compiled okay, so do it */
3753
676a678a 3754 CvDEPTH(evalcv) = 1;
3280af22 3755 SP = PL_stack_base + POPMARK; /* pop original mark */
533c011a 3756 PL_op = saveop; /* The caller may need it. */
bc177e6b 3757 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
5dc0d613 3758
410be5db
DM
3759 PUTBACK;
3760 return TRUE;
a0d0e21e
LW
3761}
3762
a6c40364 3763STATIC PerlIO *
282b29ee 3764S_check_type_and_open(pTHX_ SV *name)
ce8abf5f
SP
3765{
3766 Stat_t st;
282b29ee
NC
3767 const char *p = SvPV_nolen_const(name);
3768 const int st_rc = PerlLIO_stat(p, &st);
df528165 3769
7918f24d
NC
3770 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3771
6b845e56 3772 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
4608196e 3773 return NULL;
ce8abf5f
SP
3774 }
3775
ccb84406 3776#if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
639dfab0 3777 return PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
ccb84406 3778#else
282b29ee 3779 return PerlIO_open(p, PERL_SCRIPT_MODE);
ccb84406 3780#endif
ce8abf5f
SP
3781}
3782
75c20bac 3783#ifndef PERL_DISABLE_PMC
ce8abf5f 3784STATIC PerlIO *
282b29ee 3785S_doopen_pm(pTHX_ SV *name)
b295d113 3786{
282b29ee
NC
3787 STRLEN namelen;
3788 const char *p = SvPV_const(name, namelen);
b295d113 3789
7918f24d
NC
3790 PERL_ARGS_ASSERT_DOOPEN_PM;
3791
282b29ee 3792 if (namelen > 3 && memEQs(p + namelen - 3, 3, ".pm")) {
eb70bb4a 3793 SV *const pmcsv = sv_newmortal();
a6c40364 3794 Stat_t pmcstat;
50b8ed39 3795
eb70bb4a 3796 SvSetSV_nosteal(pmcsv,name);
282b29ee 3797 sv_catpvn(pmcsv, "c", 1);
50b8ed39 3798
282b29ee
NC
3799 if (PerlLIO_stat(SvPV_nolen_const(pmcsv), &pmcstat) >= 0)
3800 return check_type_and_open(pmcsv);
a6c40364 3801 }
282b29ee 3802 return check_type_and_open(name);
75c20bac 3803}
7925835c 3804#else
282b29ee 3805# define doopen_pm(name) check_type_and_open(name)
7925835c 3806#endif /* !PERL_DISABLE_PMC */
b295d113 3807
a0d0e21e
LW
3808PP(pp_require)
3809{
27da23d5 3810 dVAR; dSP;
c09156bb 3811 register PERL_CONTEXT *cx;
a0d0e21e 3812 SV *sv;
5c144d81 3813 const char *name;
6132ea6c 3814 STRLEN len;
4492be7a
JM
3815 char * unixname;
3816 STRLEN unixlen;
62f5ad7a 3817#ifdef VMS
4492be7a 3818 int vms_unixname = 0;
62f5ad7a 3819#endif
c445ea15
AL
3820 const char *tryname = NULL;
3821 SV *namesv = NULL;
f54cb97a 3822 const I32 gimme = GIMME_V;
bbed91b5 3823 int filter_has_file = 0;
c445ea15 3824 PerlIO *tryrsfp = NULL;
34113e50 3825 SV *filter_cache = NULL;
c445ea15
AL
3826 SV *filter_state = NULL;
3827 SV *filter_sub = NULL;
3828 SV *hook_sv = NULL;
6ec9efec
JH
3829 SV *encoding;
3830 OP *op;
a0d0e21e
LW
3831
3832 sv = POPs;
d7aa5382 3833 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
d086148c 3834 sv = sv_2mortal(new_version(sv));
d7aa5382 3835 if (!sv_derived_from(PL_patchlevel, "version"))
ac0e6a2f 3836 upg_version(PL_patchlevel, TRUE);
149c1637 3837 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3cacfbb9 3838 if ( vcmp(sv,PL_patchlevel) <= 0 )
468aa647 3839 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
e753e3b1
FC
3840 SVfARG(sv_2mortal(vnormal(sv))),
3841 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3842 );
468aa647
RGS
3843 }
3844 else {
d1029faa
JP
3845 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3846 I32 first = 0;
3847 AV *lav;
3848 SV * const req = SvRV(sv);
85fbaab2 3849 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
d1029faa
JP
3850
3851 /* get the left hand term */
502c6561 3852 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
d1029faa
JP
3853
3854 first = SvIV(*av_fetch(lav,0,0));
3855 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
85fbaab2 3856 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
d1029faa
JP
3857 || av_len(lav) > 1 /* FP with > 3 digits */
3858 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3859 ) {
3860 DIE(aTHX_ "Perl %"SVf" required--this is only "
9d056fb0
FC
3861 "%"SVf", stopped",
3862 SVfARG(sv_2mortal(vnormal(req))),
3863 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3864 );
d1029faa
JP
3865 }
3866 else { /* probably 'use 5.10' or 'use 5.8' */
af61dbfd 3867 SV *hintsv;
d1029faa
JP
3868 I32 second = 0;
3869
3870 if (av_len(lav)>=1)
3871 second = SvIV(*av_fetch(lav,1,0));
3872
3873 second /= second >= 600 ? 100 : 10;
af61dbfd
NC
3874 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.0",
3875 (int)first, (int)second);
d1029faa
JP
3876 upg_version(hintsv, TRUE);
3877
3878 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3879 "--this is only %"SVf", stopped",
1be7d6f3
FC
3880 SVfARG(sv_2mortal(vnormal(req))),
3881 SVfARG(sv_2mortal(vnormal(sv_2mortal(hintsv)))),
3882 SVfARG(sv_2mortal(vnormal(PL_patchlevel)))
3883 );
d1029faa
JP
3884 }
3885 }
468aa647 3886 }
d7aa5382 3887
7dfde25d 3888 RETPUSHYES;
a0d0e21e 3889 }
5c144d81 3890 name = SvPV_const(sv, len);
6132ea6c 3891 if (!(name && len > 0 && *name))
cea2e8a9 3892 DIE(aTHX_ "Null filename used");
4633a7c4 3893 TAINT_PROPER("require");
4492be7a
JM
3894
3895
3896#ifdef VMS
3897 /* The key in the %ENV hash is in the syntax of file passed as the argument
3898 * usually this is in UNIX format, but sometimes in VMS format, which
3899 * can result in a module being pulled in more than once.
3900 * To prevent this, the key must be stored in UNIX format if the VMS
3901 * name can be translated to UNIX.
3902 */
3903 if ((unixname = tounixspec(name, NULL)) != NULL) {
3904 unixlen = strlen(unixname);
3905 vms_unixname = 1;
3906 }
3907 else
3908#endif
3909 {
3910 /* if not VMS or VMS name can not be translated to UNIX, pass it
3911 * through.
3912 */
3913 unixname = (char *) name;
3914 unixlen = len;
3915 }
44f8325f 3916 if (PL_op->op_type == OP_REQUIRE) {
4492be7a
JM
3917 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3918 unixname, unixlen, 0);
44f8325f
AL
3919 if ( svp ) {
3920 if (*svp != &PL_sv_undef)
3921 RETPUSHYES;
3922 else
087b5369
RD
3923 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3924 "Compilation failed in require", unixname);
44f8325f 3925 }
4d8b06f1 3926 }
a0d0e21e
LW
3927
3928 /* prepare to compile file */
3929
be4b629d 3930 if (path_is_absolute(name)) {
282b29ee 3931 /* At this point, name is SvPVX(sv) */
46fc3d4c 3932 tryname = name;
282b29ee 3933 tryrsfp = doopen_pm(sv);
bf4acbe4 3934 }
be4b629d 3935 if (!tryrsfp) {
44f8325f 3936 AV * const ar = GvAVn(PL_incgv);
a0d0e21e 3937 I32 i;
748a9306 3938#ifdef VMS
4492be7a 3939 if (vms_unixname)
46fc3d4c 3940#endif
3941 {
d0328fd7 3942 namesv = newSV_type(SVt_PV);
46fc3d4c 3943 for (i = 0; i <= AvFILL(ar); i++) {
df528165 3944 SV * const dirsv = *av_fetch(ar, i, TRUE);
bbed91b5 3945
ad64d0ec 3946 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
c38a6530 3947 mg_get(dirsv);
bbed91b5
KF
3948 if (SvROK(dirsv)) {
3949 int count;
a3b58a99 3950 SV **svp;
bbed91b5
KF
3951 SV *loader = dirsv;
3952
e14e2dc8
NC
3953 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3954 && !sv_isobject(loader))
3955 {
502c6561 3956 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
bbed91b5
KF
3957 }
3958
b900a521 3959 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
44f0be63 3960 PTR2UV(SvRV(dirsv)), name);
349d4f2f 3961 tryname = SvPVX_const(namesv);
c445ea15 3962 tryrsfp = NULL;
bbed91b5 3963
d343c3ef 3964 ENTER_with_name("call_INC");
bbed91b5
KF
3965 SAVETMPS;
3966 EXTEND(SP, 2);
3967
3968 PUSHMARK(SP);
3969 PUSHs(dirsv);
3970 PUSHs(sv);
3971 PUTBACK;
e982885c
NC
3972 if (sv_isobject(loader))
3973 count = call_method("INC", G_ARRAY);
3974 else
3975 count = call_sv(loader, G_ARRAY);
bbed91b5
KF
3976 SPAGAIN;
3977
3978 if (count > 0) {
3979 int i = 0;
3980 SV *arg;
3981
3982 SP -= count - 1;
3983 arg = SP[i++];
3984
34113e50
NC
3985 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3986 && !isGV_with_GP(SvRV(arg))) {
3987 filter_cache = SvRV(arg);
74c765eb 3988 SvREFCNT_inc_simple_void_NN(filter_cache);
34113e50
NC
3989
3990 if (i < count) {
3991 arg = SP[i++];
3992 }
3993 }
3994
6e592b3a 3995 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
bbed91b5
KF
3996 arg = SvRV(arg);
3997 }
3998
6e592b3a 3999 if (isGV_with_GP(arg)) {
159b6efe 4000 IO * const io = GvIO((const GV *)arg);
bbed91b5
KF
4001
4002 ++filter_has_file;
4003
4004 if (io) {
4005 tryrsfp = IoIFP(io);
0f7de14d
NC
4006 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
4007 PerlIO_close(IoOFP(io));
bbed91b5 4008 }
0f7de14d
NC
4009 IoIFP(io) = NULL;
4010 IoOFP(io) = NULL;
bbed91b5
KF
4011 }
4012
4013 if (i < count) {
4014 arg = SP[i++];
4015 }
4016 }
4017
4018 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVCV) {
4019 filter_sub = arg;
74c765eb 4020 SvREFCNT_inc_simple_void_NN(filter_sub);
bbed91b5
KF
4021
4022 if (i < count) {
4023 filter_state = SP[i];
b37c2d43 4024 SvREFCNT_inc_simple_void(filter_state);
bbed91b5 4025 }
34113e50 4026 }
bbed91b5 4027
34113e50
NC
4028 if (!tryrsfp && (filter_cache || filter_sub)) {
4029 tryrsfp = PerlIO_open(BIT_BUCKET,
4030 PERL_SCRIPT_MODE);
bbed91b5 4031 }
1d06aecd 4032 SP--;
bbed91b5
KF
4033 }
4034
4035 PUTBACK;
4036 FREETMPS;
d343c3ef 4037 LEAVE_with_name("call_INC");
bbed91b5 4038
c5f55552
NC
4039 /* Adjust file name if the hook has set an %INC entry.
4040 This needs to happen after the FREETMPS above. */
4041 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
4042 if (svp)
4043 tryname = SvPV_nolen_const(*svp);
4044
bbed91b5 4045 if (tryrsfp) {
89ccab8c 4046 hook_sv = dirsv;
bbed91b5
KF
4047 break;
4048 }
4049
4050 filter_has_file = 0;
34113e50
NC
4051 if (filter_cache) {
4052 SvREFCNT_dec(filter_cache);
4053 filter_cache = NULL;
4054 }
bbed91b5
KF
4055 if (filter_state) {
4056 SvREFCNT_dec(filter_state);
c445ea15 4057 filter_state = NULL;
bbed91b5
KF
4058 }
4059 if (filter_sub) {
4060 SvREFCNT_dec(filter_sub);
c445ea15 4061 filter_sub = NULL;
bbed91b5
KF
4062 }
4063 }
4064 else {
be4b629d 4065 if (!path_is_absolute(name)
be4b629d 4066 ) {
b640a14a
NC
4067 const char *dir;
4068 STRLEN dirlen;
4069
4070 if (SvOK(dirsv)) {
4071 dir = SvPV_const(dirsv, dirlen);
4072 } else {
4073 dir = "";
4074 dirlen = 0;
4075 }
4076
e37778c2 4077#ifdef VMS
bbed91b5 4078 char *unixdir;
c445ea15 4079 if ((unixdir = tounixpath(dir, NULL)) == NULL)
bbed91b5
KF
4080 continue;
4081 sv_setpv(namesv, unixdir);
4082 sv_catpv(namesv, unixname);
e37778c2
NC
4083#else
4084# ifdef __SYMBIAN32__
27da23d5
JH
4085 if (PL_origfilename[0] &&
4086 PL_origfilename[1] == ':' &&
4087 !(dir[0] && dir[1] == ':'))
4088 Perl_sv_setpvf(aTHX_ namesv,
4089 "%c:%s\\%s",
4090 PL_origfilename[0],
4091 dir, name);
4092 else
4093 Perl_sv_setpvf(aTHX_ namesv,
4094 "%s\\%s",
4095 dir, name);
e37778c2 4096# else
b640a14a
NC
4097 /* The equivalent of
4098 Perl_sv_setpvf(aTHX_ namesv, "%s/%s", dir, name);
4099 but without the need to parse the format string, or
4100 call strlen on either pointer, and with the correct
4101 allocation up front. */
4102 {
4103 char *tmp = SvGROW(namesv, dirlen + len + 2);
4104
4105 memcpy(tmp, dir, dirlen);
4106 tmp +=dirlen;
4107 *tmp++ = '/';
4108 /* name came from an SV, so it will have a '\0' at the
4109 end that we can copy as part of this memcpy(). */
4110 memcpy(tmp, name, len + 1);
4111
4112 SvCUR_set(namesv, dirlen + len + 1);
282b29ee 4113 SvPOK_on(namesv);
b640a14a 4114 }
27da23d5 4115# endif
bf4acbe4 4116#endif
bbed91b5 4117 TAINT_PROPER("require");
349d4f2f 4118 tryname = SvPVX_const(namesv);
282b29ee 4119 tryrsfp = doopen_pm(namesv);
bbed91b5 4120 if (tryrsfp) {
e63be746
RGS
4121 if (tryname[0] == '.' && tryname[1] == '/') {
4122 ++tryname;
4123 while (*++tryname == '/');
4124 }
bbed91b5
KF
4125 break;
4126 }
ff806af2
DM
4127 else if (errno == EMFILE)
4128 /* no point in trying other paths if out of handles */
4129 break;
be4b629d 4130 }
46fc3d4c 4131 }
a0d0e21e
LW
4132 }
4133 }
4134 }
b2ef6d44 4135 sv_2mortal(namesv);
a0d0e21e 4136 if (!tryrsfp) {
533c011a 4137 if (PL_op->op_type == OP_REQUIRE) {
e31de809 4138 if(errno == EMFILE) {
c9d5e35e
NC
4139 /* diag_listed_as: Can't locate %s */
4140 DIE(aTHX_ "Can't locate %s: %s", name, Strerror(errno));
e31de809
SP
4141 } else {
4142 if (namesv) { /* did we lookup @INC? */
44f8325f 4143 AV * const ar = GvAVn(PL_incgv);
e31de809 4144 I32 i;
c9d5e35e
NC
4145 SV *const inc = newSVpvs_flags("", SVs_TEMP);
4146 for (i = 0; i <= AvFILL(ar); i++) {
4147 sv_catpvs(inc, " ");
4148 sv_catsv(inc, *av_fetch(ar, i, TRUE));
4149 }
4150
4151 /* diag_listed_as: Can't locate %s */
4152 DIE(aTHX_
4153 "Can't locate %s in @INC%s%s (@INC contains:%" SVf ")",
4154 name,
686c4ca0
NC
4155 (memEQ(name + len - 2, ".h", 3)
4156 ? " (change .h to .ph maybe?) (did you run h2ph?)" : ""),
4157 (memEQ(name + len - 3, ".ph", 4)
c9d5e35e
NC
4158 ? " (did you run h2ph?)" : ""),
4159 inc
4160 );
4161 }
2683423c 4162 }
c9d5e35e 4163 DIE(aTHX_ "Can't locate %s", name);
a0d0e21e
LW
4164 }
4165
4166 RETPUSHUNDEF;
4167 }
d8bfb8bd 4168 else
93189314 4169 SETERRNO(0, SS_NORMAL);
a0d0e21e
LW
4170
4171 /* Assume success here to prevent recursive requirement. */
238d24b4 4172 /* name is never assigned to again, so len is still strlen(name) */
d3a4e64e 4173 /* Check whether a hook in @INC has already filled %INC */
44f8325f 4174 if (!hook_sv) {
4492be7a 4175 (void)hv_store(GvHVn(PL_incgv),
b2ef6d44 4176 unixname, unixlen, newSVpv(tryname,0),0);
44f8325f 4177 } else {
4492be7a 4178 SV** const svp = hv_fetch(GvHVn(PL_incgv), unixname, unixlen, 0);
44f8325f 4179 if (!svp)
4492be7a
JM
4180 (void)hv_store(GvHVn(PL_incgv),
4181 unixname, unixlen, SvREFCNT_inc_simple(hook_sv), 0 );
d3a4e64e 4182 }
a0d0e21e 4183
d343c3ef 4184 ENTER_with_name("eval");
a0d0e21e 4185 SAVETMPS;
b2ef6d44
FC
4186 SAVECOPFILE_FREE(&PL_compiling);
4187 CopFILE_set(&PL_compiling, tryname);
8eaa0acf 4188 lex_start(NULL, tryrsfp, 0);
e50aee73 4189
34113e50 4190 if (filter_sub || filter_cache) {
4464f08e
NC
4191 /* We can use the SvPV of the filter PVIO itself as our cache, rather
4192 than hanging another SV from it. In turn, filter_add() optionally
4193 takes the SV to use as the filter (or creates a new SV if passed
4194 NULL), so simply pass in whatever value filter_cache has. */
4195 SV * const datasv = filter_add(S_run_user_filter, filter_cache);
bbed91b5 4196 IoLINES(datasv) = filter_has_file;
159b6efe
NC
4197 IoTOP_GV(datasv) = MUTABLE_GV(filter_state);
4198 IoBOTTOM_GV(datasv) = MUTABLE_GV(filter_sub);
bbed91b5
KF
4199 }
4200
4201 /* switch to eval mode */
a0d0e21e 4202 PUSHBLOCK(cx, CXt_EVAL, SP);
6b75f042 4203 PUSHEVAL(cx, name);
f39bc417 4204 cx->blk_eval.retop = PL_op->op_next;
a0d0e21e 4205
57843af0
GS
4206 SAVECOPLINE(&PL_compiling);
4207 CopLINE_set(&PL_compiling, 0);
a0d0e21e
LW
4208
4209 PUTBACK;
6ec9efec
JH
4210
4211 /* Store and reset encoding. */
4212 encoding = PL_encoding;
c445ea15 4213 PL_encoding = NULL;
6ec9efec 4214
f45b078d 4215 if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq, NULL))
410be5db
DM
4216 op = DOCATCH(PL_eval_start);
4217 else
4218 op = PL_op->op_next;
bfed75c6 4219
6ec9efec
JH
4220 /* Restore encoding. */
4221 PL_encoding = encoding;
4222
4223 return op;
a0d0e21e
LW
4224}
4225
996c9baa
VP
4226/* This is a op added to hold the hints hash for
4227 pp_entereval. The hash can be modified by the code
4228 being eval'ed, so we return a copy instead. */
4229
4230PP(pp_hintseval)
4231{
4232 dVAR;
4233 dSP;
defdfed5 4234 mXPUSHs(MUTABLE_SV(hv_copy_hints_hv(MUTABLE_HV(cSVOP_sv))));
996c9baa
VP
4235 RETURN;
4236}
4237
4238
a0d0e21e
LW
4239PP(pp_entereval)
4240{
27da23d5 4241 dVAR; dSP;
c09156bb 4242 register PERL_CONTEXT *cx;
0d863452 4243 SV *sv;
890ce7af 4244 const I32 gimme = GIMME_V;
fd06b02c 4245 const U32 was = PL_breakable_sub_gen;
83ee9e09 4246 char tbuf[TYPE_DIGITS(long) + 12];
78da7625 4247 bool saved_delete = FALSE;
83ee9e09 4248 char *tmpbuf = tbuf;
a0d0e21e 4249 STRLEN len;
a3985cdc 4250 CV* runcv;
0abcdfa4 4251 U32 seq, lex_flags = 0;
c445ea15 4252 HV *saved_hh = NULL;
60d63348 4253 const bool bytes = PL_op->op_private & OPpEVAL_BYTES;
e389bba9 4254
0d863452 4255 if (PL_op->op_private & OPpEVAL_HAS_HH) {
85fbaab2 4256 saved_hh = MUTABLE_HV(SvREFCNT_inc(POPs));
0d863452 4257 }
bc344123
FC
4258 else if (PL_hints & HINT_LOCALIZE_HH || (
4259 PL_op->op_private & OPpEVAL_COPHH
4260 && PL_curcop->cop_hints & HINT_LOCALIZE_HH
4261 )) {
7d789282
FC
4262 saved_hh = cop_hints_2hv(PL_curcop, 0);
4263 hv_magic(saved_hh, NULL, PERL_MAGIC_hints);
4264 }
0d863452 4265 sv = POPs;
895b760f
DM
4266 if (!SvPOK(sv)) {
4267 /* make sure we've got a plain PV (no overload etc) before testing
4268 * for taint. Making a copy here is probably overkill, but better
4269 * safe than sorry */
0479a84a
NC
4270 STRLEN len;
4271 const char * const p = SvPV_const(sv, len);
4272
4273 sv = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
0abcdfa4 4274 lex_flags |= LEX_START_COPIED;
7d789282 4275
60d63348 4276 if (bytes && SvUTF8(sv))
7d789282
FC
4277 SvPVbyte_force(sv, len);
4278 }
60d63348 4279 else if (bytes && SvUTF8(sv)) {
e1fa07e3 4280 /* Don't modify someone else's scalar */
7d789282
FC
4281 STRLEN len;
4282 sv = newSVsv(sv);
5cefc8c1 4283 (void)sv_2mortal(sv);
7d789282 4284 SvPVbyte_force(sv,len);
0abcdfa4 4285 lex_flags |= LEX_START_COPIED;
895b760f 4286 }
a0d0e21e 4287
af2d3def 4288 TAINT_IF(SvTAINTED(sv));
748a9306 4289 TAINT_PROPER("eval");
a0d0e21e 4290
d343c3ef 4291 ENTER_with_name("eval");
0abcdfa4 4292 lex_start(sv, NULL, lex_flags | (PL_op->op_private & OPpEVAL_UNICODE
60d63348
FC
4293 ? LEX_IGNORE_UTF8_HINTS
4294 : bytes ? LEX_EVALBYTES : LEX_START_SAME_FILTER
0abcdfa4 4295 )
60d63348 4296 );
748a9306 4297 SAVETMPS;
ac27b0f5 4298
a0d0e21e
LW
4299 /* switch to eval mode */
4300
83ee9e09 4301 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
8b38226b
AL
4302 SV * const temp_sv = sv_newmortal();
4303 Perl_sv_setpvf(aTHX_ temp_sv, "_<(eval %lu)[%s:%"IVdf"]",
83ee9e09
GS
4304 (unsigned long)++PL_evalseq,
4305 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
8b38226b
AL
4306 tmpbuf = SvPVX(temp_sv);
4307 len = SvCUR(temp_sv);
83ee9e09
GS
4308 }
4309 else
d9fad198 4310 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned long)++PL_evalseq);
f4dd75d9 4311 SAVECOPFILE_FREE(&PL_compiling);
57843af0 4312 CopFILE_set(&PL_compiling, tmpbuf+2);
f4dd75d9 4313 SAVECOPLINE(&PL_compiling);
57843af0 4314 CopLINE_set(&PL_compiling, 1);
d819b83a
DM
4315 /* special case: an eval '' executed within the DB package gets lexically
4316 * placed in the first non-DB CV rather than the current CV - this
4317 * allows the debugger to execute code, find lexicals etc, in the
4318 * scope of the code being debugged. Passing &seq gets find_runcv
4319 * to do the dirty work for us */
4320 runcv = find_runcv(&seq);
a0d0e21e 4321
6b35e009 4322 PUSHBLOCK(cx, (CXt_EVAL|CXp_REAL), SP);
6b75f042 4323 PUSHEVAL(cx, 0);
f39bc417 4324 cx->blk_eval.retop = PL_op->op_next;
a0d0e21e
LW
4325
4326 /* prepare to compile string */
4327
a44e3ce2 4328 if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
bdc0bf6f 4329 save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
78da7625 4330 else {
c8cb8d55
FC
4331 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
4332 deleting the eval's FILEGV from the stash before gv_check() runs
4333 (i.e. before run-time proper). To work around the coredump that
4334 ensues, we always turn GvMULTI_on for any globals that were
4335 introduced within evals. See force_ident(). GSAR 96-10-12 */
78da7625
FC
4336 char *const safestr = savepvn(tmpbuf, len);
4337 SAVEDELETE(PL_defstash, safestr, len);
4338 saved_delete = TRUE;
4339 }
4340
a0d0e21e 4341 PUTBACK;
f9bddea7 4342
f45b078d 4343 if (doeval(gimme, NULL, runcv, seq, saved_hh)) {
f9bddea7
NC
4344 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4345 ? (PERLDB_LINE || PERLDB_SAVESRC)
4346 : PERLDB_SAVESRC_NOSUBS) {
4347 /* Retain the filegv we created. */
78da7625 4348 } else if (!saved_delete) {
f9bddea7
NC
4349 char *const safestr = savepvn(tmpbuf, len);
4350 SAVEDELETE(PL_defstash, safestr, len);
4351 }
4352 return DOCATCH(PL_eval_start);
4353 } else {
486ec47a 4354 /* We have already left the scope set up earlier thanks to the LEAVE
f9bddea7 4355 in doeval(). */
eb044b10
NC
4356 if (was != PL_breakable_sub_gen /* Some subs defined here. */
4357 ? (PERLDB_LINE || PERLDB_SAVESRC)
4358 : PERLDB_SAVESRC_INVALID) {
f9bddea7 4359 /* Retain the filegv we created. */
7857f360 4360 } else if (!saved_delete) {
f9bddea7
NC
4361 (void)hv_delete(PL_defstash, tmpbuf, len, G_DISCARD);
4362 }
4363 return PL_op->op_next;
4364 }
a0d0e21e
LW
4365}
4366
4367PP(pp_leaveeval)
4368{
27da23d5 4369 dVAR; dSP;
a0d0e21e
LW
4370 SV **newsp;
4371 PMOP *newpm;
4372 I32 gimme;
c09156bb 4373 register PERL_CONTEXT *cx;
a0d0e21e 4374 OP *retop;
06b5626a 4375 const U8 save_flags = PL_op -> op_flags;
a0d0e21e 4376 I32 optype;
b6494f15 4377 SV *namesv;
676a678a 4378 CV *evalcv;
a0d0e21e 4379
011c3814 4380 PERL_ASYNC_CHECK();
a0d0e21e
LW
4381 POPBLOCK(cx,newpm);
4382 POPEVAL(cx);
b6494f15 4383 namesv = cx->blk_eval.old_namesv;
f39bc417 4384 retop = cx->blk_eval.retop;
676a678a 4385 evalcv = cx->blk_eval.cv;
a0d0e21e 4386
a1f49e72 4387 TAINT_NOT;
b9d76716
VP
4388 SP = adjust_stack_on_leave((gimme == G_VOID) ? SP : newsp, SP, newsp,
4389 gimme, SVs_TEMP);
3280af22 4390 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e 4391
4fdae800 4392#ifdef DEBUGGING
676a678a 4393 assert(CvDEPTH(evalcv) == 1);
4fdae800 4394#endif
676a678a 4395 CvDEPTH(evalcv) = 0;
4fdae800 4396
1ce6579f 4397 if (optype == OP_REQUIRE &&
924508f0 4398 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
54310121 4399 {
1ce6579f 4400 /* Unassume the success we assumed earlier. */
b6494f15 4401 (void)hv_delete(GvHVn(PL_incgv),
ecad31f0 4402 SvPVX_const(namesv),
c60dbbc3 4403 SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
b6494f15
VP
4404 G_DISCARD);
4405 retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
4406 SVfARG(namesv));
c5df3096 4407 /* die_unwind() did LEAVE, or we won't be here */
f46d017c
GS
4408 }
4409 else {
d343c3ef 4410 LEAVE_with_name("eval");
8433848b 4411 if (!(save_flags & OPf_SPECIAL)) {
ab69dbc2 4412 CLEAR_ERRSV();
8433848b 4413 }
a0d0e21e 4414 }
a0d0e21e
LW
4415
4416 RETURNOP(retop);
4417}
4418
edb2152a
NC
4419/* Common code for Perl_call_sv and Perl_fold_constants, put here to keep it
4420 close to the related Perl_create_eval_scope. */
4421void
4422Perl_delete_eval_scope(pTHX)
a0d0e21e 4423{
edb2152a
NC
4424 SV **newsp;
4425 PMOP *newpm;
4426 I32 gimme;
c09156bb 4427 register PERL_CONTEXT *cx;
edb2152a
NC
4428 I32 optype;
4429
4430 POPBLOCK(cx,newpm);
4431 POPEVAL(cx);
4432 PL_curpm = newpm;
d343c3ef 4433 LEAVE_with_name("eval_scope");
edb2152a
NC
4434 PERL_UNUSED_VAR(newsp);
4435 PERL_UNUSED_VAR(gimme);
4436 PERL_UNUSED_VAR(optype);
4437}
a0d0e21e 4438
edb2152a
NC
4439/* Common-ish code salvaged from Perl_call_sv and pp_entertry, because it was
4440 also needed by Perl_fold_constants. */
4441PERL_CONTEXT *
4442Perl_create_eval_scope(pTHX_ U32 flags)
4443{
4444 PERL_CONTEXT *cx;
4445 const I32 gimme = GIMME_V;
4446
d343c3ef 4447 ENTER_with_name("eval_scope");
a0d0e21e
LW
4448 SAVETMPS;
4449
edb2152a 4450 PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp);
6b75f042 4451 PUSHEVAL(cx, 0);
a0d0e21e 4452
faef0170 4453 PL_in_eval = EVAL_INEVAL;
edb2152a
NC
4454 if (flags & G_KEEPERR)
4455 PL_in_eval |= EVAL_KEEPERR;
ab69dbc2
RGS
4456 else
4457 CLEAR_ERRSV();
edb2152a
NC
4458 if (flags & G_FAKINGEVAL) {
4459 PL_eval_root = PL_op; /* Only needed so that goto works right. */
4460 }
4461 return cx;
4462}
4463
4464PP(pp_entertry)
4465{
4466 dVAR;
df528165 4467 PERL_CONTEXT * const cx = create_eval_scope(0);
edb2152a 4468 cx->blk_eval.retop = cLOGOP->op_other->op_next;
533c011a 4469 return DOCATCH(PL_op->op_next);
a0d0e21e
LW
4470}
4471
4472PP(pp_leavetry)
4473{
27da23d5 4474 dVAR; dSP;
a0d0e21e
LW
4475 SV **newsp;
4476 PMOP *newpm;
4477 I32 gimme;
c09156bb 4478 register PERL_CONTEXT *cx;
a0d0e21e
LW
4479 I32 optype;
4480
011c3814 4481 PERL_ASYNC_CHECK();
a0d0e21e
LW
4482 POPBLOCK(cx,newpm);
4483 POPEVAL(cx);
9d4ba2ae 4484 PERL_UNUSED_VAR(optype);
a0d0e21e 4485
a1f49e72 4486 TAINT_NOT;
b9d76716 4487 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
3280af22 4488 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e 4489
d343c3ef 4490 LEAVE_with_name("eval_scope");
ab69dbc2 4491 CLEAR_ERRSV();
745cf2ff 4492 RETURN;
a0d0e21e
LW
4493}
4494
0d863452
RH
4495PP(pp_entergiven)
4496{
4497 dVAR; dSP;
4498 register PERL_CONTEXT *cx;
4499 const I32 gimme = GIMME_V;
4500
d343c3ef 4501 ENTER_with_name("given");
0d863452
RH
4502 SAVETMPS;
4503
87e4a53a 4504 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
f7010667 4505 sv_setsv_mg(PAD_SV(PL_op->op_targ), POPs);
0d863452
RH
4506
4507 PUSHBLOCK(cx, CXt_GIVEN, SP);
4508 PUSHGIVEN(cx);
4509
4510 RETURN;
4511}
4512
4513PP(pp_leavegiven)
4514{
4515 dVAR; dSP;
4516 register PERL_CONTEXT *cx;
4517 I32 gimme;
4518 SV **newsp;
4519 PMOP *newpm;
96a5add6 4520 PERL_UNUSED_CONTEXT;
0d863452
RH
4521
4522 POPBLOCK(cx,newpm);
4523 assert(CxTYPE(cx) == CXt_GIVEN);
0d863452 4524
25b991bf 4525 TAINT_NOT;
b9d76716 4526 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
25b991bf 4527 PL_curpm = newpm; /* Don't pop $1 et al till now */
0d863452 4528
d343c3ef 4529 LEAVE_with_name("given");
25b991bf 4530 RETURN;
0d863452
RH
4531}
4532
4533/* Helper routines used by pp_smartmatch */
4136a0f7 4534STATIC PMOP *
84679df5 4535S_make_matcher(pTHX_ REGEXP *re)
0d863452 4536{
97aff369 4537 dVAR;
0d863452 4538 PMOP *matcher = (PMOP *) newPMOP(OP_MATCH, OPf_WANT_SCALAR | OPf_STACKED);
7918f24d
NC
4539
4540 PERL_ARGS_ASSERT_MAKE_MATCHER;
4541
d6106309 4542 PM_SETRE(matcher, ReREFCNT_inc(re));
7918f24d 4543
0d863452 4544 SAVEFREEOP((OP *) matcher);
d343c3ef 4545 ENTER_with_name("matcher"); SAVETMPS;
0d863452
RH
4546 SAVEOP();
4547 return matcher;
4548}
4549
4136a0f7 4550STATIC bool
0d863452
RH
4551S_matcher_matches_sv(pTHX_ PMOP *matcher, SV *sv)
4552{
97aff369 4553 dVAR;
0d863452 4554 dSP;
7918f24d
NC
4555
4556 PERL_ARGS_ASSERT_MATCHER_MATCHES_SV;
0d863452
RH
4557
4558 PL_op = (OP *) matcher;
4559 XPUSHs(sv);
4560 PUTBACK;
897d3989 4561 (void) Perl_pp_match(aTHX);
0d863452
RH
4562 SPAGAIN;
4563 return (SvTRUEx(POPs));
4564}
4565
4136a0f7 4566STATIC void
0d863452
RH
4567S_destroy_matcher(pTHX_ PMOP *matcher)
4568{
97aff369 4569 dVAR;
7918f24d
NC
4570
4571 PERL_ARGS_ASSERT_DESTROY_MATCHER;
0d863452 4572 PERL_UNUSED_ARG(matcher);
7918f24d 4573
0d863452 4574 FREETMPS;
d343c3ef 4575 LEAVE_with_name("matcher");
0d863452
RH
4576}
4577
4578/* Do a smart match */
4579PP(pp_smartmatch)
4580{
d7c0d282 4581 DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
be88a5c3 4582 return do_smartmatch(NULL, NULL, 0);
0d863452
RH
4583}
4584
4b021f5f
RGS
4585/* This version of do_smartmatch() implements the
4586 * table of smart matches that is found in perlsyn.
0d863452 4587 */
4136a0f7 4588STATIC OP *
be88a5c3 4589S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied)
0d863452 4590{
97aff369 4591 dVAR;
0d863452
RH
4592 dSP;
4593
41e726ac 4594 bool object_on_left = FALSE;
0d863452
RH
4595 SV *e = TOPs; /* e is for 'expression' */
4596 SV *d = TOPm1s; /* d is for 'default', as in PL_defgv */
a566f585 4597
6f1401dc
DM
4598 /* Take care only to invoke mg_get() once for each argument.
4599 * Currently we do this by copying the SV if it's magical. */
4600 if (d) {
be88a5c3 4601 if (!copied && SvGMAGICAL(d))
6f1401dc
DM
4602 d = sv_mortalcopy(d);
4603 }
4604 else
4605 d = &PL_sv_undef;
4606
4607 assert(e);
4608 if (SvGMAGICAL(e))
4609 e = sv_mortalcopy(e);
4610
2c9d2554 4611 /* First of all, handle overload magic of the rightmost argument */
6d743019 4612 if (SvAMAGIC(e)) {
d7c0d282
DM
4613 SV * tmpsv;
4614 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
4615 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
4616
b900a653 4617 tmpsv = amagic_call(d, e, smart_amg, AMGf_noleft);
7c41e62e
RGS
4618 if (tmpsv) {
4619 SPAGAIN;
4620 (void)POPs;
4621 SETs(tmpsv);
4622 RETURN;
4623 }
d7c0d282 4624 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; continuing...\n"));
7c41e62e 4625 }
62ec5f58 4626
0d863452
RH
4627 SP -= 2; /* Pop the values */
4628
0d863452 4629
b0138e99 4630 /* ~~ undef */
62ec5f58 4631 if (!SvOK(e)) {
d7c0d282 4632 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-undef\n"));
62ec5f58 4633 if (SvOK(d))
33570f8b
RGS
4634 RETPUSHNO;
4635 else
62ec5f58 4636 RETPUSHYES;
33570f8b 4637 }
e67b97bd 4638
d7c0d282
DM
4639 if (sv_isobject(e) && (SvTYPE(SvRV(e)) != SVt_REGEXP)) {
4640 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Object\n"));
62ec5f58 4641 Perl_croak(aTHX_ "Smart matching a non-overloaded object breaks encapsulation");
d7c0d282 4642 }
41e726ac
RGS
4643 if (sv_isobject(d) && (SvTYPE(SvRV(d)) != SVt_REGEXP))
4644 object_on_left = TRUE;
62ec5f58 4645
b0138e99 4646 /* ~~ sub */
a4a197da 4647 if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVCV) {
0d863452 4648 I32 c;
41e726ac
RGS
4649 if (object_on_left) {
4650 goto sm_any_sub; /* Treat objects like scalars */
4651 }
4652 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
a4a197da
RGS
4653 /* Test sub truth for each key */
4654 HE *he;
4655 bool andedresults = TRUE;
4656 HV *hv = (HV*) SvRV(d);
168ff818 4657 I32 numkeys = hv_iterinit(hv);
d7c0d282 4658 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-CodeRef\n"));
168ff818 4659 if (numkeys == 0)
07edf497 4660 RETPUSHYES;
a4a197da 4661 while ( (he = hv_iternext(hv)) ) {
d7c0d282 4662 DEBUG_M(Perl_deb(aTHX_ " testing hash key...\n"));
d343c3ef 4663 ENTER_with_name("smartmatch_hash_key_test");
a4a197da
RGS
4664 SAVETMPS;
4665 PUSHMARK(SP);
4666 PUSHs(hv_iterkeysv(he));
4667 PUTBACK;
4668 c = call_sv(e, G_SCALAR);
4669 SPAGAIN;
4670 if (c == 0)
4671 andedresults = FALSE;
4672 else
4673 andedresults = SvTRUEx(POPs) && andedresults;
4674 FREETMPS;
d343c3ef 4675 LEAVE_with_name("smartmatch_hash_key_test");
a4a197da
RGS
4676 }
4677 if (andedresults)
4678 RETPUSHYES;
4679 else
4680 RETPUSHNO;
4681 }
4682 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4683 /* Test sub truth for each element */
4684 I32 i;
4685 bool andedresults = TRUE;
4686 AV *av = (AV*) SvRV(d);
4687 const I32 len = av_len(av);
d7c0d282 4688 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-CodeRef\n"));
168ff818 4689 if (len == -1)
07edf497 4690 RETPUSHYES;
a4a197da
RGS
4691 for (i = 0; i <= len; ++i) {
4692 SV * const * const svp = av_fetch(av, i, FALSE);
d7c0d282 4693 DEBUG_M(Perl_deb(aTHX_ " testing array element...\n"));
d343c3ef 4694 ENTER_with_name("smartmatch_array_elem_test");
a4a197da
RGS
4695 SAVETMPS;
4696 PUSHMARK(SP);
4697 if (svp)
4698 PUSHs(*svp);
4699 PUTBACK;
4700 c = call_sv(e, G_SCALAR);
4701 SPAGAIN;
4702 if (c == 0)
4703 andedresults = FALSE;
4704 else
4705 andedresults = SvTRUEx(POPs) && andedresults;
4706 FREETMPS;
d343c3ef 4707 LEAVE_with_name("smartmatch_array_elem_test");
a4a197da
RGS
4708 }
4709 if (andedresults)
4710 RETPUSHYES;
4711 else
4712 RETPUSHNO;
4713 }
4714 else {
41e726ac 4715 sm_any_sub:
d7c0d282 4716 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-CodeRef\n"));
d343c3ef 4717 ENTER_with_name("smartmatch_coderef");
a4a197da
RGS
4718 SAVETMPS;
4719 PUSHMARK(SP);
4720 PUSHs(d);
4721 PUTBACK;
4722 c = call_sv(e, G_SCALAR);
4723 SPAGAIN;
4724 if (c == 0)
4725 PUSHs(&PL_sv_no);
4726 else if (SvTEMP(TOPs))
4727 SvREFCNT_inc_void(TOPs);
4728 FREETMPS;
d343c3ef 4729 LEAVE_with_name("smartmatch_coderef");
a4a197da
RGS
4730 RETURN;
4731 }
0d863452 4732 }
b0138e99 4733 /* ~~ %hash */
61a621c6 4734 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
41e726ac
RGS
4735 if (object_on_left) {
4736 goto sm_any_hash; /* Treat objects like scalars */
4737 }
4738 else if (!SvOK(d)) {
d7c0d282 4739 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash ($a undef)\n"));
61a621c6
RGS
4740 RETPUSHNO;
4741 }
4742 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
0d863452
RH
4743 /* Check that the key-sets are identical */
4744 HE *he;
61a621c6 4745 HV *other_hv = MUTABLE_HV(SvRV(d));
0d863452
RH
4746 bool tied = FALSE;
4747 bool other_tied = FALSE;
4748 U32 this_key_count = 0,
4749 other_key_count = 0;
33ed63a2 4750 HV *hv = MUTABLE_HV(SvRV(e));
d7c0d282
DM
4751
4752 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Hash\n"));
0d863452 4753 /* Tied hashes don't know how many keys they have. */
33ed63a2 4754 if (SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
0d863452
RH
4755 tied = TRUE;
4756 }
ad64d0ec 4757 else if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied)) {
c445ea15 4758 HV * const temp = other_hv;
33ed63a2
RGS
4759 other_hv = hv;
4760 hv = temp;
0d863452
RH
4761 tied = TRUE;
4762 }
ad64d0ec 4763 if (SvTIED_mg((const SV *)other_hv, PERL_MAGIC_tied))
0d863452
RH
4764 other_tied = TRUE;
4765
33ed63a2 4766 if (!tied && HvUSEDKEYS((const HV *) hv) != HvUSEDKEYS(other_hv))
0d863452
RH
4767 RETPUSHNO;
4768
4769 /* The hashes have the same number of keys, so it suffices
4770 to check that one is a subset of the other. */
33ed63a2
RGS
4771 (void) hv_iterinit(hv);
4772 while ( (he = hv_iternext(hv)) ) {
b15feb55 4773 SV *key = hv_iterkeysv(he);
d7c0d282
DM
4774
4775 DEBUG_M(Perl_deb(aTHX_ " comparing hash key...\n"));
0d863452
RH
4776 ++ this_key_count;
4777
b15feb55 4778 if(!hv_exists_ent(other_hv, key, 0)) {
33ed63a2 4779 (void) hv_iterinit(hv); /* reset iterator */
0d863452
RH
4780 RETPUSHNO;
4781 }
4782 }
4783
4784 if (other_tied) {
4785 (void) hv_iterinit(other_hv);
4786 while ( hv_iternext(other_hv) )
4787 ++other_key_count;
4788 }
4789 else
4790 other_key_count = HvUSEDKEYS(other_hv);
4791
4792 if (this_key_count != other_key_count)
4793 RETPUSHNO;
4794 else
4795 RETPUSHYES;
4796 }
61a621c6
RGS
4797 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4798 AV * const other_av = MUTABLE_AV(SvRV(d));
c445ea15 4799 const I32 other_len = av_len(other_av) + 1;
0d863452 4800 I32 i;
33ed63a2 4801 HV *hv = MUTABLE_HV(SvRV(e));
71b0fb34 4802
d7c0d282 4803 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Hash\n"));
71b0fb34 4804 for (i = 0; i < other_len; ++i) {
c445ea15 4805 SV ** const svp = av_fetch(other_av, i, FALSE);
d7c0d282 4806 DEBUG_M(Perl_deb(aTHX_ " checking for key existence...\n"));
71b0fb34 4807 if (svp) { /* ??? When can this not happen? */
b15feb55 4808 if (hv_exists_ent(hv, *svp, 0))
71b0fb34
DK
4809 RETPUSHYES;
4810 }
0d863452 4811 }
71b0fb34 4812 RETPUSHNO;
0d863452 4813 }
a566f585 4814 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
d7c0d282 4815 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Hash\n"));
ea0c2dbd
RGS
4816 sm_regex_hash:
4817 {
4818 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4819 HE *he;
4820 HV *hv = MUTABLE_HV(SvRV(e));
4821
4822 (void) hv_iterinit(hv);
4823 while ( (he = hv_iternext(hv)) ) {
d7c0d282 4824 DEBUG_M(Perl_deb(aTHX_ " testing key against pattern...\n"));
ea0c2dbd
RGS
4825 if (matcher_matches_sv(matcher, hv_iterkeysv(he))) {
4826 (void) hv_iterinit(hv);
4827 destroy_matcher(matcher);
4828 RETPUSHYES;
4829 }
0d863452 4830 }
ea0c2dbd
RGS
4831 destroy_matcher(matcher);
4832 RETPUSHNO;
0d863452 4833 }
0d863452
RH
4834 }
4835 else {
41e726ac 4836 sm_any_hash:
d7c0d282 4837 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Hash\n"));
61a621c6 4838 if (hv_exists_ent(MUTABLE_HV(SvRV(e)), d, 0))
0d863452
RH
4839 RETPUSHYES;
4840 else
4841 RETPUSHNO;
4842 }
4843 }
b0138e99
RGS
4844 /* ~~ @array */
4845 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVAV) {
41e726ac
RGS
4846 if (object_on_left) {
4847 goto sm_any_array; /* Treat objects like scalars */
4848 }
4849 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
b0138e99
RGS
4850 AV * const other_av = MUTABLE_AV(SvRV(e));
4851 const I32 other_len = av_len(other_av) + 1;
4852 I32 i;
4853
d7c0d282 4854 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Array\n"));
b0138e99
RGS
4855 for (i = 0; i < other_len; ++i) {
4856 SV ** const svp = av_fetch(other_av, i, FALSE);
d7c0d282
DM
4857
4858 DEBUG_M(Perl_deb(aTHX_ " testing for key existence...\n"));
b0138e99 4859 if (svp) { /* ??? When can this not happen? */
b15feb55 4860 if (hv_exists_ent(MUTABLE_HV(SvRV(d)), *svp, 0))
b0138e99
RGS
4861 RETPUSHYES;
4862 }
4863 }
4864 RETPUSHNO;
4865 }
4866 if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4867 AV *other_av = MUTABLE_AV(SvRV(d));
d7c0d282 4868 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Array\n"));
b0138e99 4869 if (av_len(MUTABLE_AV(SvRV(e))) != av_len(other_av))
0d863452
RH
4870 RETPUSHNO;
4871 else {
4872 I32 i;
c445ea15 4873 const I32 other_len = av_len(other_av);
0d863452 4874
a0714e2c 4875 if (NULL == seen_this) {
0d863452 4876 seen_this = newHV();
ad64d0ec 4877 (void) sv_2mortal(MUTABLE_SV(seen_this));
0d863452 4878 }
a0714e2c 4879 if (NULL == seen_other) {
6bc991bf 4880 seen_other = newHV();
ad64d0ec 4881 (void) sv_2mortal(MUTABLE_SV(seen_other));
0d863452
RH
4882 }
4883 for(i = 0; i <= other_len; ++i) {
b0138e99 4884 SV * const * const this_elem = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
c445ea15
AL
4885 SV * const * const other_elem = av_fetch(other_av, i, FALSE);
4886
0d863452 4887 if (!this_elem || !other_elem) {
69c3dccf
RGS
4888 if ((this_elem && SvOK(*this_elem))
4889 || (other_elem && SvOK(*other_elem)))
0d863452
RH
4890 RETPUSHNO;
4891 }
365c4e3d
RGS
4892 else if (hv_exists_ent(seen_this,
4893 sv_2mortal(newSViv(PTR2IV(*this_elem))), 0) ||
4894 hv_exists_ent(seen_other,
4895 sv_2mortal(newSViv(PTR2IV(*other_elem))), 0))
0d863452
RH
4896 {
4897 if (*this_elem != *other_elem)
4898 RETPUSHNO;
4899 }
4900 else {
04fe65b0
RGS
4901 (void)hv_store_ent(seen_this,
4902 sv_2mortal(newSViv(PTR2IV(*this_elem))),
4903 &PL_sv_undef, 0);
4904 (void)hv_store_ent(seen_other,
4905 sv_2mortal(newSViv(PTR2IV(*other_elem))),
4906 &PL_sv_undef, 0);
0d863452 4907 PUSHs(*other_elem);
a566f585 4908 PUSHs(*this_elem);
0d863452
RH
4909
4910 PUTBACK;
d7c0d282 4911 DEBUG_M(Perl_deb(aTHX_ " recursively comparing array element...\n"));
be88a5c3 4912 (void) do_smartmatch(seen_this, seen_other, 0);
0d863452 4913 SPAGAIN;
d7c0d282 4914 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
0d863452
RH
4915
4916 if (!SvTRUEx(POPs))
4917 RETPUSHNO;
4918 }
4919 }
4920 RETPUSHYES;
4921 }
4922 }
a566f585 4923 else if (SvROK(d) && SvTYPE(SvRV(d)) == SVt_REGEXP) {
d7c0d282 4924 DEBUG_M(Perl_deb(aTHX_ " applying rule Regex-Array\n"));
ea0c2dbd
RGS
4925 sm_regex_array:
4926 {
4927 PMOP * const matcher = make_matcher((REGEXP*) SvRV(d));
4928 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
4929 I32 i;
0d863452 4930
ea0c2dbd
RGS
4931 for(i = 0; i <= this_len; ++i) {
4932 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
d7c0d282 4933 DEBUG_M(Perl_deb(aTHX_ " testing element against pattern...\n"));
ea0c2dbd
RGS
4934 if (svp && matcher_matches_sv(matcher, *svp)) {
4935 destroy_matcher(matcher);
4936 RETPUSHYES;
4937 }
0d863452 4938 }
ea0c2dbd
RGS
4939 destroy_matcher(matcher);
4940 RETPUSHNO;
0d863452 4941 }
0d863452 4942 }
015eb7b9
RGS
4943 else if (!SvOK(d)) {
4944 /* undef ~~ array */
4945 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
0d863452
RH
4946 I32 i;
4947
d7c0d282 4948 DEBUG_M(Perl_deb(aTHX_ " applying rule Undef-Array\n"));
015eb7b9 4949 for (i = 0; i <= this_len; ++i) {
b0138e99 4950 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
d7c0d282 4951 DEBUG_M(Perl_deb(aTHX_ " testing for undef element...\n"));
015eb7b9 4952 if (!svp || !SvOK(*svp))
0d863452
RH
4953 RETPUSHYES;
4954 }
4955 RETPUSHNO;
4956 }
015eb7b9 4957 else {
41e726ac
RGS
4958 sm_any_array:
4959 {
4960 I32 i;
4961 const I32 this_len = av_len(MUTABLE_AV(SvRV(e)));
0d863452 4962
d7c0d282 4963 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Array\n"));
41e726ac
RGS
4964 for (i = 0; i <= this_len; ++i) {
4965 SV * const * const svp = av_fetch(MUTABLE_AV(SvRV(e)), i, FALSE);
4966 if (!svp)
4967 continue;
015eb7b9 4968
41e726ac
RGS
4969 PUSHs(d);
4970 PUSHs(*svp);
4971 PUTBACK;
4972 /* infinite recursion isn't supposed to happen here */
d7c0d282 4973 DEBUG_M(Perl_deb(aTHX_ " recursively testing array element...\n"));
be88a5c3 4974 (void) do_smartmatch(NULL, NULL, 1);
41e726ac 4975 SPAGAIN;
d7c0d282 4976 DEBUG_M(Perl_deb(aTHX_ " recursion finished\n"));
41e726ac
RGS
4977 if (SvTRUEx(POPs))
4978 RETPUSHYES;
4979 }
4980 RETPUSHNO;
0d863452 4981 }
0d863452
RH
4982 }
4983 }
b0138e99 4984 /* ~~ qr// */
a566f585 4985 else if (SvROK(e) && SvTYPE(SvRV(e)) == SVt_REGEXP) {
ea0c2dbd
RGS
4986 if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVHV) {
4987 SV *t = d; d = e; e = t;
d7c0d282 4988 DEBUG_M(Perl_deb(aTHX_ " applying rule Hash-Regex\n"));
ea0c2dbd
RGS
4989 goto sm_regex_hash;
4990 }
4991 else if (!object_on_left && SvROK(d) && SvTYPE(SvRV(d)) == SVt_PVAV) {
4992 SV *t = d; d = e; e = t;
d7c0d282 4993 DEBUG_M(Perl_deb(aTHX_ " applying rule Array-Regex\n"));
ea0c2dbd
RGS
4994 goto sm_regex_array;
4995 }
4996 else {
4997 PMOP * const matcher = make_matcher((REGEXP*) SvRV(e));
0d863452 4998
d7c0d282 4999 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Regex\n"));
ea0c2dbd
RGS
5000 PUTBACK;
5001 PUSHs(matcher_matches_sv(matcher, d)
5002 ? &PL_sv_yes
5003 : &PL_sv_no);
5004 destroy_matcher(matcher);
5005 RETURN;
5006 }
0d863452 5007 }
b0138e99 5008 /* ~~ scalar */
2c9d2554
RGS
5009 /* See if there is overload magic on left */
5010 else if (object_on_left && SvAMAGIC(d)) {
5011 SV *tmpsv;
d7c0d282
DM
5012 DEBUG_M(Perl_deb(aTHX_ " applying rule Object-Any\n"));
5013 DEBUG_M(Perl_deb(aTHX_ " attempting overload\n"));
2c9d2554
RGS
5014 PUSHs(d); PUSHs(e);
5015 PUTBACK;
5016 tmpsv = amagic_call(d, e, smart_amg, AMGf_noright);
5017 if (tmpsv) {
5018 SPAGAIN;
5019 (void)POPs;
5020 SETs(tmpsv);
5021 RETURN;
5022 }
5023 SP -= 2;
d7c0d282 5024 DEBUG_M(Perl_deb(aTHX_ " failed to run overload method; falling back...\n"));
2c9d2554
RGS
5025 goto sm_any_scalar;
5026 }
fb51372e
RGS
5027 else if (!SvOK(d)) {
5028 /* undef ~~ scalar ; we already know that the scalar is SvOK */
d7c0d282 5029 DEBUG_M(Perl_deb(aTHX_ " applying rule undef-Any\n"));
fb51372e
RGS
5030 RETPUSHNO;
5031 }
2c9d2554
RGS
5032 else
5033 sm_any_scalar:
5034 if (SvNIOK(e) || (SvPOK(e) && looks_like_number(e) && SvNIOK(d))) {
d7c0d282
DM
5035 DEBUG_M(if (SvNIOK(e))
5036 Perl_deb(aTHX_ " applying rule Any-Num\n");
5037 else
5038 Perl_deb(aTHX_ " applying rule Num-numish\n");
5039 );
33ed63a2 5040 /* numeric comparison */
0d863452
RH
5041 PUSHs(d); PUSHs(e);
5042 PUTBACK;
a98fe34d 5043 if (CopHINTS_get(PL_curcop) & HINT_INTEGER)
897d3989 5044 (void) Perl_pp_i_eq(aTHX);
0d863452 5045 else
897d3989 5046 (void) Perl_pp_eq(aTHX);
0d863452
RH
5047 SPAGAIN;
5048 if (SvTRUEx(POPs))
5049 RETPUSHYES;
5050 else
5051 RETPUSHNO;
5052 }
5053
5054 /* As a last resort, use string comparison */
d7c0d282 5055 DEBUG_M(Perl_deb(aTHX_ " applying rule Any-Any\n"));
0d863452
RH
5056 PUSHs(d); PUSHs(e);
5057 PUTBACK;
897d3989 5058 return Perl_pp_seq(aTHX);
0d863452
RH
5059}
5060
5061PP(pp_enterwhen)
5062{
5063 dVAR; dSP;
5064 register PERL_CONTEXT *cx;
5065 const I32 gimme = GIMME_V;
5066
5067 /* This is essentially an optimization: if the match
5068 fails, we don't want to push a context and then
5069 pop it again right away, so we skip straight
5070 to the op that follows the leavewhen.
25b991bf 5071 RETURNOP calls PUTBACK which restores the stack pointer after the POPs.
0d863452
RH
5072 */
5073 if ((0 == (PL_op->op_flags & OPf_SPECIAL)) && !SvTRUEx(POPs))
25b991bf 5074 RETURNOP(cLOGOP->op_other->op_next);
0d863452 5075
c08f093b 5076 ENTER_with_name("when");
0d863452
RH
5077 SAVETMPS;
5078
5079 PUSHBLOCK(cx, CXt_WHEN, SP);
5080 PUSHWHEN(cx);
5081
5082 RETURN;
5083}
5084
5085PP(pp_leavewhen)
5086{
5087 dVAR; dSP;
c08f093b 5088 I32 cxix;
0d863452 5089 register PERL_CONTEXT *cx;
c08f093b 5090 I32 gimme;
0d863452
RH
5091 SV **newsp;
5092 PMOP *newpm;
5093
c08f093b
VP
5094 cxix = dopoptogiven(cxstack_ix);
5095 if (cxix < 0)
fc7debfb
FC
5096 /* diag_listed_as: Can't "when" outside a topicalizer */
5097 DIE(aTHX_ "Can't \"%s\" outside a topicalizer",
5098 PL_op->op_flags & OPf_SPECIAL ? "default" : "when");
c08f093b 5099
0d863452
RH
5100 POPBLOCK(cx,newpm);
5101 assert(CxTYPE(cx) == CXt_WHEN);
5102
c08f093b
VP
5103 TAINT_NOT;
5104 SP = adjust_stack_on_leave(newsp, SP, newsp, gimme, SVs_PADTMP|SVs_TEMP);
0d863452
RH
5105 PL_curpm = newpm; /* pop $1 et al */
5106
c08f093b
VP
5107 LEAVE_with_name("when");
5108
5109 if (cxix < cxstack_ix)
5110 dounwind(cxix);
5111
5112 cx = &cxstack[cxix];
5113
5114 if (CxFOREACH(cx)) {
5115 /* clear off anything above the scope we're re-entering */
5116 I32 inner = PL_scopestack_ix;
5117
5118 TOPBLOCK(cx);
5119 if (PL_scopestack_ix < inner)
5120 leave_scope(PL_scopestack[PL_scopestack_ix]);
5121 PL_curcop = cx->blk_oldcop;
5122
5123 return cx->blk_loop.my_op->op_nextop;
5124 }
5125 else
b1b5a4ae 5126 RETURNOP(cx->blk_givwhen.leave_op);
0d863452
RH
5127}
5128
5129PP(pp_continue)
5130{
c08f093b 5131 dVAR; dSP;
0d863452
RH
5132 I32 cxix;
5133 register PERL_CONTEXT *cx;
c08f093b
VP
5134 I32 gimme;
5135 SV **newsp;
5136 PMOP *newpm;
7be5bd17
FR
5137
5138 PERL_UNUSED_VAR(gimme);
0d863452
RH
5139
5140 cxix = dopoptowhen(cxstack_ix);
5141 if (cxix < 0)
5142 DIE(aTHX_ "Can't \"continue\" outside a when block");
c08f093b 5143
0d863452
RH
5144 if (cxix < cxstack_ix)
5145 dounwind(cxix);
5146
c08f093b
VP
5147 POPBLOCK(cx,newpm);
5148 assert(CxTYPE(cx) == CXt_WHEN);
5149
5150 SP = newsp;
5151 PL_curpm = newpm; /* pop $1 et al */
5152
5153 LEAVE_with_name("when");
5154 RETURNOP(cx->blk_givwhen.leave_op->op_next);
0d863452
RH
5155}
5156
5157PP(pp_break)
5158{
5159 dVAR;
5160 I32 cxix;
5161 register PERL_CONTEXT *cx;
25b991bf 5162
0d863452 5163 cxix = dopoptogiven(cxstack_ix);
c08f093b
VP
5164 if (cxix < 0)
5165 DIE(aTHX_ "Can't \"break\" outside a given block");
5166
5167 cx = &cxstack[cxix];
5168 if (CxFOREACH(cx))
0d863452
RH
5169 DIE(aTHX_ "Can't \"break\" in a loop topicalizer");
5170
5171 if (cxix < cxstack_ix)
5172 dounwind(cxix);
0d863452 5173
0787ea8a
VP
5174 /* Restore the sp at the time we entered the given block */
5175 TOPBLOCK(cx);
5176
c08f093b 5177 return cx->blk_givwhen.leave_op;
0d863452
RH
5178}
5179
74e0ddf7 5180static MAGIC *
cea2e8a9 5181S_doparseform(pTHX_ SV *sv)
a0d0e21e
LW
5182{
5183 STRLEN len;
37ffbfcc 5184 register char *s = SvPV(sv, len);
3808a683 5185 register char *send;
086b26f3
DM
5186 register char *base = NULL; /* start of current field */
5187 register I32 skipspaces = 0; /* number of contiguous spaces seen */
5188 bool noblank = FALSE; /* ~ or ~~ seen on this line */
5189 bool repeat = FALSE; /* ~~ seen on this line */
5190 bool postspace = FALSE; /* a text field may need right padding */
dea28490
JJ
5191 U32 *fops;
5192 register U32 *fpc;
086b26f3 5193 U32 *linepc = NULL; /* position of last FF_LINEMARK */
a0d0e21e 5194 register I32 arg;
086b26f3
DM
5195 bool ischop; /* it's a ^ rather than a @ */
5196 bool unchopnum = FALSE; /* at least one @ (i.e. non-chop) num field seen */
a1b95068 5197 int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */
3808a683
DM
5198 MAGIC *mg = NULL;
5199 SV *sv_copy;
a0d0e21e 5200
7918f24d
NC
5201 PERL_ARGS_ASSERT_DOPARSEFORM;
5202
55497cff 5203 if (len == 0)
cea2e8a9 5204 Perl_croak(aTHX_ "Null picture in formline");
ac27b0f5 5205
3808a683
DM
5206 if (SvTYPE(sv) >= SVt_PVMG) {
5207 /* This might, of course, still return NULL. */
5208 mg = mg_find(sv, PERL_MAGIC_fm);
5209 } else {
5210 sv_upgrade(sv, SVt_PVMG);
5211 }
5212
5213 if (mg) {
5214 /* still the same as previously-compiled string? */
5215 SV *old = mg->mg_obj;
5216 if ( !(!!SvUTF8(old) ^ !!SvUTF8(sv))
5217 && len == SvCUR(old)
5218 && strnEQ(SvPVX(old), SvPVX(sv), len)
b57b1734
DM
5219 ) {
5220 DEBUG_f(PerlIO_printf(Perl_debug_log,"Re-using compiled format\n"));
3808a683 5221 return mg;
b57b1734 5222 }
3808a683 5223
b57b1734 5224 DEBUG_f(PerlIO_printf(Perl_debug_log, "Re-compiling format\n"));
3808a683
DM
5225 Safefree(mg->mg_ptr);
5226 mg->mg_ptr = NULL;
5227 SvREFCNT_dec(old);
5228 mg->mg_obj = NULL;
5229 }
b57b1734
DM
5230 else {
5231 DEBUG_f(PerlIO_printf(Perl_debug_log, "Compiling format\n"));
3808a683 5232 mg = sv_magicext(sv, NULL, PERL_MAGIC_fm, &PL_vtbl_fm, NULL, 0);
b57b1734 5233 }
3808a683
DM
5234
5235 sv_copy = newSVpvn_utf8(s, len, SvUTF8(sv));
5236 s = SvPV(sv_copy, len); /* work on the copy, not the original */
5237 send = s + len;
5238
5239
815f25c6
DM
5240 /* estimate the buffer size needed */
5241 for (base = s; s <= send; s++) {
a1b95068 5242 if (*s == '\n' || *s == '@' || *s == '^')
815f25c6
DM
5243 maxops += 10;
5244 }
5245 s = base;
c445ea15 5246 base = NULL;
815f25c6 5247
a02a5408 5248 Newx(fops, maxops, U32);
a0d0e21e
LW
5249 fpc = fops;
5250
5251 if (s < send) {
5252 linepc = fpc;
5253 *fpc++ = FF_LINEMARK;
5254 noblank = repeat = FALSE;
5255 base = s;
5256 }
5257
5258 while (s <= send) {
5259 switch (*s++) {
5260 default:
5261 skipspaces = 0;
5262 continue;
5263
5264 case '~':
5265 if (*s == '~') {
5266 repeat = TRUE;
b57b1734
DM
5267 skipspaces++;
5268 s++;
a0d0e21e
LW
5269 }
5270 noblank = TRUE;
a0d0e21e
LW
5271 /* FALL THROUGH */
5272 case ' ': case '\t':
5273 skipspaces++;
5274 continue;
a1b95068
WL
5275 case 0:
5276 if (s < send) {
5277 skipspaces = 0;
5278 continue;
5279 } /* else FALL THROUGH */
5280 case '\n':
a0d0e21e
LW
5281 arg = s - base;
5282 skipspaces++;
5283 arg -= skipspaces;
5284 if (arg) {
5f05dabc 5285 if (postspace)
a0d0e21e 5286 *fpc++ = FF_SPACE;
a0d0e21e 5287 *fpc++ = FF_LITERAL;
76912796 5288 *fpc++ = (U32)arg;
a0d0e21e 5289 }
5f05dabc 5290 postspace = FALSE;
a0d0e21e
LW
5291 if (s <= send)
5292 skipspaces--;
5293 if (skipspaces) {
5294 *fpc++ = FF_SKIP;
76912796 5295 *fpc++ = (U32)skipspaces;
a0d0e21e
LW
5296 }
5297 skipspaces = 0;
5298 if (s <= send)
5299 *fpc++ = FF_NEWLINE;
5300 if (noblank) {
5301 *fpc++ = FF_BLANK;
5302 if (repeat)
5303 arg = fpc - linepc + 1;
5304 else
5305 arg = 0;
76912796 5306 *fpc++ = (U32)arg;
a0d0e21e
LW
5307 }
5308 if (s < send) {
5309 linepc = fpc;
5310 *fpc++ = FF_LINEMARK;
5311 noblank = repeat = FALSE;
5312 base = s;
5313 }
5314 else
5315 s++;
5316 continue;
5317
5318 case '@':
5319 case '^':
5320 ischop = s[-1] == '^';
5321
5322 if (postspace) {
5323 *fpc++ = FF_SPACE;
5324 postspace = FALSE;
5325 }
5326 arg = (s - base) - 1;
5327 if (arg) {
5328 *fpc++ = FF_LITERAL;
76912796 5329 *fpc++ = (U32)arg;
a0d0e21e
LW
5330 }
5331
5332 base = s - 1;
5333 *fpc++ = FF_FETCH;
086b26f3 5334 if (*s == '*') { /* @* or ^* */
a0d0e21e 5335 s++;
a1b95068
WL
5336 *fpc++ = 2; /* skip the @* or ^* */
5337 if (ischop) {
5338 *fpc++ = FF_LINESNGL;
5339 *fpc++ = FF_CHOP;
5340 } else
5341 *fpc++ = FF_LINEGLOB;
a0d0e21e 5342 }
086b26f3 5343 else if (*s == '#' || (*s == '.' && s[1] == '#')) { /* @###, ^### */
a701009a 5344 arg = ischop ? FORM_NUM_BLANK : 0;
a0d0e21e
LW
5345 base = s - 1;
5346 while (*s == '#')
5347 s++;
5348 if (*s == '.') {
06b5626a 5349 const char * const f = ++s;
a0d0e21e
LW
5350 while (*s == '#')
5351 s++;
a701009a 5352 arg |= FORM_NUM_POINT + (s - f);
a0d0e21e
LW
5353 }
5354 *fpc++ = s - base; /* fieldsize for FETCH */
5355 *fpc++ = FF_DECIMAL;
76912796 5356 *fpc++ = (U32)arg;
a1b95068 5357 unchopnum |= ! ischop;
784707d5
JP
5358 }
5359 else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */
a701009a 5360 arg = ischop ? FORM_NUM_BLANK : 0;
784707d5
JP
5361 base = s - 1;
5362 s++; /* skip the '0' first */
5363 while (*s == '#')
5364 s++;
5365 if (*s == '.') {
06b5626a 5366 const char * const f = ++s;
784707d5
JP
5367 while (*s == '#')
5368 s++;
a701009a 5369 arg |= FORM_NUM_POINT + (s - f);
784707d5
JP
5370 }
5371 *fpc++ = s - base; /* fieldsize for FETCH */
5372 *fpc++ = FF_0DECIMAL;
76912796 5373 *fpc++ = (U32)arg;
a1b95068 5374 unchopnum |= ! ischop;
a0d0e21e 5375 }
086b26f3 5376 else { /* text field */
a0d0e21e
LW
5377 I32 prespace = 0;
5378 bool ismore = FALSE;
5379
5380 if (*s == '>') {
5381 while (*++s == '>') ;
5382 prespace = FF_SPACE;
5383 }
5384 else if (*s == '|') {
5385 while (*++s == '|') ;
5386 prespace = FF_HALFSPACE;
5387 postspace = TRUE;
5388 }
5389 else {
5390 if (*s == '<')
5391 while (*++s == '<') ;
5392 postspace = TRUE;
5393 }
5394 if (*s == '.' && s[1] == '.' && s[2] == '.') {
5395 s += 3;
5396 ismore = TRUE;
5397 }
5398 *fpc++ = s - base; /* fieldsize for FETCH */
5399
5400 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
5401
5402 if (prespace)
76912796 5403 *fpc++ = (U32)prespace; /* add SPACE or HALFSPACE */
a0d0e21e
LW
5404 *fpc++ = FF_ITEM;
5405 if (ismore)
5406 *fpc++ = FF_MORE;
5407 if (ischop)
5408 *fpc++ = FF_CHOP;
5409 }
5410 base = s;
5411 skipspaces = 0;
5412 continue;
5413 }
5414 }
5415 *fpc++ = FF_END;
5416
815f25c6 5417 assert (fpc <= fops + maxops); /* ensure our buffer estimate was valid */
a0d0e21e 5418 arg = fpc - fops;
74e0ddf7 5419
3808a683 5420 mg->mg_ptr = (char *) fops;
74e0ddf7 5421 mg->mg_len = arg * sizeof(U32);
3808a683
DM
5422 mg->mg_obj = sv_copy;
5423 mg->mg_flags |= MGf_REFCOUNTED;
a1b95068 5424
bfed75c6 5425 if (unchopnum && repeat)
75f63940 5426 Perl_die(aTHX_ "Repeated format line will never terminate (~~ and @#)");
74e0ddf7
NC
5427
5428 return mg;
a1b95068
WL
5429}
5430
5431
5432STATIC bool
5433S_num_overflow(NV value, I32 fldsize, I32 frcsize)
5434{
5435 /* Can value be printed in fldsize chars, using %*.*f ? */
5436 NV pwr = 1;
5437 NV eps = 0.5;
5438 bool res = FALSE;
5439 int intsize = fldsize - (value < 0 ? 1 : 0);
5440
a701009a 5441 if (frcsize & FORM_NUM_POINT)
a1b95068 5442 intsize--;
a701009a 5443 frcsize &= ~(FORM_NUM_POINT|FORM_NUM_BLANK);
a1b95068
WL
5444 intsize -= frcsize;
5445
5446 while (intsize--) pwr *= 10.0;
5447 while (frcsize--) eps /= 10.0;
5448
5449 if( value >= 0 ){
5450 if (value + eps >= pwr)
5451 res = TRUE;
5452 } else {
5453 if (value - eps <= -pwr)
5454 res = TRUE;
5455 }
5456 return res;
a0d0e21e 5457}
4e35701f 5458
bbed91b5 5459static I32
0bd48802 5460S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
bbed91b5 5461{
27da23d5 5462 dVAR;
0bd48802 5463 SV * const datasv = FILTER_DATA(idx);
504618e9 5464 const int filter_has_file = IoLINES(datasv);
ad64d0ec
NC
5465 SV * const filter_state = MUTABLE_SV(IoTOP_GV(datasv));
5466 SV * const filter_sub = MUTABLE_SV(IoBOTTOM_GV(datasv));
941a98a0 5467 int status = 0;
ec0b63d7 5468 SV *upstream;
941a98a0 5469 STRLEN got_len;
162177c1
Z
5470 char *got_p = NULL;
5471 char *prune_from = NULL;
34113e50 5472 bool read_from_cache = FALSE;
bb7a0f54
MHM
5473 STRLEN umaxlen;
5474
7918f24d
NC
5475 PERL_ARGS_ASSERT_RUN_USER_FILTER;
5476
bb7a0f54
MHM
5477 assert(maxlen >= 0);
5478 umaxlen = maxlen;
5675696b 5479
bbed91b5
KF
5480 /* I was having segfault trouble under Linux 2.2.5 after a
5481 parse error occured. (Had to hack around it with a test
13765c85 5482 for PL_parser->error_count == 0.) Solaris doesn't segfault --
bbed91b5
KF
5483 not sure where the trouble is yet. XXX */
5484
4464f08e
NC
5485 {
5486 SV *const cache = datasv;
937b367d
NC
5487 if (SvOK(cache)) {
5488 STRLEN cache_len;
5489 const char *cache_p = SvPV(cache, cache_len);
941a98a0
NC
5490 STRLEN take = 0;
5491
bb7a0f54 5492 if (umaxlen) {
941a98a0
NC
5493 /* Running in block mode and we have some cached data already.
5494 */
bb7a0f54 5495 if (cache_len >= umaxlen) {
941a98a0
NC
5496 /* In fact, so much data we don't even need to call
5497 filter_read. */
bb7a0f54 5498 take = umaxlen;
941a98a0
NC
5499 }
5500 } else {
10edeb5d
JH
5501 const char *const first_nl =
5502 (const char *)memchr(cache_p, '\n', cache_len);
941a98a0
NC
5503 if (first_nl) {
5504 take = first_nl + 1 - cache_p;
5505 }
5506 }
5507 if (take) {
5508 sv_catpvn(buf_sv, cache_p, take);
5509 sv_chop(cache, cache_p + take);
486ec47a 5510 /* Definitely not EOF */
937b367d
NC
5511 return 1;
5512 }
941a98a0 5513
937b367d 5514 sv_catsv(buf_sv, cache);
bb7a0f54
MHM
5515 if (umaxlen) {
5516 umaxlen -= cache_len;
941a98a0 5517 }
937b367d 5518 SvOK_off(cache);
34113e50 5519 read_from_cache = TRUE;
937b367d
NC
5520 }
5521 }
ec0b63d7 5522
34113e50
NC
5523 /* Filter API says that the filter appends to the contents of the buffer.
5524 Usually the buffer is "", so the details don't matter. But if it's not,
5525 then clearly what it contains is already filtered by this filter, so we
5526 don't want to pass it in a second time.
5527 I'm going to use a mortal in case the upstream filter croaks. */
ec0b63d7
NC
5528 upstream = ((SvOK(buf_sv) && sv_len(buf_sv)) || SvGMAGICAL(buf_sv))
5529 ? sv_newmortal() : buf_sv;
5530 SvUPGRADE(upstream, SVt_PV);
937b367d 5531
bbed91b5 5532 if (filter_has_file) {
67e70b33 5533 status = FILTER_READ(idx+1, upstream, 0);
bbed91b5
KF
5534 }
5535
34113e50 5536 if (filter_sub && status >= 0) {
39644a26 5537 dSP;
bbed91b5
KF
5538 int count;
5539
d343c3ef 5540 ENTER_with_name("call_filter_sub");
55b5114f 5541 SAVE_DEFSV;
bbed91b5
KF
5542 SAVETMPS;
5543 EXTEND(SP, 2);
5544
414bf5ae 5545 DEFSV_set(upstream);
bbed91b5 5546 PUSHMARK(SP);
6e449a3a 5547 mPUSHi(0);
bbed91b5
KF
5548 if (filter_state) {
5549 PUSHs(filter_state);
5550 }
5551 PUTBACK;
5552 count = call_sv(filter_sub, G_SCALAR);
5553 SPAGAIN;
5554
5555 if (count > 0) {
5556 SV *out = POPs;
5557 if (SvOK(out)) {
941a98a0 5558 status = SvIV(out);
bbed91b5
KF
5559 }
5560 }
5561
5562 PUTBACK;
5563 FREETMPS;
d343c3ef 5564 LEAVE_with_name("call_filter_sub");
bbed91b5
KF
5565 }
5566
941a98a0
NC
5567 if(SvOK(upstream)) {
5568 got_p = SvPV(upstream, got_len);
bb7a0f54
MHM
5569 if (umaxlen) {
5570 if (got_len > umaxlen) {
5571 prune_from = got_p + umaxlen;
937b367d 5572 }
941a98a0 5573 } else {
162177c1 5574 char *const first_nl = (char *)memchr(got_p, '\n', got_len);
941a98a0
NC
5575 if (first_nl && first_nl + 1 < got_p + got_len) {
5576 /* There's a second line here... */
5577 prune_from = first_nl + 1;
937b367d 5578 }
937b367d
NC
5579 }
5580 }
941a98a0
NC
5581 if (prune_from) {
5582 /* Oh. Too long. Stuff some in our cache. */
5583 STRLEN cached_len = got_p + got_len - prune_from;
4464f08e 5584 SV *const cache = datasv;
941a98a0 5585
4464f08e 5586 if (SvOK(cache)) {
941a98a0
NC
5587 /* Cache should be empty. */
5588 assert(!SvCUR(cache));
5589 }
5590
5591 sv_setpvn(cache, prune_from, cached_len);
5592 /* If you ask for block mode, you may well split UTF-8 characters.
5593 "If it breaks, you get to keep both parts"
5594 (Your code is broken if you don't put them back together again
5595 before something notices.) */
5596 if (SvUTF8(upstream)) {
5597 SvUTF8_on(cache);
5598 }
5599 SvCUR_set(upstream, got_len - cached_len);
162177c1 5600 *prune_from = 0;
941a98a0
NC
5601 /* Can't yet be EOF */
5602 if (status == 0)
5603 status = 1;
5604 }
937b367d 5605
34113e50
NC
5606 /* If they are at EOF but buf_sv has something in it, then they may never
5607 have touched the SV upstream, so it may be undefined. If we naively
5608 concatenate it then we get a warning about use of uninitialised value.
5609 */
5610 if (upstream != buf_sv && (SvOK(upstream) || SvGMAGICAL(upstream))) {
937b367d
NC
5611 sv_catsv(buf_sv, upstream);
5612 }
5613
941a98a0 5614 if (status <= 0) {
bbed91b5 5615 IoLINES(datasv) = 0;
bbed91b5
KF
5616 if (filter_state) {
5617 SvREFCNT_dec(filter_state);
a0714e2c 5618 IoTOP_GV(datasv) = NULL;
bbed91b5
KF
5619 }
5620 if (filter_sub) {
5621 SvREFCNT_dec(filter_sub);
a0714e2c 5622 IoBOTTOM_GV(datasv) = NULL;
bbed91b5 5623 }
0bd48802 5624 filter_del(S_run_user_filter);
bbed91b5 5625 }
34113e50
NC
5626 if (status == 0 && read_from_cache) {
5627 /* If we read some data from the cache (and by getting here it implies
5628 that we emptied the cache) then we aren't yet at EOF, and mustn't
5629 report that to our caller. */
5630 return 1;
5631 }
941a98a0 5632 return status;
bbed91b5 5633}
84d4ea48 5634
be4b629d
CN
5635/* perhaps someone can come up with a better name for
5636 this? it is not really "absolute", per se ... */
cf42f822 5637static bool
5f66b61c 5638S_path_is_absolute(const char *name)
be4b629d 5639{
7918f24d
NC
5640 PERL_ARGS_ASSERT_PATH_IS_ABSOLUTE;
5641
be4b629d 5642 if (PERL_FILE_IS_ABSOLUTE(name)
3f66cd94 5643#ifdef WIN32
36f064bc
CL
5644 || (*name == '.' && ((name[1] == '/' ||
5645 (name[1] == '.' && name[2] == '/'))
5646 || (name[1] == '\\' ||
5647 ( name[1] == '.' && name[2] == '\\')))
5648 )
5649#else
be4b629d 5650 || (*name == '.' && (name[1] == '/' ||
0bd48802 5651 (name[1] == '.' && name[2] == '/')))
36f064bc 5652#endif
0bd48802 5653 )
be4b629d
CN
5654 {
5655 return TRUE;
5656 }
5657 else
5658 return FALSE;
5659}
241d1a3b
NC
5660
5661/*
5662 * Local variables:
5663 * c-indentation-style: bsd
5664 * c-basic-offset: 4
14d04a33 5665 * indent-tabs-mode: nil
241d1a3b
NC
5666 * End:
5667 *
14d04a33 5668 * ex: set ts=8 sts=4 sw=4 et:
37442d52 5669 */