This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Bytecode? Back into service.
[perl5.git] / pp_hot.c
CommitLineData
a0d0e21e
LW
1/* pp_hot.c
2 *
4c79ee7a 3 * Copyright (c) 1991-2003, Larry Wall
a0d0e21e
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
10/*
11 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
12 * shaking the air.
13 *
14 * Awake! Awake! Fear, Fire, Foes! Awake!
15 * Fire, Foes! Awake!
16 */
17
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_PP_HOT_C
a0d0e21e
LW
20#include "perl.h"
21
22/* Hot code. */
23
24PP(pp_const)
25{
39644a26 26 dSP;
1d7c1841 27 XPUSHs(cSVOP_sv);
a0d0e21e
LW
28 RETURN;
29}
30
31PP(pp_nextstate)
32{
533c011a 33 PL_curcop = (COP*)PL_op;
a0d0e21e 34 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 35 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e
LW
36 FREETMPS;
37 return NORMAL;
38}
39
40PP(pp_gvsv)
41{
39644a26 42 dSP;
924508f0 43 EXTEND(SP,1);
533c011a 44 if (PL_op->op_private & OPpLVAL_INTRO)
1d7c1841 45 PUSHs(save_scalar(cGVOP_gv));
a0d0e21e 46 else
1d7c1841 47 PUSHs(GvSV(cGVOP_gv));
a0d0e21e
LW
48 RETURN;
49}
50
51PP(pp_null)
52{
53 return NORMAL;
54}
55
7399586d
HS
56PP(pp_setstate)
57{
58 PL_curcop = (COP*)PL_op;
59 return NORMAL;
60}
61
a0d0e21e
LW
62PP(pp_pushmark)
63{
3280af22 64 PUSHMARK(PL_stack_sp);
a0d0e21e
LW
65 return NORMAL;
66}
67
68PP(pp_stringify)
69{
39644a26 70 dSP; dTARGET;
6050d10e 71 sv_copypv(TARG,TOPs);
a0d0e21e
LW
72 SETTARG;
73 RETURN;
74}
75
76PP(pp_gv)
77{
39644a26 78 dSP;
1d7c1841 79 XPUSHs((SV*)cGVOP_gv);
a0d0e21e
LW
80 RETURN;
81}
82
83PP(pp_and)
84{
39644a26 85 dSP;
a0d0e21e
LW
86 if (!SvTRUE(TOPs))
87 RETURN;
88 else {
89 --SP;
90 RETURNOP(cLOGOP->op_other);
91 }
92}
93
94PP(pp_sassign)
95{
39644a26 96 dSP; dPOPTOPssrl;
748a9306 97
533c011a 98 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
a0d0e21e
LW
99 SV *temp;
100 temp = left; left = right; right = temp;
101 }
3280af22 102 if (PL_tainting && PL_tainted && !SvTAINTED(left))
a0d0e21e 103 TAINT_NOT;
54310121 104 SvSetMagicSV(right, left);
a0d0e21e
LW
105 SETs(right);
106 RETURN;
107}
108
109PP(pp_cond_expr)
110{
39644a26 111 dSP;
a0d0e21e 112 if (SvTRUEx(POPs))
1a67a97c 113 RETURNOP(cLOGOP->op_other);
a0d0e21e 114 else
1a67a97c 115 RETURNOP(cLOGOP->op_next);
a0d0e21e
LW
116}
117
118PP(pp_unstack)
119{
120 I32 oldsave;
121 TAINT_NOT; /* Each statement is presumed innocent */
3280af22 122 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
a0d0e21e 123 FREETMPS;
3280af22 124 oldsave = PL_scopestack[PL_scopestack_ix - 1];
a0d0e21e
LW
125 LEAVE_SCOPE(oldsave);
126 return NORMAL;
127}
128
a0d0e21e
LW
129PP(pp_concat)
130{
39644a26 131 dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
748a9306
LW
132 {
133 dPOPTOPssrl;
8d6d96c1
HS
134 STRLEN llen;
135 char* lpv;
136 bool lbyte;
137 STRLEN rlen;
138 char* rpv = SvPV(right, rlen); /* mg_get(right) happens here */
db79b45b 139 bool rbyte = !SvUTF8(right), rcopied = FALSE;
8d6d96c1
HS
140
141 if (TARG == right && right != left) {
142 right = sv_2mortal(newSVpvn(rpv, rlen));
143 rpv = SvPV(right, rlen); /* no point setting UTF8 here */
db79b45b 144 rcopied = TRUE;
8d6d96c1 145 }
7889fe52 146
8d6d96c1
HS
147 if (TARG != left) {
148 lpv = SvPV(left, llen); /* mg_get(left) may happen here */
149 lbyte = !SvUTF8(left);
150 sv_setpvn(TARG, lpv, llen);
151 if (!lbyte)
152 SvUTF8_on(TARG);
153 else
154 SvUTF8_off(TARG);
155 }
156 else { /* TARG == left */
157 if (SvGMAGICAL(left))
158 mg_get(left); /* or mg_get(left) may happen here */
159 if (!SvOK(TARG))
160 sv_setpv(left, "");
161 lpv = SvPV_nomg(left, llen);
162 lbyte = !SvUTF8(left);
163 }
a12c0f56 164
fd15e783
PN
165#if defined(PERL_Y2KWARN)
166 if ((SvIOK(right) || SvNOK(right)) && ckWARN(WARN_Y2K) && SvOK(TARG)) {
8d6d96c1
HS
167 if (llen >= 2 && lpv[llen - 2] == '1' && lpv[llen - 1] == '9'
168 && (llen == 2 || !isDIGIT(lpv[llen - 3])))
169 {
9014280d 170 Perl_warner(aTHX_ packWARN(WARN_Y2K), "Possible Y2K bug: %s",
8d6d96c1
HS
171 "about to append an integer to '19'");
172 }
fd15e783
PN
173 }
174#endif
175
8d6d96c1
HS
176 if (lbyte != rbyte) {
177 if (lbyte)
178 sv_utf8_upgrade_nomg(TARG);
179 else {
db79b45b
JH
180 if (!rcopied)
181 right = sv_2mortal(newSVpvn(rpv, rlen));
8d6d96c1
HS
182 sv_utf8_upgrade_nomg(right);
183 rpv = SvPV(right, rlen);
69b47968 184 }
a0d0e21e 185 }
8d6d96c1 186 sv_catpvn_nomg(TARG, rpv, rlen);
43ebc500 187
a0d0e21e
LW
188 SETTARG;
189 RETURN;
748a9306 190 }
a0d0e21e
LW
191}
192
193PP(pp_padsv)
194{
39644a26 195 dSP; dTARGET;
a0d0e21e 196 XPUSHs(TARG);
533c011a
NIS
197 if (PL_op->op_flags & OPf_MOD) {
198 if (PL_op->op_private & OPpLVAL_INTRO)
dd2155a4 199 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
533c011a 200 else if (PL_op->op_private & OPpDEREF) {
8ec5e241 201 PUTBACK;
dd2155a4 202 vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
8ec5e241
NIS
203 SPAGAIN;
204 }
4633a7c4 205 }
a0d0e21e
LW
206 RETURN;
207}
208
209PP(pp_readline)
210{
f5284f61 211 tryAMAGICunTARGET(iter, 0);
3280af22 212 PL_last_in_gv = (GV*)(*PL_stack_sp--);
8efb3254 213 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
1c846c1f 214 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
f5284f61 215 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
8efb3254 216 else {
f5284f61
IZ
217 dSP;
218 XPUSHs((SV*)PL_last_in_gv);
219 PUTBACK;
cea2e8a9 220 pp_rv2gv();
f5284f61 221 PL_last_in_gv = (GV*)(*PL_stack_sp--);
f5284f61
IZ
222 }
223 }
a0d0e21e
LW
224 return do_readline();
225}
226
227PP(pp_eq)
228{
39644a26 229 dSP; tryAMAGICbinSET(eq,0);
4c9fe80f
AS
230#ifndef NV_PRESERVES_UV
231 if (SvROK(TOPs) && SvROK(TOPm1s)) {
e61d22ef
NC
232 SP--;
233 SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s)));
4c9fe80f
AS
234 RETURN;
235 }
236#endif
28e5dec8
JH
237#ifdef PERL_PRESERVE_IVUV
238 SvIV_please(TOPs);
239 if (SvIOK(TOPs)) {
4c9fe80f
AS
240 /* Unless the left argument is integer in range we are going
241 to have to use NV maths. Hence only attempt to coerce the
242 right argument if we know the left is integer. */
28e5dec8
JH
243 SvIV_please(TOPm1s);
244 if (SvIOK(TOPm1s)) {
245 bool auvok = SvUOK(TOPm1s);
246 bool buvok = SvUOK(TOPs);
a12c0f56 247
1605159e
NC
248 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
249 /* Casting IV to UV before comparison isn't going to matter
250 on 2s complement. On 1s complement or sign&magnitude
251 (if we have any of them) it could to make negative zero
252 differ from normal zero. As I understand it. (Need to
253 check - is negative zero implementation defined behaviour
254 anyway?). NWC */
255 UV buv = SvUVX(POPs);
256 UV auv = SvUVX(TOPs);
28e5dec8 257
28e5dec8
JH
258 SETs(boolSV(auv == buv));
259 RETURN;
260 }
261 { /* ## Mixed IV,UV ## */
1605159e 262 SV *ivp, *uvp;
28e5dec8 263 IV iv;
28e5dec8 264
1605159e 265 /* == is commutative so doesn't matter which is left or right */
28e5dec8 266 if (auvok) {
1605159e
NC
267 /* top of stack (b) is the iv */
268 ivp = *SP;
269 uvp = *--SP;
270 } else {
271 uvp = *SP;
272 ivp = *--SP;
273 }
274 iv = SvIVX(ivp);
275 if (iv < 0) {
276 /* As uv is a UV, it's >0, so it cannot be == */
277 SETs(&PL_sv_no);
278 RETURN;
279 }
28e5dec8 280 /* we know iv is >= 0 */
1605159e 281 SETs(boolSV((UV)iv == SvUVX(uvp)));
28e5dec8
JH
282 RETURN;
283 }
284 }
285 }
286#endif
a0d0e21e
LW
287 {
288 dPOPnv;
54310121 289 SETs(boolSV(TOPn == value));
a0d0e21e
LW
290 RETURN;
291 }
292}
293
294PP(pp_preinc)
295{
39644a26 296 dSP;
3510b4a1 297 if (SvTYPE(TOPs) > SVt_PVLV)
d470f89e 298 DIE(aTHX_ PL_no_modify);
3510b4a1
NC
299 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
300 && SvIVX(TOPs) != IV_MAX)
55497cff 301 {
748a9306 302 ++SvIVX(TOPs);
55497cff 303 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306 304 }
28e5dec8 305 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
748a9306 306 sv_inc(TOPs);
a0d0e21e
LW
307 SvSETMAGIC(TOPs);
308 return NORMAL;
309}
310
311PP(pp_or)
312{
39644a26 313 dSP;
a0d0e21e
LW
314 if (SvTRUE(TOPs))
315 RETURN;
316 else {
317 --SP;
318 RETURNOP(cLOGOP->op_other);
319 }
320}
321
c963b151
BD
322PP(pp_dor)
323{
324 /* Most of this is lifted straight from pp_defined */
325 dSP;
326 register SV* sv;
327
328 sv = TOPs;
329 if (!sv || !SvANY(sv)) {
330 --SP;
331 RETURNOP(cLOGOP->op_other);
332 }
333
334 switch (SvTYPE(sv)) {
335 case SVt_PVAV:
336 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
337 RETURN;
338 break;
339 case SVt_PVHV:
340 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
341 RETURN;
342 break;
343 case SVt_PVCV:
344 if (CvROOT(sv) || CvXSUB(sv))
345 RETURN;
346 break;
347 default:
348 if (SvGMAGICAL(sv))
349 mg_get(sv);
350 if (SvOK(sv))
351 RETURN;
352 }
353
354 --SP;
355 RETURNOP(cLOGOP->op_other);
356}
357
a0d0e21e
LW
358PP(pp_add)
359{
39644a26 360 dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
28e5dec8
JH
361 useleft = USE_LEFT(TOPm1s);
362#ifdef PERL_PRESERVE_IVUV
363 /* We must see if we can perform the addition with integers if possible,
364 as the integer code detects overflow while the NV code doesn't.
365 If either argument hasn't had a numeric conversion yet attempt to get
366 the IV. It's important to do this now, rather than just assuming that
367 it's not IOK as a PV of "9223372036854775806" may not take well to NV
368 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
369 integer in case the second argument is IV=9223372036854775806
370 We can (now) rely on sv_2iv to do the right thing, only setting the
371 public IOK flag if the value in the NV (or PV) slot is truly integer.
372
373 A side effect is that this also aggressively prefers integer maths over
7dca457a
NC
374 fp maths for integer values.
375
a00b5bd3 376 How to detect overflow?
7dca457a
NC
377
378 C 99 section 6.2.6.1 says
379
380 The range of nonnegative values of a signed integer type is a subrange
381 of the corresponding unsigned integer type, and the representation of
382 the same value in each type is the same. A computation involving
383 unsigned operands can never overflow, because a result that cannot be
384 represented by the resulting unsigned integer type is reduced modulo
385 the number that is one greater than the largest value that can be
386 represented by the resulting type.
387
388 (the 9th paragraph)
389
390 which I read as "unsigned ints wrap."
391
392 signed integer overflow seems to be classed as "exception condition"
393
394 If an exceptional condition occurs during the evaluation of an
395 expression (that is, if the result is not mathematically defined or not
396 in the range of representable values for its type), the behavior is
397 undefined.
398
399 (6.5, the 5th paragraph)
400
401 I had assumed that on 2s complement machines signed arithmetic would
402 wrap, hence coded pp_add and pp_subtract on the assumption that
403 everything perl builds on would be happy. After much wailing and
404 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
405 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
406 unsigned code below is actually shorter than the old code. :-)
407 */
408
28e5dec8
JH
409 SvIV_please(TOPs);
410 if (SvIOK(TOPs)) {
411 /* Unless the left argument is integer in range we are going to have to
412 use NV maths. Hence only attempt to coerce the right argument if
413 we know the left is integer. */
9c5ffd7c
JH
414 register UV auv = 0;
415 bool auvok = FALSE;
7dca457a
NC
416 bool a_valid = 0;
417
28e5dec8 418 if (!useleft) {
7dca457a
NC
419 auv = 0;
420 a_valid = auvok = 1;
421 /* left operand is undef, treat as zero. + 0 is identity,
422 Could SETi or SETu right now, but space optimise by not adding
423 lots of code to speed up what is probably a rarish case. */
424 } else {
425 /* Left operand is defined, so is it IV? */
426 SvIV_please(TOPm1s);
427 if (SvIOK(TOPm1s)) {
428 if ((auvok = SvUOK(TOPm1s)))
429 auv = SvUVX(TOPm1s);
430 else {
431 register IV aiv = SvIVX(TOPm1s);
432 if (aiv >= 0) {
433 auv = aiv;
434 auvok = 1; /* Now acting as a sign flag. */
435 } else { /* 2s complement assumption for IV_MIN */
436 auv = (UV)-aiv;
437 }
438 }
439 a_valid = 1;
28e5dec8
JH
440 }
441 }
7dca457a
NC
442 if (a_valid) {
443 bool result_good = 0;
444 UV result;
445 register UV buv;
28e5dec8 446 bool buvok = SvUOK(TOPs);
a00b5bd3 447
7dca457a
NC
448 if (buvok)
449 buv = SvUVX(TOPs);
450 else {
451 register IV biv = SvIVX(TOPs);
452 if (biv >= 0) {
453 buv = biv;
454 buvok = 1;
455 } else
456 buv = (UV)-biv;
457 }
458 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
602f51c4 459 else "IV" now, independent of how it came in.
7dca457a
NC
460 if a, b represents positive, A, B negative, a maps to -A etc
461 a + b => (a + b)
462 A + b => -(a - b)
463 a + B => (a - b)
464 A + B => -(a + b)
465 all UV maths. negate result if A negative.
466 add if signs same, subtract if signs differ. */
467
468 if (auvok ^ buvok) {
469 /* Signs differ. */
470 if (auv >= buv) {
471 result = auv - buv;
472 /* Must get smaller */
473 if (result <= auv)
474 result_good = 1;
475 } else {
476 result = buv - auv;
477 if (result <= buv) {
478 /* result really should be -(auv-buv). as its negation
479 of true value, need to swap our result flag */
480 auvok = !auvok;
481 result_good = 1;
28e5dec8
JH
482 }
483 }
7dca457a
NC
484 } else {
485 /* Signs same */
486 result = auv + buv;
487 if (result >= auv)
488 result_good = 1;
489 }
490 if (result_good) {
491 SP--;
492 if (auvok)
28e5dec8 493 SETu( result );
7dca457a
NC
494 else {
495 /* Negate result */
496 if (result <= (UV)IV_MIN)
497 SETi( -(IV)result );
498 else {
499 /* result valid, but out of range for IV. */
500 SETn( -(NV)result );
28e5dec8
JH
501 }
502 }
7dca457a
NC
503 RETURN;
504 } /* Overflow, drop through to NVs. */
28e5dec8
JH
505 }
506 }
507#endif
a0d0e21e 508 {
28e5dec8
JH
509 dPOPnv;
510 if (!useleft) {
511 /* left operand is undef, treat as zero. + 0.0 is identity. */
512 SETn(value);
513 RETURN;
514 }
515 SETn( value + TOPn );
516 RETURN;
a0d0e21e
LW
517 }
518}
519
520PP(pp_aelemfast)
521{
39644a26 522 dSP;
1d7c1841 523 AV *av = GvAV(cGVOP_gv);
533c011a
NIS
524 U32 lval = PL_op->op_flags & OPf_MOD;
525 SV** svp = av_fetch(av, PL_op->op_private, lval);
3280af22 526 SV *sv = (svp ? *svp : &PL_sv_undef);
6ff81951 527 EXTEND(SP, 1);
be6c24e0
GS
528 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
529 sv = sv_mortalcopy(sv);
530 PUSHs(sv);
a0d0e21e
LW
531 RETURN;
532}
533
534PP(pp_join)
535{
39644a26 536 dSP; dMARK; dTARGET;
a0d0e21e
LW
537 MARK++;
538 do_join(TARG, *MARK, MARK, SP);
539 SP = MARK;
540 SETs(TARG);
541 RETURN;
542}
543
544PP(pp_pushre)
545{
39644a26 546 dSP;
44a8e56a
PP
547#ifdef DEBUGGING
548 /*
549 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
550 * will be enough to hold an OP*.
551 */
552 SV* sv = sv_newmortal();
553 sv_upgrade(sv, SVt_PVLV);
554 LvTYPE(sv) = '/';
533c011a 555 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
44a8e56a
PP
556 XPUSHs(sv);
557#else
6b88bc9c 558 XPUSHs((SV*)PL_op);
44a8e56a 559#endif
a0d0e21e
LW
560 RETURN;
561}
562
563/* Oversized hot code. */
564
565PP(pp_print)
566{
39644a26 567 dSP; dMARK; dORIGMARK;
a0d0e21e
LW
568 GV *gv;
569 IO *io;
760ac839 570 register PerlIO *fp;
236988e4 571 MAGIC *mg;
a0d0e21e 572
533c011a 573 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
574 gv = (GV*)*++MARK;
575 else
3280af22 576 gv = PL_defoutgv;
5b468f54
AMS
577
578 if (gv && (io = GvIO(gv))
579 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
580 {
01bb7c6d 581 had_magic:
68dc0745 582 if (MARK == ORIGMARK) {
1c846c1f 583 /* If using default handle then we need to make space to
a60c0954
NIS
584 * pass object as 1st arg, so move other args up ...
585 */
4352c267 586 MEXTEND(SP, 1);
68dc0745
PP
587 ++MARK;
588 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
589 ++SP;
590 }
591 PUSHMARK(MARK - 1);
5b468f54 592 *MARK = SvTIED_obj((SV*)io, mg);
68dc0745 593 PUTBACK;
236988e4 594 ENTER;
864dbfa3 595 call_method("PRINT", G_SCALAR);
236988e4
PP
596 LEAVE;
597 SPAGAIN;
68dc0745
PP
598 MARK = ORIGMARK + 1;
599 *MARK = *SP;
600 SP = MARK;
236988e4
PP
601 RETURN;
602 }
a0d0e21e 603 if (!(io = GvIO(gv))) {
5b468f54
AMS
604 if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv)))
605 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
01bb7c6d 606 goto had_magic;
2dd78f96
JH
607 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
608 report_evil_fh(gv, io, PL_op->op_type);
93189314 609 SETERRNO(EBADF,RMS_IFI);
a0d0e21e
LW
610 goto just_say_no;
611 }
612 else if (!(fp = IoOFP(io))) {
599cee73 613 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
4c80c0b2
NC
614 if (IoIFP(io))
615 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
2dd78f96 616 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
bc37a18f 617 report_evil_fh(gv, io, PL_op->op_type);
a0d0e21e 618 }
93189314 619 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
a0d0e21e
LW
620 goto just_say_no;
621 }
622 else {
623 MARK++;
7889fe52 624 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
a0d0e21e
LW
625 while (MARK <= SP) {
626 if (!do_print(*MARK, fp))
627 break;
628 MARK++;
629 if (MARK <= SP) {
7889fe52 630 if (!do_print(PL_ofs_sv, fp)) { /* $, */
a0d0e21e
LW
631 MARK--;
632 break;
633 }
634 }
635 }
636 }
637 else {
638 while (MARK <= SP) {
639 if (!do_print(*MARK, fp))
640 break;
641 MARK++;
642 }
643 }
644 if (MARK <= SP)
645 goto just_say_no;
646 else {
7889fe52
NIS
647 if (PL_ors_sv && SvOK(PL_ors_sv))
648 if (!do_print(PL_ors_sv, fp)) /* $\ */
a0d0e21e
LW
649 goto just_say_no;
650
651 if (IoFLAGS(io) & IOf_FLUSH)
760ac839 652 if (PerlIO_flush(fp) == EOF)
a0d0e21e
LW
653 goto just_say_no;
654 }
655 }
656 SP = ORIGMARK;
3280af22 657 PUSHs(&PL_sv_yes);
a0d0e21e
LW
658 RETURN;
659
660 just_say_no:
661 SP = ORIGMARK;
3280af22 662 PUSHs(&PL_sv_undef);
a0d0e21e
LW
663 RETURN;
664}
665
666PP(pp_rv2av)
667{
39644a26 668 dSP; dTOPss;
a0d0e21e
LW
669 AV *av;
670
671 if (SvROK(sv)) {
672 wasref:
f5284f61
IZ
673 tryAMAGICunDEREF(to_av);
674
a0d0e21e
LW
675 av = (AV*)SvRV(sv);
676 if (SvTYPE(av) != SVt_PVAV)
cea2e8a9 677 DIE(aTHX_ "Not an ARRAY reference");
533c011a 678 if (PL_op->op_flags & OPf_REF) {
f5284f61 679 SETs((SV*)av);
a0d0e21e
LW
680 RETURN;
681 }
78f9721b
SM
682 else if (LVRET) {
683 if (GIMME == G_SCALAR)
684 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
685 SETs((SV*)av);
686 RETURN;
687 }
82d03984
RGS
688 else if (PL_op->op_flags & OPf_MOD
689 && PL_op->op_private & OPpLVAL_INTRO)
690 Perl_croak(aTHX_ PL_no_localize_ref);
a0d0e21e
LW
691 }
692 else {
693 if (SvTYPE(sv) == SVt_PVAV) {
694 av = (AV*)sv;
533c011a 695 if (PL_op->op_flags & OPf_REF) {
f5284f61 696 SETs((SV*)av);
a0d0e21e
LW
697 RETURN;
698 }
78f9721b
SM
699 else if (LVRET) {
700 if (GIMME == G_SCALAR)
701 Perl_croak(aTHX_ "Can't return array to lvalue"
702 " scalar context");
703 SETs((SV*)av);
704 RETURN;
705 }
a0d0e21e
LW
706 }
707 else {
67955e0c 708 GV *gv;
1c846c1f 709
a0d0e21e 710 if (SvTYPE(sv) != SVt_PVGV) {
748a9306 711 char *sym;
c9d5ac95 712 STRLEN len;
748a9306 713
a0d0e21e
LW
714 if (SvGMAGICAL(sv)) {
715 mg_get(sv);
716 if (SvROK(sv))
717 goto wasref;
718 }
719 if (!SvOK(sv)) {
533c011a
NIS
720 if (PL_op->op_flags & OPf_REF ||
721 PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 722 DIE(aTHX_ PL_no_usym, "an ARRAY");
599cee73 723 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 724 report_uninit();
f5284f61 725 if (GIMME == G_ARRAY) {
c2444246 726 (void)POPs;
4633a7c4 727 RETURN;
f5284f61
IZ
728 }
729 RETSETUNDEF;
a0d0e21e 730 }
c9d5ac95 731 sym = SvPV(sv,len);
35cd451c
GS
732 if ((PL_op->op_flags & OPf_SPECIAL) &&
733 !(PL_op->op_flags & OPf_MOD))
734 {
735 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVAV);
c9d5ac95
GS
736 if (!gv
737 && (!is_gv_magical(sym,len,0)
738 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV))))
739 {
35cd451c 740 RETSETUNDEF;
c9d5ac95 741 }
35cd451c
GS
742 }
743 else {
744 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 745 DIE(aTHX_ PL_no_symref, sym, "an ARRAY");
35cd451c
GS
746 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
747 }
748 }
749 else {
67955e0c 750 gv = (GV*)sv;
a0d0e21e 751 }
67955e0c 752 av = GvAVn(gv);
533c011a 753 if (PL_op->op_private & OPpLVAL_INTRO)
67955e0c 754 av = save_ary(gv);
533c011a 755 if (PL_op->op_flags & OPf_REF) {
f5284f61 756 SETs((SV*)av);
a0d0e21e
LW
757 RETURN;
758 }
78f9721b
SM
759 else if (LVRET) {
760 if (GIMME == G_SCALAR)
761 Perl_croak(aTHX_ "Can't return array to lvalue"
762 " scalar context");
763 SETs((SV*)av);
764 RETURN;
765 }
a0d0e21e
LW
766 }
767 }
768
769 if (GIMME == G_ARRAY) {
770 I32 maxarg = AvFILL(av) + 1;
c2444246 771 (void)POPs; /* XXXX May be optimized away? */
1c846c1f 772 EXTEND(SP, maxarg);
93965878 773 if (SvRMAGICAL(av)) {
1c846c1f 774 U32 i;
eb160463 775 for (i=0; i < (U32)maxarg; i++) {
93965878 776 SV **svp = av_fetch(av, i, FALSE);
3280af22 777 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
93965878 778 }
1c846c1f 779 }
93965878
NIS
780 else {
781 Copy(AvARRAY(av), SP+1, maxarg, SV*);
782 }
a0d0e21e
LW
783 SP += maxarg;
784 }
c754c3d7 785 else if (GIMME_V == G_SCALAR) {
a0d0e21e
LW
786 dTARGET;
787 I32 maxarg = AvFILL(av) + 1;
f5284f61 788 SETi(maxarg);
a0d0e21e
LW
789 }
790 RETURN;
791}
792
793PP(pp_rv2hv)
794{
39644a26 795 dSP; dTOPss;
a0d0e21e
LW
796 HV *hv;
797
798 if (SvROK(sv)) {
799 wasref:
f5284f61
IZ
800 tryAMAGICunDEREF(to_hv);
801
a0d0e21e 802 hv = (HV*)SvRV(sv);
6d822dc4 803 if (SvTYPE(hv) != SVt_PVHV)
cea2e8a9 804 DIE(aTHX_ "Not a HASH reference");
533c011a 805 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
806 SETs((SV*)hv);
807 RETURN;
808 }
78f9721b
SM
809 else if (LVRET) {
810 if (GIMME == G_SCALAR)
811 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
812 SETs((SV*)hv);
813 RETURN;
814 }
82d03984
RGS
815 else if (PL_op->op_flags & OPf_MOD
816 && PL_op->op_private & OPpLVAL_INTRO)
817 Perl_croak(aTHX_ PL_no_localize_ref);
a0d0e21e
LW
818 }
819 else {
6d822dc4 820 if (SvTYPE(sv) == SVt_PVHV) {
a0d0e21e 821 hv = (HV*)sv;
533c011a 822 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
823 SETs((SV*)hv);
824 RETURN;
825 }
78f9721b
SM
826 else if (LVRET) {
827 if (GIMME == G_SCALAR)
828 Perl_croak(aTHX_ "Can't return hash to lvalue"
829 " scalar context");
830 SETs((SV*)hv);
831 RETURN;
832 }
a0d0e21e
LW
833 }
834 else {
67955e0c 835 GV *gv;
1c846c1f 836
a0d0e21e 837 if (SvTYPE(sv) != SVt_PVGV) {
748a9306 838 char *sym;
c9d5ac95 839 STRLEN len;
748a9306 840
a0d0e21e
LW
841 if (SvGMAGICAL(sv)) {
842 mg_get(sv);
843 if (SvROK(sv))
844 goto wasref;
845 }
846 if (!SvOK(sv)) {
533c011a
NIS
847 if (PL_op->op_flags & OPf_REF ||
848 PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 849 DIE(aTHX_ PL_no_usym, "a HASH");
599cee73 850 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 851 report_uninit();
4633a7c4
LW
852 if (GIMME == G_ARRAY) {
853 SP--;
854 RETURN;
855 }
a0d0e21e
LW
856 RETSETUNDEF;
857 }
c9d5ac95 858 sym = SvPV(sv,len);
35cd451c
GS
859 if ((PL_op->op_flags & OPf_SPECIAL) &&
860 !(PL_op->op_flags & OPf_MOD))
861 {
862 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVHV);
c9d5ac95
GS
863 if (!gv
864 && (!is_gv_magical(sym,len,0)
865 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV))))
866 {
35cd451c 867 RETSETUNDEF;
c9d5ac95 868 }
35cd451c
GS
869 }
870 else {
871 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 872 DIE(aTHX_ PL_no_symref, sym, "a HASH");
35cd451c
GS
873 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
874 }
875 }
876 else {
67955e0c 877 gv = (GV*)sv;
a0d0e21e 878 }
67955e0c 879 hv = GvHVn(gv);
533c011a 880 if (PL_op->op_private & OPpLVAL_INTRO)
67955e0c 881 hv = save_hash(gv);
533c011a 882 if (PL_op->op_flags & OPf_REF) {
a0d0e21e
LW
883 SETs((SV*)hv);
884 RETURN;
885 }
78f9721b
SM
886 else if (LVRET) {
887 if (GIMME == G_SCALAR)
888 Perl_croak(aTHX_ "Can't return hash to lvalue"
889 " scalar context");
890 SETs((SV*)hv);
891 RETURN;
892 }
a0d0e21e
LW
893 }
894 }
895
896 if (GIMME == G_ARRAY) { /* array wanted */
3280af22 897 *PL_stack_sp = (SV*)hv;
cea2e8a9 898 return do_kv();
a0d0e21e
LW
899 }
900 else {
901 dTARGET;
b9c39e73 902 if (HvFILL(hv))
57def98f
JH
903 Perl_sv_setpvf(aTHX_ TARG, "%"IVdf"/%"IVdf,
904 (IV)HvFILL(hv), (IV)HvMAX(hv) + 1);
a0d0e21e
LW
905 else
906 sv_setiv(TARG, 0);
c750a3ec 907
a0d0e21e
LW
908 SETTARG;
909 RETURN;
910 }
911}
912
10c8fecd
GS
913STATIC void
914S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
915{
916 if (*relem) {
917 SV *tmpstr;
6d822dc4
MS
918 HE *didstore;
919
920 if (ckWARN(WARN_MISC)) {
10c8fecd
GS
921 if (relem == firstrelem &&
922 SvROK(*relem) &&
923 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
924 SvTYPE(SvRV(*relem)) == SVt_PVHV))
925 {
9014280d 926 Perl_warner(aTHX_ packWARN(WARN_MISC),
10c8fecd
GS
927 "Reference found where even-sized list expected");
928 }
929 else
9014280d 930 Perl_warner(aTHX_ packWARN(WARN_MISC),
10c8fecd
GS
931 "Odd number of elements in hash assignment");
932 }
6d822dc4
MS
933
934 tmpstr = NEWSV(29,0);
935 didstore = hv_store_ent(hash,*relem,tmpstr,0);
936 if (SvMAGICAL(hash)) {
937 if (SvSMAGICAL(tmpstr))
938 mg_set(tmpstr);
939 if (!didstore)
940 sv_2mortal(tmpstr);
941 }
942 TAINT_NOT;
10c8fecd
GS
943 }
944}
945
a0d0e21e
LW
946PP(pp_aassign)
947{
39644a26 948 dSP;
3280af22
NIS
949 SV **lastlelem = PL_stack_sp;
950 SV **lastrelem = PL_stack_base + POPMARK;
951 SV **firstrelem = PL_stack_base + POPMARK + 1;
a0d0e21e
LW
952 SV **firstlelem = lastrelem + 1;
953
954 register SV **relem;
955 register SV **lelem;
956
957 register SV *sv;
958 register AV *ary;
959
54310121 960 I32 gimme;
a0d0e21e
LW
961 HV *hash;
962 I32 i;
963 int magic;
964
3280af22 965 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
a0d0e21e
LW
966
967 /* If there's a common identifier on both sides we have to take
968 * special care that assigning the identifier on the left doesn't
969 * clobber a value on the right that's used later in the list.
970 */
10c8fecd 971 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
cc5e57d2 972 EXTEND_MORTAL(lastrelem - firstrelem + 1);
10c8fecd
GS
973 for (relem = firstrelem; relem <= lastrelem; relem++) {
974 /*SUPPRESS 560*/
155aba94 975 if ((sv = *relem)) {
a1f49e72 976 TAINT_NOT; /* Each item is independent */
10c8fecd 977 *relem = sv_mortalcopy(sv);
a1f49e72 978 }
10c8fecd 979 }
a0d0e21e
LW
980 }
981
982 relem = firstrelem;
983 lelem = firstlelem;
984 ary = Null(AV*);
985 hash = Null(HV*);
10c8fecd 986
a0d0e21e 987 while (lelem <= lastlelem) {
bbce6d69 988 TAINT_NOT; /* Each item stands on its own, taintwise. */
a0d0e21e
LW
989 sv = *lelem++;
990 switch (SvTYPE(sv)) {
991 case SVt_PVAV:
992 ary = (AV*)sv;
748a9306 993 magic = SvMAGICAL(ary) != 0;
a0d0e21e 994 av_clear(ary);
7e42bd57 995 av_extend(ary, lastrelem - relem);
a0d0e21e
LW
996 i = 0;
997 while (relem <= lastrelem) { /* gobble up all the rest */
5117ca91 998 SV **didstore;
a0d0e21e
LW
999 sv = NEWSV(28,0);
1000 assert(*relem);
1001 sv_setsv(sv,*relem);
1002 *(relem++) = sv;
5117ca91
GS
1003 didstore = av_store(ary,i++,sv);
1004 if (magic) {
fb73857a
PP
1005 if (SvSMAGICAL(sv))
1006 mg_set(sv);
5117ca91 1007 if (!didstore)
8127e0e3 1008 sv_2mortal(sv);
5117ca91 1009 }
bbce6d69 1010 TAINT_NOT;
a0d0e21e
LW
1011 }
1012 break;
10c8fecd 1013 case SVt_PVHV: { /* normal hash */
a0d0e21e
LW
1014 SV *tmpstr;
1015
1016 hash = (HV*)sv;
748a9306 1017 magic = SvMAGICAL(hash) != 0;
a0d0e21e
LW
1018 hv_clear(hash);
1019
1020 while (relem < lastrelem) { /* gobble up all the rest */
5117ca91 1021 HE *didstore;
4633a7c4 1022 if (*relem)
a0d0e21e 1023 sv = *(relem++);
4633a7c4 1024 else
3280af22 1025 sv = &PL_sv_no, relem++;
a0d0e21e
LW
1026 tmpstr = NEWSV(29,0);
1027 if (*relem)
1028 sv_setsv(tmpstr,*relem); /* value */
1029 *(relem++) = tmpstr;
5117ca91
GS
1030 didstore = hv_store_ent(hash,sv,tmpstr,0);
1031 if (magic) {
fb73857a
PP
1032 if (SvSMAGICAL(tmpstr))
1033 mg_set(tmpstr);
5117ca91 1034 if (!didstore)
8127e0e3 1035 sv_2mortal(tmpstr);
5117ca91 1036 }
bbce6d69 1037 TAINT_NOT;
8e07c86e 1038 }
6a0deba8 1039 if (relem == lastrelem) {
10c8fecd 1040 do_oddball(hash, relem, firstrelem);
6a0deba8 1041 relem++;
1930e939 1042 }
a0d0e21e
LW
1043 }
1044 break;
1045 default:
6fc92669
GS
1046 if (SvIMMORTAL(sv)) {
1047 if (relem <= lastrelem)
1048 relem++;
1049 break;
a0d0e21e
LW
1050 }
1051 if (relem <= lastrelem) {
1052 sv_setsv(sv, *relem);
1053 *(relem++) = sv;
1054 }
1055 else
3280af22 1056 sv_setsv(sv, &PL_sv_undef);
a0d0e21e
LW
1057 SvSETMAGIC(sv);
1058 break;
1059 }
1060 }
3280af22
NIS
1061 if (PL_delaymagic & ~DM_DELAY) {
1062 if (PL_delaymagic & DM_UID) {
a0d0e21e 1063#ifdef HAS_SETRESUID
b28d0864 1064 (void)setresuid(PL_uid,PL_euid,(Uid_t)-1);
56febc5e
AD
1065#else
1066# ifdef HAS_SETREUID
3280af22 1067 (void)setreuid(PL_uid,PL_euid);
56febc5e
AD
1068# else
1069# ifdef HAS_SETRUID
b28d0864
NIS
1070 if ((PL_delaymagic & DM_UID) == DM_RUID) {
1071 (void)setruid(PL_uid);
1072 PL_delaymagic &= ~DM_RUID;
a0d0e21e 1073 }
56febc5e
AD
1074# endif /* HAS_SETRUID */
1075# ifdef HAS_SETEUID
b28d0864
NIS
1076 if ((PL_delaymagic & DM_UID) == DM_EUID) {
1077 (void)seteuid(PL_uid);
1078 PL_delaymagic &= ~DM_EUID;
a0d0e21e 1079 }
56febc5e 1080# endif /* HAS_SETEUID */
b28d0864
NIS
1081 if (PL_delaymagic & DM_UID) {
1082 if (PL_uid != PL_euid)
cea2e8a9 1083 DIE(aTHX_ "No setreuid available");
b28d0864 1084 (void)PerlProc_setuid(PL_uid);
a0d0e21e 1085 }
56febc5e
AD
1086# endif /* HAS_SETREUID */
1087#endif /* HAS_SETRESUID */
d8eceb89
JH
1088 PL_uid = PerlProc_getuid();
1089 PL_euid = PerlProc_geteuid();
a0d0e21e 1090 }
3280af22 1091 if (PL_delaymagic & DM_GID) {
a0d0e21e 1092#ifdef HAS_SETRESGID
b28d0864 1093 (void)setresgid(PL_gid,PL_egid,(Gid_t)-1);
56febc5e
AD
1094#else
1095# ifdef HAS_SETREGID
3280af22 1096 (void)setregid(PL_gid,PL_egid);
56febc5e
AD
1097# else
1098# ifdef HAS_SETRGID
b28d0864
NIS
1099 if ((PL_delaymagic & DM_GID) == DM_RGID) {
1100 (void)setrgid(PL_gid);
1101 PL_delaymagic &= ~DM_RGID;
a0d0e21e 1102 }
56febc5e
AD
1103# endif /* HAS_SETRGID */
1104# ifdef HAS_SETEGID
b28d0864
NIS
1105 if ((PL_delaymagic & DM_GID) == DM_EGID) {
1106 (void)setegid(PL_gid);
1107 PL_delaymagic &= ~DM_EGID;
a0d0e21e 1108 }
56febc5e 1109# endif /* HAS_SETEGID */
b28d0864
NIS
1110 if (PL_delaymagic & DM_GID) {
1111 if (PL_gid != PL_egid)
cea2e8a9 1112 DIE(aTHX_ "No setregid available");
b28d0864 1113 (void)PerlProc_setgid(PL_gid);
a0d0e21e 1114 }
56febc5e
AD
1115# endif /* HAS_SETREGID */
1116#endif /* HAS_SETRESGID */
d8eceb89
JH
1117 PL_gid = PerlProc_getgid();
1118 PL_egid = PerlProc_getegid();
a0d0e21e 1119 }
3280af22 1120 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
a0d0e21e 1121 }
3280af22 1122 PL_delaymagic = 0;
54310121
PP
1123
1124 gimme = GIMME_V;
1125 if (gimme == G_VOID)
1126 SP = firstrelem - 1;
1127 else if (gimme == G_SCALAR) {
1128 dTARGET;
1129 SP = firstrelem;
1130 SETi(lastrelem - firstrelem + 1);
1131 }
1132 else {
a0d0e21e
LW
1133 if (ary || hash)
1134 SP = lastrelem;
1135 else
1136 SP = firstrelem + (lastlelem - firstlelem);
0c8c7a05 1137 lelem = firstlelem + (relem - firstrelem);
5f05dabc 1138 while (relem <= SP)
3280af22 1139 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
a0d0e21e 1140 }
54310121 1141 RETURN;
a0d0e21e
LW
1142}
1143
8782bef2
GB
1144PP(pp_qr)
1145{
39644a26 1146 dSP;
8782bef2
GB
1147 register PMOP *pm = cPMOP;
1148 SV *rv = sv_newmortal();
57668c4d 1149 SV *sv = newSVrv(rv, "Regexp");
e08e52cf
AMS
1150 if (pm->op_pmdynflags & PMdf_TAINTED)
1151 SvTAINTED_on(rv);
aaa362c4 1152 sv_magic(sv,(SV*)ReREFCNT_inc(PM_GETRE(pm)), PERL_MAGIC_qr,0,0);
8782bef2
GB
1153 RETURNX(PUSHs(rv));
1154}
1155
a0d0e21e
LW
1156PP(pp_match)
1157{
39644a26 1158 dSP; dTARG;
a0d0e21e 1159 register PMOP *pm = cPMOP;
d65afb4b 1160 PMOP *dynpm = pm;
a0d0e21e
LW
1161 register char *t;
1162 register char *s;
1163 char *strend;
1164 I32 global;
f722798b
IZ
1165 I32 r_flags = REXEC_CHECKED;
1166 char *truebase; /* Start of string */
aaa362c4 1167 register REGEXP *rx = PM_GETRE(pm);
b3eb6a9b 1168 bool rxtainted;
a0d0e21e
LW
1169 I32 gimme = GIMME;
1170 STRLEN len;
748a9306 1171 I32 minmatch = 0;
3280af22 1172 I32 oldsave = PL_savestack_ix;
f86702cc 1173 I32 update_minmatch = 1;
e60df1fa 1174 I32 had_zerolen = 0;
a0d0e21e 1175
533c011a 1176 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1177 TARG = POPs;
1178 else {
54b9620d 1179 TARG = DEFSV;
a0d0e21e
LW
1180 EXTEND(SP,1);
1181 }
d9f424b2 1182
c277df42 1183 PUTBACK; /* EVAL blocks need stack_sp. */
a0d0e21e
LW
1184 s = SvPV(TARG, len);
1185 strend = s + len;
1186 if (!s)
2269b42e 1187 DIE(aTHX_ "panic: pp_match");
b3eb6a9b 1188 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22 1189 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
9212bbba 1190 TAINT_NOT;
a0d0e21e 1191
a30b2f1f 1192 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
d9f424b2 1193
d65afb4b 1194 /* PMdf_USED is set after a ?? matches once */
48c036b1 1195 if (pm->op_pmdynflags & PMdf_USED) {
c277df42 1196 failure:
a0d0e21e
LW
1197 if (gimme == G_ARRAY)
1198 RETURN;
1199 RETPUSHNO;
1200 }
1201
d65afb4b 1202 /* empty pattern special-cased to use last successful pattern if possible */
3280af22
NIS
1203 if (!rx->prelen && PL_curpm) {
1204 pm = PL_curpm;
aaa362c4 1205 rx = PM_GETRE(pm);
a0d0e21e 1206 }
d65afb4b 1207
eb160463 1208 if (rx->minlen > (I32)len)
d65afb4b 1209 goto failure;
c277df42 1210
a0d0e21e 1211 truebase = t = s;
ad94a511
IZ
1212
1213 /* XXXX What part of this is needed with true \G-support? */
d65afb4b 1214 if ((global = dynpm->op_pmflags & PMf_GLOBAL)) {
cf93c79d 1215 rx->startp[0] = -1;
a0d0e21e 1216 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
14befaf4 1217 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
565764a8 1218 if (mg && mg->mg_len >= 0) {
b7a35066 1219 if (!(rx->reganch & ROPT_GPOS_SEEN))
1c846c1f 1220 rx->endp[0] = rx->startp[0] = mg->mg_len;
0ef3e39e
HS
1221 else if (rx->reganch & ROPT_ANCH_GPOS) {
1222 r_flags |= REXEC_IGNOREPOS;
1c846c1f 1223 rx->endp[0] = rx->startp[0] = mg->mg_len;
0ef3e39e 1224 }
748a9306 1225 minmatch = (mg->mg_flags & MGf_MINMATCH);
f86702cc 1226 update_minmatch = 0;
748a9306 1227 }
a0d0e21e
LW
1228 }
1229 }
14977893
JH
1230 if ((!global && rx->nparens)
1231 || SvTEMP(TARG) || PL_sawampersand)
1232 r_flags |= REXEC_COPY_STR;
1c846c1f 1233 if (SvSCREAM(TARG))
22e551b9
IZ
1234 r_flags |= REXEC_SCREAM;
1235
a0d0e21e 1236 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1237 SAVEINT(PL_multiline);
1238 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1239 }
1240
1241play_it_again:
cf93c79d
IZ
1242 if (global && rx->startp[0] != -1) {
1243 t = s = rx->endp[0] + truebase;
d9f97599 1244 if ((s + rx->minlen) > strend)
a0d0e21e 1245 goto nope;
f86702cc 1246 if (update_minmatch++)
e60df1fa 1247 minmatch = had_zerolen;
a0d0e21e 1248 }
60aeb6fd
NIS
1249 if (rx->reganch & RE_USE_INTUIT &&
1250 DO_UTF8(TARG) == ((rx->reganch & ROPT_UTF8) != 0)) {
ee0b7718 1251 PL_bostr = truebase;
f722798b
IZ
1252 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1253
1254 if (!s)
1255 goto nope;
1256 if ( (rx->reganch & ROPT_CHECK_ALL)
14977893 1257 && !PL_sawampersand
f722798b
IZ
1258 && ((rx->reganch & ROPT_NOSCAN)
1259 || !((rx->reganch & RE_INTUIT_TAIL)
05b4157f
GS
1260 && (r_flags & REXEC_SCREAM)))
1261 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
f722798b 1262 goto yup;
a0d0e21e 1263 }
cea2e8a9 1264 if (CALLREGEXEC(aTHX_ rx, s, strend, truebase, minmatch, TARG, NULL, r_flags))
bbce6d69 1265 {
3280af22 1266 PL_curpm = pm;
d65afb4b
HS
1267 if (dynpm->op_pmflags & PMf_ONCE)
1268 dynpm->op_pmdynflags |= PMdf_USED;
a0d0e21e
LW
1269 goto gotcha;
1270 }
1271 else
1272 goto ret_no;
1273 /*NOTREACHED*/
1274
1275 gotcha:
72311751
GS
1276 if (rxtainted)
1277 RX_MATCH_TAINTED_on(rx);
1278 TAINT_IF(RX_MATCH_TAINTED(rx));
a0d0e21e 1279 if (gimme == G_ARRAY) {
ffc61ed2 1280 I32 nparens, i, len;
a0d0e21e 1281
ffc61ed2
JH
1282 nparens = rx->nparens;
1283 if (global && !nparens)
a0d0e21e
LW
1284 i = 1;
1285 else
1286 i = 0;
c277df42 1287 SPAGAIN; /* EVAL blocks could move the stack. */
ffc61ed2
JH
1288 EXTEND(SP, nparens + i);
1289 EXTEND_MORTAL(nparens + i);
1290 for (i = !i; i <= nparens; i++) {
a0d0e21e
LW
1291 PUSHs(sv_newmortal());
1292 /*SUPPRESS 560*/
cf93c79d
IZ
1293 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1294 len = rx->endp[i] - rx->startp[i];
290deeac
A
1295 if (rx->endp[i] < 0 || rx->startp[i] < 0 ||
1296 len < 0 || len > strend - s)
1297 DIE(aTHX_ "panic: pp_match start/end pointers");
cf93c79d 1298 s = rx->startp[i] + truebase;
a0d0e21e 1299 sv_setpvn(*SP, s, len);
cce850e4 1300 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
a197cbdd 1301 SvUTF8_on(*SP);
a0d0e21e
LW
1302 }
1303 }
1304 if (global) {
d65afb4b 1305 if (dynpm->op_pmflags & PMf_CONTINUE) {
0af80b60
HS
1306 MAGIC* mg = 0;
1307 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1308 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1309 if (!mg) {
1310 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1311 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1312 }
1313 if (rx->startp[0] != -1) {
1314 mg->mg_len = rx->endp[0];
1315 if (rx->startp[0] == rx->endp[0])
1316 mg->mg_flags |= MGf_MINMATCH;
1317 else
1318 mg->mg_flags &= ~MGf_MINMATCH;
1319 }
1320 }
cf93c79d
IZ
1321 had_zerolen = (rx->startp[0] != -1
1322 && rx->startp[0] == rx->endp[0]);
c277df42 1323 PUTBACK; /* EVAL blocks may use stack */
cf93c79d 1324 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
1325 goto play_it_again;
1326 }
ffc61ed2 1327 else if (!nparens)
bde848c5 1328 XPUSHs(&PL_sv_yes);
4633a7c4 1329 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1330 RETURN;
1331 }
1332 else {
1333 if (global) {
1334 MAGIC* mg = 0;
1335 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
14befaf4 1336 mg = mg_find(TARG, PERL_MAGIC_regex_global);
a0d0e21e 1337 if (!mg) {
14befaf4
DM
1338 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1339 mg = mg_find(TARG, PERL_MAGIC_regex_global);
a0d0e21e 1340 }
cf93c79d
IZ
1341 if (rx->startp[0] != -1) {
1342 mg->mg_len = rx->endp[0];
d9f97599 1343 if (rx->startp[0] == rx->endp[0])
748a9306
LW
1344 mg->mg_flags |= MGf_MINMATCH;
1345 else
1346 mg->mg_flags &= ~MGf_MINMATCH;
1347 }
a0d0e21e 1348 }
4633a7c4 1349 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1350 RETPUSHYES;
1351 }
1352
f722798b 1353yup: /* Confirmed by INTUIT */
72311751
GS
1354 if (rxtainted)
1355 RX_MATCH_TAINTED_on(rx);
1356 TAINT_IF(RX_MATCH_TAINTED(rx));
3280af22 1357 PL_curpm = pm;
d65afb4b
HS
1358 if (dynpm->op_pmflags & PMf_ONCE)
1359 dynpm->op_pmdynflags |= PMdf_USED;
cf93c79d
IZ
1360 if (RX_MATCH_COPIED(rx))
1361 Safefree(rx->subbeg);
1362 RX_MATCH_COPIED_off(rx);
1363 rx->subbeg = Nullch;
a0d0e21e 1364 if (global) {
d9f97599 1365 rx->subbeg = truebase;
cf93c79d 1366 rx->startp[0] = s - truebase;
a30b2f1f 1367 if (RX_MATCH_UTF8(rx)) {
60aeb6fd
NIS
1368 char *t = (char*)utf8_hop((U8*)s, rx->minlen);
1369 rx->endp[0] = t - truebase;
1370 }
1371 else {
1372 rx->endp[0] = s - truebase + rx->minlen;
1373 }
cf93c79d 1374 rx->sublen = strend - truebase;
a0d0e21e 1375 goto gotcha;
1c846c1f 1376 }
14977893
JH
1377 if (PL_sawampersand) {
1378 I32 off;
ed252734
NC
1379#ifdef PERL_COPY_ON_WRITE
1380 if (SvIsCOW(TARG) || (SvFLAGS(TARG) & CAN_COW_MASK) == CAN_COW_FLAGS) {
1381 if (DEBUG_C_TEST) {
1382 PerlIO_printf(Perl_debug_log,
1383 "Copy on write: pp_match $& capture, type %d, truebase=%p, t=%p, difference %d\n",
1384 (int) SvTYPE(TARG), truebase, t,
1385 (int)(t-truebase));
1386 }
1387 rx->saved_copy = sv_setsv_cow(rx->saved_copy, TARG);
1388 rx->subbeg = SvPVX(rx->saved_copy) + (t - truebase);
1389 assert (SvPOKp(rx->saved_copy));
1390 } else
1391#endif
1392 {
14977893 1393
ed252734
NC
1394 rx->subbeg = savepvn(t, strend - t);
1395#ifdef PERL_COPY_ON_WRITE
1396 rx->saved_copy = Nullsv;
1397#endif
1398 }
14977893
JH
1399 rx->sublen = strend - t;
1400 RX_MATCH_COPIED_on(rx);
1401 off = rx->startp[0] = s - t;
1402 rx->endp[0] = off + rx->minlen;
1403 }
1404 else { /* startp/endp are used by @- @+. */
1405 rx->startp[0] = s - truebase;
1406 rx->endp[0] = s - truebase + rx->minlen;
1407 }
fc19f8d0 1408 rx->nparens = rx->lastparen = 0; /* used by @- and @+ */
4633a7c4 1409 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1410 RETPUSHYES;
1411
1412nope:
a0d0e21e 1413ret_no:
d65afb4b 1414 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
a0d0e21e 1415 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
14befaf4 1416 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
a0d0e21e 1417 if (mg)
565764a8 1418 mg->mg_len = -1;
a0d0e21e
LW
1419 }
1420 }
4633a7c4 1421 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
1422 if (gimme == G_ARRAY)
1423 RETURN;
1424 RETPUSHNO;
1425}
1426
1427OP *
864dbfa3 1428Perl_do_readline(pTHX)
a0d0e21e
LW
1429{
1430 dSP; dTARGETSTACKED;
1431 register SV *sv;
1432 STRLEN tmplen = 0;
1433 STRLEN offset;
760ac839 1434 PerlIO *fp;
3280af22 1435 register IO *io = GvIO(PL_last_in_gv);
533c011a 1436 register I32 type = PL_op->op_type;
54310121 1437 I32 gimme = GIMME_V;
e79b0511 1438 MAGIC *mg;
a0d0e21e 1439
5b468f54 1440 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
e79b0511 1441 PUSHMARK(SP);
5b468f54 1442 XPUSHs(SvTIED_obj((SV*)io, mg));
e79b0511
PP
1443 PUTBACK;
1444 ENTER;
864dbfa3 1445 call_method("READLINE", gimme);
e79b0511
PP
1446 LEAVE;
1447 SPAGAIN;
0b7c7b4f
HS
1448 if (gimme == G_SCALAR) {
1449 SV* result = POPs;
1450 SvSetSV_nosteal(TARG, result);
1451 PUSHTARG;
1452 }
e79b0511
PP
1453 RETURN;
1454 }
a0d0e21e
LW
1455 fp = Nullfp;
1456 if (io) {
1457 fp = IoIFP(io);
1458 if (!fp) {
1459 if (IoFLAGS(io) & IOf_ARGV) {
1460 if (IoFLAGS(io) & IOf_START) {
a0d0e21e 1461 IoLINES(io) = 0;
3280af22 1462 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1d7c1841 1463 IoFLAGS(io) &= ~IOf_START;
9d116dd7 1464 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
3280af22
NIS
1465 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1466 SvSETMAGIC(GvSV(PL_last_in_gv));
a2008d6d
GS
1467 fp = IoIFP(io);
1468 goto have_fp;
a0d0e21e
LW
1469 }
1470 }
3280af22 1471 fp = nextargv(PL_last_in_gv);
a0d0e21e 1472 if (!fp) { /* Note: fp != IoIFP(io) */
3280af22 1473 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
a0d0e21e
LW
1474 }
1475 }
0d44d22b
NC
1476 else if (type == OP_GLOB)
1477 fp = Perl_start_glob(aTHX_ POPs, io);
a0d0e21e
LW
1478 }
1479 else if (type == OP_GLOB)
1480 SP--;
a00b5bd3 1481 else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) {
4c80c0b2 1482 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
a00b5bd3 1483 }
a0d0e21e
LW
1484 }
1485 if (!fp) {
790090df
HS
1486 if (ckWARN2(WARN_GLOB, WARN_CLOSED)
1487 && (!io || !(IoFLAGS(io) & IOf_START))) {
3f4520fe 1488 if (type == OP_GLOB)
9014280d 1489 Perl_warner(aTHX_ packWARN(WARN_GLOB),
af8c498a
GS
1490 "glob failed (can't start child: %s)",
1491 Strerror(errno));
69282e91 1492 else
bc37a18f 1493 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
3f4520fe 1494 }
54310121 1495 if (gimme == G_SCALAR) {
79628082
SM
1496 /* undef TARG, and push that undefined value */
1497 SV_CHECK_THINKFIRST_COW_DROP(TARG);
a0d0e21e
LW
1498 (void)SvOK_off(TARG);
1499 PUSHTARG;
1500 }
1501 RETURN;
1502 }
a2008d6d 1503 have_fp:
54310121 1504 if (gimme == G_SCALAR) {
a0d0e21e 1505 sv = TARG;
9607fc9c
PP
1506 if (SvROK(sv))
1507 sv_unref(sv);
a0d0e21e
LW
1508 (void)SvUPGRADE(sv, SVt_PV);
1509 tmplen = SvLEN(sv); /* remember if already alloced */
1510 if (!tmplen)
1511 Sv_Grow(sv, 80); /* try short-buffering it */
2b5e58c4
AMS
1512 offset = 0;
1513 if (type == OP_RCATLINE && SvOK(sv)) {
1514 if (!SvPOK(sv)) {
1515 STRLEN n_a;
1516 (void)SvPV_force(sv, n_a);
1517 }
a0d0e21e 1518 offset = SvCUR(sv);
2b5e58c4 1519 }
a0d0e21e 1520 }
54310121
PP
1521 else {
1522 sv = sv_2mortal(NEWSV(57, 80));
1523 offset = 0;
1524 }
fbad3eb5 1525
3887d568
AP
1526 /* This should not be marked tainted if the fp is marked clean */
1527#define MAYBE_TAINT_LINE(io, sv) \
1528 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1529 TAINT; \
1530 SvTAINTED_on(sv); \
1531 }
1532
684bef36 1533/* delay EOF state for a snarfed empty file */
fbad3eb5 1534#define SNARF_EOF(gimme,rs,io,sv) \
684bef36 1535 (gimme != G_SCALAR || SvCUR(sv) \
b9fee9ba 1536 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
fbad3eb5 1537
a0d0e21e 1538 for (;;) {
09e8efcc 1539 PUTBACK;
fbad3eb5
GS
1540 if (!sv_gets(sv, fp, offset)
1541 && (type == OP_GLOB || SNARF_EOF(gimme, PL_rs, io, sv)))
1542 {
760ac839 1543 PerlIO_clearerr(fp);
a0d0e21e 1544 if (IoFLAGS(io) & IOf_ARGV) {
3280af22 1545 fp = nextargv(PL_last_in_gv);
a0d0e21e
LW
1546 if (fp)
1547 continue;
3280af22 1548 (void)do_close(PL_last_in_gv, FALSE);
a0d0e21e
LW
1549 }
1550 else if (type == OP_GLOB) {
e476b1b5 1551 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
9014280d 1552 Perl_warner(aTHX_ packWARN(WARN_GLOB),
4eb79ab5 1553 "glob failed (child exited with status %d%s)",
894356b3 1554 (int)(STATUS_CURRENT >> 8),
cf494569 1555 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
4eb79ab5 1556 }
a0d0e21e 1557 }
54310121 1558 if (gimme == G_SCALAR) {
79628082 1559 SV_CHECK_THINKFIRST_COW_DROP(TARG);
a0d0e21e 1560 (void)SvOK_off(TARG);
09e8efcc 1561 SPAGAIN;
a0d0e21e
LW
1562 PUSHTARG;
1563 }
3887d568 1564 MAYBE_TAINT_LINE(io, sv);
a0d0e21e
LW
1565 RETURN;
1566 }
3887d568 1567 MAYBE_TAINT_LINE(io, sv);
a0d0e21e 1568 IoLINES(io)++;
b9fee9ba 1569 IoFLAGS(io) |= IOf_NOLINE;
71be2cbc 1570 SvSETMAGIC(sv);
09e8efcc 1571 SPAGAIN;
a0d0e21e 1572 XPUSHs(sv);
a0d0e21e
LW
1573 if (type == OP_GLOB) {
1574 char *tmps;
1575
3280af22 1576 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
c07a80fd 1577 tmps = SvEND(sv) - 1;
3280af22 1578 if (*tmps == *SvPVX(PL_rs)) {
c07a80fd
PP
1579 *tmps = '\0';
1580 SvCUR(sv)--;
1581 }
1582 }
a0d0e21e
LW
1583 for (tmps = SvPVX(sv); *tmps; tmps++)
1584 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1585 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1586 break;
43384a1a 1587 if (*tmps && PerlLIO_lstat(SvPVX(sv), &PL_statbuf) < 0) {
a0d0e21e
LW
1588 (void)POPs; /* Unmatched wildcard? Chuck it... */
1589 continue;
1590 }
1591 }
54310121 1592 if (gimme == G_ARRAY) {
a0d0e21e
LW
1593 if (SvLEN(sv) - SvCUR(sv) > 20) {
1594 SvLEN_set(sv, SvCUR(sv)+1);
1595 Renew(SvPVX(sv), SvLEN(sv), char);
1596 }
1597 sv = sv_2mortal(NEWSV(58, 80));
1598 continue;
1599 }
54310121 1600 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
a0d0e21e
LW
1601 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1602 if (SvCUR(sv) < 60)
1603 SvLEN_set(sv, 80);
1604 else
1605 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1606 Renew(SvPVX(sv), SvLEN(sv), char);
1607 }
1608 RETURN;
1609 }
1610}
1611
1612PP(pp_enter)
1613{
39644a26 1614 dSP;
c09156bb 1615 register PERL_CONTEXT *cx;
533c011a 1616 I32 gimme = OP_GIMME(PL_op, -1);
a0d0e21e 1617
54310121
PP
1618 if (gimme == -1) {
1619 if (cxstack_ix >= 0)
1620 gimme = cxstack[cxstack_ix].blk_gimme;
1621 else
1622 gimme = G_SCALAR;
1623 }
a0d0e21e
LW
1624
1625 ENTER;
1626
1627 SAVETMPS;
924508f0 1628 PUSHBLOCK(cx, CXt_BLOCK, SP);
a0d0e21e
LW
1629
1630 RETURN;
1631}
1632
1633PP(pp_helem)
1634{
39644a26 1635 dSP;
760ac839 1636 HE* he;
ae77835f 1637 SV **svp;
a0d0e21e 1638 SV *keysv = POPs;
a0d0e21e 1639 HV *hv = (HV*)POPs;
78f9721b 1640 U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
533c011a 1641 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
be6c24e0 1642 SV *sv;
765f542d
NC
1643#ifdef PERL_COPY_ON_WRITE
1644 U32 hash = (SvIsCOW_shared_hash(keysv)) ? SvUVX(keysv) : 0;
1645#else
1c846c1f 1646 U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
765f542d 1647#endif
9c5ffd7c 1648 I32 preeminent = 0;
a0d0e21e 1649
ae77835f 1650 if (SvTYPE(hv) == SVt_PVHV) {
8d1f198f
DM
1651 if (PL_op->op_private & OPpLVAL_INTRO) {
1652 MAGIC *mg;
1653 HV *stash;
1654 /* does the element we're localizing already exist? */
c39e6ab0 1655 preeminent =
8d1f198f
DM
1656 /* can we determine whether it exists? */
1657 ( !SvRMAGICAL(hv)
1658 || mg_find((SV*)hv, PERL_MAGIC_env)
1659 || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied))
1660 /* Try to preserve the existenceness of a tied hash
1661 * element by using EXISTS and DELETE if possible.
1662 * Fallback to FETCH and STORE otherwise */
1663 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
1664 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
1665 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
1666 )
1667 ) ? hv_exists_ent(hv, keysv, 0) : 1;
c39e6ab0 1668
8d1f198f 1669 }
1c846c1f 1670 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
97fcbf96 1671 svp = he ? &HeVAL(he) : 0;
ae77835f 1672 }
c750a3ec 1673 else {
a0d0e21e 1674 RETPUSHUNDEF;
c750a3ec 1675 }
a0d0e21e 1676 if (lval) {
3280af22 1677 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
1678 SV* lv;
1679 SV* key2;
2d8e6c8d
GS
1680 if (!defer) {
1681 STRLEN n_a;
cea2e8a9 1682 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2d8e6c8d 1683 }
68dc0745
PP
1684 lv = sv_newmortal();
1685 sv_upgrade(lv, SVt_PVLV);
1686 LvTYPE(lv) = 'y';
14befaf4 1687 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, Nullch, 0);
68dc0745
PP
1688 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1689 LvTARG(lv) = SvREFCNT_inc(hv);
1690 LvTARGLEN(lv) = 1;
1691 PUSHs(lv);
1692 RETURN;
1693 }
533c011a 1694 if (PL_op->op_private & OPpLVAL_INTRO) {
ae77835f 1695 if (HvNAME(hv) && isGV(*svp))
533c011a 1696 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1f5346dc
SC
1697 else {
1698 if (!preeminent) {
1699 STRLEN keylen;
1700 char *key = SvPV(keysv, keylen);
57813020 1701 SAVEDELETE(hv, savepvn(key,keylen), keylen);
bfc4de9f 1702 } else
1f5346dc
SC
1703 save_helem(hv, keysv, svp);
1704 }
5f05dabc 1705 }
533c011a
NIS
1706 else if (PL_op->op_private & OPpDEREF)
1707 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 1708 }
3280af22 1709 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
1710 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1711 * Pushing the magical RHS on to the stack is useless, since
1712 * that magic is soon destined to be misled by the local(),
1713 * and thus the later pp_sassign() will fail to mg_get() the
1714 * old value. This should also cure problems with delayed
1715 * mg_get()s. GSAR 98-07-03 */
1716 if (!lval && SvGMAGICAL(sv))
1717 sv = sv_mortalcopy(sv);
1718 PUSHs(sv);
a0d0e21e
LW
1719 RETURN;
1720}
1721
1722PP(pp_leave)
1723{
39644a26 1724 dSP;
c09156bb 1725 register PERL_CONTEXT *cx;
a0d0e21e
LW
1726 register SV **mark;
1727 SV **newsp;
1728 PMOP *newpm;
1729 I32 gimme;
1730
533c011a 1731 if (PL_op->op_flags & OPf_SPECIAL) {
a0d0e21e 1732 cx = &cxstack[cxstack_ix];
3280af22 1733 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
a0d0e21e
LW
1734 }
1735
1736 POPBLOCK(cx,newpm);
1737
533c011a 1738 gimme = OP_GIMME(PL_op, -1);
54310121
PP
1739 if (gimme == -1) {
1740 if (cxstack_ix >= 0)
1741 gimme = cxstack[cxstack_ix].blk_gimme;
1742 else
1743 gimme = G_SCALAR;
1744 }
a0d0e21e 1745
a1f49e72 1746 TAINT_NOT;
54310121
PP
1747 if (gimme == G_VOID)
1748 SP = newsp;
1749 else if (gimme == G_SCALAR) {
1750 MARK = newsp + 1;
09256e2f 1751 if (MARK <= SP) {
54310121
PP
1752 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1753 *MARK = TOPs;
1754 else
1755 *MARK = sv_mortalcopy(TOPs);
09256e2f 1756 } else {
54310121 1757 MEXTEND(mark,0);
3280af22 1758 *MARK = &PL_sv_undef;
a0d0e21e 1759 }
54310121 1760 SP = MARK;
a0d0e21e 1761 }
54310121 1762 else if (gimme == G_ARRAY) {
a1f49e72
CS
1763 /* in case LEAVE wipes old return values */
1764 for (mark = newsp + 1; mark <= SP; mark++) {
1765 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
a0d0e21e 1766 *mark = sv_mortalcopy(*mark);
a1f49e72
CS
1767 TAINT_NOT; /* Each item is independent */
1768 }
1769 }
a0d0e21e 1770 }
3280af22 1771 PL_curpm = newpm; /* Don't pop $1 et al till now */
a0d0e21e
LW
1772
1773 LEAVE;
1774
1775 RETURN;
1776}
1777
1778PP(pp_iter)
1779{
39644a26 1780 dSP;
c09156bb 1781 register PERL_CONTEXT *cx;
5f05dabc 1782 SV* sv;
4633a7c4 1783 AV* av;
1d7c1841 1784 SV **itersvp;
a0d0e21e 1785
924508f0 1786 EXTEND(SP, 1);
a0d0e21e 1787 cx = &cxstack[cxstack_ix];
6b35e009 1788 if (CxTYPE(cx) != CXt_LOOP)
cea2e8a9 1789 DIE(aTHX_ "panic: pp_iter");
a0d0e21e 1790
1d7c1841 1791 itersvp = CxITERVAR(cx);
4633a7c4 1792 av = cx->blk_loop.iterary;
89ea2908
GA
1793 if (SvTYPE(av) != SVt_PVAV) {
1794 /* iterate ($min .. $max) */
1795 if (cx->blk_loop.iterlval) {
1796 /* string increment */
1797 register SV* cur = cx->blk_loop.iterlval;
1798 STRLEN maxlen;
1799 char *max = SvPV((SV*)av, maxlen);
1800 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1d7c1841 1801 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1802 /* safe to reuse old SV */
1d7c1841 1803 sv_setsv(*itersvp, cur);
eaa5c2d6 1804 }
1c846c1f 1805 else
eaa5c2d6
GA
1806 {
1807 /* we need a fresh SV every time so that loop body sees a
1808 * completely new SV for closures/references to work as
1809 * they used to */
1d7c1841
GS
1810 SvREFCNT_dec(*itersvp);
1811 *itersvp = newSVsv(cur);
eaa5c2d6 1812 }
89ea2908
GA
1813 if (strEQ(SvPVX(cur), max))
1814 sv_setiv(cur, 0); /* terminate next time */
1815 else
1816 sv_inc(cur);
1817 RETPUSHYES;
1818 }
1819 RETPUSHNO;
1820 }
1821 /* integer increment */
1822 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1823 RETPUSHNO;
7f61b687 1824
3db8f154 1825 /* don't risk potential race */
1d7c1841 1826 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
eaa5c2d6 1827 /* safe to reuse old SV */
1d7c1841 1828 sv_setiv(*itersvp, cx->blk_loop.iterix++);
eaa5c2d6 1829 }
1c846c1f 1830 else
eaa5c2d6
GA
1831 {
1832 /* we need a fresh SV every time so that loop body sees a
1833 * completely new SV for closures/references to work as they
1834 * used to */
1d7c1841
GS
1835 SvREFCNT_dec(*itersvp);
1836 *itersvp = newSViv(cx->blk_loop.iterix++);
eaa5c2d6 1837 }
89ea2908
GA
1838 RETPUSHYES;
1839 }
1840
1841 /* iterate array */
3280af22 1842 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp : AvFILL(av)))
4633a7c4 1843 RETPUSHNO;
a0d0e21e 1844
1d7c1841 1845 SvREFCNT_dec(*itersvp);
a0d0e21e 1846
d42935ef
JH
1847 if (SvMAGICAL(av) || AvREIFY(av)) {
1848 SV **svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE);
1849 if (svp)
1850 sv = *svp;
1851 else
1852 sv = Nullsv;
1853 }
1854 else {
1855 sv = AvARRAY(av)[++cx->blk_loop.iterix];
1856 }
1857 if (sv)
a0d0e21e 1858 SvTEMP_off(sv);
a0d0e21e 1859 else
3280af22 1860 sv = &PL_sv_undef;
8b530633 1861 if (av != PL_curstack && sv == &PL_sv_undef) {
5f05dabc 1862 SV *lv = cx->blk_loop.iterlval;
71be2cbc
PP
1863 if (lv && SvREFCNT(lv) > 1) {
1864 SvREFCNT_dec(lv);
1865 lv = Nullsv;
1866 }
5f05dabc
PP
1867 if (lv)
1868 SvREFCNT_dec(LvTARG(lv));
1869 else {
68dc0745 1870 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
5f05dabc 1871 sv_upgrade(lv, SVt_PVLV);
5f05dabc 1872 LvTYPE(lv) = 'y';
14befaf4 1873 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
5f05dabc
PP
1874 }
1875 LvTARG(lv) = SvREFCNT_inc(av);
1876 LvTARGOFF(lv) = cx->blk_loop.iterix;
42718184 1877 LvTARGLEN(lv) = (STRLEN)UV_MAX;
5f05dabc
PP
1878 sv = (SV*)lv;
1879 }
a0d0e21e 1880
1d7c1841 1881 *itersvp = SvREFCNT_inc(sv);
a0d0e21e
LW
1882 RETPUSHYES;
1883}
1884
1885PP(pp_subst)
1886{
39644a26 1887 dSP; dTARG;
a0d0e21e
LW
1888 register PMOP *pm = cPMOP;
1889 PMOP *rpm = pm;
1890 register SV *dstr;
1891 register char *s;
1892 char *strend;
1893 register char *m;
1894 char *c;
1895 register char *d;
1896 STRLEN clen;
1897 I32 iters = 0;
1898 I32 maxiters;
1899 register I32 i;
1900 bool once;
71be2cbc 1901 bool rxtainted;
a0d0e21e 1902 char *orig;
22e551b9 1903 I32 r_flags;
aaa362c4 1904 register REGEXP *rx = PM_GETRE(pm);
a0d0e21e
LW
1905 STRLEN len;
1906 int force_on_match = 0;
3280af22 1907 I32 oldsave = PL_savestack_ix;
792b2c16 1908 STRLEN slen;
f272994b 1909 bool doutf8 = FALSE;
ed252734
NC
1910#ifdef PERL_COPY_ON_WRITE
1911 bool is_cow;
1912#endif
db79b45b 1913 SV *nsv = Nullsv;
a0d0e21e 1914
5cd24f17
PP
1915 /* known replacement string? */
1916 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
533c011a 1917 if (PL_op->op_flags & OPf_STACKED)
a0d0e21e
LW
1918 TARG = POPs;
1919 else {
54b9620d 1920 TARG = DEFSV;
a0d0e21e 1921 EXTEND(SP,1);
1c846c1f 1922 }
d9f424b2 1923
ed252734
NC
1924#ifdef PERL_COPY_ON_WRITE
1925 /* Awooga. Awooga. "bool" types that are actually char are dangerous,
1926 because they make integers such as 256 "false". */
1927 is_cow = SvIsCOW(TARG) ? TRUE : FALSE;
1928#else
765f542d
NC
1929 if (SvIsCOW(TARG))
1930 sv_force_normal_flags(TARG,0);
ed252734
NC
1931#endif
1932 if (
1933#ifdef PERL_COPY_ON_WRITE
1934 !is_cow &&
1935#endif
1936 (SvREADONLY(TARG)
68dc0745 1937 || (SvTYPE(TARG) > SVt_PVLV
ed252734 1938 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
d470f89e 1939 DIE(aTHX_ PL_no_modify);
8ec5e241
NIS
1940 PUTBACK;
1941
a0d0e21e 1942 s = SvPV(TARG, len);
68dc0745 1943 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
a0d0e21e 1944 force_on_match = 1;
b3eb6a9b 1945 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
3280af22
NIS
1946 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1947 if (PL_tainted)
b3eb6a9b 1948 rxtainted |= 2;
9212bbba 1949 TAINT_NOT;
a12c0f56 1950
a30b2f1f 1951 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
d9f424b2 1952
a0d0e21e
LW
1953 force_it:
1954 if (!pm || !s)
2269b42e 1955 DIE(aTHX_ "panic: pp_subst");
a0d0e21e
LW
1956
1957 strend = s + len;
a30b2f1f 1958 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
792b2c16
JH
1959 maxiters = 2 * slen + 10; /* We can match twice at each
1960 position, once with zero-length,
1961 second time with non-zero. */
a0d0e21e 1962
3280af22
NIS
1963 if (!rx->prelen && PL_curpm) {
1964 pm = PL_curpm;
aaa362c4 1965 rx = PM_GETRE(pm);
a0d0e21e 1966 }
22e551b9 1967 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
ed252734 1968 ? REXEC_COPY_STR : 0;
f722798b 1969 if (SvSCREAM(TARG))
22e551b9 1970 r_flags |= REXEC_SCREAM;
a0d0e21e 1971 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
3280af22
NIS
1972 SAVEINT(PL_multiline);
1973 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
a0d0e21e
LW
1974 }
1975 orig = m = s;
f722798b 1976 if (rx->reganch & RE_USE_INTUIT) {
ee0b7718 1977 PL_bostr = orig;
f722798b
IZ
1978 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1979
1980 if (!s)
1981 goto nope;
1982 /* How to do it in subst? */
1983/* if ( (rx->reganch & ROPT_CHECK_ALL)
1c846c1f 1984 && !PL_sawampersand
f722798b
IZ
1985 && ((rx->reganch & ROPT_NOSCAN)
1986 || !((rx->reganch & RE_INTUIT_TAIL)
1987 && (r_flags & REXEC_SCREAM))))
1988 goto yup;
1989*/
a0d0e21e 1990 }
71be2cbc
PP
1991
1992 /* only replace once? */
a0d0e21e 1993 once = !(rpm->op_pmflags & PMf_GLOBAL);
71be2cbc
PP
1994
1995 /* known replacement string? */
f272994b 1996 if (dstr) {
8514a05a
JH
1997 /* replacement needing upgrading? */
1998 if (DO_UTF8(TARG) && !doutf8) {
db79b45b 1999 nsv = sv_newmortal();
4a176938 2000 SvSetSV(nsv, dstr);
8514a05a
JH
2001 if (PL_encoding)
2002 sv_recode_to_utf8(nsv, PL_encoding);
2003 else
2004 sv_utf8_upgrade(nsv);
2005 c = SvPV(nsv, clen);
4a176938
JH
2006 doutf8 = TRUE;
2007 }
2008 else {
2009 c = SvPV(dstr, clen);
2010 doutf8 = DO_UTF8(dstr);
8514a05a 2011 }
f272994b
A
2012 }
2013 else {
2014 c = Nullch;
2015 doutf8 = FALSE;
2016 }
2017
71be2cbc 2018 /* can do inplace substitution? */
ed252734
NC
2019 if (c
2020#ifdef PERL_COPY_ON_WRITE
2021 && !is_cow
2022#endif
2023 && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
db79b45b
JH
2024 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)
2025 && (!doutf8 || SvUTF8(TARG))) {
f722798b
IZ
2026 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2027 r_flags | REXEC_CHECKED))
2028 {
8ec5e241 2029 SPAGAIN;
3280af22 2030 PUSHs(&PL_sv_no);
71be2cbc
PP
2031 LEAVE_SCOPE(oldsave);
2032 RETURN;
2033 }
ed252734
NC
2034#ifdef PERL_COPY_ON_WRITE
2035 if (SvIsCOW(TARG)) {
2036 assert (!force_on_match);
2037 goto have_a_cow;
2038 }
2039#endif
71be2cbc
PP
2040 if (force_on_match) {
2041 force_on_match = 0;
2042 s = SvPV_force(TARG, len);
2043 goto force_it;
2044 }
71be2cbc 2045 d = s;
3280af22 2046 PL_curpm = pm;
71be2cbc
PP
2047 SvSCREAM_off(TARG); /* disable possible screamer */
2048 if (once) {
48c036b1 2049 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d
IZ
2050 m = orig + rx->startp[0];
2051 d = orig + rx->endp[0];
71be2cbc
PP
2052 s = orig;
2053 if (m - s > strend - d) { /* faster to shorten from end */
2054 if (clen) {
2055 Copy(c, m, clen, char);
2056 m += clen;
a0d0e21e 2057 }
71be2cbc
PP
2058 i = strend - d;
2059 if (i > 0) {
2060 Move(d, m, i, char);
2061 m += i;
a0d0e21e 2062 }
71be2cbc
PP
2063 *m = '\0';
2064 SvCUR_set(TARG, m - s);
2065 }
2066 /*SUPPRESS 560*/
155aba94 2067 else if ((i = m - s)) { /* faster from front */
71be2cbc
PP
2068 d -= clen;
2069 m = d;
2070 sv_chop(TARG, d-i);
2071 s += i;
2072 while (i--)
2073 *--d = *--s;
2074 if (clen)
2075 Copy(c, m, clen, char);
2076 }
2077 else if (clen) {
2078 d -= clen;
2079 sv_chop(TARG, d);
2080 Copy(c, d, clen, char);
2081 }
2082 else {
2083 sv_chop(TARG, d);
2084 }
48c036b1 2085 TAINT_IF(rxtainted & 1);
8ec5e241 2086 SPAGAIN;
3280af22 2087 PUSHs(&PL_sv_yes);
71be2cbc
PP
2088 }
2089 else {
71be2cbc
PP
2090 do {
2091 if (iters++ > maxiters)
cea2e8a9 2092 DIE(aTHX_ "Substitution loop");
d9f97599 2093 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 2094 m = rx->startp[0] + orig;
71be2cbc 2095 /*SUPPRESS 560*/
155aba94 2096 if ((i = m - s)) {
71be2cbc
PP
2097 if (s != d)
2098 Move(s, d, i, char);
2099 d += i;
a0d0e21e 2100 }
71be2cbc
PP
2101 if (clen) {
2102 Copy(c, d, clen, char);
2103 d += clen;
2104 }
cf93c79d 2105 s = rx->endp[0] + orig;
cea2e8a9 2106 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
f722798b
IZ
2107 TARG, NULL,
2108 /* don't match same null twice */
2109 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
71be2cbc
PP
2110 if (s != d) {
2111 i = strend - s;
2112 SvCUR_set(TARG, d - SvPVX(TARG) + i);
2113 Move(s, d, i+1, char); /* include the NUL */
a0d0e21e 2114 }
48c036b1 2115 TAINT_IF(rxtainted & 1);
8ec5e241 2116 SPAGAIN;
71be2cbc 2117 PUSHs(sv_2mortal(newSViv((I32)iters)));
a0d0e21e 2118 }
80b498e0 2119 (void)SvPOK_only_UTF8(TARG);
48c036b1 2120 TAINT_IF(rxtainted);
8ec5e241
NIS
2121 if (SvSMAGICAL(TARG)) {
2122 PUTBACK;
2123 mg_set(TARG);
2124 SPAGAIN;
2125 }
9212bbba 2126 SvTAINT(TARG);
aefe6dfc
JH
2127 if (doutf8)
2128 SvUTF8_on(TARG);
71be2cbc
PP
2129 LEAVE_SCOPE(oldsave);
2130 RETURN;
a0d0e21e 2131 }
71be2cbc 2132
f722798b
IZ
2133 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2134 r_flags | REXEC_CHECKED))
2135 {
a0d0e21e
LW
2136 if (force_on_match) {
2137 force_on_match = 0;
2138 s = SvPV_force(TARG, len);
2139 goto force_it;
2140 }
ed252734
NC
2141#ifdef PERL_COPY_ON_WRITE
2142 have_a_cow:
2143#endif
48c036b1 2144 rxtainted |= RX_MATCH_TAINTED(rx);
8ec5e241 2145 dstr = NEWSV(25, len);
a0d0e21e 2146 sv_setpvn(dstr, m, s-m);
ffc61ed2
JH
2147 if (DO_UTF8(TARG))
2148 SvUTF8_on(dstr);
3280af22 2149 PL_curpm = pm;
a0d0e21e 2150 if (!c) {
c09156bb 2151 register PERL_CONTEXT *cx;
8ec5e241 2152 SPAGAIN;
a0d0e21e
LW
2153 PUSHSUBST(cx);
2154 RETURNOP(cPMOP->op_pmreplroot);
2155 }
cf93c79d 2156 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
a0d0e21e
LW
2157 do {
2158 if (iters++ > maxiters)
cea2e8a9 2159 DIE(aTHX_ "Substitution loop");
d9f97599 2160 rxtainted |= RX_MATCH_TAINTED(rx);
cf93c79d 2161 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
a0d0e21e
LW
2162 m = s;
2163 s = orig;
cf93c79d 2164 orig = rx->subbeg;
a0d0e21e
LW
2165 s = orig + (m - s);
2166 strend = s + (strend - m);
2167 }
cf93c79d 2168 m = rx->startp[0] + orig;
db79b45b
JH
2169 if (doutf8 && !SvUTF8(dstr))
2170 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
2171 else
2172 sv_catpvn(dstr, s, m-s);
cf93c79d 2173 s = rx->endp[0] + orig;
a0d0e21e
LW
2174 if (clen)
2175 sv_catpvn(dstr, c, clen);
2176 if (once)
2177 break;
ffc61ed2
JH
2178 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2179 TARG, NULL, r_flags));
db79b45b
JH
2180 if (doutf8 && !DO_UTF8(TARG))
2181 sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv);
89afcb60
A
2182 else
2183 sv_catpvn(dstr, s, strend - s);
748a9306 2184
ed252734
NC
2185#ifdef PERL_COPY_ON_WRITE
2186 /* The match may make the string COW. If so, brilliant, because that's
2187 just saved us one malloc, copy and free - the regexp has donated
2188 the old buffer, and we malloc an entirely new one, rather than the
2189 regexp malloc()ing a buffer and copying our original, only for
2190 us to throw it away here during the substitution. */
2191 if (SvIsCOW(TARG)) {
2192 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
2193 } else
2194#endif
2195 {
2196 (void)SvOOK_off(TARG);
2197 if (SvLEN(TARG))
2198 Safefree(SvPVX(TARG));
2199 }
748a9306
LW
2200 SvPVX(TARG) = SvPVX(dstr);
2201 SvCUR_set(TARG, SvCUR(dstr));
2202 SvLEN_set(TARG, SvLEN(dstr));
f272994b 2203 doutf8 |= DO_UTF8(dstr);
748a9306
LW
2204 SvPVX(dstr) = 0;
2205 sv_free(dstr);
2206
48c036b1 2207 TAINT_IF(rxtainted & 1);
f878fbec 2208 SPAGAIN;
48c036b1
GS
2209 PUSHs(sv_2mortal(newSViv((I32)iters)));
2210
a0d0e21e 2211 (void)SvPOK_only(TARG);
f272994b 2212 if (doutf8)
60aeb6fd 2213 SvUTF8_on(TARG);
48c036b1 2214 TAINT_IF(rxtainted);
a0d0e21e 2215 SvSETMAGIC(TARG);
9212bbba 2216 SvTAINT(TARG);
4633a7c4 2217 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2218 RETURN;
2219 }
5cd24f17 2220 goto ret_no;
a0d0e21e
LW
2221
2222nope:
1c846c1f 2223ret_no:
8ec5e241 2224 SPAGAIN;
3280af22 2225 PUSHs(&PL_sv_no);
4633a7c4 2226 LEAVE_SCOPE(oldsave);
a0d0e21e
LW
2227 RETURN;
2228}
2229
2230PP(pp_grepwhile)
2231{
39644a26 2232 dSP;
a0d0e21e
LW
2233
2234 if (SvTRUEx(POPs))
3280af22
NIS
2235 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2236 ++*PL_markstack_ptr;
a0d0e21e
LW
2237 LEAVE; /* exit inner scope */
2238
2239 /* All done yet? */
3280af22 2240 if (PL_stack_base + *PL_markstack_ptr > SP) {
a0d0e21e 2241 I32 items;
54310121 2242 I32 gimme = GIMME_V;
a0d0e21e
LW
2243
2244 LEAVE; /* exit outer scope */
2245 (void)POPMARK; /* pop src */
3280af22 2246 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
a0d0e21e 2247 (void)POPMARK; /* pop dst */
3280af22 2248 SP = PL_stack_base + POPMARK; /* pop original mark */
54310121 2249 if (gimme == G_SCALAR) {
a0d0e21e
LW
2250 dTARGET;
2251 XPUSHi(items);
a0d0e21e 2252 }
54310121
PP
2253 else if (gimme == G_ARRAY)
2254 SP += items;
a0d0e21e
LW
2255 RETURN;
2256 }
2257 else {
2258 SV *src;
2259
2260 ENTER; /* enter inner scope */
1d7c1841 2261 SAVEVPTR(PL_curpm);
a0d0e21e 2262
3280af22 2263 src = PL_stack_base[*PL_markstack_ptr];
a0d0e21e 2264 SvTEMP_off(src);
54b9620d 2265 DEFSV = src;
a0d0e21e
LW
2266
2267 RETURNOP(cLOGOP->op_other);
2268 }
2269}
2270
2271PP(pp_leavesub)
2272{
39644a26 2273 dSP;
a0d0e21e
LW
2274 SV **mark;
2275 SV **newsp;
2276 PMOP *newpm;
2277 I32 gimme;
c09156bb 2278 register PERL_CONTEXT *cx;
b0d9ce38 2279 SV *sv;
a0d0e21e
LW
2280
2281 POPBLOCK(cx,newpm);
1c846c1f 2282
a1f49e72 2283 TAINT_NOT;
a0d0e21e
LW
2284 if (gimme == G_SCALAR) {
2285 MARK = newsp + 1;
a29cdaf0 2286 if (MARK <= SP) {
a8bba7fa 2287 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
a29cdaf0
IZ
2288 if (SvTEMP(TOPs)) {
2289 *MARK = SvREFCNT_inc(TOPs);
2290 FREETMPS;
2291 sv_2mortal(*MARK);
cd06dffe
GS
2292 }
2293 else {
959e3673 2294 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
a29cdaf0 2295 FREETMPS;
959e3673
GS
2296 *MARK = sv_mortalcopy(sv);
2297 SvREFCNT_dec(sv);
a29cdaf0 2298 }
cd06dffe
GS
2299 }
2300 else
a29cdaf0 2301 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
cd06dffe
GS
2302 }
2303 else {
f86702cc 2304 MEXTEND(MARK, 0);
3280af22 2305 *MARK = &PL_sv_undef;
a0d0e21e
LW
2306 }
2307 SP = MARK;
2308 }
54310121 2309 else if (gimme == G_ARRAY) {
f86702cc 2310 for (MARK = newsp + 1; MARK <= SP; MARK++) {
a1f49e72 2311 if (!SvTEMP(*MARK)) {
f86702cc 2312 *MARK = sv_mortalcopy(*MARK);
a1f49e72
CS
2313 TAINT_NOT; /* Each item is independent */
2314 }
f86702cc 2315 }
a0d0e21e 2316 }
f86702cc 2317 PUTBACK;
1c846c1f 2318
b0d9ce38 2319 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
3280af22 2320 PL_curpm = newpm; /* ... and pop $1 et al */
a0d0e21e
LW
2321
2322 LEAVE;
b0d9ce38 2323 LEAVESUB(sv);
a0d0e21e
LW
2324 return pop_return();
2325}
2326
cd06dffe
GS
2327/* This duplicates the above code because the above code must not
2328 * get any slower by more conditions */
2329PP(pp_leavesublv)
2330{
39644a26 2331 dSP;
cd06dffe
GS
2332 SV **mark;
2333 SV **newsp;
2334 PMOP *newpm;
2335 I32 gimme;
2336 register PERL_CONTEXT *cx;
b0d9ce38 2337 SV *sv;
cd06dffe
GS
2338
2339 POPBLOCK(cx,newpm);
1c846c1f 2340
cd06dffe
GS
2341 TAINT_NOT;
2342
2343 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2344 /* We are an argument to a function or grep().
2345 * This kind of lvalueness was legal before lvalue
2346 * subroutines too, so be backward compatible:
2347 * cannot report errors. */
2348
2349 /* Scalar context *is* possible, on the LHS of -> only,
2350 * as in f()->meth(). But this is not an lvalue. */
2351 if (gimme == G_SCALAR)
2352 goto temporise;
2353 if (gimme == G_ARRAY) {
a8bba7fa 2354 if (!CvLVALUE(cx->blk_sub.cv))
cd06dffe
GS
2355 goto temporise_array;
2356 EXTEND_MORTAL(SP - newsp);
2357 for (mark = newsp + 1; mark <= SP; mark++) {
2358 if (SvTEMP(*mark))
2359 /* empty */ ;
2360 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2361 *mark = sv_mortalcopy(*mark);
2362 else {
2363 /* Can be a localized value subject to deletion. */
2364 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2365 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2366 }
2367 }
2368 }
2369 }
2370 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2371 /* Here we go for robustness, not for speed, so we change all
2372 * the refcounts so the caller gets a live guy. Cannot set
2373 * TEMP, so sv_2mortal is out of question. */
a8bba7fa 2374 if (!CvLVALUE(cx->blk_sub.cv)) {
b0d9ce38 2375 POPSUB(cx,sv);
d470f89e 2376 PL_curpm = newpm;
b0d9ce38
GS
2377 LEAVE;
2378 LEAVESUB(sv);
d470f89e
GS
2379 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2380 }
cd06dffe
GS
2381 if (gimme == G_SCALAR) {
2382 MARK = newsp + 1;
2383 EXTEND_MORTAL(1);
2384 if (MARK == SP) {
d470f89e 2385 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
b0d9ce38 2386 POPSUB(cx,sv);
d470f89e 2387 PL_curpm = newpm;
b0d9ce38
GS
2388 LEAVE;
2389 LEAVESUB(sv);
e9f19e3c
HS
2390 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2391 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2392 : "a readonly value" : "a temporary");
d470f89e 2393 }
cd06dffe
GS
2394 else { /* Can be a localized value
2395 * subject to deletion. */
2396 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2397 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2398 }
2399 }
d470f89e 2400 else { /* Should not happen? */
b0d9ce38 2401 POPSUB(cx,sv);
d470f89e 2402 PL_curpm = newpm;
b0d9ce38
GS
2403 LEAVE;
2404 LEAVESUB(sv);
d470f89e 2405 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
cd06dffe 2406 (MARK > SP ? "Empty array" : "Array"));
d470f89e 2407 }
cd06dffe
GS
2408 SP = MARK;
2409 }
2410 else if (gimme == G_ARRAY) {
2411 EXTEND_MORTAL(SP - newsp);
2412 for (mark = newsp + 1; mark <= SP; mark++) {
f206cdda
AMS
2413 if (*mark != &PL_sv_undef
2414 && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
d470f89e
GS
2415 /* Might be flattened array after $#array = */
2416 PUTBACK;
b0d9ce38 2417 POPSUB(cx,sv);
d470f89e 2418 PL_curpm = newpm;
b0d9ce38
GS
2419 LEAVE;
2420 LEAVESUB(sv);
f206cdda
AMS
2421 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2422 SvREADONLY(TOPs) ? "readonly value" : "temporary");
d470f89e 2423 }
cd06dffe 2424 else {
cd06dffe
GS
2425 /* Can be a localized value subject to deletion. */
2426 PL_tmps_stack[++PL_tmps_ix] = *mark;
e1f15930 2427 (void)SvREFCNT_inc(*mark);
cd06dffe
GS
2428 }
2429 }
2430 }
2431 }
2432 else {
2433 if (gimme == G_SCALAR) {
2434 temporise:
2435 MARK = newsp + 1;
2436 if (MARK <= SP) {
a8bba7fa 2437 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
cd06dffe
GS
2438 if (SvTEMP(TOPs)) {
2439 *MARK = SvREFCNT_inc(TOPs);
2440 FREETMPS;
2441 sv_2mortal(*MARK);
2442 }
2443 else {
959e3673 2444 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
cd06dffe 2445 FREETMPS;
959e3673
GS
2446 *MARK = sv_mortalcopy(sv);
2447 SvREFCNT_dec(sv);
cd06dffe
GS
2448 }
2449 }
2450 else
2451 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2452 }
2453 else {
2454 MEXTEND(MARK, 0);
2455 *MARK = &PL_sv_undef;
2456 }
2457 SP = MARK;
2458 }
2459 else if (gimme == G_ARRAY) {
2460 temporise_array:
2461 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2462 if (!SvTEMP(*MARK)) {
2463 *MARK = sv_mortalcopy(*MARK);
2464 TAINT_NOT; /* Each item is independent */
2465 }
2466 }
2467 }
2468 }
2469 PUTBACK;
1c846c1f 2470
b0d9ce38 2471 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
cd06dffe
GS
2472 PL_curpm = newpm; /* ... and pop $1 et al */
2473
2474 LEAVE;
b0d9ce38 2475 LEAVESUB(sv);
cd06dffe
GS
2476 return pop_return();
2477}
2478
2479
76e3520e 2480STATIC CV *
cea2e8a9 2481S_get_db_sub(pTHX_ SV **svp, CV *cv)
3de9ffa1 2482{
3280af22 2483 SV *dbsv = GvSV(PL_DBsub);
491527d0
GS
2484
2485 if (!PERLDB_SUB_NN) {
2486 GV *gv = CvGV(cv);
2487
2488 save_item(dbsv);
2489 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
1c846c1f 2490 || strEQ(GvNAME(gv), "END")
491527d0
GS
2491 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2492 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2493 && (gv = (GV*)*svp) ))) {
2494 /* Use GV from the stack as a fallback. */
2495 /* GV is potentially non-unique, or contain different CV. */
c2e66d9e
GS
2496 SV *tmp = newRV((SV*)cv);
2497 sv_setsv(dbsv, tmp);
2498 SvREFCNT_dec(tmp);
491527d0
GS
2499 }
2500 else {
2501 gv_efullname3(dbsv, gv, Nullch);
2502 }
3de9ffa1
MB
2503 }
2504 else {
155aba94
GS
2505 (void)SvUPGRADE(dbsv, SVt_PVIV);
2506 (void)SvIOK_on(dbsv);
491527d0 2507 SAVEIV(SvIVX(dbsv));
5bc28da9 2508 SvIVX(dbsv) = PTR2IV(cv); /* Do it the quickest way */
3de9ffa1 2509 }
491527d0 2510
3de9ffa1 2511 if (CvXSUB(cv))
3280af22
NIS
2512 PL_curcopdb = PL_curcop;
2513 cv = GvCV(PL_DBsub);
3de9ffa1
MB
2514 return cv;
2515}
2516
a0d0e21e
LW
2517PP(pp_entersub)
2518{
39644a26 2519 dSP; dPOPss;
a0d0e21e
LW
2520 GV *gv;
2521 HV *stash;
2522 register CV *cv;
c09156bb 2523 register PERL_CONTEXT *cx;
5d94fbed 2524 I32 gimme;
533c011a 2525 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
a0d0e21e
LW
2526
2527 if (!sv)
cea2e8a9 2528 DIE(aTHX_ "Not a CODE reference");
a0d0e21e 2529 switch (SvTYPE(sv)) {
f1025168
NC
2530 /* This is overwhelming the most common case: */
2531 case SVt_PVGV:
2532 if (!(cv = GvCVu((GV*)sv)))
2533 cv = sv_2cv(sv, &stash, &gv, FALSE);
2534 if (!cv) {
2535 ENTER;
2536 SAVETMPS;
2537 goto try_autoload;
2538 }
2539 break;
a0d0e21e
LW
2540 default:
2541 if (!SvROK(sv)) {
748a9306 2542 char *sym;
2d8e6c8d 2543 STRLEN n_a;
748a9306 2544
3280af22 2545 if (sv == &PL_sv_yes) { /* unfound import, ignore */
fb73857a 2546 if (hasargs)
3280af22 2547 SP = PL_stack_base + POPMARK;
a0d0e21e 2548 RETURN;
fb73857a 2549 }
15ff848f
CS
2550 if (SvGMAGICAL(sv)) {
2551 mg_get(sv);
f5f1d18e
AMS
2552 if (SvROK(sv))
2553 goto got_rv;
15ff848f
CS
2554 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2555 }
2556 else
2d8e6c8d 2557 sym = SvPV(sv, n_a);
15ff848f 2558 if (!sym)
cea2e8a9 2559 DIE(aTHX_ PL_no_usym, "a subroutine");
533c011a 2560 if (PL_op->op_private & HINT_STRICT_REFS)
cea2e8a9 2561 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
864dbfa3 2562 cv = get_cv(sym, TRUE);
a0d0e21e
LW
2563 break;
2564 }
f5f1d18e 2565 got_rv:
f5284f61
IZ
2566 {
2567 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2568 tryAMAGICunDEREF(to_cv);
2569 }
a0d0e21e
LW
2570 cv = (CV*)SvRV(sv);
2571 if (SvTYPE(cv) == SVt_PVCV)
2572 break;
2573 /* FALL THROUGH */
2574 case SVt_PVHV:
2575 case SVt_PVAV:
cea2e8a9 2576 DIE(aTHX_ "Not a CODE reference");
f1025168 2577 /* This is the second most common case: */
a0d0e21e
LW
2578 case SVt_PVCV:
2579 cv = (CV*)sv;
2580 break;
a0d0e21e
LW
2581 }
2582
2583 ENTER;
2584 SAVETMPS;
2585
2586 retry:
a0d0e21e 2587 if (!CvROOT(cv) && !CvXSUB(cv)) {
f1025168 2588 goto fooey;
a0d0e21e
LW
2589 }
2590
54310121 2591 gimme = GIMME_V;
67caa1fe 2592 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
06492da6
SF
2593 if (CvASSERTION(cv) && PL_DBassertion)
2594 sv_setiv(PL_DBassertion, 1);
2595
4f01c5a5 2596 cv = get_db_sub(&sv, cv);
67caa1fe 2597 if (!cv)
cea2e8a9 2598 DIE(aTHX_ "No DBsub routine");
67caa1fe 2599 }
a0d0e21e 2600
f1025168
NC
2601 if (!(CvXSUB(cv))) {
2602 /* This path taken at least 75% of the time */
a0d0e21e
LW
2603 dMARK;
2604 register I32 items = SP - MARK;
a0d0e21e 2605 AV* padlist = CvPADLIST(cv);
533c011a 2606 push_return(PL_op->op_next);
a0d0e21e
LW
2607 PUSHBLOCK(cx, CXt_SUB, MARK);
2608 PUSHSUB(cx);
2609 CvDEPTH(cv)++;
6b35e009
GS
2610 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2611 * that eval'' ops within this sub know the correct lexical space.
a3985cdc
DM
2612 * Owing the speed considerations, we choose instead to search for
2613 * the cv using find_runcv() when calling doeval().
6b35e009 2614 */
a0d0e21e
LW
2615 if (CvDEPTH(cv) < 2)
2616 (void)SvREFCNT_inc(cv);
dd2155a4 2617 else {
1d7c1841 2618 PERL_STACK_OVERFLOW_CHECK();
dd2155a4 2619 pad_push(padlist, CvDEPTH(cv), 1);
a0d0e21e 2620 }
dd2155a4 2621 PAD_SET_CUR(padlist, CvDEPTH(cv));
6d4ff0d2 2622 if (hasargs)
6d4ff0d2
MB
2623 {
2624 AV* av;
a0d0e21e
LW
2625 SV** ary;
2626
77a005ab 2627#if 0
bf49b057 2628 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2629 "%p entersub preparing @_\n", thr));
77a005ab 2630#endif
dd2155a4 2631 av = (AV*)PAD_SVl(0);
221373f0
GS
2632 if (AvREAL(av)) {
2633 /* @_ is normally not REAL--this should only ever
2634 * happen when DB::sub() calls things that modify @_ */
2635 av_clear(av);
2636 AvREAL_off(av);
2637 AvREIFY_on(av);
2638 }
3280af22
NIS
2639 cx->blk_sub.savearray = GvAV(PL_defgv);
2640 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
dd2155a4 2641 CX_CURPAD_SAVE(cx->blk_sub);
6d4ff0d2 2642 cx->blk_sub.argarray = av;
a0d0e21e
LW
2643 ++MARK;
2644
2645 if (items > AvMAX(av) + 1) {
2646 ary = AvALLOC(av);
2647 if (AvARRAY(av) != ary) {
2648 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2649 SvPVX(av) = (char*)ary;
2650 }
2651 if (items > AvMAX(av) + 1) {
2652 AvMAX(av) = items - 1;
2653 Renew(ary,items,SV*);
2654 AvALLOC(av) = ary;
2655 SvPVX(av) = (char*)ary;
2656 }
2657 }
2658 Copy(MARK,AvARRAY(av),items,SV*);
93965878 2659 AvFILLp(av) = items - 1;
1c846c1f 2660
a0d0e21e
LW
2661 while (items--) {
2662 if (*MARK)
2663 SvTEMP_off(*MARK);
2664 MARK++;
2665 }
2666 }
4a925ff6
GS
2667 /* warning must come *after* we fully set up the context
2668 * stuff so that __WARN__ handlers can safely dounwind()
2669 * if they want to
2670 */
2671 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2672 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2673 sub_crush_depth(cv);
77a005ab 2674#if 0
bf49b057 2675 DEBUG_S(PerlIO_printf(Perl_debug_log,
0f15f207 2676 "%p entersub returning %p\n", thr, CvSTART(cv)));
77a005ab 2677#endif
a0d0e21e
LW
2678 RETURNOP(CvSTART(cv));
2679 }
f1025168
NC
2680 else {
2681#ifdef PERL_XSUB_OLDSTYLE
2682 if (CvOLDSTYLE(cv)) {
2683 I32 (*fp3)(int,int,int);
2684 dMARK;
2685 register I32 items = SP - MARK;
2686 /* We dont worry to copy from @_. */
2687 while (SP > mark) {
2688 SP[1] = SP[0];
2689 SP--;
2690 }
2691 PL_stack_sp = mark + 1;
2692 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2693 items = (*fp3)(CvXSUBANY(cv).any_i32,
2694 MARK - PL_stack_base + 1,
2695 items);
2696 PL_stack_sp = PL_stack_base + items;
2697 }
2698 else
2699#endif /* PERL_XSUB_OLDSTYLE */
2700 {
2701 I32 markix = TOPMARK;
2702
2703 PUTBACK;
2704
2705 if (!hasargs) {
2706 /* Need to copy @_ to stack. Alternative may be to
2707 * switch stack to @_, and copy return values
2708 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2709 AV* av;
2710 I32 items;
2711 av = GvAV(PL_defgv);
2712 items = AvFILLp(av) + 1; /* @_ is not tieable */
2713
2714 if (items) {
2715 /* Mark is at the end of the stack. */
2716 EXTEND(SP, items);
2717 Copy(AvARRAY(av), SP + 1, items, SV*);
2718 SP += items;
2719 PUTBACK ;
2720 }
2721 }
2722 /* We assume first XSUB in &DB::sub is the called one. */
2723 if (PL_curcopdb) {
2724 SAVEVPTR(PL_curcop);
2725 PL_curcop = PL_curcopdb;
2726 PL_curcopdb = NULL;
2727 }
2728 /* Do we need to open block here? XXXX */
2729 (void)(*CvXSUB(cv))(aTHX_ cv);
2730
2731 /* Enforce some sanity in scalar context. */
2732 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2733 if (markix > PL_stack_sp - PL_stack_base)
2734 *(PL_stack_base + markix) = &PL_sv_undef;
2735 else
2736 *(PL_stack_base + markix) = *PL_stack_sp;
2737 PL_stack_sp = PL_stack_base + markix;
2738 }
2739 }
2740 LEAVE;
2741 return NORMAL;
2742 }
2743
2744 assert (0); /* Cannot get here. */
2745 /* This is deliberately moved here as spaghetti code to keep it out of the
2746 hot path. */
2747 {
2748 GV* autogv;
2749 SV* sub_name;
2750
2751 fooey:
2752 /* anonymous or undef'd function leaves us no recourse */
2753 if (CvANON(cv) || !(gv = CvGV(cv)))
2754 DIE(aTHX_ "Undefined subroutine called");
2755
2756 /* autoloaded stub? */
2757 if (cv != GvCV(gv)) {
2758 cv = GvCV(gv);
2759 }
2760 /* should call AUTOLOAD now? */
2761 else {
2762try_autoload:
2763 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2764 FALSE)))
2765 {
2766 cv = GvCV(autogv);
2767 }
2768 /* sorry */
2769 else {
2770 sub_name = sv_newmortal();
2771 gv_efullname3(sub_name, gv, Nullch);
35c1215d 2772 DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name);
f1025168
NC
2773 }
2774 }
2775 if (!cv)
2776 DIE(aTHX_ "Not a CODE reference");
2777 goto retry;
2778 }
a0d0e21e
LW
2779}
2780
44a8e56a 2781void
864dbfa3 2782Perl_sub_crush_depth(pTHX_ CV *cv)
44a8e56a
PP
2783{
2784 if (CvANON(cv))
9014280d 2785 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
44a8e56a
PP
2786 else {
2787 SV* tmpstr = sv_newmortal();
2788 gv_efullname3(tmpstr, CvGV(cv), Nullch);
35c1215d
NC
2789 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
2790 tmpstr);
44a8e56a
PP
2791 }
2792}
2793
a0d0e21e
LW
2794PP(pp_aelem)
2795{
39644a26 2796 dSP;
a0d0e21e 2797 SV** svp;
d804643f
SC
2798 SV* elemsv = POPs;
2799 IV elem = SvIV(elemsv);
68dc0745 2800 AV* av = (AV*)POPs;
78f9721b 2801 U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
533c011a 2802 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
be6c24e0 2803 SV *sv;
a0d0e21e 2804
e35c1634 2805 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
35c1215d 2806 Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv);
748a9306 2807 if (elem > 0)
3280af22 2808 elem -= PL_curcop->cop_arybase;
a0d0e21e
LW
2809 if (SvTYPE(av) != SVt_PVAV)
2810 RETPUSHUNDEF;
68dc0745 2811 svp = av_fetch(av, elem, lval && !defer);
a0d0e21e 2812 if (lval) {
3280af22 2813 if (!svp || *svp == &PL_sv_undef) {
68dc0745
PP
2814 SV* lv;
2815 if (!defer)
cea2e8a9 2816 DIE(aTHX_ PL_no_aelem, elem);
68dc0745
PP
2817 lv = sv_newmortal();
2818 sv_upgrade(lv, SVt_PVLV);
2819 LvTYPE(lv) = 'y';
14befaf4 2820 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
68dc0745
PP
2821 LvTARG(lv) = SvREFCNT_inc(av);
2822 LvTARGOFF(lv) = elem;
2823 LvTARGLEN(lv) = 1;
2824 PUSHs(lv);
2825 RETURN;
2826 }
bfc4de9f 2827 if (PL_op->op_private & OPpLVAL_INTRO)
161b7d16 2828 save_aelem(av, elem, svp);
533c011a
NIS
2829 else if (PL_op->op_private & OPpDEREF)
2830 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
a0d0e21e 2831 }
3280af22 2832 sv = (svp ? *svp : &PL_sv_undef);
be6c24e0
GS
2833 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2834 sv = sv_mortalcopy(sv);
2835 PUSHs(sv);
a0d0e21e
LW
2836 RETURN;
2837}
2838
02a9e968 2839void
864dbfa3 2840Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
02a9e968
CS
2841{
2842 if (SvGMAGICAL(sv))
2843 mg_get(sv);
2844 if (!SvOK(sv)) {
2845 if (SvREADONLY(sv))
cea2e8a9 2846 Perl_croak(aTHX_ PL_no_modify);
5f05dabc
PP
2847 if (SvTYPE(sv) < SVt_RV)
2848 sv_upgrade(sv, SVt_RV);
2849 else if (SvTYPE(sv) >= SVt_PV) {
2850 (void)SvOOK_off(sv);
2851 Safefree(SvPVX(sv));
2852 SvLEN(sv) = SvCUR(sv) = 0;
2853 }
68dc0745 2854 switch (to_what) {
5f05dabc 2855 case OPpDEREF_SV:
8c52afec 2856 SvRV(sv) = NEWSV(355,0);
5f05dabc
PP
2857 break;
2858 case OPpDEREF_AV:
2859 SvRV(sv) = (SV*)newAV();
2860 break;
2861 case OPpDEREF_HV:
2862 SvRV(sv) = (SV*)newHV();
2863 break;
2864 }
02a9e968
CS
2865 SvROK_on(sv);
2866 SvSETMAGIC(sv);
2867 }
2868}
2869
a0d0e21e
LW
2870PP(pp_method)
2871{
39644a26 2872 dSP;
f5d5a27c
CS
2873 SV* sv = TOPs;
2874
2875 if (SvROK(sv)) {
eda383f2 2876 SV* rsv = SvRV(sv);
f5d5a27c
CS
2877 if (SvTYPE(rsv) == SVt_PVCV) {
2878 SETs(rsv);
2879 RETURN;
2880 }
2881 }
2882
2883 SETs(method_common(sv, Null(U32*)));
2884 RETURN;
2885}
2886
2887PP(pp_method_named)
2888{
39644a26 2889 dSP;
3848b962 2890 SV* sv = cSVOP_sv;
f5d5a27c
CS
2891 U32 hash = SvUVX(sv);
2892
2893 XPUSHs(method_common(sv, &hash));
2894 RETURN;
2895}
2896
2897STATIC SV *
2898S_method_common(pTHX_ SV* meth, U32* hashp)
2899{
a0d0e21e
LW
2900 SV* sv;
2901 SV* ob;
2902 GV* gv;
56304f61
CS
2903 HV* stash;
2904 char* name;
f5d5a27c 2905 STRLEN namelen;
9c5ffd7c 2906 char* packname = 0;
0dae17bd 2907 SV *packsv = Nullsv;
ac91690f 2908 STRLEN packlen;
a0d0e21e 2909
f5d5a27c 2910 name = SvPV(meth, namelen);
3280af22 2911 sv = *(PL_stack_base + TOPMARK + 1);
f5d5a27c 2912
4f1b7578
SC
2913 if (!sv)
2914 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
2915
16d20bd9 2916 if (SvGMAGICAL(sv))
af09ea45 2917 mg_get(sv);
a0d0e21e 2918 if (SvROK(sv))
16d20bd9 2919 ob = (SV*)SvRV(sv);
a0d0e21e
LW
2920 else {
2921 GV* iogv;
a0d0e21e 2922
af09ea45 2923 /* this isn't a reference */
56304f61 2924 packname = Nullch;
a0d0e21e 2925 if (!SvOK(sv) ||
56304f61 2926 !(packname = SvPV(sv, packlen)) ||
a0d0e21e
LW
2927 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
2928 !(ob=(SV*)GvIO(iogv)))
2929 {
af09ea45 2930 /* this isn't the name of a filehandle either */
1c846c1f 2931 if (!packname ||
fd400ab9 2932 ((UTF8_IS_START(*packname) && DO_UTF8(sv))
b86a2fa7 2933 ? !isIDFIRST_utf8((U8*)packname)
834a4ddd
LW
2934 : !isIDFIRST(*packname)
2935 ))
2936 {
f5d5a27c
CS
2937 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
2938 SvOK(sv) ? "without a package or object reference"
2939 : "on an undefined value");
834a4ddd 2940 }
af09ea45
IK
2941 /* assume it's a package name */
2942 stash = gv_stashpvn(packname, packlen, FALSE);
0dae17bd
GS
2943 if (!stash)
2944 packsv = sv;
ac91690f 2945 goto fetch;
a0d0e21e 2946 }
af09ea45 2947 /* it _is_ a filehandle name -- replace with a reference */
3280af22 2948 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
a0d0e21e
LW
2949 }
2950
af09ea45 2951 /* if we got here, ob should be a reference or a glob */
f0d43078
GS
2952 if (!ob || !(SvOBJECT(ob)
2953 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
2954 && SvOBJECT(ob))))
2955 {
f5d5a27c
CS
2956 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
2957 name);
f0d43078 2958 }
a0d0e21e 2959
56304f61 2960 stash = SvSTASH(ob);
a0d0e21e 2961
ac91690f 2962 fetch:
af09ea45
IK
2963 /* NOTE: stash may be null, hope hv_fetch_ent and
2964 gv_fetchmethod can cope (it seems they can) */
2965
f5d5a27c
CS
2966 /* shortcut for simple names */
2967 if (hashp) {
2968 HE* he = hv_fetch_ent(stash, meth, 0, *hashp);
2969 if (he) {
2970 gv = (GV*)HeVAL(he);
2971 if (isGV(gv) && GvCV(gv) &&
2972 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
2973 return (SV*)GvCV(gv);
2974 }
2975 }
2976
0dae17bd 2977 gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name);
af09ea45 2978
56304f61 2979 if (!gv) {
af09ea45
IK
2980 /* This code tries to figure out just what went wrong with
2981 gv_fetchmethod. It therefore needs to duplicate a lot of
2982 the internals of that function. We can't move it inside
2983 Perl_gv_fetchmethod_autoload(), however, since that would
2984 cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we
2985 don't want that.
2986 */
56304f61
CS
2987 char* leaf = name;
2988 char* sep = Nullch;
2989 char* p;
2990
2991 for (p = name; *p; p++) {
2992 if (*p == '\'')
2993 sep = p, leaf = p + 1;
2994 else if (*p == ':' && *(p + 1) == ':')
2995 sep = p, leaf = p + 2;
2996 }
2997 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
af09ea45
IK
2998 /* the method name is unqualified or starts with SUPER:: */
2999 packname = sep ? CopSTASHPV(PL_curcop) :
3000 stash ? HvNAME(stash) : packname;
56304f61
CS
3001 packlen = strlen(packname);
3002 }
3003 else {
af09ea45 3004 /* the method name is qualified */
56304f61
CS
3005 packname = name;
3006 packlen = sep - name;
3007 }
af09ea45
IK
3008
3009 /* we're relying on gv_fetchmethod not autovivifying the stash */
3010 if (gv_stashpvn(packname, packlen, FALSE)) {
c1899e02 3011 Perl_croak(aTHX_
af09ea45
IK
3012 "Can't locate object method \"%s\" via package \"%.*s\"",
3013 leaf, (int)packlen, packname);
c1899e02
GS
3014 }
3015 else {
3016 Perl_croak(aTHX_
af09ea45
IK
3017 "Can't locate object method \"%s\" via package \"%.*s\""
3018 " (perhaps you forgot to load \"%.*s\"?)",
3019 leaf, (int)packlen, packname, (int)packlen, packname);
c1899e02 3020 }
56304f61 3021 }
f5d5a27c 3022 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
a0d0e21e 3023}