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 if (left == right && ckWARN(WARN_UNINITIALIZED))
179 report_uninit(right);
180 sv_setpvn(left, "", 0);
182 (void)SvPV_nomg_const(left, llen); /* Needed to set UTF8 flag */
183 lbyte = !DO_UTF8(left);
188 /* or mg_get(right) may happen here */
190 rpv = SvPV_const(right, rlen);
191 rbyte = !DO_UTF8(right);
193 if (lbyte != rbyte) {
195 sv_utf8_upgrade_nomg(TARG);
198 right = sv_2mortal(newSVpvn(rpv, rlen));
199 sv_utf8_upgrade_nomg(right);
200 rpv = SvPV_const(right, rlen);
203 sv_catpvn_nomg(TARG, rpv, rlen);
214 if (PL_op->op_flags & OPf_MOD) {
215 if (PL_op->op_private & OPpLVAL_INTRO)
216 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
217 if (PL_op->op_private & OPpDEREF) {
219 vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
228 tryAMAGICunTARGET(iter, 0);
229 PL_last_in_gv = (GV*)(*PL_stack_sp--);
230 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
231 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
232 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
235 XPUSHs((SV*)PL_last_in_gv);
238 PL_last_in_gv = (GV*)(*PL_stack_sp--);
241 return do_readline();
246 dSP; tryAMAGICbinSET(eq,0);
247 #ifndef NV_PRESERVES_UV
248 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
250 SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s)));
254 #ifdef PERL_PRESERVE_IVUV
257 /* Unless the left argument is integer in range we are going
258 to have to use NV maths. Hence only attempt to coerce the
259 right argument if we know the left is integer. */
262 bool auvok = SvUOK(TOPm1s);
263 bool buvok = SvUOK(TOPs);
265 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
266 /* Casting IV to UV before comparison isn't going to matter
267 on 2s complement. On 1s complement or sign&magnitude
268 (if we have any of them) it could to make negative zero
269 differ from normal zero. As I understand it. (Need to
270 check - is negative zero implementation defined behaviour
272 UV buv = SvUVX(POPs);
273 UV auv = SvUVX(TOPs);
275 SETs(boolSV(auv == buv));
278 { /* ## Mixed IV,UV ## */
282 /* == is commutative so doesn't matter which is left or right */
284 /* top of stack (b) is the iv */
293 /* As uv is a UV, it's >0, so it cannot be == */
297 /* we know iv is >= 0 */
298 SETs(boolSV((UV)iv == SvUVX(uvp)));
306 SETs(boolSV(TOPn == value));
314 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
315 DIE(aTHX_ PL_no_modify);
316 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
317 && SvIVX(TOPs) != IV_MAX)
319 SvIV_set(TOPs, SvIVX(TOPs) + 1);
320 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
322 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
334 if (PL_op->op_type == OP_OR)
336 RETURNOP(cLOGOP->op_other);
343 register SV* sv = NULL;
344 bool defined = FALSE;
345 const int op_type = PL_op->op_type;
347 if(op_type == OP_DOR || op_type == OP_DORASSIGN) {
349 if (!sv || !SvANY(sv)) {
350 if (op_type == OP_DOR)
352 RETURNOP(cLOGOP->op_other);
354 } else if (op_type == OP_DEFINED) {
356 if (!sv || !SvANY(sv))
359 DIE(aTHX_ "panic: Invalid op (%s) in pp_defined()", OP_NAME(PL_op));
361 switch (SvTYPE(sv)) {
363 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
367 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
371 if (CvROOT(sv) || CvXSUB(sv))
380 if(op_type == OP_DOR || op_type == OP_DORASSIGN) {
383 if(op_type == OP_DOR)
385 RETURNOP(cLOGOP->op_other);
387 /* assuming OP_DEFINED */
395 dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
396 useleft = USE_LEFT(TOPm1s);
397 #ifdef PERL_PRESERVE_IVUV
398 /* We must see if we can perform the addition with integers if possible,
399 as the integer code detects overflow while the NV code doesn't.
400 If either argument hasn't had a numeric conversion yet attempt to get
401 the IV. It's important to do this now, rather than just assuming that
402 it's not IOK as a PV of "9223372036854775806" may not take well to NV
403 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
404 integer in case the second argument is IV=9223372036854775806
405 We can (now) rely on sv_2iv to do the right thing, only setting the
406 public IOK flag if the value in the NV (or PV) slot is truly integer.
408 A side effect is that this also aggressively prefers integer maths over
409 fp maths for integer values.
411 How to detect overflow?
413 C 99 section 6.2.6.1 says
415 The range of nonnegative values of a signed integer type is a subrange
416 of the corresponding unsigned integer type, and the representation of
417 the same value in each type is the same. A computation involving
418 unsigned operands can never overflow, because a result that cannot be
419 represented by the resulting unsigned integer type is reduced modulo
420 the number that is one greater than the largest value that can be
421 represented by the resulting type.
425 which I read as "unsigned ints wrap."
427 signed integer overflow seems to be classed as "exception condition"
429 If an exceptional condition occurs during the evaluation of an
430 expression (that is, if the result is not mathematically defined or not
431 in the range of representable values for its type), the behavior is
434 (6.5, the 5th paragraph)
436 I had assumed that on 2s complement machines signed arithmetic would
437 wrap, hence coded pp_add and pp_subtract on the assumption that
438 everything perl builds on would be happy. After much wailing and
439 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
440 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
441 unsigned code below is actually shorter than the old code. :-)
446 /* Unless the left argument is integer in range we are going to have to
447 use NV maths. Hence only attempt to coerce the right argument if
448 we know the left is integer. */
456 /* left operand is undef, treat as zero. + 0 is identity,
457 Could SETi or SETu right now, but space optimise by not adding
458 lots of code to speed up what is probably a rarish case. */
460 /* Left operand is defined, so is it IV? */
463 if ((auvok = SvUOK(TOPm1s)))
466 register const IV aiv = SvIVX(TOPm1s);
469 auvok = 1; /* Now acting as a sign flag. */
470 } else { /* 2s complement assumption for IV_MIN */
478 bool result_good = 0;
481 bool buvok = SvUOK(TOPs);
486 register const IV biv = SvIVX(TOPs);
493 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
494 else "IV" now, independent of how it came in.
495 if a, b represents positive, A, B negative, a maps to -A etc
500 all UV maths. negate result if A negative.
501 add if signs same, subtract if signs differ. */
507 /* Must get smaller */
513 /* result really should be -(auv-buv). as its negation
514 of true value, need to swap our result flag */
531 if (result <= (UV)IV_MIN)
534 /* result valid, but out of range for IV. */
539 } /* Overflow, drop through to NVs. */
546 /* left operand is undef, treat as zero. + 0.0 is identity. */
550 SETn( value + TOPn );
558 AV *av = PL_op->op_flags & OPf_SPECIAL ?
559 (AV*)PAD_SV(PL_op->op_targ) : GvAV(cGVOP_gv);
560 const U32 lval = PL_op->op_flags & OPf_MOD;
561 SV** svp = av_fetch(av, PL_op->op_private, lval);
562 SV *sv = (svp ? *svp : &PL_sv_undef);
564 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
565 sv = sv_mortalcopy(sv);
574 do_join(TARG, *MARK, MARK, SP);
585 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
586 * will be enough to hold an OP*.
588 SV* const sv = sv_newmortal();
589 sv_upgrade(sv, SVt_PVLV);
591 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
599 /* Oversized hot code. */
603 dVAR; dSP; dMARK; dORIGMARK;
609 if (PL_op->op_flags & OPf_STACKED)
614 if (gv && (io = GvIO(gv))
615 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
618 if (MARK == ORIGMARK) {
619 /* If using default handle then we need to make space to
620 * pass object as 1st arg, so move other args up ...
624 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
628 *MARK = SvTIED_obj((SV*)io, mg);
631 call_method("PRINT", G_SCALAR);
639 if (!(io = GvIO(gv))) {
640 if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv)))
641 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
643 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
644 report_evil_fh(gv, io, PL_op->op_type);
645 SETERRNO(EBADF,RMS_IFI);
648 else if (!(fp = IoOFP(io))) {
649 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
651 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
652 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
653 report_evil_fh(gv, io, PL_op->op_type);
655 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
660 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
662 if (!do_print(*MARK, fp))
666 if (!do_print(PL_ofs_sv, fp)) { /* $, */
675 if (!do_print(*MARK, fp))
683 if (PL_ors_sv && SvOK(PL_ors_sv))
684 if (!do_print(PL_ors_sv, fp)) /* $\ */
687 if (IoFLAGS(io) & IOf_FLUSH)
688 if (PerlIO_flush(fp) == EOF)
698 XPUSHs(&PL_sv_undef);
709 tryAMAGICunDEREF(to_av);
712 if (SvTYPE(av) != SVt_PVAV)
713 DIE(aTHX_ "Not an ARRAY reference");
714 if (PL_op->op_flags & OPf_REF) {
719 if (GIMME == G_SCALAR)
720 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
724 else if (PL_op->op_flags & OPf_MOD
725 && PL_op->op_private & OPpLVAL_INTRO)
726 Perl_croak(aTHX_ PL_no_localize_ref);
729 if (SvTYPE(sv) == SVt_PVAV) {
731 if (PL_op->op_flags & OPf_REF) {
736 if (GIMME == G_SCALAR)
737 Perl_croak(aTHX_ "Can't return array to lvalue"
746 if (SvTYPE(sv) != SVt_PVGV) {
747 if (SvGMAGICAL(sv)) {
753 if (PL_op->op_flags & OPf_REF ||
754 PL_op->op_private & HINT_STRICT_REFS)
755 DIE(aTHX_ PL_no_usym, "an ARRAY");
756 if (ckWARN(WARN_UNINITIALIZED))
758 if (GIMME == G_ARRAY) {
764 if ((PL_op->op_flags & OPf_SPECIAL) &&
765 !(PL_op->op_flags & OPf_MOD))
767 gv = (GV*)gv_fetchsv(sv, FALSE, SVt_PVAV);
769 && (!is_gv_magical_sv(sv,0)
770 || !(gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVAV))))
776 if (PL_op->op_private & HINT_STRICT_REFS)
777 DIE(aTHX_ PL_no_symref_sv, sv, "an ARRAY");
778 gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVAV);
785 if (PL_op->op_private & OPpLVAL_INTRO)
787 if (PL_op->op_flags & OPf_REF) {
792 if (GIMME == G_SCALAR)
793 Perl_croak(aTHX_ "Can't return array to lvalue"
801 if (GIMME == G_ARRAY) {
802 const I32 maxarg = AvFILL(av) + 1;
803 (void)POPs; /* XXXX May be optimized away? */
805 if (SvRMAGICAL(av)) {
807 for (i=0; i < (U32)maxarg; i++) {
808 SV ** const svp = av_fetch(av, i, FALSE);
809 /* See note in pp_helem, and bug id #27839 */
811 ? SvGMAGICAL(*svp) ? sv_mortalcopy(*svp) : *svp
816 Copy(AvARRAY(av), SP+1, maxarg, SV*);
820 else if (GIMME_V == G_SCALAR) {
822 const I32 maxarg = AvFILL(av) + 1;
832 const I32 gimme = GIMME_V;
833 static const char return_hash_to_lvalue_scalar[] = "Can't return hash to lvalue scalar context";
837 tryAMAGICunDEREF(to_hv);
840 if (SvTYPE(hv) != SVt_PVHV)
841 DIE(aTHX_ "Not a HASH reference");
842 if (PL_op->op_flags & OPf_REF) {
847 if (gimme != G_ARRAY)
848 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
852 else if (PL_op->op_flags & OPf_MOD
853 && PL_op->op_private & OPpLVAL_INTRO)
854 Perl_croak(aTHX_ PL_no_localize_ref);
857 if (SvTYPE(sv) == SVt_PVHV) {
859 if (PL_op->op_flags & OPf_REF) {
864 if (gimme != G_ARRAY)
865 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
873 if (SvTYPE(sv) != SVt_PVGV) {
874 if (SvGMAGICAL(sv)) {
880 if (PL_op->op_flags & OPf_REF ||
881 PL_op->op_private & HINT_STRICT_REFS)
882 DIE(aTHX_ PL_no_usym, "a HASH");
883 if (ckWARN(WARN_UNINITIALIZED))
885 if (gimme == G_ARRAY) {
891 if ((PL_op->op_flags & OPf_SPECIAL) &&
892 !(PL_op->op_flags & OPf_MOD))
894 gv = (GV*)gv_fetchsv(sv, FALSE, SVt_PVHV);
896 && (!is_gv_magical_sv(sv,0)
897 || !(gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVHV))))
903 if (PL_op->op_private & HINT_STRICT_REFS)
904 DIE(aTHX_ PL_no_symref_sv, sv, "a HASH");
905 gv = (GV*)gv_fetchsv(sv, TRUE, SVt_PVHV);
912 if (PL_op->op_private & OPpLVAL_INTRO)
914 if (PL_op->op_flags & OPf_REF) {
919 if (gimme != G_ARRAY)
920 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
927 if (gimme == G_ARRAY) { /* array wanted */
928 *PL_stack_sp = (SV*)hv;
931 else if (gimme == G_SCALAR) {
933 TARG = Perl_hv_scalar(aTHX_ hv);
940 S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
946 if (ckWARN(WARN_MISC)) {
948 if (relem == firstrelem &&
950 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
951 SvTYPE(SvRV(*relem)) == SVt_PVHV))
953 err = "Reference found where even-sized list expected";
956 err = "Odd number of elements in hash assignment";
957 Perl_warner(aTHX_ packWARN(WARN_MISC), err);
960 tmpstr = NEWSV(29,0);
961 didstore = hv_store_ent(hash,*relem,tmpstr,0);
962 if (SvMAGICAL(hash)) {
963 if (SvSMAGICAL(tmpstr))
975 SV **lastlelem = PL_stack_sp;
976 SV **lastrelem = PL_stack_base + POPMARK;
977 SV **firstrelem = PL_stack_base + POPMARK + 1;
978 SV **firstlelem = lastrelem + 1;
991 SV **firsthashrelem = 0; /* "= 0" keeps gcc 2.95 quiet */
994 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
997 /* If there's a common identifier on both sides we have to take
998 * special care that assigning the identifier on the left doesn't
999 * clobber a value on the right that's used later in the list.
1001 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
1002 EXTEND_MORTAL(lastrelem - firstrelem + 1);
1003 for (relem = firstrelem; relem <= lastrelem; relem++) {
1004 if ((sv = *relem)) {
1005 TAINT_NOT; /* Each item is independent */
1006 *relem = sv_mortalcopy(sv);
1016 while (lelem <= lastlelem) {
1017 TAINT_NOT; /* Each item stands on its own, taintwise. */
1019 switch (SvTYPE(sv)) {
1022 magic = SvMAGICAL(ary) != 0;
1024 av_extend(ary, lastrelem - relem);
1026 while (relem <= lastrelem) { /* gobble up all the rest */
1029 sv = newSVsv(*relem);
1031 didstore = av_store(ary,i++,sv);
1041 case SVt_PVHV: { /* normal hash */
1045 magic = SvMAGICAL(hash) != 0;
1047 firsthashrelem = relem;
1049 while (relem < lastrelem) { /* gobble up all the rest */
1054 sv = &PL_sv_no, relem++;
1055 tmpstr = NEWSV(29,0);
1057 sv_setsv(tmpstr,*relem); /* value */
1058 *(relem++) = tmpstr;
1059 if (gimme != G_VOID && hv_exists_ent(hash, sv, 0))
1060 /* key overwrites an existing entry */
1062 didstore = hv_store_ent(hash,sv,tmpstr,0);
1064 if (SvSMAGICAL(tmpstr))
1071 if (relem == lastrelem) {
1072 do_oddball(hash, relem, firstrelem);
1078 if (SvIMMORTAL(sv)) {
1079 if (relem <= lastrelem)
1083 if (relem <= lastrelem) {
1084 sv_setsv(sv, *relem);
1088 sv_setsv(sv, &PL_sv_undef);
1093 if (PL_delaymagic & ~DM_DELAY) {
1094 if (PL_delaymagic & DM_UID) {
1095 #ifdef HAS_SETRESUID
1096 (void)setresuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1097 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1,
1100 # ifdef HAS_SETREUID
1101 (void)setreuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1102 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1);
1105 if ((PL_delaymagic & DM_UID) == DM_RUID) {
1106 (void)setruid(PL_uid);
1107 PL_delaymagic &= ~DM_RUID;
1109 # endif /* HAS_SETRUID */
1111 if ((PL_delaymagic & DM_UID) == DM_EUID) {
1112 (void)seteuid(PL_euid);
1113 PL_delaymagic &= ~DM_EUID;
1115 # endif /* HAS_SETEUID */
1116 if (PL_delaymagic & DM_UID) {
1117 if (PL_uid != PL_euid)
1118 DIE(aTHX_ "No setreuid available");
1119 (void)PerlProc_setuid(PL_uid);
1121 # endif /* HAS_SETREUID */
1122 #endif /* HAS_SETRESUID */
1123 PL_uid = PerlProc_getuid();
1124 PL_euid = PerlProc_geteuid();
1126 if (PL_delaymagic & DM_GID) {
1127 #ifdef HAS_SETRESGID
1128 (void)setresgid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1129 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1,
1132 # ifdef HAS_SETREGID
1133 (void)setregid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1134 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1);
1137 if ((PL_delaymagic & DM_GID) == DM_RGID) {
1138 (void)setrgid(PL_gid);
1139 PL_delaymagic &= ~DM_RGID;
1141 # endif /* HAS_SETRGID */
1143 if ((PL_delaymagic & DM_GID) == DM_EGID) {
1144 (void)setegid(PL_egid);
1145 PL_delaymagic &= ~DM_EGID;
1147 # endif /* HAS_SETEGID */
1148 if (PL_delaymagic & DM_GID) {
1149 if (PL_gid != PL_egid)
1150 DIE(aTHX_ "No setregid available");
1151 (void)PerlProc_setgid(PL_gid);
1153 # endif /* HAS_SETREGID */
1154 #endif /* HAS_SETRESGID */
1155 PL_gid = PerlProc_getgid();
1156 PL_egid = PerlProc_getegid();
1158 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1162 if (gimme == G_VOID)
1163 SP = firstrelem - 1;
1164 else if (gimme == G_SCALAR) {
1167 SETi(lastrelem - firstrelem + 1 - duplicates);
1174 /* Removes from the stack the entries which ended up as
1175 * duplicated keys in the hash (fix for [perl #24380]) */
1176 Move(firsthashrelem + duplicates,
1177 firsthashrelem, duplicates, SV**);
1178 lastrelem -= duplicates;
1183 SP = firstrelem + (lastlelem - firstlelem);
1184 lelem = firstlelem + (relem - firstrelem);
1186 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
1194 register PMOP * const pm = cPMOP;
1195 SV * const rv = sv_newmortal();
1196 SV * const sv = newSVrv(rv, "Regexp");
1197 if (pm->op_pmdynflags & PMdf_TAINTED)
1199 sv_magic(sv,(SV*)ReREFCNT_inc(PM_GETRE(pm)), PERL_MAGIC_qr,0,0);
1206 register PMOP *pm = cPMOP;
1208 register const char *t;
1209 register const char *s;
1212 I32 r_flags = REXEC_CHECKED;
1213 const char *truebase; /* Start of string */
1214 register REGEXP *rx = PM_GETRE(pm);
1216 const I32 gimme = GIMME;
1219 const I32 oldsave = PL_savestack_ix;
1220 I32 update_minmatch = 1;
1221 I32 had_zerolen = 0;
1223 if (PL_op->op_flags & OPf_STACKED)
1225 else if (PL_op->op_private & OPpTARGET_MY)
1232 PUTBACK; /* EVAL blocks need stack_sp. */
1233 s = SvPV_const(TARG, len);
1235 DIE(aTHX_ "panic: pp_match");
1237 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1238 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1241 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
1243 /* PMdf_USED is set after a ?? matches once */
1244 if (pm->op_pmdynflags & PMdf_USED) {
1246 if (gimme == G_ARRAY)
1251 /* empty pattern special-cased to use last successful pattern if possible */
1252 if (!rx->prelen && PL_curpm) {
1257 if (rx->minlen > (I32)len)
1262 /* XXXX What part of this is needed with true \G-support? */
1263 if ((global = dynpm->op_pmflags & PMf_GLOBAL)) {
1265 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1266 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1267 if (mg && mg->mg_len >= 0) {
1268 if (!(rx->reganch & ROPT_GPOS_SEEN))
1269 rx->endp[0] = rx->startp[0] = mg->mg_len;
1270 else if (rx->reganch & ROPT_ANCH_GPOS) {
1271 r_flags |= REXEC_IGNOREPOS;
1272 rx->endp[0] = rx->startp[0] = mg->mg_len;
1274 minmatch = (mg->mg_flags & MGf_MINMATCH);
1275 update_minmatch = 0;
1279 if ((!global && rx->nparens)
1280 || SvTEMP(TARG) || PL_sawampersand)
1281 r_flags |= REXEC_COPY_STR;
1283 r_flags |= REXEC_SCREAM;
1286 if (global && rx->startp[0] != -1) {
1287 t = s = rx->endp[0] + truebase;
1288 if ((s + rx->minlen) > strend)
1290 if (update_minmatch++)
1291 minmatch = had_zerolen;
1293 if (rx->reganch & RE_USE_INTUIT &&
1294 DO_UTF8(TARG) == ((rx->reganch & ROPT_UTF8) != 0)) {
1295 /* FIXME - can PL_bostr be made const char *? */
1296 PL_bostr = (char *)truebase;
1297 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, (char *)s, (char *)strend, r_flags, NULL);
1301 if ( (rx->reganch & ROPT_CHECK_ALL)
1303 && ((rx->reganch & ROPT_NOSCAN)
1304 || !((rx->reganch & RE_INTUIT_TAIL)
1305 && (r_flags & REXEC_SCREAM)))
1306 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
1309 if (CALLREGEXEC(aTHX_ rx, (char*)s, (char *)strend, (char*)truebase, minmatch, TARG, NULL, r_flags))
1312 if (dynpm->op_pmflags & PMf_ONCE)
1313 dynpm->op_pmdynflags |= PMdf_USED;
1322 RX_MATCH_TAINTED_on(rx);
1323 TAINT_IF(RX_MATCH_TAINTED(rx));
1324 if (gimme == G_ARRAY) {
1325 const I32 nparens = rx->nparens;
1326 I32 i = (global && !nparens) ? 1 : 0;
1328 SPAGAIN; /* EVAL blocks could move the stack. */
1329 EXTEND(SP, nparens + i);
1330 EXTEND_MORTAL(nparens + i);
1331 for (i = !i; i <= nparens; i++) {
1332 PUSHs(sv_newmortal());
1333 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1334 const I32 len = rx->endp[i] - rx->startp[i];
1335 s = rx->startp[i] + truebase;
1336 if (rx->endp[i] < 0 || rx->startp[i] < 0 ||
1337 len < 0 || len > strend - s)
1338 DIE(aTHX_ "panic: pp_match start/end pointers");
1339 sv_setpvn(*SP, s, len);
1340 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
1345 if (dynpm->op_pmflags & PMf_CONTINUE) {
1347 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1348 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1350 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1351 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1353 if (rx->startp[0] != -1) {
1354 mg->mg_len = rx->endp[0];
1355 if (rx->startp[0] == rx->endp[0])
1356 mg->mg_flags |= MGf_MINMATCH;
1358 mg->mg_flags &= ~MGf_MINMATCH;
1361 had_zerolen = (rx->startp[0] != -1
1362 && rx->startp[0] == rx->endp[0]);
1363 PUTBACK; /* EVAL blocks may use stack */
1364 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
1369 LEAVE_SCOPE(oldsave);
1375 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1376 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1378 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1379 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1381 if (rx->startp[0] != -1) {
1382 mg->mg_len = rx->endp[0];
1383 if (rx->startp[0] == rx->endp[0])
1384 mg->mg_flags |= MGf_MINMATCH;
1386 mg->mg_flags &= ~MGf_MINMATCH;
1389 LEAVE_SCOPE(oldsave);
1393 yup: /* Confirmed by INTUIT */
1395 RX_MATCH_TAINTED_on(rx);
1396 TAINT_IF(RX_MATCH_TAINTED(rx));
1398 if (dynpm->op_pmflags & PMf_ONCE)
1399 dynpm->op_pmdynflags |= PMdf_USED;
1400 if (RX_MATCH_COPIED(rx))
1401 Safefree(rx->subbeg);
1402 RX_MATCH_COPIED_off(rx);
1403 rx->subbeg = Nullch;
1405 /* FIXME - should rx->subbeg be const char *? */
1406 rx->subbeg = (char *) truebase;
1407 rx->startp[0] = s - truebase;
1408 if (RX_MATCH_UTF8(rx)) {
1409 char * const t = (char*)utf8_hop((U8*)s, rx->minlen);
1410 rx->endp[0] = t - truebase;
1413 rx->endp[0] = s - truebase + rx->minlen;
1415 rx->sublen = strend - truebase;
1418 if (PL_sawampersand) {
1420 #ifdef PERL_OLD_COPY_ON_WRITE
1421 if (SvIsCOW(TARG) || (SvFLAGS(TARG) & CAN_COW_MASK) == CAN_COW_FLAGS) {
1423 PerlIO_printf(Perl_debug_log,
1424 "Copy on write: pp_match $& capture, type %d, truebase=%p, t=%p, difference %d\n",
1425 (int) SvTYPE(TARG), truebase, t,
1428 rx->saved_copy = sv_setsv_cow(rx->saved_copy, TARG);
1429 rx->subbeg = (char *) SvPVX_const(rx->saved_copy) + (t - truebase);
1430 assert (SvPOKp(rx->saved_copy));
1435 rx->subbeg = savepvn(t, strend - t);
1436 #ifdef PERL_OLD_COPY_ON_WRITE
1437 rx->saved_copy = Nullsv;
1440 rx->sublen = strend - t;
1441 RX_MATCH_COPIED_on(rx);
1442 off = rx->startp[0] = s - t;
1443 rx->endp[0] = off + rx->minlen;
1445 else { /* startp/endp are used by @- @+. */
1446 rx->startp[0] = s - truebase;
1447 rx->endp[0] = s - truebase + rx->minlen;
1449 rx->nparens = rx->lastparen = rx->lastcloseparen = 0; /* used by @-, @+, and $^N */
1450 LEAVE_SCOPE(oldsave);
1455 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
1456 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1457 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1462 LEAVE_SCOPE(oldsave);
1463 if (gimme == G_ARRAY)
1469 Perl_do_readline(pTHX)
1471 dVAR; dSP; dTARGETSTACKED;
1476 register IO * const io = GvIO(PL_last_in_gv);
1477 register const I32 type = PL_op->op_type;
1478 const I32 gimme = GIMME_V;
1481 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
1483 XPUSHs(SvTIED_obj((SV*)io, mg));
1486 call_method("READLINE", gimme);
1489 if (gimme == G_SCALAR) {
1491 SvSetSV_nosteal(TARG, result);
1500 if (IoFLAGS(io) & IOf_ARGV) {
1501 if (IoFLAGS(io) & IOf_START) {
1503 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1504 IoFLAGS(io) &= ~IOf_START;
1505 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
1506 sv_setpvn(GvSVn(PL_last_in_gv), "-", 1);
1507 SvSETMAGIC(GvSV(PL_last_in_gv));
1512 fp = nextargv(PL_last_in_gv);
1513 if (!fp) { /* Note: fp != IoIFP(io) */
1514 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
1517 else if (type == OP_GLOB)
1518 fp = Perl_start_glob(aTHX_ POPs, io);
1520 else if (type == OP_GLOB)
1522 else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) {
1523 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
1527 if ((!io || !(IoFLAGS(io) & IOf_START))
1528 && ckWARN2(WARN_GLOB, WARN_CLOSED))
1530 if (type == OP_GLOB)
1531 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1532 "glob failed (can't start child: %s)",
1535 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
1537 if (gimme == G_SCALAR) {
1538 /* undef TARG, and push that undefined value */
1539 if (type != OP_RCATLINE) {
1540 SV_CHECK_THINKFIRST_COW_DROP(TARG);
1548 if (gimme == G_SCALAR) {
1552 SvUPGRADE(sv, SVt_PV);
1553 tmplen = SvLEN(sv); /* remember if already alloced */
1554 if (!tmplen && !SvREADONLY(sv))
1555 Sv_Grow(sv, 80); /* try short-buffering it */
1557 if (type == OP_RCATLINE && SvOK(sv)) {
1559 SvPV_force_nolen(sv);
1565 sv = sv_2mortal(NEWSV(57, 80));
1569 /* This should not be marked tainted if the fp is marked clean */
1570 #define MAYBE_TAINT_LINE(io, sv) \
1571 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1576 /* delay EOF state for a snarfed empty file */
1577 #define SNARF_EOF(gimme,rs,io,sv) \
1578 (gimme != G_SCALAR || SvCUR(sv) \
1579 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
1583 if (!sv_gets(sv, fp, offset)
1585 || SNARF_EOF(gimme, PL_rs, io, sv)
1586 || PerlIO_error(fp)))
1588 PerlIO_clearerr(fp);
1589 if (IoFLAGS(io) & IOf_ARGV) {
1590 fp = nextargv(PL_last_in_gv);
1593 (void)do_close(PL_last_in_gv, FALSE);
1595 else if (type == OP_GLOB) {
1596 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1597 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1598 "glob failed (child exited with status %d%s)",
1599 (int)(STATUS_CURRENT >> 8),
1600 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
1603 if (gimme == G_SCALAR) {
1604 if (type != OP_RCATLINE) {
1605 SV_CHECK_THINKFIRST_COW_DROP(TARG);
1611 MAYBE_TAINT_LINE(io, sv);
1614 MAYBE_TAINT_LINE(io, sv);
1616 IoFLAGS(io) |= IOf_NOLINE;
1620 if (type == OP_GLOB) {
1624 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
1625 tmps = SvEND(sv) - 1;
1626 if (*tmps == *SvPVX_const(PL_rs)) {
1628 SvCUR_set(sv, SvCUR(sv) - 1);
1631 for (t1 = SvPVX_const(sv); *t1; t1++)
1632 if (!isALPHA(*t1) && !isDIGIT(*t1) &&
1633 strchr("$&*(){}[]'\";\\|?<>~`", *t1))
1635 if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &PL_statbuf) < 0) {
1636 (void)POPs; /* Unmatched wildcard? Chuck it... */
1639 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
1640 const U8 *s = (const U8*)SvPVX_const(sv) + offset;
1641 const STRLEN len = SvCUR(sv) - offset;
1644 if (ckWARN(WARN_UTF8) &&
1645 !is_utf8_string_loc(s, len, &f))
1646 /* Emulate :encoding(utf8) warning in the same case. */
1647 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1648 "utf8 \"\\x%02X\" does not map to Unicode",
1649 f < (U8*)SvEND(sv) ? *f : 0);
1651 if (gimme == G_ARRAY) {
1652 if (SvLEN(sv) - SvCUR(sv) > 20) {
1653 SvPV_shrink_to_cur(sv);
1655 sv = sv_2mortal(NEWSV(58, 80));
1658 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1659 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1660 const STRLEN new_len
1661 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
1662 SvPV_renew(sv, new_len);
1671 register PERL_CONTEXT *cx;
1672 I32 gimme = OP_GIMME(PL_op, -1);
1675 if (cxstack_ix >= 0)
1676 gimme = cxstack[cxstack_ix].blk_gimme;
1684 PUSHBLOCK(cx, CXt_BLOCK, SP);
1696 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
1697 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
1699 const U32 hash = (SvIsCOW_shared_hash(keysv)) ? SvSHARED_HASH(keysv) : 0;
1702 if (SvTYPE(hv) == SVt_PVHV) {
1703 if (PL_op->op_private & OPpLVAL_INTRO) {
1706 /* does the element we're localizing already exist? */
1708 /* can we determine whether it exists? */
1710 || mg_find((SV*)hv, PERL_MAGIC_env)
1711 || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied))
1712 /* Try to preserve the existenceness of a tied hash
1713 * element by using EXISTS and DELETE if possible.
1714 * Fallback to FETCH and STORE otherwise */
1715 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
1716 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
1717 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
1719 ) ? hv_exists_ent(hv, keysv, 0) : 1;
1722 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
1723 svp = he ? &HeVAL(he) : 0;
1729 if (!svp || *svp == &PL_sv_undef) {
1733 DIE(aTHX_ PL_no_helem_sv, keysv);
1735 lv = sv_newmortal();
1736 sv_upgrade(lv, SVt_PVLV);
1738 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, Nullch, 0);
1739 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1740 LvTARG(lv) = SvREFCNT_inc(hv);
1745 if (PL_op->op_private & OPpLVAL_INTRO) {
1746 if (HvNAME_get(hv) && isGV(*svp))
1747 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1751 const char * const key = SvPV_const(keysv, keylen);
1752 SAVEDELETE(hv, savepvn(key,keylen), keylen);
1754 save_helem(hv, keysv, svp);
1757 else if (PL_op->op_private & OPpDEREF)
1758 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
1760 sv = (svp ? *svp : &PL_sv_undef);
1761 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1762 * Pushing the magical RHS on to the stack is useless, since
1763 * that magic is soon destined to be misled by the local(),
1764 * and thus the later pp_sassign() will fail to mg_get() the
1765 * old value. This should also cure problems with delayed
1766 * mg_get()s. GSAR 98-07-03 */
1767 if (!lval && SvGMAGICAL(sv))
1768 sv = sv_mortalcopy(sv);
1776 register PERL_CONTEXT *cx;
1781 if (PL_op->op_flags & OPf_SPECIAL) {
1782 cx = &cxstack[cxstack_ix];
1783 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
1788 gimme = OP_GIMME(PL_op, -1);
1790 if (cxstack_ix >= 0)
1791 gimme = cxstack[cxstack_ix].blk_gimme;
1797 if (gimme == G_VOID)
1799 else if (gimme == G_SCALAR) {
1803 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1806 *MARK = sv_mortalcopy(TOPs);
1809 *MARK = &PL_sv_undef;
1813 else if (gimme == G_ARRAY) {
1814 /* in case LEAVE wipes old return values */
1816 for (mark = newsp + 1; mark <= SP; mark++) {
1817 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
1818 *mark = sv_mortalcopy(*mark);
1819 TAINT_NOT; /* Each item is independent */
1823 PL_curpm = newpm; /* Don't pop $1 et al till now */
1833 register PERL_CONTEXT *cx;
1839 cx = &cxstack[cxstack_ix];
1840 if (CxTYPE(cx) != CXt_LOOP)
1841 DIE(aTHX_ "panic: pp_iter");
1843 itersvp = CxITERVAR(cx);
1844 av = cx->blk_loop.iterary;
1845 if (SvTYPE(av) != SVt_PVAV) {
1846 /* iterate ($min .. $max) */
1847 if (cx->blk_loop.iterlval) {
1848 /* string increment */
1849 register SV* cur = cx->blk_loop.iterlval;
1851 const char *max = SvOK((SV*)av) ? SvPV_const((SV*)av, maxlen) : "";
1852 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1853 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1854 /* safe to reuse old SV */
1855 sv_setsv(*itersvp, cur);
1859 /* we need a fresh SV every time so that loop body sees a
1860 * completely new SV for closures/references to work as
1863 *itersvp = newSVsv(cur);
1864 SvREFCNT_dec(oldsv);
1866 if (strEQ(SvPVX_const(cur), max))
1867 sv_setiv(cur, 0); /* terminate next time */
1874 /* integer increment */
1875 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1878 /* don't risk potential race */
1879 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1880 /* safe to reuse old SV */
1881 sv_setiv(*itersvp, cx->blk_loop.iterix++);
1885 /* we need a fresh SV every time so that loop body sees a
1886 * completely new SV for closures/references to work as they
1889 *itersvp = newSViv(cx->blk_loop.iterix++);
1890 SvREFCNT_dec(oldsv);
1896 if (PL_op->op_private & OPpITER_REVERSED) {
1897 /* In reverse, use itermax as the min :-) */
1898 if (cx->blk_loop.iterix <= cx->blk_loop.itermax)
1901 if (SvMAGICAL(av) || AvREIFY(av)) {
1902 SV ** const svp = av_fetch(av, --cx->blk_loop.iterix, FALSE);
1903 sv = svp ? *svp : Nullsv;
1906 sv = AvARRAY(av)[--cx->blk_loop.iterix];
1910 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp :
1914 if (SvMAGICAL(av) || AvREIFY(av)) {
1915 SV ** const svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE);
1916 sv = svp ? *svp : Nullsv;
1919 sv = AvARRAY(av)[++cx->blk_loop.iterix];
1923 if (sv && SvIS_FREED(sv)) {
1925 Perl_croak(aTHX_ "Use of freed value in iteration");
1932 if (av != PL_curstack && sv == &PL_sv_undef) {
1933 SV *lv = cx->blk_loop.iterlval;
1934 if (lv && SvREFCNT(lv) > 1) {
1939 SvREFCNT_dec(LvTARG(lv));
1941 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
1942 sv_upgrade(lv, SVt_PVLV);
1944 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
1946 LvTARG(lv) = SvREFCNT_inc(av);
1947 LvTARGOFF(lv) = cx->blk_loop.iterix;
1948 LvTARGLEN(lv) = (STRLEN)UV_MAX;
1953 *itersvp = SvREFCNT_inc(sv);
1954 SvREFCNT_dec(oldsv);
1962 register PMOP *pm = cPMOP;
1978 register REGEXP *rx = PM_GETRE(pm);
1980 int force_on_match = 0;
1981 const I32 oldsave = PL_savestack_ix;
1983 bool doutf8 = FALSE;
1984 #ifdef PERL_OLD_COPY_ON_WRITE
1989 /* known replacement string? */
1990 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
1991 if (PL_op->op_flags & OPf_STACKED)
1993 else if (PL_op->op_private & OPpTARGET_MY)
2000 #ifdef PERL_OLD_COPY_ON_WRITE
2001 /* Awooga. Awooga. "bool" types that are actually char are dangerous,
2002 because they make integers such as 256 "false". */
2003 is_cow = SvIsCOW(TARG) ? TRUE : FALSE;
2006 sv_force_normal_flags(TARG,0);
2009 #ifdef PERL_OLD_COPY_ON_WRITE
2013 || ( (SvTYPE(TARG) == SVt_PVGV || SvTYPE(TARG) > SVt_PVLV)
2014 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
2015 DIE(aTHX_ PL_no_modify);
2018 s = SvPV_mutable(TARG, len);
2019 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
2021 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
2022 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
2027 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
2031 DIE(aTHX_ "panic: pp_subst");
2034 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
2035 maxiters = 2 * slen + 10; /* We can match twice at each
2036 position, once with zero-length,
2037 second time with non-zero. */
2039 if (!rx->prelen && PL_curpm) {
2043 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
2044 ? REXEC_COPY_STR : 0;
2046 r_flags |= REXEC_SCREAM;
2049 if (rx->reganch & RE_USE_INTUIT) {
2051 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
2055 /* How to do it in subst? */
2056 /* if ( (rx->reganch & ROPT_CHECK_ALL)
2058 && ((rx->reganch & ROPT_NOSCAN)
2059 || !((rx->reganch & RE_INTUIT_TAIL)
2060 && (r_flags & REXEC_SCREAM))))
2065 /* only replace once? */
2066 once = !(rpm->op_pmflags & PMf_GLOBAL);
2068 /* known replacement string? */
2070 /* replacement needing upgrading? */
2071 if (DO_UTF8(TARG) && !doutf8) {
2072 nsv = sv_newmortal();
2075 sv_recode_to_utf8(nsv, PL_encoding);
2077 sv_utf8_upgrade(nsv);
2078 c = SvPV_const(nsv, clen);
2082 c = SvPV_const(dstr, clen);
2083 doutf8 = DO_UTF8(dstr);
2091 /* can do inplace substitution? */
2093 #ifdef PERL_OLD_COPY_ON_WRITE
2096 && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
2097 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)
2098 && (!doutf8 || SvUTF8(TARG))) {
2099 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2100 r_flags | REXEC_CHECKED))
2104 LEAVE_SCOPE(oldsave);
2107 #ifdef PERL_OLD_COPY_ON_WRITE
2108 if (SvIsCOW(TARG)) {
2109 assert (!force_on_match);
2113 if (force_on_match) {
2115 s = SvPV_force(TARG, len);
2120 SvSCREAM_off(TARG); /* disable possible screamer */
2122 rxtainted |= RX_MATCH_TAINTED(rx);
2123 m = orig + rx->startp[0];
2124 d = orig + rx->endp[0];
2126 if (m - s > strend - d) { /* faster to shorten from end */
2128 Copy(c, m, clen, char);
2133 Move(d, m, i, char);
2137 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;
2169 Move(s, d, i, char);
2173 Copy(c, d, clen, char);
2176 s = rx->endp[0] + orig;
2177 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2179 /* don't match same null twice */
2180 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
2183 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
2184 Move(s, d, i+1, char); /* include the NUL */
2186 TAINT_IF(rxtainted & 1);
2188 PUSHs(sv_2mortal(newSViv((I32)iters)));
2190 (void)SvPOK_only_UTF8(TARG);
2191 TAINT_IF(rxtainted);
2192 if (SvSMAGICAL(TARG)) {
2200 LEAVE_SCOPE(oldsave);
2204 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2205 r_flags | REXEC_CHECKED))
2207 if (force_on_match) {
2209 s = SvPV_force(TARG, len);
2212 #ifdef PERL_OLD_COPY_ON_WRITE
2215 rxtainted |= RX_MATCH_TAINTED(rx);
2216 dstr = newSVpvn(m, s-m);
2221 register PERL_CONTEXT *cx;
2223 (void)ReREFCNT_inc(rx);
2225 RETURNOP(cPMOP->op_pmreplroot);
2227 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
2229 if (iters++ > maxiters)
2230 DIE(aTHX_ "Substitution loop");
2231 rxtainted |= RX_MATCH_TAINTED(rx);
2232 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
2237 strend = s + (strend - m);
2239 m = rx->startp[0] + orig;
2240 if (doutf8 && !SvUTF8(dstr))
2241 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
2243 sv_catpvn(dstr, s, m-s);
2244 s = rx->endp[0] + orig;
2246 sv_catpvn(dstr, c, clen);
2249 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2250 TARG, NULL, r_flags));
2251 if (doutf8 && !DO_UTF8(TARG))
2252 sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv);
2254 sv_catpvn(dstr, s, strend - s);
2256 #ifdef PERL_OLD_COPY_ON_WRITE
2257 /* The match may make the string COW. If so, brilliant, because that's
2258 just saved us one malloc, copy and free - the regexp has donated
2259 the old buffer, and we malloc an entirely new one, rather than the
2260 regexp malloc()ing a buffer and copying our original, only for
2261 us to throw it away here during the substitution. */
2262 if (SvIsCOW(TARG)) {
2263 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
2269 SvPV_set(TARG, SvPVX(dstr));
2270 SvCUR_set(TARG, SvCUR(dstr));
2271 SvLEN_set(TARG, SvLEN(dstr));
2272 doutf8 |= DO_UTF8(dstr);
2273 SvPV_set(dstr, (char*)0);
2276 TAINT_IF(rxtainted & 1);
2278 PUSHs(sv_2mortal(newSViv((I32)iters)));
2280 (void)SvPOK_only(TARG);
2283 TAINT_IF(rxtainted);
2286 LEAVE_SCOPE(oldsave);
2295 LEAVE_SCOPE(oldsave);
2304 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2305 ++*PL_markstack_ptr;
2306 LEAVE; /* exit inner scope */
2309 if (PL_stack_base + *PL_markstack_ptr > SP) {
2311 const I32 gimme = GIMME_V;
2313 LEAVE; /* exit outer scope */
2314 (void)POPMARK; /* pop src */
2315 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
2316 (void)POPMARK; /* pop dst */
2317 SP = PL_stack_base + POPMARK; /* pop original mark */
2318 if (gimme == G_SCALAR) {
2319 if (PL_op->op_private & OPpGREP_LEX) {
2320 SV* const sv = sv_newmortal();
2321 sv_setiv(sv, items);
2329 else if (gimme == G_ARRAY)
2336 ENTER; /* enter inner scope */
2339 src = PL_stack_base[*PL_markstack_ptr];
2341 if (PL_op->op_private & OPpGREP_LEX)
2342 PAD_SVl(PL_op->op_targ) = src;
2346 RETURNOP(cLOGOP->op_other);
2357 register PERL_CONTEXT *cx;
2360 if (CxMULTICALL(&cxstack[cxstack_ix]))
2364 cxstack_ix++; /* temporarily protect top context */
2367 if (gimme == G_SCALAR) {
2370 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2372 *MARK = SvREFCNT_inc(TOPs);
2377 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2379 *MARK = sv_mortalcopy(sv);
2384 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2388 *MARK = &PL_sv_undef;
2392 else if (gimme == G_ARRAY) {
2393 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2394 if (!SvTEMP(*MARK)) {
2395 *MARK = sv_mortalcopy(*MARK);
2396 TAINT_NOT; /* Each item is independent */
2404 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2405 PL_curpm = newpm; /* ... and pop $1 et al */
2408 return cx->blk_sub.retop;
2411 /* This duplicates the above code because the above code must not
2412 * get any slower by more conditions */
2420 register PERL_CONTEXT *cx;
2423 if (CxMULTICALL(&cxstack[cxstack_ix]))
2427 cxstack_ix++; /* temporarily protect top context */
2431 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2432 /* We are an argument to a function or grep().
2433 * This kind of lvalueness was legal before lvalue
2434 * subroutines too, so be backward compatible:
2435 * cannot report errors. */
2437 /* Scalar context *is* possible, on the LHS of -> only,
2438 * as in f()->meth(). But this is not an lvalue. */
2439 if (gimme == G_SCALAR)
2441 if (gimme == G_ARRAY) {
2442 if (!CvLVALUE(cx->blk_sub.cv))
2443 goto temporise_array;
2444 EXTEND_MORTAL(SP - newsp);
2445 for (mark = newsp + 1; mark <= SP; mark++) {
2448 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2449 *mark = sv_mortalcopy(*mark);
2451 /* Can be a localized value subject to deletion. */
2452 PL_tmps_stack[++PL_tmps_ix] = *mark;
2453 (void)SvREFCNT_inc(*mark);
2458 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2459 /* Here we go for robustness, not for speed, so we change all
2460 * the refcounts so the caller gets a live guy. Cannot set
2461 * TEMP, so sv_2mortal is out of question. */
2462 if (!CvLVALUE(cx->blk_sub.cv)) {
2468 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2470 if (gimme == G_SCALAR) {
2474 /* Temporaries are bad unless they happen to be elements
2475 * of a tied hash or array */
2476 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY) &&
2477 !(SvRMAGICAL(TOPs) && mg_find(TOPs, PERL_MAGIC_tiedelem))) {
2483 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2484 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2485 : "a readonly value" : "a temporary");
2487 else { /* Can be a localized value
2488 * subject to deletion. */
2489 PL_tmps_stack[++PL_tmps_ix] = *mark;
2490 (void)SvREFCNT_inc(*mark);
2493 else { /* Should not happen? */
2499 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
2500 (MARK > SP ? "Empty array" : "Array"));
2504 else if (gimme == G_ARRAY) {
2505 EXTEND_MORTAL(SP - newsp);
2506 for (mark = newsp + 1; mark <= SP; mark++) {
2507 if (*mark != &PL_sv_undef
2508 && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2509 /* Might be flattened array after $#array = */
2516 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2517 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2520 /* Can be a localized value subject to deletion. */
2521 PL_tmps_stack[++PL_tmps_ix] = *mark;
2522 (void)SvREFCNT_inc(*mark);
2528 if (gimme == G_SCALAR) {
2532 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2534 *MARK = SvREFCNT_inc(TOPs);
2539 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2541 *MARK = sv_mortalcopy(sv);
2546 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2550 *MARK = &PL_sv_undef;
2554 else if (gimme == G_ARRAY) {
2556 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2557 if (!SvTEMP(*MARK)) {
2558 *MARK = sv_mortalcopy(*MARK);
2559 TAINT_NOT; /* Each item is independent */
2568 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2569 PL_curpm = newpm; /* ... and pop $1 et al */
2572 return cx->blk_sub.retop;
2577 S_get_db_sub(pTHX_ SV **svp, CV *cv)
2579 SV * const dbsv = GvSVn(PL_DBsub);
2582 if (!PERLDB_SUB_NN) {
2585 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2586 || strEQ(GvNAME(gv), "END")
2587 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2588 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2589 && (gv = (GV*)*svp) ))) {
2590 /* Use GV from the stack as a fallback. */
2591 /* GV is potentially non-unique, or contain different CV. */
2592 SV * const tmp = newRV((SV*)cv);
2593 sv_setsv(dbsv, tmp);
2597 gv_efullname3(dbsv, gv, Nullch);
2601 const int type = SvTYPE(dbsv);
2602 if (type < SVt_PVIV && type != SVt_IV)
2603 sv_upgrade(dbsv, SVt_PVIV);
2604 (void)SvIOK_on(dbsv);
2605 SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */
2609 PL_curcopdb = PL_curcop;
2610 cv = GvCV(PL_DBsub);
2620 register PERL_CONTEXT *cx;
2622 const bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
2625 DIE(aTHX_ "Not a CODE reference");
2626 switch (SvTYPE(sv)) {
2627 /* This is overwhelming the most common case: */
2629 if (!(cv = GvCVu((GV*)sv)))
2630 cv = sv_2cv(sv, &stash, &gv, FALSE);
2640 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2642 SP = PL_stack_base + POPMARK;
2645 if (SvGMAGICAL(sv)) {
2649 sym = SvPOKp(sv) ? SvPVX_const(sv) : Nullch;
2652 sym = SvPV_nolen_const(sv);
2655 DIE(aTHX_ PL_no_usym, "a subroutine");
2656 if (PL_op->op_private & HINT_STRICT_REFS)
2657 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
2658 cv = get_cv(sym, TRUE);
2663 SV * const * sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2664 tryAMAGICunDEREF(to_cv);
2667 if (SvTYPE(cv) == SVt_PVCV)
2672 DIE(aTHX_ "Not a CODE reference");
2673 /* This is the second most common case: */
2683 if (!CvROOT(cv) && !CvXSUB(cv)) {
2688 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
2689 if (CvASSERTION(cv) && PL_DBassertion)
2690 sv_setiv(PL_DBassertion, 1);
2692 cv = get_db_sub(&sv, cv);
2693 if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
2694 DIE(aTHX_ "No DB::sub routine defined");
2697 if (!(CvXSUB(cv))) {
2698 /* This path taken at least 75% of the time */
2700 register I32 items = SP - MARK;
2701 AV* const padlist = CvPADLIST(cv);
2702 PUSHBLOCK(cx, CXt_SUB, MARK);
2704 cx->blk_sub.retop = PL_op->op_next;
2706 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2707 * that eval'' ops within this sub know the correct lexical space.
2708 * Owing the speed considerations, we choose instead to search for
2709 * the cv using find_runcv() when calling doeval().
2711 if (CvDEPTH(cv) >= 2) {
2712 PERL_STACK_OVERFLOW_CHECK();
2713 pad_push(padlist, CvDEPTH(cv));
2716 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2719 AV* const av = (AV*)PAD_SVl(0);
2721 /* @_ is normally not REAL--this should only ever
2722 * happen when DB::sub() calls things that modify @_ */
2727 cx->blk_sub.savearray = GvAV(PL_defgv);
2728 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2729 CX_CURPAD_SAVE(cx->blk_sub);
2730 cx->blk_sub.argarray = av;
2733 if (items > AvMAX(av) + 1) {
2734 SV **ary = AvALLOC(av);
2735 if (AvARRAY(av) != ary) {
2736 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2737 SvPV_set(av, (char*)ary);
2739 if (items > AvMAX(av) + 1) {
2740 AvMAX(av) = items - 1;
2741 Renew(ary,items,SV*);
2743 SvPV_set(av, (char*)ary);
2746 Copy(MARK,AvARRAY(av),items,SV*);
2747 AvFILLp(av) = items - 1;
2755 /* warning must come *after* we fully set up the context
2756 * stuff so that __WARN__ handlers can safely dounwind()
2759 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2760 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2761 sub_crush_depth(cv);
2763 DEBUG_S(PerlIO_printf(Perl_debug_log,
2764 "%p entersub returning %p\n", thr, CvSTART(cv)));
2766 RETURNOP(CvSTART(cv));
2769 #ifdef PERL_XSUB_OLDSTYLE
2770 if (CvOLDSTYLE(cv)) {
2771 I32 (*fp3)(int,int,int);
2773 register I32 items = SP - MARK;
2774 /* We dont worry to copy from @_. */
2779 PL_stack_sp = mark + 1;
2780 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2781 items = (*fp3)(CvXSUBANY(cv).any_i32,
2782 MARK - PL_stack_base + 1,
2784 PL_stack_sp = PL_stack_base + items;
2787 #endif /* PERL_XSUB_OLDSTYLE */
2789 I32 markix = TOPMARK;
2794 /* Need to copy @_ to stack. Alternative may be to
2795 * switch stack to @_, and copy return values
2796 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2797 AV * const av = GvAV(PL_defgv);
2798 const I32 items = AvFILLp(av) + 1; /* @_ is not tieable */
2801 /* Mark is at the end of the stack. */
2803 Copy(AvARRAY(av), SP + 1, items, SV*);
2808 /* We assume first XSUB in &DB::sub is the called one. */
2810 SAVEVPTR(PL_curcop);
2811 PL_curcop = PL_curcopdb;
2814 /* Do we need to open block here? XXXX */
2815 (void)(*CvXSUB(cv))(aTHX_ cv);
2817 /* Enforce some sanity in scalar context. */
2818 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2819 if (markix > PL_stack_sp - PL_stack_base)
2820 *(PL_stack_base + markix) = &PL_sv_undef;
2822 *(PL_stack_base + markix) = *PL_stack_sp;
2823 PL_stack_sp = PL_stack_base + markix;
2831 assert (0); /* Cannot get here. */
2832 /* This is deliberately moved here as spaghetti code to keep it out of the
2839 /* anonymous or undef'd function leaves us no recourse */
2840 if (CvANON(cv) || !(gv = CvGV(cv)))
2841 DIE(aTHX_ "Undefined subroutine called");
2843 /* autoloaded stub? */
2844 if (cv != GvCV(gv)) {
2847 /* should call AUTOLOAD now? */
2850 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2857 sub_name = sv_newmortal();
2858 gv_efullname3(sub_name, gv, Nullch);
2859 DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name);
2863 DIE(aTHX_ "Not a CODE reference");
2869 Perl_sub_crush_depth(pTHX_ CV *cv)
2872 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
2874 SV* const tmpstr = sv_newmortal();
2875 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2876 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
2885 SV* const elemsv = POPs;
2886 IV elem = SvIV(elemsv);
2887 AV* const av = (AV*)POPs;
2888 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2889 const U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > av_len(av));
2892 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
2893 Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv);
2895 elem -= PL_curcop->cop_arybase;
2896 if (SvTYPE(av) != SVt_PVAV)
2898 svp = av_fetch(av, elem, lval && !defer);
2900 #ifdef PERL_MALLOC_WRAP
2901 if (SvUOK(elemsv)) {
2902 const UV uv = SvUV(elemsv);
2903 elem = uv > IV_MAX ? IV_MAX : uv;
2905 else if (SvNOK(elemsv))
2906 elem = (IV)SvNV(elemsv);
2908 static const char oom_array_extend[] =
2909 "Out of memory during array extend"; /* Duplicated in av.c */
2910 MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
2913 if (!svp || *svp == &PL_sv_undef) {
2916 DIE(aTHX_ PL_no_aelem, elem);
2917 lv = sv_newmortal();
2918 sv_upgrade(lv, SVt_PVLV);
2920 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
2921 LvTARG(lv) = SvREFCNT_inc(av);
2922 LvTARGOFF(lv) = elem;
2927 if (PL_op->op_private & OPpLVAL_INTRO)
2928 save_aelem(av, elem, svp);
2929 else if (PL_op->op_private & OPpDEREF)
2930 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
2932 sv = (svp ? *svp : &PL_sv_undef);
2933 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2934 sv = sv_mortalcopy(sv);
2940 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
2945 Perl_croak(aTHX_ PL_no_modify);
2946 if (SvTYPE(sv) < SVt_RV)
2947 sv_upgrade(sv, SVt_RV);
2948 else if (SvTYPE(sv) >= SVt_PV) {
2955 SvRV_set(sv, NEWSV(355,0));
2958 SvRV_set(sv, (SV*)newAV());
2961 SvRV_set(sv, (SV*)newHV());
2972 SV* const sv = TOPs;
2975 SV* const rsv = SvRV(sv);
2976 if (SvTYPE(rsv) == SVt_PVCV) {
2982 SETs(method_common(sv, Null(U32*)));
2989 SV* const sv = cSVOP_sv;
2990 U32 hash = SvSHARED_HASH(sv);
2992 XPUSHs(method_common(sv, &hash));
2997 S_method_common(pTHX_ SV* meth, U32* hashp)
3003 const char* packname = Nullch;
3004 SV *packsv = Nullsv;
3006 const char * const name = SvPV_const(meth, namelen);
3007 SV * const sv = *(PL_stack_base + TOPMARK + 1);
3010 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
3018 /* this isn't a reference */
3019 if(SvOK(sv) && (packname = SvPV_const(sv, packlen))) {
3020 const HE* const he = hv_fetch_ent(PL_stashcache, sv, 0, 0);
3022 stash = INT2PTR(HV*,SvIV(HeVAL(he)));
3029 !(iogv = gv_fetchsv(sv, FALSE, SVt_PVIO)) ||
3030 !(ob=(SV*)GvIO(iogv)))
3032 /* this isn't the name of a filehandle either */
3034 ((UTF8_IS_START(*packname) && DO_UTF8(sv))
3035 ? !isIDFIRST_utf8((U8*)packname)
3036 : !isIDFIRST(*packname)
3039 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
3040 SvOK(sv) ? "without a package or object reference"
3041 : "on an undefined value");
3043 /* assume it's a package name */
3044 stash = gv_stashpvn(packname, packlen, FALSE);
3048 SV* ref = newSViv(PTR2IV(stash));
3049 hv_store(PL_stashcache, packname, packlen, ref, 0);
3053 /* it _is_ a filehandle name -- replace with a reference */
3054 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
3057 /* if we got here, ob should be a reference or a glob */
3058 if (!ob || !(SvOBJECT(ob)
3059 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
3062 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
3066 stash = SvSTASH(ob);
3069 /* NOTE: stash may be null, hope hv_fetch_ent and
3070 gv_fetchmethod can cope (it seems they can) */
3072 /* shortcut for simple names */
3074 const HE* const he = hv_fetch_ent(stash, meth, 0, *hashp);
3076 gv = (GV*)HeVAL(he);
3077 if (isGV(gv) && GvCV(gv) &&
3078 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
3079 return (SV*)GvCV(gv);
3083 gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name);
3086 /* This code tries to figure out just what went wrong with
3087 gv_fetchmethod. It therefore needs to duplicate a lot of
3088 the internals of that function. We can't move it inside
3089 Perl_gv_fetchmethod_autoload(), however, since that would
3090 cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we
3093 const char* leaf = name;
3094 const char* sep = Nullch;
3097 for (p = name; *p; p++) {
3099 sep = p, leaf = p + 1;
3100 else if (*p == ':' && *(p + 1) == ':')
3101 sep = p, leaf = p + 2;
3103 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3104 /* the method name is unqualified or starts with SUPER:: */
3105 bool need_strlen = 1;
3107 packname = CopSTASHPV(PL_curcop);
3110 HEK * const packhek = HvNAME_HEK(stash);
3112 packname = HEK_KEY(packhek);
3113 packlen = HEK_LEN(packhek);
3123 "Can't use anonymous symbol table for method lookup");
3125 else if (need_strlen)
3126 packlen = strlen(packname);
3130 /* the method name is qualified */
3132 packlen = sep - name;
3135 /* we're relying on gv_fetchmethod not autovivifying the stash */
3136 if (gv_stashpvn(packname, packlen, FALSE)) {
3138 "Can't locate object method \"%s\" via package \"%.*s\"",
3139 leaf, (int)packlen, packname);
3143 "Can't locate object method \"%s\" via package \"%.*s\""
3144 " (perhaps you forgot to load \"%.*s\"?)",
3145 leaf, (int)packlen, packname, (int)packlen, packname);
3148 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
3153 * c-indentation-style: bsd
3155 * indent-tabs-mode: t
3158 * ex: set ts=8 sts=4 sw=4 noet: