3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 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(GvSV(cGVOP_gv));
72 PL_curcop = (COP*)PL_op;
78 PUSHMARK(PL_stack_sp);
93 XPUSHs((SV*)cGVOP_gv);
104 RETURNOP(cLOGOP->op_other);
112 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
114 temp = left; left = right; right = temp;
116 if (PL_tainting && PL_tainted && !SvTAINTED(left))
118 SvSetMagicSV(right, left);
127 RETURNOP(cLOGOP->op_other);
129 RETURNOP(cLOGOP->op_next);
135 TAINT_NOT; /* Each statement is presumed innocent */
136 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
138 oldsave = PL_scopestack[PL_scopestack_ix - 1];
139 LEAVE_SCOPE(oldsave);
145 dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
152 char* rpv = SvPV(right, rlen); /* mg_get(right) happens here */
153 bool rbyte = !DO_UTF8(right), rcopied = FALSE;
155 if (TARG == right && right != left) {
156 right = sv_2mortal(newSVpvn(rpv, rlen));
157 rpv = SvPV(right, rlen); /* no point setting UTF-8 here */
162 lpv = SvPV(left, llen); /* mg_get(left) may happen here */
163 lbyte = !DO_UTF8(left);
164 sv_setpvn(TARG, lpv, llen);
170 else { /* TARG == left */
171 if (SvGMAGICAL(left))
172 mg_get(left); /* or mg_get(left) may happen here */
175 lpv = SvPV_nomg(left, llen);
176 lbyte = !DO_UTF8(left);
181 #if defined(PERL_Y2KWARN)
182 if ((SvIOK(right) || SvNOK(right)) && ckWARN(WARN_Y2K) && SvOK(TARG)) {
183 if (llen >= 2 && lpv[llen - 2] == '1' && lpv[llen - 1] == '9'
184 && (llen == 2 || !isDIGIT(lpv[llen - 3])))
186 Perl_warner(aTHX_ packWARN(WARN_Y2K), "Possible Y2K bug: %s",
187 "about to append an integer to '19'");
192 if (lbyte != rbyte) {
194 sv_utf8_upgrade_nomg(TARG);
197 right = sv_2mortal(newSVpvn(rpv, rlen));
198 sv_utf8_upgrade_nomg(right);
199 rpv = SvPV(right, rlen);
202 sv_catpvn_nomg(TARG, rpv, rlen);
213 if (PL_op->op_flags & OPf_MOD) {
214 if (PL_op->op_private & OPpLVAL_INTRO)
215 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
216 if (PL_op->op_private & OPpDEREF) {
218 vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
227 tryAMAGICunTARGET(iter, 0);
228 PL_last_in_gv = (GV*)(*PL_stack_sp--);
229 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
230 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
231 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
234 XPUSHs((SV*)PL_last_in_gv);
237 PL_last_in_gv = (GV*)(*PL_stack_sp--);
240 return do_readline();
245 dSP; tryAMAGICbinSET(eq,0);
246 #ifndef NV_PRESERVES_UV
247 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
249 SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s)));
253 #ifdef PERL_PRESERVE_IVUV
256 /* Unless the left argument is integer in range we are going
257 to have to use NV maths. Hence only attempt to coerce the
258 right argument if we know the left is integer. */
261 bool auvok = SvUOK(TOPm1s);
262 bool buvok = SvUOK(TOPs);
264 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
265 /* Casting IV to UV before comparison isn't going to matter
266 on 2s complement. On 1s complement or sign&magnitude
267 (if we have any of them) it could to make negative zero
268 differ from normal zero. As I understand it. (Need to
269 check - is negative zero implementation defined behaviour
271 UV buv = SvUVX(POPs);
272 UV auv = SvUVX(TOPs);
274 SETs(boolSV(auv == buv));
277 { /* ## Mixed IV,UV ## */
281 /* == is commutative so doesn't matter which is left or right */
283 /* top of stack (b) is the iv */
292 /* As uv is a UV, it's >0, so it cannot be == */
296 /* we know iv is >= 0 */
297 SETs(boolSV((UV)iv == SvUVX(uvp)));
305 SETs(boolSV(TOPn == value));
313 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
314 DIE(aTHX_ PL_no_modify);
315 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
316 && SvIVX(TOPs) != IV_MAX)
319 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
321 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
334 RETURNOP(cLOGOP->op_other);
340 /* Most of this is lifted straight from pp_defined */
345 if (!sv || !SvANY(sv)) {
347 RETURNOP(cLOGOP->op_other);
350 switch (SvTYPE(sv)) {
352 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
356 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
360 if (CvROOT(sv) || CvXSUB(sv))
371 RETURNOP(cLOGOP->op_other);
376 dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
377 useleft = USE_LEFT(TOPm1s);
378 #ifdef PERL_PRESERVE_IVUV
379 /* We must see if we can perform the addition with integers if possible,
380 as the integer code detects overflow while the NV code doesn't.
381 If either argument hasn't had a numeric conversion yet attempt to get
382 the IV. It's important to do this now, rather than just assuming that
383 it's not IOK as a PV of "9223372036854775806" may not take well to NV
384 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
385 integer in case the second argument is IV=9223372036854775806
386 We can (now) rely on sv_2iv to do the right thing, only setting the
387 public IOK flag if the value in the NV (or PV) slot is truly integer.
389 A side effect is that this also aggressively prefers integer maths over
390 fp maths for integer values.
392 How to detect overflow?
394 C 99 section 6.2.6.1 says
396 The range of nonnegative values of a signed integer type is a subrange
397 of the corresponding unsigned integer type, and the representation of
398 the same value in each type is the same. A computation involving
399 unsigned operands can never overflow, because a result that cannot be
400 represented by the resulting unsigned integer type is reduced modulo
401 the number that is one greater than the largest value that can be
402 represented by the resulting type.
406 which I read as "unsigned ints wrap."
408 signed integer overflow seems to be classed as "exception condition"
410 If an exceptional condition occurs during the evaluation of an
411 expression (that is, if the result is not mathematically defined or not
412 in the range of representable values for its type), the behavior is
415 (6.5, the 5th paragraph)
417 I had assumed that on 2s complement machines signed arithmetic would
418 wrap, hence coded pp_add and pp_subtract on the assumption that
419 everything perl builds on would be happy. After much wailing and
420 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
421 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
422 unsigned code below is actually shorter than the old code. :-)
427 /* Unless the left argument is integer in range we are going to have to
428 use NV maths. Hence only attempt to coerce the right argument if
429 we know the left is integer. */
437 /* left operand is undef, treat as zero. + 0 is identity,
438 Could SETi or SETu right now, but space optimise by not adding
439 lots of code to speed up what is probably a rarish case. */
441 /* Left operand is defined, so is it IV? */
444 if ((auvok = SvUOK(TOPm1s)))
447 register IV aiv = SvIVX(TOPm1s);
450 auvok = 1; /* Now acting as a sign flag. */
451 } else { /* 2s complement assumption for IV_MIN */
459 bool result_good = 0;
462 bool buvok = SvUOK(TOPs);
467 register IV biv = SvIVX(TOPs);
474 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
475 else "IV" now, independent of how it came in.
476 if a, b represents positive, A, B negative, a maps to -A etc
481 all UV maths. negate result if A negative.
482 add if signs same, subtract if signs differ. */
488 /* Must get smaller */
494 /* result really should be -(auv-buv). as its negation
495 of true value, need to swap our result flag */
512 if (result <= (UV)IV_MIN)
515 /* result valid, but out of range for IV. */
520 } /* Overflow, drop through to NVs. */
527 /* left operand is undef, treat as zero. + 0.0 is identity. */
531 SETn( value + TOPn );
539 AV *av = PL_op->op_flags & OPf_SPECIAL ?
540 (AV*)PAD_SV(PL_op->op_targ) : GvAV(cGVOP_gv);
541 U32 lval = PL_op->op_flags & OPf_MOD;
542 SV** svp = av_fetch(av, PL_op->op_private, lval);
543 SV *sv = (svp ? *svp : &PL_sv_undef);
545 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
546 sv = sv_mortalcopy(sv);
555 do_join(TARG, *MARK, MARK, SP);
566 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
567 * will be enough to hold an OP*.
569 SV* sv = sv_newmortal();
570 sv_upgrade(sv, SVt_PVLV);
572 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
580 /* Oversized hot code. */
584 dSP; dMARK; dORIGMARK;
590 if (PL_op->op_flags & OPf_STACKED)
595 if (gv && (io = GvIO(gv))
596 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
599 if (MARK == ORIGMARK) {
600 /* If using default handle then we need to make space to
601 * pass object as 1st arg, so move other args up ...
605 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
609 *MARK = SvTIED_obj((SV*)io, mg);
612 call_method("PRINT", G_SCALAR);
620 if (!(io = GvIO(gv))) {
621 if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv)))
622 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
624 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
625 report_evil_fh(gv, io, PL_op->op_type);
626 SETERRNO(EBADF,RMS_IFI);
629 else if (!(fp = IoOFP(io))) {
630 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
632 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
633 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
634 report_evil_fh(gv, io, PL_op->op_type);
636 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
641 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
643 if (!do_print(*MARK, fp))
647 if (!do_print(PL_ofs_sv, fp)) { /* $, */
656 if (!do_print(*MARK, fp))
664 if (PL_ors_sv && SvOK(PL_ors_sv))
665 if (!do_print(PL_ors_sv, fp)) /* $\ */
668 if (IoFLAGS(io) & IOf_FLUSH)
669 if (PerlIO_flush(fp) == EOF)
690 tryAMAGICunDEREF(to_av);
693 if (SvTYPE(av) != SVt_PVAV)
694 DIE(aTHX_ "Not an ARRAY reference");
695 if (PL_op->op_flags & OPf_REF) {
700 if (GIMME == G_SCALAR)
701 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
705 else if (PL_op->op_flags & OPf_MOD
706 && PL_op->op_private & OPpLVAL_INTRO)
707 Perl_croak(aTHX_ PL_no_localize_ref);
710 if (SvTYPE(sv) == SVt_PVAV) {
712 if (PL_op->op_flags & OPf_REF) {
717 if (GIMME == G_SCALAR)
718 Perl_croak(aTHX_ "Can't return array to lvalue"
727 if (SvTYPE(sv) != SVt_PVGV) {
728 if (SvGMAGICAL(sv)) {
734 if (PL_op->op_flags & OPf_REF ||
735 PL_op->op_private & HINT_STRICT_REFS)
736 DIE(aTHX_ PL_no_usym, "an ARRAY");
737 if (ckWARN(WARN_UNINITIALIZED))
739 if (GIMME == G_ARRAY) {
745 if ((PL_op->op_flags & OPf_SPECIAL) &&
746 !(PL_op->op_flags & OPf_MOD))
748 gv = (GV*)gv_fetchsv(sv, FALSE, SVt_PVAV);
750 && (!is_gv_magical_sv(sv,0)
751 || !(gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVAV))))
757 if (PL_op->op_private & HINT_STRICT_REFS)
758 DIE(aTHX_ PL_no_symref_sv, sv, "an ARRAY");
759 gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVAV);
766 if (PL_op->op_private & OPpLVAL_INTRO)
768 if (PL_op->op_flags & OPf_REF) {
773 if (GIMME == G_SCALAR)
774 Perl_croak(aTHX_ "Can't return array to lvalue"
782 if (GIMME == G_ARRAY) {
783 I32 maxarg = AvFILL(av) + 1;
784 (void)POPs; /* XXXX May be optimized away? */
786 if (SvRMAGICAL(av)) {
788 for (i=0; i < (U32)maxarg; i++) {
789 SV **svp = av_fetch(av, i, FALSE);
790 /* See note in pp_helem, and bug id #27839 */
792 ? SvGMAGICAL(*svp) ? sv_mortalcopy(*svp) : *svp
797 Copy(AvARRAY(av), SP+1, maxarg, SV*);
801 else if (GIMME_V == G_SCALAR) {
803 I32 maxarg = AvFILL(av) + 1;
817 tryAMAGICunDEREF(to_hv);
820 if (SvTYPE(hv) != SVt_PVHV)
821 DIE(aTHX_ "Not a HASH reference");
822 if (PL_op->op_flags & OPf_REF) {
827 if (gimme != G_ARRAY)
828 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
832 else if (PL_op->op_flags & OPf_MOD
833 && PL_op->op_private & OPpLVAL_INTRO)
834 Perl_croak(aTHX_ PL_no_localize_ref);
837 if (SvTYPE(sv) == SVt_PVHV) {
839 if (PL_op->op_flags & OPf_REF) {
844 if (gimme != G_ARRAY)
845 Perl_croak(aTHX_ "Can't return hash to lvalue"
854 if (SvTYPE(sv) != SVt_PVGV) {
855 if (SvGMAGICAL(sv)) {
861 if (PL_op->op_flags & OPf_REF ||
862 PL_op->op_private & HINT_STRICT_REFS)
863 DIE(aTHX_ PL_no_usym, "a HASH");
864 if (ckWARN(WARN_UNINITIALIZED))
866 if (gimme == G_ARRAY) {
872 if ((PL_op->op_flags & OPf_SPECIAL) &&
873 !(PL_op->op_flags & OPf_MOD))
875 gv = (GV*)gv_fetchsv(sv, FALSE, SVt_PVHV);
877 && (!is_gv_magical_sv(sv,0)
878 || !(gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVHV))))
884 if (PL_op->op_private & HINT_STRICT_REFS)
885 DIE(aTHX_ PL_no_symref_sv, sv, "a HASH");
886 gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVHV);
893 if (PL_op->op_private & OPpLVAL_INTRO)
895 if (PL_op->op_flags & OPf_REF) {
900 if (gimme != G_ARRAY)
901 Perl_croak(aTHX_ "Can't return hash to lvalue"
909 if (gimme == G_ARRAY) { /* array wanted */
910 *PL_stack_sp = (SV*)hv;
913 else if (gimme == G_SCALAR) {
915 TARG = Perl_hv_scalar(aTHX_ hv);
922 S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
928 if (ckWARN(WARN_MISC)) {
929 if (relem == firstrelem &&
931 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
932 SvTYPE(SvRV(*relem)) == SVt_PVHV))
934 Perl_warner(aTHX_ packWARN(WARN_MISC),
935 "Reference found where even-sized list expected");
938 Perl_warner(aTHX_ packWARN(WARN_MISC),
939 "Odd number of elements in hash assignment");
942 tmpstr = NEWSV(29,0);
943 didstore = hv_store_ent(hash,*relem,tmpstr,0);
944 if (SvMAGICAL(hash)) {
945 if (SvSMAGICAL(tmpstr))
957 SV **lastlelem = PL_stack_sp;
958 SV **lastrelem = PL_stack_base + POPMARK;
959 SV **firstrelem = PL_stack_base + POPMARK + 1;
960 SV **firstlelem = lastrelem + 1;
973 SV **firsthashrelem = 0; /* "= 0" keeps gcc 2.95 quiet */
976 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
979 /* If there's a common identifier on both sides we have to take
980 * special care that assigning the identifier on the left doesn't
981 * clobber a value on the right that's used later in the list.
983 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
984 EXTEND_MORTAL(lastrelem - firstrelem + 1);
985 for (relem = firstrelem; relem <= lastrelem; relem++) {
988 TAINT_NOT; /* Each item is independent */
989 *relem = sv_mortalcopy(sv);
999 while (lelem <= lastlelem) {
1000 TAINT_NOT; /* Each item stands on its own, taintwise. */
1002 switch (SvTYPE(sv)) {
1005 magic = SvMAGICAL(ary) != 0;
1007 av_extend(ary, lastrelem - relem);
1009 while (relem <= lastrelem) { /* gobble up all the rest */
1013 sv_setsv(sv,*relem);
1015 didstore = av_store(ary,i++,sv);
1025 case SVt_PVHV: { /* normal hash */
1029 magic = SvMAGICAL(hash) != 0;
1031 firsthashrelem = relem;
1033 while (relem < lastrelem) { /* gobble up all the rest */
1038 sv = &PL_sv_no, relem++;
1039 tmpstr = NEWSV(29,0);
1041 sv_setsv(tmpstr,*relem); /* value */
1042 *(relem++) = tmpstr;
1043 if (gimme != G_VOID && hv_exists_ent(hash, sv, 0))
1044 /* key overwrites an existing entry */
1046 didstore = hv_store_ent(hash,sv,tmpstr,0);
1048 if (SvSMAGICAL(tmpstr))
1055 if (relem == lastrelem) {
1056 do_oddball(hash, relem, firstrelem);
1062 if (SvIMMORTAL(sv)) {
1063 if (relem <= lastrelem)
1067 if (relem <= lastrelem) {
1068 sv_setsv(sv, *relem);
1072 sv_setsv(sv, &PL_sv_undef);
1077 if (PL_delaymagic & ~DM_DELAY) {
1078 if (PL_delaymagic & DM_UID) {
1079 #ifdef HAS_SETRESUID
1080 (void)setresuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1081 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1,
1084 # ifdef HAS_SETREUID
1085 (void)setreuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1086 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1);
1089 if ((PL_delaymagic & DM_UID) == DM_RUID) {
1090 (void)setruid(PL_uid);
1091 PL_delaymagic &= ~DM_RUID;
1093 # endif /* HAS_SETRUID */
1095 if ((PL_delaymagic & DM_UID) == DM_EUID) {
1096 (void)seteuid(PL_euid);
1097 PL_delaymagic &= ~DM_EUID;
1099 # endif /* HAS_SETEUID */
1100 if (PL_delaymagic & DM_UID) {
1101 if (PL_uid != PL_euid)
1102 DIE(aTHX_ "No setreuid available");
1103 (void)PerlProc_setuid(PL_uid);
1105 # endif /* HAS_SETREUID */
1106 #endif /* HAS_SETRESUID */
1107 PL_uid = PerlProc_getuid();
1108 PL_euid = PerlProc_geteuid();
1110 if (PL_delaymagic & DM_GID) {
1111 #ifdef HAS_SETRESGID
1112 (void)setresgid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1113 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1,
1116 # ifdef HAS_SETREGID
1117 (void)setregid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1118 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1);
1121 if ((PL_delaymagic & DM_GID) == DM_RGID) {
1122 (void)setrgid(PL_gid);
1123 PL_delaymagic &= ~DM_RGID;
1125 # endif /* HAS_SETRGID */
1127 if ((PL_delaymagic & DM_GID) == DM_EGID) {
1128 (void)setegid(PL_egid);
1129 PL_delaymagic &= ~DM_EGID;
1131 # endif /* HAS_SETEGID */
1132 if (PL_delaymagic & DM_GID) {
1133 if (PL_gid != PL_egid)
1134 DIE(aTHX_ "No setregid available");
1135 (void)PerlProc_setgid(PL_gid);
1137 # endif /* HAS_SETREGID */
1138 #endif /* HAS_SETRESGID */
1139 PL_gid = PerlProc_getgid();
1140 PL_egid = PerlProc_getegid();
1142 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1146 if (gimme == G_VOID)
1147 SP = firstrelem - 1;
1148 else if (gimme == G_SCALAR) {
1151 SETi(lastrelem - firstrelem + 1 - duplicates);
1158 /* Removes from the stack the entries which ended up as
1159 * duplicated keys in the hash (fix for [perl #24380]) */
1160 Move(firsthashrelem + duplicates,
1161 firsthashrelem, duplicates, SV**);
1162 lastrelem -= duplicates;
1167 SP = firstrelem + (lastlelem - firstlelem);
1168 lelem = firstlelem + (relem - firstrelem);
1170 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
1178 register PMOP *pm = cPMOP;
1179 SV *rv = sv_newmortal();
1180 SV *sv = newSVrv(rv, "Regexp");
1181 if (pm->op_pmdynflags & PMdf_TAINTED)
1183 sv_magic(sv,(SV*)ReREFCNT_inc(PM_GETRE(pm)), PERL_MAGIC_qr,0,0);
1190 register PMOP *pm = cPMOP;
1196 I32 r_flags = REXEC_CHECKED;
1197 char *truebase; /* Start of string */
1198 register REGEXP *rx = PM_GETRE(pm);
1203 I32 oldsave = PL_savestack_ix;
1204 I32 update_minmatch = 1;
1205 I32 had_zerolen = 0;
1207 if (PL_op->op_flags & OPf_STACKED)
1209 else if (PL_op->op_private & OPpTARGET_MY)
1216 PUTBACK; /* EVAL blocks need stack_sp. */
1217 s = SvPV(TARG, len);
1220 DIE(aTHX_ "panic: pp_match");
1221 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1222 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1225 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
1227 /* PMdf_USED is set after a ?? matches once */
1228 if (pm->op_pmdynflags & PMdf_USED) {
1230 if (gimme == G_ARRAY)
1235 /* empty pattern special-cased to use last successful pattern if possible */
1236 if (!rx->prelen && PL_curpm) {
1241 if (rx->minlen > (I32)len)
1246 /* XXXX What part of this is needed with true \G-support? */
1247 if ((global = dynpm->op_pmflags & PMf_GLOBAL)) {
1249 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1250 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1251 if (mg && mg->mg_len >= 0) {
1252 if (!(rx->reganch & ROPT_GPOS_SEEN))
1253 rx->endp[0] = rx->startp[0] = mg->mg_len;
1254 else if (rx->reganch & ROPT_ANCH_GPOS) {
1255 r_flags |= REXEC_IGNOREPOS;
1256 rx->endp[0] = rx->startp[0] = mg->mg_len;
1258 minmatch = (mg->mg_flags & MGf_MINMATCH);
1259 update_minmatch = 0;
1263 if ((!global && rx->nparens)
1264 || SvTEMP(TARG) || PL_sawampersand)
1265 r_flags |= REXEC_COPY_STR;
1267 r_flags |= REXEC_SCREAM;
1270 if (global && rx->startp[0] != -1) {
1271 t = s = rx->endp[0] + truebase;
1272 if ((s + rx->minlen) > strend)
1274 if (update_minmatch++)
1275 minmatch = had_zerolen;
1277 if (rx->reganch & RE_USE_INTUIT &&
1278 DO_UTF8(TARG) == ((rx->reganch & ROPT_UTF8) != 0)) {
1279 PL_bostr = truebase;
1280 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1284 if ( (rx->reganch & ROPT_CHECK_ALL)
1286 && ((rx->reganch & ROPT_NOSCAN)
1287 || !((rx->reganch & RE_INTUIT_TAIL)
1288 && (r_flags & REXEC_SCREAM)))
1289 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
1292 if (CALLREGEXEC(aTHX_ rx, s, strend, truebase, minmatch, TARG, NULL, r_flags))
1295 if (dynpm->op_pmflags & PMf_ONCE)
1296 dynpm->op_pmdynflags |= PMdf_USED;
1305 RX_MATCH_TAINTED_on(rx);
1306 TAINT_IF(RX_MATCH_TAINTED(rx));
1307 if (gimme == G_ARRAY) {
1308 I32 nparens, i, len;
1310 nparens = rx->nparens;
1311 if (global && !nparens)
1315 SPAGAIN; /* EVAL blocks could move the stack. */
1316 EXTEND(SP, nparens + i);
1317 EXTEND_MORTAL(nparens + i);
1318 for (i = !i; i <= nparens; i++) {
1319 PUSHs(sv_newmortal());
1321 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1322 len = rx->endp[i] - rx->startp[i];
1323 s = rx->startp[i] + truebase;
1324 if (rx->endp[i] < 0 || rx->startp[i] < 0 ||
1325 len < 0 || len > strend - s)
1326 DIE(aTHX_ "panic: pp_match start/end pointers");
1327 sv_setpvn(*SP, s, len);
1328 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
1333 if (dynpm->op_pmflags & PMf_CONTINUE) {
1335 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1336 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1338 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1339 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1341 if (rx->startp[0] != -1) {
1342 mg->mg_len = rx->endp[0];
1343 if (rx->startp[0] == rx->endp[0])
1344 mg->mg_flags |= MGf_MINMATCH;
1346 mg->mg_flags &= ~MGf_MINMATCH;
1349 had_zerolen = (rx->startp[0] != -1
1350 && rx->startp[0] == rx->endp[0]);
1351 PUTBACK; /* EVAL blocks may use stack */
1352 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
1357 LEAVE_SCOPE(oldsave);
1363 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1364 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1366 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1367 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1369 if (rx->startp[0] != -1) {
1370 mg->mg_len = rx->endp[0];
1371 if (rx->startp[0] == rx->endp[0])
1372 mg->mg_flags |= MGf_MINMATCH;
1374 mg->mg_flags &= ~MGf_MINMATCH;
1377 LEAVE_SCOPE(oldsave);
1381 yup: /* Confirmed by INTUIT */
1383 RX_MATCH_TAINTED_on(rx);
1384 TAINT_IF(RX_MATCH_TAINTED(rx));
1386 if (dynpm->op_pmflags & PMf_ONCE)
1387 dynpm->op_pmdynflags |= PMdf_USED;
1388 if (RX_MATCH_COPIED(rx))
1389 Safefree(rx->subbeg);
1390 RX_MATCH_COPIED_off(rx);
1391 rx->subbeg = Nullch;
1393 rx->subbeg = truebase;
1394 rx->startp[0] = s - truebase;
1395 if (RX_MATCH_UTF8(rx)) {
1396 char *t = (char*)utf8_hop((U8*)s, rx->minlen);
1397 rx->endp[0] = t - truebase;
1400 rx->endp[0] = s - truebase + rx->minlen;
1402 rx->sublen = strend - truebase;
1405 if (PL_sawampersand) {
1407 #ifdef PERL_COPY_ON_WRITE
1408 if (SvIsCOW(TARG) || (SvFLAGS(TARG) & CAN_COW_MASK) == CAN_COW_FLAGS) {
1410 PerlIO_printf(Perl_debug_log,
1411 "Copy on write: pp_match $& capture, type %d, truebase=%p, t=%p, difference %d\n",
1412 (int) SvTYPE(TARG), truebase, t,
1415 rx->saved_copy = sv_setsv_cow(rx->saved_copy, TARG);
1416 rx->subbeg = SvPVX(rx->saved_copy) + (t - truebase);
1417 assert (SvPOKp(rx->saved_copy));
1422 rx->subbeg = savepvn(t, strend - t);
1423 #ifdef PERL_COPY_ON_WRITE
1424 rx->saved_copy = Nullsv;
1427 rx->sublen = strend - t;
1428 RX_MATCH_COPIED_on(rx);
1429 off = rx->startp[0] = s - t;
1430 rx->endp[0] = off + rx->minlen;
1432 else { /* startp/endp are used by @- @+. */
1433 rx->startp[0] = s - truebase;
1434 rx->endp[0] = s - truebase + rx->minlen;
1436 rx->nparens = rx->lastparen = rx->lastcloseparen = 0; /* used by @-, @+, and $^N */
1437 LEAVE_SCOPE(oldsave);
1442 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
1443 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1444 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1449 LEAVE_SCOPE(oldsave);
1450 if (gimme == G_ARRAY)
1456 Perl_do_readline(pTHX)
1458 dSP; dTARGETSTACKED;
1463 register IO *io = GvIO(PL_last_in_gv);
1464 register I32 type = PL_op->op_type;
1465 I32 gimme = GIMME_V;
1468 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
1470 XPUSHs(SvTIED_obj((SV*)io, mg));
1473 call_method("READLINE", gimme);
1476 if (gimme == G_SCALAR) {
1478 SvSetSV_nosteal(TARG, result);
1487 if (IoFLAGS(io) & IOf_ARGV) {
1488 if (IoFLAGS(io) & IOf_START) {
1490 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1491 IoFLAGS(io) &= ~IOf_START;
1492 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
1493 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1494 SvSETMAGIC(GvSV(PL_last_in_gv));
1499 fp = nextargv(PL_last_in_gv);
1500 if (!fp) { /* Note: fp != IoIFP(io) */
1501 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
1504 else if (type == OP_GLOB)
1505 fp = Perl_start_glob(aTHX_ POPs, io);
1507 else if (type == OP_GLOB)
1509 else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) {
1510 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
1514 if (ckWARN2(WARN_GLOB, WARN_CLOSED)
1515 && (!io || !(IoFLAGS(io) & IOf_START))) {
1516 if (type == OP_GLOB)
1517 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1518 "glob failed (can't start child: %s)",
1521 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
1523 if (gimme == G_SCALAR) {
1524 /* undef TARG, and push that undefined value */
1525 if (type != OP_RCATLINE) {
1526 SV_CHECK_THINKFIRST_COW_DROP(TARG);
1534 if (gimme == G_SCALAR) {
1538 (void)SvUPGRADE(sv, SVt_PV);
1539 tmplen = SvLEN(sv); /* remember if already alloced */
1540 if (!tmplen && !SvREADONLY(sv))
1541 Sv_Grow(sv, 80); /* try short-buffering it */
1543 if (type == OP_RCATLINE && SvOK(sv)) {
1546 (void)SvPV_force(sv, n_a);
1552 sv = sv_2mortal(NEWSV(57, 80));
1556 /* This should not be marked tainted if the fp is marked clean */
1557 #define MAYBE_TAINT_LINE(io, sv) \
1558 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1563 /* delay EOF state for a snarfed empty file */
1564 #define SNARF_EOF(gimme,rs,io,sv) \
1565 (gimme != G_SCALAR || SvCUR(sv) \
1566 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
1570 if (!sv_gets(sv, fp, offset)
1572 || SNARF_EOF(gimme, PL_rs, io, sv)
1573 || PerlIO_error(fp)))
1575 PerlIO_clearerr(fp);
1576 if (IoFLAGS(io) & IOf_ARGV) {
1577 fp = nextargv(PL_last_in_gv);
1580 (void)do_close(PL_last_in_gv, FALSE);
1582 else if (type == OP_GLOB) {
1583 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1584 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1585 "glob failed (child exited with status %d%s)",
1586 (int)(STATUS_CURRENT >> 8),
1587 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
1590 if (gimme == G_SCALAR) {
1591 if (type != OP_RCATLINE) {
1592 SV_CHECK_THINKFIRST_COW_DROP(TARG);
1598 MAYBE_TAINT_LINE(io, sv);
1601 MAYBE_TAINT_LINE(io, sv);
1603 IoFLAGS(io) |= IOf_NOLINE;
1607 if (type == OP_GLOB) {
1610 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
1611 tmps = SvEND(sv) - 1;
1612 if (*tmps == *SvPVX(PL_rs)) {
1617 for (tmps = SvPVX(sv); *tmps; tmps++)
1618 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1619 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1621 if (*tmps && PerlLIO_lstat(SvPVX(sv), &PL_statbuf) < 0) {
1622 (void)POPs; /* Unmatched wildcard? Chuck it... */
1625 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
1626 U8 *s = (U8*)SvPVX(sv) + offset;
1627 STRLEN len = SvCUR(sv) - offset;
1630 if (ckWARN(WARN_UTF8) &&
1631 !Perl_is_utf8_string_loc(aTHX_ s, len, &f))
1632 /* Emulate :encoding(utf8) warning in the same case. */
1633 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1634 "utf8 \"\\x%02X\" does not map to Unicode",
1635 f < (U8*)SvEND(sv) ? *f : 0);
1637 if (gimme == G_ARRAY) {
1638 if (SvLEN(sv) - SvCUR(sv) > 20) {
1639 SvLEN_set(sv, SvCUR(sv)+1);
1640 Renew(SvPVX(sv), SvLEN(sv), char);
1642 sv = sv_2mortal(NEWSV(58, 80));
1645 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1646 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1650 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1651 Renew(SvPVX(sv), SvLEN(sv), char);
1660 register PERL_CONTEXT *cx;
1661 I32 gimme = OP_GIMME(PL_op, -1);
1664 if (cxstack_ix >= 0)
1665 gimme = cxstack[cxstack_ix].blk_gimme;
1673 PUSHBLOCK(cx, CXt_BLOCK, SP);
1685 U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
1686 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
1688 #ifdef PERL_COPY_ON_WRITE
1689 U32 hash = (SvIsCOW_shared_hash(keysv)) ? SvUVX(keysv) : 0;
1691 U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
1695 if (SvTYPE(hv) == SVt_PVHV) {
1696 if (PL_op->op_private & OPpLVAL_INTRO) {
1699 /* does the element we're localizing already exist? */
1701 /* can we determine whether it exists? */
1703 || mg_find((SV*)hv, PERL_MAGIC_env)
1704 || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied))
1705 /* Try to preserve the existenceness of a tied hash
1706 * element by using EXISTS and DELETE if possible.
1707 * Fallback to FETCH and STORE otherwise */
1708 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
1709 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
1710 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
1712 ) ? hv_exists_ent(hv, keysv, 0) : 1;
1715 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
1716 svp = he ? &HeVAL(he) : 0;
1722 if (!svp || *svp == &PL_sv_undef) {
1727 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
1729 lv = sv_newmortal();
1730 sv_upgrade(lv, SVt_PVLV);
1732 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, Nullch, 0);
1733 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1734 LvTARG(lv) = SvREFCNT_inc(hv);
1739 if (PL_op->op_private & OPpLVAL_INTRO) {
1740 if (HvNAME(hv) && isGV(*svp))
1741 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1745 char *key = SvPV(keysv, keylen);
1746 SAVEDELETE(hv, savepvn(key,keylen), keylen);
1748 save_helem(hv, keysv, svp);
1751 else if (PL_op->op_private & OPpDEREF)
1752 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
1754 sv = (svp ? *svp : &PL_sv_undef);
1755 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1756 * Pushing the magical RHS on to the stack is useless, since
1757 * that magic is soon destined to be misled by the local(),
1758 * and thus the later pp_sassign() will fail to mg_get() the
1759 * old value. This should also cure problems with delayed
1760 * mg_get()s. GSAR 98-07-03 */
1761 if (!lval && SvGMAGICAL(sv))
1762 sv = sv_mortalcopy(sv);
1770 register PERL_CONTEXT *cx;
1776 if (PL_op->op_flags & OPf_SPECIAL) {
1777 cx = &cxstack[cxstack_ix];
1778 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
1783 gimme = OP_GIMME(PL_op, -1);
1785 if (cxstack_ix >= 0)
1786 gimme = cxstack[cxstack_ix].blk_gimme;
1792 if (gimme == G_VOID)
1794 else if (gimme == G_SCALAR) {
1797 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1800 *MARK = sv_mortalcopy(TOPs);
1803 *MARK = &PL_sv_undef;
1807 else if (gimme == G_ARRAY) {
1808 /* in case LEAVE wipes old return values */
1809 for (mark = newsp + 1; mark <= SP; mark++) {
1810 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
1811 *mark = sv_mortalcopy(*mark);
1812 TAINT_NOT; /* Each item is independent */
1816 PL_curpm = newpm; /* Don't pop $1 et al till now */
1826 register PERL_CONTEXT *cx;
1832 cx = &cxstack[cxstack_ix];
1833 if (CxTYPE(cx) != CXt_LOOP)
1834 DIE(aTHX_ "panic: pp_iter");
1836 itersvp = CxITERVAR(cx);
1837 av = cx->blk_loop.iterary;
1838 if (SvTYPE(av) != SVt_PVAV) {
1839 /* iterate ($min .. $max) */
1840 if (cx->blk_loop.iterlval) {
1841 /* string increment */
1842 register SV* cur = cx->blk_loop.iterlval;
1844 char *max = SvOK((SV*)av) ? SvPV((SV*)av, maxlen) : "";
1845 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1846 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1847 /* safe to reuse old SV */
1848 sv_setsv(*itersvp, cur);
1852 /* we need a fresh SV every time so that loop body sees a
1853 * completely new SV for closures/references to work as
1856 *itersvp = newSVsv(cur);
1857 SvREFCNT_dec(oldsv);
1859 if (strEQ(SvPVX(cur), max))
1860 sv_setiv(cur, 0); /* terminate next time */
1867 /* integer increment */
1868 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1871 /* don't risk potential race */
1872 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1873 /* safe to reuse old SV */
1874 sv_setiv(*itersvp, cx->blk_loop.iterix++);
1878 /* we need a fresh SV every time so that loop body sees a
1879 * completely new SV for closures/references to work as they
1882 *itersvp = newSViv(cx->blk_loop.iterix++);
1883 SvREFCNT_dec(oldsv);
1889 if (PL_op->op_private & OPpITER_REVERSED) {
1890 /* In reverse, use itermax as the min :-) */
1891 if (cx->blk_loop.iterix <= cx->blk_loop.itermax)
1894 if (SvMAGICAL(av) || AvREIFY(av)) {
1895 SV **svp = av_fetch(av, cx->blk_loop.iterix--, FALSE);
1902 sv = AvARRAY(av)[cx->blk_loop.iterix--];
1906 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp :
1910 if (SvMAGICAL(av) || AvREIFY(av)) {
1911 SV **svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE);
1918 sv = AvARRAY(av)[++cx->blk_loop.iterix];
1922 if (sv && SvREFCNT(sv) == 0) {
1924 Perl_croak(aTHX_ "Use of freed value in iteration");
1931 if (av != PL_curstack && sv == &PL_sv_undef) {
1932 SV *lv = cx->blk_loop.iterlval;
1933 if (lv && SvREFCNT(lv) > 1) {
1938 SvREFCNT_dec(LvTARG(lv));
1940 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
1941 sv_upgrade(lv, SVt_PVLV);
1943 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
1945 LvTARG(lv) = SvREFCNT_inc(av);
1946 LvTARGOFF(lv) = cx->blk_loop.iterix;
1947 LvTARGLEN(lv) = (STRLEN)UV_MAX;
1952 *itersvp = SvREFCNT_inc(sv);
1953 SvREFCNT_dec(oldsv);
1961 register PMOP *pm = cPMOP;
1977 register REGEXP *rx = PM_GETRE(pm);
1979 int force_on_match = 0;
1980 I32 oldsave = PL_savestack_ix;
1982 bool doutf8 = FALSE;
1983 #ifdef PERL_COPY_ON_WRITE
1988 /* known replacement string? */
1989 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
1990 if (PL_op->op_flags & OPf_STACKED)
1992 else if (PL_op->op_private & OPpTARGET_MY)
1999 #ifdef PERL_COPY_ON_WRITE
2000 /* Awooga. Awooga. "bool" types that are actually char are dangerous,
2001 because they make integers such as 256 "false". */
2002 is_cow = SvIsCOW(TARG) ? TRUE : FALSE;
2005 sv_force_normal_flags(TARG,0);
2008 #ifdef PERL_COPY_ON_WRITE
2012 || ( (SvTYPE(TARG) == SVt_PVGV || SvTYPE(TARG) > SVt_PVLV)
2013 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
2014 DIE(aTHX_ PL_no_modify);
2017 s = SvPV(TARG, len);
2018 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
2020 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
2021 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
2026 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
2030 DIE(aTHX_ "panic: pp_subst");
2033 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
2034 maxiters = 2 * slen + 10; /* We can match twice at each
2035 position, once with zero-length,
2036 second time with non-zero. */
2038 if (!rx->prelen && PL_curpm) {
2042 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
2043 ? REXEC_COPY_STR : 0;
2045 r_flags |= REXEC_SCREAM;
2048 if (rx->reganch & RE_USE_INTUIT) {
2050 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
2054 /* How to do it in subst? */
2055 /* if ( (rx->reganch & ROPT_CHECK_ALL)
2057 && ((rx->reganch & ROPT_NOSCAN)
2058 || !((rx->reganch & RE_INTUIT_TAIL)
2059 && (r_flags & REXEC_SCREAM))))
2064 /* only replace once? */
2065 once = !(rpm->op_pmflags & PMf_GLOBAL);
2067 /* known replacement string? */
2069 /* replacement needing upgrading? */
2070 if (DO_UTF8(TARG) && !doutf8) {
2071 nsv = sv_newmortal();
2074 sv_recode_to_utf8(nsv, PL_encoding);
2076 sv_utf8_upgrade(nsv);
2077 c = SvPV(nsv, clen);
2081 c = SvPV(dstr, clen);
2082 doutf8 = DO_UTF8(dstr);
2090 /* can do inplace substitution? */
2092 #ifdef PERL_COPY_ON_WRITE
2095 && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
2096 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)
2097 && (!doutf8 || SvUTF8(TARG))) {
2098 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2099 r_flags | REXEC_CHECKED))
2103 LEAVE_SCOPE(oldsave);
2106 #ifdef PERL_COPY_ON_WRITE
2107 if (SvIsCOW(TARG)) {
2108 assert (!force_on_match);
2112 if (force_on_match) {
2114 s = SvPV_force(TARG, len);
2119 SvSCREAM_off(TARG); /* disable possible screamer */
2121 rxtainted |= RX_MATCH_TAINTED(rx);
2122 m = orig + rx->startp[0];
2123 d = orig + rx->endp[0];
2125 if (m - s > strend - d) { /* faster to shorten from end */
2127 Copy(c, m, clen, char);
2132 Move(d, m, i, char);
2136 SvCUR_set(TARG, m - s);
2139 else if ((i = m - s)) { /* faster from front */
2147 Copy(c, m, clen, char);
2152 Copy(c, d, clen, char);
2157 TAINT_IF(rxtainted & 1);
2163 if (iters++ > maxiters)
2164 DIE(aTHX_ "Substitution loop");
2165 rxtainted |= RX_MATCH_TAINTED(rx);
2166 m = rx->startp[0] + orig;
2170 Move(s, d, i, char);
2174 Copy(c, d, clen, char);
2177 s = rx->endp[0] + orig;
2178 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2180 /* don't match same null twice */
2181 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
2184 SvCUR_set(TARG, d - SvPVX(TARG) + i);
2185 Move(s, d, i+1, char); /* include the NUL */
2187 TAINT_IF(rxtainted & 1);
2189 PUSHs(sv_2mortal(newSViv((I32)iters)));
2191 (void)SvPOK_only_UTF8(TARG);
2192 TAINT_IF(rxtainted);
2193 if (SvSMAGICAL(TARG)) {
2201 LEAVE_SCOPE(oldsave);
2205 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2206 r_flags | REXEC_CHECKED))
2208 if (force_on_match) {
2210 s = SvPV_force(TARG, len);
2213 #ifdef PERL_COPY_ON_WRITE
2216 rxtainted |= RX_MATCH_TAINTED(rx);
2217 dstr = NEWSV(25, len);
2218 sv_setpvn(dstr, m, s-m);
2223 register PERL_CONTEXT *cx;
2227 RETURNOP(cPMOP->op_pmreplroot);
2229 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
2231 if (iters++ > maxiters)
2232 DIE(aTHX_ "Substitution loop");
2233 rxtainted |= RX_MATCH_TAINTED(rx);
2234 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
2239 strend = s + (strend - m);
2241 m = rx->startp[0] + orig;
2242 if (doutf8 && !SvUTF8(dstr))
2243 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
2245 sv_catpvn(dstr, s, m-s);
2246 s = rx->endp[0] + orig;
2248 sv_catpvn(dstr, c, clen);
2251 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2252 TARG, NULL, r_flags));
2253 if (doutf8 && !DO_UTF8(TARG))
2254 sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv);
2256 sv_catpvn(dstr, s, strend - s);
2258 #ifdef PERL_COPY_ON_WRITE
2259 /* The match may make the string COW. If so, brilliant, because that's
2260 just saved us one malloc, copy and free - the regexp has donated
2261 the old buffer, and we malloc an entirely new one, rather than the
2262 regexp malloc()ing a buffer and copying our original, only for
2263 us to throw it away here during the substitution. */
2264 if (SvIsCOW(TARG)) {
2265 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
2271 Safefree(SvPVX(TARG));
2273 SvPVX(TARG) = SvPVX(dstr);
2274 SvCUR_set(TARG, SvCUR(dstr));
2275 SvLEN_set(TARG, SvLEN(dstr));
2276 doutf8 |= DO_UTF8(dstr);
2280 TAINT_IF(rxtainted & 1);
2282 PUSHs(sv_2mortal(newSViv((I32)iters)));
2284 (void)SvPOK_only(TARG);
2287 TAINT_IF(rxtainted);
2290 LEAVE_SCOPE(oldsave);
2299 LEAVE_SCOPE(oldsave);
2308 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2309 ++*PL_markstack_ptr;
2310 LEAVE; /* exit inner scope */
2313 if (PL_stack_base + *PL_markstack_ptr > SP) {
2315 I32 gimme = GIMME_V;
2317 LEAVE; /* exit outer scope */
2318 (void)POPMARK; /* pop src */
2319 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
2320 (void)POPMARK; /* pop dst */
2321 SP = PL_stack_base + POPMARK; /* pop original mark */
2322 if (gimme == G_SCALAR) {
2323 if (PL_op->op_private & OPpGREP_LEX) {
2324 SV* sv = sv_newmortal();
2325 sv_setiv(sv, items);
2333 else if (gimme == G_ARRAY)
2340 ENTER; /* enter inner scope */
2343 src = PL_stack_base[*PL_markstack_ptr];
2345 if (PL_op->op_private & OPpGREP_LEX)
2346 PAD_SVl(PL_op->op_targ) = src;
2350 RETURNOP(cLOGOP->op_other);
2361 register PERL_CONTEXT *cx;
2365 cxstack_ix++; /* temporarily protect top context */
2368 if (gimme == G_SCALAR) {
2371 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2373 *MARK = SvREFCNT_inc(TOPs);
2378 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2380 *MARK = sv_mortalcopy(sv);
2385 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2389 *MARK = &PL_sv_undef;
2393 else if (gimme == G_ARRAY) {
2394 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2395 if (!SvTEMP(*MARK)) {
2396 *MARK = sv_mortalcopy(*MARK);
2397 TAINT_NOT; /* Each item is independent */
2405 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2406 PL_curpm = newpm; /* ... and pop $1 et al */
2409 return cx->blk_sub.retop;
2412 /* This duplicates the above code because the above code must not
2413 * get any slower by more conditions */
2421 register PERL_CONTEXT *cx;
2425 cxstack_ix++; /* temporarily protect top context */
2429 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2430 /* We are an argument to a function or grep().
2431 * This kind of lvalueness was legal before lvalue
2432 * subroutines too, so be backward compatible:
2433 * cannot report errors. */
2435 /* Scalar context *is* possible, on the LHS of -> only,
2436 * as in f()->meth(). But this is not an lvalue. */
2437 if (gimme == G_SCALAR)
2439 if (gimme == G_ARRAY) {
2440 if (!CvLVALUE(cx->blk_sub.cv))
2441 goto temporise_array;
2442 EXTEND_MORTAL(SP - newsp);
2443 for (mark = newsp + 1; mark <= SP; mark++) {
2446 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2447 *mark = sv_mortalcopy(*mark);
2449 /* Can be a localized value subject to deletion. */
2450 PL_tmps_stack[++PL_tmps_ix] = *mark;
2451 (void)SvREFCNT_inc(*mark);
2456 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2457 /* Here we go for robustness, not for speed, so we change all
2458 * the refcounts so the caller gets a live guy. Cannot set
2459 * TEMP, so sv_2mortal is out of question. */
2460 if (!CvLVALUE(cx->blk_sub.cv)) {
2466 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2468 if (gimme == G_SCALAR) {
2472 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2478 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2479 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2480 : "a readonly value" : "a temporary");
2482 else { /* Can be a localized value
2483 * subject to deletion. */
2484 PL_tmps_stack[++PL_tmps_ix] = *mark;
2485 (void)SvREFCNT_inc(*mark);
2488 else { /* Should not happen? */
2494 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
2495 (MARK > SP ? "Empty array" : "Array"));
2499 else if (gimme == G_ARRAY) {
2500 EXTEND_MORTAL(SP - newsp);
2501 for (mark = newsp + 1; mark <= SP; mark++) {
2502 if (*mark != &PL_sv_undef
2503 && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2504 /* Might be flattened array after $#array = */
2511 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2512 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2515 /* Can be a localized value subject to deletion. */
2516 PL_tmps_stack[++PL_tmps_ix] = *mark;
2517 (void)SvREFCNT_inc(*mark);
2523 if (gimme == G_SCALAR) {
2527 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2529 *MARK = SvREFCNT_inc(TOPs);
2534 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2536 *MARK = sv_mortalcopy(sv);
2541 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2545 *MARK = &PL_sv_undef;
2549 else if (gimme == G_ARRAY) {
2551 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2552 if (!SvTEMP(*MARK)) {
2553 *MARK = sv_mortalcopy(*MARK);
2554 TAINT_NOT; /* Each item is independent */
2563 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2564 PL_curpm = newpm; /* ... and pop $1 et al */
2567 return cx->blk_sub.retop;
2572 S_get_db_sub(pTHX_ SV **svp, CV *cv)
2574 SV *dbsv = GvSV(PL_DBsub);
2576 if (!PERLDB_SUB_NN) {
2580 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2581 || strEQ(GvNAME(gv), "END")
2582 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2583 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2584 && (gv = (GV*)*svp) ))) {
2585 /* Use GV from the stack as a fallback. */
2586 /* GV is potentially non-unique, or contain different CV. */
2587 SV *tmp = newRV((SV*)cv);
2588 sv_setsv(dbsv, tmp);
2592 gv_efullname3(dbsv, gv, Nullch);
2596 (void)SvUPGRADE(dbsv, SVt_PVIV);
2597 (void)SvIOK_on(dbsv);
2598 SAVEIV(SvIVX(dbsv));
2599 SvIVX(dbsv) = PTR2IV(cv); /* Do it the quickest way */
2603 PL_curcopdb = PL_curcop;
2604 cv = GvCV(PL_DBsub);
2614 register PERL_CONTEXT *cx;
2616 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
2619 DIE(aTHX_ "Not a CODE reference");
2620 switch (SvTYPE(sv)) {
2621 /* This is overwhelming the most common case: */
2623 if (!(cv = GvCVu((GV*)sv)))
2624 cv = sv_2cv(sv, &stash, &gv, FALSE);
2636 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2638 SP = PL_stack_base + POPMARK;
2641 if (SvGMAGICAL(sv)) {
2645 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2648 sym = SvPV(sv, n_a);
2650 DIE(aTHX_ PL_no_usym, "a subroutine");
2651 if (PL_op->op_private & HINT_STRICT_REFS)
2652 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
2653 cv = get_cv(sym, TRUE);
2658 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2659 tryAMAGICunDEREF(to_cv);
2662 if (SvTYPE(cv) == SVt_PVCV)
2667 DIE(aTHX_ "Not a CODE reference");
2668 /* This is the second most common case: */
2678 if (!CvROOT(cv) && !CvXSUB(cv)) {
2683 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
2684 if (CvASSERTION(cv) && PL_DBassertion)
2685 sv_setiv(PL_DBassertion, 1);
2687 cv = get_db_sub(&sv, cv);
2689 DIE(aTHX_ "No DBsub routine");
2692 if (!(CvXSUB(cv))) {
2693 /* This path taken at least 75% of the time */
2695 register I32 items = SP - MARK;
2696 AV* padlist = CvPADLIST(cv);
2697 PUSHBLOCK(cx, CXt_SUB, MARK);
2699 cx->blk_sub.retop = PL_op->op_next;
2701 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2702 * that eval'' ops within this sub know the correct lexical space.
2703 * Owing the speed considerations, we choose instead to search for
2704 * the cv using find_runcv() when calling doeval().
2706 if (CvDEPTH(cv) >= 2) {
2707 PERL_STACK_OVERFLOW_CHECK();
2708 pad_push(padlist, CvDEPTH(cv), 1);
2710 PAD_SET_CUR(padlist, CvDEPTH(cv));
2717 DEBUG_S(PerlIO_printf(Perl_debug_log,
2718 "%p entersub preparing @_\n", thr));
2720 av = (AV*)PAD_SVl(0);
2722 /* @_ is normally not REAL--this should only ever
2723 * happen when DB::sub() calls things that modify @_ */
2728 cx->blk_sub.savearray = GvAV(PL_defgv);
2729 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2730 CX_CURPAD_SAVE(cx->blk_sub);
2731 cx->blk_sub.argarray = av;
2734 if (items > AvMAX(av) + 1) {
2736 if (AvARRAY(av) != ary) {
2737 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2738 SvPVX(av) = (char*)ary;
2740 if (items > AvMAX(av) + 1) {
2741 AvMAX(av) = items - 1;
2742 Renew(ary,items,SV*);
2744 SvPVX(av) = (char*)ary;
2747 Copy(MARK,AvARRAY(av),items,SV*);
2748 AvFILLp(av) = items - 1;
2756 /* warning must come *after* we fully set up the context
2757 * stuff so that __WARN__ handlers can safely dounwind()
2760 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2761 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2762 sub_crush_depth(cv);
2764 DEBUG_S(PerlIO_printf(Perl_debug_log,
2765 "%p entersub returning %p\n", thr, CvSTART(cv)));
2767 RETURNOP(CvSTART(cv));
2770 #ifdef PERL_XSUB_OLDSTYLE
2771 if (CvOLDSTYLE(cv)) {
2772 I32 (*fp3)(int,int,int);
2774 register I32 items = SP - MARK;
2775 /* We dont worry to copy from @_. */
2780 PL_stack_sp = mark + 1;
2781 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2782 items = (*fp3)(CvXSUBANY(cv).any_i32,
2783 MARK - PL_stack_base + 1,
2785 PL_stack_sp = PL_stack_base + items;
2788 #endif /* PERL_XSUB_OLDSTYLE */
2790 I32 markix = TOPMARK;
2795 /* Need to copy @_ to stack. Alternative may be to
2796 * switch stack to @_, and copy return values
2797 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2800 av = GvAV(PL_defgv);
2801 items = AvFILLp(av) + 1; /* @_ is not tieable */
2804 /* Mark is at the end of the stack. */
2806 Copy(AvARRAY(av), SP + 1, items, SV*);
2811 /* We assume first XSUB in &DB::sub is the called one. */
2813 SAVEVPTR(PL_curcop);
2814 PL_curcop = PL_curcopdb;
2817 /* Do we need to open block here? XXXX */
2818 (void)(*CvXSUB(cv))(aTHX_ cv);
2820 /* Enforce some sanity in scalar context. */
2821 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2822 if (markix > PL_stack_sp - PL_stack_base)
2823 *(PL_stack_base + markix) = &PL_sv_undef;
2825 *(PL_stack_base + markix) = *PL_stack_sp;
2826 PL_stack_sp = PL_stack_base + markix;
2833 assert (0); /* Cannot get here. */
2834 /* This is deliberately moved here as spaghetti code to keep it out of the
2841 /* anonymous or undef'd function leaves us no recourse */
2842 if (CvANON(cv) || !(gv = CvGV(cv)))
2843 DIE(aTHX_ "Undefined subroutine called");
2845 /* autoloaded stub? */
2846 if (cv != GvCV(gv)) {
2849 /* should call AUTOLOAD now? */
2852 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2859 sub_name = sv_newmortal();
2860 gv_efullname3(sub_name, gv, Nullch);
2861 DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name);
2865 DIE(aTHX_ "Not a CODE reference");
2871 Perl_sub_crush_depth(pTHX_ CV *cv)
2874 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
2876 SV* tmpstr = sv_newmortal();
2877 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2878 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
2888 IV elem = SvIV(elemsv);
2890 U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2891 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
2894 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
2895 Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv);
2897 elem -= PL_curcop->cop_arybase;
2898 if (SvTYPE(av) != SVt_PVAV)
2900 svp = av_fetch(av, elem, lval && !defer);
2902 #ifdef PERL_MALLOC_WRAP
2903 static const char oom_array_extend[] =
2904 "Out of memory during array extend"; /* Duplicated in av.c */
2905 if (SvUOK(elemsv)) {
2906 UV uv = SvUV(elemsv);
2907 elem = uv > IV_MAX ? IV_MAX : uv;
2909 else if (SvNOK(elemsv))
2910 elem = (IV)SvNV(elemsv);
2912 MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
2914 if (!svp || *svp == &PL_sv_undef) {
2917 DIE(aTHX_ PL_no_aelem, elem);
2918 lv = sv_newmortal();
2919 sv_upgrade(lv, SVt_PVLV);
2921 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
2922 LvTARG(lv) = SvREFCNT_inc(av);
2923 LvTARGOFF(lv) = elem;
2928 if (PL_op->op_private & OPpLVAL_INTRO)
2929 save_aelem(av, elem, svp);
2930 else if (PL_op->op_private & OPpDEREF)
2931 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
2933 sv = (svp ? *svp : &PL_sv_undef);
2934 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2935 sv = sv_mortalcopy(sv);
2941 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
2947 Perl_croak(aTHX_ PL_no_modify);
2948 if (SvTYPE(sv) < SVt_RV)
2949 sv_upgrade(sv, SVt_RV);
2950 else if (SvTYPE(sv) >= SVt_PV) {
2952 Safefree(SvPVX(sv));
2953 SvLEN(sv) = SvCUR(sv) = 0;
2957 SvRV(sv) = NEWSV(355,0);
2960 SvRV(sv) = (SV*)newAV();
2963 SvRV(sv) = (SV*)newHV();
2978 if (SvTYPE(rsv) == SVt_PVCV) {
2984 SETs(method_common(sv, Null(U32*)));
2992 U32 hash = SvUVX(sv);
2994 XPUSHs(method_common(sv, &hash));
2999 S_method_common(pTHX_ SV* meth, U32* hashp)
3008 SV *packsv = Nullsv;
3011 name = SvPV(meth, namelen);
3012 sv = *(PL_stack_base + TOPMARK + 1);
3015 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
3024 /* this isn't a reference */
3027 if(SvOK(sv) && (packname = SvPV(sv, packlen))) {
3029 he = hv_fetch_ent(PL_stashcache, sv, 0, 0);
3031 stash = INT2PTR(HV*,SvIV(HeVAL(he)));
3038 !(iogv = gv_fetchsv(sv, FALSE, SVt_PVIO)) ||
3039 !(ob=(SV*)GvIO(iogv)))
3041 /* this isn't the name of a filehandle either */
3043 ((UTF8_IS_START(*packname) && DO_UTF8(sv))
3044 ? !isIDFIRST_utf8((U8*)packname)
3045 : !isIDFIRST(*packname)
3048 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
3049 SvOK(sv) ? "without a package or object reference"
3050 : "on an undefined value");
3052 /* assume it's a package name */
3053 stash = gv_stashpvn(packname, packlen, FALSE);
3057 SV* ref = newSViv(PTR2IV(stash));
3058 hv_store(PL_stashcache, packname, packlen, ref, 0);
3062 /* it _is_ a filehandle name -- replace with a reference */
3063 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
3066 /* if we got here, ob should be a reference or a glob */
3067 if (!ob || !(SvOBJECT(ob)
3068 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
3071 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
3075 stash = SvSTASH(ob);
3078 /* NOTE: stash may be null, hope hv_fetch_ent and
3079 gv_fetchmethod can cope (it seems they can) */
3081 /* shortcut for simple names */
3083 HE* he = hv_fetch_ent(stash, meth, 0, *hashp);
3085 gv = (GV*)HeVAL(he);
3086 if (isGV(gv) && GvCV(gv) &&
3087 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
3088 return (SV*)GvCV(gv);
3092 gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name);
3095 /* This code tries to figure out just what went wrong with
3096 gv_fetchmethod. It therefore needs to duplicate a lot of
3097 the internals of that function. We can't move it inside
3098 Perl_gv_fetchmethod_autoload(), however, since that would
3099 cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we
3106 for (p = name; *p; p++) {
3108 sep = p, leaf = p + 1;
3109 else if (*p == ':' && *(p + 1) == ':')
3110 sep = p, leaf = p + 2;
3112 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3113 /* the method name is unqualified or starts with SUPER:: */
3114 packname = sep ? CopSTASHPV(PL_curcop) :
3115 stash ? HvNAME(stash) : packname;
3118 "Can't use anonymous symbol table for method lookup");
3120 packlen = strlen(packname);
3123 /* the method name is qualified */
3125 packlen = sep - name;
3128 /* we're relying on gv_fetchmethod not autovivifying the stash */
3129 if (gv_stashpvn(packname, packlen, FALSE)) {
3131 "Can't locate object method \"%s\" via package \"%.*s\"",
3132 leaf, (int)packlen, packname);
3136 "Can't locate object method \"%s\" via package \"%.*s\""
3137 " (perhaps you forgot to load \"%.*s\"?)",
3138 leaf, (int)packlen, packname, (int)packlen, packname);
3141 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;