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