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