3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * Then he heard Merry change the note, and up went the Horn-cry of Buckland,
15 * Awake! Awake! Fear, Fire, Foes! Awake!
19 /* This file contains 'hot' pp ("push/pop") functions that
20 * execute the opcodes that make up a perl program. A typical pp function
21 * expects to find its arguments on the stack, and usually pushes its
22 * results onto the stack, hence the 'pp' terminology. Each OP structure
23 * contains a pointer to the relevant pp_foo() function.
25 * By 'hot', we mean common ops whose execution speed is critical.
26 * By gathering them together into a single file, we encourage
27 * CPU cache hits on hot code. Also it could be taken as a warning not to
28 * change any code in this file unless you're sure it won't affect
33 #define PERL_IN_PP_HOT_C
47 PL_curcop = (COP*)PL_op;
48 TAINT_NOT; /* Each statement is presumed innocent */
49 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
58 if (PL_op->op_private & OPpLVAL_INTRO)
59 PUSHs(save_scalar(cGVOP_gv));
61 PUSHs(GvSVn(cGVOP_gv));
72 PL_curcop = (COP*)PL_op;
78 PUSHMARK(PL_stack_sp);
93 XPUSHs((SV*)cGVOP_gv);
103 if (PL_op->op_type == OP_AND)
105 RETURNOP(cLOGOP->op_other);
113 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
115 temp = left; left = right; right = temp;
117 if (PL_tainting && PL_tainted && !SvTAINTED(left))
119 SvSetMagicSV(right, left);
128 RETURNOP(cLOGOP->op_other);
130 RETURNOP(cLOGOP->op_next);
136 TAINT_NOT; /* Each statement is presumed innocent */
137 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
139 oldsave = PL_scopestack[PL_scopestack_ix - 1];
140 LEAVE_SCOPE(oldsave);
146 dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
153 bool rcopied = FALSE;
155 if (TARG == right && right != left) {
156 /* mg_get(right) may happen here ... */
157 rpv = SvPV_const(right, rlen);
158 rbyte = !DO_UTF8(right);
159 right = sv_2mortal(newSVpvn(rpv, rlen));
160 rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */
166 const char* const lpv = SvPV_const(left, llen); /* mg_get(left) may happen here */
167 lbyte = !DO_UTF8(left);
168 sv_setpvn(TARG, lpv, llen);
174 else { /* TARG == left */
176 SvGETMAGIC(left); /* or mg_get(left) may happen here */
178 sv_setpvn(left, "", 0);
179 (void)SvPV_nomg_const(left, llen); /* Needed to set UTF8 flag */
180 lbyte = !DO_UTF8(left);
185 /* or mg_get(right) may happen here */
187 rpv = SvPV_const(right, rlen);
188 rbyte = !DO_UTF8(right);
190 if (lbyte != rbyte) {
192 sv_utf8_upgrade_nomg(TARG);
195 right = sv_2mortal(newSVpvn(rpv, rlen));
196 sv_utf8_upgrade_nomg(right);
197 rpv = SvPV_const(right, rlen);
200 sv_catpvn_nomg(TARG, rpv, rlen);
211 if (PL_op->op_flags & OPf_MOD) {
212 if (PL_op->op_private & OPpLVAL_INTRO)
213 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
214 if (PL_op->op_private & OPpDEREF) {
216 vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
225 tryAMAGICunTARGET(iter, 0);
226 PL_last_in_gv = (GV*)(*PL_stack_sp--);
227 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
228 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
229 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
232 XPUSHs((SV*)PL_last_in_gv);
235 PL_last_in_gv = (GV*)(*PL_stack_sp--);
238 return do_readline();
243 dSP; tryAMAGICbinSET(eq,0);
244 #ifndef NV_PRESERVES_UV
245 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
247 SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s)));
251 #ifdef PERL_PRESERVE_IVUV
254 /* Unless the left argument is integer in range we are going
255 to have to use NV maths. Hence only attempt to coerce the
256 right argument if we know the left is integer. */
259 bool auvok = SvUOK(TOPm1s);
260 bool buvok = SvUOK(TOPs);
262 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
263 /* Casting IV to UV before comparison isn't going to matter
264 on 2s complement. On 1s complement or sign&magnitude
265 (if we have any of them) it could to make negative zero
266 differ from normal zero. As I understand it. (Need to
267 check - is negative zero implementation defined behaviour
269 UV buv = SvUVX(POPs);
270 UV auv = SvUVX(TOPs);
272 SETs(boolSV(auv == buv));
275 { /* ## Mixed IV,UV ## */
279 /* == is commutative so doesn't matter which is left or right */
281 /* top of stack (b) is the iv */
290 /* As uv is a UV, it's >0, so it cannot be == */
294 /* we know iv is >= 0 */
295 SETs(boolSV((UV)iv == SvUVX(uvp)));
303 SETs(boolSV(TOPn == value));
311 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
312 DIE(aTHX_ PL_no_modify);
313 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
314 && SvIVX(TOPs) != IV_MAX)
316 SvIV_set(TOPs, SvIVX(TOPs) + 1);
317 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
319 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
331 if (PL_op->op_type == OP_OR)
333 RETURNOP(cLOGOP->op_other);
340 register SV* sv = NULL;
341 bool defined = FALSE;
342 const int op_type = PL_op->op_type;
344 if(op_type == OP_DOR || op_type == OP_DORASSIGN) {
346 if (!sv || !SvANY(sv)) {
347 if (op_type == OP_DOR)
349 RETURNOP(cLOGOP->op_other);
351 } else if (op_type == OP_DEFINED) {
353 if (!sv || !SvANY(sv))
356 DIE(aTHX_ "panic: Invalid op (%s) in pp_defined()", OP_NAME(PL_op));
358 switch (SvTYPE(sv)) {
360 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
364 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
368 if (CvROOT(sv) || CvXSUB(sv))
377 if(op_type == OP_DOR || op_type == OP_DORASSIGN) {
380 if(op_type == OP_DOR)
382 RETURNOP(cLOGOP->op_other);
384 /* assuming OP_DEFINED */
392 dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
393 useleft = USE_LEFT(TOPm1s);
394 #ifdef PERL_PRESERVE_IVUV
395 /* We must see if we can perform the addition with integers if possible,
396 as the integer code detects overflow while the NV code doesn't.
397 If either argument hasn't had a numeric conversion yet attempt to get
398 the IV. It's important to do this now, rather than just assuming that
399 it's not IOK as a PV of "9223372036854775806" may not take well to NV
400 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
401 integer in case the second argument is IV=9223372036854775806
402 We can (now) rely on sv_2iv to do the right thing, only setting the
403 public IOK flag if the value in the NV (or PV) slot is truly integer.
405 A side effect is that this also aggressively prefers integer maths over
406 fp maths for integer values.
408 How to detect overflow?
410 C 99 section 6.2.6.1 says
412 The range of nonnegative values of a signed integer type is a subrange
413 of the corresponding unsigned integer type, and the representation of
414 the same value in each type is the same. A computation involving
415 unsigned operands can never overflow, because a result that cannot be
416 represented by the resulting unsigned integer type is reduced modulo
417 the number that is one greater than the largest value that can be
418 represented by the resulting type.
422 which I read as "unsigned ints wrap."
424 signed integer overflow seems to be classed as "exception condition"
426 If an exceptional condition occurs during the evaluation of an
427 expression (that is, if the result is not mathematically defined or not
428 in the range of representable values for its type), the behavior is
431 (6.5, the 5th paragraph)
433 I had assumed that on 2s complement machines signed arithmetic would
434 wrap, hence coded pp_add and pp_subtract on the assumption that
435 everything perl builds on would be happy. After much wailing and
436 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
437 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
438 unsigned code below is actually shorter than the old code. :-)
443 /* Unless the left argument is integer in range we are going to have to
444 use NV maths. Hence only attempt to coerce the right argument if
445 we know the left is integer. */
453 /* left operand is undef, treat as zero. + 0 is identity,
454 Could SETi or SETu right now, but space optimise by not adding
455 lots of code to speed up what is probably a rarish case. */
457 /* Left operand is defined, so is it IV? */
460 if ((auvok = SvUOK(TOPm1s)))
463 register const IV aiv = SvIVX(TOPm1s);
466 auvok = 1; /* Now acting as a sign flag. */
467 } else { /* 2s complement assumption for IV_MIN */
475 bool result_good = 0;
478 bool buvok = SvUOK(TOPs);
483 register const IV biv = SvIVX(TOPs);
490 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
491 else "IV" now, independent of how it came in.
492 if a, b represents positive, A, B negative, a maps to -A etc
497 all UV maths. negate result if A negative.
498 add if signs same, subtract if signs differ. */
504 /* Must get smaller */
510 /* result really should be -(auv-buv). as its negation
511 of true value, need to swap our result flag */
528 if (result <= (UV)IV_MIN)
531 /* result valid, but out of range for IV. */
536 } /* Overflow, drop through to NVs. */
543 /* left operand is undef, treat as zero. + 0.0 is identity. */
547 SETn( value + TOPn );
555 AV *av = PL_op->op_flags & OPf_SPECIAL ?
556 (AV*)PAD_SV(PL_op->op_targ) : GvAV(cGVOP_gv);
557 const U32 lval = PL_op->op_flags & OPf_MOD;
558 SV** svp = av_fetch(av, PL_op->op_private, lval);
559 SV *sv = (svp ? *svp : &PL_sv_undef);
561 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
562 sv = sv_mortalcopy(sv);
571 do_join(TARG, *MARK, MARK, SP);
582 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
583 * will be enough to hold an OP*.
585 SV* const sv = sv_newmortal();
586 sv_upgrade(sv, SVt_PVLV);
588 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
596 /* Oversized hot code. */
600 dVAR; dSP; dMARK; dORIGMARK;
606 if (PL_op->op_flags & OPf_STACKED)
611 if (gv && (io = GvIO(gv))
612 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
615 if (MARK == ORIGMARK) {
616 /* If using default handle then we need to make space to
617 * pass object as 1st arg, so move other args up ...
621 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
625 *MARK = SvTIED_obj((SV*)io, mg);
628 call_method("PRINT", G_SCALAR);
636 if (!(io = GvIO(gv))) {
637 if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv)))
638 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
640 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
641 report_evil_fh(gv, io, PL_op->op_type);
642 SETERRNO(EBADF,RMS_IFI);
645 else if (!(fp = IoOFP(io))) {
646 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
648 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
649 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
650 report_evil_fh(gv, io, PL_op->op_type);
652 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
657 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
659 if (!do_print(*MARK, fp))
663 if (!do_print(PL_ofs_sv, fp)) { /* $, */
672 if (!do_print(*MARK, fp))
680 if (PL_ors_sv && SvOK(PL_ors_sv))
681 if (!do_print(PL_ors_sv, fp)) /* $\ */
684 if (IoFLAGS(io) & IOf_FLUSH)
685 if (PerlIO_flush(fp) == EOF)
695 XPUSHs(&PL_sv_undef);
706 tryAMAGICunDEREF(to_av);
709 if (SvTYPE(av) != SVt_PVAV)
710 DIE(aTHX_ "Not an ARRAY reference");
711 if (PL_op->op_flags & OPf_REF) {
716 if (GIMME == G_SCALAR)
717 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
721 else if (PL_op->op_flags & OPf_MOD
722 && PL_op->op_private & OPpLVAL_INTRO)
723 Perl_croak(aTHX_ PL_no_localize_ref);
726 if (SvTYPE(sv) == SVt_PVAV) {
728 if (PL_op->op_flags & OPf_REF) {
733 if (GIMME == G_SCALAR)
734 Perl_croak(aTHX_ "Can't return array to lvalue"
743 if (SvTYPE(sv) != SVt_PVGV) {
744 if (SvGMAGICAL(sv)) {
750 if (PL_op->op_flags & OPf_REF ||
751 PL_op->op_private & HINT_STRICT_REFS)
752 DIE(aTHX_ PL_no_usym, "an ARRAY");
753 if (ckWARN(WARN_UNINITIALIZED))
755 if (GIMME == G_ARRAY) {
761 if ((PL_op->op_flags & OPf_SPECIAL) &&
762 !(PL_op->op_flags & OPf_MOD))
764 gv = (GV*)gv_fetchsv(sv, FALSE, SVt_PVAV);
766 && (!is_gv_magical_sv(sv,0)
767 || !(gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVAV))))
773 if (PL_op->op_private & HINT_STRICT_REFS)
774 DIE(aTHX_ PL_no_symref_sv, sv, "an ARRAY");
775 gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVAV);
782 if (PL_op->op_private & OPpLVAL_INTRO)
784 if (PL_op->op_flags & OPf_REF) {
789 if (GIMME == G_SCALAR)
790 Perl_croak(aTHX_ "Can't return array to lvalue"
798 if (GIMME == G_ARRAY) {
799 const I32 maxarg = AvFILL(av) + 1;
800 (void)POPs; /* XXXX May be optimized away? */
802 if (SvRMAGICAL(av)) {
804 for (i=0; i < (U32)maxarg; i++) {
805 SV ** const svp = av_fetch(av, i, FALSE);
806 /* See note in pp_helem, and bug id #27839 */
808 ? SvGMAGICAL(*svp) ? sv_mortalcopy(*svp) : *svp
813 Copy(AvARRAY(av), SP+1, maxarg, SV*);
817 else if (GIMME_V == G_SCALAR) {
819 const I32 maxarg = AvFILL(av) + 1;
829 const I32 gimme = GIMME_V;
830 static const char return_hash_to_lvalue_scalar[] = "Can't return hash to lvalue scalar context";
834 tryAMAGICunDEREF(to_hv);
837 if (SvTYPE(hv) != SVt_PVHV)
838 DIE(aTHX_ "Not a HASH reference");
839 if (PL_op->op_flags & OPf_REF) {
844 if (gimme != G_ARRAY)
845 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
849 else if (PL_op->op_flags & OPf_MOD
850 && PL_op->op_private & OPpLVAL_INTRO)
851 Perl_croak(aTHX_ PL_no_localize_ref);
854 if (SvTYPE(sv) == SVt_PVHV) {
856 if (PL_op->op_flags & OPf_REF) {
861 if (gimme != G_ARRAY)
862 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
870 if (SvTYPE(sv) != SVt_PVGV) {
871 if (SvGMAGICAL(sv)) {
877 if (PL_op->op_flags & OPf_REF ||
878 PL_op->op_private & HINT_STRICT_REFS)
879 DIE(aTHX_ PL_no_usym, "a HASH");
880 if (ckWARN(WARN_UNINITIALIZED))
882 if (gimme == G_ARRAY) {
888 if ((PL_op->op_flags & OPf_SPECIAL) &&
889 !(PL_op->op_flags & OPf_MOD))
891 gv = (GV*)gv_fetchsv(sv, FALSE, SVt_PVHV);
893 && (!is_gv_magical_sv(sv,0)
894 || !(gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVHV))))
900 if (PL_op->op_private & HINT_STRICT_REFS)
901 DIE(aTHX_ PL_no_symref_sv, sv, "a HASH");
902 gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVHV);
909 if (PL_op->op_private & OPpLVAL_INTRO)
911 if (PL_op->op_flags & OPf_REF) {
916 if (gimme != G_ARRAY)
917 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
924 if (gimme == G_ARRAY) { /* array wanted */
925 *PL_stack_sp = (SV*)hv;
928 else if (gimme == G_SCALAR) {
930 TARG = Perl_hv_scalar(aTHX_ hv);
937 S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
943 if (ckWARN(WARN_MISC)) {
945 if (relem == firstrelem &&
947 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
948 SvTYPE(SvRV(*relem)) == SVt_PVHV))
950 err = "Reference found where even-sized list expected";
953 err = "Odd number of elements in hash assignment";
954 Perl_warner(aTHX_ packWARN(WARN_MISC), err);
957 tmpstr = NEWSV(29,0);
958 didstore = hv_store_ent(hash,*relem,tmpstr,0);
959 if (SvMAGICAL(hash)) {
960 if (SvSMAGICAL(tmpstr))
972 SV **lastlelem = PL_stack_sp;
973 SV **lastrelem = PL_stack_base + POPMARK;
974 SV **firstrelem = PL_stack_base + POPMARK + 1;
975 SV **firstlelem = lastrelem + 1;
988 SV **firsthashrelem = 0; /* "= 0" keeps gcc 2.95 quiet */
991 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
994 /* If there's a common identifier on both sides we have to take
995 * special care that assigning the identifier on the left doesn't
996 * clobber a value on the right that's used later in the list.
998 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
999 EXTEND_MORTAL(lastrelem - firstrelem + 1);
1000 for (relem = firstrelem; relem <= lastrelem; relem++) {
1001 if ((sv = *relem)) {
1002 TAINT_NOT; /* Each item is independent */
1003 *relem = sv_mortalcopy(sv);
1013 while (lelem <= lastlelem) {
1014 TAINT_NOT; /* Each item stands on its own, taintwise. */
1016 switch (SvTYPE(sv)) {
1019 magic = SvMAGICAL(ary) != 0;
1021 av_extend(ary, lastrelem - relem);
1023 while (relem <= lastrelem) { /* gobble up all the rest */
1026 sv = newSVsv(*relem);
1028 didstore = av_store(ary,i++,sv);
1038 case SVt_PVHV: { /* normal hash */
1042 magic = SvMAGICAL(hash) != 0;
1044 firsthashrelem = relem;
1046 while (relem < lastrelem) { /* gobble up all the rest */
1051 sv = &PL_sv_no, relem++;
1052 tmpstr = NEWSV(29,0);
1054 sv_setsv(tmpstr,*relem); /* value */
1055 *(relem++) = tmpstr;
1056 if (gimme != G_VOID && hv_exists_ent(hash, sv, 0))
1057 /* key overwrites an existing entry */
1059 didstore = hv_store_ent(hash,sv,tmpstr,0);
1061 if (SvSMAGICAL(tmpstr))
1068 if (relem == lastrelem) {
1069 do_oddball(hash, relem, firstrelem);
1075 if (SvIMMORTAL(sv)) {
1076 if (relem <= lastrelem)
1080 if (relem <= lastrelem) {
1081 sv_setsv(sv, *relem);
1085 sv_setsv(sv, &PL_sv_undef);
1090 if (PL_delaymagic & ~DM_DELAY) {
1091 if (PL_delaymagic & DM_UID) {
1092 #ifdef HAS_SETRESUID
1093 (void)setresuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1094 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1,
1097 # ifdef HAS_SETREUID
1098 (void)setreuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1099 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1);
1102 if ((PL_delaymagic & DM_UID) == DM_RUID) {
1103 (void)setruid(PL_uid);
1104 PL_delaymagic &= ~DM_RUID;
1106 # endif /* HAS_SETRUID */
1108 if ((PL_delaymagic & DM_UID) == DM_EUID) {
1109 (void)seteuid(PL_euid);
1110 PL_delaymagic &= ~DM_EUID;
1112 # endif /* HAS_SETEUID */
1113 if (PL_delaymagic & DM_UID) {
1114 if (PL_uid != PL_euid)
1115 DIE(aTHX_ "No setreuid available");
1116 (void)PerlProc_setuid(PL_uid);
1118 # endif /* HAS_SETREUID */
1119 #endif /* HAS_SETRESUID */
1120 PL_uid = PerlProc_getuid();
1121 PL_euid = PerlProc_geteuid();
1123 if (PL_delaymagic & DM_GID) {
1124 #ifdef HAS_SETRESGID
1125 (void)setresgid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1126 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1,
1129 # ifdef HAS_SETREGID
1130 (void)setregid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1131 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1);
1134 if ((PL_delaymagic & DM_GID) == DM_RGID) {
1135 (void)setrgid(PL_gid);
1136 PL_delaymagic &= ~DM_RGID;
1138 # endif /* HAS_SETRGID */
1140 if ((PL_delaymagic & DM_GID) == DM_EGID) {
1141 (void)setegid(PL_egid);
1142 PL_delaymagic &= ~DM_EGID;
1144 # endif /* HAS_SETEGID */
1145 if (PL_delaymagic & DM_GID) {
1146 if (PL_gid != PL_egid)
1147 DIE(aTHX_ "No setregid available");
1148 (void)PerlProc_setgid(PL_gid);
1150 # endif /* HAS_SETREGID */
1151 #endif /* HAS_SETRESGID */
1152 PL_gid = PerlProc_getgid();
1153 PL_egid = PerlProc_getegid();
1155 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1159 if (gimme == G_VOID)
1160 SP = firstrelem - 1;
1161 else if (gimme == G_SCALAR) {
1164 SETi(lastrelem - firstrelem + 1 - duplicates);
1171 /* Removes from the stack the entries which ended up as
1172 * duplicated keys in the hash (fix for [perl #24380]) */
1173 Move(firsthashrelem + duplicates,
1174 firsthashrelem, duplicates, SV**);
1175 lastrelem -= duplicates;
1180 SP = firstrelem + (lastlelem - firstlelem);
1181 lelem = firstlelem + (relem - firstrelem);
1183 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
1191 register PMOP * const pm = cPMOP;
1192 SV * const rv = sv_newmortal();
1193 SV * const sv = newSVrv(rv, "Regexp");
1194 if (pm->op_pmdynflags & PMdf_TAINTED)
1196 sv_magic(sv,(SV*)ReREFCNT_inc(PM_GETRE(pm)), PERL_MAGIC_qr,0,0);
1203 register PMOP *pm = cPMOP;
1205 register const char *t;
1206 register const char *s;
1209 I32 r_flags = REXEC_CHECKED;
1210 const char *truebase; /* Start of string */
1211 register REGEXP *rx = PM_GETRE(pm);
1213 const I32 gimme = GIMME;
1216 const I32 oldsave = PL_savestack_ix;
1217 I32 update_minmatch = 1;
1218 I32 had_zerolen = 0;
1220 if (PL_op->op_flags & OPf_STACKED)
1222 else if (PL_op->op_private & OPpTARGET_MY)
1229 PUTBACK; /* EVAL blocks need stack_sp. */
1230 s = SvPV_const(TARG, len);
1232 DIE(aTHX_ "panic: pp_match");
1234 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1235 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1238 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
1240 /* PMdf_USED is set after a ?? matches once */
1241 if (pm->op_pmdynflags & PMdf_USED) {
1243 if (gimme == G_ARRAY)
1248 /* empty pattern special-cased to use last successful pattern if possible */
1249 if (!rx->prelen && PL_curpm) {
1254 if (rx->minlen > (I32)len)
1259 /* XXXX What part of this is needed with true \G-support? */
1260 if ((global = dynpm->op_pmflags & PMf_GLOBAL)) {
1262 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1263 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1264 if (mg && mg->mg_len >= 0) {
1265 if (!(rx->reganch & ROPT_GPOS_SEEN))
1266 rx->endp[0] = rx->startp[0] = mg->mg_len;
1267 else if (rx->reganch & ROPT_ANCH_GPOS) {
1268 r_flags |= REXEC_IGNOREPOS;
1269 rx->endp[0] = rx->startp[0] = mg->mg_len;
1271 minmatch = (mg->mg_flags & MGf_MINMATCH);
1272 update_minmatch = 0;
1276 if ((!global && rx->nparens)
1277 || SvTEMP(TARG) || PL_sawampersand)
1278 r_flags |= REXEC_COPY_STR;
1280 r_flags |= REXEC_SCREAM;
1283 if (global && rx->startp[0] != -1) {
1284 t = s = rx->endp[0] + truebase;
1285 if ((s + rx->minlen) > strend)
1287 if (update_minmatch++)
1288 minmatch = had_zerolen;
1290 if (rx->reganch & RE_USE_INTUIT &&
1291 DO_UTF8(TARG) == ((rx->reganch & ROPT_UTF8) != 0)) {
1292 /* FIXME - can PL_bostr be made const char *? */
1293 PL_bostr = (char *)truebase;
1294 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, (char *)s, (char *)strend, r_flags, NULL);
1298 if ( (rx->reganch & ROPT_CHECK_ALL)
1300 && ((rx->reganch & ROPT_NOSCAN)
1301 || !((rx->reganch & RE_INTUIT_TAIL)
1302 && (r_flags & REXEC_SCREAM)))
1303 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
1306 if (CALLREGEXEC(aTHX_ rx, (char*)s, (char *)strend, (char*)truebase, minmatch, TARG, NULL, r_flags))
1309 if (dynpm->op_pmflags & PMf_ONCE)
1310 dynpm->op_pmdynflags |= PMdf_USED;
1319 RX_MATCH_TAINTED_on(rx);
1320 TAINT_IF(RX_MATCH_TAINTED(rx));
1321 if (gimme == G_ARRAY) {
1322 const I32 nparens = rx->nparens;
1323 I32 i = (global && !nparens) ? 1 : 0;
1325 SPAGAIN; /* EVAL blocks could move the stack. */
1326 EXTEND(SP, nparens + i);
1327 EXTEND_MORTAL(nparens + i);
1328 for (i = !i; i <= nparens; i++) {
1329 PUSHs(sv_newmortal());
1330 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1331 const I32 len = rx->endp[i] - rx->startp[i];
1332 s = rx->startp[i] + truebase;
1333 if (rx->endp[i] < 0 || rx->startp[i] < 0 ||
1334 len < 0 || len > strend - s)
1335 DIE(aTHX_ "panic: pp_match start/end pointers");
1336 sv_setpvn(*SP, s, len);
1337 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
1342 if (dynpm->op_pmflags & PMf_CONTINUE) {
1344 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1345 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1347 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1348 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1350 if (rx->startp[0] != -1) {
1351 mg->mg_len = rx->endp[0];
1352 if (rx->startp[0] == rx->endp[0])
1353 mg->mg_flags |= MGf_MINMATCH;
1355 mg->mg_flags &= ~MGf_MINMATCH;
1358 had_zerolen = (rx->startp[0] != -1
1359 && rx->startp[0] == rx->endp[0]);
1360 PUTBACK; /* EVAL blocks may use stack */
1361 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
1366 LEAVE_SCOPE(oldsave);
1372 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1373 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1375 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1376 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1378 if (rx->startp[0] != -1) {
1379 mg->mg_len = rx->endp[0];
1380 if (rx->startp[0] == rx->endp[0])
1381 mg->mg_flags |= MGf_MINMATCH;
1383 mg->mg_flags &= ~MGf_MINMATCH;
1386 LEAVE_SCOPE(oldsave);
1390 yup: /* Confirmed by INTUIT */
1392 RX_MATCH_TAINTED_on(rx);
1393 TAINT_IF(RX_MATCH_TAINTED(rx));
1395 if (dynpm->op_pmflags & PMf_ONCE)
1396 dynpm->op_pmdynflags |= PMdf_USED;
1397 if (RX_MATCH_COPIED(rx))
1398 Safefree(rx->subbeg);
1399 RX_MATCH_COPIED_off(rx);
1400 rx->subbeg = Nullch;
1402 /* FIXME - should rx->subbeg be const char *? */
1403 rx->subbeg = (char *) truebase;
1404 rx->startp[0] = s - truebase;
1405 if (RX_MATCH_UTF8(rx)) {
1406 char * const t = (char*)utf8_hop((U8*)s, rx->minlen);
1407 rx->endp[0] = t - truebase;
1410 rx->endp[0] = s - truebase + rx->minlen;
1412 rx->sublen = strend - truebase;
1415 if (PL_sawampersand) {
1417 #ifdef PERL_OLD_COPY_ON_WRITE
1418 if (SvIsCOW(TARG) || (SvFLAGS(TARG) & CAN_COW_MASK) == CAN_COW_FLAGS) {
1420 PerlIO_printf(Perl_debug_log,
1421 "Copy on write: pp_match $& capture, type %d, truebase=%p, t=%p, difference %d\n",
1422 (int) SvTYPE(TARG), truebase, t,
1425 rx->saved_copy = sv_setsv_cow(rx->saved_copy, TARG);
1426 rx->subbeg = (char *) SvPVX_const(rx->saved_copy) + (t - truebase);
1427 assert (SvPOKp(rx->saved_copy));
1432 rx->subbeg = savepvn(t, strend - t);
1433 #ifdef PERL_OLD_COPY_ON_WRITE
1434 rx->saved_copy = Nullsv;
1437 rx->sublen = strend - t;
1438 RX_MATCH_COPIED_on(rx);
1439 off = rx->startp[0] = s - t;
1440 rx->endp[0] = off + rx->minlen;
1442 else { /* startp/endp are used by @- @+. */
1443 rx->startp[0] = s - truebase;
1444 rx->endp[0] = s - truebase + rx->minlen;
1446 rx->nparens = rx->lastparen = rx->lastcloseparen = 0; /* used by @-, @+, and $^N */
1447 LEAVE_SCOPE(oldsave);
1452 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
1453 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1454 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1459 LEAVE_SCOPE(oldsave);
1460 if (gimme == G_ARRAY)
1466 Perl_do_readline(pTHX)
1468 dVAR; dSP; dTARGETSTACKED;
1473 register IO * const io = GvIO(PL_last_in_gv);
1474 register const I32 type = PL_op->op_type;
1475 const I32 gimme = GIMME_V;
1478 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
1480 XPUSHs(SvTIED_obj((SV*)io, mg));
1483 call_method("READLINE", gimme);
1486 if (gimme == G_SCALAR) {
1488 SvSetSV_nosteal(TARG, result);
1497 if (IoFLAGS(io) & IOf_ARGV) {
1498 if (IoFLAGS(io) & IOf_START) {
1500 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1501 IoFLAGS(io) &= ~IOf_START;
1502 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
1503 sv_setpvn(GvSVn(PL_last_in_gv), "-", 1);
1504 SvSETMAGIC(GvSV(PL_last_in_gv));
1509 fp = nextargv(PL_last_in_gv);
1510 if (!fp) { /* Note: fp != IoIFP(io) */
1511 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
1514 else if (type == OP_GLOB)
1515 fp = Perl_start_glob(aTHX_ POPs, io);
1517 else if (type == OP_GLOB)
1519 else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) {
1520 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
1524 if ((!io || !(IoFLAGS(io) & IOf_START))
1525 && ckWARN2(WARN_GLOB, WARN_CLOSED))
1527 if (type == OP_GLOB)
1528 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1529 "glob failed (can't start child: %s)",
1532 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
1534 if (gimme == G_SCALAR) {
1535 /* undef TARG, and push that undefined value */
1536 if (type != OP_RCATLINE) {
1537 SV_CHECK_THINKFIRST_COW_DROP(TARG);
1545 if (gimme == G_SCALAR) {
1549 SvUPGRADE(sv, SVt_PV);
1550 tmplen = SvLEN(sv); /* remember if already alloced */
1551 if (!tmplen && !SvREADONLY(sv))
1552 Sv_Grow(sv, 80); /* try short-buffering it */
1554 if (type == OP_RCATLINE && SvOK(sv)) {
1556 SvPV_force_nolen(sv);
1562 sv = sv_2mortal(NEWSV(57, 80));
1566 /* This should not be marked tainted if the fp is marked clean */
1567 #define MAYBE_TAINT_LINE(io, sv) \
1568 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1573 /* delay EOF state for a snarfed empty file */
1574 #define SNARF_EOF(gimme,rs,io,sv) \
1575 (gimme != G_SCALAR || SvCUR(sv) \
1576 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
1580 if (!sv_gets(sv, fp, offset)
1582 || SNARF_EOF(gimme, PL_rs, io, sv)
1583 || PerlIO_error(fp)))
1585 PerlIO_clearerr(fp);
1586 if (IoFLAGS(io) & IOf_ARGV) {
1587 fp = nextargv(PL_last_in_gv);
1590 (void)do_close(PL_last_in_gv, FALSE);
1592 else if (type == OP_GLOB) {
1593 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1594 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1595 "glob failed (child exited with status %d%s)",
1596 (int)(STATUS_CURRENT >> 8),
1597 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
1600 if (gimme == G_SCALAR) {
1601 if (type != OP_RCATLINE) {
1602 SV_CHECK_THINKFIRST_COW_DROP(TARG);
1608 MAYBE_TAINT_LINE(io, sv);
1611 MAYBE_TAINT_LINE(io, sv);
1613 IoFLAGS(io) |= IOf_NOLINE;
1617 if (type == OP_GLOB) {
1621 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
1622 tmps = SvEND(sv) - 1;
1623 if (*tmps == *SvPVX_const(PL_rs)) {
1625 SvCUR_set(sv, SvCUR(sv) - 1);
1628 for (t1 = SvPVX_const(sv); *t1; t1++)
1629 if (!isALPHA(*t1) && !isDIGIT(*t1) &&
1630 strchr("$&*(){}[]'\";\\|?<>~`", *t1))
1632 if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &PL_statbuf) < 0) {
1633 (void)POPs; /* Unmatched wildcard? Chuck it... */
1636 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
1637 const U8 *s = (const U8*)SvPVX_const(sv) + offset;
1638 const STRLEN len = SvCUR(sv) - offset;
1641 if (ckWARN(WARN_UTF8) &&
1642 !is_utf8_string_loc(s, len, &f))
1643 /* Emulate :encoding(utf8) warning in the same case. */
1644 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1645 "utf8 \"\\x%02X\" does not map to Unicode",
1646 f < (U8*)SvEND(sv) ? *f : 0);
1648 if (gimme == G_ARRAY) {
1649 if (SvLEN(sv) - SvCUR(sv) > 20) {
1650 SvPV_shrink_to_cur(sv);
1652 sv = sv_2mortal(NEWSV(58, 80));
1655 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1656 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1657 const STRLEN new_len
1658 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
1659 SvPV_renew(sv, new_len);
1668 register PERL_CONTEXT *cx;
1669 I32 gimme = OP_GIMME(PL_op, -1);
1672 if (cxstack_ix >= 0)
1673 gimme = cxstack[cxstack_ix].blk_gimme;
1681 PUSHBLOCK(cx, CXt_BLOCK, SP);
1693 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
1694 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
1696 const U32 hash = (SvIsCOW_shared_hash(keysv)) ? SvSHARED_HASH(keysv) : 0;
1699 if (SvTYPE(hv) == SVt_PVHV) {
1700 if (PL_op->op_private & OPpLVAL_INTRO) {
1703 /* does the element we're localizing already exist? */
1705 /* can we determine whether it exists? */
1707 || mg_find((SV*)hv, PERL_MAGIC_env)
1708 || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied))
1709 /* Try to preserve the existenceness of a tied hash
1710 * element by using EXISTS and DELETE if possible.
1711 * Fallback to FETCH and STORE otherwise */
1712 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
1713 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
1714 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
1716 ) ? hv_exists_ent(hv, keysv, 0) : 1;
1719 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
1720 svp = he ? &HeVAL(he) : 0;
1726 if (!svp || *svp == &PL_sv_undef) {
1730 DIE(aTHX_ PL_no_helem_sv, keysv);
1732 lv = sv_newmortal();
1733 sv_upgrade(lv, SVt_PVLV);
1735 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, Nullch, 0);
1736 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1737 LvTARG(lv) = SvREFCNT_inc(hv);
1742 if (PL_op->op_private & OPpLVAL_INTRO) {
1743 if (HvNAME_get(hv) && isGV(*svp))
1744 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1748 const char * const key = SvPV_const(keysv, keylen);
1749 SAVEDELETE(hv, savepvn(key,keylen), keylen);
1751 save_helem(hv, keysv, svp);
1754 else if (PL_op->op_private & OPpDEREF)
1755 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
1757 sv = (svp ? *svp : &PL_sv_undef);
1758 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1759 * Pushing the magical RHS on to the stack is useless, since
1760 * that magic is soon destined to be misled by the local(),
1761 * and thus the later pp_sassign() will fail to mg_get() the
1762 * old value. This should also cure problems with delayed
1763 * mg_get()s. GSAR 98-07-03 */
1764 if (!lval && SvGMAGICAL(sv))
1765 sv = sv_mortalcopy(sv);
1773 register PERL_CONTEXT *cx;
1778 if (PL_op->op_flags & OPf_SPECIAL) {
1779 cx = &cxstack[cxstack_ix];
1780 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
1785 gimme = OP_GIMME(PL_op, -1);
1787 if (cxstack_ix >= 0)
1788 gimme = cxstack[cxstack_ix].blk_gimme;
1794 if (gimme == G_VOID)
1796 else if (gimme == G_SCALAR) {
1800 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1803 *MARK = sv_mortalcopy(TOPs);
1806 *MARK = &PL_sv_undef;
1810 else if (gimme == G_ARRAY) {
1811 /* in case LEAVE wipes old return values */
1813 for (mark = newsp + 1; mark <= SP; mark++) {
1814 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
1815 *mark = sv_mortalcopy(*mark);
1816 TAINT_NOT; /* Each item is independent */
1820 PL_curpm = newpm; /* Don't pop $1 et al till now */
1830 register PERL_CONTEXT *cx;
1836 cx = &cxstack[cxstack_ix];
1837 if (CxTYPE(cx) != CXt_LOOP)
1838 DIE(aTHX_ "panic: pp_iter");
1840 itersvp = CxITERVAR(cx);
1841 av = cx->blk_loop.iterary;
1842 if (SvTYPE(av) != SVt_PVAV) {
1843 /* iterate ($min .. $max) */
1844 if (cx->blk_loop.iterlval) {
1845 /* string increment */
1846 register SV* cur = cx->blk_loop.iterlval;
1848 const char *max = SvOK((SV*)av) ? SvPV_const((SV*)av, maxlen) : "";
1849 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1850 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1851 /* safe to reuse old SV */
1852 sv_setsv(*itersvp, cur);
1856 /* we need a fresh SV every time so that loop body sees a
1857 * completely new SV for closures/references to work as
1860 *itersvp = newSVsv(cur);
1861 SvREFCNT_dec(oldsv);
1863 if (strEQ(SvPVX_const(cur), max))
1864 sv_setiv(cur, 0); /* terminate next time */
1871 /* integer increment */
1872 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1875 /* don't risk potential race */
1876 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1877 /* safe to reuse old SV */
1878 sv_setiv(*itersvp, cx->blk_loop.iterix++);
1882 /* we need a fresh SV every time so that loop body sees a
1883 * completely new SV for closures/references to work as they
1886 *itersvp = newSViv(cx->blk_loop.iterix++);
1887 SvREFCNT_dec(oldsv);
1893 if (PL_op->op_private & OPpITER_REVERSED) {
1894 /* In reverse, use itermax as the min :-) */
1895 if (cx->blk_loop.iterix <= cx->blk_loop.itermax)
1898 if (SvMAGICAL(av) || AvREIFY(av)) {
1899 SV ** const svp = av_fetch(av, cx->blk_loop.iterix--, FALSE);
1900 sv = svp ? *svp : Nullsv;
1903 sv = AvARRAY(av)[cx->blk_loop.iterix--];
1907 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp :
1911 if (SvMAGICAL(av) || AvREIFY(av)) {
1912 SV ** const svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE);
1913 sv = svp ? *svp : Nullsv;
1916 sv = AvARRAY(av)[++cx->blk_loop.iterix];
1920 if (sv && SvIS_FREED(sv)) {
1922 Perl_croak(aTHX_ "Use of freed value in iteration");
1929 if (av != PL_curstack && sv == &PL_sv_undef) {
1930 SV *lv = cx->blk_loop.iterlval;
1931 if (lv && SvREFCNT(lv) > 1) {
1936 SvREFCNT_dec(LvTARG(lv));
1938 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
1939 sv_upgrade(lv, SVt_PVLV);
1941 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
1943 LvTARG(lv) = SvREFCNT_inc(av);
1944 LvTARGOFF(lv) = cx->blk_loop.iterix;
1945 LvTARGLEN(lv) = (STRLEN)UV_MAX;
1950 *itersvp = SvREFCNT_inc(sv);
1951 SvREFCNT_dec(oldsv);
1959 register PMOP *pm = cPMOP;
1975 register REGEXP *rx = PM_GETRE(pm);
1977 int force_on_match = 0;
1978 const I32 oldsave = PL_savestack_ix;
1980 bool doutf8 = FALSE;
1981 #ifdef PERL_OLD_COPY_ON_WRITE
1986 /* known replacement string? */
1987 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
1988 if (PL_op->op_flags & OPf_STACKED)
1990 else if (PL_op->op_private & OPpTARGET_MY)
1997 #ifdef PERL_OLD_COPY_ON_WRITE
1998 /* Awooga. Awooga. "bool" types that are actually char are dangerous,
1999 because they make integers such as 256 "false". */
2000 is_cow = SvIsCOW(TARG) ? TRUE : FALSE;
2003 sv_force_normal_flags(TARG,0);
2006 #ifdef PERL_OLD_COPY_ON_WRITE
2010 || ( (SvTYPE(TARG) == SVt_PVGV || SvTYPE(TARG) > SVt_PVLV)
2011 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
2012 DIE(aTHX_ PL_no_modify);
2015 s = SvPV_mutable(TARG, len);
2016 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
2018 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
2019 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
2024 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
2028 DIE(aTHX_ "panic: pp_subst");
2031 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
2032 maxiters = 2 * slen + 10; /* We can match twice at each
2033 position, once with zero-length,
2034 second time with non-zero. */
2036 if (!rx->prelen && PL_curpm) {
2040 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
2041 ? REXEC_COPY_STR : 0;
2043 r_flags |= REXEC_SCREAM;
2046 if (rx->reganch & RE_USE_INTUIT) {
2048 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
2052 /* How to do it in subst? */
2053 /* if ( (rx->reganch & ROPT_CHECK_ALL)
2055 && ((rx->reganch & ROPT_NOSCAN)
2056 || !((rx->reganch & RE_INTUIT_TAIL)
2057 && (r_flags & REXEC_SCREAM))))
2062 /* only replace once? */
2063 once = !(rpm->op_pmflags & PMf_GLOBAL);
2065 /* known replacement string? */
2067 /* replacement needing upgrading? */
2068 if (DO_UTF8(TARG) && !doutf8) {
2069 nsv = sv_newmortal();
2072 sv_recode_to_utf8(nsv, PL_encoding);
2074 sv_utf8_upgrade(nsv);
2075 c = SvPV_const(nsv, clen);
2079 c = SvPV_const(dstr, clen);
2080 doutf8 = DO_UTF8(dstr);
2088 /* can do inplace substitution? */
2090 #ifdef PERL_OLD_COPY_ON_WRITE
2093 && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
2094 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)
2095 && (!doutf8 || SvUTF8(TARG))) {
2096 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2097 r_flags | REXEC_CHECKED))
2101 LEAVE_SCOPE(oldsave);
2104 #ifdef PERL_OLD_COPY_ON_WRITE
2105 if (SvIsCOW(TARG)) {
2106 assert (!force_on_match);
2110 if (force_on_match) {
2112 s = SvPV_force(TARG, len);
2117 SvSCREAM_off(TARG); /* disable possible screamer */
2119 rxtainted |= RX_MATCH_TAINTED(rx);
2120 m = orig + rx->startp[0];
2121 d = orig + rx->endp[0];
2123 if (m - s > strend - d) { /* faster to shorten from end */
2125 Copy(c, m, clen, char);
2130 Move(d, m, i, char);
2134 SvCUR_set(TARG, m - s);
2136 else if ((i = m - s)) { /* faster from front */
2144 Copy(c, m, clen, char);
2149 Copy(c, d, clen, char);
2154 TAINT_IF(rxtainted & 1);
2160 if (iters++ > maxiters)
2161 DIE(aTHX_ "Substitution loop");
2162 rxtainted |= RX_MATCH_TAINTED(rx);
2163 m = rx->startp[0] + orig;
2166 Move(s, d, i, char);
2170 Copy(c, d, clen, char);
2173 s = rx->endp[0] + orig;
2174 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2176 /* don't match same null twice */
2177 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
2180 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
2181 Move(s, d, i+1, char); /* include the NUL */
2183 TAINT_IF(rxtainted & 1);
2185 PUSHs(sv_2mortal(newSViv((I32)iters)));
2187 (void)SvPOK_only_UTF8(TARG);
2188 TAINT_IF(rxtainted);
2189 if (SvSMAGICAL(TARG)) {
2197 LEAVE_SCOPE(oldsave);
2201 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2202 r_flags | REXEC_CHECKED))
2204 if (force_on_match) {
2206 s = SvPV_force(TARG, len);
2209 #ifdef PERL_OLD_COPY_ON_WRITE
2212 rxtainted |= RX_MATCH_TAINTED(rx);
2213 dstr = newSVpvn(m, s-m);
2218 register PERL_CONTEXT *cx;
2220 (void)ReREFCNT_inc(rx);
2222 RETURNOP(cPMOP->op_pmreplroot);
2224 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
2226 if (iters++ > maxiters)
2227 DIE(aTHX_ "Substitution loop");
2228 rxtainted |= RX_MATCH_TAINTED(rx);
2229 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
2234 strend = s + (strend - m);
2236 m = rx->startp[0] + orig;
2237 if (doutf8 && !SvUTF8(dstr))
2238 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
2240 sv_catpvn(dstr, s, m-s);
2241 s = rx->endp[0] + orig;
2243 sv_catpvn(dstr, c, clen);
2246 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2247 TARG, NULL, r_flags));
2248 if (doutf8 && !DO_UTF8(TARG))
2249 sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv);
2251 sv_catpvn(dstr, s, strend - s);
2253 #ifdef PERL_OLD_COPY_ON_WRITE
2254 /* The match may make the string COW. If so, brilliant, because that's
2255 just saved us one malloc, copy and free - the regexp has donated
2256 the old buffer, and we malloc an entirely new one, rather than the
2257 regexp malloc()ing a buffer and copying our original, only for
2258 us to throw it away here during the substitution. */
2259 if (SvIsCOW(TARG)) {
2260 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
2266 SvPV_set(TARG, SvPVX(dstr));
2267 SvCUR_set(TARG, SvCUR(dstr));
2268 SvLEN_set(TARG, SvLEN(dstr));
2269 doutf8 |= DO_UTF8(dstr);
2270 SvPV_set(dstr, (char*)0);
2273 TAINT_IF(rxtainted & 1);
2275 PUSHs(sv_2mortal(newSViv((I32)iters)));
2277 (void)SvPOK_only(TARG);
2280 TAINT_IF(rxtainted);
2283 LEAVE_SCOPE(oldsave);
2292 LEAVE_SCOPE(oldsave);
2301 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2302 ++*PL_markstack_ptr;
2303 LEAVE; /* exit inner scope */
2306 if (PL_stack_base + *PL_markstack_ptr > SP) {
2308 const I32 gimme = GIMME_V;
2310 LEAVE; /* exit outer scope */
2311 (void)POPMARK; /* pop src */
2312 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
2313 (void)POPMARK; /* pop dst */
2314 SP = PL_stack_base + POPMARK; /* pop original mark */
2315 if (gimme == G_SCALAR) {
2316 if (PL_op->op_private & OPpGREP_LEX) {
2317 SV* const sv = sv_newmortal();
2318 sv_setiv(sv, items);
2326 else if (gimme == G_ARRAY)
2333 ENTER; /* enter inner scope */
2336 src = PL_stack_base[*PL_markstack_ptr];
2338 if (PL_op->op_private & OPpGREP_LEX)
2339 PAD_SVl(PL_op->op_targ) = src;
2343 RETURNOP(cLOGOP->op_other);
2354 register PERL_CONTEXT *cx;
2357 if (CxMULTICALL(&cxstack[cxstack_ix]))
2361 cxstack_ix++; /* temporarily protect top context */
2364 if (gimme == G_SCALAR) {
2367 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2369 *MARK = SvREFCNT_inc(TOPs);
2374 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2376 *MARK = sv_mortalcopy(sv);
2381 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2385 *MARK = &PL_sv_undef;
2389 else if (gimme == G_ARRAY) {
2390 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2391 if (!SvTEMP(*MARK)) {
2392 *MARK = sv_mortalcopy(*MARK);
2393 TAINT_NOT; /* Each item is independent */
2401 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2402 PL_curpm = newpm; /* ... and pop $1 et al */
2405 return cx->blk_sub.retop;
2408 /* This duplicates the above code because the above code must not
2409 * get any slower by more conditions */
2417 register PERL_CONTEXT *cx;
2420 if (CxMULTICALL(&cxstack[cxstack_ix]))
2424 cxstack_ix++; /* temporarily protect top context */
2428 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2429 /* We are an argument to a function or grep().
2430 * This kind of lvalueness was legal before lvalue
2431 * subroutines too, so be backward compatible:
2432 * cannot report errors. */
2434 /* Scalar context *is* possible, on the LHS of -> only,
2435 * as in f()->meth(). But this is not an lvalue. */
2436 if (gimme == G_SCALAR)
2438 if (gimme == G_ARRAY) {
2439 if (!CvLVALUE(cx->blk_sub.cv))
2440 goto temporise_array;
2441 EXTEND_MORTAL(SP - newsp);
2442 for (mark = newsp + 1; mark <= SP; mark++) {
2445 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2446 *mark = sv_mortalcopy(*mark);
2448 /* Can be a localized value subject to deletion. */
2449 PL_tmps_stack[++PL_tmps_ix] = *mark;
2450 (void)SvREFCNT_inc(*mark);
2455 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2456 /* Here we go for robustness, not for speed, so we change all
2457 * the refcounts so the caller gets a live guy. Cannot set
2458 * TEMP, so sv_2mortal is out of question. */
2459 if (!CvLVALUE(cx->blk_sub.cv)) {
2465 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2467 if (gimme == G_SCALAR) {
2471 /* Temporaries are bad unless they happen to be elements
2472 * of a tied hash or array */
2473 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY) &&
2474 !(SvRMAGICAL(TOPs) && mg_find(TOPs, PERL_MAGIC_tiedelem))) {
2480 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2481 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2482 : "a readonly value" : "a temporary");
2484 else { /* Can be a localized value
2485 * subject to deletion. */
2486 PL_tmps_stack[++PL_tmps_ix] = *mark;
2487 (void)SvREFCNT_inc(*mark);
2490 else { /* Should not happen? */
2496 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
2497 (MARK > SP ? "Empty array" : "Array"));
2501 else if (gimme == G_ARRAY) {
2502 EXTEND_MORTAL(SP - newsp);
2503 for (mark = newsp + 1; mark <= SP; mark++) {
2504 if (*mark != &PL_sv_undef
2505 && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2506 /* Might be flattened array after $#array = */
2513 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2514 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2517 /* Can be a localized value subject to deletion. */
2518 PL_tmps_stack[++PL_tmps_ix] = *mark;
2519 (void)SvREFCNT_inc(*mark);
2525 if (gimme == G_SCALAR) {
2529 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2531 *MARK = SvREFCNT_inc(TOPs);
2536 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2538 *MARK = sv_mortalcopy(sv);
2543 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2547 *MARK = &PL_sv_undef;
2551 else if (gimme == G_ARRAY) {
2553 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2554 if (!SvTEMP(*MARK)) {
2555 *MARK = sv_mortalcopy(*MARK);
2556 TAINT_NOT; /* Each item is independent */
2565 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2566 PL_curpm = newpm; /* ... and pop $1 et al */
2569 return cx->blk_sub.retop;
2574 S_get_db_sub(pTHX_ SV **svp, CV *cv)
2576 SV * const dbsv = GvSVn(PL_DBsub);
2579 if (!PERLDB_SUB_NN) {
2582 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2583 || strEQ(GvNAME(gv), "END")
2584 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2585 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2586 && (gv = (GV*)*svp) ))) {
2587 /* Use GV from the stack as a fallback. */
2588 /* GV is potentially non-unique, or contain different CV. */
2589 SV * const tmp = newRV((SV*)cv);
2590 sv_setsv(dbsv, tmp);
2594 gv_efullname3(dbsv, gv, Nullch);
2598 const int type = SvTYPE(dbsv);
2599 if (type < SVt_PVIV && type != SVt_IV)
2600 sv_upgrade(dbsv, SVt_PVIV);
2601 (void)SvIOK_on(dbsv);
2602 SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */
2606 PL_curcopdb = PL_curcop;
2607 cv = GvCV(PL_DBsub);
2617 register PERL_CONTEXT *cx;
2619 const bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
2622 DIE(aTHX_ "Not a CODE reference");
2623 switch (SvTYPE(sv)) {
2624 /* This is overwhelming the most common case: */
2626 if (!(cv = GvCVu((GV*)sv)))
2627 cv = sv_2cv(sv, &stash, &gv, FALSE);
2637 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2639 SP = PL_stack_base + POPMARK;
2642 if (SvGMAGICAL(sv)) {
2646 sym = SvPOKp(sv) ? SvPVX_const(sv) : Nullch;
2649 sym = SvPV_nolen_const(sv);
2652 DIE(aTHX_ PL_no_usym, "a subroutine");
2653 if (PL_op->op_private & HINT_STRICT_REFS)
2654 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
2655 cv = get_cv(sym, TRUE);
2660 SV * const * sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2661 tryAMAGICunDEREF(to_cv);
2664 if (SvTYPE(cv) == SVt_PVCV)
2669 DIE(aTHX_ "Not a CODE reference");
2670 /* This is the second most common case: */
2680 if (!CvROOT(cv) && !CvXSUB(cv)) {
2685 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
2686 if (CvASSERTION(cv) && PL_DBassertion)
2687 sv_setiv(PL_DBassertion, 1);
2689 cv = get_db_sub(&sv, cv);
2690 if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
2691 DIE(aTHX_ "No DB::sub routine defined");
2694 if (!(CvXSUB(cv))) {
2695 /* This path taken at least 75% of the time */
2697 register I32 items = SP - MARK;
2698 AV* const padlist = CvPADLIST(cv);
2699 PUSHBLOCK(cx, CXt_SUB, MARK);
2701 cx->blk_sub.retop = PL_op->op_next;
2703 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2704 * that eval'' ops within this sub know the correct lexical space.
2705 * Owing the speed considerations, we choose instead to search for
2706 * the cv using find_runcv() when calling doeval().
2708 if (CvDEPTH(cv) >= 2) {
2709 PERL_STACK_OVERFLOW_CHECK();
2710 pad_push(padlist, CvDEPTH(cv));
2713 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2716 AV* const av = (AV*)PAD_SVl(0);
2718 /* @_ is normally not REAL--this should only ever
2719 * happen when DB::sub() calls things that modify @_ */
2724 cx->blk_sub.savearray = GvAV(PL_defgv);
2725 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2726 CX_CURPAD_SAVE(cx->blk_sub);
2727 cx->blk_sub.argarray = av;
2730 if (items > AvMAX(av) + 1) {
2731 SV **ary = AvALLOC(av);
2732 if (AvARRAY(av) != ary) {
2733 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2734 SvPV_set(av, (char*)ary);
2736 if (items > AvMAX(av) + 1) {
2737 AvMAX(av) = items - 1;
2738 Renew(ary,items,SV*);
2740 SvPV_set(av, (char*)ary);
2743 Copy(MARK,AvARRAY(av),items,SV*);
2744 AvFILLp(av) = items - 1;
2752 /* warning must come *after* we fully set up the context
2753 * stuff so that __WARN__ handlers can safely dounwind()
2756 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2757 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2758 sub_crush_depth(cv);
2760 DEBUG_S(PerlIO_printf(Perl_debug_log,
2761 "%p entersub returning %p\n", thr, CvSTART(cv)));
2763 RETURNOP(CvSTART(cv));
2766 #ifdef PERL_XSUB_OLDSTYLE
2767 if (CvOLDSTYLE(cv)) {
2768 I32 (*fp3)(int,int,int);
2770 register I32 items = SP - MARK;
2771 /* We dont worry to copy from @_. */
2776 PL_stack_sp = mark + 1;
2777 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2778 items = (*fp3)(CvXSUBANY(cv).any_i32,
2779 MARK - PL_stack_base + 1,
2781 PL_stack_sp = PL_stack_base + items;
2784 #endif /* PERL_XSUB_OLDSTYLE */
2786 I32 markix = TOPMARK;
2791 /* Need to copy @_ to stack. Alternative may be to
2792 * switch stack to @_, and copy return values
2793 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2794 AV * const av = GvAV(PL_defgv);
2795 const I32 items = AvFILLp(av) + 1; /* @_ is not tieable */
2798 /* Mark is at the end of the stack. */
2800 Copy(AvARRAY(av), SP + 1, items, SV*);
2805 /* We assume first XSUB in &DB::sub is the called one. */
2807 SAVEVPTR(PL_curcop);
2808 PL_curcop = PL_curcopdb;
2811 /* Do we need to open block here? XXXX */
2812 (void)(*CvXSUB(cv))(aTHX_ cv);
2814 /* Enforce some sanity in scalar context. */
2815 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2816 if (markix > PL_stack_sp - PL_stack_base)
2817 *(PL_stack_base + markix) = &PL_sv_undef;
2819 *(PL_stack_base + markix) = *PL_stack_sp;
2820 PL_stack_sp = PL_stack_base + markix;
2828 assert (0); /* Cannot get here. */
2829 /* This is deliberately moved here as spaghetti code to keep it out of the
2836 /* anonymous or undef'd function leaves us no recourse */
2837 if (CvANON(cv) || !(gv = CvGV(cv)))
2838 DIE(aTHX_ "Undefined subroutine called");
2840 /* autoloaded stub? */
2841 if (cv != GvCV(gv)) {
2844 /* should call AUTOLOAD now? */
2847 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2854 sub_name = sv_newmortal();
2855 gv_efullname3(sub_name, gv, Nullch);
2856 DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name);
2860 DIE(aTHX_ "Not a CODE reference");
2866 Perl_sub_crush_depth(pTHX_ CV *cv)
2869 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
2871 SV* const tmpstr = sv_newmortal();
2872 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2873 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
2882 SV* const elemsv = POPs;
2883 IV elem = SvIV(elemsv);
2884 AV* const av = (AV*)POPs;
2885 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2886 const U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > av_len(av));
2889 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
2890 Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv);
2892 elem -= PL_curcop->cop_arybase;
2893 if (SvTYPE(av) != SVt_PVAV)
2895 svp = av_fetch(av, elem, lval && !defer);
2897 #ifdef PERL_MALLOC_WRAP
2898 if (SvUOK(elemsv)) {
2899 const UV uv = SvUV(elemsv);
2900 elem = uv > IV_MAX ? IV_MAX : uv;
2902 else if (SvNOK(elemsv))
2903 elem = (IV)SvNV(elemsv);
2905 static const char oom_array_extend[] =
2906 "Out of memory during array extend"; /* Duplicated in av.c */
2907 MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
2910 if (!svp || *svp == &PL_sv_undef) {
2913 DIE(aTHX_ PL_no_aelem, elem);
2914 lv = sv_newmortal();
2915 sv_upgrade(lv, SVt_PVLV);
2917 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
2918 LvTARG(lv) = SvREFCNT_inc(av);
2919 LvTARGOFF(lv) = elem;
2924 if (PL_op->op_private & OPpLVAL_INTRO)
2925 save_aelem(av, elem, svp);
2926 else if (PL_op->op_private & OPpDEREF)
2927 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
2929 sv = (svp ? *svp : &PL_sv_undef);
2930 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2931 sv = sv_mortalcopy(sv);
2937 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
2942 Perl_croak(aTHX_ PL_no_modify);
2943 if (SvTYPE(sv) < SVt_RV)
2944 sv_upgrade(sv, SVt_RV);
2945 else if (SvTYPE(sv) >= SVt_PV) {
2952 SvRV_set(sv, NEWSV(355,0));
2955 SvRV_set(sv, (SV*)newAV());
2958 SvRV_set(sv, (SV*)newHV());
2969 SV* const sv = TOPs;
2972 SV* const rsv = SvRV(sv);
2973 if (SvTYPE(rsv) == SVt_PVCV) {
2979 SETs(method_common(sv, Null(U32*)));
2986 SV* const sv = cSVOP_sv;
2987 U32 hash = SvSHARED_HASH(sv);
2989 XPUSHs(method_common(sv, &hash));
2994 S_method_common(pTHX_ SV* meth, U32* hashp)
3000 const char* packname = Nullch;
3001 SV *packsv = Nullsv;
3003 const char * const name = SvPV_const(meth, namelen);
3004 SV * const sv = *(PL_stack_base + TOPMARK + 1);
3007 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
3015 /* this isn't a reference */
3016 if(SvOK(sv) && (packname = SvPV_const(sv, packlen))) {
3017 const HE* const he = hv_fetch_ent(PL_stashcache, sv, 0, 0);
3019 stash = INT2PTR(HV*,SvIV(HeVAL(he)));
3026 !(iogv = gv_fetchsv(sv, FALSE, SVt_PVIO)) ||
3027 !(ob=(SV*)GvIO(iogv)))
3029 /* this isn't the name of a filehandle either */
3031 ((UTF8_IS_START(*packname) && DO_UTF8(sv))
3032 ? !isIDFIRST_utf8((U8*)packname)
3033 : !isIDFIRST(*packname)
3036 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
3037 SvOK(sv) ? "without a package or object reference"
3038 : "on an undefined value");
3040 /* assume it's a package name */
3041 stash = gv_stashpvn(packname, packlen, FALSE);
3045 SV* ref = newSViv(PTR2IV(stash));
3046 hv_store(PL_stashcache, packname, packlen, ref, 0);
3050 /* it _is_ a filehandle name -- replace with a reference */
3051 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
3054 /* if we got here, ob should be a reference or a glob */
3055 if (!ob || !(SvOBJECT(ob)
3056 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
3059 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
3063 stash = SvSTASH(ob);
3066 /* NOTE: stash may be null, hope hv_fetch_ent and
3067 gv_fetchmethod can cope (it seems they can) */
3069 /* shortcut for simple names */
3071 const HE* const he = hv_fetch_ent(stash, meth, 0, *hashp);
3073 gv = (GV*)HeVAL(he);
3074 if (isGV(gv) && GvCV(gv) &&
3075 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
3076 return (SV*)GvCV(gv);
3080 gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name);
3083 /* This code tries to figure out just what went wrong with
3084 gv_fetchmethod. It therefore needs to duplicate a lot of
3085 the internals of that function. We can't move it inside
3086 Perl_gv_fetchmethod_autoload(), however, since that would
3087 cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we
3090 const char* leaf = name;
3091 const char* sep = Nullch;
3094 for (p = name; *p; p++) {
3096 sep = p, leaf = p + 1;
3097 else if (*p == ':' && *(p + 1) == ':')
3098 sep = p, leaf = p + 2;
3100 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3101 /* the method name is unqualified or starts with SUPER:: */
3102 bool need_strlen = 1;
3104 packname = CopSTASHPV(PL_curcop);
3107 HEK * const packhek = HvNAME_HEK(stash);
3109 packname = HEK_KEY(packhek);
3110 packlen = HEK_LEN(packhek);
3120 "Can't use anonymous symbol table for method lookup");
3122 else if (need_strlen)
3123 packlen = strlen(packname);
3127 /* the method name is qualified */
3129 packlen = sep - name;
3132 /* we're relying on gv_fetchmethod not autovivifying the stash */
3133 if (gv_stashpvn(packname, packlen, FALSE)) {
3135 "Can't locate object method \"%s\" via package \"%.*s\"",
3136 leaf, (int)packlen, packname);
3140 "Can't locate object method \"%s\" via package \"%.*s\""
3141 " (perhaps you forgot to load \"%.*s\"?)",
3142 leaf, (int)packlen, packname, (int)packlen, packname);
3145 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
3150 * c-indentation-style: bsd
3152 * indent-tabs-mode: t
3155 * ex: set ts=8 sts=4 sw=4 noet: