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
38 #ifdef USE_5005THREADS
39 static void unset_cvowner(pTHX_ void *cvarg);
40 #endif /* USE_5005THREADS */
51 PL_curcop = (COP*)PL_op;
52 TAINT_NOT; /* Each statement is presumed innocent */
53 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
62 if (PL_op->op_private & OPpLVAL_INTRO)
63 PUSHs(save_scalar(cGVOP_gv));
65 PUSHs(GvSV(cGVOP_gv));
76 PL_curcop = (COP*)PL_op;
82 PUSHMARK(PL_stack_sp);
97 XPUSHs((SV*)cGVOP_gv);
108 RETURNOP(cLOGOP->op_other);
116 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
118 temp = left; left = right; right = temp;
120 if (PL_tainting && PL_tainted && !SvTAINTED(left))
122 SvSetMagicSV(right, left);
131 RETURNOP(cLOGOP->op_other);
133 RETURNOP(cLOGOP->op_next);
139 TAINT_NOT; /* Each statement is presumed innocent */
140 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
142 oldsave = PL_scopestack[PL_scopestack_ix - 1];
143 LEAVE_SCOPE(oldsave);
149 dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
154 const char *rpv = SvPV(right, rlen); /* mg_get(right) happens here */
155 const bool rbyte = !DO_UTF8(right);
156 bool rcopied = FALSE;
158 if (TARG == right && right != left) {
159 right = sv_2mortal(newSVpvn(rpv, rlen));
160 rpv = SvPV(right, rlen); /* no point setting UTF-8 here */
166 const char* const lpv = SvPV(left, llen); /* mg_get(left) may happen here */
167 lbyte = !DO_UTF8(left);
168 sv_setpvn(TARG, lpv, llen);
174 else { /* TARG == left */
176 if (SvGMAGICAL(left))
177 mg_get(left); /* or mg_get(left) may happen here */
179 sv_setpvn(left, "", 0);
180 (void)SvPV_nomg(left, llen); /* Needed to set UTF8 flag */
181 lbyte = !DO_UTF8(left);
186 #if defined(PERL_Y2KWARN)
187 if ((SvIOK(right) || SvNOK(right)) && ckWARN(WARN_Y2K) && SvOK(TARG)) {
188 if (llen >= 2 && lpv[llen - 2] == '1' && lpv[llen - 1] == '9'
189 && (llen == 2 || !isDIGIT(lpv[llen - 3])))
191 Perl_warner(aTHX_ packWARN(WARN_Y2K), "Possible Y2K bug: %s",
192 "about to append an integer to '19'");
197 if (lbyte != rbyte) {
199 sv_utf8_upgrade_nomg(TARG);
202 right = sv_2mortal(newSVpvn(rpv, rlen));
203 sv_utf8_upgrade_nomg(right);
204 rpv = SvPV(right, rlen);
207 sv_catpvn_nomg(TARG, rpv, rlen);
218 if (PL_op->op_flags & OPf_MOD) {
219 if (PL_op->op_private & OPpLVAL_INTRO)
220 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
221 else if (PL_op->op_private & OPpDEREF) {
223 vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
232 tryAMAGICunTARGET(iter, 0);
233 PL_last_in_gv = (GV*)(*PL_stack_sp--);
234 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
235 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
236 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
239 XPUSHs((SV*)PL_last_in_gv);
242 PL_last_in_gv = (GV*)(*PL_stack_sp--);
245 return do_readline();
250 dSP; tryAMAGICbinSET(eq,0);
251 #ifndef NV_PRESERVES_UV
252 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
254 SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s)));
258 #ifdef PERL_PRESERVE_IVUV
261 /* Unless the left argument is integer in range we are going
262 to have to use NV maths. Hence only attempt to coerce the
263 right argument if we know the left is integer. */
266 bool auvok = SvUOK(TOPm1s);
267 bool buvok = SvUOK(TOPs);
269 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
270 /* Casting IV to UV before comparison isn't going to matter
271 on 2s complement. On 1s complement or sign&magnitude
272 (if we have any of them) it could to make negative zero
273 differ from normal zero. As I understand it. (Need to
274 check - is negative zero implementation defined behaviour
276 UV buv = SvUVX(POPs);
277 UV auv = SvUVX(TOPs);
279 SETs(boolSV(auv == buv));
282 { /* ## Mixed IV,UV ## */
286 /* == is commutative so doesn't matter which is left or right */
288 /* top of stack (b) is the iv */
297 /* As uv is a UV, it's >0, so it cannot be == */
301 /* we know iv is >= 0 */
302 SETs(boolSV((UV)iv == SvUVX(uvp)));
310 SETs(boolSV(TOPn == value));
318 if (SvTYPE(TOPs) > SVt_PVLV)
319 DIE(aTHX_ PL_no_modify);
320 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
321 && SvIVX(TOPs) != IV_MAX)
323 SvIV_set(TOPs, SvIVX(TOPs) + 1);
324 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
326 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
339 RETURNOP(cLOGOP->op_other);
345 dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
346 useleft = USE_LEFT(TOPm1s);
347 #ifdef PERL_PRESERVE_IVUV
348 /* We must see if we can perform the addition with integers if possible,
349 as the integer code detects overflow while the NV code doesn't.
350 If either argument hasn't had a numeric conversion yet attempt to get
351 the IV. It's important to do this now, rather than just assuming that
352 it's not IOK as a PV of "9223372036854775806" may not take well to NV
353 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
354 integer in case the second argument is IV=9223372036854775806
355 We can (now) rely on sv_2iv to do the right thing, only setting the
356 public IOK flag if the value in the NV (or PV) slot is truly integer.
358 A side effect is that this also aggressively prefers integer maths over
359 fp maths for integer values.
361 How to detect overflow?
363 C 99 section 6.2.6.1 says
365 The range of nonnegative values of a signed integer type is a subrange
366 of the corresponding unsigned integer type, and the representation of
367 the same value in each type is the same. A computation involving
368 unsigned operands can never overflow, because a result that cannot be
369 represented by the resulting unsigned integer type is reduced modulo
370 the number that is one greater than the largest value that can be
371 represented by the resulting type.
375 which I read as "unsigned ints wrap."
377 signed integer overflow seems to be classed as "exception condition"
379 If an exceptional condition occurs during the evaluation of an
380 expression (that is, if the result is not mathematically defined or not
381 in the range of representable values for its type), the behavior is
384 (6.5, the 5th paragraph)
386 I had assumed that on 2s complement machines signed arithmetic would
387 wrap, hence coded pp_add and pp_subtract on the assumption that
388 everything perl builds on would be happy. After much wailing and
389 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
390 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
391 unsigned code below is actually shorter than the old code. :-)
396 /* Unless the left argument is integer in range we are going to have to
397 use NV maths. Hence only attempt to coerce the right argument if
398 we know the left is integer. */
406 /* left operand is undef, treat as zero. + 0 is identity,
407 Could SETi or SETu right now, but space optimise by not adding
408 lots of code to speed up what is probably a rarish case. */
410 /* Left operand is defined, so is it IV? */
413 if ((auvok = SvUOK(TOPm1s)))
416 register IV aiv = SvIVX(TOPm1s);
419 auvok = 1; /* Now acting as a sign flag. */
420 } else { /* 2s complement assumption for IV_MIN */
428 bool result_good = 0;
431 bool buvok = SvUOK(TOPs);
436 register IV biv = SvIVX(TOPs);
443 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
444 else "IV" now, independent of how it came in.
445 if a, b represents positive, A, B negative, a maps to -A etc
450 all UV maths. negate result if A negative.
451 add if signs same, subtract if signs differ. */
457 /* Must get smaller */
463 /* result really should be -(auv-buv). as its negation
464 of true value, need to swap our result flag */
481 if (result <= (UV)IV_MIN)
484 /* result valid, but out of range for IV. */
489 } /* Overflow, drop through to NVs. */
496 /* left operand is undef, treat as zero. + 0.0 is identity. */
500 SETn( value + TOPn );
508 AV *av = PL_op->op_flags & OPf_SPECIAL ?
509 (AV*)PAD_SV(PL_op->op_targ) : GvAV(cGVOP_gv);
510 U32 lval = PL_op->op_flags & OPf_MOD;
511 SV** svp = av_fetch(av, PL_op->op_private, lval);
512 SV *sv = (svp ? *svp : &PL_sv_undef);
514 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
515 sv = sv_mortalcopy(sv);
524 do_join(TARG, *MARK, MARK, SP);
535 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
536 * will be enough to hold an OP*.
538 SV* sv = sv_newmortal();
539 sv_upgrade(sv, SVt_PVLV);
541 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
549 /* Oversized hot code. */
553 dSP; dMARK; dORIGMARK;
559 if (PL_op->op_flags & OPf_STACKED)
564 if (gv && (io = GvIO(gv))
565 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
568 if (MARK == ORIGMARK) {
569 /* If using default handle then we need to make space to
570 * pass object as 1st arg, so move other args up ...
574 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
578 *MARK = SvTIED_obj((SV*)io, mg);
581 call_method("PRINT", G_SCALAR);
589 if (!(io = GvIO(gv))) {
590 if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv)))
591 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
593 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
594 report_evil_fh(gv, io, PL_op->op_type);
595 SETERRNO(EBADF,RMS_IFI);
598 else if (!(fp = IoOFP(io))) {
599 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
601 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
602 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
603 report_evil_fh(gv, io, PL_op->op_type);
605 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
610 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
612 if (!do_print(*MARK, fp))
616 if (!do_print(PL_ofs_sv, fp)) { /* $, */
625 if (!do_print(*MARK, fp))
633 if (PL_ors_sv && SvOK(PL_ors_sv))
634 if (!do_print(PL_ors_sv, fp)) /* $\ */
637 if (IoFLAGS(io) & IOf_FLUSH)
638 if (PerlIO_flush(fp) == EOF)
659 tryAMAGICunDEREF(to_av);
662 if (SvTYPE(av) != SVt_PVAV)
663 DIE(aTHX_ "Not an ARRAY reference");
664 if (PL_op->op_flags & OPf_REF) {
669 if (GIMME == G_SCALAR)
670 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
674 else if (PL_op->op_flags & OPf_MOD
675 && PL_op->op_private & OPpLVAL_INTRO)
676 Perl_croak(aTHX_ PL_no_localize_ref);
679 if (SvTYPE(sv) == SVt_PVAV) {
681 if (PL_op->op_flags & OPf_REF) {
686 if (GIMME == G_SCALAR)
687 Perl_croak(aTHX_ "Can't return array to lvalue"
696 if (SvTYPE(sv) != SVt_PVGV) {
700 if (SvGMAGICAL(sv)) {
706 if (PL_op->op_flags & OPf_REF ||
707 PL_op->op_private & HINT_STRICT_REFS)
708 DIE(aTHX_ PL_no_usym, "an ARRAY");
709 if (ckWARN(WARN_UNINITIALIZED))
711 if (GIMME == G_ARRAY) {
718 if ((PL_op->op_flags & OPf_SPECIAL) &&
719 !(PL_op->op_flags & OPf_MOD))
721 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVAV);
723 && (!is_gv_magical(sym,len,0)
724 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV))))
730 if (PL_op->op_private & HINT_STRICT_REFS)
731 DIE(aTHX_ PL_no_symref, sym, "an ARRAY");
732 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
739 if (PL_op->op_private & OPpLVAL_INTRO)
741 if (PL_op->op_flags & OPf_REF) {
746 if (GIMME == G_SCALAR)
747 Perl_croak(aTHX_ "Can't return array to lvalue"
755 if (GIMME == G_ARRAY) {
756 I32 maxarg = AvFILL(av) + 1;
757 (void)POPs; /* XXXX May be optimized away? */
759 if (SvRMAGICAL(av)) {
761 for (i=0; i < (U32)maxarg; i++) {
762 SV **svp = av_fetch(av, i, FALSE);
763 /* See note in pp_helem, and bug id #27839 */
765 ? SvGMAGICAL(*svp) ? sv_mortalcopy(*svp) : *svp
770 Copy(AvARRAY(av), SP+1, maxarg, SV*);
774 else if (GIMME_V == G_SCALAR) {
776 I32 maxarg = AvFILL(av) + 1;
790 tryAMAGICunDEREF(to_hv);
793 if (SvTYPE(hv) != SVt_PVHV && SvTYPE(hv) != SVt_PVAV)
794 DIE(aTHX_ "Not a HASH reference");
795 if (PL_op->op_flags & OPf_REF) {
800 if (gimme != G_ARRAY)
801 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
805 else if (PL_op->op_flags & OPf_MOD
806 && PL_op->op_private & OPpLVAL_INTRO)
807 Perl_croak(aTHX_ PL_no_localize_ref);
810 if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) {
812 if (PL_op->op_flags & OPf_REF) {
817 if (gimme != G_ARRAY)
818 Perl_croak(aTHX_ "Can't return hash to lvalue"
827 if (SvTYPE(sv) != SVt_PVGV) {
831 if (SvGMAGICAL(sv)) {
837 if (PL_op->op_flags & OPf_REF ||
838 PL_op->op_private & HINT_STRICT_REFS)
839 DIE(aTHX_ PL_no_usym, "a HASH");
840 if (ckWARN(WARN_UNINITIALIZED))
842 if (gimme == G_ARRAY) {
849 if ((PL_op->op_flags & OPf_SPECIAL) &&
850 !(PL_op->op_flags & OPf_MOD))
852 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVHV);
854 && (!is_gv_magical(sym,len,0)
855 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV))))
861 if (PL_op->op_private & HINT_STRICT_REFS)
862 DIE(aTHX_ PL_no_symref, sym, "a HASH");
863 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
870 if (PL_op->op_private & OPpLVAL_INTRO)
872 if (PL_op->op_flags & OPf_REF) {
877 if (gimme != G_ARRAY)
878 Perl_croak(aTHX_ "Can't return hash to lvalue"
886 if (gimme == G_ARRAY) { /* array wanted */
887 *PL_stack_sp = (SV*)hv;
890 else if (gimme == G_SCALAR) {
893 if (SvTYPE(hv) == SVt_PVAV)
894 hv = avhv_keys((AV*)hv);
896 TARG = Perl_hv_scalar(aTHX_ hv);
903 S_do_maybe_phash(pTHX_ AV *ary, SV **lelem, SV **firstlelem, SV **relem,
909 leftop = ((BINOP*)PL_op)->op_last;
911 assert(leftop->op_type == OP_NULL && leftop->op_targ == OP_LIST);
912 leftop = ((LISTOP*)leftop)->op_first;
914 /* Skip PUSHMARK and each element already assigned to. */
915 for (i = lelem - firstlelem; i > 0; i--) {
916 leftop = leftop->op_sibling;
919 if (leftop->op_type != OP_RV2HV)
924 av_fill(ary, 0); /* clear all but the fields hash */
925 if (lastrelem >= relem) {
926 while (relem < lastrelem) { /* gobble up all the rest */
930 /* Avoid a memory leak when avhv_store_ent dies. */
931 tmpstr = sv_newmortal();
932 sv_setsv(tmpstr,relem[1]); /* value */
934 if (avhv_store_ent(ary,relem[0],tmpstr,0))
935 (void)SvREFCNT_inc(tmpstr);
936 if (SvMAGICAL(ary) != 0 && SvSMAGICAL(tmpstr))
942 if (relem == lastrelem)
948 S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
952 if (ckWARN(WARN_MISC)) {
953 if (relem == firstrelem &&
955 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
956 SvTYPE(SvRV(*relem)) == SVt_PVHV))
958 Perl_warner(aTHX_ packWARN(WARN_MISC),
959 "Reference found where even-sized list expected");
962 Perl_warner(aTHX_ packWARN(WARN_MISC),
963 "Odd number of elements in hash assignment");
965 if (SvTYPE(hash) == SVt_PVAV) {
967 tmpstr = sv_newmortal();
968 if (avhv_store_ent((AV*)hash,*relem,tmpstr,0))
969 (void)SvREFCNT_inc(tmpstr);
970 if (SvMAGICAL(hash) && SvSMAGICAL(tmpstr))
975 tmpstr = NEWSV(29,0);
976 didstore = hv_store_ent(hash,*relem,tmpstr,0);
977 if (SvMAGICAL(hash)) {
978 if (SvSMAGICAL(tmpstr))
991 SV **lastlelem = PL_stack_sp;
992 SV **lastrelem = PL_stack_base + POPMARK;
993 SV **firstrelem = PL_stack_base + POPMARK + 1;
994 SV **firstlelem = lastrelem + 1;
1007 SV **firsthashrelem = 0; /* "= 0" keeps gcc 2.95 quiet */
1010 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
1013 /* If there's a common identifier on both sides we have to take
1014 * special care that assigning the identifier on the left doesn't
1015 * clobber a value on the right that's used later in the list.
1017 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
1018 EXTEND_MORTAL(lastrelem - firstrelem + 1);
1019 for (relem = firstrelem; relem <= lastrelem; relem++) {
1021 if ((sv = *relem)) {
1022 TAINT_NOT; /* Each item is independent */
1023 *relem = sv_mortalcopy(sv);
1033 while (lelem <= lastlelem) {
1034 TAINT_NOT; /* Each item stands on its own, taintwise. */
1036 switch (SvTYPE(sv)) {
1039 magic = SvMAGICAL(ary) != 0;
1040 if (PL_op->op_private & OPpASSIGN_HASH) {
1041 switch (do_maybe_phash(ary, lelem, firstlelem, relem,
1047 do_oddball((HV*)ary, relem, firstrelem);
1049 relem = lastrelem + 1;
1054 av_extend(ary, lastrelem - relem);
1056 while (relem <= lastrelem) { /* gobble up all the rest */
1059 sv = newSVsv(*relem);
1061 didstore = av_store(ary,i++,sv);
1071 case SVt_PVHV: { /* normal hash */
1075 magic = SvMAGICAL(hash) != 0;
1077 firsthashrelem = relem;
1079 while (relem < lastrelem) { /* gobble up all the rest */
1084 sv = &PL_sv_no, relem++;
1085 tmpstr = NEWSV(29,0);
1087 sv_setsv(tmpstr,*relem); /* value */
1088 *(relem++) = tmpstr;
1089 if (gimme != G_VOID && hv_exists_ent(hash, sv, 0))
1090 /* key overwrites an existing entry */
1092 didstore = hv_store_ent(hash,sv,tmpstr,0);
1094 if (SvSMAGICAL(tmpstr))
1101 if (relem == lastrelem) {
1102 do_oddball(hash, relem, firstrelem);
1108 if (SvIMMORTAL(sv)) {
1109 if (relem <= lastrelem)
1113 if (relem <= lastrelem) {
1114 sv_setsv(sv, *relem);
1118 sv_setsv(sv, &PL_sv_undef);
1123 if (PL_delaymagic & ~DM_DELAY) {
1124 if (PL_delaymagic & DM_UID) {
1125 #ifdef HAS_SETRESUID
1126 (void)setresuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1127 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1,
1130 # ifdef HAS_SETREUID
1131 (void)setreuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1132 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1);
1135 if ((PL_delaymagic & DM_UID) == DM_RUID) {
1136 (void)setruid(PL_uid);
1137 PL_delaymagic &= ~DM_RUID;
1139 # endif /* HAS_SETRUID */
1141 if ((PL_delaymagic & DM_UID) == DM_EUID) {
1142 (void)seteuid(PL_euid);
1143 PL_delaymagic &= ~DM_EUID;
1145 # endif /* HAS_SETEUID */
1146 if (PL_delaymagic & DM_UID) {
1147 if (PL_uid != PL_euid)
1148 DIE(aTHX_ "No setreuid available");
1149 (void)PerlProc_setuid(PL_uid);
1151 # endif /* HAS_SETREUID */
1152 #endif /* HAS_SETRESUID */
1153 PL_uid = PerlProc_getuid();
1154 PL_euid = PerlProc_geteuid();
1156 if (PL_delaymagic & DM_GID) {
1157 #ifdef HAS_SETRESGID
1158 (void)setresgid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1159 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1,
1162 # ifdef HAS_SETREGID
1163 (void)setregid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1164 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1);
1167 if ((PL_delaymagic & DM_GID) == DM_RGID) {
1168 (void)setrgid(PL_gid);
1169 PL_delaymagic &= ~DM_RGID;
1171 # endif /* HAS_SETRGID */
1173 if ((PL_delaymagic & DM_GID) == DM_EGID) {
1174 (void)setegid(PL_egid);
1175 PL_delaymagic &= ~DM_EGID;
1177 # endif /* HAS_SETEGID */
1178 if (PL_delaymagic & DM_GID) {
1179 if (PL_gid != PL_egid)
1180 DIE(aTHX_ "No setregid available");
1181 (void)PerlProc_setgid(PL_gid);
1183 # endif /* HAS_SETREGID */
1184 #endif /* HAS_SETRESGID */
1185 PL_gid = PerlProc_getgid();
1186 PL_egid = PerlProc_getegid();
1188 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1192 if (gimme == G_VOID)
1193 SP = firstrelem - 1;
1194 else if (gimme == G_SCALAR) {
1197 SETi(lastrelem - firstrelem + 1 - duplicates);
1204 /* Removes from the stack the entries which ended up as
1205 * duplicated keys in the hash (fix for [perl #24380]) */
1206 Move(firsthashrelem + duplicates,
1207 firsthashrelem, duplicates, SV**);
1208 lastrelem -= duplicates;
1213 SP = firstrelem + (lastlelem - firstlelem);
1214 lelem = firstlelem + (relem - firstrelem);
1216 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
1224 register PMOP *pm = cPMOP;
1225 SV *rv = sv_newmortal();
1226 SV *sv = newSVrv(rv, "Regexp");
1227 if (pm->op_pmdynflags & PMdf_TAINTED)
1229 sv_magic(sv,(SV*)ReREFCNT_inc(PM_GETRE(pm)), PERL_MAGIC_qr,0,0);
1236 register PMOP *pm = cPMOP;
1242 I32 r_flags = REXEC_CHECKED;
1243 char *truebase; /* Start of string */
1244 register REGEXP *rx = PM_GETRE(pm);
1249 I32 oldsave = PL_savestack_ix;
1250 I32 update_minmatch = 1;
1251 I32 had_zerolen = 0;
1253 if (PL_op->op_flags & OPf_STACKED)
1260 PUTBACK; /* EVAL blocks need stack_sp. */
1261 s = SvPV(TARG, len);
1264 DIE(aTHX_ "panic: pp_match");
1265 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1266 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1269 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
1271 /* PMdf_USED is set after a ?? matches once */
1272 if (pm->op_pmdynflags & PMdf_USED) {
1274 if (gimme == G_ARRAY)
1279 /* empty pattern special-cased to use last successful pattern if possible */
1280 if (!rx->prelen && PL_curpm) {
1285 if (rx->minlen > (I32)len)
1290 /* XXXX What part of this is needed with true \G-support? */
1291 if ((global = dynpm->op_pmflags & PMf_GLOBAL)) {
1293 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1294 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1295 if (mg && mg->mg_len >= 0) {
1296 if (!(rx->reganch & ROPT_GPOS_SEEN))
1297 rx->endp[0] = rx->startp[0] = mg->mg_len;
1298 else if (rx->reganch & ROPT_ANCH_GPOS) {
1299 r_flags |= REXEC_IGNOREPOS;
1300 rx->endp[0] = rx->startp[0] = mg->mg_len;
1302 minmatch = (mg->mg_flags & MGf_MINMATCH);
1303 update_minmatch = 0;
1307 if ((!global && rx->nparens)
1308 || SvTEMP(TARG) || PL_sawampersand)
1309 r_flags |= REXEC_COPY_STR;
1311 r_flags |= REXEC_SCREAM;
1313 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1314 SAVEINT(PL_multiline);
1315 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
1319 if (global && rx->startp[0] != -1) {
1320 t = s = rx->endp[0] + truebase;
1321 if ((s + rx->minlen) > strend)
1323 if (update_minmatch++)
1324 minmatch = had_zerolen;
1326 if (rx->reganch & RE_USE_INTUIT &&
1327 DO_UTF8(TARG) == ((rx->reganch & ROPT_UTF8) != 0)) {
1328 PL_bostr = truebase;
1329 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1333 if ( (rx->reganch & ROPT_CHECK_ALL)
1335 && ((rx->reganch & ROPT_NOSCAN)
1336 || !((rx->reganch & RE_INTUIT_TAIL)
1337 && (r_flags & REXEC_SCREAM)))
1338 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
1341 if (CALLREGEXEC(aTHX_ rx, s, strend, truebase, minmatch, TARG, NULL, r_flags))
1344 if (dynpm->op_pmflags & PMf_ONCE)
1345 dynpm->op_pmdynflags |= PMdf_USED;
1354 RX_MATCH_TAINTED_on(rx);
1355 TAINT_IF(RX_MATCH_TAINTED(rx));
1356 if (gimme == G_ARRAY) {
1357 I32 nparens, i, len;
1359 nparens = rx->nparens;
1360 if (global && !nparens)
1364 SPAGAIN; /* EVAL blocks could move the stack. */
1365 EXTEND(SP, nparens + i);
1366 EXTEND_MORTAL(nparens + i);
1367 for (i = !i; i <= nparens; i++) {
1368 PUSHs(sv_newmortal());
1370 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1371 len = rx->endp[i] - rx->startp[i];
1372 s = rx->startp[i] + truebase;
1373 if (rx->endp[i] < 0 || rx->startp[i] < 0 ||
1374 len < 0 || len > strend - s)
1375 DIE(aTHX_ "panic: pp_match start/end pointers");
1376 sv_setpvn(*SP, s, len);
1377 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
1382 if (dynpm->op_pmflags & PMf_CONTINUE) {
1384 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1385 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1387 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1388 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1390 if (rx->startp[0] != -1) {
1391 mg->mg_len = rx->endp[0];
1392 if (rx->startp[0] == rx->endp[0])
1393 mg->mg_flags |= MGf_MINMATCH;
1395 mg->mg_flags &= ~MGf_MINMATCH;
1398 had_zerolen = (rx->startp[0] != -1
1399 && rx->startp[0] == rx->endp[0]);
1400 PUTBACK; /* EVAL blocks may use stack */
1401 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
1406 LEAVE_SCOPE(oldsave);
1412 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1413 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1415 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1416 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1418 if (rx->startp[0] != -1) {
1419 mg->mg_len = rx->endp[0];
1420 if (rx->startp[0] == rx->endp[0])
1421 mg->mg_flags |= MGf_MINMATCH;
1423 mg->mg_flags &= ~MGf_MINMATCH;
1426 LEAVE_SCOPE(oldsave);
1430 yup: /* Confirmed by INTUIT */
1432 RX_MATCH_TAINTED_on(rx);
1433 TAINT_IF(RX_MATCH_TAINTED(rx));
1435 if (dynpm->op_pmflags & PMf_ONCE)
1436 dynpm->op_pmdynflags |= PMdf_USED;
1437 if (RX_MATCH_COPIED(rx))
1438 Safefree(rx->subbeg);
1439 RX_MATCH_COPIED_off(rx);
1440 rx->subbeg = Nullch;
1442 rx->subbeg = truebase;
1443 rx->startp[0] = s - truebase;
1444 if (RX_MATCH_UTF8(rx)) {
1445 char *t = (char*)utf8_hop((U8*)s, rx->minlen);
1446 rx->endp[0] = t - truebase;
1449 rx->endp[0] = s - truebase + rx->minlen;
1451 rx->sublen = strend - truebase;
1454 if (PL_sawampersand) {
1457 rx->subbeg = savepvn(t, strend - t);
1458 rx->sublen = strend - t;
1459 RX_MATCH_COPIED_on(rx);
1460 off = rx->startp[0] = s - t;
1461 rx->endp[0] = off + rx->minlen;
1463 else { /* startp/endp are used by @- @+. */
1464 rx->startp[0] = s - truebase;
1465 rx->endp[0] = s - truebase + rx->minlen;
1467 rx->nparens = rx->lastparen = rx->lastcloseparen = 0; /* used by @-, @+, and $^N */
1468 LEAVE_SCOPE(oldsave);
1473 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
1474 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1475 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1480 LEAVE_SCOPE(oldsave);
1481 if (gimme == G_ARRAY)
1487 Perl_do_readline(pTHX)
1489 dSP; dTARGETSTACKED;
1494 register IO *io = GvIO(PL_last_in_gv);
1495 register I32 type = PL_op->op_type;
1496 I32 gimme = GIMME_V;
1499 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
1501 XPUSHs(SvTIED_obj((SV*)io, mg));
1504 call_method("READLINE", gimme);
1507 if (gimme == G_SCALAR) {
1509 SvSetSV_nosteal(TARG, result);
1518 if (IoFLAGS(io) & IOf_ARGV) {
1519 if (IoFLAGS(io) & IOf_START) {
1521 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1522 IoFLAGS(io) &= ~IOf_START;
1523 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
1524 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1525 SvSETMAGIC(GvSV(PL_last_in_gv));
1530 fp = nextargv(PL_last_in_gv);
1531 if (!fp) { /* Note: fp != IoIFP(io) */
1532 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
1535 else if (type == OP_GLOB)
1536 fp = Perl_start_glob(aTHX_ POPs, io);
1538 else if (type == OP_GLOB)
1540 else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) {
1541 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
1545 if (ckWARN2(WARN_GLOB, WARN_CLOSED)
1546 && (!io || !(IoFLAGS(io) & IOf_START))) {
1547 if (type == OP_GLOB)
1548 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1549 "glob failed (can't start child: %s)",
1552 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
1554 if (gimme == G_SCALAR) {
1555 /* undef TARG, and push that undefined value */
1556 if (type != OP_RCATLINE) {
1557 SV_CHECK_THINKFIRST(TARG);
1565 if (gimme == G_SCALAR) {
1569 (void)SvUPGRADE(sv, SVt_PV);
1570 tmplen = SvLEN(sv); /* remember if already alloced */
1571 if (!tmplen && !SvREADONLY(sv))
1572 Sv_Grow(sv, 80); /* try short-buffering it */
1574 if (type == OP_RCATLINE && SvOK(sv)) {
1577 (void)SvPV_force(sv, n_a);
1583 sv = sv_2mortal(NEWSV(57, 80));
1587 /* This should not be marked tainted if the fp is marked clean */
1588 #define MAYBE_TAINT_LINE(io, sv) \
1589 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1594 /* delay EOF state for a snarfed empty file */
1595 #define SNARF_EOF(gimme,rs,io,sv) \
1596 (gimme != G_SCALAR || SvCUR(sv) \
1597 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
1601 if (!sv_gets(sv, fp, offset)
1603 || SNARF_EOF(gimme, PL_rs, io, sv)
1604 || PerlIO_error(fp)))
1606 PerlIO_clearerr(fp);
1607 if (IoFLAGS(io) & IOf_ARGV) {
1608 fp = nextargv(PL_last_in_gv);
1611 (void)do_close(PL_last_in_gv, FALSE);
1613 else if (type == OP_GLOB) {
1614 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1615 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1616 "glob failed (child exited with status %d%s)",
1617 (int)(STATUS_CURRENT >> 8),
1618 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
1621 if (gimme == G_SCALAR) {
1622 if (type != OP_RCATLINE) {
1623 SV_CHECK_THINKFIRST(TARG);
1629 MAYBE_TAINT_LINE(io, sv);
1632 MAYBE_TAINT_LINE(io, sv);
1634 IoFLAGS(io) |= IOf_NOLINE;
1638 if (type == OP_GLOB) {
1641 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
1642 tmps = SvEND(sv) - 1;
1643 if (*tmps == *SvPVX(PL_rs)) {
1645 SvCUR_set(sv, SvCUR(sv) - 1);
1648 for (tmps = SvPVX(sv); *tmps; tmps++)
1649 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1650 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1652 if (*tmps && PerlLIO_lstat(SvPVX(sv), &PL_statbuf) < 0) {
1653 (void)POPs; /* Unmatched wildcard? Chuck it... */
1656 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
1657 const U8 *s = (U8*)SvPVX(sv) + offset;
1658 const STRLEN len = SvCUR(sv) - offset;
1661 if (ckWARN(WARN_UTF8) &&
1662 !Perl_is_utf8_string_loc(aTHX_ (U8 *) s, len, (U8 **) &f))
1663 /* Emulate :encoding(utf8) warning in the same case. */
1664 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1665 "utf8 \"\\x%02X\" does not map to Unicode",
1666 f < (U8*)SvEND(sv) ? *f : 0);
1668 if (gimme == G_ARRAY) {
1669 if (SvLEN(sv) - SvCUR(sv) > 20) {
1670 SvPV_shrink_to_cur(sv);
1672 sv = sv_2mortal(NEWSV(58, 80));
1675 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1676 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1677 const STRLEN new_len
1678 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
1679 SvPV_renew(sv, new_len);
1688 register PERL_CONTEXT *cx;
1689 I32 gimme = OP_GIMME(PL_op, -1);
1692 if (cxstack_ix >= 0)
1693 gimme = cxstack[cxstack_ix].blk_gimme;
1701 PUSHBLOCK(cx, CXt_BLOCK, SP);
1713 U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
1714 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
1716 U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
1719 if (SvTYPE(hv) == SVt_PVHV) {
1720 if (PL_op->op_private & OPpLVAL_INTRO) {
1723 /* does the element we're localizing already exist? */
1725 /* can we determine whether it exists? */
1727 || mg_find((SV*)hv, PERL_MAGIC_env)
1728 || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied))
1729 /* Try to preserve the existenceness of a tied hash
1730 * element by using EXISTS and DELETE if possible.
1731 * Fallback to FETCH and STORE otherwise */
1732 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
1733 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
1734 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
1736 ) ? hv_exists_ent(hv, keysv, 0) : 1;
1739 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
1740 svp = he ? &HeVAL(he) : 0;
1742 else if (SvTYPE(hv) == SVt_PVAV) {
1743 if (PL_op->op_private & OPpLVAL_INTRO)
1744 DIE(aTHX_ "Can't localize pseudo-hash element");
1745 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, hash);
1751 if (!svp || *svp == &PL_sv_undef) {
1755 DIE(aTHX_ PL_no_helem_sv, keysv);
1757 lv = sv_newmortal();
1758 sv_upgrade(lv, SVt_PVLV);
1760 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, Nullch, 0);
1761 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1762 LvTARG(lv) = SvREFCNT_inc(hv);
1767 if (PL_op->op_private & OPpLVAL_INTRO) {
1768 if (HvNAME_get(hv) && isGV(*svp))
1769 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1773 char *key = SvPV(keysv, keylen);
1774 SAVEDELETE(hv, savepvn(key,keylen), keylen);
1776 save_helem(hv, keysv, svp);
1779 else if (PL_op->op_private & OPpDEREF)
1780 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
1782 sv = (svp ? *svp : &PL_sv_undef);
1783 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1784 * Pushing the magical RHS on to the stack is useless, since
1785 * that magic is soon destined to be misled by the local(),
1786 * and thus the later pp_sassign() will fail to mg_get() the
1787 * old value. This should also cure problems with delayed
1788 * mg_get()s. GSAR 98-07-03 */
1789 if (!lval && SvGMAGICAL(sv))
1790 sv = sv_mortalcopy(sv);
1798 register PERL_CONTEXT *cx;
1804 if (PL_op->op_flags & OPf_SPECIAL) {
1805 cx = &cxstack[cxstack_ix];
1806 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
1811 gimme = OP_GIMME(PL_op, -1);
1813 if (cxstack_ix >= 0)
1814 gimme = cxstack[cxstack_ix].blk_gimme;
1820 if (gimme == G_VOID)
1822 else if (gimme == G_SCALAR) {
1825 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1828 *MARK = sv_mortalcopy(TOPs);
1831 *MARK = &PL_sv_undef;
1835 else if (gimme == G_ARRAY) {
1836 /* in case LEAVE wipes old return values */
1837 for (mark = newsp + 1; mark <= SP; mark++) {
1838 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
1839 *mark = sv_mortalcopy(*mark);
1840 TAINT_NOT; /* Each item is independent */
1844 PL_curpm = newpm; /* Don't pop $1 et al till now */
1854 register PERL_CONTEXT *cx;
1860 cx = &cxstack[cxstack_ix];
1861 if (CxTYPE(cx) != CXt_LOOP)
1862 DIE(aTHX_ "panic: pp_iter");
1864 itersvp = CxITERVAR(cx);
1865 av = cx->blk_loop.iterary;
1866 if (SvTYPE(av) != SVt_PVAV) {
1867 /* iterate ($min .. $max) */
1868 if (cx->blk_loop.iterlval) {
1869 /* string increment */
1870 register SV* cur = cx->blk_loop.iterlval;
1872 const char *max = SvOK((SV*)av) ? SvPV((SV*)av, maxlen) : "";
1873 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1874 #ifndef USE_5005THREADS /* don't risk potential race */
1875 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1876 /* safe to reuse old SV */
1877 sv_setsv(*itersvp, cur);
1882 /* we need a fresh SV every time so that loop body sees a
1883 * completely new SV for closures/references to work as
1886 *itersvp = newSVsv(cur);
1887 SvREFCNT_dec(oldsv);
1889 if (strEQ(SvPVX(cur), max))
1890 sv_setiv(cur, 0); /* terminate next time */
1897 /* integer increment */
1898 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1901 #ifndef USE_5005THREADS /* don't risk potential race */
1902 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1903 /* safe to reuse old SV */
1904 sv_setiv(*itersvp, cx->blk_loop.iterix++);
1909 /* we need a fresh SV every time so that loop body sees a
1910 * completely new SV for closures/references to work as they
1913 *itersvp = newSViv(cx->blk_loop.iterix++);
1914 SvREFCNT_dec(oldsv);
1920 if (PL_op->op_private & OPpITER_REVERSED) {
1921 /* In reverse, use itermax as the min :-) */
1922 if (cx->blk_loop.iterix <= cx->blk_loop.itermax)
1925 if (SvMAGICAL(av) || AvREIFY(av)) {
1926 SV **svp = av_fetch(av, cx->blk_loop.iterix--, FALSE);
1933 sv = AvARRAY(av)[cx->blk_loop.iterix--];
1937 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp :
1941 if (SvMAGICAL(av) || AvREIFY(av)) {
1942 SV **svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE);
1949 sv = AvARRAY(av)[++cx->blk_loop.iterix];
1953 if (sv && SvREFCNT(sv) == 0) {
1955 Perl_croak(aTHX_ "Use of freed value in iteration");
1962 if (av != PL_curstack && sv == &PL_sv_undef) {
1963 SV *lv = cx->blk_loop.iterlval;
1964 if (lv && SvREFCNT(lv) > 1) {
1969 SvREFCNT_dec(LvTARG(lv));
1971 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
1972 sv_upgrade(lv, SVt_PVLV);
1974 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
1976 LvTARG(lv) = SvREFCNT_inc(av);
1977 LvTARGOFF(lv) = cx->blk_loop.iterix;
1978 LvTARGLEN(lv) = (STRLEN)UV_MAX;
1983 *itersvp = SvREFCNT_inc(sv);
1984 SvREFCNT_dec(oldsv);
1992 register PMOP *pm = cPMOP;
2008 register REGEXP *rx = PM_GETRE(pm);
2010 int force_on_match = 0;
2011 I32 oldsave = PL_savestack_ix;
2013 bool doutf8 = FALSE;
2016 /* known replacement string? */
2017 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
2018 if (PL_op->op_flags & OPf_STACKED)
2025 if (SvFAKE(TARG) && SvREADONLY(TARG))
2026 sv_force_normal(TARG);
2027 if (SvREADONLY(TARG)
2028 || (SvTYPE(TARG) > SVt_PVLV
2029 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
2030 DIE(aTHX_ PL_no_modify);
2033 s = SvPV(TARG, len);
2034 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
2036 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
2037 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
2042 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
2046 DIE(aTHX_ "panic: pp_subst");
2049 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
2050 maxiters = 2 * slen + 10; /* We can match twice at each
2051 position, once with zero-length,
2052 second time with non-zero. */
2054 if (!rx->prelen && PL_curpm) {
2058 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
2059 ? REXEC_COPY_STR : 0;
2061 r_flags |= REXEC_SCREAM;
2062 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
2063 SAVEINT(PL_multiline);
2064 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
2067 if (rx->reganch & RE_USE_INTUIT) {
2069 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
2073 /* How to do it in subst? */
2074 /* if ( (rx->reganch & ROPT_CHECK_ALL)
2076 && ((rx->reganch & ROPT_NOSCAN)
2077 || !((rx->reganch & RE_INTUIT_TAIL)
2078 && (r_flags & REXEC_SCREAM))))
2083 /* only replace once? */
2084 once = !(rpm->op_pmflags & PMf_GLOBAL);
2086 /* known replacement string? */
2088 /* replacement needing upgrading? */
2089 if (DO_UTF8(TARG) && !doutf8) {
2090 nsv = sv_newmortal();
2093 sv_recode_to_utf8(nsv, PL_encoding);
2095 sv_utf8_upgrade(nsv);
2096 c = SvPV(nsv, clen);
2100 c = SvPV(dstr, clen);
2101 doutf8 = DO_UTF8(dstr);
2109 /* can do inplace substitution? */
2110 if (c && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
2111 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)
2112 && (!doutf8 || SvUTF8(TARG))) {
2113 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2114 r_flags | REXEC_CHECKED))
2118 LEAVE_SCOPE(oldsave);
2121 if (force_on_match) {
2123 s = SvPV_force(TARG, len);
2128 SvSCREAM_off(TARG); /* disable possible screamer */
2130 rxtainted |= RX_MATCH_TAINTED(rx);
2131 m = orig + rx->startp[0];
2132 d = orig + rx->endp[0];
2134 if (m - s > strend - d) { /* faster to shorten from end */
2136 Copy(c, m, clen, char);
2141 Move(d, m, i, char);
2145 SvCUR_set(TARG, m - s);
2148 else if ((i = m - s)) { /* faster from front */
2156 Copy(c, m, clen, char);
2161 Copy(c, d, clen, char);
2166 TAINT_IF(rxtainted & 1);
2172 if (iters++ > maxiters)
2173 DIE(aTHX_ "Substitution loop");
2174 rxtainted |= RX_MATCH_TAINTED(rx);
2175 m = rx->startp[0] + orig;
2179 Move(s, d, i, char);
2183 Copy(c, d, clen, char);
2186 s = rx->endp[0] + orig;
2187 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2189 /* don't match same null twice */
2190 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
2193 SvCUR_set(TARG, d - SvPVX(TARG) + i);
2194 Move(s, d, i+1, char); /* include the NUL */
2196 TAINT_IF(rxtainted & 1);
2198 PUSHs(sv_2mortal(newSViv((I32)iters)));
2200 (void)SvPOK_only_UTF8(TARG);
2201 TAINT_IF(rxtainted);
2202 if (SvSMAGICAL(TARG)) {
2210 LEAVE_SCOPE(oldsave);
2214 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2215 r_flags | REXEC_CHECKED))
2217 if (force_on_match) {
2219 s = SvPV_force(TARG, len);
2222 rxtainted |= RX_MATCH_TAINTED(rx);
2223 dstr = newSVpvn(m, s-m);
2228 register PERL_CONTEXT *cx;
2232 RETURNOP(cPMOP->op_pmreplroot);
2234 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
2236 if (iters++ > maxiters)
2237 DIE(aTHX_ "Substitution loop");
2238 rxtainted |= RX_MATCH_TAINTED(rx);
2239 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
2244 strend = s + (strend - m);
2246 m = rx->startp[0] + orig;
2247 if (doutf8 && !SvUTF8(dstr))
2248 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
2250 sv_catpvn(dstr, s, m-s);
2251 s = rx->endp[0] + orig;
2253 sv_catpvn(dstr, c, clen);
2256 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2257 TARG, NULL, r_flags));
2258 if (doutf8 && !DO_UTF8(TARG))
2259 sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv);
2261 sv_catpvn(dstr, s, strend - s);
2264 SvPV_set(TARG, SvPVX(dstr));
2265 SvCUR_set(TARG, SvCUR(dstr));
2266 SvLEN_set(TARG, SvLEN(dstr));
2267 doutf8 |= DO_UTF8(dstr);
2268 SvPV_set(dstr, (char*)0);
2271 TAINT_IF(rxtainted & 1);
2273 PUSHs(sv_2mortal(newSViv((I32)iters)));
2275 (void)SvPOK_only(TARG);
2278 TAINT_IF(rxtainted);
2281 LEAVE_SCOPE(oldsave);
2290 LEAVE_SCOPE(oldsave);
2299 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2300 ++*PL_markstack_ptr;
2301 LEAVE; /* exit inner scope */
2304 if (PL_stack_base + *PL_markstack_ptr > SP) {
2306 I32 gimme = GIMME_V;
2308 LEAVE; /* exit outer scope */
2309 (void)POPMARK; /* pop src */
2310 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
2311 (void)POPMARK; /* pop dst */
2312 SP = PL_stack_base + POPMARK; /* pop original mark */
2313 if (gimme == G_SCALAR) {
2317 else if (gimme == G_ARRAY)
2324 ENTER; /* enter inner scope */
2327 src = PL_stack_base[*PL_markstack_ptr];
2331 RETURNOP(cLOGOP->op_other);
2342 register PERL_CONTEXT *cx;
2346 cxstack_ix++; /* temporarily protect top context */
2349 if (gimme == G_SCALAR) {
2352 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2354 *MARK = SvREFCNT_inc(TOPs);
2359 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2361 *MARK = sv_mortalcopy(sv);
2366 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2370 *MARK = &PL_sv_undef;
2374 else if (gimme == G_ARRAY) {
2375 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2376 if (!SvTEMP(*MARK)) {
2377 *MARK = sv_mortalcopy(*MARK);
2378 TAINT_NOT; /* Each item is independent */
2386 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2387 PL_curpm = newpm; /* ... and pop $1 et al */
2390 return pop_return();
2393 /* This duplicates the above code because the above code must not
2394 * get any slower by more conditions */
2402 register PERL_CONTEXT *cx;
2406 cxstack_ix++; /* temporarily protect top context */
2410 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2411 /* We are an argument to a function or grep().
2412 * This kind of lvalueness was legal before lvalue
2413 * subroutines too, so be backward compatible:
2414 * cannot report errors. */
2416 /* Scalar context *is* possible, on the LHS of -> only,
2417 * as in f()->meth(). But this is not an lvalue. */
2418 if (gimme == G_SCALAR)
2420 if (gimme == G_ARRAY) {
2421 if (!CvLVALUE(cx->blk_sub.cv))
2422 goto temporise_array;
2423 EXTEND_MORTAL(SP - newsp);
2424 for (mark = newsp + 1; mark <= SP; mark++) {
2427 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2428 *mark = sv_mortalcopy(*mark);
2430 /* Can be a localized value subject to deletion. */
2431 PL_tmps_stack[++PL_tmps_ix] = *mark;
2432 (void)SvREFCNT_inc(*mark);
2437 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2438 /* Here we go for robustness, not for speed, so we change all
2439 * the refcounts so the caller gets a live guy. Cannot set
2440 * TEMP, so sv_2mortal is out of question. */
2441 if (!CvLVALUE(cx->blk_sub.cv)) {
2447 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2449 if (gimme == G_SCALAR) {
2453 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2459 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2460 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2461 : "a readonly value" : "a temporary");
2463 else { /* Can be a localized value
2464 * subject to deletion. */
2465 PL_tmps_stack[++PL_tmps_ix] = *mark;
2466 (void)SvREFCNT_inc(*mark);
2469 else { /* Should not happen? */
2475 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
2476 (MARK > SP ? "Empty array" : "Array"));
2480 else if (gimme == G_ARRAY) {
2481 EXTEND_MORTAL(SP - newsp);
2482 for (mark = newsp + 1; mark <= SP; mark++) {
2483 if (*mark != &PL_sv_undef
2484 && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2485 /* Might be flattened array after $#array = */
2492 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2493 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2496 /* Can be a localized value subject to deletion. */
2497 PL_tmps_stack[++PL_tmps_ix] = *mark;
2498 (void)SvREFCNT_inc(*mark);
2504 if (gimme == G_SCALAR) {
2508 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2510 *MARK = SvREFCNT_inc(TOPs);
2515 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2517 *MARK = sv_mortalcopy(sv);
2522 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2526 *MARK = &PL_sv_undef;
2530 else if (gimme == G_ARRAY) {
2532 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2533 if (!SvTEMP(*MARK)) {
2534 *MARK = sv_mortalcopy(*MARK);
2535 TAINT_NOT; /* Each item is independent */
2544 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2545 PL_curpm = newpm; /* ... and pop $1 et al */
2548 return pop_return();
2553 S_get_db_sub(pTHX_ SV **svp, CV *cv)
2555 SV *dbsv = GvSV(PL_DBsub);
2558 if (!PERLDB_SUB_NN) {
2561 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2562 || strEQ(GvNAME(gv), "END")
2563 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2564 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2565 && (gv = (GV*)*svp) ))) {
2566 /* Use GV from the stack as a fallback. */
2567 /* GV is potentially non-unique, or contain different CV. */
2568 SV *tmp = newRV((SV*)cv);
2569 sv_setsv(dbsv, tmp);
2573 gv_efullname3(dbsv, gv, Nullch);
2577 const int type = SvTYPE(dbsv);
2578 if (type < SVt_PVIV && type != SVt_IV)
2579 sv_upgrade(dbsv, SVt_PVIV);
2580 (void)SvIOK_on(dbsv);
2581 SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */
2585 PL_curcopdb = PL_curcop;
2586 cv = GvCV(PL_DBsub);
2596 register PERL_CONTEXT *cx;
2598 const bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
2601 DIE(aTHX_ "Not a CODE reference");
2602 switch (SvTYPE(sv)) {
2606 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2608 SP = PL_stack_base + POPMARK;
2611 if (SvGMAGICAL(sv)) {
2615 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2619 sym = SvPV(sv, n_a);
2622 DIE(aTHX_ PL_no_usym, "a subroutine");
2623 if (PL_op->op_private & HINT_STRICT_REFS)
2624 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
2625 cv = get_cv(sym, TRUE);
2630 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2631 tryAMAGICunDEREF(to_cv);
2634 if (SvTYPE(cv) == SVt_PVCV)
2639 DIE(aTHX_ "Not a CODE reference");
2644 if (!(cv = GvCVu((GV*)sv)))
2645 cv = sv_2cv(sv, &stash, &gv, FALSE);
2658 if (!CvROOT(cv) && !CvXSUB(cv)) {
2662 /* anonymous or undef'd function leaves us no recourse */
2663 if (CvANON(cv) || !(gv = CvGV(cv)))
2664 DIE(aTHX_ "Undefined subroutine called");
2666 /* autoloaded stub? */
2667 if (cv != GvCV(gv)) {
2670 /* should call AUTOLOAD now? */
2673 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2680 sub_name = sv_newmortal();
2681 gv_efullname3(sub_name, gv, Nullch);
2682 DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name);
2686 DIE(aTHX_ "Not a CODE reference");
2691 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
2692 cv = get_db_sub(&sv, cv);
2693 if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
2694 DIE(aTHX_ "No DB::sub routine defined");
2697 #ifdef USE_5005THREADS
2699 * First we need to check if the sub or method requires locking.
2700 * If so, we gain a lock on the CV, the first argument or the
2701 * stash (for static methods), as appropriate. This has to be
2702 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2703 * reschedule by returning a new op.
2705 MUTEX_LOCK(CvMUTEXP(cv));
2706 if (CvFLAGS(cv) & CVf_LOCKED) {
2708 if (CvFLAGS(cv) & CVf_METHOD) {
2709 if (SP > PL_stack_base + TOPMARK)
2710 sv = *(PL_stack_base + TOPMARK + 1);
2712 AV *av = (AV*)PAD_SVl(0);
2713 if (hasargs || !av || AvFILLp(av) < 0
2714 || !(sv = AvARRAY(av)[0]))
2716 MUTEX_UNLOCK(CvMUTEXP(cv));
2717 DIE(aTHX_ "no argument for locked method call");
2724 char *stashname = SvPV(sv, len);
2725 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2731 MUTEX_UNLOCK(CvMUTEXP(cv));
2732 mg = condpair_magic(sv);
2733 MUTEX_LOCK(MgMUTEXP(mg));
2734 if (MgOWNER(mg) == thr)
2735 MUTEX_UNLOCK(MgMUTEXP(mg));
2738 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2740 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: pp_entersub lock %p\n",
2742 MUTEX_UNLOCK(MgMUTEXP(mg));
2743 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
2745 MUTEX_LOCK(CvMUTEXP(cv));
2748 * Now we have permission to enter the sub, we must distinguish
2749 * four cases. (0) It's an XSUB (in which case we don't care
2750 * about ownership); (1) it's ours already (and we're recursing);
2751 * (2) it's free (but we may already be using a cached clone);
2752 * (3) another thread owns it. Case (1) is easy: we just use it.
2753 * Case (2) means we look for a clone--if we have one, use it
2754 * otherwise grab ownership of cv. Case (3) means we look for a
2755 * clone (for non-XSUBs) and have to create one if we don't
2757 * Why look for a clone in case (2) when we could just grab
2758 * ownership of cv straight away? Well, we could be recursing,
2759 * i.e. we originally tried to enter cv while another thread
2760 * owned it (hence we used a clone) but it has been freed up
2761 * and we're now recursing into it. It may or may not be "better"
2762 * to use the clone but at least CvDEPTH can be trusted.
2764 if (CvOWNER(cv) == thr || CvXSUB(cv))
2765 MUTEX_UNLOCK(CvMUTEXP(cv));
2767 /* Case (2) or (3) */
2771 * XXX Might it be better to release CvMUTEXP(cv) while we
2772 * do the hv_fetch? We might find someone has pinched it
2773 * when we look again, in which case we would be in case
2774 * (3) instead of (2) so we'd have to clone. Would the fact
2775 * that we released the mutex more quickly make up for this?
2777 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
2779 /* We already have a clone to use */
2780 MUTEX_UNLOCK(CvMUTEXP(cv));
2782 DEBUG_S(PerlIO_printf(Perl_debug_log,
2783 "entersub: %p already has clone %p:%s\n",
2784 thr, cv, SvPEEK((SV*)cv)));
2787 if (CvDEPTH(cv) == 0)
2788 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
2791 /* (2) => grab ownership of cv. (3) => make clone */
2795 MUTEX_UNLOCK(CvMUTEXP(cv));
2796 DEBUG_S(PerlIO_printf(Perl_debug_log,
2797 "entersub: %p grabbing %p:%s in stash %s\n",
2798 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
2799 HvNAME(CvSTASH(cv)) : "(none)"));
2802 /* Make a new clone. */
2804 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2805 MUTEX_UNLOCK(CvMUTEXP(cv));
2806 DEBUG_S((PerlIO_printf(Perl_debug_log,
2807 "entersub: %p cloning %p:%s\n",
2808 thr, cv, SvPEEK((SV*)cv))));
2810 * We're creating a new clone so there's no race
2811 * between the original MUTEX_UNLOCK and the
2812 * SvREFCNT_inc since no one will be trying to undef
2813 * it out from underneath us. At least, I don't think
2816 clonecv = cv_clone(cv);
2817 SvREFCNT_dec(cv); /* finished with this */
2818 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
2819 CvOWNER(clonecv) = thr;
2823 DEBUG_S(if (CvDEPTH(cv) != 0)
2824 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
2826 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
2829 #endif /* USE_5005THREADS */
2832 #ifdef PERL_XSUB_OLDSTYLE
2833 if (CvOLDSTYLE(cv)) {
2834 I32 (*fp3)(int,int,int);
2836 register I32 items = SP - MARK;
2837 /* We dont worry to copy from @_. */
2842 PL_stack_sp = mark + 1;
2843 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2844 items = (*fp3)(CvXSUBANY(cv).any_i32,
2845 MARK - PL_stack_base + 1,
2847 PL_stack_sp = PL_stack_base + items;
2850 #endif /* PERL_XSUB_OLDSTYLE */
2852 I32 markix = TOPMARK;
2857 /* Need to copy @_ to stack. Alternative may be to
2858 * switch stack to @_, and copy return values
2859 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2862 #ifdef USE_5005THREADS
2863 av = (AV*)PAD_SVl(0);
2865 av = GvAV(PL_defgv);
2866 #endif /* USE_5005THREADS */
2867 items = AvFILLp(av) + 1; /* @_ is not tieable */
2870 /* Mark is at the end of the stack. */
2872 Copy(AvARRAY(av), SP + 1, items, SV*);
2877 /* We assume first XSUB in &DB::sub is the called one. */
2879 SAVEVPTR(PL_curcop);
2880 PL_curcop = PL_curcopdb;
2883 /* Do we need to open block here? XXXX */
2884 (void)(*CvXSUB(cv))(aTHX_ cv);
2886 /* Enforce some sanity in scalar context. */
2887 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2888 if (markix > PL_stack_sp - PL_stack_base)
2889 *(PL_stack_base + markix) = &PL_sv_undef;
2891 *(PL_stack_base + markix) = *PL_stack_sp;
2892 PL_stack_sp = PL_stack_base + markix;
2900 register I32 items = SP - MARK;
2901 AV* padlist = CvPADLIST(cv);
2902 push_return(PL_op->op_next);
2903 PUSHBLOCK(cx, CXt_SUB, MARK);
2906 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2907 * that eval'' ops within this sub know the correct lexical space.
2908 * Owing the speed considerations, we choose instead to search for
2909 * the cv using find_runcv() when calling doeval().
2911 if (CvDEPTH(cv) >= 2) {
2912 PERL_STACK_OVERFLOW_CHECK();
2913 pad_push(padlist, CvDEPTH(cv), 1);
2915 #ifdef USE_5005THREADS
2917 AV* av = (AV*)PAD_SVl(0);
2919 items = AvFILLp(av) + 1;
2921 /* Mark is at the end of the stack. */
2923 Copy(AvARRAY(av), SP + 1, items, SV*);
2928 #endif /* USE_5005THREADS */
2929 PAD_SET_CUR(padlist, CvDEPTH(cv));
2930 #ifndef USE_5005THREADS
2932 #endif /* USE_5005THREADS */
2938 DEBUG_S(PerlIO_printf(Perl_debug_log,
2939 "%p entersub preparing @_\n", thr));
2941 av = (AV*)PAD_SVl(0);
2943 /* @_ is normally not REAL--this should only ever
2944 * happen when DB::sub() calls things that modify @_ */
2949 #ifndef USE_5005THREADS
2950 cx->blk_sub.savearray = GvAV(PL_defgv);
2951 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2952 #endif /* USE_5005THREADS */
2953 CX_CURPAD_SAVE(cx->blk_sub);
2954 cx->blk_sub.argarray = av;
2957 if (items > AvMAX(av) + 1) {
2959 if (AvARRAY(av) != ary) {
2960 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2961 SvPVX(av) = (char*)ary;
2963 if (items > AvMAX(av) + 1) {
2964 AvMAX(av) = items - 1;
2965 Renew(ary,items,SV*);
2967 SvPVX(av) = (char*)ary;
2970 Copy(MARK,AvARRAY(av),items,SV*);
2971 AvFILLp(av) = items - 1;
2979 /* warning must come *after* we fully set up the context
2980 * stuff so that __WARN__ handlers can safely dounwind()
2983 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2984 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2985 sub_crush_depth(cv);
2987 DEBUG_S(PerlIO_printf(Perl_debug_log,
2988 "%p entersub returning %p\n", thr, CvSTART(cv)));
2990 RETURNOP(CvSTART(cv));
2995 Perl_sub_crush_depth(pTHX_ CV *cv)
2998 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
3000 SV* tmpstr = sv_newmortal();
3001 gv_efullname3(tmpstr, CvGV(cv), Nullch);
3002 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
3012 IV elem = SvIV(elemsv);
3014 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
3015 const U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > av_len(av));
3018 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
3019 Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv);
3021 elem -= PL_curcop->cop_arybase;
3022 if (SvTYPE(av) != SVt_PVAV)
3024 svp = av_fetch(av, elem, lval && !defer);
3026 #ifdef PERL_MALLOC_WRAP
3027 static const char oom_array_extend[] =
3028 "Out of memory during array extend"; /* Duplicated in av.c */
3029 if (SvUOK(elemsv)) {
3030 const UV uv = SvUV(elemsv);
3031 elem = uv > IV_MAX ? IV_MAX : uv;
3033 else if (SvNOK(elemsv))
3034 elem = (IV)SvNV(elemsv);
3036 MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
3038 if (!svp || *svp == &PL_sv_undef) {
3041 DIE(aTHX_ PL_no_aelem, elem);
3042 lv = sv_newmortal();
3043 sv_upgrade(lv, SVt_PVLV);
3045 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
3046 LvTARG(lv) = SvREFCNT_inc(av);
3047 LvTARGOFF(lv) = elem;
3052 if (PL_op->op_private & OPpLVAL_INTRO)
3053 save_aelem(av, elem, svp);
3054 else if (PL_op->op_private & OPpDEREF)
3055 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
3057 sv = (svp ? *svp : &PL_sv_undef);
3058 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
3059 sv = sv_mortalcopy(sv);
3065 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
3071 Perl_croak(aTHX_ PL_no_modify);
3072 if (SvTYPE(sv) < SVt_RV)
3073 sv_upgrade(sv, SVt_RV);
3074 else if (SvTYPE(sv) >= SVt_PV) {
3081 SvRV_set(sv, NEWSV(355,0));
3084 SvRV_set(sv, (SV*)newAV());
3087 SvRV_set(sv, (SV*)newHV());
3102 if (SvTYPE(rsv) == SVt_PVCV) {
3108 SETs(method_common(sv, Null(U32*)));
3116 U32 hash = SvUVX(sv);
3118 XPUSHs(method_common(sv, &hash));
3123 S_method_common(pTHX_ SV* meth, U32* hashp)
3130 const char* packname = 0;
3131 SV *packsv = Nullsv;
3133 const char *name = SvPV(meth, namelen);
3135 sv = *(PL_stack_base + TOPMARK + 1);
3138 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
3147 /* this isn't a reference */
3150 if(SvOK(sv) && (packname = SvPV(sv, packlen))) {
3152 he = hv_fetch_ent(PL_stashcache, sv, 0, 0);
3154 stash = INT2PTR(HV*,SvIV(HeVAL(he)));
3161 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
3162 !(ob=(SV*)GvIO(iogv)))
3164 /* this isn't the name of a filehandle either */
3166 ((UTF8_IS_START(*packname) && DO_UTF8(sv))
3167 ? !isIDFIRST_utf8((U8*)packname)
3168 : !isIDFIRST(*packname)
3171 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
3172 SvOK(sv) ? "without a package or object reference"
3173 : "on an undefined value");
3175 /* assume it's a package name */
3176 stash = gv_stashpvn(packname, packlen, FALSE);
3180 SV* ref = newSViv(PTR2IV(stash));
3181 hv_store(PL_stashcache, packname, packlen, ref, 0);
3185 /* it _is_ a filehandle name -- replace with a reference */
3186 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
3189 /* if we got here, ob should be a reference or a glob */
3190 if (!ob || !(SvOBJECT(ob)
3191 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
3194 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
3198 stash = SvSTASH(ob);
3201 /* NOTE: stash may be null, hope hv_fetch_ent and
3202 gv_fetchmethod can cope (it seems they can) */
3204 /* shortcut for simple names */
3206 HE* he = hv_fetch_ent(stash, meth, 0, *hashp);
3208 gv = (GV*)HeVAL(he);
3209 if (isGV(gv) && GvCV(gv) &&
3210 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
3211 return (SV*)GvCV(gv);
3215 gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name);
3218 /* This code tries to figure out just what went wrong with
3219 gv_fetchmethod. It therefore needs to duplicate a lot of
3220 the internals of that function. We can't move it inside
3221 Perl_gv_fetchmethod_autoload(), however, since that would
3222 cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we
3225 const char* leaf = name;
3226 const char* sep = Nullch;
3229 for (p = name; *p; p++) {
3231 sep = p, leaf = p + 1;
3232 else if (*p == ':' && *(p + 1) == ':')
3233 sep = p, leaf = p + 2;
3235 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3236 /* the method name is unqualified or starts with SUPER:: */
3237 packname = sep ? CopSTASHPV(PL_curcop) :
3238 stash ? HvNAME_get(stash) : packname;
3241 "Can't use anonymous symbol table for method lookup");
3243 packlen = strlen(packname);
3246 /* the method name is qualified */
3248 packlen = sep - name;
3251 /* we're relying on gv_fetchmethod not autovivifying the stash */
3252 if (gv_stashpvn(packname, packlen, FALSE)) {
3254 "Can't locate object method \"%s\" via package \"%.*s\"",
3255 leaf, (int)packlen, packname);
3259 "Can't locate object method \"%s\" via package \"%.*s\""
3260 " (perhaps you forgot to load \"%.*s\"?)",
3261 leaf, (int)packlen, packname, (int)packlen, packname);
3264 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
3267 #ifdef USE_5005THREADS
3269 unset_cvowner(pTHX_ void *cvarg)
3271 register CV* cv = (CV *) cvarg;
3273 DEBUG_S((PerlIO_printf(Perl_debug_log, "%p unsetting CvOWNER of %p:%s\n",
3274 thr, cv, SvPEEK((SV*)cv))));
3275 MUTEX_LOCK(CvMUTEXP(cv));
3276 DEBUG_S(if (CvDEPTH(cv) != 0)
3277 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
3279 assert(thr == CvOWNER(cv));
3281 MUTEX_UNLOCK(CvMUTEXP(cv));
3284 #endif /* USE_5005THREADS */
3288 * c-indentation-style: bsd
3290 * indent-tabs-mode: t
3293 * ex: set ts=8 sts=4 sw=4 noet: