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