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