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