This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Just s/Perl_/S_/ isn't good enough - you also need to add the C<static>
[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
131b3ad0
DM
96 if (PL_op->op_flags & OPf_STACKED) {
97 /* multiple args; concatentate them */
98 dMARK; dORIGMARK;
99 tmpstr = PAD_SV(ARGTARG);
76f68e9b 100 sv_setpvs(tmpstr, "");
131b3ad0
DM
101 while (++MARK <= SP) {
102 if (PL_amagic_generation) {
103 SV *sv;
104 if ((SvAMAGIC(tmpstr) || SvAMAGIC(*MARK)) &&
105 (sv = amagic_call(tmpstr, *MARK, concat_amg, AMGf_assign)))
106 {
107 sv_setsv(tmpstr, sv);
108 continue;
109 }
110 }
111 sv_catsv(tmpstr, *MARK);
112 }
113 SvSETMAGIC(tmpstr);
114 SP = ORIGMARK;
115 }
116 else
117 tmpstr = POPs;
513629ba 118
b3eb6a9b 119 if (SvROK(tmpstr)) {
d8f6592e 120 SV * const sv = SvRV(tmpstr);
5c35adbb 121 if (SvTYPE(sv) == SVt_REGEXP)
d2f13c59 122 re = (REGEXP*) sv;
c277df42 123 }
5c35adbb
NC
124 if (re) {
125 re = reg_temp_copy(re);
aaa362c4 126 ReREFCNT_dec(PM_GETRE(pm));
28d8d7f4 127 PM_SETRE(pm, re);
c277df42
IZ
128 }
129 else {
e62f0680 130 STRLEN len;
3ab4a224 131 const char *t = SvOK(tmpstr) ? SvPV_const(tmpstr, len) : "";
c737faaf 132 re = PM_GETRE(pm);
14a49a24 133 assert (re != (REGEXP*) &PL_sv_undef);
c277df42 134
20408e3c 135 /* Check against the last compiled regexp. */
a11c8683 136 if (!re || !RX_PRECOMP(re) || RX_PRELEN(re) != len ||
220fc49f 137 memNE(RX_PRECOMP(re), t, len))
85aff577 138 {
07bc277f 139 const regexp_engine *eng = re ? RX_ENGINE(re) : NULL;
c737faaf 140 U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME;
d8f6592e
AL
141 if (re) {
142 ReREFCNT_dec(re);
14a49a24
NC
143#ifdef USE_ITHREADS
144 PM_SETRE(pm, (REGEXP*) &PL_sv_undef);
145#else
4608196e 146 PM_SETRE(pm, NULL); /* crucial if regcomp aborts */
14a49a24 147#endif
1e2e3d02
YO
148 } else if (PL_curcop->cop_hints_hash) {
149 SV *ptr = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash, 0,
150 "regcomp", 7, 0, 0);
151 if (ptr && SvIOK(ptr) && SvIV(ptr))
152 eng = INT2PTR(regexp_engine*,SvIV(ptr));
c277df42 153 }
664e119d 154
533c011a 155 if (PL_op->op_flags & OPf_SPECIAL)
3280af22 156 PL_reginterp_cnt = I32_MAX; /* Mark as safe. */
a0d0e21e 157
b9ad30b4
NC
158 if (DO_UTF8(tmpstr)) {
159 assert (SvUTF8(tmpstr));
160 } else if (SvUTF8(tmpstr)) {
161 /* Not doing UTF-8, despite what the SV says. Is this only if
162 we're trapped in use 'bytes'? */
163 /* Make a copy of the octet sequence, but without the flag on,
164 as the compiler now honours the SvUTF8 flag on tmpstr. */
165 STRLEN len;
166 const char *const p = SvPV(tmpstr, len);
167 tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
168 }
c737faaf 169
3ab4a224
AB
170 if (eng)
171 PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
172 else
173 PM_SETRE(pm, CALLREGCOMP(tmpstr, pm_flags));
c737faaf 174
f86aaa29 175 PL_reginterp_cnt = 0; /* XXXX Be extra paranoid - needed
2cd61cdb 176 inside tie/overload accessors. */
c277df42 177 }
4633a7c4 178 }
c737faaf
YO
179
180 re = PM_GETRE(pm);
a0d0e21e 181
72311751 182#ifndef INCOMPLETE_TAINTS
3280af22
NIS
183 if (PL_tainting) {
184 if (PL_tainted)
07bc277f 185 RX_EXTFLAGS(re) |= RXf_TAINTED;
72311751 186 else
07bc277f 187 RX_EXTFLAGS(re) &= ~RXf_TAINTED;
72311751
GS
188 }
189#endif
190
220fc49f 191 if (!RX_PRELEN(PM_GETRE(pm)) && PL_curpm)
3280af22 192 pm = PL_curpm;
a0d0e21e 193
c737faaf
YO
194
195#if !defined(USE_ITHREADS)
196 /* can't change the optree at runtime either */
197 /* PMf_KEEP is handled differently under threads to avoid these problems */
a0d0e21e 198 if (pm->op_pmflags & PMf_KEEP) {
c90c0ff4 199 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
533c011a 200 cLOGOP->op_first->op_next = PL_op->op_next;
a0d0e21e 201 }
c737faaf 202#endif
a0d0e21e
LW
203 RETURN;
204}
205
206PP(pp_substcont)
207{
97aff369 208 dVAR;
39644a26 209 dSP;
c09156bb 210 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
901017d6
AL
211 register PMOP * const pm = (PMOP*) cLOGOP->op_other;
212 register SV * const dstr = cx->sb_dstr;
a0d0e21e
LW
213 register char *s = cx->sb_s;
214 register char *m = cx->sb_m;
215 char *orig = cx->sb_orig;
901017d6 216 register REGEXP * const rx = cx->sb_rx;
c445ea15 217 SV *nsv = NULL;
988e6e7e
AE
218 REGEXP *old = PM_GETRE(pm);
219 if(old != rx) {
bfed75c6 220 if(old)
988e6e7e 221 ReREFCNT_dec(old);
d6106309 222 PM_SETRE(pm,ReREFCNT_inc(rx));
d8f2cf8a
AB
223 }
224
d9f97599 225 rxres_restore(&cx->sb_rxres, rx);
01b35787 226 RX_MATCH_UTF8_set(rx, DO_UTF8(cx->sb_targ));
c90c0ff4 227
a0d0e21e 228 if (cx->sb_iters++) {
a3b680e6 229 const I32 saviters = cx->sb_iters;
a0d0e21e 230 if (cx->sb_iters > cx->sb_maxiters)
cea2e8a9 231 DIE(aTHX_ "Substitution loop");
a0d0e21e 232
48c036b1
GS
233 if (!(cx->sb_rxtainted & 2) && SvTAINTED(TOPs))
234 cx->sb_rxtainted |= 2;
a0d0e21e 235 sv_catsv(dstr, POPs);
a0d0e21e
LW
236
237 /* Are we done */
c5bed6a7 238 if (CxONCE(cx) || !CALLREGEXEC(rx, s, cx->sb_strend, orig,
9661b544 239 s == m, cx->sb_targ, NULL,
22e551b9 240 ((cx->sb_rflags & REXEC_COPY_STR)
cf93c79d
IZ
241 ? (REXEC_IGNOREPOS|REXEC_NOT_FIRST)
242 : (REXEC_COPY_STR|REXEC_IGNOREPOS|REXEC_NOT_FIRST))))
a0d0e21e 243 {
823a54a3 244 SV * const targ = cx->sb_targ;
748a9306 245
078c425b
JH
246 assert(cx->sb_strend >= s);
247 if(cx->sb_strend > s) {
248 if (DO_UTF8(dstr) && !SvUTF8(targ))
249 sv_catpvn_utf8_upgrade(dstr, s, cx->sb_strend - s, nsv);
250 else
251 sv_catpvn(dstr, s, cx->sb_strend - s);
252 }
48c036b1 253 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
9212bbba 254
f8c7b90f 255#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
256 if (SvIsCOW(targ)) {
257 sv_force_normal_flags(targ, SV_COW_DROP_PV);
258 } else
259#endif
260 {
8bd4d4c5 261 SvPV_free(targ);
ed252734 262 }
f880fe2f 263 SvPV_set(targ, SvPVX(dstr));
748a9306
LW
264 SvCUR_set(targ, SvCUR(dstr));
265 SvLEN_set(targ, SvLEN(dstr));
1aa99e6b
IH
266 if (DO_UTF8(dstr))
267 SvUTF8_on(targ);
6136c704 268 SvPV_set(dstr, NULL);
48c036b1
GS
269
270 TAINT_IF(cx->sb_rxtainted & 1);
6e449a3a 271 mPUSHi(saviters - 1);
48c036b1 272
ffc61ed2 273 (void)SvPOK_only_UTF8(targ);
48c036b1 274 TAINT_IF(cx->sb_rxtainted);
a0d0e21e 275 SvSETMAGIC(targ);
9212bbba 276 SvTAINT(targ);
5cd24f17 277
4633a7c4 278 LEAVE_SCOPE(cx->sb_oldsave);
a0d0e21e
LW
279 POPSUBST(cx);
280 RETURNOP(pm->op_next);
281 }
8e5e9ebe 282 cx->sb_iters = saviters;
a0d0e21e 283 }
07bc277f 284 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
a0d0e21e
LW
285 m = s;
286 s = orig;
07bc277f 287 cx->sb_orig = orig = RX_SUBBEG(rx);
a0d0e21e
LW
288 s = orig + (m - s);
289 cx->sb_strend = s + (cx->sb_strend - m);
290 }
07bc277f 291 cx->sb_m = m = RX_OFFS(rx)[0].start + orig;
db79b45b 292 if (m > s) {
bfed75c6 293 if (DO_UTF8(dstr) && !SvUTF8(cx->sb_targ))
db79b45b
JH
294 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
295 else
296 sv_catpvn(dstr, s, m-s);
297 }
07bc277f 298 cx->sb_s = RX_OFFS(rx)[0].end + orig;
084916e3 299 { /* Update the pos() information. */
44f8325f 300 SV * const sv = cx->sb_targ;
084916e3 301 MAGIC *mg;
7a7f3e45 302 SvUPGRADE(sv, SVt_PVMG);
14befaf4 303 if (!(mg = mg_find(sv, PERL_MAGIC_regex_global))) {
d83f0a82 304#ifdef PERL_OLD_COPY_ON_WRITE
51a9ea20 305 if (SvIsCOW(sv))
d83f0a82
NC
306 sv_force_normal_flags(sv, 0);
307#endif
308 mg = sv_magicext(sv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob,
309 NULL, 0);
084916e3 310 }
ce474962 311 mg->mg_len = m - orig;
084916e3 312 }
988e6e7e 313 if (old != rx)
d6106309 314 (void)ReREFCNT_inc(rx);
d9f97599
GS
315 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
316 rxres_save(&cx->sb_rxres, rx);
29f2e912 317 RETURNOP(pm->op_pmstashstartu.op_pmreplstart);
a0d0e21e
LW
318}
319
c90c0ff4 320void
864dbfa3 321Perl_rxres_save(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4 322{
323 UV *p = (UV*)*rsp;
324 U32 i;
7918f24d
NC
325
326 PERL_ARGS_ASSERT_RXRES_SAVE;
96a5add6 327 PERL_UNUSED_CONTEXT;
c90c0ff4 328
07bc277f 329 if (!p || p[1] < RX_NPARENS(rx)) {
f8c7b90f 330#ifdef PERL_OLD_COPY_ON_WRITE
07bc277f 331 i = 7 + RX_NPARENS(rx) * 2;
ed252734 332#else
07bc277f 333 i = 6 + RX_NPARENS(rx) * 2;
ed252734 334#endif
c90c0ff4 335 if (!p)
a02a5408 336 Newx(p, i, UV);
c90c0ff4 337 else
338 Renew(p, i, UV);
339 *rsp = (void*)p;
340 }
341
07bc277f 342 *p++ = PTR2UV(RX_MATCH_COPIED(rx) ? RX_SUBBEG(rx) : NULL);
cf93c79d 343 RX_MATCH_COPIED_off(rx);
c90c0ff4 344
f8c7b90f 345#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1
NC
346 *p++ = PTR2UV(RX_SAVED_COPY(rx));
347 RX_SAVED_COPY(rx) = NULL;
ed252734
NC
348#endif
349
07bc277f 350 *p++ = RX_NPARENS(rx);
c90c0ff4 351
07bc277f
NC
352 *p++ = PTR2UV(RX_SUBBEG(rx));
353 *p++ = (UV)RX_SUBLEN(rx);
354 for (i = 0; i <= RX_NPARENS(rx); ++i) {
355 *p++ = (UV)RX_OFFS(rx)[i].start;
356 *p++ = (UV)RX_OFFS(rx)[i].end;
c90c0ff4 357 }
358}
359
360void
864dbfa3 361Perl_rxres_restore(pTHX_ void **rsp, REGEXP *rx)
c90c0ff4 362{
363 UV *p = (UV*)*rsp;
364 U32 i;
7918f24d
NC
365
366 PERL_ARGS_ASSERT_RXRES_RESTORE;
96a5add6 367 PERL_UNUSED_CONTEXT;
c90c0ff4 368
ed252734 369 RX_MATCH_COPY_FREE(rx);
cf93c79d 370 RX_MATCH_COPIED_set(rx, *p);
c90c0ff4 371 *p++ = 0;
372
f8c7b90f 373#ifdef PERL_OLD_COPY_ON_WRITE
bdd9a1b1
NC
374 if (RX_SAVED_COPY(rx))
375 SvREFCNT_dec (RX_SAVED_COPY(rx));
376 RX_SAVED_COPY(rx) = INT2PTR(SV*,*p);
ed252734
NC
377 *p++ = 0;
378#endif
379
07bc277f 380 RX_NPARENS(rx) = *p++;
c90c0ff4 381
07bc277f
NC
382 RX_SUBBEG(rx) = INT2PTR(char*,*p++);
383 RX_SUBLEN(rx) = (I32)(*p++);
384 for (i = 0; i <= RX_NPARENS(rx); ++i) {
385 RX_OFFS(rx)[i].start = (I32)(*p++);
386 RX_OFFS(rx)[i].end = (I32)(*p++);
c90c0ff4 387 }
388}
389
390void
864dbfa3 391Perl_rxres_free(pTHX_ void **rsp)
c90c0ff4 392{
44f8325f 393 UV * const p = (UV*)*rsp;
7918f24d
NC
394
395 PERL_ARGS_ASSERT_RXRES_FREE;
96a5add6 396 PERL_UNUSED_CONTEXT;
c90c0ff4 397
398 if (p) {
94010e71
NC
399#ifdef PERL_POISON
400 void *tmp = INT2PTR(char*,*p);
401 Safefree(tmp);
402 if (*p)
7e337ee0 403 PoisonFree(*p, 1, sizeof(*p));
94010e71 404#else
56431972 405 Safefree(INT2PTR(char*,*p));
94010e71 406#endif
f8c7b90f 407#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
408 if (p[1]) {
409 SvREFCNT_dec (INT2PTR(SV*,p[1]));
410 }
411#endif
c90c0ff4 412 Safefree(p);
4608196e 413 *rsp = NULL;
c90c0ff4 414 }
415}
416
a0d0e21e
LW
417PP(pp_formline)
418{
97aff369 419 dVAR; dSP; dMARK; dORIGMARK;
823a54a3 420 register SV * const tmpForm = *++MARK;
dea28490 421 register U32 *fpc;
a0d0e21e 422 register char *t;
245d4a47 423 const char *f;
a0d0e21e 424 register I32 arg;
c445ea15
AL
425 register SV *sv = NULL;
426 const char *item = NULL;
9c5ffd7c
JH
427 I32 itemsize = 0;
428 I32 fieldsize = 0;
a0d0e21e 429 I32 lines = 0;
c445ea15
AL
430 bool chopspace = (strchr(PL_chopset, ' ') != NULL);
431 const char *chophere = NULL;
432 char *linemark = NULL;
65202027 433 NV value;
9c5ffd7c 434 bool gotsome = FALSE;
a0d0e21e 435 STRLEN len;
823a54a3 436 const STRLEN fudge = SvPOK(tmpForm)
24c89738 437 ? (SvCUR(tmpForm) * (IN_BYTES ? 1 : 3) + 1) : 0;
1bd51a4c
IH
438 bool item_is_utf8 = FALSE;
439 bool targ_is_utf8 = FALSE;
c445ea15 440 SV * nsv = NULL;
cbbf8932 441 OP * parseres = NULL;
bfed75c6 442 const char *fmt;
a0d0e21e 443
76e3520e 444 if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) {
445b3f51
GS
445 if (SvREADONLY(tmpForm)) {
446 SvREADONLY_off(tmpForm);
a1b95068 447 parseres = doparseform(tmpForm);
445b3f51
GS
448 SvREADONLY_on(tmpForm);
449 }
450 else
a1b95068
WL
451 parseres = doparseform(tmpForm);
452 if (parseres)
453 return parseres;
a0d0e21e 454 }
3280af22 455 SvPV_force(PL_formtarget, len);
1bd51a4c
IH
456 if (DO_UTF8(PL_formtarget))
457 targ_is_utf8 = TRUE;
a0ed51b3 458 t = SvGROW(PL_formtarget, len + fudge + 1); /* XXX SvCUR bad */
a0d0e21e 459 t += len;
245d4a47 460 f = SvPV_const(tmpForm, len);
a0d0e21e 461 /* need to jump to the next word */
245d4a47 462 fpc = (U32*)(f + len + WORD_ALIGN - SvCUR(tmpForm) % WORD_ALIGN);
a0d0e21e
LW
463
464 for (;;) {
465 DEBUG_f( {
bfed75c6 466 const char *name = "???";
a0d0e21e
LW
467 arg = -1;
468 switch (*fpc) {
469 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
470 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
471 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
472 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
473 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
474
475 case FF_CHECKNL: name = "CHECKNL"; break;
476 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
477 case FF_SPACE: name = "SPACE"; break;
478 case FF_HALFSPACE: name = "HALFSPACE"; break;
479 case FF_ITEM: name = "ITEM"; break;
480 case FF_CHOP: name = "CHOP"; break;
481 case FF_LINEGLOB: name = "LINEGLOB"; break;
482 case FF_NEWLINE: name = "NEWLINE"; break;
483 case FF_MORE: name = "MORE"; break;
484 case FF_LINEMARK: name = "LINEMARK"; break;
485 case FF_END: name = "END"; break;
bfed75c6 486 case FF_0DECIMAL: name = "0DECIMAL"; break;
a1b95068 487 case FF_LINESNGL: name = "LINESNGL"; break;
a0d0e21e
LW
488 }
489 if (arg >= 0)
bf49b057 490 PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg);
a0d0e21e 491 else
bf49b057 492 PerlIO_printf(Perl_debug_log, "%-16s\n", name);
5f80b19c 493 } );
a0d0e21e
LW
494 switch (*fpc++) {
495 case FF_LINEMARK:
496 linemark = t;
a0d0e21e
LW
497 lines++;
498 gotsome = FALSE;
499 break;
500
501 case FF_LITERAL:
502 arg = *fpc++;
1bd51a4c 503 if (targ_is_utf8 && !SvUTF8(tmpForm)) {
b15aece3 504 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
78da4d13
JH
505 *t = '\0';
506 sv_catpvn_utf8_upgrade(PL_formtarget, f, arg, nsv);
507 t = SvEND(PL_formtarget);
f3f2f1a3 508 f += arg;
1bd51a4c
IH
509 break;
510 }
511 if (!targ_is_utf8 && DO_UTF8(tmpForm)) {
b15aece3 512 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1bd51a4c
IH
513 *t = '\0';
514 sv_utf8_upgrade(PL_formtarget);
515 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
516 t = SvEND(PL_formtarget);
517 targ_is_utf8 = TRUE;
518 }
a0d0e21e
LW
519 while (arg--)
520 *t++ = *f++;
521 break;
522
523 case FF_SKIP:
524 f += *fpc++;
525 break;
526
527 case FF_FETCH:
528 arg = *fpc++;
529 f += arg;
530 fieldsize = arg;
531
532 if (MARK < SP)
533 sv = *++MARK;
534 else {
3280af22 535 sv = &PL_sv_no;
599cee73 536 if (ckWARN(WARN_SYNTAX))
9014280d 537 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Not enough format arguments");
a0d0e21e
LW
538 }
539 break;
540
541 case FF_CHECKNL:
5a34cab7
NC
542 {
543 const char *send;
544 const char *s = item = SvPV_const(sv, len);
545 itemsize = len;
546 if (DO_UTF8(sv)) {
547 itemsize = sv_len_utf8(sv);
548 if (itemsize != (I32)len) {
549 I32 itembytes;
550 if (itemsize > fieldsize) {
551 itemsize = fieldsize;
552 itembytes = itemsize;
553 sv_pos_u2b(sv, &itembytes, 0);
554 }
555 else
556 itembytes = len;
557 send = chophere = s + itembytes;
558 while (s < send) {
559 if (*s & ~31)
560 gotsome = TRUE;
561 else if (*s == '\n')
562 break;
563 s++;
564 }
565 item_is_utf8 = TRUE;
566 itemsize = s - item;
567 sv_pos_b2u(sv, &itemsize);
568 break;
a0ed51b3 569 }
a0ed51b3 570 }
5a34cab7
NC
571 item_is_utf8 = FALSE;
572 if (itemsize > fieldsize)
573 itemsize = fieldsize;
574 send = chophere = s + itemsize;
575 while (s < send) {
576 if (*s & ~31)
577 gotsome = TRUE;
578 else if (*s == '\n')
579 break;
580 s++;
581 }
582 itemsize = s - item;
583 break;
a0ed51b3 584 }
a0d0e21e
LW
585
586 case FF_CHECKCHOP:
5a34cab7
NC
587 {
588 const char *s = item = SvPV_const(sv, len);
589 itemsize = len;
590 if (DO_UTF8(sv)) {
591 itemsize = sv_len_utf8(sv);
592 if (itemsize != (I32)len) {
593 I32 itembytes;
594 if (itemsize <= fieldsize) {
595 const char *send = chophere = s + itemsize;
596 while (s < send) {
597 if (*s == '\r') {
598 itemsize = s - item;
a0ed51b3 599 chophere = s;
a0ed51b3 600 break;
5a34cab7
NC
601 }
602 if (*s++ & ~31)
a0ed51b3 603 gotsome = TRUE;
a0ed51b3 604 }
a0ed51b3 605 }
5a34cab7
NC
606 else {
607 const char *send;
608 itemsize = fieldsize;
609 itembytes = itemsize;
610 sv_pos_u2b(sv, &itembytes, 0);
611 send = chophere = s + itembytes;
612 while (s < send || (s == send && isSPACE(*s))) {
613 if (isSPACE(*s)) {
614 if (chopspace)
615 chophere = s;
616 if (*s == '\r')
617 break;
618 }
619 else {
620 if (*s & ~31)
621 gotsome = TRUE;
622 if (strchr(PL_chopset, *s))
623 chophere = s + 1;
624 }
625 s++;
626 }
627 itemsize = chophere - item;
628 sv_pos_b2u(sv, &itemsize);
629 }
630 item_is_utf8 = TRUE;
a0d0e21e
LW
631 break;
632 }
a0d0e21e 633 }
5a34cab7
NC
634 item_is_utf8 = FALSE;
635 if (itemsize <= fieldsize) {
636 const char *const send = chophere = s + itemsize;
637 while (s < send) {
638 if (*s == '\r') {
639 itemsize = s - item;
a0d0e21e 640 chophere = s;
a0d0e21e 641 break;
5a34cab7
NC
642 }
643 if (*s++ & ~31)
a0d0e21e 644 gotsome = TRUE;
a0d0e21e 645 }
a0d0e21e 646 }
5a34cab7
NC
647 else {
648 const char *send;
649 itemsize = fieldsize;
650 send = chophere = s + itemsize;
651 while (s < send || (s == send && isSPACE(*s))) {
652 if (isSPACE(*s)) {
653 if (chopspace)
654 chophere = s;
655 if (*s == '\r')
656 break;
657 }
658 else {
659 if (*s & ~31)
660 gotsome = TRUE;
661 if (strchr(PL_chopset, *s))
662 chophere = s + 1;
663 }
664 s++;
665 }
666 itemsize = chophere - item;
667 }
668 break;
a0d0e21e 669 }
a0d0e21e
LW
670
671 case FF_SPACE:
672 arg = fieldsize - itemsize;
673 if (arg) {
674 fieldsize -= arg;
675 while (arg-- > 0)
676 *t++ = ' ';
677 }
678 break;
679
680 case FF_HALFSPACE:
681 arg = fieldsize - itemsize;
682 if (arg) {
683 arg /= 2;
684 fieldsize -= arg;
685 while (arg-- > 0)
686 *t++ = ' ';
687 }
688 break;
689
690 case FF_ITEM:
5a34cab7
NC
691 {
692 const char *s = item;
693 arg = itemsize;
694 if (item_is_utf8) {
695 if (!targ_is_utf8) {
696 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
697 *t = '\0';
698 sv_utf8_upgrade(PL_formtarget);
699 SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1);
700 t = SvEND(PL_formtarget);
701 targ_is_utf8 = TRUE;
a0ed51b3 702 }
5a34cab7
NC
703 while (arg--) {
704 if (UTF8_IS_CONTINUED(*s)) {
705 STRLEN skip = UTF8SKIP(s);
706 switch (skip) {
707 default:
708 Move(s,t,skip,char);
709 s += skip;
710 t += skip;
711 break;
712 case 7: *t++ = *s++;
713 case 6: *t++ = *s++;
714 case 5: *t++ = *s++;
715 case 4: *t++ = *s++;
716 case 3: *t++ = *s++;
717 case 2: *t++ = *s++;
718 case 1: *t++ = *s++;
719 }
720 }
721 else {
722 if ( !((*t++ = *s++) & ~31) )
723 t[-1] = ' ';
724 }
a0ed51b3 725 }
5a34cab7 726 break;
a0ed51b3 727 }
5a34cab7
NC
728 if (targ_is_utf8 && !item_is_utf8) {
729 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
730 *t = '\0';
731 sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv);
732 for (; t < SvEND(PL_formtarget); t++) {
78da4d13 733#ifdef EBCDIC
901017d6 734 const int ch = *t;
5a34cab7 735 if (iscntrl(ch))
78da4d13 736#else
5a34cab7 737 if (!(*t & ~31))
78da4d13 738#endif
5a34cab7
NC
739 *t = ' ';
740 }
741 break;
78da4d13 742 }
5a34cab7 743 while (arg--) {
9d116dd7 744#ifdef EBCDIC
901017d6 745 const int ch = *t++ = *s++;
5a34cab7 746 if (iscntrl(ch))
a0d0e21e 747#else
5a34cab7 748 if ( !((*t++ = *s++) & ~31) )
a0d0e21e 749#endif
5a34cab7
NC
750 t[-1] = ' ';
751 }
752 break;
a0d0e21e 753 }
a0d0e21e
LW
754
755 case FF_CHOP:
5a34cab7
NC
756 {
757 const char *s = chophere;
758 if (chopspace) {
af68e756 759 while (isSPACE(*s))
5a34cab7
NC
760 s++;
761 }
762 sv_chop(sv,s);
763 SvSETMAGIC(sv);
764 break;
a0d0e21e 765 }
a0d0e21e 766
a1b95068
WL
767 case FF_LINESNGL:
768 chopspace = 0;
a0d0e21e 769 case FF_LINEGLOB:
5a34cab7 770 {
e32383e2 771 const bool oneline = fpc[-1] == FF_LINESNGL;
5a34cab7 772 const char *s = item = SvPV_const(sv, len);
f3f2f1a3 773 item_is_utf8 = DO_UTF8(sv);
5a34cab7 774 itemsize = len;
5a34cab7 775 if (itemsize) {
e8e72d41 776 STRLEN to_copy = itemsize;
5a34cab7 777 const char *const send = s + len;
35c6393c 778 const U8 *source = (const U8 *) s;
e8e72d41
NC
779 U8 *tmp = NULL;
780
5a34cab7
NC
781 gotsome = TRUE;
782 chophere = s + itemsize;
783 while (s < send) {
784 if (*s++ == '\n') {
785 if (oneline) {
e8e72d41 786 to_copy = s - SvPVX_const(sv) - 1;
5a34cab7
NC
787 chophere = s;
788 break;
789 } else {
790 if (s == send) {
791 itemsize--;
e8e72d41 792 to_copy--;
5a34cab7
NC
793 } else
794 lines++;
795 }
1bd51a4c 796 }
a0d0e21e 797 }
e8e72d41 798 if (targ_is_utf8 && !item_is_utf8) {
35c6393c 799 source = tmp = bytes_to_utf8(source, &to_copy);
e8e72d41
NC
800 SvCUR_set(PL_formtarget,
801 t - SvPVX_const(PL_formtarget));
802 } else {
803 if (item_is_utf8 && !targ_is_utf8) {
804 /* Upgrade targ to UTF8, and then we reduce it to
805 a problem we have a simple solution for. */
806 SvCUR_set(PL_formtarget,
807 t - SvPVX_const(PL_formtarget));
808 targ_is_utf8 = TRUE;
809 /* Don't need get magic. */
810 sv_utf8_upgrade_flags(PL_formtarget, 0);
811 } else {
812 SvCUR_set(PL_formtarget,
813 t - SvPVX_const(PL_formtarget));
814 }
e8e72d41
NC
815
816 /* Easy. They agree. */
817 assert (item_is_utf8 == targ_is_utf8);
818 }
819 SvGROW(PL_formtarget,
820 SvCUR(PL_formtarget) + to_copy + fudge + 1);
5a34cab7 821 t = SvPVX(PL_formtarget) + SvCUR(PL_formtarget);
e8e72d41
NC
822
823 Copy(source, t, to_copy, char);
824 t += to_copy;
825 SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) + to_copy);
f3f2f1a3 826 if (item_is_utf8) {
e8e72d41
NC
827 if (SvGMAGICAL(sv)) {
828 /* Mustn't call sv_pos_b2u() as it does a second
829 mg_get(). Is this a bug? Do we need a _flags()
830 variant? */
831 itemsize = utf8_length(source, source + itemsize);
832 } else {
833 sv_pos_b2u(sv, &itemsize);
834 }
835 assert(!tmp);
836 } else if (tmp) {
837 Safefree(tmp);
f3f2f1a3 838 }
a0d0e21e 839 }
5a34cab7 840 break;
a0d0e21e 841 }
a0d0e21e 842
a1b95068
WL
843 case FF_0DECIMAL:
844 arg = *fpc++;
845#if defined(USE_LONG_DOUBLE)
10edeb5d
JH
846 fmt = (const char *)
847 ((arg & 256) ?
848 "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl);
a1b95068 849#else
10edeb5d
JH
850 fmt = (const char *)
851 ((arg & 256) ?
852 "%#0*.*f" : "%0*.*f");
a1b95068
WL
853#endif
854 goto ff_dec;
a0d0e21e 855 case FF_DECIMAL:
a0d0e21e 856 arg = *fpc++;
65202027 857#if defined(USE_LONG_DOUBLE)
10edeb5d
JH
858 fmt = (const char *)
859 ((arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl);
65202027 860#else
10edeb5d
JH
861 fmt = (const char *)
862 ((arg & 256) ? "%#*.*f" : "%*.*f");
65202027 863#endif
a1b95068 864 ff_dec:
784707d5
JP
865 /* If the field is marked with ^ and the value is undefined,
866 blank it out. */
784707d5
JP
867 if ((arg & 512) && !SvOK(sv)) {
868 arg = fieldsize;
869 while (arg--)
870 *t++ = ' ';
871 break;
872 }
873 gotsome = TRUE;
874 value = SvNV(sv);
a1b95068 875 /* overflow evidence */
bfed75c6 876 if (num_overflow(value, fieldsize, arg)) {
a1b95068
WL
877 arg = fieldsize;
878 while (arg--)
879 *t++ = '#';
880 break;
881 }
784707d5
JP
882 /* Formats aren't yet marked for locales, so assume "yes". */
883 {
884 STORE_NUMERIC_STANDARD_SET_LOCAL();
d9fad198 885 my_snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), fmt, (int) fieldsize, (int) arg & 255, value);
784707d5
JP
886 RESTORE_NUMERIC_STANDARD();
887 }
888 t += fieldsize;
889 break;
a1b95068 890
a0d0e21e
LW
891 case FF_NEWLINE:
892 f++;
893 while (t-- > linemark && *t == ' ') ;
894 t++;
895 *t++ = '\n';
896 break;
897
898 case FF_BLANK:
899 arg = *fpc++;
900 if (gotsome) {
901 if (arg) { /* repeat until fields exhausted? */
902 *t = '\0';
b15aece3 903 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
3280af22 904 lines += FmLINES(PL_formtarget);
a0d0e21e
LW
905 if (lines == 200) {
906 arg = t - linemark;
907 if (strnEQ(linemark, linemark - arg, arg))
cea2e8a9 908 DIE(aTHX_ "Runaway format");
a0d0e21e 909 }
1bd51a4c
IH
910 if (targ_is_utf8)
911 SvUTF8_on(PL_formtarget);
3280af22 912 FmLINES(PL_formtarget) = lines;
a0d0e21e
LW
913 SP = ORIGMARK;
914 RETURNOP(cLISTOP->op_first);
915 }
916 }
917 else {
918 t = linemark;
919 lines--;
920 }
921 break;
922
923 case FF_MORE:
5a34cab7
NC
924 {
925 const char *s = chophere;
926 const char *send = item + len;
927 if (chopspace) {
af68e756 928 while (isSPACE(*s) && (s < send))
5a34cab7 929 s++;
a0d0e21e 930 }
5a34cab7
NC
931 if (s < send) {
932 char *s1;
933 arg = fieldsize - itemsize;
934 if (arg) {
935 fieldsize -= arg;
936 while (arg-- > 0)
937 *t++ = ' ';
938 }
939 s1 = t - 3;
940 if (strnEQ(s1," ",3)) {
941 while (s1 > SvPVX_const(PL_formtarget) && isSPACE(s1[-1]))
942 s1--;
943 }
944 *s1++ = '.';
945 *s1++ = '.';
946 *s1++ = '.';
a0d0e21e 947 }
5a34cab7 948 break;
a0d0e21e 949 }
a0d0e21e
LW
950 case FF_END:
951 *t = '\0';
b15aece3 952 SvCUR_set(PL_formtarget, t - SvPVX_const(PL_formtarget));
1bd51a4c
IH
953 if (targ_is_utf8)
954 SvUTF8_on(PL_formtarget);
3280af22 955 FmLINES(PL_formtarget) += lines;
a0d0e21e
LW
956 SP = ORIGMARK;
957 RETPUSHYES;
958 }
959 }
960}
961
962PP(pp_grepstart)
963{
27da23d5 964 dVAR; dSP;
a0d0e21e
LW
965 SV *src;
966
3280af22 967 if (PL_stack_base + *PL_markstack_ptr == SP) {
a0d0e21e 968 (void)POPMARK;
54310121 969 if (GIMME_V == G_SCALAR)
6e449a3a 970 mXPUSHi(0);
533c011a 971 RETURNOP(PL_op->op_next->op_next);
a0d0e21e 972 }
3280af22 973 PL_stack_sp = PL_stack_base + *PL_markstack_ptr + 1;
cea2e8a9
GS
974 pp_pushmark(); /* push dst */
975 pp_pushmark(); /* push src */
a0d0e21e
LW
976 ENTER; /* enter outer scope */
977
978 SAVETMPS;
59f00321
RGS
979 if (PL_op->op_private & OPpGREP_LEX)
980 SAVESPTR(PAD_SVl(PL_op->op_targ));
981 else
982 SAVE_DEFSV;
a0d0e21e 983 ENTER; /* enter inner scope */
7766f137 984 SAVEVPTR(PL_curpm);
a0d0e21e 985
3280af22 986 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 987 SvTEMP_off(src);
59f00321
RGS
988 if (PL_op->op_private & OPpGREP_LEX)
989 PAD_SVl(PL_op->op_targ) = src;
990 else
414bf5ae 991 DEFSV_set(src);
a0d0e21e
LW
992
993 PUTBACK;
533c011a 994 if (PL_op->op_type == OP_MAPSTART)
cea2e8a9 995 pp_pushmark(); /* push top */
533c011a 996 return ((LOGOP*)PL_op->op_next)->op_other;
a0d0e21e
LW
997}
998
a0d0e21e
LW
999PP(pp_mapwhile)
1000{
27da23d5 1001 dVAR; dSP;
f54cb97a 1002 const I32 gimme = GIMME_V;
544f3153 1003 I32 items = (SP - PL_stack_base) - *PL_markstack_ptr; /* how many new items */
a0d0e21e
LW
1004 I32 count;
1005 I32 shift;
1006 SV** src;
ac27b0f5 1007 SV** dst;
a0d0e21e 1008
544f3153 1009 /* first, move source pointer to the next item in the source list */
3280af22 1010 ++PL_markstack_ptr[-1];
544f3153
GS
1011
1012 /* if there are new items, push them into the destination list */
4c90a460 1013 if (items && gimme != G_VOID) {
544f3153
GS
1014 /* might need to make room back there first */
1015 if (items > PL_markstack_ptr[-1] - PL_markstack_ptr[-2]) {
1016 /* XXX this implementation is very pessimal because the stack
1017 * is repeatedly extended for every set of items. Is possible
1018 * to do this without any stack extension or copying at all
1019 * by maintaining a separate list over which the map iterates
18ef8bea 1020 * (like foreach does). --gsar */
544f3153
GS
1021
1022 /* everything in the stack after the destination list moves
1023 * towards the end the stack by the amount of room needed */
1024 shift = items - (PL_markstack_ptr[-1] - PL_markstack_ptr[-2]);
1025
1026 /* items to shift up (accounting for the moved source pointer) */
1027 count = (SP - PL_stack_base) - (PL_markstack_ptr[-1] - 1);
18ef8bea
BT
1028
1029 /* This optimization is by Ben Tilly and it does
1030 * things differently from what Sarathy (gsar)
1031 * is describing. The downside of this optimization is
1032 * that leaves "holes" (uninitialized and hopefully unused areas)
1033 * to the Perl stack, but on the other hand this
1034 * shouldn't be a problem. If Sarathy's idea gets
1035 * implemented, this optimization should become
1036 * irrelevant. --jhi */
1037 if (shift < count)
1038 shift = count; /* Avoid shifting too often --Ben Tilly */
bfed75c6 1039
924508f0
GS
1040 EXTEND(SP,shift);
1041 src = SP;
1042 dst = (SP += shift);
3280af22
NIS
1043 PL_markstack_ptr[-1] += shift;
1044 *PL_markstack_ptr += shift;
544f3153 1045 while (count--)
a0d0e21e
LW
1046 *dst-- = *src--;
1047 }
544f3153 1048 /* copy the new items down to the destination list */
ac27b0f5 1049 dst = PL_stack_base + (PL_markstack_ptr[-2] += items) - 1;
22023b26
TP
1050 if (gimme == G_ARRAY) {
1051 while (items-- > 0)
1052 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
1053 }
bfed75c6 1054 else {
22023b26
TP
1055 /* scalar context: we don't care about which values map returns
1056 * (we use undef here). And so we certainly don't want to do mortal
1057 * copies of meaningless values. */
1058 while (items-- > 0) {
b988aa42 1059 (void)POPs;
22023b26
TP
1060 *dst-- = &PL_sv_undef;
1061 }
1062 }
a0d0e21e
LW
1063 }
1064 LEAVE; /* exit inner scope */
1065
1066 /* All done yet? */
3280af22 1067 if (PL_markstack_ptr[-1] > *PL_markstack_ptr) {
a0d0e21e
LW
1068
1069 (void)POPMARK; /* pop top */
1070 LEAVE; /* exit outer scope */
1071 (void)POPMARK; /* pop src */
3280af22 1072 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 1073 (void)POPMARK; /* pop dst */
3280af22 1074 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 1075 if (gimme == G_SCALAR) {
7cc47870
RGS
1076 if (PL_op->op_private & OPpGREP_LEX) {
1077 SV* sv = sv_newmortal();
1078 sv_setiv(sv, items);
1079 PUSHs(sv);
1080 }
1081 else {
1082 dTARGET;
1083 XPUSHi(items);
1084 }
a0d0e21e 1085 }
54310121 1086 else if (gimme == G_ARRAY)
1087 SP += items;
a0d0e21e
LW
1088 RETURN;
1089 }
1090 else {
1091 SV *src;
1092
1093 ENTER; /* enter inner scope */
7766f137 1094 SAVEVPTR(PL_curpm);
a0d0e21e 1095
544f3153 1096 /* set $_ to the new source item */
3280af22 1097 src = PL_stack_base[PL_markstack_ptr[-1]];
a0d0e21e 1098 SvTEMP_off(src);
59f00321
RGS
1099 if (PL_op->op_private & OPpGREP_LEX)
1100 PAD_SVl(PL_op->op_targ) = src;
1101 else
414bf5ae 1102 DEFSV_set(src);
a0d0e21e
LW
1103
1104 RETURNOP(cLOGOP->op_other);
1105 }
1106}
1107
a0d0e21e
LW
1108/* Range stuff. */
1109
1110PP(pp_range)
1111{
97aff369 1112 dVAR;
a0d0e21e 1113 if (GIMME == G_ARRAY)
1a67a97c 1114 return NORMAL;
538573f7 1115 if (SvTRUEx(PAD_SV(PL_op->op_targ)))
1a67a97c 1116 return cLOGOP->op_other;
538573f7 1117 else
1a67a97c 1118 return NORMAL;
a0d0e21e
LW
1119}
1120
1121PP(pp_flip)
1122{
97aff369 1123 dVAR;
39644a26 1124 dSP;
a0d0e21e
LW
1125
1126 if (GIMME == G_ARRAY) {
1a67a97c 1127 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1128 }
1129 else {
1130 dTOPss;
44f8325f 1131 SV * const targ = PAD_SV(PL_op->op_targ);
bfed75c6 1132 int flip = 0;
790090df 1133
bfed75c6 1134 if (PL_op->op_private & OPpFLIP_LINENUM) {
4e3399f9
YST
1135 if (GvIO(PL_last_in_gv)) {
1136 flip = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1137 }
1138 else {
fafc274c 1139 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
44f8325f
AL
1140 if (gv && GvSV(gv))
1141 flip = SvIV(sv) == SvIV(GvSV(gv));
4e3399f9 1142 }
bfed75c6
AL
1143 } else {
1144 flip = SvTRUE(sv);
1145 }
1146 if (flip) {
a0d0e21e 1147 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
533c011a 1148 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1149 sv_setiv(targ, 1);
3e3baf6d 1150 SETs(targ);
a0d0e21e
LW
1151 RETURN;
1152 }
1153 else {
1154 sv_setiv(targ, 0);
924508f0 1155 SP--;
1a67a97c 1156 RETURNOP(((LOGOP*)cUNOP->op_first)->op_other);
a0d0e21e
LW
1157 }
1158 }
76f68e9b 1159 sv_setpvs(TARG, "");
a0d0e21e
LW
1160 SETs(targ);
1161 RETURN;
1162 }
1163}
1164
8e9bbdb9
RGS
1165/* This code tries to decide if "$left .. $right" should use the
1166 magical string increment, or if the range is numeric (we make
1167 an exception for .."0" [#18165]). AMS 20021031. */
1168
1169#define RANGE_IS_NUMERIC(left,right) ( \
b0e74086
RGS
1170 SvNIOKp(left) || (SvOK(left) && !SvPOKp(left)) || \
1171 SvNIOKp(right) || (SvOK(right) && !SvPOKp(right)) || \
e0ab1c0e 1172 (((!SvOK(left) && SvOK(right)) || ((!SvOK(left) || \
b15aece3 1173 looks_like_number(left)) && SvPOKp(left) && *SvPVX_const(left) != '0')) \
e0ab1c0e 1174 && (!SvOK(right) || looks_like_number(right))))
8e9bbdb9 1175
a0d0e21e
LW
1176PP(pp_flop)
1177{
97aff369 1178 dVAR; dSP;
a0d0e21e
LW
1179
1180 if (GIMME == G_ARRAY) {
1181 dPOPPOPssrl;
86cb7173 1182
5b295bef
RD
1183 SvGETMAGIC(left);
1184 SvGETMAGIC(right);
a0d0e21e 1185
8e9bbdb9 1186 if (RANGE_IS_NUMERIC(left,right)) {
901017d6
AL
1187 register IV i, j;
1188 IV max;
4fe3f0fa
MHM
1189 if ((SvOK(left) && SvNV(left) < IV_MIN) ||
1190 (SvOK(right) && SvNV(right) > IV_MAX))
d470f89e 1191 DIE(aTHX_ "Range iterator outside integer range");
a0d0e21e
LW
1192 i = SvIV(left);
1193 max = SvIV(right);
bbce6d69 1194 if (max >= i) {
c1ab3db2
AK
1195 j = max - i + 1;
1196 EXTEND_MORTAL(j);
1197 EXTEND(SP, j);
bbce6d69 1198 }
c1ab3db2
AK
1199 else
1200 j = 0;
1201 while (j--) {
901017d6 1202 SV * const sv = sv_2mortal(newSViv(i++));
a0d0e21e
LW
1203 PUSHs(sv);
1204 }
1205 }
1206 else {
44f8325f 1207 SV * const final = sv_mortalcopy(right);
13c5b33c 1208 STRLEN len;
823a54a3 1209 const char * const tmps = SvPV_const(final, len);
a0d0e21e 1210
901017d6 1211 SV *sv = sv_mortalcopy(left);
13c5b33c 1212 SvPV_force_nolen(sv);
89ea2908 1213 while (!SvNIOKp(sv) && SvCUR(sv) <= len) {
a0d0e21e 1214 XPUSHs(sv);
b15aece3 1215 if (strEQ(SvPVX_const(sv),tmps))
89ea2908 1216 break;
a0d0e21e
LW
1217 sv = sv_2mortal(newSVsv(sv));
1218 sv_inc(sv);
1219 }
a0d0e21e
LW
1220 }
1221 }
1222 else {
1223 dTOPss;
901017d6 1224 SV * const targ = PAD_SV(cUNOP->op_first->op_targ);
4e3399f9 1225 int flop = 0;
a0d0e21e 1226 sv_inc(targ);
4e3399f9
YST
1227
1228 if (PL_op->op_private & OPpFLIP_LINENUM) {
1229 if (GvIO(PL_last_in_gv)) {
1230 flop = SvIV(sv) == (IV)IoLINES(GvIOp(PL_last_in_gv));
1231 }
1232 else {
fafc274c 1233 GV * const gv = gv_fetchpvs(".", GV_ADD|GV_NOTQUAL, SVt_PV);
4e3399f9
YST
1234 if (gv && GvSV(gv)) flop = SvIV(sv) == SvIV(GvSV(gv));
1235 }
1236 }
1237 else {
1238 flop = SvTRUE(sv);
1239 }
1240
1241 if (flop) {
a0d0e21e 1242 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
396482e1 1243 sv_catpvs(targ, "E0");
a0d0e21e
LW
1244 }
1245 SETs(targ);
1246 }
1247
1248 RETURN;
1249}
1250
1251/* Control. */
1252
27da23d5 1253static const char * const context_name[] = {
515afda2 1254 "pseudo-block",
76753e7f
NC
1255 "when",
1256 NULL, /* CXt_BLOCK never actually needs "block" */
1257 "given",
1258 NULL, /* CXt_LOOP_FOR never actually needs "loop" */
1259 NULL, /* CXt_LOOP_PLAIN never actually needs "loop" */
1260 NULL, /* CXt_LOOP_LAZYSV never actually needs "loop" */
1261 NULL, /* CXt_LOOP_LAZYIV never actually needs "loop" */
515afda2 1262 "subroutine",
76753e7f 1263 "format",
515afda2 1264 "eval",
515afda2 1265 "substitution",
515afda2
NC
1266};
1267
76e3520e 1268STATIC I32
06b5626a 1269S_dopoptolabel(pTHX_ const char *label)
a0d0e21e 1270{
97aff369 1271 dVAR;
a0d0e21e 1272 register I32 i;
a0d0e21e 1273
7918f24d
NC
1274 PERL_ARGS_ASSERT_DOPOPTOLABEL;
1275
a0d0e21e 1276 for (i = cxstack_ix; i >= 0; i--) {
901017d6 1277 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1278 switch (CxTYPE(cx)) {
a0d0e21e 1279 case CXt_SUBST:
a0d0e21e 1280 case CXt_SUB:
7766f137 1281 case CXt_FORMAT:
a0d0e21e 1282 case CXt_EVAL:
0a753a76 1283 case CXt_NULL:
0d863452
RH
1284 case CXt_GIVEN:
1285 case CXt_WHEN:
e476b1b5 1286 if (ckWARN(WARN_EXITING))
515afda2
NC
1287 Perl_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1288 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1289 if (CxTYPE(cx) == CXt_NULL)
1290 return -1;
1291 break;
c6fdafd0 1292 case CXt_LOOP_LAZYIV:
d01136d6 1293 case CXt_LOOP_LAZYSV:
3b719c58
NC
1294 case CXt_LOOP_FOR:
1295 case CXt_LOOP_PLAIN:
a61a7064 1296 if ( !CxLABEL(cx) || strNE(label, CxLABEL(cx)) ) {
cea2e8a9 1297 DEBUG_l(Perl_deb(aTHX_ "(Skipping label #%ld %s)\n",
a61a7064 1298 (long)i, CxLABEL(cx)));
a0d0e21e
LW
1299 continue;
1300 }
cea2e8a9 1301 DEBUG_l( Perl_deb(aTHX_ "(Found label #%ld %s)\n", (long)i, label));
a0d0e21e
LW
1302 return i;
1303 }
1304 }
1305 return i;
1306}
1307
0d863452
RH
1308
1309
e50aee73 1310I32
864dbfa3 1311Perl_dowantarray(pTHX)
e50aee73 1312{
97aff369 1313 dVAR;
f54cb97a 1314 const I32 gimme = block_gimme();
54310121 1315 return (gimme == G_VOID) ? G_SCALAR : gimme;
1316}
1317
1318I32
864dbfa3 1319Perl_block_gimme(pTHX)
54310121 1320{
97aff369 1321 dVAR;
06b5626a 1322 const I32 cxix = dopoptosub(cxstack_ix);
e50aee73 1323 if (cxix < 0)
46fc3d4c 1324 return G_VOID;
e50aee73 1325
54310121 1326 switch (cxstack[cxix].blk_gimme) {
d2719217
GS
1327 case G_VOID:
1328 return G_VOID;
54310121 1329 case G_SCALAR:
e50aee73 1330 return G_SCALAR;
54310121 1331 case G_ARRAY:
1332 return G_ARRAY;
1333 default:
cea2e8a9 1334 Perl_croak(aTHX_ "panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
d2719217
GS
1335 /* NOTREACHED */
1336 return 0;
54310121 1337 }
e50aee73
AD
1338}
1339
78f9721b
SM
1340I32
1341Perl_is_lvalue_sub(pTHX)
1342{
97aff369 1343 dVAR;
06b5626a 1344 const I32 cxix = dopoptosub(cxstack_ix);
78f9721b
SM
1345 assert(cxix >= 0); /* We should only be called from inside subs */
1346
bafb2adc
NC
1347 if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
1348 return CxLVAL(cxstack + cxix);
78f9721b
SM
1349 else
1350 return 0;
1351}
1352
76e3520e 1353STATIC I32
901017d6 1354S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
2c375eb9 1355{
97aff369 1356 dVAR;
a0d0e21e 1357 I32 i;
7918f24d
NC
1358
1359 PERL_ARGS_ASSERT_DOPOPTOSUB_AT;
1360
a0d0e21e 1361 for (i = startingblock; i >= 0; i--) {
901017d6 1362 register const PERL_CONTEXT * const cx = &cxstk[i];
6b35e009 1363 switch (CxTYPE(cx)) {
a0d0e21e
LW
1364 default:
1365 continue;
1366 case CXt_EVAL:
1367 case CXt_SUB:
7766f137 1368 case CXt_FORMAT:
cea2e8a9 1369 DEBUG_l( Perl_deb(aTHX_ "(Found sub #%ld)\n", (long)i));
a0d0e21e
LW
1370 return i;
1371 }
1372 }
1373 return i;
1374}
1375
76e3520e 1376STATIC I32
cea2e8a9 1377S_dopoptoeval(pTHX_ I32 startingblock)
a0d0e21e 1378{
97aff369 1379 dVAR;
a0d0e21e 1380 I32 i;
a0d0e21e 1381 for (i = startingblock; i >= 0; i--) {
06b5626a 1382 register const PERL_CONTEXT *cx = &cxstack[i];
6b35e009 1383 switch (CxTYPE(cx)) {
a0d0e21e
LW
1384 default:
1385 continue;
1386 case CXt_EVAL:
cea2e8a9 1387 DEBUG_l( Perl_deb(aTHX_ "(Found eval #%ld)\n", (long)i));
a0d0e21e
LW
1388 return i;
1389 }
1390 }
1391 return i;
1392}
1393
76e3520e 1394STATIC I32
cea2e8a9 1395S_dopoptoloop(pTHX_ I32 startingblock)
a0d0e21e 1396{
97aff369 1397 dVAR;
a0d0e21e 1398 I32 i;
a0d0e21e 1399 for (i = startingblock; i >= 0; i--) {
901017d6 1400 register const PERL_CONTEXT * const cx = &cxstack[i];
6b35e009 1401 switch (CxTYPE(cx)) {
a0d0e21e 1402 case CXt_SUBST:
a0d0e21e 1403 case CXt_SUB:
7766f137 1404 case CXt_FORMAT:
a0d0e21e 1405 case CXt_EVAL:
0a753a76 1406 case CXt_NULL:
e476b1b5 1407 if (ckWARN(WARN_EXITING))
515afda2
NC
1408 Perl_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
1409 context_name[CxTYPE(cx)], OP_NAME(PL_op));
1410 if ((CxTYPE(cx)) == CXt_NULL)
1411 return -1;
1412 break;
c6fdafd0 1413 case CXt_LOOP_LAZYIV:
d01136d6 1414 case CXt_LOOP_LAZYSV:
3b719c58
NC
1415 case CXt_LOOP_FOR:
1416 case CXt_LOOP_PLAIN:
cea2e8a9 1417 DEBUG_l( Perl_deb(aTHX_ "(Found loop #%ld)\n", (long)i));
a0d0e21e
LW
1418 return i;
1419 }
1420 }
1421 return i;
1422}
1423
0d863452
RH
1424STATIC I32
1425S_dopoptogiven(pTHX_ I32 startingblock)
1426{
97aff369 1427 dVAR;
0d863452
RH
1428 I32 i;
1429 for (i = startingblock; i >= 0; i--) {
1430 register const PERL_CONTEXT *cx = &cxstack[i];
1431 switch (CxTYPE(cx)) {
1432 default:
1433 continue;
1434 case CXt_GIVEN:
1435 DEBUG_l( Perl_deb(aTHX_ "(Found given #%ld)\n", (long)i));
1436 return i;
3b719c58
NC
1437 case CXt_LOOP_PLAIN:
1438 assert(!CxFOREACHDEF(cx));
1439 break;
c6fdafd0 1440 case CXt_LOOP_LAZYIV:
d01136d6 1441 case CXt_LOOP_LAZYSV:
3b719c58 1442 case CXt_LOOP_FOR:
0d863452
RH
1443 if (CxFOREACHDEF(cx)) {
1444 DEBUG_l( Perl_deb(aTHX_ "(Found foreach #%ld)\n", (long)i));
1445 return i;
1446 }
1447 }
1448 }
1449 return i;
1450}
1451
1452STATIC I32
1453S_dopoptowhen(pTHX_ I32 startingblock)
1454{
97aff369 1455 dVAR;
0d863452
RH
1456 I32 i;
1457 for (i = startingblock; i >= 0; i--) {
1458 register const PERL_CONTEXT *cx = &cxstack[i];
1459 switch (CxTYPE(cx)) {
1460 default:
1461 continue;
1462 case CXt_WHEN:
1463 DEBUG_l( Perl_deb(aTHX_ "(Found when #%ld)\n", (long)i));
1464 return i;
1465 }
1466 }
1467 return i;
1468}
1469
a0d0e21e 1470void
864dbfa3 1471Perl_dounwind(pTHX_ I32 cxix)
a0d0e21e 1472{
97aff369 1473 dVAR;
a0d0e21e
LW
1474 I32 optype;
1475
1476 while (cxstack_ix > cxix) {
b0d9ce38 1477 SV *sv;
06b5626a 1478 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
c90c0ff4 1479 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
22c35a8c 1480 (long) cxstack_ix, PL_block_type[CxTYPE(cx)]));
a0d0e21e 1481 /* Note: we don't need to restore the base context info till the end. */
6b35e009 1482 switch (CxTYPE(cx)) {
c90c0ff4 1483 case CXt_SUBST:
1484 POPSUBST(cx);
1485 continue; /* not break */
a0d0e21e 1486 case CXt_SUB:
b0d9ce38
GS
1487 POPSUB(cx,sv);
1488 LEAVESUB(sv);
a0d0e21e
LW
1489 break;
1490 case CXt_EVAL:
1491 POPEVAL(cx);
1492 break;
c6fdafd0 1493 case CXt_LOOP_LAZYIV:
d01136d6 1494 case CXt_LOOP_LAZYSV:
3b719c58
NC
1495 case CXt_LOOP_FOR:
1496 case CXt_LOOP_PLAIN:
a0d0e21e
LW
1497 POPLOOP(cx);
1498 break;
0a753a76 1499 case CXt_NULL:
a0d0e21e 1500 break;
7766f137
GS
1501 case CXt_FORMAT:
1502 POPFORMAT(cx);
1503 break;
a0d0e21e 1504 }
c90c0ff4 1505 cxstack_ix--;
a0d0e21e 1506 }
1b6737cc 1507 PERL_UNUSED_VAR(optype);
a0d0e21e
LW
1508}
1509
5a844595
GS
1510void
1511Perl_qerror(pTHX_ SV *err)
1512{
97aff369 1513 dVAR;
7918f24d
NC
1514
1515 PERL_ARGS_ASSERT_QERROR;
1516
5a844595
GS
1517 if (PL_in_eval)
1518 sv_catsv(ERRSV, err);
1519 else if (PL_errors)
1520 sv_catsv(PL_errors, err);
1521 else
be2597df 1522 Perl_warn(aTHX_ "%"SVf, SVfARG(err));
13765c85
DM
1523 if (PL_parser)
1524 ++PL_parser->error_count;
5a844595
GS
1525}
1526
a0d0e21e 1527OP *
35a4481c 1528Perl_die_where(pTHX_ const char *message, STRLEN msglen)
a0d0e21e 1529{
27da23d5 1530 dVAR;
87582a92 1531
3280af22 1532 if (PL_in_eval) {
a0d0e21e 1533 I32 cxix;
a0d0e21e 1534 I32 gimme;
a0d0e21e 1535
4e6ea2c3 1536 if (message) {
faef0170 1537 if (PL_in_eval & EVAL_KEEPERR) {
bfed75c6 1538 static const char prefix[] = "\t(in cleanup) ";
2d03de9c 1539 SV * const err = ERRSV;
c445ea15 1540 const char *e = NULL;
98eae8f5 1541 if (!SvPOK(err))
76f68e9b 1542 sv_setpvs(err,"");
98eae8f5 1543 else if (SvCUR(err) >= sizeof(prefix)+msglen-1) {
0510663f 1544 STRLEN len;
349d4f2f 1545 e = SvPV_const(err, len);
0510663f 1546 e += len - msglen;
98eae8f5 1547 if (*e != *message || strNE(e,message))
c445ea15 1548 e = NULL;
98eae8f5
GS
1549 }
1550 if (!e) {
1551 SvGROW(err, SvCUR(err)+sizeof(prefix)+msglen);
1552 sv_catpvn(err, prefix, sizeof(prefix)-1);
1553 sv_catpvn(err, message, msglen);
e476b1b5 1554 if (ckWARN(WARN_MISC)) {
504618e9 1555 const STRLEN start = SvCUR(err)-msglen-sizeof(prefix)+1;
f1f66076
RGS
1556 Perl_warner(aTHX_ packWARN(WARN_MISC), "%s",
1557 SvPVX_const(err)+start);
4e6ea2c3 1558 }
4633a7c4 1559 }
4633a7c4 1560 }
1aa99e6b 1561 else {
06bf62c7 1562 sv_setpvn(ERRSV, message, msglen);
1aa99e6b 1563 }
4633a7c4 1564 }
4e6ea2c3 1565
5a844595
GS
1566 while ((cxix = dopoptoeval(cxstack_ix)) < 0
1567 && PL_curstackinfo->si_prev)
1568 {
bac4b2ad 1569 dounwind(-1);
d3acc0f7 1570 POPSTACK;
bac4b2ad 1571 }
e336de0d 1572
a0d0e21e
LW
1573 if (cxix >= 0) {
1574 I32 optype;
35a4481c 1575 register PERL_CONTEXT *cx;
901017d6 1576 SV **newsp;
a0d0e21e
LW
1577
1578 if (cxix < cxstack_ix)
1579 dounwind(cxix);
1580
3280af22 1581 POPBLOCK(cx,PL_curpm);
6b35e009 1582 if (CxTYPE(cx) != CXt_EVAL) {
16869676 1583 if (!message)
349d4f2f 1584 message = SvPVx_const(ERRSV, msglen);
10edeb5d 1585 PerlIO_write(Perl_error_log, (const char *)"panic: die ", 11);
bf49b057 1586 PerlIO_write(Perl_error_log, message, msglen);
a0d0e21e
LW
1587 my_exit(1);
1588 }
1589 POPEVAL(cx);
1590
1591 if (gimme == G_SCALAR)
3280af22
NIS
1592 *++newsp = &PL_sv_undef;
1593 PL_stack_sp = newsp;
a0d0e21e
LW
1594
1595 LEAVE;
748a9306 1596
7fb6a879
GS
1597 /* LEAVE could clobber PL_curcop (see save_re_context())
1598 * XXX it might be better to find a way to avoid messing with
1599 * PL_curcop in save_re_context() instead, but this is a more
1600 * minimal fix --GSAR */
1601 PL_curcop = cx->blk_oldcop;
1602
7a2e2cd6 1603 if (optype == OP_REQUIRE) {
44f8325f 1604 const char* const msg = SvPVx_nolen_const(ERRSV);
901017d6 1605 SV * const nsv = cx->blk_eval.old_namesv;
b15aece3 1606 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
27bcc0a7 1607 &PL_sv_undef, 0);
5a844595
GS
1608 DIE(aTHX_ "%sCompilation failed in require",
1609 *msg ? msg : "Unknown error\n");
7a2e2cd6 1610 }
f39bc417
DM
1611 assert(CxTYPE(cx) == CXt_EVAL);
1612 return cx->blk_eval.retop;
a0d0e21e
LW
1613 }
1614 }
9cc2fdd3 1615 if (!message)
349d4f2f 1616 message = SvPVx_const(ERRSV, msglen);
87582a92 1617
7ff03255 1618 write_to_stderr(message, msglen);
f86702cc 1619 my_failure_exit();
1620 /* NOTREACHED */
a0d0e21e
LW
1621 return 0;
1622}
1623
1624PP(pp_xor)
1625{
97aff369 1626 dVAR; dSP; dPOPTOPssrl;
a0d0e21e
LW
1627 if (SvTRUE(left) != SvTRUE(right))
1628 RETSETYES;
1629 else
1630 RETSETNO;
1631}
1632
a0d0e21e
LW
1633PP(pp_caller)
1634{
97aff369 1635 dVAR;
39644a26 1636 dSP;
a0d0e21e 1637 register I32 cxix = dopoptosub(cxstack_ix);
901017d6
AL
1638 register const PERL_CONTEXT *cx;
1639 register const PERL_CONTEXT *ccstack = cxstack;
1640 const PERL_SI *top_si = PL_curstackinfo;
54310121 1641 I32 gimme;
06b5626a 1642 const char *stashname;
a0d0e21e
LW
1643 I32 count = 0;
1644
1645 if (MAXARG)
1646 count = POPi;
27d41816 1647
a0d0e21e 1648 for (;;) {
2c375eb9
GS
1649 /* we may be in a higher stacklevel, so dig down deeper */
1650 while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1651 top_si = top_si->si_prev;
1652 ccstack = top_si->si_cxstack;
1653 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1654 }
a0d0e21e 1655 if (cxix < 0) {
27d41816
DM
1656 if (GIMME != G_ARRAY) {
1657 EXTEND(SP, 1);
a0d0e21e 1658 RETPUSHUNDEF;
27d41816 1659 }
a0d0e21e
LW
1660 RETURN;
1661 }
f2a7f298
DG
1662 /* caller() should not report the automatic calls to &DB::sub */
1663 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 &&
3280af22 1664 ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
a0d0e21e
LW
1665 count++;
1666 if (!count--)
1667 break;
2c375eb9 1668 cxix = dopoptosub_at(ccstack, cxix - 1);
a0d0e21e 1669 }
2c375eb9
GS
1670
1671 cx = &ccstack[cxix];
7766f137 1672 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
f54cb97a 1673 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
2c375eb9 1674 /* We expect that ccstack[dbcxix] is CXt_SUB, anyway, the
06a5b730 1675 field below is defined for any cx. */
f2a7f298
DG
1676 /* caller() should not report the automatic calls to &DB::sub */
1677 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
2c375eb9 1678 cx = &ccstack[dbcxix];
06a5b730 1679 }
1680
ed094faf 1681 stashname = CopSTASHPV(cx->blk_oldcop);
a0d0e21e 1682 if (GIMME != G_ARRAY) {
27d41816 1683 EXTEND(SP, 1);
ed094faf 1684 if (!stashname)
3280af22 1685 PUSHs(&PL_sv_undef);
49d8d3a1
MB
1686 else {
1687 dTARGET;
ed094faf 1688 sv_setpv(TARG, stashname);
49d8d3a1
MB
1689 PUSHs(TARG);
1690 }
a0d0e21e
LW
1691 RETURN;
1692 }
a0d0e21e 1693
b3ca2e83 1694 EXTEND(SP, 11);
27d41816 1695
ed094faf 1696 if (!stashname)
3280af22 1697 PUSHs(&PL_sv_undef);
49d8d3a1 1698 else
6e449a3a
MHM
1699 mPUSHs(newSVpv(stashname, 0));
1700 mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
1701 mPUSHi((I32)CopLINE(cx->blk_oldcop));
a0d0e21e
LW
1702 if (!MAXARG)
1703 RETURN;
7766f137 1704 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
0bd48802 1705 GV * const cvgv = CvGV(ccstack[cxix].blk_sub.cv);
7766f137 1706 /* So is ccstack[dbcxix]. */
07b8c804 1707 if (isGV(cvgv)) {
561b68a9 1708 SV * const sv = newSV(0);
c445ea15 1709 gv_efullname3(sv, cvgv, NULL);
6e449a3a 1710 mPUSHs(sv);
bf38a478 1711 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804
RGS
1712 }
1713 else {
84bafc02 1714 PUSHs(newSVpvs_flags("(unknown)", SVs_TEMP));
bf38a478 1715 PUSHs(boolSV(CxHASARGS(cx)));
07b8c804 1716 }
a0d0e21e
LW
1717 }
1718 else {
84bafc02 1719 PUSHs(newSVpvs_flags("(eval)", SVs_TEMP));
6e449a3a 1720 mPUSHi(0);
a0d0e21e 1721 }
54310121 1722 gimme = (I32)cx->blk_gimme;
1723 if (gimme == G_VOID)
3280af22 1724 PUSHs(&PL_sv_undef);
54310121 1725 else
98625aca 1726 PUSHs(boolSV((gimme & G_WANT) == G_ARRAY));
6b35e009 1727 if (CxTYPE(cx) == CXt_EVAL) {
811a4de9 1728 /* eval STRING */
85a64632 1729 if (CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
4633a7c4 1730 PUSHs(cx->blk_eval.cur_text);
3280af22 1731 PUSHs(&PL_sv_no);
0f79a09d 1732 }
811a4de9 1733 /* require */
0f79a09d 1734 else if (cx->blk_eval.old_namesv) {
6e449a3a 1735 mPUSHs(newSVsv(cx->blk_eval.old_namesv));
3280af22 1736 PUSHs(&PL_sv_yes);
06a5b730 1737 }
811a4de9
GS
1738 /* eval BLOCK (try blocks have old_namesv == 0) */
1739 else {
1740 PUSHs(&PL_sv_undef);
1741 PUSHs(&PL_sv_undef);
1742 }
4633a7c4 1743 }
a682de96
GS
1744 else {
1745 PUSHs(&PL_sv_undef);
1746 PUSHs(&PL_sv_undef);
1747 }
bafb2adc 1748 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)
ed094faf 1749 && CopSTASH_eq(PL_curcop, PL_debstash))
4633a7c4 1750 {
66a1b24b
AL
1751 AV * const ary = cx->blk_sub.argarray;
1752 const int off = AvARRAY(ary) - AvALLOC(ary);
a0d0e21e 1753
3280af22 1754 if (!PL_dbargs) {
71315bf2 1755 GV* const tmpgv = gv_fetchpvs("DB::args", GV_ADD, SVt_PVAV);
0bd48802 1756 PL_dbargs = GvAV(gv_AVadd(tmpgv));
a5f75d66 1757 GvMULTI_on(tmpgv);
3ddcf04c 1758 AvREAL_off(PL_dbargs); /* XXX should be REIFY (see av.h) */
a0d0e21e
LW
1759 }
1760
3280af22
NIS
1761 if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
1762 av_extend(PL_dbargs, AvFILLp(ary) + off);
1763 Copy(AvALLOC(ary), AvARRAY(PL_dbargs), AvFILLp(ary) + 1 + off, SV*);
1764 AvFILLp(PL_dbargs) = AvFILLp(ary) + off;
a0d0e21e 1765 }
f3aa04c2
GS
1766 /* XXX only hints propagated via op_private are currently
1767 * visible (others are not easily accessible, since they
1768 * use the global PL_hints) */
6e449a3a 1769 mPUSHi(CopHINTS_get(cx->blk_oldcop));
e476b1b5
GS
1770 {
1771 SV * mask ;
72dc9ed5 1772 STRLEN * const old_warnings = cx->blk_oldcop->cop_warnings ;
114bafba 1773
ac27b0f5 1774 if (old_warnings == pWARN_NONE ||
114bafba 1775 (old_warnings == pWARN_STD && (PL_dowarn & G_WARN_ON) == 0))
e476b1b5 1776 mask = newSVpvn(WARN_NONEstring, WARNsize) ;
ac27b0f5 1777 else if (old_warnings == pWARN_ALL ||
75b6c4ca
RGS
1778 (old_warnings == pWARN_STD && PL_dowarn & G_WARN_ON)) {
1779 /* Get the bit mask for $warnings::Bits{all}, because
1780 * it could have been extended by warnings::register */
1781 SV **bits_all;
0bd48802 1782 HV * const bits = get_hv("warnings::Bits", FALSE);
017a3ce5 1783 if (bits && (bits_all=hv_fetchs(bits, "all", FALSE))) {
75b6c4ca
RGS
1784 mask = newSVsv(*bits_all);
1785 }
1786 else {
1787 mask = newSVpvn(WARN_ALLstring, WARNsize) ;
1788 }
1789 }
e476b1b5 1790 else
72dc9ed5 1791 mask = newSVpvn((char *) (old_warnings + 1), old_warnings[0]);
6e449a3a 1792 mPUSHs(mask);
e476b1b5 1793 }
b3ca2e83 1794
c28fe1ec 1795 PUSHs(cx->blk_oldcop->cop_hints_hash ?
b3ca2e83 1796 sv_2mortal(newRV_noinc(
ad64d0ec
NC
1797 MUTABLE_SV(Perl_refcounted_he_chain_2hv(aTHX_
1798 cx->blk_oldcop->cop_hints_hash))))
b3ca2e83 1799 : &PL_sv_undef);
a0d0e21e
LW
1800 RETURN;
1801}
1802
a0d0e21e
LW
1803PP(pp_reset)
1804{
97aff369 1805 dVAR;
39644a26 1806 dSP;
10edeb5d 1807 const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
11faa288 1808 sv_reset(tmps, CopSTASH(PL_curcop));
3280af22 1809 PUSHs(&PL_sv_yes);
a0d0e21e
LW
1810 RETURN;
1811}
1812
dd2155a4
DM
1813/* like pp_nextstate, but used instead when the debugger is active */
1814
a0d0e21e
LW
1815PP(pp_dbstate)
1816{
27da23d5 1817 dVAR;
533c011a 1818 PL_curcop = (COP*)PL_op;
a0d0e21e 1819 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 1820 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
1821 FREETMPS;
1822
5df8de69
DM
1823 if (PL_op->op_flags & OPf_SPECIAL /* breakpoint */
1824 || SvIV(PL_DBsingle) || SvIV(PL_DBsignal) || SvIV(PL_DBtrace))
a0d0e21e 1825 {
39644a26 1826 dSP;
c09156bb 1827 register PERL_CONTEXT *cx;
f54cb97a 1828 const I32 gimme = G_ARRAY;
eb160463 1829 U8 hasargs;
0bd48802
AL
1830 GV * const gv = PL_DBgv;
1831 register CV * const cv = GvCV(gv);
a0d0e21e 1832
a0d0e21e 1833 if (!cv)
cea2e8a9 1834 DIE(aTHX_ "No DB::DB routine defined");
a0d0e21e 1835
aea4f609
DM
1836 if (CvDEPTH(cv) >= 1 && !(PL_debug & DEBUG_DB_RECURSE_FLAG))
1837 /* don't do recursive DB::DB call */
a0d0e21e 1838 return NORMAL;
748a9306 1839
4633a7c4
LW
1840 ENTER;
1841 SAVETMPS;
1842
3280af22 1843 SAVEI32(PL_debug);
55497cff 1844 SAVESTACK_POS();
3280af22 1845 PL_debug = 0;
748a9306 1846 hasargs = 0;
924508f0 1847 SPAGAIN;
748a9306 1848
aed2304a 1849 if (CvISXSUB(cv)) {
c127bd3a
SF
1850 CvDEPTH(cv)++;
1851 PUSHMARK(SP);
1852 (void)(*CvXSUB(cv))(aTHX_ cv);
1853 CvDEPTH(cv)--;
1854 FREETMPS;
1855 LEAVE;
1856 return NORMAL;
1857 }
1858 else {
1859 PUSHBLOCK(cx, CXt_SUB, SP);
1860 PUSHSUB_DB(cx);
1861 cx->blk_sub.retop = PL_op->op_next;
1862 CvDEPTH(cv)++;
1863 SAVECOMPPAD();
1864 PAD_SET_CUR_NOSAVE(CvPADLIST(cv), 1);
1865 RETURNOP(CvSTART(cv));
1866 }
a0d0e21e
LW
1867 }
1868 else
1869 return NORMAL;
1870}
1871
a0d0e21e
LW
1872PP(pp_enteriter)
1873{
27da23d5 1874 dVAR; dSP; dMARK;
c09156bb 1875 register PERL_CONTEXT *cx;
f54cb97a 1876 const I32 gimme = GIMME_V;
a0d0e21e 1877 SV **svp;
840fe433 1878 U8 cxtype = CXt_LOOP_FOR;
7766f137 1879#ifdef USE_ITHREADS
e846cb92 1880 PAD *iterdata;
7766f137 1881#endif
a0d0e21e 1882
4633a7c4
LW
1883 ENTER;
1884 SAVETMPS;
1885
533c011a 1886 if (PL_op->op_targ) {
14f338dc
DM
1887 if (PL_op->op_private & OPpLVAL_INTRO) { /* for my $x (...) */
1888 SvPADSTALE_off(PAD_SVl(PL_op->op_targ));
1889 SAVESETSVFLAGS(PAD_SVl(PL_op->op_targ),
1890 SVs_PADSTALE, SVs_PADSTALE);
1891 }
09edbca0 1892 SAVEPADSVANDMORTALIZE(PL_op->op_targ);
c3564e5c 1893#ifndef USE_ITHREADS
dd2155a4 1894 svp = &PAD_SVl(PL_op->op_targ); /* "my" variable */
c3564e5c 1895#else
e846cb92 1896 iterdata = NULL;
7766f137 1897#endif
54b9620d
MB
1898 }
1899 else {
159b6efe 1900 GV * const gv = MUTABLE_GV(POPs);
7766f137 1901 svp = &GvSV(gv); /* symbol table variable */
0214ae40 1902 SAVEGENERICSV(*svp);
561b68a9 1903 *svp = newSV(0);
7766f137 1904#ifdef USE_ITHREADS
e846cb92 1905 iterdata = (PAD*)gv;
7766f137 1906#endif
54b9620d 1907 }
4633a7c4 1908
0d863452
RH
1909 if (PL_op->op_private & OPpITER_DEF)
1910 cxtype |= CXp_FOR_DEF;
1911
a0d0e21e
LW
1912 ENTER;
1913
7766f137
GS
1914 PUSHBLOCK(cx, cxtype, SP);
1915#ifdef USE_ITHREADS
e846cb92 1916 PUSHLOOP_FOR(cx, iterdata, MARK, PL_op->op_targ);
7766f137 1917#else
52d1f6fb 1918 PUSHLOOP_FOR(cx, svp, MARK, 0);
7766f137 1919#endif
533c011a 1920 if (PL_op->op_flags & OPf_STACKED) {
d01136d6
BS
1921 SV *maybe_ary = POPs;
1922 if (SvTYPE(maybe_ary) != SVt_PVAV) {
89ea2908 1923 dPOPss;
d01136d6 1924 SV * const right = maybe_ary;
984a4bea
RD
1925 SvGETMAGIC(sv);
1926 SvGETMAGIC(right);
4fe3f0fa 1927 if (RANGE_IS_NUMERIC(sv,right)) {
d01136d6 1928 cx->cx_type &= ~CXTYPEMASK;
c6fdafd0
NC
1929 cx->cx_type |= CXt_LOOP_LAZYIV;
1930 /* Make sure that no-one re-orders cop.h and breaks our
1931 assumptions */
1932 assert(CxTYPE(cx) == CXt_LOOP_LAZYIV);
a2309040
JH
1933#ifdef NV_PRESERVES_UV
1934 if ((SvOK(sv) && ((SvNV(sv) < (NV)IV_MIN) ||
1935 (SvNV(sv) > (NV)IV_MAX)))
1936 ||
1937 (SvOK(right) && ((SvNV(right) > (NV)IV_MAX) ||
1938 (SvNV(right) < (NV)IV_MIN))))
1939#else
1940 if ((SvOK(sv) && ((SvNV(sv) <= (NV)IV_MIN)
1941 ||
1942 ((SvNV(sv) > 0) &&
1943 ((SvUV(sv) > (UV)IV_MAX) ||
1944 (SvNV(sv) > (NV)UV_MAX)))))
1945 ||
1946 (SvOK(right) && ((SvNV(right) <= (NV)IV_MIN)
1947 ||
1948 ((SvNV(right) > 0) &&
1949 ((SvUV(right) > (UV)IV_MAX) ||
1950 (SvNV(right) > (NV)UV_MAX))))))
1951#endif
076d9a11 1952 DIE(aTHX_ "Range iterator outside integer range");
d01136d6
BS
1953 cx->blk_loop.state_u.lazyiv.cur = SvIV(sv);
1954 cx->blk_loop.state_u.lazyiv.end = SvIV(right);
d4665a05
DM
1955#ifdef DEBUGGING
1956 /* for correct -Dstv display */
1957 cx->blk_oldsp = sp - PL_stack_base;
1958#endif
89ea2908 1959 }
3f63a782 1960 else {
d01136d6
BS
1961 cx->cx_type &= ~CXTYPEMASK;
1962 cx->cx_type |= CXt_LOOP_LAZYSV;
1963 /* Make sure that no-one re-orders cop.h and breaks our
1964 assumptions */
1965 assert(CxTYPE(cx) == CXt_LOOP_LAZYSV);
1966 cx->blk_loop.state_u.lazysv.cur = newSVsv(sv);
1967 cx->blk_loop.state_u.lazysv.end = right;
1968 SvREFCNT_inc(right);
1969 (void) SvPV_force_nolen(cx->blk_loop.state_u.lazysv.cur);
267cc4a8
NC
1970 /* This will do the upgrade to SVt_PV, and warn if the value
1971 is uninitialised. */
10516c54 1972 (void) SvPV_nolen_const(right);
267cc4a8
NC
1973 /* Doing this avoids a check every time in pp_iter in pp_hot.c
1974 to replace !SvOK() with a pointer to "". */
1975 if (!SvOK(right)) {
1976 SvREFCNT_dec(right);
d01136d6 1977 cx->blk_loop.state_u.lazysv.end = &PL_sv_no;
267cc4a8 1978 }
3f63a782 1979 }
89ea2908 1980 }
d01136d6 1981 else /* SvTYPE(maybe_ary) == SVt_PVAV */ {
502c6561 1982 cx->blk_loop.state_u.ary.ary = MUTABLE_AV(maybe_ary);
d01136d6
BS
1983 SvREFCNT_inc(maybe_ary);
1984 cx->blk_loop.state_u.ary.ix =
1985 (PL_op->op_private & OPpITER_REVERSED) ?
1986 AvFILL(cx->blk_loop.state_u.ary.ary) + 1 :
1987 -1;
ef3e5ea9 1988 }
89ea2908 1989 }
d01136d6
BS
1990 else { /* iterating over items on the stack */
1991 cx->blk_loop.state_u.ary.ary = NULL; /* means to use the stack */
ef3e5ea9 1992 if (PL_op->op_private & OPpITER_REVERSED) {
d01136d6 1993 cx->blk_loop.state_u.ary.ix = cx->blk_oldsp + 1;
ef3e5ea9
NC
1994 }
1995 else {
d01136d6 1996 cx->blk_loop.state_u.ary.ix = MARK - PL_stack_base;
ef3e5ea9 1997 }
4633a7c4 1998 }
a0d0e21e
LW
1999
2000 RETURN;
2001}
2002
2003PP(pp_enterloop)
2004{
27da23d5 2005 dVAR; dSP;
c09156bb 2006 register PERL_CONTEXT *cx;
f54cb97a 2007 const I32 gimme = GIMME_V;
a0d0e21e
LW
2008
2009 ENTER;
2010 SAVETMPS;
2011 ENTER;
2012
3b719c58
NC
2013 PUSHBLOCK(cx, CXt_LOOP_PLAIN, SP);
2014 PUSHLOOP_PLAIN(cx, SP);
a0d0e21e
LW
2015
2016 RETURN;
2017}
2018
2019PP(pp_leaveloop)
2020{
27da23d5 2021 dVAR; dSP;
c09156bb 2022 register PERL_CONTEXT *cx;
a0d0e21e
LW
2023 I32 gimme;
2024 SV **newsp;
2025 PMOP *newpm;
2026 SV **mark;
2027
2028 POPBLOCK(cx,newpm);
3b719c58 2029 assert(CxTYPE_is_LOOP(cx));
4fdae800 2030 mark = newsp;
a8bba7fa 2031 newsp = PL_stack_base + cx->blk_loop.resetsp;
f86702cc 2032
a1f49e72 2033 TAINT_NOT;
54310121 2034 if (gimme == G_VOID)
6f207bd3 2035 NOOP;
54310121 2036 else if (gimme == G_SCALAR) {
2037 if (mark < SP)
2038 *++newsp = sv_mortalcopy(*SP);
2039 else
3280af22 2040 *++newsp = &PL_sv_undef;
a0d0e21e
LW
2041 }
2042 else {
a1f49e72 2043 while (mark < SP) {
a0d0e21e 2044 *++newsp = sv_mortalcopy(*++mark);
a1f49e72
CS
2045 TAINT_NOT; /* Each item is independent */
2046 }
a0d0e21e 2047 }
f86702cc 2048 SP = newsp;
2049 PUTBACK;
2050
a8bba7fa 2051 POPLOOP(cx); /* Stack values are safe: release loop vars ... */
3280af22 2052 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2053
a0d0e21e
LW
2054 LEAVE;
2055 LEAVE;
2056
f86702cc 2057 return NORMAL;
a0d0e21e
LW
2058}
2059
2060PP(pp_return)
2061{
27da23d5 2062 dVAR; dSP; dMARK;
c09156bb 2063 register PERL_CONTEXT *cx;
f86702cc 2064 bool popsub2 = FALSE;
b45de488 2065 bool clear_errsv = FALSE;
a0d0e21e
LW
2066 I32 gimme;
2067 SV **newsp;
2068 PMOP *newpm;
2069 I32 optype = 0;
b0d9ce38 2070 SV *sv;
f39bc417 2071 OP *retop;
a0d0e21e 2072
0bd48802
AL
2073 const I32 cxix = dopoptosub(cxstack_ix);
2074
9850bf21
RH
2075 if (cxix < 0) {
2076 if (CxMULTICALL(cxstack)) { /* In this case we must be in a
2077 * sort block, which is a CXt_NULL
2078 * not a CXt_SUB */
2079 dounwind(0);
d7507f74
RH
2080 PL_stack_base[1] = *PL_stack_sp;
2081 PL_stack_sp = PL_stack_base + 1;
a0d0e21e
LW
2082 return 0;
2083 }
9850bf21
RH
2084 else
2085 DIE(aTHX_ "Can't return outside a subroutine");
a0d0e21e 2086 }
a0d0e21e
LW
2087 if (cxix < cxstack_ix)
2088 dounwind(cxix);
2089
d7507f74
RH
2090 if (CxMULTICALL(&cxstack[cxix])) {
2091 gimme = cxstack[cxix].blk_gimme;
2092 if (gimme == G_VOID)
2093 PL_stack_sp = PL_stack_base;
2094 else if (gimme == G_SCALAR) {
2095 PL_stack_base[1] = *PL_stack_sp;
2096 PL_stack_sp = PL_stack_base + 1;
2097 }
9850bf21 2098 return 0;
d7507f74 2099 }
9850bf21 2100
a0d0e21e 2101 POPBLOCK(cx,newpm);
6b35e009 2102 switch (CxTYPE(cx)) {
a0d0e21e 2103 case CXt_SUB:
f86702cc 2104 popsub2 = TRUE;
f39bc417 2105 retop = cx->blk_sub.retop;
5dd42e15 2106 cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
a0d0e21e
LW
2107 break;
2108 case CXt_EVAL:
b45de488
GS
2109 if (!(PL_in_eval & EVAL_KEEPERR))
2110 clear_errsv = TRUE;
a0d0e21e 2111 POPEVAL(cx);
f39bc417 2112 retop = cx->blk_eval.retop;
1d76a5c3
GS
2113 if (CxTRYBLOCK(cx))
2114 break;
067f92a0 2115 lex_end();
748a9306
LW
2116 if (optype == OP_REQUIRE &&
2117 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
2118 {
54310121 2119 /* Unassume the success we assumed earlier. */
901017d6 2120 SV * const nsv = cx->blk_eval.old_namesv;
b15aece3 2121 (void)hv_delete(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv), G_DISCARD);
be2597df 2122 DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(nsv));
748a9306 2123 }
a0d0e21e 2124 break;
7766f137
GS
2125 case CXt_FORMAT:
2126 POPFORMAT(cx);
f39bc417 2127 retop = cx->blk_sub.retop;
7766f137 2128 break;
a0d0e21e 2129 default:
cea2e8a9 2130 DIE(aTHX_ "panic: return");
a0d0e21e
LW
2131 }
2132
a1f49e72 2133 TAINT_NOT;
a0d0e21e 2134 if (gimme == G_SCALAR) {
a29cdaf0
IZ
2135 if (MARK < SP) {
2136 if (popsub2) {
a8bba7fa 2137 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
2138 if (SvTEMP(TOPs)) {
2139 *++newsp = SvREFCNT_inc(*SP);
2140 FREETMPS;
2141 sv_2mortal(*newsp);
959e3673
GS
2142 }
2143 else {
2144 sv = SvREFCNT_inc(*SP); /* FREETMPS could clobber it */
a29cdaf0 2145 FREETMPS;
959e3673
GS
2146 *++newsp = sv_mortalcopy(sv);
2147 SvREFCNT_dec(sv);
a29cdaf0 2148 }
959e3673
GS
2149 }
2150 else
a29cdaf0 2151 *++newsp = (SvTEMP(*SP)) ? *SP : sv_mortalcopy(*SP);
959e3673
GS
2152 }
2153 else
a29cdaf0 2154 *++newsp = sv_mortalcopy(*SP);
959e3673
GS
2155 }
2156 else
3280af22 2157 *++newsp = &PL_sv_undef;
a0d0e21e 2158 }
54310121 2159 else if (gimme == G_ARRAY) {
a1f49e72 2160 while (++MARK <= SP) {
f86702cc 2161 *++newsp = (popsub2 && SvTEMP(*MARK))
2162 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2163 TAINT_NOT; /* Each item is independent */
2164 }
a0d0e21e 2165 }
3280af22 2166 PL_stack_sp = newsp;
a0d0e21e 2167
5dd42e15 2168 LEAVE;
f86702cc 2169 /* Stack values are safe: */
2170 if (popsub2) {
5dd42e15 2171 cxstack_ix--;
b0d9ce38 2172 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2173 }
b0d9ce38 2174 else
c445ea15 2175 sv = NULL;
3280af22 2176 PL_curpm = newpm; /* ... and pop $1 et al */
f86702cc 2177
b0d9ce38 2178 LEAVESUB(sv);
8433848b 2179 if (clear_errsv) {
ab69dbc2 2180 CLEAR_ERRSV();
8433848b 2181 }
f39bc417 2182 return retop;
a0d0e21e
LW
2183}
2184
2185PP(pp_last)
2186{
27da23d5 2187 dVAR; dSP;
a0d0e21e 2188 I32 cxix;
c09156bb 2189 register PERL_CONTEXT *cx;
f86702cc 2190 I32 pop2 = 0;
a0d0e21e 2191 I32 gimme;
8772537c 2192 I32 optype;
a0d0e21e
LW
2193 OP *nextop;
2194 SV **newsp;
2195 PMOP *newpm;
a8bba7fa 2196 SV **mark;
c445ea15 2197 SV *sv = NULL;
9d4ba2ae 2198
a0d0e21e 2199
533c011a 2200 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2201 cxix = dopoptoloop(cxstack_ix);
2202 if (cxix < 0)
a651a37d 2203 DIE(aTHX_ "Can't \"last\" outside a loop block");
a0d0e21e
LW
2204 }
2205 else {
2206 cxix = dopoptolabel(cPVOP->op_pv);
2207 if (cxix < 0)
cea2e8a9 2208 DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
a0d0e21e
LW
2209 }
2210 if (cxix < cxstack_ix)
2211 dounwind(cxix);
2212
2213 POPBLOCK(cx,newpm);
5dd42e15 2214 cxstack_ix++; /* temporarily protect top context */
a8bba7fa 2215 mark = newsp;
6b35e009 2216 switch (CxTYPE(cx)) {
c6fdafd0 2217 case CXt_LOOP_LAZYIV:
d01136d6 2218 case CXt_LOOP_LAZYSV:
3b719c58
NC
2219 case CXt_LOOP_FOR:
2220 case CXt_LOOP_PLAIN:
2221 pop2 = CxTYPE(cx);
a8bba7fa 2222 newsp = PL_stack_base + cx->blk_loop.resetsp;
022eaa24 2223 nextop = cx->blk_loop.my_op->op_lastop->op_next;
a0d0e21e 2224 break;
f86702cc 2225 case CXt_SUB:
f86702cc 2226 pop2 = CXt_SUB;
f39bc417 2227 nextop = cx->blk_sub.retop;
a0d0e21e 2228 break;
f86702cc 2229 case CXt_EVAL:
2230 POPEVAL(cx);
f39bc417 2231 nextop = cx->blk_eval.retop;
a0d0e21e 2232 break;
7766f137
GS
2233 case CXt_FORMAT:
2234 POPFORMAT(cx);
f39bc417 2235 nextop = cx->blk_sub.retop;
7766f137 2236 break;
a0d0e21e 2237 default:
cea2e8a9 2238 DIE(aTHX_ "panic: last");
a0d0e21e
LW
2239 }
2240
a1f49e72 2241 TAINT_NOT;
a0d0e21e 2242 if (gimme == G_SCALAR) {
f86702cc 2243 if (MARK < SP)
2244 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
2245 ? *SP : sv_mortalcopy(*SP);
a0d0e21e 2246 else
3280af22 2247 *++newsp = &PL_sv_undef;
a0d0e21e 2248 }
54310121 2249 else if (gimme == G_ARRAY) {
a1f49e72 2250 while (++MARK <= SP) {
f86702cc 2251 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
2252 ? *MARK : sv_mortalcopy(*MARK);
a1f49e72
CS
2253 TAINT_NOT; /* Each item is independent */
2254 }
f86702cc 2255 }
2256 SP = newsp;
2257 PUTBACK;
2258
5dd42e15
DM
2259 LEAVE;
2260 cxstack_ix--;
f86702cc 2261 /* Stack values are safe: */
2262 switch (pop2) {
c6fdafd0 2263 case CXt_LOOP_LAZYIV:
3b719c58 2264 case CXt_LOOP_PLAIN:
d01136d6 2265 case CXt_LOOP_LAZYSV:
3b719c58 2266 case CXt_LOOP_FOR:
a8bba7fa 2267 POPLOOP(cx); /* release loop vars ... */
4fdae800 2268 LEAVE;
f86702cc 2269 break;
2270 case CXt_SUB:
b0d9ce38 2271 POPSUB(cx,sv); /* release CV and @_ ... */
f86702cc 2272 break;
a0d0e21e 2273 }
3280af22 2274 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e 2275
b0d9ce38 2276 LEAVESUB(sv);
9d4ba2ae
AL
2277 PERL_UNUSED_VAR(optype);
2278 PERL_UNUSED_VAR(gimme);
f86702cc 2279 return nextop;
a0d0e21e
LW
2280}
2281
2282PP(pp_next)
2283{
27da23d5 2284 dVAR;
a0d0e21e 2285 I32 cxix;
c09156bb 2286 register PERL_CONTEXT *cx;
85538317 2287 I32 inner;
a0d0e21e 2288
533c011a 2289 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2290 cxix = dopoptoloop(cxstack_ix);
2291 if (cxix < 0)
a651a37d 2292 DIE(aTHX_ "Can't \"next\" outside a loop block");
a0d0e21e
LW
2293 }
2294 else {
2295 cxix = dopoptolabel(cPVOP->op_pv);
2296 if (cxix < 0)
cea2e8a9 2297 DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
a0d0e21e
LW
2298 }
2299 if (cxix < cxstack_ix)
2300 dounwind(cxix);
2301
85538317
GS
2302 /* clear off anything above the scope we're re-entering, but
2303 * save the rest until after a possible continue block */
2304 inner = PL_scopestack_ix;
1ba6ee2b 2305 TOPBLOCK(cx);
85538317
GS
2306 if (PL_scopestack_ix < inner)
2307 leave_scope(PL_scopestack[PL_scopestack_ix]);
3a1b2b9e 2308 PL_curcop = cx->blk_oldcop;
022eaa24 2309 return CX_LOOP_NEXTOP_GET(cx);
a0d0e21e
LW
2310}
2311
2312PP(pp_redo)
2313{
27da23d5 2314 dVAR;
a0d0e21e 2315 I32 cxix;
c09156bb 2316 register PERL_CONTEXT *cx;
a0d0e21e 2317 I32 oldsave;
a034e688 2318 OP* redo_op;
a0d0e21e 2319
533c011a 2320 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e
LW
2321 cxix = dopoptoloop(cxstack_ix);
2322 if (cxix < 0)
a651a37d 2323 DIE(aTHX_ "Can't \"redo\" outside a loop block");
a0d0e21e
LW
2324 }
2325 else {
2326 cxix = dopoptolabel(cPVOP->op_pv);
2327 if (cxix < 0)
cea2e8a9 2328 DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
a0d0e21e
LW
2329 }
2330 if (cxix < cxstack_ix)
2331 dounwind(cxix);
2332
022eaa24 2333 redo_op = cxstack[cxix].blk_loop.my_op->op_redoop;
a034e688
DM
2334 if (redo_op->op_type == OP_ENTER) {
2335 /* pop one less context to avoid $x being freed in while (my $x..) */
2336 cxstack_ix++;
2337 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_BLOCK);
2338 redo_op = redo_op->op_next;
2339 }
2340
a0d0e21e 2341 TOPBLOCK(cx);
3280af22 2342 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e 2343 LEAVE_SCOPE(oldsave);
936c78b5 2344 FREETMPS;
3a1b2b9e 2345 PL_curcop = cx->blk_oldcop;
a034e688 2346 return redo_op;
a0d0e21e
LW
2347}
2348
0824fdcb 2349STATIC OP *
bfed75c6 2350S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
a0d0e21e 2351{
97aff369 2352 dVAR;
a0d0e21e 2353 OP **ops = opstack;
bfed75c6 2354 static const char too_deep[] = "Target of goto is too deeply nested";
a0d0e21e 2355
7918f24d
NC
2356 PERL_ARGS_ASSERT_DOFINDLABEL;
2357
fc36a67e 2358 if (ops >= oplimit)
cea2e8a9 2359 Perl_croak(aTHX_ too_deep);
11343788
MB
2360 if (o->op_type == OP_LEAVE ||
2361 o->op_type == OP_SCOPE ||
2362 o->op_type == OP_LEAVELOOP ||
33d34e4c 2363 o->op_type == OP_LEAVESUB ||
11343788 2364 o->op_type == OP_LEAVETRY)
fc36a67e 2365 {
5dc0d613 2366 *ops++ = cUNOPo->op_first;
fc36a67e 2367 if (ops >= oplimit)
cea2e8a9 2368 Perl_croak(aTHX_ too_deep);
fc36a67e 2369 }
c4aa4e48 2370 *ops = 0;
11343788 2371 if (o->op_flags & OPf_KIDS) {
aec46f14 2372 OP *kid;
a0d0e21e 2373 /* First try all the kids at this level, since that's likeliest. */
11343788 2374 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
c4aa4e48 2375 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
4b65a919 2376 CopLABEL(kCOP) && strEQ(CopLABEL(kCOP), label))
a0d0e21e
LW
2377 return kid;
2378 }
11343788 2379 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
3280af22 2380 if (kid == PL_lastgotoprobe)
a0d0e21e 2381 continue;
ed8d0fe2
SM
2382 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2383 if (ops == opstack)
2384 *ops++ = kid;
2385 else if (ops[-1]->op_type == OP_NEXTSTATE ||
2386 ops[-1]->op_type == OP_DBSTATE)
2387 ops[-1] = kid;
2388 else
2389 *ops++ = kid;
2390 }
155aba94 2391 if ((o = dofindlabel(kid, label, ops, oplimit)))
11343788 2392 return o;
a0d0e21e
LW
2393 }
2394 }
c4aa4e48 2395 *ops = 0;
a0d0e21e
LW
2396 return 0;
2397}
2398
a0d0e21e
LW
2399PP(pp_goto)
2400{
27da23d5 2401 dVAR; dSP;
cbbf8932 2402 OP *retop = NULL;
a0d0e21e 2403 I32 ix;
c09156bb 2404 register PERL_CONTEXT *cx;
fc36a67e 2405#define GOTO_DEPTH 64
2406 OP *enterops[GOTO_DEPTH];
cbbf8932 2407 const char *label = NULL;
bfed75c6
AL
2408 const bool do_dump = (PL_op->op_type == OP_DUMP);
2409 static const char must_have_label[] = "goto must have label";
a0d0e21e 2410
533c011a 2411 if (PL_op->op_flags & OPf_STACKED) {
9d4ba2ae 2412 SV * const sv = POPs;
a0d0e21e
LW
2413
2414 /* This egregious kludge implements goto &subroutine */
2415 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
2416 I32 cxix;
c09156bb 2417 register PERL_CONTEXT *cx;
ea726b52 2418 CV *cv = MUTABLE_CV(SvRV(sv));
a0d0e21e
LW
2419 SV** mark;
2420 I32 items = 0;
2421 I32 oldsave;
b1464ded 2422 bool reified = 0;
a0d0e21e 2423
e8f7dd13 2424 retry:
4aa0a1f7 2425 if (!CvROOT(cv) && !CvXSUB(cv)) {
7fc63493 2426 const GV * const gv = CvGV(cv);
e8f7dd13 2427 if (gv) {
7fc63493 2428 GV *autogv;
e8f7dd13
GS
2429 SV *tmpstr;
2430 /* autoloaded stub? */
2431 if (cv != GvCV(gv) && (cv = GvCV(gv)))
2432 goto retry;
2433 autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
2434 GvNAMELEN(gv), FALSE);
2435 if (autogv && (cv = GvCV(autogv)))
2436 goto retry;
2437 tmpstr = sv_newmortal();
c445ea15 2438 gv_efullname3(tmpstr, gv, NULL);
be2597df 2439 DIE(aTHX_ "Goto undefined subroutine &%"SVf"", SVfARG(tmpstr));
4aa0a1f7 2440 }
cea2e8a9 2441 DIE(aTHX_ "Goto undefined subroutine");
4aa0a1f7
AD
2442 }
2443
a0d0e21e 2444 /* First do some returnish stuff. */
b37c2d43 2445 SvREFCNT_inc_simple_void(cv); /* avoid premature free during unwind */
71fc2216 2446 FREETMPS;
a0d0e21e
LW
2447 cxix = dopoptosub(cxstack_ix);
2448 if (cxix < 0)
cea2e8a9 2449 DIE(aTHX_ "Can't goto subroutine outside a subroutine");
a0d0e21e
LW
2450 if (cxix < cxstack_ix)
2451 dounwind(cxix);
2452 TOPBLOCK(cx);
2d43a17f 2453 SPAGAIN;
564abe23 2454 /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
2d43a17f 2455 if (CxTYPE(cx) == CXt_EVAL) {
c74ace89
DM
2456 if (CxREALEVAL(cx))
2457 DIE(aTHX_ "Can't goto subroutine from an eval-string");
2458 else
2459 DIE(aTHX_ "Can't goto subroutine from an eval-block");
2d43a17f 2460 }
9850bf21
RH
2461 else if (CxMULTICALL(cx))
2462 DIE(aTHX_ "Can't goto subroutine from a sort sub (or similar callback)");
bafb2adc 2463 if (CxTYPE(cx) == CXt_SUB && CxHASARGS(cx)) {
d8b46c1b 2464 /* put @_ back onto stack */
a0d0e21e 2465 AV* av = cx->blk_sub.argarray;
bfed75c6 2466
93965878 2467 items = AvFILLp(av) + 1;
a45cdc79
DM
2468 EXTEND(SP, items+1); /* @_ could have been extended. */
2469 Copy(AvARRAY(av), SP + 1, items, SV*);
3280af22
NIS
2470 SvREFCNT_dec(GvAV(PL_defgv));
2471 GvAV(PL_defgv) = cx->blk_sub.savearray;
b1464ded 2472 CLEAR_ARGARRAY(av);
d8b46c1b 2473 /* abandon @_ if it got reified */
62b1ebc2 2474 if (AvREAL(av)) {
b1464ded
DM
2475 reified = 1;
2476 SvREFCNT_dec(av);
d8b46c1b
GS
2477 av = newAV();
2478 av_extend(av, items-1);
11ca45c0 2479 AvREIFY_only(av);
ad64d0ec 2480 PAD_SVl(0) = MUTABLE_SV(cx->blk_sub.argarray = av);
62b1ebc2 2481 }
a0d0e21e 2482 }
aed2304a 2483 else if (CvISXSUB(cv)) { /* put GvAV(defgv) back onto stack */
890ce7af 2484 AV* const av = GvAV(PL_defgv);
1fa4e549 2485 items = AvFILLp(av) + 1;
a45cdc79
DM
2486 EXTEND(SP, items+1); /* @_ could have been extended. */
2487 Copy(AvARRAY(av), SP + 1, items, SV*);
1fa4e549 2488 }
a45cdc79
DM
2489 mark = SP;
2490 SP += items;
6b35e009 2491 if (CxTYPE(cx) == CXt_SUB &&
b150fb22 2492 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
a0d0e21e 2493 SvREFCNT_dec(cx->blk_sub.cv);
3280af22 2494 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
2495 LEAVE_SCOPE(oldsave);
2496
2497 /* Now do some callish stuff. */
2498 SAVETMPS;
5023d17a 2499 SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
aed2304a 2500 if (CvISXSUB(cv)) {
b37c2d43 2501 OP* const retop = cx->blk_sub.retop;
f73ef291
NC
2502 SV **newsp;
2503 I32 gimme;
b1464ded
DM
2504 if (reified) {
2505 I32 index;
2506 for (index=0; index<items; index++)
2507 sv_2mortal(SP[-index]);
2508 }
1fa4e549 2509
b37c2d43
AL
2510 /* XS subs don't have a CxSUB, so pop it */
2511 POPBLOCK(cx, PL_curpm);
2512 /* Push a mark for the start of arglist */
2513 PUSHMARK(mark);
2514 PUTBACK;
2515 (void)(*CvXSUB(cv))(aTHX_ cv);
a0d0e21e 2516 LEAVE;
5eff7df7 2517 return retop;
a0d0e21e
LW
2518 }
2519 else {
b37c2d43 2520 AV* const padlist = CvPADLIST(cv);
6b35e009 2521 if (CxTYPE(cx) == CXt_EVAL) {
85a64632 2522 PL_in_eval = CxOLD_IN_EVAL(cx);
3280af22 2523 PL_eval_root = cx->blk_eval.old_eval_root;
b150fb22 2524 cx->cx_type = CXt_SUB;
b150fb22 2525 }
a0d0e21e 2526 cx->blk_sub.cv = cv;
1a5b3db4 2527 cx->blk_sub.olddepth = CvDEPTH(cv);
dd2155a4 2528
a0d0e21e
LW
2529 CvDEPTH(cv)++;
2530 if (CvDEPTH(cv) < 2)
74c765eb 2531 SvREFCNT_inc_simple_void_NN(cv);
dd2155a4 2532 else {
2b9dff67 2533 if (CvDEPTH(cv) == PERL_SUB_DEPTH_WARN && ckWARN(WARN_RECURSION))
44a8e56a 2534 sub_crush_depth(cv);
26019298 2535 pad_push(padlist, CvDEPTH(cv));
a0d0e21e 2536 }
fd617465
DM
2537 SAVECOMPPAD();
2538 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
bafb2adc 2539 if (CxHASARGS(cx))
6d4ff0d2 2540 {
502c6561 2541 AV *const av = MUTABLE_AV(PAD_SVl(0));
a0d0e21e 2542
3280af22 2543 cx->blk_sub.savearray = GvAV(PL_defgv);
502c6561 2544 GvAV(PL_defgv) = MUTABLE_AV(SvREFCNT_inc_simple(av));
dd2155a4 2545 CX_CURPAD_SAVE(cx->blk_sub);
6d4ff0d2 2546 cx->blk_sub.argarray = av;
a0d0e21e
LW
2547
2548 if (items >= AvMAX(av) + 1) {
b37c2d43 2549 SV **ary = AvALLOC(av);
a0d0e21e
LW
2550 if (AvARRAY(av) != ary) {
2551 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
9c6bc640 2552 AvARRAY(av) = ary;
a0d0e21e
LW
2553 }
2554 if (items >= AvMAX(av) + 1) {
2555 AvMAX(av) = items - 1;
2556 Renew(ary,items+1,SV*);
2557 AvALLOC(av) = ary;
9c6bc640 2558 AvARRAY(av) = ary;
a0d0e21e
LW
2559 }
2560 }
a45cdc79 2561 ++mark;
a0d0e21e 2562 Copy(mark,AvARRAY(av),items,SV*);
93965878 2563 AvFILLp(av) = items - 1;
d8b46c1b 2564 assert(!AvREAL(av));
b1464ded
DM
2565 if (reified) {
2566 /* transfer 'ownership' of refcnts to new @_ */
2567 AvREAL_on(av);
2568 AvREIFY_off(av);
2569 }
a0d0e21e
LW
2570 while (items--) {
2571 if (*mark)
2572 SvTEMP_off(*mark);
2573 mark++;
2574 }
2575 }
491527d0 2576 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
005a8a35 2577 Perl_get_db_sub(aTHX_ NULL, cv);
b37c2d43
AL
2578 if (PERLDB_GOTO) {
2579 CV * const gotocv = get_cv("DB::goto", FALSE);
2580 if (gotocv) {
2581 PUSHMARK( PL_stack_sp );
ad64d0ec 2582 call_sv(MUTABLE_SV(gotocv), G_SCALAR | G_NODEBUG);
b37c2d43
AL
2583 PL_stack_sp--;
2584 }
491527d0 2585 }
1ce6579f 2586 }
a0d0e21e
LW
2587 RETURNOP(CvSTART(cv));
2588 }
2589 }
1614b0e3 2590 else {
0510663f 2591 label = SvPV_nolen_const(sv);
1614b0e3 2592 if (!(do_dump || *label))
cea2e8a9 2593 DIE(aTHX_ must_have_label);
1614b0e3 2594 }
a0d0e21e 2595 }
533c011a 2596 else if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 2597 if (! do_dump)
cea2e8a9 2598 DIE(aTHX_ must_have_label);
a0d0e21e
LW
2599 }
2600 else
2601 label = cPVOP->op_pv;
2602
2603 if (label && *label) {
cbbf8932 2604 OP *gotoprobe = NULL;
3b2447bc 2605 bool leaving_eval = FALSE;
33d34e4c 2606 bool in_block = FALSE;
cbbf8932 2607 PERL_CONTEXT *last_eval_cx = NULL;
a0d0e21e
LW
2608
2609 /* find label */
2610
d4c19fe8 2611 PL_lastgotoprobe = NULL;
a0d0e21e
LW
2612 *enterops = 0;
2613 for (ix = cxstack_ix; ix >= 0; ix--) {
2614 cx = &cxstack[ix];
6b35e009 2615 switch (CxTYPE(cx)) {
a0d0e21e 2616 case CXt_EVAL:
3b2447bc 2617 leaving_eval = TRUE;
971ecbe6 2618 if (!CxTRYBLOCK(cx)) {
a4f3a277
RH
2619 gotoprobe = (last_eval_cx ?
2620 last_eval_cx->blk_eval.old_eval_root :
2621 PL_eval_root);
2622 last_eval_cx = cx;
9c5794fe
RH
2623 break;
2624 }
2625 /* else fall through */
c6fdafd0 2626 case CXt_LOOP_LAZYIV:
d01136d6 2627 case CXt_LOOP_LAZYSV:
3b719c58
NC
2628 case CXt_LOOP_FOR:
2629 case CXt_LOOP_PLAIN:
a0d0e21e
LW
2630 gotoprobe = cx->blk_oldcop->op_sibling;
2631 break;
2632 case CXt_SUBST:
2633 continue;
2634 case CXt_BLOCK:
33d34e4c 2635 if (ix) {
a0d0e21e 2636 gotoprobe = cx->blk_oldcop->op_sibling;
33d34e4c
AE
2637 in_block = TRUE;
2638 } else
3280af22 2639 gotoprobe = PL_main_root;
a0d0e21e 2640 break;
b3933176 2641 case CXt_SUB:
9850bf21 2642 if (CvDEPTH(cx->blk_sub.cv) && !CxMULTICALL(cx)) {
b3933176
CS
2643 gotoprobe = CvROOT(cx->blk_sub.cv);
2644 break;
2645 }
2646 /* FALL THROUGH */
7766f137 2647 case CXt_FORMAT:
0a753a76 2648 case CXt_NULL:
a651a37d 2649 DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
a0d0e21e
LW
2650 default:
2651 if (ix)
cea2e8a9 2652 DIE(aTHX_ "panic: goto");
3280af22 2653 gotoprobe = PL_main_root;
a0d0e21e
LW
2654 break;
2655 }
2b597662
GS
2656 if (gotoprobe) {
2657 retop = dofindlabel(gotoprobe, label,
2658 enterops, enterops + GOTO_DEPTH);
2659 if (retop)
2660 break;
2661 }
3280af22 2662 PL_lastgotoprobe = gotoprobe;
a0d0e21e
LW
2663 }
2664 if (!retop)
cea2e8a9 2665 DIE(aTHX_ "Can't find label %s", label);
a0d0e21e 2666
3b2447bc
RH
2667 /* if we're leaving an eval, check before we pop any frames
2668 that we're not going to punt, otherwise the error
2669 won't be caught */
2670
2671 if (leaving_eval && *enterops && enterops[1]) {
2672 I32 i;
2673 for (i = 1; enterops[i]; i++)
2674 if (enterops[i]->op_type == OP_ENTERITER)
2675 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
2676 }
2677
a0d0e21e
LW
2678 /* pop unwanted frames */
2679
2680 if (ix < cxstack_ix) {
2681 I32 oldsave;
2682
2683 if (ix < 0)
2684 ix = 0;
2685 dounwind(ix);
2686 TOPBLOCK(cx);
3280af22 2687 oldsave = PL_scopestack[PL_scopestack_ix];
a0d0e21e
LW
2688 LEAVE_SCOPE(oldsave);
2689 }
2690
2691 /* push wanted frames */
2692
748a9306 2693 if (*enterops && enterops[1]) {
0bd48802 2694 OP * const oldop = PL_op;
33d34e4c
AE
2695 ix = enterops[1]->op_type == OP_ENTER && in_block ? 2 : 1;
2696 for (; enterops[ix]; ix++) {
533c011a 2697 PL_op = enterops[ix];
84902520
TB
2698 /* Eventually we may want to stack the needed arguments
2699 * for each op. For now, we punt on the hard ones. */
533c011a 2700 if (PL_op->op_type == OP_ENTERITER)
894356b3 2701 DIE(aTHX_ "Can't \"goto\" into the middle of a foreach loop");
fc0dc3b3 2702 CALL_FPTR(PL_op->op_ppaddr)(aTHX);
a0d0e21e 2703 }
533c011a 2704 PL_op = oldop;
a0d0e21e
LW
2705 }
2706 }
2707
2708 if (do_dump) {
a5f75d66 2709#ifdef VMS
6b88bc9c 2710 if (!retop) retop = PL_main_start;
a5f75d66 2711#endif
3280af22
NIS
2712 PL_restartop = retop;
2713 PL_do_undump = TRUE;
a0d0e21e
LW
2714
2715 my_unexec();
2716
3280af22
NIS
2717 PL_restartop = 0; /* hmm, must be GNU unexec().. */
2718 PL_do_undump = FALSE;
a0d0e21e
LW
2719 }
2720
2721 RETURNOP(retop);
2722}
2723
2724PP(pp_exit)
2725{
97aff369 2726 dVAR;
39644a26 2727 dSP;
a0d0e21e
LW
2728 I32 anum;
2729
2730 if (MAXARG < 1)
2731 anum = 0;
ff0cee69 2732 else {
a0d0e21e 2733 anum = SvIVx(POPs);
d98f61e7
GS
2734#ifdef VMS
2735 if (anum == 1 && (PL_op->op_private & OPpEXIT_VMSISH))
ff0cee69 2736 anum = 0;
96e176bf 2737 VMSISH_HUSHED = VMSISH_HUSHED || (PL_op->op_private & OPpHUSH_VMSISH);
ff0cee69 2738#endif
2739 }
cc3604b1 2740 PL_exit_flags |= PERL_EXIT_EXPECTED;
81d86705
NC
2741#ifdef PERL_MAD
2742 /* KLUDGE: disable exit 0 in BEGIN blocks when we're just compiling */
2743 if (anum || !(PL_minus_c && PL_madskills))
2744 my_exit(anum);
2745#else
a0d0e21e 2746 my_exit(anum);
81d86705 2747#endif
3280af22 2748 PUSHs(&PL_sv_undef);
a0d0e21e
LW
2749 RETURN;
2750}
2751
a0d0e21e
LW
2752/* Eval. */
2753
0824fdcb 2754STATIC void
cea2e8a9 2755S_save_lines(pTHX_ AV *array, SV *sv)
a0d0e21e 2756{
504618e9 2757 const char *s = SvPVX_const(sv);
890ce7af 2758 const char * const send = SvPVX_const(sv) + SvCUR(sv);
504618e9 2759 I32 line = 1;
a0d0e21e 2760
7918f24d
NC
2761 PERL_ARGS_ASSERT_SAVE_LINES;
2762
a0d0e21e 2763 while (s && s < send) {
f54cb97a 2764 const char *t;
b9f83d2f 2765 SV * const tmpstr = newSV_type(SVt_PVMG);
a0d0e21e 2766
1d963ff3 2767 t = (const char *)memchr(s, '\n', send - s);
a0d0e21e
LW
2768 if (t)
2769 t++;
2770 else
2771 t = send;
2772
2773 sv_setpvn(tmpstr, s, t - s);
2774 av_store(array, line++, tmpstr);
2775 s = t;
2776 }
2777}
2778
0824fdcb 2779STATIC OP *
cea2e8a9 2780S_docatch(pTHX_ OP *o)
1e422769 2781{
97aff369 2782 dVAR;
6224f72b 2783 int ret;
06b5626a 2784 OP * const oldop = PL_op;
db36c5a1 2785 dJMPENV;
1e422769 2786
1e422769 2787#ifdef DEBUGGING
54310121 2788 assert(CATCH_GET == TRUE);
1e422769 2789#endif
312caa8e 2790 PL_op = o;
8bffa5f8 2791
14dd3ad8 2792 JMPENV_PUSH(ret);
6224f72b 2793 switch (ret) {
312caa8e 2794 case 0:
abd70938
DM
2795 assert(cxstack_ix >= 0);
2796 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2797 cxstack[cxstack_ix].blk_eval.cur_top_env = PL_top_env;
14dd3ad8 2798 redo_body:
85aaa934 2799 CALLRUNOPS(aTHX);
312caa8e
CS
2800 break;
2801 case 3:
8bffa5f8 2802 /* die caught by an inner eval - continue inner loop */
abd70938
DM
2803
2804 /* NB XXX we rely on the old popped CxEVAL still being at the top
2805 * of the stack; the way die_where() currently works, this
2806 * assumption is valid. In theory The cur_top_env value should be
2807 * returned in another global, the way retop (aka PL_restartop)
2808 * is. */
2809 assert(CxTYPE(&cxstack[cxstack_ix+1]) == CXt_EVAL);
2810
2811 if (PL_restartop
2812 && cxstack[cxstack_ix+1].blk_eval.cur_top_env == PL_top_env)
2813 {
312caa8e
CS
2814 PL_op = PL_restartop;
2815 PL_restartop = 0;
2816 goto redo_body;
2817 }
2818 /* FALL THROUGH */
2819 default:
14dd3ad8 2820 JMPENV_POP;
533c011a 2821 PL_op = oldop;
6224f72b 2822 JMPENV_JUMP(ret);
1e422769 2823 /* NOTREACHED */
1e422769 2824 }
14dd3ad8 2825 JMPENV_POP;
533c011a 2826 PL_op = oldop;
5f66b61c 2827 return NULL;
1e422769 2828}
2829
c277df42 2830OP *
bfed75c6 2831Perl_sv_compile_2op(pTHX_ SV *sv, OP** startop, const char *code, PAD** padp)
c277df42
IZ
2832/* sv Text to convert to OP tree. */
2833/* startop op_free() this to undo. */
2834/* code Short string id of the caller. */
2835{
f7997f86 2836 /* FIXME - how much of this code is common with pp_entereval? */
27da23d5 2837 dVAR; dSP; /* Make POPBLOCK work. */
c277df42
IZ
2838 PERL_CONTEXT *cx;
2839 SV **newsp;
b094c71d 2840 I32 gimme = G_VOID;
c277df42
IZ
2841 I32 optype;
2842 OP dummy;
83ee9e09
GS
2843 char tbuf[TYPE_DIGITS(long) + 12 + 10];
2844 char *tmpbuf = tbuf;
c277df42 2845 char *safestr;
a3985cdc 2846 int runtime;
601f1833 2847 CV* runcv = NULL; /* initialise to avoid compiler warnings */
f7997f86 2848 STRLEN len;
c277df42 2849
7918f24d
NC
2850 PERL_ARGS_ASSERT_SV_COMPILE_2OP;
2851
c277df42 2852 ENTER;
5486870f 2853 lex_start(sv, NULL, FALSE);
c277df42
IZ
2854 SAVETMPS;
2855 /* switch to eval mode */
2856
923e4eb5 2857 if (IN_PERL_COMPILETIME) {
f4dd75d9 2858 SAVECOPSTASH_FREE(&PL_compiling);
11faa288 2859 CopSTASH_set(&PL_compiling, PL_curstash);
cbce877f 2860 }
83ee9e09 2861 if (PERLDB_NAMEEVAL && CopLINE(PL_curcop)) {
9d4ba2ae 2862 SV * const sv = sv_newmortal();
83ee9e09
GS
2863 Perl_sv_setpvf(aTHX_ sv, "_<(%.10seval %lu)[%s:%"IVdf"]",
2864 code, (unsigned long)++PL_evalseq,
2865 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
2866 tmpbuf = SvPVX(sv);
fc009855 2867 len = SvCUR(sv);
83ee9e09
GS
2868 }
2869 else
d9fad198
JH
2870 len = my_snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
2871 (unsigned long)++PL_evalseq);
f4dd75d9 2872 SAVECOPFILE_FREE(&PL_compiling);
57843af0 2873 CopFILE_set(&PL_compiling, tmpbuf+2);
f4dd75d9 2874 SAVECOPLINE(&PL_compiling);
57843af0 2875 CopLINE_set(&PL_compiling, 1);
c277df42
IZ
2876 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2877 deleting the eval's FILEGV from the stash before gv_check() runs
2878 (i.e. before run-time proper). To work around the coredump that
2879 ensues, we always turn GvMULTI_on for any globals that were
2880 introduced within evals. See force_ident(). GSAR 96-10-12 */
f7997f86
NC
2881 safestr = savepvn(tmpbuf, len);
2882 SAVEDELETE(PL_defstash, safestr, len);
b3ac6de7 2883 SAVEHINTS();
d1ca3daa 2884#ifdef OP_IN_REGISTER
6b88bc9c 2885 PL_opsave = op;
d1ca3daa 2886#else
7766f137 2887 SAVEVPTR(PL_op);
d1ca3daa 2888#endif
c277df42 2889
a3985cdc 2890 /* we get here either during compilation, or via pp_regcomp at runtime */
923e4eb5 2891 runtime = IN_PERL_RUNTIME;
a3985cdc 2892 if (runtime)
d819b83a 2893 runcv = find_runcv(NULL);
a3985cdc 2894
533c011a 2895 PL_op = &dummy;
13b51b79 2896 PL_op->op_type = OP_ENTEREVAL;
533c011a 2897 PL_op->op_flags = 0; /* Avoid uninit warning. */
923e4eb5 2898 PUSHBLOCK(cx, CXt_EVAL|(IN_PERL_COMPILETIME ? 0 : CXp_REAL), SP);
6b75f042 2899 PUSHEVAL(cx, 0);
a3985cdc
DM
2900
2901 if (runtime)
410be5db 2902 (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
a3985cdc 2903 else
410be5db 2904 (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
13b51b79 2905 POPBLOCK(cx,PL_curpm);
e84b9f1f 2906 POPEVAL(cx);
c277df42
IZ
2907
2908 (*startop)->op_type = OP_NULL;
22c35a8c 2909 (*startop)->op_ppaddr = PL_ppaddr[OP_NULL];
c277df42 2910 lex_end();
f3548bdc 2911 /* XXX DAPM do this properly one year */
502c6561 2912 *padp = MUTABLE_AV(SvREFCNT_inc_simple(PL_comppad));
c277df42 2913 LEAVE;
923e4eb5 2914 if (IN_PERL_COMPILETIME)
623e6609 2915 CopHINTS_set(&PL_compiling, PL_hints);
d1ca3daa 2916#ifdef OP_IN_REGISTER
6b88bc9c 2917 op = PL_opsave;
d1ca3daa 2918#endif
9d4ba2ae
AL
2919 PERL_UNUSED_VAR(newsp);
2920 PERL_UNUSED_VAR(optype);
2921
410be5db 2922 return PL_eval_start;
c277df42
IZ
2923}
2924
a3985cdc
DM
2925
2926/*
2927=for apidoc find_runcv
2928
2929Locate the CV corresponding to the currently executing sub or eval.
d819b83a
DM
2930If db_seqp is non_null, skip CVs that are in the DB package and populate
2931*db_seqp with the cop sequence number at the point that the DB:: code was
2932entered. (allows debuggers to eval in the scope of the breakpoint rather
cf525c36 2933than in the scope of the debugger itself).
a3985cdc
DM
2934
2935=cut
2936*/
2937
2938CV*
d819b83a 2939Perl_find_runcv(pTHX_ U32 *db_seqp)
a3985cdc 2940{
97aff369 2941 dVAR;
a3985cdc 2942 PERL_SI *si;
a3985cdc 2943
d819b83a
DM
2944 if (db_seqp)
2945 *db_seqp = PL_curcop->cop_seq;
a3985cdc 2946 for (si = PL_curstackinfo; si; si = si->si_prev) {
06b5626a 2947 I32 ix;
a3985cdc 2948 for (ix = si->si_cxix; ix >= 0; ix--) {
06b5626a 2949 const PERL_CONTEXT *cx = &(si->si_cxstack[ix]);
d819b83a 2950 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1b6737cc 2951 CV * const cv = cx->blk_sub.cv;
d819b83a
DM
2952 /* skip DB:: code */
2953 if (db_seqp && PL_debstash && CvSTASH(cv) == PL_debstash) {
2954 *db_seqp = cx->blk_oldcop->cop_seq;
2955 continue;
2956 }
2957 return cv;
2958 }
a3985cdc
DM
2959 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
2960 return PL_compcv;
2961 }
2962 }
2963 return PL_main_cv;
2964}
2965
2966
2967/* Compile a require/do, an eval '', or a /(?{...})/.
2968 * In the last case, startop is non-null, and contains the address of
2969 * a pointer that should be set to the just-compiled code.
2970 * outside is the lexically enclosing CV (if any) that invoked us.
410be5db
DM
2971 * Returns a bool indicating whether the compile was successful; if so,
2972 * PL_eval_start contains the first op of the compiled ocde; otherwise,
2973 * pushes undef (also croaks if startop != NULL).
a3985cdc
DM
2974 */
2975
410be5db 2976STATIC bool
a3985cdc 2977S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
a0d0e21e 2978{
27da23d5 2979 dVAR; dSP;
46c461b5 2980 OP * const saveop = PL_op;
a0d0e21e 2981
6dc8a9e4
IZ
2982 PL_in_eval = ((saveop && saveop->op_type == OP_REQUIRE)
2983 ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
2984 : EVAL_INEVAL);
a0d0e21e 2985
1ce6579f 2986 PUSHMARK(SP);
2987
3280af22 2988 SAVESPTR(PL_compcv);
ea726b52 2989 PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
1aff0e91 2990 CvEVAL_on(PL_compcv);
2090ab20
JH
2991 assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
2992 cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
2993
a3985cdc 2994 CvOUTSIDE_SEQ(PL_compcv) = seq;
ea726b52 2995 CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
a3985cdc 2996
dd2155a4 2997 /* set up a scratch pad */
a0d0e21e 2998
dd2155a4 2999 CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
cecbe010 3000 PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
2c05e328 3001
07055b4c 3002
81d86705
NC
3003 if (!PL_madskills)
3004 SAVEMORTALIZESV(PL_compcv); /* must remain until end of current statement */
748a9306 3005
a0d0e21e
LW
3006 /* make sure we compile in the right package */
3007
ed094faf 3008 if (CopSTASH_ne(PL_curcop, PL_curstash)) {
3280af22 3009 SAVESPTR(PL_curstash);
ed094faf 3010 PL_curstash = CopSTASH(PL_curcop);
a0d0e21e 3011 }
3c10abe3 3012 /* XXX:ajgo do we really need to alloc an AV for begin/checkunit */
3280af22
NIS
3013 SAVESPTR(PL_beginav);
3014 PL_beginav = newAV();
3015 SAVEFREESV(PL_beginav);
3c10abe3
AG
3016 SAVESPTR(PL_unitcheckav);
3017 PL_unitcheckav = newAV();
3018 SAVEFREESV(PL_unitcheckav);
a0d0e21e 3019
81d86705 3020#ifdef PERL_MAD
9da243ce 3021 SAVEBOOL(PL_madskills);
81d86705
NC
3022 PL_madskills = 0;
3023#endif
3024
a0d0e21e
LW
3025 /* try to compile it */
3026
5f66b61c 3027 PL_eval_root = NULL;
3280af22 3028 PL_curcop = &PL_compiling;
fc15ae8f 3029 CopARYBASE_set(PL_curcop, 0);
5f66b61c 3030 if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
faef0170 3031 PL_in_eval |= EVAL_KEEPERR;
ab69dbc2
RGS
3032 else
3033 CLEAR_ERRSV();
13765c85 3034 if (yyparse() || PL_parser->error_count || !PL_eval_root) {
0c58d367 3035 SV **newsp; /* Used by POPBLOCK. */
9d4ba2ae 3036 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
c277df42 3037 I32 optype = 0; /* Might be reset by POPEVAL. */
9d4ba2ae 3038 const char *msg;
bfed75c6 3039
533c011a 3040 PL_op = saveop;
3280af22
NIS
3041 if (PL_eval_root) {
3042 op_free(PL_eval_root);
5f66b61c 3043 PL_eval_root = NULL;
a0d0e21e 3044 }
3280af22 3045 SP = PL_stack_base + POPMARK; /* pop original mark */
c277df42 3046 if (!startop) {
3280af22 3047 POPBLOCK(cx,PL_curpm);
c277df42 3048 POPEVAL(cx);
c277df42 3049 }
a0d0e21e
LW
3050 lex_end();
3051 LEAVE;
9d4ba2ae
AL
3052
3053 msg = SvPVx_nolen_const(ERRSV);
7a2e2cd6 3054 if (optype == OP_REQUIRE) {
b464bac0 3055 const SV * const nsv = cx->blk_eval.old_namesv;
504618e9 3056 (void)hv_store(GvHVn(PL_incgv), SvPVX_const(nsv), SvCUR(nsv),
27bcc0a7 3057 &PL_sv_undef, 0);
58d3fd3b
SH
3058 Perl_croak(aTHX_ "%sCompilation failed in require",
3059 *msg ? msg : "Unknown error\n");
5a844595
GS
3060 }
3061 else if (startop) {
3280af22 3062 POPBLOCK(cx,PL_curpm);
c277df42 3063 POPEVAL(cx);
5a844595
GS
3064 Perl_croak(aTHX_ "%sCompilation failed in regexp",
3065 (*msg ? msg : "Unknown error\n"));
7a2e2cd6 3066 }
9d7f88dd 3067 else {
9d7f88dd 3068 if (!*msg) {
6502358f 3069 sv_setpvs(ERRSV, "Compilation error");
9d7f88dd
SR
3070 }
3071 }
9d4ba2ae 3072 PERL_UNUSED_VAR(newsp);
410be5db
DM
3073 PUSHs(&PL_sv_undef);
3074 PUTBACK;
3075 return FALSE;
a0d0e21e 3076 }
57843af0 3077 CopLINE_set(&PL_compiling, 0);
c277df42 3078 if (startop) {
3280af22 3079 *startop = PL_eval_root;
c277df42 3080 } else
3280af22 3081 SAVEFREEOP(PL_eval_root);
0c58d367
RGS
3082
3083 /* Set the context for this new optree.
3084 * If the last op is an OP_REQUIRE, force scalar context.
3085 * Otherwise, propagate the context from the eval(). */
3086 if (PL_eval_root->op_type == OP_LEAVEEVAL
3087 && cUNOPx(PL_eval_root)->op_first->op_type == OP_LINESEQ
3088 && cLISTOPx(cUNOPx(PL_eval_root)->op_first)->op_last->op_type
3089 == OP_REQUIRE)
3090 scalar(PL_eval_root);
7df0357e 3091 else if ((gimme & G_WANT) == G_VOID)
3280af22 3092 scalarvoid(PL_eval_root);
7df0357e 3093 else if ((gimme & G_WANT) == G_ARRAY)
3280af22 3094 list(PL_eval_root);
a0d0e21e 3095 else
3280af22 3096 scalar(PL_eval_root);
a0d0e21e
LW
3097
3098 DEBUG_x(dump_eval());
3099
55497cff 3100 /* Register with debugger: */
6482a30d 3101 if (PERLDB_INTER && saveop && saveop->op_type == OP_REQUIRE) {
890ce7af 3102 CV * const cv = get_cv("DB::postponed", FALSE);
55497cff 3103 if (cv) {
3104 dSP;
924508f0 3105 PUSHMARK(SP);
ad64d0ec 3106 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
55497cff 3107 PUTBACK;
ad64d0ec 3108 call_sv(MUTABLE_SV(cv), G_DISCARD);
55497cff 3109 }
3110 }
3111
3c10abe3
AG
3112 if (PL_unitcheckav)
3113 call_list(PL_scopestack_ix, PL_unitcheckav);
3114
a0d0e21e
LW
3115 /* compiled okay, so do it */
3116
3280af22
NIS
3117 CvDEPTH(PL_compcv) = 1;
3118 SP = PL_stack_base + POPMARK; /* pop original mark */
533c011a 3119 PL_op = saveop; /* The caller may need it. */
bc177e6b 3120 PL_parser->lex_state = LEX_NOTPARSING; /* $^S needs this. */
5dc0d613 3121
410be5db
DM
3122 PUTBACK;
3123 return TRUE;
a0d0e21e
LW
3124}
3125
a6c40364 3126STATIC PerlIO *
0786552a 3127S_check_type_and_open(pTHX_ const char *name)
ce8abf5f
SP
3128{
3129 Stat_t st;
c445ea15 3130 const int st_rc = PerlLIO_stat(name, &st);
df528165 3131
7918f24d
NC
3132 PERL_ARGS_ASSERT_CHECK_TYPE_AND_OPEN;
3133
6b845e56 3134 if (st_rc < 0 || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
4608196e 3135 return NULL;
ce8abf5f
SP
3136 }
3137
0786552a 3138 return PerlIO_open(name, PERL_SCRIPT_MODE);
ce8abf5f
SP
3139}
3140
75c20bac 3141#ifndef PERL_DISABLE_PMC
ce8abf5f 3142STATIC PerlIO *
0786552a 3143S_doopen_pm(pTHX_ const char *name, const STRLEN namelen)
b295d113 3144{
b295d113
TH
3145 PerlIO *fp;
3146
7918f24d
NC
3147 PERL_ARGS_ASSERT_DOOPEN_PM;
3148
ce9440c8 3149 if (namelen > 3 && memEQs(name + namelen - 3, 3, ".pm")) {
50b8ed39
NC
3150 SV *const pmcsv = newSV(namelen + 2);
3151 char *const pmc = SvPVX(pmcsv);
a6c40364 3152 Stat_t pmcstat;
50b8ed39
NC
3153
3154 memcpy(pmc, name, namelen);
3155 pmc[namelen] = 'c';
3156 pmc[namelen + 1] = '\0';
3157
a6c40364 3158 if (PerlLIO_stat(pmc, &pmcstat) < 0) {
0786552a 3159 fp = check_type_and_open(name);
a6c40364
GS
3160 }
3161 else {
0786552a 3162 fp = check_type_and_open(pmc);
b295d113 3163 }
a6c40364
GS
3164 SvREFCNT_dec(pmcsv);
3165 }
3166 else {
0786552a 3167 fp = check_type_and_open(name);
b295d113 3168 }
b295d113 3169 return fp;
75c20bac 3170}
7925835c 3171#else
75c20bac 3172# define doopen_pm(name, namelen) check_type_and_open(name)
7925835c 3173#endif /* !PERL_DISABLE_PMC */
b295d113 3174
a0d0e21e
LW
3175PP(pp_require)
3176{
27da23d5 3177 dVAR; dSP;
c09156bb 3178 register PERL_CONTEXT *cx;
a0d0e21e 3179 SV *sv;
5c144d81 3180 const char *name;
6132ea6c 3181 STRLEN len;
4492be7a
JM
3182 char * unixname;
3183 STRLEN unixlen;
62f5ad7a 3184#ifdef VMS
4492be7a 3185 int vms_unixname = 0;
62f5ad7a 3186#endif
c445ea15
AL
3187 const char *tryname = NULL;
3188 SV *namesv = NULL;
f54cb97a 3189 const I32 gimme = GIMME_V;
bbed91b5 3190 int filter_has_file = 0;
c445ea15 3191 PerlIO *tryrsfp = NULL;
34113e50 3192 SV *filter_cache = NULL;
c445ea15
AL
3193 SV *filter_state = NULL;
3194 SV *filter_sub = NULL;
3195 SV *hook_sv = NULL;
6ec9efec
JH
3196 SV *encoding;
3197 OP *op;
a0d0e21e
LW
3198
3199 sv = POPs;
d7aa5382 3200 if ( (SvNIOKp(sv) || SvVOK(sv)) && PL_op->op_type != OP_DOFILE) {
d7aa5382
JP
3201 sv = new_version(sv);
3202 if (!sv_derived_from(PL_patchlevel, "version"))
ac0e6a2f 3203 upg_version(PL_patchlevel, TRUE);
149c1637 3204 if (cUNOP->op_first->op_type == OP_CONST && cUNOP->op_first->op_private & OPpCONST_NOVER) {
3cacfbb9 3205 if ( vcmp(sv,PL_patchlevel) <= 0 )
468aa647 3206 DIE(aTHX_ "Perls since %"SVf" too modern--this is %"SVf", stopped",
be2597df 3207 SVfARG(vnormal(sv)), SVfARG(vnormal(PL_patchlevel)));
468aa647
RGS
3208 }
3209 else {
d1029faa
JP
3210 if ( vcmp(sv,PL_patchlevel) > 0 ) {
3211 I32 first = 0;
3212 AV *lav;
3213 SV * const req = SvRV(sv);
85fbaab2 3214 SV * const pv = *hv_fetchs(MUTABLE_HV(req), "original", FALSE);
d1029faa
JP
3215
3216 /* get the left hand term */
502c6561 3217 lav = MUTABLE_AV(SvRV(*hv_fetchs(MUTABLE_HV(req), "version", FALSE)));
d1029faa
JP
3218
3219 first = SvIV(*av_fetch(lav,0,0));
3220 if ( first > (int)PERL_REVISION /* probably 'use 6.0' */
85fbaab2 3221 || hv_exists(MUTABLE_HV(req), "qv", 2 ) /* qv style */
d1029faa
JP
3222 || av_len(lav) > 1 /* FP with > 3 digits */
3223 || strstr(SvPVX(pv),".0") /* FP with leading 0 */
3224 ) {
3225 DIE(aTHX_ "Perl %"SVf" required--this is only "
3226 "%"SVf", stopped", SVfARG(vnormal(req)),
3227 SVfARG(vnormal(PL_patchlevel)));
3228 }
3229 else { /* probably 'use 5.10' or 'use 5.8' */
3230 SV * hintsv = newSV(0);
3231 I32 second = 0;
3232
3233 if (av_len(lav)>=1)
3234 second = SvIV(*av_fetch(lav,1,0));
3235
3236 second /= second >= 600 ? 100 : 10;
3237 hintsv = Perl_newSVpvf(aTHX_ "v%d.%d.%d",
3238 (int)first, (int)second,0);
3239 upg_version(hintsv, TRUE);
3240
3241 DIE(aTHX_ "Perl %"SVf" required (did you mean %"SVf"?)"
3242 "--this is only %"SVf", stopped",
3243 SVfARG(vnormal(req)),
3244 SVfARG(vnormal(hintsv)),
3245 SVfARG(vnormal(PL_patchlevel)));
3246 }
3247 }
468aa647 3248 }
d7aa5382 3249
fbc891ce
RB
3250 /* We do this only with use, not require. */
3251 if (PL_compcv &&
fbc891ce
RB
3252 /* If we request a version >= 5.9.5, load feature.pm with the
3253 * feature bundle that corresponds to the required version. */
2e8342de 3254 vcmp(sv, sv_2mortal(upg_version(newSVnv(5.009005), FALSE))) >= 0) {
7dfde25d
RGS
3255 SV *const importsv = vnormal(sv);
3256 *SvPVX_mutable(importsv) = ':';
3257 ENTER;
3258 Perl_load_module(aTHX_ 0, newSVpvs("feature"), NULL, importsv, NULL);
3259 LEAVE;
3260 }
3261
3262 RETPUSHYES;
a0d0e21e 3263 }
5c144d81 3264 name = SvPV_const(sv, len);
6132ea6c 3265 if (!(name && len > 0 && *name))
cea2e8a9 3266 DIE(aTHX_ "Null filename used");
4633a7c4 3267 TAINT_PROPER("require");
4492be7a
JM
3268
3269
3270#ifdef VMS
3271 /* The key in the %ENV hash is in the syntax of file passed as the argument
3272 * usually this is in UNIX format, but sometimes in VMS format, which
3273 * can result in a module being pulled in more than once.
3274 * To prevent this, the key must be stored in UNIX format if the VMS
3275 * name can be translated to UNIX.
3276 */
3277 if ((unixname = tounixspec(name, NULL)) != NULL) {
3278 unixlen = strlen(unixname);
3279 vms_unixname = 1;
3280 }
3281 else
3282#endif
3283 {
3284 /* if not VMS or VMS name can not be translated to UNIX, pass it
3285 * through.
3286 */
3287 unixname = (char *) name;
3288 unixlen = len;
3289 }
44f8325f 3290 if (PL_op->op_type == OP_REQUIRE) {
4492be7a
JM
3291 SV * const * const svp = hv_fetch(GvHVn(PL_incgv),
3292 unixname, unixlen, 0);
44f8325f
AL
3293 if ( svp ) {
3294 if (*svp != &PL_sv_undef)
3295 RETPUSHYES;
3296 else
087b5369
RD
3297 DIE(aTHX_ "Attempt to reload %s aborted.\n"
3298 "Compilation failed in require", unixname);
44f8325f 3299 }
4d8b06f1 3300 }
a0d0e21e
LW
3301
3302 /* prepare to compile file */
3303
be4b629d 3304 if (path_is_absolute(name)) {
46fc3d4c 3305 tryname = name;
0786552a 3306 tryrsfp = doopen_pm(name, len);
bf4acbe4 3307 }
67627c52
JH
3308#ifdef MACOS_TRADITIONAL
3309 if (!tryrsfp) {
3310 char newname[256];
3311
3312 MacPerl_CanonDir(name, newname, 1);
3313 if (path_is_absolute(newname)) {
3314 tryname = newname;
0786552a 3315 tryrsfp = doopen_pm(newname, strlen(newname));
67627c52
JH
3316 }
3317 }
3318#endif
be4b629d 3319 if (!tryrsfp) {
44f8325f 3320 AV * const ar = GvAVn(PL_incgv);
a0d0e21e 3321 I32 i;
748a9306 3322#ifdef VMS
4492be7a 3323 if (vms_unixname)
46fc3d4c 3324#endif
3325 {
d0328fd7 3326 namesv = newSV_type(SVt_PV);
46fc3d4c 3327 for (i = 0; i <= AvFILL(ar); i++) {
df528165 3328 SV * const dirsv = *av_fetch(ar, i, TRUE);
bbed91b5 3329
ad64d0ec 3330 if (SvTIED_mg((const SV *)ar, PERL_MAGIC_tied))
c38a6530 3331 mg_get(dirsv);
bbed91b5
KF
3332 if (SvROK(dirsv)) {
3333 int count;
a3b58a99 3334 SV **svp;
bbed91b5
KF
3335 SV *loader = dirsv;
3336
e14e2dc8
NC
3337 if (SvTYPE(SvRV(loader)) == SVt_PVAV
3338 && !sv_isobject(loader))
3339 {
502c6561 3340 loader = *av_fetch(MUTABLE_AV(SvRV(loader)), 0, TRUE);
bbed91b5
KF
3341 }
3342
b900a521 3343 Perl_sv_setpvf(aTHX_ namesv, "/loader/0x%"UVxf"/%s",
44f0be63 3344 PTR2UV(SvRV(dirsv)), name);
349d4f2f 3345 tryname = SvPVX_const(namesv);
c445ea15 3346 tryrsfp = NULL;
bbed91b5
KF
3347
3348 ENTER;
3349 SAVETMPS;
3350 EXTEND(SP, 2);
3351
3352 PUSHMARK(SP);
3353 PUSHs(dirsv);
3354 PUSHs(sv);
3355 PUTBACK;
e982885c
NC
3356 if (sv_isobject(loader))
3357 count = call_method("INC", G_ARRAY);
3358 else
3359 count = call_sv(loader, G_ARRAY);
bbed91b5
KF
3360 SPAGAIN;
3361
a3b58a99
RGS
3362 /* Adjust file name if the hook has set an %INC entry */
3363 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
3364 if (svp)
3365 tryname = SvPVX_const(*svp);
3366
bbed91b5
KF
3367 if (count > 0) {
3368 int i = 0;
3369 SV *arg;
3370
3371 SP -= count - 1;
3372 arg = SP[i++];
3373
34113e50
NC
3374 if (SvROK(arg) && (SvTYPE(SvRV(arg)) <= SVt_PVLV)
3375 && !isGV_with_GP(SvRV(arg))) {
3376 filter_cache = SvRV(arg);
74c765eb 3377 SvREFCNT_inc_simple_void_NN(filter_cache);
34113e50
NC
3378
3379 if (i < count) {
3380 arg = SP[i++];
3381 }
3382 }
3383
6e592b3a 3384 if (SvROK(arg) && isGV_with_GP(SvRV(arg))) {
bbed91b5
KF
3385 arg = SvRV(arg);
3386 }
3387
6e592b3a 3388 if (isGV_with_GP(arg)) {
159b6efe 3389 IO * const io = GvIO((const GV *)arg);
bbed91b5
KF
3390
3391 ++filter_has_file;
3392
3393 if (io) {
3394 tryrsfp = IoIFP(io);
0f7de14d
NC
3395 if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
3396 PerlIO_close(IoOFP(io));
bbed91b5 3397 }
0f7de14d
NC
3398 IoIFP(io) = NULL;
3399<