3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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(GvSVn(cGVOP_gv));
76 PL_curcop = (COP*)PL_op;
82 PUSHMARK(PL_stack_sp);
97 XPUSHs((SV*)cGVOP_gv);
107 if (PL_op->op_type == OP_AND)
109 RETURNOP(cLOGOP->op_other);
117 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
119 temp = left; left = right; right = temp;
121 if (PL_tainting && PL_tainted && !SvTAINTED(left))
123 SvSetMagicSV(right, left);
132 RETURNOP(cLOGOP->op_other);
134 RETURNOP(cLOGOP->op_next);
140 TAINT_NOT; /* Each statement is presumed innocent */
141 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
143 oldsave = PL_scopestack[PL_scopestack_ix - 1];
144 LEAVE_SCOPE(oldsave);
150 dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
155 const char *rpv = SvPV_const(right, rlen); /* mg_get(right) happens here */
156 const bool rbyte = !DO_UTF8(right);
157 bool rcopied = FALSE;
159 if (TARG == right && right != left) {
160 right = sv_2mortal(newSVpvn(rpv, rlen));
161 rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */
167 const char* const lpv = SvPV_const(left, llen); /* mg_get(left) may happen here */
168 lbyte = !DO_UTF8(left);
169 sv_setpvn(TARG, lpv, llen);
175 else { /* TARG == left */
177 if (SvGMAGICAL(left))
178 mg_get(left); /* or mg_get(left) may happen here */
180 sv_setpvn(left, "", 0);
181 (void)SvPV_nomg_const(left, llen); /* Needed to set UTF8 flag */
182 lbyte = !DO_UTF8(left);
187 #if defined(PERL_Y2KWARN)
188 if ((SvIOK(right) || SvNOK(right)) && ckWARN(WARN_Y2K) && SvOK(TARG)) {
189 if (llen >= 2 && lpv[llen - 2] == '1' && lpv[llen - 1] == '9'
190 && (llen == 2 || !isDIGIT(lpv[llen - 3])))
192 Perl_warner(aTHX_ packWARN(WARN_Y2K), "Possible Y2K bug: %s",
193 "about to append an integer to '19'");
198 if (lbyte != rbyte) {
200 sv_utf8_upgrade_nomg(TARG);
203 right = sv_2mortal(newSVpvn(rpv, rlen));
204 sv_utf8_upgrade_nomg(right);
205 rpv = SvPV_const(right, rlen);
208 sv_catpvn_nomg(TARG, rpv, rlen);
219 if (PL_op->op_flags & OPf_MOD) {
220 if (PL_op->op_private & OPpLVAL_INTRO)
221 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
222 else if (PL_op->op_private & OPpDEREF) {
224 vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
233 tryAMAGICunTARGET(iter, 0);
234 PL_last_in_gv = (GV*)(*PL_stack_sp--);
235 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
236 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
237 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
240 XPUSHs((SV*)PL_last_in_gv);
243 PL_last_in_gv = (GV*)(*PL_stack_sp--);
246 return do_readline();
251 dSP; tryAMAGICbinSET(eq,0);
252 #ifndef NV_PRESERVES_UV
253 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
255 SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s)));
259 #ifdef PERL_PRESERVE_IVUV
262 /* Unless the left argument is integer in range we are going
263 to have to use NV maths. Hence only attempt to coerce the
264 right argument if we know the left is integer. */
267 bool auvok = SvUOK(TOPm1s);
268 bool buvok = SvUOK(TOPs);
270 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
271 /* Casting IV to UV before comparison isn't going to matter
272 on 2s complement. On 1s complement or sign&magnitude
273 (if we have any of them) it could to make negative zero
274 differ from normal zero. As I understand it. (Need to
275 check - is negative zero implementation defined behaviour
277 UV buv = SvUVX(POPs);
278 UV auv = SvUVX(TOPs);
280 SETs(boolSV(auv == buv));
283 { /* ## Mixed IV,UV ## */
287 /* == is commutative so doesn't matter which is left or right */
289 /* top of stack (b) is the iv */
298 /* As uv is a UV, it's >0, so it cannot be == */
302 /* we know iv is >= 0 */
303 SETs(boolSV((UV)iv == SvUVX(uvp)));
311 SETs(boolSV(TOPn == value));
319 if (SvTYPE(TOPs) > SVt_PVLV)
320 DIE(aTHX_ PL_no_modify);
321 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
322 && SvIVX(TOPs) != IV_MAX)
324 SvIV_set(TOPs, SvIVX(TOPs) + 1);
325 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
327 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
339 if (PL_op->op_type == OP_OR)
341 RETURNOP(cLOGOP->op_other);
347 dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
348 useleft = USE_LEFT(TOPm1s);
349 #ifdef PERL_PRESERVE_IVUV
350 /* We must see if we can perform the addition with integers if possible,
351 as the integer code detects overflow while the NV code doesn't.
352 If either argument hasn't had a numeric conversion yet attempt to get
353 the IV. It's important to do this now, rather than just assuming that
354 it's not IOK as a PV of "9223372036854775806" may not take well to NV
355 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
356 integer in case the second argument is IV=9223372036854775806
357 We can (now) rely on sv_2iv to do the right thing, only setting the
358 public IOK flag if the value in the NV (or PV) slot is truly integer.
360 A side effect is that this also aggressively prefers integer maths over
361 fp maths for integer values.
363 How to detect overflow?
365 C 99 section 6.2.6.1 says
367 The range of nonnegative values of a signed integer type is a subrange
368 of the corresponding unsigned integer type, and the representation of
369 the same value in each type is the same. A computation involving
370 unsigned operands can never overflow, because a result that cannot be
371 represented by the resulting unsigned integer type is reduced modulo
372 the number that is one greater than the largest value that can be
373 represented by the resulting type.
377 which I read as "unsigned ints wrap."
379 signed integer overflow seems to be classed as "exception condition"
381 If an exceptional condition occurs during the evaluation of an
382 expression (that is, if the result is not mathematically defined or not
383 in the range of representable values for its type), the behavior is
386 (6.5, the 5th paragraph)
388 I had assumed that on 2s complement machines signed arithmetic would
389 wrap, hence coded pp_add and pp_subtract on the assumption that
390 everything perl builds on would be happy. After much wailing and
391 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
392 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
393 unsigned code below is actually shorter than the old code. :-)
398 /* Unless the left argument is integer in range we are going to have to
399 use NV maths. Hence only attempt to coerce the right argument if
400 we know the left is integer. */
408 /* left operand is undef, treat as zero. + 0 is identity,
409 Could SETi or SETu right now, but space optimise by not adding
410 lots of code to speed up what is probably a rarish case. */
412 /* Left operand is defined, so is it IV? */
415 if ((auvok = SvUOK(TOPm1s)))
418 register const IV aiv = SvIVX(TOPm1s);
421 auvok = 1; /* Now acting as a sign flag. */
422 } else { /* 2s complement assumption for IV_MIN */
430 bool result_good = 0;
433 bool buvok = SvUOK(TOPs);
438 register const IV biv = SvIVX(TOPs);
445 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
446 else "IV" now, independent of how it came in.
447 if a, b represents positive, A, B negative, a maps to -A etc
452 all UV maths. negate result if A negative.
453 add if signs same, subtract if signs differ. */
459 /* Must get smaller */
465 /* result really should be -(auv-buv). as its negation
466 of true value, need to swap our result flag */
483 if (result <= (UV)IV_MIN)
486 /* result valid, but out of range for IV. */
491 } /* Overflow, drop through to NVs. */
498 /* left operand is undef, treat as zero. + 0.0 is identity. */
502 SETn( value + TOPn );
510 AV *av = PL_op->op_flags & OPf_SPECIAL ?
511 (AV*)PAD_SV(PL_op->op_targ) : GvAV(cGVOP_gv);
512 const U32 lval = PL_op->op_flags & OPf_MOD;
513 SV** svp = av_fetch(av, PL_op->op_private, lval);
514 SV *sv = (svp ? *svp : &PL_sv_undef);
516 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
517 sv = sv_mortalcopy(sv);
526 do_join(TARG, *MARK, MARK, SP);
537 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
538 * will be enough to hold an OP*.
540 SV* sv = sv_newmortal();
541 sv_upgrade(sv, SVt_PVLV);
543 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
551 /* Oversized hot code. */
555 dSP; dMARK; dORIGMARK;
561 if (PL_op->op_flags & OPf_STACKED)
566 if (gv && (io = GvIO(gv))
567 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
570 if (MARK == ORIGMARK) {
571 /* If using default handle then we need to make space to
572 * pass object as 1st arg, so move other args up ...
576 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
580 *MARK = SvTIED_obj((SV*)io, mg);
583 call_method("PRINT", G_SCALAR);
591 if (!(io = GvIO(gv))) {
592 if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv)))
593 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
595 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
596 report_evil_fh(gv, io, PL_op->op_type);
597 SETERRNO(EBADF,RMS_IFI);
600 else if (!(fp = IoOFP(io))) {
601 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
603 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
604 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
605 report_evil_fh(gv, io, PL_op->op_type);
607 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
612 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
614 if (!do_print(*MARK, fp))
618 if (!do_print(PL_ofs_sv, fp)) { /* $, */
627 if (!do_print(*MARK, fp))
635 if (PL_ors_sv && SvOK(PL_ors_sv))
636 if (!do_print(PL_ors_sv, fp)) /* $\ */
639 if (IoFLAGS(io) & IOf_FLUSH)
640 if (PerlIO_flush(fp) == EOF)
650 XPUSHs(&PL_sv_undef);
661 tryAMAGICunDEREF(to_av);
664 if (SvTYPE(av) != SVt_PVAV)
665 DIE(aTHX_ "Not an ARRAY reference");
666 if (PL_op->op_flags & OPf_REF) {
671 if (GIMME == G_SCALAR)
672 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
676 else if (PL_op->op_flags & OPf_MOD
677 && PL_op->op_private & OPpLVAL_INTRO)
678 Perl_croak(aTHX_ PL_no_localize_ref);
681 if (SvTYPE(sv) == SVt_PVAV) {
683 if (PL_op->op_flags & OPf_REF) {
688 if (GIMME == G_SCALAR)
689 Perl_croak(aTHX_ "Can't return array to lvalue"
698 if (SvTYPE(sv) != SVt_PVGV) {
702 if (SvGMAGICAL(sv)) {
708 if (PL_op->op_flags & OPf_REF ||
709 PL_op->op_private & HINT_STRICT_REFS)
710 DIE(aTHX_ PL_no_usym, "an ARRAY");
711 if (ckWARN(WARN_UNINITIALIZED))
713 if (GIMME == G_ARRAY) {
720 if ((PL_op->op_flags & OPf_SPECIAL) &&
721 !(PL_op->op_flags & OPf_MOD))
723 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVAV);
725 && (!is_gv_magical(sym,len,0)
726 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV))))
732 if (PL_op->op_private & HINT_STRICT_REFS)
733 DIE(aTHX_ PL_no_symref, sym, "an ARRAY");
734 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
741 if (PL_op->op_private & OPpLVAL_INTRO)
743 if (PL_op->op_flags & OPf_REF) {
748 if (GIMME == G_SCALAR)
749 Perl_croak(aTHX_ "Can't return array to lvalue"
757 if (GIMME == G_ARRAY) {
758 const I32 maxarg = AvFILL(av) + 1;
759 (void)POPs; /* XXXX May be optimized away? */
761 if (SvRMAGICAL(av)) {
763 for (i=0; i < (U32)maxarg; i++) {
764 SV **svp = av_fetch(av, i, FALSE);
765 /* See note in pp_helem, and bug id #27839 */
767 ? SvGMAGICAL(*svp) ? sv_mortalcopy(*svp) : *svp
772 Copy(AvARRAY(av), SP+1, maxarg, SV*);
776 else if (GIMME_V == G_SCALAR) {
778 const I32 maxarg = AvFILL(av) + 1;
788 const I32 gimme = GIMME_V;
789 static const char return_hash_to_lvalue_scalar[] = "Can't return hash to lvalue scalar context";
793 tryAMAGICunDEREF(to_hv);
796 if (SvTYPE(hv) != SVt_PVHV && SvTYPE(hv) != SVt_PVAV)
797 DIE(aTHX_ "Not a HASH reference");
798 if (PL_op->op_flags & OPf_REF) {
803 if (gimme != G_ARRAY)
804 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
808 else if (PL_op->op_flags & OPf_MOD
809 && PL_op->op_private & OPpLVAL_INTRO)
810 Perl_croak(aTHX_ PL_no_localize_ref);
813 if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) {
815 if (PL_op->op_flags & OPf_REF) {
820 if (gimme != G_ARRAY)
821 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
829 if (SvTYPE(sv) != SVt_PVGV) {
833 if (SvGMAGICAL(sv)) {
839 if (PL_op->op_flags & OPf_REF ||
840 PL_op->op_private & HINT_STRICT_REFS)
841 DIE(aTHX_ PL_no_usym, "a HASH");
842 if (ckWARN(WARN_UNINITIALIZED))
844 if (gimme == G_ARRAY) {
851 if ((PL_op->op_flags & OPf_SPECIAL) &&
852 !(PL_op->op_flags & OPf_MOD))
854 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVHV);
856 && (!is_gv_magical(sym,len,0)
857 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV))))
863 if (PL_op->op_private & HINT_STRICT_REFS)
864 DIE(aTHX_ PL_no_symref, sym, "a HASH");
865 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
872 if (PL_op->op_private & OPpLVAL_INTRO)
874 if (PL_op->op_flags & OPf_REF) {
879 if (gimme != G_ARRAY)
880 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
887 if (gimme == G_ARRAY) { /* array wanted */
888 *PL_stack_sp = (SV*)hv;
891 else if (gimme == G_SCALAR) {
894 if (SvTYPE(hv) == SVt_PVAV)
895 hv = avhv_keys((AV*)hv);
897 TARG = Perl_hv_scalar(aTHX_ hv);
904 S_do_maybe_phash(pTHX_ AV *ary, SV **lelem, SV **firstlelem, SV **relem,
910 leftop = ((BINOP*)PL_op)->op_last;
912 assert(leftop->op_type == OP_NULL && leftop->op_targ == OP_LIST);
913 leftop = ((LISTOP*)leftop)->op_first;
915 /* Skip PUSHMARK and each element already assigned to. */
916 for (i = lelem - firstlelem; i > 0; i--) {
917 leftop = leftop->op_sibling;
920 if (leftop->op_type != OP_RV2HV)
925 av_fill(ary, 0); /* clear all but the fields hash */
926 if (lastrelem >= relem) {
927 while (relem < lastrelem) { /* gobble up all the rest */
931 /* Avoid a memory leak when avhv_store_ent dies. */
932 tmpstr = sv_newmortal();
933 sv_setsv(tmpstr,relem[1]); /* value */
935 if (avhv_store_ent(ary,relem[0],tmpstr,0))
936 (void)SvREFCNT_inc(tmpstr);
937 if (SvMAGICAL(ary) != 0 && SvSMAGICAL(tmpstr))
943 if (relem == lastrelem)
949 S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
953 if (ckWARN(WARN_MISC)) {
955 if (relem == firstrelem &&
957 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
958 SvTYPE(SvRV(*relem)) == SVt_PVHV))
960 err = "Reference found where even-sized list expected";
963 err = "Odd number of elements in hash assignment";
964 Perl_warner(aTHX_ packWARN(WARN_MISC), err);
966 if (SvTYPE(hash) == SVt_PVAV) {
968 tmpstr = sv_newmortal();
969 if (avhv_store_ent((AV*)hash,*relem,tmpstr,0))
970 (void)SvREFCNT_inc(tmpstr);
971 if (SvMAGICAL(hash) && SvSMAGICAL(tmpstr))
976 tmpstr = NEWSV(29,0);
977 didstore = hv_store_ent(hash,*relem,tmpstr,0);
978 if (SvMAGICAL(hash)) {
979 if (SvSMAGICAL(tmpstr))
992 SV **lastlelem = PL_stack_sp;
993 SV **lastrelem = PL_stack_base + POPMARK;
994 SV **firstrelem = PL_stack_base + POPMARK + 1;
995 SV **firstlelem = lastrelem + 1;
1008 SV **firsthashrelem = 0; /* "= 0" keeps gcc 2.95 quiet */
1011 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
1014 /* If there's a common identifier on both sides we have to take
1015 * special care that assigning the identifier on the left doesn't
1016 * clobber a value on the right that's used later in the list.
1018 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
1019 EXTEND_MORTAL(lastrelem - firstrelem + 1);
1020 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;
1238 register const char *t;
1239 register const char *s;
1242 I32 r_flags = REXEC_CHECKED;
1243 const char *truebase; /* Start of string */
1244 register REGEXP *rx = PM_GETRE(pm);
1246 const I32 gimme = GIMME;
1249 const 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_const(TARG, len);
1263 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 /* FIXME - can PL_bostr be made const char *? */
1329 PL_bostr = (char *)truebase;
1330 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, (char *)s, (char *)strend, r_flags, NULL);
1334 if ( (rx->reganch & ROPT_CHECK_ALL)
1336 && ((rx->reganch & ROPT_NOSCAN)
1337 || !((rx->reganch & RE_INTUIT_TAIL)
1338 && (r_flags & REXEC_SCREAM)))
1339 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
1342 if (CALLREGEXEC(aTHX_ rx, (char*)s, (char *)strend, (char*)truebase, minmatch, TARG, NULL, r_flags))
1345 if (dynpm->op_pmflags & PMf_ONCE)
1346 dynpm->op_pmdynflags |= PMdf_USED;
1355 RX_MATCH_TAINTED_on(rx);
1356 TAINT_IF(RX_MATCH_TAINTED(rx));
1357 if (gimme == G_ARRAY) {
1358 const I32 nparens = rx->nparens;
1359 I32 i = (global && !nparens) ? 1 : 0;
1361 SPAGAIN; /* EVAL blocks could move the stack. */
1362 EXTEND(SP, nparens + i);
1363 EXTEND_MORTAL(nparens + i);
1364 for (i = !i; i <= nparens; i++) {
1365 PUSHs(sv_newmortal());
1366 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1367 const I32 len = rx->endp[i] - rx->startp[i];
1368 s = rx->startp[i] + truebase;
1369 if (rx->endp[i] < 0 || rx->startp[i] < 0 ||
1370 len < 0 || len > strend - s)
1371 DIE(aTHX_ "panic: pp_match start/end pointers");
1372 sv_setpvn(*SP, s, len);
1373 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
1378 if (dynpm->op_pmflags & PMf_CONTINUE) {
1380 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1381 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1383 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1384 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1386 if (rx->startp[0] != -1) {
1387 mg->mg_len = rx->endp[0];
1388 if (rx->startp[0] == rx->endp[0])
1389 mg->mg_flags |= MGf_MINMATCH;
1391 mg->mg_flags &= ~MGf_MINMATCH;
1394 had_zerolen = (rx->startp[0] != -1
1395 && rx->startp[0] == rx->endp[0]);
1396 PUTBACK; /* EVAL blocks may use stack */
1397 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
1402 LEAVE_SCOPE(oldsave);
1408 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1409 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1411 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1412 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1414 if (rx->startp[0] != -1) {
1415 mg->mg_len = rx->endp[0];
1416 if (rx->startp[0] == rx->endp[0])
1417 mg->mg_flags |= MGf_MINMATCH;
1419 mg->mg_flags &= ~MGf_MINMATCH;
1422 LEAVE_SCOPE(oldsave);
1426 yup: /* Confirmed by INTUIT */
1428 RX_MATCH_TAINTED_on(rx);
1429 TAINT_IF(RX_MATCH_TAINTED(rx));
1431 if (dynpm->op_pmflags & PMf_ONCE)
1432 dynpm->op_pmdynflags |= PMdf_USED;
1433 if (RX_MATCH_COPIED(rx))
1434 Safefree(rx->subbeg);
1435 RX_MATCH_COPIED_off(rx);
1436 rx->subbeg = Nullch;
1438 /* FIXME - should rx->subbeg be const char *? */
1439 rx->subbeg = (char *) truebase;
1440 rx->startp[0] = s - truebase;
1441 if (RX_MATCH_UTF8(rx)) {
1442 char *t = (char*)utf8_hop((U8*)s, rx->minlen);
1443 rx->endp[0] = t - truebase;
1446 rx->endp[0] = s - truebase + rx->minlen;
1448 rx->sublen = strend - truebase;
1451 if (PL_sawampersand) {
1454 rx->subbeg = savepvn(t, strend - t);
1455 rx->sublen = strend - t;
1456 RX_MATCH_COPIED_on(rx);
1457 off = rx->startp[0] = s - t;
1458 rx->endp[0] = off + rx->minlen;
1460 else { /* startp/endp are used by @- @+. */
1461 rx->startp[0] = s - truebase;
1462 rx->endp[0] = s - truebase + rx->minlen;
1464 rx->nparens = rx->lastparen = rx->lastcloseparen = 0; /* used by @-, @+, and $^N */
1465 LEAVE_SCOPE(oldsave);
1470 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
1471 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1472 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1477 LEAVE_SCOPE(oldsave);
1478 if (gimme == G_ARRAY)
1484 Perl_do_readline(pTHX)
1486 dSP; dTARGETSTACKED;
1491 register IO * const io = GvIO(PL_last_in_gv);
1492 register const I32 type = PL_op->op_type;
1493 const I32 gimme = GIMME_V;
1496 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
1498 XPUSHs(SvTIED_obj((SV*)io, mg));
1501 call_method("READLINE", gimme);
1504 if (gimme == G_SCALAR) {
1506 SvSetSV_nosteal(TARG, result);
1515 if (IoFLAGS(io) & IOf_ARGV) {
1516 if (IoFLAGS(io) & IOf_START) {
1518 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1519 IoFLAGS(io) &= ~IOf_START;
1520 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
1521 sv_setpvn(GvSVn(PL_last_in_gv), "-", 1);
1522 SvSETMAGIC(GvSV(PL_last_in_gv));
1527 fp = nextargv(PL_last_in_gv);
1528 if (!fp) { /* Note: fp != IoIFP(io) */
1529 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
1532 else if (type == OP_GLOB)
1533 fp = Perl_start_glob(aTHX_ POPs, io);
1535 else if (type == OP_GLOB)
1537 else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) {
1538 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
1542 if ((!io || !(IoFLAGS(io) & IOf_START))
1543 && ckWARN2(WARN_GLOB, WARN_CLOSED))
1545 if (type == OP_GLOB)
1546 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1547 "glob failed (can't start child: %s)",
1550 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
1552 if (gimme == G_SCALAR) {
1553 /* undef TARG, and push that undefined value */
1554 if (type != OP_RCATLINE) {
1555 SV_CHECK_THINKFIRST(TARG);
1563 if (gimme == G_SCALAR) {
1567 (void)SvUPGRADE(sv, SVt_PV);
1568 tmplen = SvLEN(sv); /* remember if already alloced */
1569 if (!tmplen && !SvREADONLY(sv))
1570 Sv_Grow(sv, 80); /* try short-buffering it */
1572 if (type == OP_RCATLINE && SvOK(sv)) {
1574 SvPV_force_nolen(sv);
1580 sv = sv_2mortal(NEWSV(57, 80));
1584 /* This should not be marked tainted if the fp is marked clean */
1585 #define MAYBE_TAINT_LINE(io, sv) \
1586 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1591 /* delay EOF state for a snarfed empty file */
1592 #define SNARF_EOF(gimme,rs,io,sv) \
1593 (gimme != G_SCALAR || SvCUR(sv) \
1594 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
1598 if (!sv_gets(sv, fp, offset)
1600 || SNARF_EOF(gimme, PL_rs, io, sv)
1601 || PerlIO_error(fp)))
1603 PerlIO_clearerr(fp);
1604 if (IoFLAGS(io) & IOf_ARGV) {
1605 fp = nextargv(PL_last_in_gv);
1608 (void)do_close(PL_last_in_gv, FALSE);
1610 else if (type == OP_GLOB) {
1611 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1612 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1613 "glob failed (child exited with status %d%s)",
1614 (int)(STATUS_CURRENT >> 8),
1615 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
1618 if (gimme == G_SCALAR) {
1619 if (type != OP_RCATLINE) {
1620 SV_CHECK_THINKFIRST(TARG);
1626 MAYBE_TAINT_LINE(io, sv);
1629 MAYBE_TAINT_LINE(io, sv);
1631 IoFLAGS(io) |= IOf_NOLINE;
1635 if (type == OP_GLOB) {
1639 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
1640 tmps = SvEND(sv) - 1;
1641 if (*tmps == *SvPVX_const(PL_rs)) {
1643 SvCUR_set(sv, SvCUR(sv) - 1);
1646 for (t1 = SvPVX_const(sv); *t1; t1++)
1647 if (!isALPHA(*t1) && !isDIGIT(*t1) &&
1648 strchr("$&*(){}[]'\";\\|?<>~`", *t1))
1650 if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &PL_statbuf) < 0) {
1651 (void)POPs; /* Unmatched wildcard? Chuck it... */
1654 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
1655 const U8 *s = (const U8*)SvPVX_const(sv) + offset;
1656 const STRLEN len = SvCUR(sv) - offset;
1659 if (ckWARN(WARN_UTF8) &&
1660 !is_utf8_string_loc((U8 *) s, len, (U8 **) &f))
1661 /* Emulate :encoding(utf8) warning in the same case. */
1662 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1663 "utf8 \"\\x%02X\" does not map to Unicode",
1664 f < (U8*)SvEND(sv) ? *f : 0);
1666 if (gimme == G_ARRAY) {
1667 if (SvLEN(sv) - SvCUR(sv) > 20) {
1668 SvPV_shrink_to_cur(sv);
1670 sv = sv_2mortal(NEWSV(58, 80));
1673 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1674 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1675 const STRLEN new_len
1676 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
1677 SvPV_renew(sv, new_len);
1686 register PERL_CONTEXT *cx;
1687 I32 gimme = OP_GIMME(PL_op, -1);
1690 if (cxstack_ix >= 0)
1691 gimme = cxstack[cxstack_ix].blk_gimme;
1699 PUSHBLOCK(cx, CXt_BLOCK, SP);
1711 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
1712 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
1714 const U32 hash = (SvIsCOW_shared_hash(keysv)) ? SvSHARED_HASH(keysv) : 0;
1717 if (SvTYPE(hv) == SVt_PVHV) {
1718 if (PL_op->op_private & OPpLVAL_INTRO) {
1721 /* does the element we're localizing already exist? */
1723 /* can we determine whether it exists? */
1725 || mg_find((SV*)hv, PERL_MAGIC_env)
1726 || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied))
1727 /* Try to preserve the existenceness of a tied hash
1728 * element by using EXISTS and DELETE if possible.
1729 * Fallback to FETCH and STORE otherwise */
1730 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
1731 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
1732 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
1734 ) ? hv_exists_ent(hv, keysv, 0) : 1;
1737 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
1738 svp = he ? &HeVAL(he) : 0;
1740 else if (SvTYPE(hv) == SVt_PVAV) {
1741 if (PL_op->op_private & OPpLVAL_INTRO)
1742 DIE(aTHX_ "Can't localize pseudo-hash element");
1743 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, hash);
1749 if (!svp || *svp == &PL_sv_undef) {
1753 DIE(aTHX_ PL_no_helem_sv, keysv);
1755 lv = sv_newmortal();
1756 sv_upgrade(lv, SVt_PVLV);
1758 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, Nullch, 0);
1759 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1760 LvTARG(lv) = SvREFCNT_inc(hv);
1765 if (PL_op->op_private & OPpLVAL_INTRO) {
1766 if (HvNAME_get(hv) && isGV(*svp))
1767 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1771 const char * const key = SvPV_const(keysv, keylen);
1772 SAVEDELETE(hv, savepvn(key,keylen), keylen);
1774 save_helem(hv, keysv, svp);
1777 else if (PL_op->op_private & OPpDEREF)
1778 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
1780 sv = (svp ? *svp : &PL_sv_undef);
1781 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1782 * Pushing the magical RHS on to the stack is useless, since
1783 * that magic is soon destined to be misled by the local(),
1784 * and thus the later pp_sassign() will fail to mg_get() the
1785 * old value. This should also cure problems with delayed
1786 * mg_get()s. GSAR 98-07-03 */
1787 if (!lval && SvGMAGICAL(sv))
1788 sv = sv_mortalcopy(sv);
1796 register PERL_CONTEXT *cx;
1801 if (PL_op->op_flags & OPf_SPECIAL) {
1802 cx = &cxstack[cxstack_ix];
1803 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
1808 gimme = OP_GIMME(PL_op, -1);
1810 if (cxstack_ix >= 0)
1811 gimme = cxstack[cxstack_ix].blk_gimme;
1817 if (gimme == G_VOID)
1819 else if (gimme == G_SCALAR) {
1823 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1826 *MARK = sv_mortalcopy(TOPs);
1829 *MARK = &PL_sv_undef;
1833 else if (gimme == G_ARRAY) {
1834 /* in case LEAVE wipes old return values */
1836 for (mark = newsp + 1; mark <= SP; mark++) {
1837 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
1838 *mark = sv_mortalcopy(*mark);
1839 TAINT_NOT; /* Each item is independent */
1843 PL_curpm = newpm; /* Don't pop $1 et al till now */
1853 register PERL_CONTEXT *cx;
1859 cx = &cxstack[cxstack_ix];
1860 if (CxTYPE(cx) != CXt_LOOP)
1861 DIE(aTHX_ "panic: pp_iter");
1863 itersvp = CxITERVAR(cx);
1864 av = cx->blk_loop.iterary;
1865 if (SvTYPE(av) != SVt_PVAV) {
1866 /* iterate ($min .. $max) */
1867 if (cx->blk_loop.iterlval) {
1868 /* string increment */
1869 register SV* cur = cx->blk_loop.iterlval;
1871 const char *max = SvOK((SV*)av) ? SvPV_const((SV*)av, maxlen) : "";
1872 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1873 #ifndef USE_5005THREADS /* don't risk potential race */
1874 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1875 /* safe to reuse old SV */
1876 sv_setsv(*itersvp, cur);
1881 /* we need a fresh SV every time so that loop body sees a
1882 * completely new SV for closures/references to work as
1885 *itersvp = newSVsv(cur);
1886 SvREFCNT_dec(oldsv);
1888 if (strEQ(SvPVX_const(cur), max))
1889 sv_setiv(cur, 0); /* terminate next time */
1896 /* integer increment */
1897 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1900 #ifndef USE_5005THREADS /* don't risk potential race */
1901 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1902 /* safe to reuse old SV */
1903 sv_setiv(*itersvp, cx->blk_loop.iterix++);
1908 /* we need a fresh SV every time so that loop body sees a
1909 * completely new SV for closures/references to work as they
1912 *itersvp = newSViv(cx->blk_loop.iterix++);
1913 SvREFCNT_dec(oldsv);
1919 if (PL_op->op_private & OPpITER_REVERSED) {
1920 /* In reverse, use itermax as the min :-) */
1921 if (cx->blk_loop.iterix <= cx->blk_loop.itermax)
1924 if (SvMAGICAL(av) || AvREIFY(av)) {
1925 SV ** const svp = av_fetch(av, --cx->blk_loop.iterix, FALSE);
1926 sv = svp ? *svp : Nullsv;
1929 sv = AvARRAY(av)[--cx->blk_loop.iterix];
1933 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp :
1937 if (SvMAGICAL(av) || AvREIFY(av)) {
1938 SV **svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE);
1945 sv = AvARRAY(av)[++cx->blk_loop.iterix];
1949 if (sv && SvREFCNT(sv) == 0) {
1951 Perl_croak(aTHX_ "Use of freed value in iteration");
1958 if (av != PL_curstack && sv == &PL_sv_undef) {
1959 SV *lv = cx->blk_loop.iterlval;
1960 if (lv && SvREFCNT(lv) > 1) {
1965 SvREFCNT_dec(LvTARG(lv));
1967 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
1968 sv_upgrade(lv, SVt_PVLV);
1970 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
1972 LvTARG(lv) = SvREFCNT_inc(av);
1973 LvTARGOFF(lv) = cx->blk_loop.iterix;
1974 LvTARGLEN(lv) = (STRLEN)UV_MAX;
1979 *itersvp = SvREFCNT_inc(sv);
1980 SvREFCNT_dec(oldsv);
1988 register PMOP *pm = cPMOP;
2004 register REGEXP *rx = PM_GETRE(pm);
2006 int force_on_match = 0;
2007 I32 oldsave = PL_savestack_ix;
2009 bool doutf8 = FALSE;
2012 /* known replacement string? */
2013 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
2014 if (PL_op->op_flags & OPf_STACKED)
2021 if (SvFAKE(TARG) && SvREADONLY(TARG))
2022 sv_force_normal(TARG);
2023 if (SvREADONLY(TARG)
2024 || (SvTYPE(TARG) > SVt_PVLV
2025 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
2026 DIE(aTHX_ PL_no_modify);
2029 s = SvPV_mutable(TARG, len);
2030 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
2032 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
2033 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
2038 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
2042 DIE(aTHX_ "panic: pp_subst");
2045 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
2046 maxiters = 2 * slen + 10; /* We can match twice at each
2047 position, once with zero-length,
2048 second time with non-zero. */
2050 if (!rx->prelen && PL_curpm) {
2054 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
2055 ? REXEC_COPY_STR : 0;
2057 r_flags |= REXEC_SCREAM;
2058 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
2059 SAVEINT(PL_multiline);
2060 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
2063 if (rx->reganch & RE_USE_INTUIT) {
2065 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
2069 /* How to do it in subst? */
2070 /* if ( (rx->reganch & ROPT_CHECK_ALL)
2072 && ((rx->reganch & ROPT_NOSCAN)
2073 || !((rx->reganch & RE_INTUIT_TAIL)
2074 && (r_flags & REXEC_SCREAM))))
2079 /* only replace once? */
2080 once = !(rpm->op_pmflags & PMf_GLOBAL);
2082 /* known replacement string? */
2084 /* replacement needing upgrading? */
2085 if (DO_UTF8(TARG) && !doutf8) {
2086 nsv = sv_newmortal();
2089 sv_recode_to_utf8(nsv, PL_encoding);
2091 sv_utf8_upgrade(nsv);
2092 c = SvPV_const(nsv, clen);
2096 c = SvPV_const(dstr, clen);
2097 doutf8 = DO_UTF8(dstr);
2105 /* can do inplace substitution? */
2106 if (c && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
2107 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)
2108 && (!doutf8 || SvUTF8(TARG))) {
2109 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2110 r_flags | REXEC_CHECKED))
2114 LEAVE_SCOPE(oldsave);
2117 if (force_on_match) {
2119 s = SvPV_force(TARG, len);
2124 SvSCREAM_off(TARG); /* disable possible screamer */
2126 rxtainted |= RX_MATCH_TAINTED(rx);
2127 m = orig + rx->startp[0];
2128 d = orig + rx->endp[0];
2130 if (m - s > strend - d) { /* faster to shorten from end */
2132 Copy(c, m, clen, char);
2137 Move(d, m, i, char);
2141 SvCUR_set(TARG, m - s);
2143 else if ((i = m - s)) { /* faster from front */
2151 Copy(c, m, clen, char);
2156 Copy(c, d, clen, char);
2161 TAINT_IF(rxtainted & 1);
2167 if (iters++ > maxiters)
2168 DIE(aTHX_ "Substitution loop");
2169 rxtainted |= RX_MATCH_TAINTED(rx);
2170 m = rx->startp[0] + orig;
2173 Move(s, d, i, char);
2177 Copy(c, d, clen, char);
2180 s = rx->endp[0] + orig;
2181 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2183 /* don't match same null twice */
2184 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
2187 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
2188 Move(s, d, i+1, char); /* include the NUL */
2190 TAINT_IF(rxtainted & 1);
2192 PUSHs(sv_2mortal(newSViv((I32)iters)));
2194 (void)SvPOK_only_UTF8(TARG);
2195 TAINT_IF(rxtainted);
2196 if (SvSMAGICAL(TARG)) {
2204 LEAVE_SCOPE(oldsave);
2208 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2209 r_flags | REXEC_CHECKED))
2211 if (force_on_match) {
2213 s = SvPV_force(TARG, len);
2216 rxtainted |= RX_MATCH_TAINTED(rx);
2217 dstr = newSVpvn(m, s-m);
2222 register PERL_CONTEXT *cx;
2224 (void)ReREFCNT_inc(rx);
2226 RETURNOP(cPMOP->op_pmreplroot);
2228 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
2230 if (iters++ > maxiters)
2231 DIE(aTHX_ "Substitution loop");
2232 rxtainted |= RX_MATCH_TAINTED(rx);
2233 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
2238 strend = s + (strend - m);
2240 m = rx->startp[0] + orig;
2241 if (doutf8 && !SvUTF8(dstr))
2242 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
2244 sv_catpvn(dstr, s, m-s);
2245 s = rx->endp[0] + orig;
2247 sv_catpvn(dstr, c, clen);
2250 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2251 TARG, NULL, r_flags));
2252 if (doutf8 && !DO_UTF8(TARG))
2253 sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv);
2255 sv_catpvn(dstr, s, strend - s);
2258 SvPV_set(TARG, SvPVX(dstr));
2259 SvCUR_set(TARG, SvCUR(dstr));
2260 SvLEN_set(TARG, SvLEN(dstr));
2261 doutf8 |= DO_UTF8(dstr);
2262 SvPV_set(dstr, (char*)0);
2265 TAINT_IF(rxtainted & 1);
2267 PUSHs(sv_2mortal(newSViv((I32)iters)));
2269 (void)SvPOK_only(TARG);
2272 TAINT_IF(rxtainted);
2275 LEAVE_SCOPE(oldsave);
2284 LEAVE_SCOPE(oldsave);
2293 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2294 ++*PL_markstack_ptr;
2295 LEAVE; /* exit inner scope */
2298 if (PL_stack_base + *PL_markstack_ptr > SP) {
2300 I32 gimme = GIMME_V;
2302 LEAVE; /* exit outer scope */
2303 (void)POPMARK; /* pop src */
2304 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
2305 (void)POPMARK; /* pop dst */
2306 SP = PL_stack_base + POPMARK; /* pop original mark */
2307 if (gimme == G_SCALAR) {
2311 else if (gimme == G_ARRAY)
2318 ENTER; /* enter inner scope */
2321 src = PL_stack_base[*PL_markstack_ptr];
2325 RETURNOP(cLOGOP->op_other);
2336 register PERL_CONTEXT *cx;
2340 cxstack_ix++; /* temporarily protect top context */
2343 if (gimme == G_SCALAR) {
2346 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2348 *MARK = SvREFCNT_inc(TOPs);
2353 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2355 *MARK = sv_mortalcopy(sv);
2360 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2364 *MARK = &PL_sv_undef;
2368 else if (gimme == G_ARRAY) {
2369 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2370 if (!SvTEMP(*MARK)) {
2371 *MARK = sv_mortalcopy(*MARK);
2372 TAINT_NOT; /* Each item is independent */
2380 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2381 PL_curpm = newpm; /* ... and pop $1 et al */
2384 return pop_return();
2387 /* This duplicates the above code because the above code must not
2388 * get any slower by more conditions */
2396 register PERL_CONTEXT *cx;
2400 cxstack_ix++; /* temporarily protect top context */
2404 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2405 /* We are an argument to a function or grep().
2406 * This kind of lvalueness was legal before lvalue
2407 * subroutines too, so be backward compatible:
2408 * cannot report errors. */
2410 /* Scalar context *is* possible, on the LHS of -> only,
2411 * as in f()->meth(). But this is not an lvalue. */
2412 if (gimme == G_SCALAR)
2414 if (gimme == G_ARRAY) {
2415 if (!CvLVALUE(cx->blk_sub.cv))
2416 goto temporise_array;
2417 EXTEND_MORTAL(SP - newsp);
2418 for (mark = newsp + 1; mark <= SP; mark++) {
2421 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2422 *mark = sv_mortalcopy(*mark);
2424 /* Can be a localized value subject to deletion. */
2425 PL_tmps_stack[++PL_tmps_ix] = *mark;
2426 (void)SvREFCNT_inc(*mark);
2431 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2432 /* Here we go for robustness, not for speed, so we change all
2433 * the refcounts so the caller gets a live guy. Cannot set
2434 * TEMP, so sv_2mortal is out of question. */
2435 if (!CvLVALUE(cx->blk_sub.cv)) {
2441 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2443 if (gimme == G_SCALAR) {
2447 /* Temporaries are bad unless they happen to be elements
2448 * of a tied hash or array */
2449 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY) &&
2450 !(SvRMAGICAL(TOPs) && mg_find(TOPs, PERL_MAGIC_tiedelem))) {
2456 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2457 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2458 : "a readonly value" : "a temporary");
2460 else { /* Can be a localized value
2461 * subject to deletion. */
2462 PL_tmps_stack[++PL_tmps_ix] = *mark;
2463 (void)SvREFCNT_inc(*mark);
2466 else { /* Should not happen? */
2472 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
2473 (MARK > SP ? "Empty array" : "Array"));
2477 else if (gimme == G_ARRAY) {
2478 EXTEND_MORTAL(SP - newsp);
2479 for (mark = newsp + 1; mark <= SP; mark++) {
2480 if (*mark != &PL_sv_undef
2481 && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2482 /* Might be flattened array after $#array = */
2489 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2490 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2493 /* Can be a localized value subject to deletion. */
2494 PL_tmps_stack[++PL_tmps_ix] = *mark;
2495 (void)SvREFCNT_inc(*mark);
2501 if (gimme == G_SCALAR) {
2505 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2507 *MARK = SvREFCNT_inc(TOPs);
2512 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2514 *MARK = sv_mortalcopy(sv);
2519 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2523 *MARK = &PL_sv_undef;
2527 else if (gimme == G_ARRAY) {
2529 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2530 if (!SvTEMP(*MARK)) {
2531 *MARK = sv_mortalcopy(*MARK);
2532 TAINT_NOT; /* Each item is independent */
2541 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2542 PL_curpm = newpm; /* ... and pop $1 et al */
2545 return pop_return();
2550 S_get_db_sub(pTHX_ SV **svp, CV *cv)
2552 SV *dbsv = GvSVn(PL_DBsub);
2555 if (!PERLDB_SUB_NN) {
2558 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2559 || strEQ(GvNAME(gv), "END")
2560 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2561 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2562 && (gv = (GV*)*svp) ))) {
2563 /* Use GV from the stack as a fallback. */
2564 /* GV is potentially non-unique, or contain different CV. */
2565 SV * const tmp = newRV((SV*)cv);
2566 sv_setsv(dbsv, tmp);
2570 gv_efullname3(dbsv, gv, Nullch);
2574 const int type = SvTYPE(dbsv);
2575 if (type < SVt_PVIV && type != SVt_IV)
2576 sv_upgrade(dbsv, SVt_PVIV);
2577 (void)SvIOK_on(dbsv);
2578 SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */
2582 PL_curcopdb = PL_curcop;
2583 cv = GvCV(PL_DBsub);
2593 register PERL_CONTEXT *cx;
2595 const bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
2598 DIE(aTHX_ "Not a CODE reference");
2599 switch (SvTYPE(sv)) {
2603 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2605 SP = PL_stack_base + POPMARK;
2608 if (SvGMAGICAL(sv)) {
2612 sym = SvPOKp(sv) ? SvPVX_const(sv) : Nullch;
2615 sym = SvPV_nolen_const(sv);
2618 DIE(aTHX_ PL_no_usym, "a subroutine");
2619 if (PL_op->op_private & HINT_STRICT_REFS)
2620 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
2621 cv = get_cv(sym, TRUE);
2626 SV * const * sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2627 tryAMAGICunDEREF(to_cv);
2630 if (SvTYPE(cv) == SVt_PVCV)
2635 DIE(aTHX_ "Not a CODE reference");
2640 if (!(cv = GvCVu((GV*)sv)))
2641 cv = sv_2cv(sv, &stash, &gv, FALSE);
2654 if (!CvROOT(cv) && !CvXSUB(cv)) {
2658 /* anonymous or undef'd function leaves us no recourse */
2659 if (CvANON(cv) || !(gv = CvGV(cv)))
2660 DIE(aTHX_ "Undefined subroutine called");
2662 /* autoloaded stub? */
2663 if (cv != GvCV(gv)) {
2666 /* should call AUTOLOAD now? */
2669 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2676 sub_name = sv_newmortal();
2677 gv_efullname3(sub_name, gv, Nullch);
2678 DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name);
2682 DIE(aTHX_ "Not a CODE reference");
2687 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
2688 cv = get_db_sub(&sv, cv);
2689 if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
2690 DIE(aTHX_ "No DB::sub routine defined");
2693 #ifdef USE_5005THREADS
2695 * First we need to check if the sub or method requires locking.
2696 * If so, we gain a lock on the CV, the first argument or the
2697 * stash (for static methods), as appropriate. This has to be
2698 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2699 * reschedule by returning a new op.
2701 MUTEX_LOCK(CvMUTEXP(cv));
2702 if (CvFLAGS(cv) & CVf_LOCKED) {
2704 if (CvFLAGS(cv) & CVf_METHOD) {
2705 if (SP > PL_stack_base + TOPMARK)
2706 sv = *(PL_stack_base + TOPMARK + 1);
2708 AV *av = (AV*)PAD_SVl(0);
2709 if (hasargs || !av || AvFILLp(av) < 0
2710 || !(sv = AvARRAY(av)[0]))
2712 MUTEX_UNLOCK(CvMUTEXP(cv));
2713 DIE(aTHX_ "no argument for locked method call");
2720 char *stashname = SvPV(sv, len);
2721 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2727 MUTEX_UNLOCK(CvMUTEXP(cv));
2728 mg = condpair_magic(sv);
2729 MUTEX_LOCK(MgMUTEXP(mg));
2730 if (MgOWNER(mg) == thr)
2731 MUTEX_UNLOCK(MgMUTEXP(mg));
2734 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2736 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: pp_entersub lock %p\n",
2738 MUTEX_UNLOCK(MgMUTEXP(mg));
2739 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
2741 MUTEX_LOCK(CvMUTEXP(cv));
2744 * Now we have permission to enter the sub, we must distinguish
2745 * four cases. (0) It's an XSUB (in which case we don't care
2746 * about ownership); (1) it's ours already (and we're recursing);
2747 * (2) it's free (but we may already be using a cached clone);
2748 * (3) another thread owns it. Case (1) is easy: we just use it.
2749 * Case (2) means we look for a clone--if we have one, use it
2750 * otherwise grab ownership of cv. Case (3) means we look for a
2751 * clone (for non-XSUBs) and have to create one if we don't
2753 * Why look for a clone in case (2) when we could just grab
2754 * ownership of cv straight away? Well, we could be recursing,
2755 * i.e. we originally tried to enter cv while another thread
2756 * owned it (hence we used a clone) but it has been freed up
2757 * and we're now recursing into it. It may or may not be "better"
2758 * to use the clone but at least CvDEPTH can be trusted.
2760 if (CvOWNER(cv) == thr || CvXSUB(cv))
2761 MUTEX_UNLOCK(CvMUTEXP(cv));
2763 /* Case (2) or (3) */
2767 * XXX Might it be better to release CvMUTEXP(cv) while we
2768 * do the hv_fetch? We might find someone has pinched it
2769 * when we look again, in which case we would be in case
2770 * (3) instead of (2) so we'd have to clone. Would the fact
2771 * that we released the mutex more quickly make up for this?
2773 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
2775 /* We already have a clone to use */
2776 MUTEX_UNLOCK(CvMUTEXP(cv));
2778 DEBUG_S(PerlIO_printf(Perl_debug_log,
2779 "entersub: %p already has clone %p:%s\n",
2780 thr, cv, SvPEEK((SV*)cv)));
2783 if (CvDEPTH(cv) == 0)
2784 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
2787 /* (2) => grab ownership of cv. (3) => make clone */
2791 MUTEX_UNLOCK(CvMUTEXP(cv));
2792 DEBUG_S(PerlIO_printf(Perl_debug_log,
2793 "entersub: %p grabbing %p:%s in stash %s\n",
2794 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
2795 HvNAME(CvSTASH(cv)) : "(none)"));
2798 /* Make a new clone. */
2800 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2801 MUTEX_UNLOCK(CvMUTEXP(cv));
2802 DEBUG_S((PerlIO_printf(Perl_debug_log,
2803 "entersub: %p cloning %p:%s\n",
2804 thr, cv, SvPEEK((SV*)cv))));
2806 * We're creating a new clone so there's no race
2807 * between the original MUTEX_UNLOCK and the
2808 * SvREFCNT_inc since no one will be trying to undef
2809 * it out from underneath us. At least, I don't think
2812 clonecv = cv_clone(cv);
2813 SvREFCNT_dec(cv); /* finished with this */
2814 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
2815 CvOWNER(clonecv) = thr;
2819 DEBUG_S(if (CvDEPTH(cv) != 0)
2820 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
2822 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
2825 #endif /* USE_5005THREADS */
2828 #ifdef PERL_XSUB_OLDSTYLE
2829 if (CvOLDSTYLE(cv)) {
2830 I32 (*fp3)(int,int,int);
2832 register I32 items = SP - MARK;
2833 /* We dont worry to copy from @_. */
2838 PL_stack_sp = mark + 1;
2839 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2840 items = (*fp3)(CvXSUBANY(cv).any_i32,
2841 MARK - PL_stack_base + 1,
2843 PL_stack_sp = PL_stack_base + items;
2846 #endif /* PERL_XSUB_OLDSTYLE */
2848 I32 markix = TOPMARK;
2853 /* Need to copy @_ to stack. Alternative may be to
2854 * switch stack to @_, and copy return values
2855 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2856 #ifdef USE_5005THREADS
2857 AV * const av = (AV*)PAD_SVl(0);
2859 AV * const av = GvAV(PL_defgv);
2860 #endif /* USE_5005THREADS */
2861 const I32 items = AvFILLp(av) + 1; /* @_ is not tieable */
2865 /* Mark is at the end of the stack. */
2867 Copy(AvARRAY(av), SP + 1, items, SV*);
2872 /* We assume first XSUB in &DB::sub is the called one. */
2874 SAVEVPTR(PL_curcop);
2875 PL_curcop = PL_curcopdb;
2878 /* Do we need to open block here? XXXX */
2879 (void)(*CvXSUB(cv))(aTHX_ cv);
2881 /* Enforce some sanity in scalar context. */
2882 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2883 if (markix > PL_stack_sp - PL_stack_base)
2884 *(PL_stack_base + markix) = &PL_sv_undef;
2886 *(PL_stack_base + markix) = *PL_stack_sp;
2887 PL_stack_sp = PL_stack_base + markix;
2895 register I32 items = SP - MARK;
2896 AV* padlist = CvPADLIST(cv);
2897 push_return(PL_op->op_next);
2898 PUSHBLOCK(cx, CXt_SUB, MARK);
2901 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2902 * that eval'' ops within this sub know the correct lexical space.
2903 * Owing the speed considerations, we choose instead to search for
2904 * the cv using find_runcv() when calling doeval().
2906 if (CvDEPTH(cv) >= 2) {
2907 PERL_STACK_OVERFLOW_CHECK();
2908 pad_push(padlist, CvDEPTH(cv), 1);
2910 #ifdef USE_5005THREADS
2912 AV* av = (AV*)PAD_SVl(0);
2915 items = AvFILLp(av) + 1;
2917 /* Mark is at the end of the stack. */
2919 Copy(AvARRAY(av), SP + 1, items, SV*);
2924 #endif /* USE_5005THREADS */
2926 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2927 #ifndef USE_5005THREADS
2929 #endif /* USE_5005THREADS */
2935 DEBUG_S(PerlIO_printf(Perl_debug_log,
2936 "%p entersub preparing @_\n", thr));
2938 av = (AV*)PAD_SVl(0);
2940 /* @_ is normally not REAL--this should only ever
2941 * happen when DB::sub() calls things that modify @_ */
2946 #ifndef USE_5005THREADS
2947 cx->blk_sub.savearray = GvAV(PL_defgv);
2948 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2949 #endif /* USE_5005THREADS */
2950 CX_CURPAD_SAVE(cx->blk_sub);
2951 cx->blk_sub.argarray = av;
2954 if (items > AvMAX(av) + 1) {
2956 if (AvARRAY(av) != ary) {
2957 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2958 SvPVX(av) = (char*)ary;
2960 if (items > AvMAX(av) + 1) {
2961 AvMAX(av) = items - 1;
2962 Renew(ary,items,SV*);
2964 SvPVX(av) = (char*)ary;
2967 Copy(MARK,AvARRAY(av),items,SV*);
2968 AvFILLp(av) = items - 1;
2976 /* warning must come *after* we fully set up the context
2977 * stuff so that __WARN__ handlers can safely dounwind()
2980 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2981 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2982 sub_crush_depth(cv);
2984 DEBUG_S(PerlIO_printf(Perl_debug_log,
2985 "%p entersub returning %p\n", thr, CvSTART(cv)));
2987 RETURNOP(CvSTART(cv));
2992 Perl_sub_crush_depth(pTHX_ CV *cv)
2995 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
2997 SV* const tmpstr = sv_newmortal();
2998 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2999 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
3008 SV* const elemsv = POPs;
3009 IV elem = SvIV(elemsv);
3011 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
3012 const U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > av_len(av));
3015 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
3016 Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv);
3018 elem -= PL_curcop->cop_arybase;
3019 if (SvTYPE(av) != SVt_PVAV)
3021 svp = av_fetch(av, elem, lval && !defer);
3023 #ifdef PERL_MALLOC_WRAP
3024 if (SvUOK(elemsv)) {
3025 const UV uv = SvUV(elemsv);
3026 elem = uv > IV_MAX ? IV_MAX : uv;
3028 else if (SvNOK(elemsv))
3029 elem = (IV)SvNV(elemsv);
3031 static const char oom_array_extend[] =
3032 "Out of memory during array extend"; /* Duplicated in av.c */
3033 MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
3036 if (!svp || *svp == &PL_sv_undef) {
3039 DIE(aTHX_ PL_no_aelem, elem);
3040 lv = sv_newmortal();
3041 sv_upgrade(lv, SVt_PVLV);
3043 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
3044 LvTARG(lv) = SvREFCNT_inc(av);
3045 LvTARGOFF(lv) = elem;
3050 if (PL_op->op_private & OPpLVAL_INTRO)
3051 save_aelem(av, elem, svp);
3052 else if (PL_op->op_private & OPpDEREF)
3053 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
3055 sv = (svp ? *svp : &PL_sv_undef);
3056 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
3057 sv = sv_mortalcopy(sv);
3063 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
3069 Perl_croak(aTHX_ PL_no_modify);
3070 if (SvTYPE(sv) < SVt_RV)
3071 sv_upgrade(sv, SVt_RV);
3072 else if (SvTYPE(sv) >= SVt_PV) {
3079 SvRV_set(sv, NEWSV(355,0));
3082 SvRV_set(sv, (SV*)newAV());
3085 SvRV_set(sv, (SV*)newHV());
3096 SV* const sv = TOPs;
3099 SV* const rsv = SvRV(sv);
3100 if (SvTYPE(rsv) == SVt_PVCV) {
3106 SETs(method_common(sv, Null(U32*)));
3113 SV* const sv = cSVOP_sv;
3114 U32 hash = SvSHARED_HASH(sv);
3116 XPUSHs(method_common(sv, &hash));
3121 S_method_common(pTHX_ SV* meth, U32* hashp)
3127 const char* packname = Nullch;
3128 SV *packsv = Nullsv;
3130 const char * const name = SvPV_const(meth, namelen);
3131 SV * const sv = *(PL_stack_base + TOPMARK + 1);
3134 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
3143 /* this isn't a reference */
3144 if(SvOK(sv) && (packname = SvPV_const(sv, packlen))) {
3145 const HE* const he = hv_fetch_ent(PL_stashcache, sv, 0, 0);
3147 stash = INT2PTR(HV*,SvIV(HeVAL(he)));
3154 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
3155 !(ob=(SV*)GvIO(iogv)))
3157 /* this isn't the name of a filehandle either */
3159 ((UTF8_IS_START(*packname) && DO_UTF8(sv))
3160 ? !isIDFIRST_utf8((U8*)packname)
3161 : !isIDFIRST(*packname)
3164 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
3165 SvOK(sv) ? "without a package or object reference"
3166 : "on an undefined value");
3168 /* assume it's a package name */
3169 stash = gv_stashpvn(packname, packlen, FALSE);
3173 SV* ref = newSViv(PTR2IV(stash));
3174 hv_store(PL_stashcache, packname, packlen, ref, 0);
3178 /* it _is_ a filehandle name -- replace with a reference */
3179 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
3182 /* if we got here, ob should be a reference or a glob */
3183 if (!ob || !(SvOBJECT(ob)
3184 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
3187 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
3191 stash = SvSTASH(ob);
3194 /* NOTE: stash may be null, hope hv_fetch_ent and
3195 gv_fetchmethod can cope (it seems they can) */
3197 /* shortcut for simple names */
3199 const HE* const he = hv_fetch_ent(stash, meth, 0, *hashp);
3201 gv = (GV*)HeVAL(he);
3202 if (isGV(gv) && GvCV(gv) &&
3203 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
3204 return (SV*)GvCV(gv);
3208 gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name);
3211 /* This code tries to figure out just what went wrong with
3212 gv_fetchmethod. It therefore needs to duplicate a lot of
3213 the internals of that function. We can't move it inside
3214 Perl_gv_fetchmethod_autoload(), however, since that would
3215 cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we
3218 const char* leaf = name;
3219 const char* sep = Nullch;
3222 for (p = name; *p; p++) {
3224 sep = p, leaf = p + 1;
3225 else if (*p == ':' && *(p + 1) == ':')
3226 sep = p, leaf = p + 2;
3228 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3229 /* the method name is unqualified or starts with SUPER:: */
3230 packname = sep ? CopSTASHPV(PL_curcop) :
3231 stash ? HvNAME_get(stash) : packname;
3234 "Can't use anonymous symbol table for method lookup");
3236 packlen = strlen(packname);
3239 /* the method name is qualified */
3241 packlen = sep - name;
3244 /* we're relying on gv_fetchmethod not autovivifying the stash */
3245 if (gv_stashpvn(packname, packlen, FALSE)) {
3247 "Can't locate object method \"%s\" via package \"%.*s\"",
3248 leaf, (int)packlen, packname);
3252 "Can't locate object method \"%s\" via package \"%.*s\""
3253 " (perhaps you forgot to load \"%.*s\"?)",
3254 leaf, (int)packlen, packname, (int)packlen, packname);
3257 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
3260 #ifdef USE_5005THREADS
3262 unset_cvowner(pTHX_ void *cvarg)
3264 register CV* cv = (CV *) cvarg;
3266 DEBUG_S((PerlIO_printf(Perl_debug_log, "%p unsetting CvOWNER of %p:%s\n",
3267 thr, cv, SvPEEK((SV*)cv))));
3268 MUTEX_LOCK(CvMUTEXP(cv));
3269 DEBUG_S(if (CvDEPTH(cv) != 0)
3270 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
3272 assert(thr == CvOWNER(cv));
3274 MUTEX_UNLOCK(CvMUTEXP(cv));
3277 #endif /* USE_5005THREADS */
3281 * c-indentation-style: bsd
3283 * indent-tabs-mode: t
3286 * ex: set ts=8 sts=4 sw=4 noet: