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