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