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