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