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