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