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