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