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);
156 char* rpv = SvPV(right, rlen); /* mg_get(right) happens here */
157 bool rbyte = !DO_UTF8(right), rcopied = FALSE;
159 if (TARG == right && right != left) {
160 right = sv_2mortal(newSVpvn(rpv, rlen));
161 rpv = SvPV(right, rlen); /* no point setting UTF-8 here */
166 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 */
175 if (SvGMAGICAL(left))
176 mg_get(left); /* or mg_get(left) may happen here */
179 lpv = SvPV_nomg(left, llen);
180 lbyte = !DO_UTF8(left);
185 #if defined(PERL_Y2KWARN)
186 if ((SvIOK(right) || SvNOK(right)) && ckWARN(WARN_Y2K) && SvOK(TARG)) {
187 if (llen >= 2 && lpv[llen - 2] == '1' && lpv[llen - 1] == '9'
188 && (llen == 2 || !isDIGIT(lpv[llen - 3])))
190 Perl_warner(aTHX_ packWARN(WARN_Y2K), "Possible Y2K bug: %s",
191 "about to append an integer to '19'");
196 if (lbyte != rbyte) {
198 sv_utf8_upgrade_nomg(TARG);
201 right = sv_2mortal(newSVpvn(rpv, rlen));
202 sv_utf8_upgrade_nomg(right);
203 rpv = SvPV(right, rlen);
206 sv_catpvn_nomg(TARG, rpv, rlen);
217 if (PL_op->op_flags & OPf_MOD) {
218 if (PL_op->op_private & OPpLVAL_INTRO)
219 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
220 else if (PL_op->op_private & OPpDEREF) {
222 vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
231 tryAMAGICunTARGET(iter, 0);
232 PL_last_in_gv = (GV*)(*PL_stack_sp--);
233 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
234 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
235 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
238 XPUSHs((SV*)PL_last_in_gv);
241 PL_last_in_gv = (GV*)(*PL_stack_sp--);
244 return do_readline();
249 dSP; tryAMAGICbinSET(eq,0);
250 #ifndef NV_PRESERVES_UV
251 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
253 SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s)));
257 #ifdef PERL_PRESERVE_IVUV
260 /* Unless the left argument is integer in range we are going
261 to have to use NV maths. Hence only attempt to coerce the
262 right argument if we know the left is integer. */
265 bool auvok = SvUOK(TOPm1s);
266 bool buvok = SvUOK(TOPs);
268 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
269 /* Casting IV to UV before comparison isn't going to matter
270 on 2s complement. On 1s complement or sign&magnitude
271 (if we have any of them) it could to make negative zero
272 differ from normal zero. As I understand it. (Need to
273 check - is negative zero implementation defined behaviour
275 UV buv = SvUVX(POPs);
276 UV auv = SvUVX(TOPs);
278 SETs(boolSV(auv == buv));
281 { /* ## Mixed IV,UV ## */
285 /* == is commutative so doesn't matter which is left or right */
287 /* top of stack (b) is the iv */
296 /* As uv is a UV, it's >0, so it cannot be == */
300 /* we know iv is >= 0 */
301 SETs(boolSV((UV)iv == SvUVX(uvp)));
309 SETs(boolSV(TOPn == value));
317 if (SvTYPE(TOPs) > SVt_PVLV)
318 DIE(aTHX_ PL_no_modify);
319 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
320 && SvIVX(TOPs) != IV_MAX)
323 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
325 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
338 RETURNOP(cLOGOP->op_other);
344 dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
345 useleft = USE_LEFT(TOPm1s);
346 #ifdef PERL_PRESERVE_IVUV
347 /* We must see if we can perform the addition with integers if possible,
348 as the integer code detects overflow while the NV code doesn't.
349 If either argument hasn't had a numeric conversion yet attempt to get
350 the IV. It's important to do this now, rather than just assuming that
351 it's not IOK as a PV of "9223372036854775806" may not take well to NV
352 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
353 integer in case the second argument is IV=9223372036854775806
354 We can (now) rely on sv_2iv to do the right thing, only setting the
355 public IOK flag if the value in the NV (or PV) slot is truly integer.
357 A side effect is that this also aggressively prefers integer maths over
358 fp maths for integer values.
360 How to detect overflow?
362 C 99 section 6.2.6.1 says
364 The range of nonnegative values of a signed integer type is a subrange
365 of the corresponding unsigned integer type, and the representation of
366 the same value in each type is the same. A computation involving
367 unsigned operands can never overflow, because a result that cannot be
368 represented by the resulting unsigned integer type is reduced modulo
369 the number that is one greater than the largest value that can be
370 represented by the resulting type.
374 which I read as "unsigned ints wrap."
376 signed integer overflow seems to be classed as "exception condition"
378 If an exceptional condition occurs during the evaluation of an
379 expression (that is, if the result is not mathematically defined or not
380 in the range of representable values for its type), the behavior is
383 (6.5, the 5th paragraph)
385 I had assumed that on 2s complement machines signed arithmetic would
386 wrap, hence coded pp_add and pp_subtract on the assumption that
387 everything perl builds on would be happy. After much wailing and
388 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
389 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
390 unsigned code below is actually shorter than the old code. :-)
395 /* Unless the left argument is integer in range we are going to have to
396 use NV maths. Hence only attempt to coerce the right argument if
397 we know the left is integer. */
405 /* left operand is undef, treat as zero. + 0 is identity,
406 Could SETi or SETu right now, but space optimise by not adding
407 lots of code to speed up what is probably a rarish case. */
409 /* Left operand is defined, so is it IV? */
412 if ((auvok = SvUOK(TOPm1s)))
415 register IV aiv = SvIVX(TOPm1s);
418 auvok = 1; /* Now acting as a sign flag. */
419 } else { /* 2s complement assumption for IV_MIN */
427 bool result_good = 0;
430 bool buvok = SvUOK(TOPs);
435 register IV biv = SvIVX(TOPs);
442 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
443 else "IV" now, independent of how it came in.
444 if a, b represents positive, A, B negative, a maps to -A etc
449 all UV maths. negate result if A negative.
450 add if signs same, subtract if signs differ. */
456 /* Must get smaller */
462 /* result really should be -(auv-buv). as its negation
463 of true value, need to swap our result flag */
480 if (result <= (UV)IV_MIN)
483 /* result valid, but out of range for IV. */
488 } /* Overflow, drop through to NVs. */
495 /* left operand is undef, treat as zero. + 0.0 is identity. */
499 SETn( value + TOPn );
507 AV *av = PL_op->op_flags & OPf_SPECIAL ?
508 (AV*)PAD_SV(PL_op->op_targ) : GvAV(cGVOP_gv);
509 U32 lval = PL_op->op_flags & OPf_MOD;
510 SV** svp = av_fetch(av, PL_op->op_private, lval);
511 SV *sv = (svp ? *svp : &PL_sv_undef);
513 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
514 sv = sv_mortalcopy(sv);
523 do_join(TARG, *MARK, MARK, SP);
534 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
535 * will be enough to hold an OP*.
537 SV* sv = sv_newmortal();
538 sv_upgrade(sv, SVt_PVLV);
540 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
548 /* Oversized hot code. */
552 dSP; dMARK; dORIGMARK;
558 if (PL_op->op_flags & OPf_STACKED)
563 if (gv && (io = GvIO(gv))
564 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
567 if (MARK == ORIGMARK) {
568 /* If using default handle then we need to make space to
569 * pass object as 1st arg, so move other args up ...
573 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
577 *MARK = SvTIED_obj((SV*)io, mg);
580 call_method("PRINT", G_SCALAR);
588 if (!(io = GvIO(gv))) {
589 if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv)))
590 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
592 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
593 report_evil_fh(gv, io, PL_op->op_type);
594 SETERRNO(EBADF,RMS_IFI);
597 else if (!(fp = IoOFP(io))) {
598 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
600 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
601 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
602 report_evil_fh(gv, io, PL_op->op_type);
604 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
609 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
611 if (!do_print(*MARK, fp))
615 if (!do_print(PL_ofs_sv, fp)) { /* $, */
624 if (!do_print(*MARK, fp))
632 if (PL_ors_sv && SvOK(PL_ors_sv))
633 if (!do_print(PL_ors_sv, fp)) /* $\ */
636 if (IoFLAGS(io) & IOf_FLUSH)
637 if (PerlIO_flush(fp) == EOF)
658 tryAMAGICunDEREF(to_av);
661 if (SvTYPE(av) != SVt_PVAV)
662 DIE(aTHX_ "Not an ARRAY reference");
663 if (PL_op->op_flags & OPf_REF) {
668 if (GIMME == G_SCALAR)
669 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
673 else if (PL_op->op_flags & OPf_MOD
674 && PL_op->op_private & OPpLVAL_INTRO)
675 Perl_croak(aTHX_ PL_no_localize_ref);
678 if (SvTYPE(sv) == SVt_PVAV) {
680 if (PL_op->op_flags & OPf_REF) {
685 if (GIMME == G_SCALAR)
686 Perl_croak(aTHX_ "Can't return array to lvalue"
695 if (SvTYPE(sv) != SVt_PVGV) {
699 if (SvGMAGICAL(sv)) {
705 if (PL_op->op_flags & OPf_REF ||
706 PL_op->op_private & HINT_STRICT_REFS)
707 DIE(aTHX_ PL_no_usym, "an ARRAY");
708 if (ckWARN(WARN_UNINITIALIZED))
710 if (GIMME == G_ARRAY) {
717 if ((PL_op->op_flags & OPf_SPECIAL) &&
718 !(PL_op->op_flags & OPf_MOD))
720 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVAV);
722 && (!is_gv_magical(sym,len,0)
723 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV))))
729 if (PL_op->op_private & HINT_STRICT_REFS)
730 DIE(aTHX_ PL_no_symref, sym, "an ARRAY");
731 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVAV);
738 if (PL_op->op_private & OPpLVAL_INTRO)
740 if (PL_op->op_flags & OPf_REF) {
745 if (GIMME == G_SCALAR)
746 Perl_croak(aTHX_ "Can't return array to lvalue"
754 if (GIMME == G_ARRAY) {
755 I32 maxarg = AvFILL(av) + 1;
756 (void)POPs; /* XXXX May be optimized away? */
758 if (SvRMAGICAL(av)) {
760 for (i=0; i < (U32)maxarg; i++) {
761 SV **svp = av_fetch(av, i, FALSE);
762 /* See note in pp_helem, and bug id #27839 */
764 ? SvGMAGICAL(*svp) ? sv_mortalcopy(*svp) : *svp
769 Copy(AvARRAY(av), SP+1, maxarg, SV*);
773 else if (GIMME_V == G_SCALAR) {
775 I32 maxarg = AvFILL(av) + 1;
789 tryAMAGICunDEREF(to_hv);
792 if (SvTYPE(hv) != SVt_PVHV && SvTYPE(hv) != SVt_PVAV)
793 DIE(aTHX_ "Not a HASH reference");
794 if (PL_op->op_flags & OPf_REF) {
799 if (gimme != G_ARRAY)
800 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
804 else if (PL_op->op_flags & OPf_MOD
805 && PL_op->op_private & OPpLVAL_INTRO)
806 Perl_croak(aTHX_ PL_no_localize_ref);
809 if (SvTYPE(sv) == SVt_PVHV || SvTYPE(sv) == SVt_PVAV) {
811 if (PL_op->op_flags & OPf_REF) {
816 if (gimme != G_ARRAY)
817 Perl_croak(aTHX_ "Can't return hash to lvalue"
826 if (SvTYPE(sv) != SVt_PVGV) {
830 if (SvGMAGICAL(sv)) {
836 if (PL_op->op_flags & OPf_REF ||
837 PL_op->op_private & HINT_STRICT_REFS)
838 DIE(aTHX_ PL_no_usym, "a HASH");
839 if (ckWARN(WARN_UNINITIALIZED))
841 if (gimme == G_ARRAY) {
848 if ((PL_op->op_flags & OPf_SPECIAL) &&
849 !(PL_op->op_flags & OPf_MOD))
851 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PVHV);
853 && (!is_gv_magical(sym,len,0)
854 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV))))
860 if (PL_op->op_private & HINT_STRICT_REFS)
861 DIE(aTHX_ PL_no_symref, sym, "a HASH");
862 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PVHV);
869 if (PL_op->op_private & OPpLVAL_INTRO)
871 if (PL_op->op_flags & OPf_REF) {
876 if (gimme != G_ARRAY)
877 Perl_croak(aTHX_ "Can't return hash to lvalue"
885 if (gimme == G_ARRAY) { /* array wanted */
886 *PL_stack_sp = (SV*)hv;
889 else if (gimme == G_SCALAR) {
892 if (SvTYPE(hv) == SVt_PVAV)
893 hv = avhv_keys((AV*)hv);
895 TARG = Perl_hv_scalar(aTHX_ hv);
902 S_do_maybe_phash(pTHX_ AV *ary, SV **lelem, SV **firstlelem, SV **relem,
908 leftop = ((BINOP*)PL_op)->op_last;
910 assert(leftop->op_type == OP_NULL && leftop->op_targ == OP_LIST);
911 leftop = ((LISTOP*)leftop)->op_first;
913 /* Skip PUSHMARK and each element already assigned to. */
914 for (i = lelem - firstlelem; i > 0; i--) {
915 leftop = leftop->op_sibling;
918 if (leftop->op_type != OP_RV2HV)
923 av_fill(ary, 0); /* clear all but the fields hash */
924 if (lastrelem >= relem) {
925 while (relem < lastrelem) { /* gobble up all the rest */
929 /* Avoid a memory leak when avhv_store_ent dies. */
930 tmpstr = sv_newmortal();
931 sv_setsv(tmpstr,relem[1]); /* value */
933 if (avhv_store_ent(ary,relem[0],tmpstr,0))
934 (void)SvREFCNT_inc(tmpstr);
935 if (SvMAGICAL(ary) != 0 && SvSMAGICAL(tmpstr))
941 if (relem == lastrelem)
947 S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
951 if (ckWARN(WARN_MISC)) {
952 if (relem == firstrelem &&
954 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
955 SvTYPE(SvRV(*relem)) == SVt_PVHV))
957 Perl_warner(aTHX_ packWARN(WARN_MISC),
958 "Reference found where even-sized list expected");
961 Perl_warner(aTHX_ packWARN(WARN_MISC),
962 "Odd number of elements in hash assignment");
964 if (SvTYPE(hash) == SVt_PVAV) {
966 tmpstr = sv_newmortal();
967 if (avhv_store_ent((AV*)hash,*relem,tmpstr,0))
968 (void)SvREFCNT_inc(tmpstr);
969 if (SvMAGICAL(hash) && SvSMAGICAL(tmpstr))
974 tmpstr = NEWSV(29,0);
975 didstore = hv_store_ent(hash,*relem,tmpstr,0);
976 if (SvMAGICAL(hash)) {
977 if (SvSMAGICAL(tmpstr))
990 SV **lastlelem = PL_stack_sp;
991 SV **lastrelem = PL_stack_base + POPMARK;
992 SV **firstrelem = PL_stack_base + POPMARK + 1;
993 SV **firstlelem = lastrelem + 1;
1006 SV **firsthashrelem = 0; /* "= 0" keeps gcc 2.95 quiet */
1009 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
1012 /* If there's a common identifier on both sides we have to take
1013 * special care that assigning the identifier on the left doesn't
1014 * clobber a value on the right that's used later in the list.
1016 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
1017 EXTEND_MORTAL(lastrelem - firstrelem + 1);
1018 for (relem = firstrelem; relem <= lastrelem; relem++) {
1020 if ((sv = *relem)) {
1021 TAINT_NOT; /* Each item is independent */
1022 *relem = sv_mortalcopy(sv);
1032 while (lelem <= lastlelem) {
1033 TAINT_NOT; /* Each item stands on its own, taintwise. */
1035 switch (SvTYPE(sv)) {
1038 magic = SvMAGICAL(ary) != 0;
1039 if (PL_op->op_private & OPpASSIGN_HASH) {
1040 switch (do_maybe_phash(ary, lelem, firstlelem, relem,
1046 do_oddball((HV*)ary, relem, firstrelem);
1048 relem = lastrelem + 1;
1053 av_extend(ary, lastrelem - relem);
1055 while (relem <= lastrelem) { /* gobble up all the rest */
1058 sv = newSVsv(*relem);
1060 didstore = av_store(ary,i++,sv);
1070 case SVt_PVHV: { /* normal hash */
1074 magic = SvMAGICAL(hash) != 0;
1076 firsthashrelem = relem;
1078 while (relem < lastrelem) { /* gobble up all the rest */
1083 sv = &PL_sv_no, relem++;
1084 tmpstr = NEWSV(29,0);
1086 sv_setsv(tmpstr,*relem); /* value */
1087 *(relem++) = tmpstr;
1088 if (gimme != G_VOID && hv_exists_ent(hash, sv, 0))
1089 /* key overwrites an existing entry */
1091 didstore = hv_store_ent(hash,sv,tmpstr,0);
1093 if (SvSMAGICAL(tmpstr))
1100 if (relem == lastrelem) {
1101 do_oddball(hash, relem, firstrelem);
1107 if (SvIMMORTAL(sv)) {
1108 if (relem <= lastrelem)
1112 if (relem <= lastrelem) {
1113 sv_setsv(sv, *relem);
1117 sv_setsv(sv, &PL_sv_undef);
1122 if (PL_delaymagic & ~DM_DELAY) {
1123 if (PL_delaymagic & DM_UID) {
1124 #ifdef HAS_SETRESUID
1125 (void)setresuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1126 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1,
1129 # ifdef HAS_SETREUID
1130 (void)setreuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1131 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1);
1134 if ((PL_delaymagic & DM_UID) == DM_RUID) {
1135 (void)setruid(PL_uid);
1136 PL_delaymagic &= ~DM_RUID;
1138 # endif /* HAS_SETRUID */
1140 if ((PL_delaymagic & DM_UID) == DM_EUID) {
1141 (void)seteuid(PL_euid);
1142 PL_delaymagic &= ~DM_EUID;
1144 # endif /* HAS_SETEUID */
1145 if (PL_delaymagic & DM_UID) {
1146 if (PL_uid != PL_euid)
1147 DIE(aTHX_ "No setreuid available");
1148 (void)PerlProc_setuid(PL_uid);
1150 # endif /* HAS_SETREUID */
1151 #endif /* HAS_SETRESUID */
1152 PL_uid = PerlProc_getuid();
1153 PL_euid = PerlProc_geteuid();
1155 if (PL_delaymagic & DM_GID) {
1156 #ifdef HAS_SETRESGID
1157 (void)setresgid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1158 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1,
1161 # ifdef HAS_SETREGID
1162 (void)setregid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1163 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1);
1166 if ((PL_delaymagic & DM_GID) == DM_RGID) {
1167 (void)setrgid(PL_gid);
1168 PL_delaymagic &= ~DM_RGID;
1170 # endif /* HAS_SETRGID */
1172 if ((PL_delaymagic & DM_GID) == DM_EGID) {
1173 (void)setegid(PL_egid);
1174 PL_delaymagic &= ~DM_EGID;
1176 # endif /* HAS_SETEGID */
1177 if (PL_delaymagic & DM_GID) {
1178 if (PL_gid != PL_egid)
1179 DIE(aTHX_ "No setregid available");
1180 (void)PerlProc_setgid(PL_gid);
1182 # endif /* HAS_SETREGID */
1183 #endif /* HAS_SETRESGID */
1184 PL_gid = PerlProc_getgid();
1185 PL_egid = PerlProc_getegid();
1187 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1191 if (gimme == G_VOID)
1192 SP = firstrelem - 1;
1193 else if (gimme == G_SCALAR) {
1196 SETi(lastrelem - firstrelem + 1 - duplicates);
1203 /* Removes from the stack the entries which ended up as
1204 * duplicated keys in the hash (fix for [perl #24380]) */
1205 Move(firsthashrelem + duplicates,
1206 firsthashrelem, duplicates, SV**);
1207 lastrelem -= duplicates;
1212 SP = firstrelem + (lastlelem - firstlelem);
1213 lelem = firstlelem + (relem - firstrelem);
1215 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
1223 register PMOP *pm = cPMOP;
1224 SV *rv = sv_newmortal();
1225 SV *sv = newSVrv(rv, "Regexp");
1226 if (pm->op_pmdynflags & PMdf_TAINTED)
1228 sv_magic(sv,(SV*)ReREFCNT_inc(PM_GETRE(pm)), PERL_MAGIC_qr,0,0);
1235 register PMOP *pm = cPMOP;
1241 I32 r_flags = REXEC_CHECKED;
1242 char *truebase; /* Start of string */
1243 register REGEXP *rx = PM_GETRE(pm);
1248 I32 oldsave = PL_savestack_ix;
1249 I32 update_minmatch = 1;
1250 I32 had_zerolen = 0;
1252 if (PL_op->op_flags & OPf_STACKED)
1259 PUTBACK; /* EVAL blocks need stack_sp. */
1260 s = SvPV(TARG, len);
1263 DIE(aTHX_ "panic: pp_match");
1264 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1265 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1268 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
1270 /* PMdf_USED is set after a ?? matches once */
1271 if (pm->op_pmdynflags & PMdf_USED) {
1273 if (gimme == G_ARRAY)
1278 /* empty pattern special-cased to use last successful pattern if possible */
1279 if (!rx->prelen && PL_curpm) {
1284 if (rx->minlen > (I32)len)
1289 /* XXXX What part of this is needed with true \G-support? */
1290 if ((global = dynpm->op_pmflags & PMf_GLOBAL)) {
1292 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1293 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1294 if (mg && mg->mg_len >= 0) {
1295 if (!(rx->reganch & ROPT_GPOS_SEEN))
1296 rx->endp[0] = rx->startp[0] = mg->mg_len;
1297 else if (rx->reganch & ROPT_ANCH_GPOS) {
1298 r_flags |= REXEC_IGNOREPOS;
1299 rx->endp[0] = rx->startp[0] = mg->mg_len;
1301 minmatch = (mg->mg_flags & MGf_MINMATCH);
1302 update_minmatch = 0;
1306 if ((!global && rx->nparens)
1307 || SvTEMP(TARG) || PL_sawampersand)
1308 r_flags |= REXEC_COPY_STR;
1310 r_flags |= REXEC_SCREAM;
1312 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
1313 SAVEINT(PL_multiline);
1314 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
1318 if (global && rx->startp[0] != -1) {
1319 t = s = rx->endp[0] + truebase;
1320 if ((s + rx->minlen) > strend)
1322 if (update_minmatch++)
1323 minmatch = had_zerolen;
1325 if (rx->reganch & RE_USE_INTUIT &&
1326 DO_UTF8(TARG) == ((rx->reganch & ROPT_UTF8) != 0)) {
1327 PL_bostr = truebase;
1328 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
1332 if ( (rx->reganch & ROPT_CHECK_ALL)
1334 && ((rx->reganch & ROPT_NOSCAN)
1335 || !((rx->reganch & RE_INTUIT_TAIL)
1336 && (r_flags & REXEC_SCREAM)))
1337 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
1340 if (CALLREGEXEC(aTHX_ rx, s, strend, truebase, minmatch, TARG, NULL, r_flags))
1343 if (dynpm->op_pmflags & PMf_ONCE)
1344 dynpm->op_pmdynflags |= PMdf_USED;
1353 RX_MATCH_TAINTED_on(rx);
1354 TAINT_IF(RX_MATCH_TAINTED(rx));
1355 if (gimme == G_ARRAY) {
1356 I32 nparens, i, len;
1358 nparens = rx->nparens;
1359 if (global && !nparens)
1363 SPAGAIN; /* EVAL blocks could move the stack. */
1364 EXTEND(SP, nparens + i);
1365 EXTEND_MORTAL(nparens + i);
1366 for (i = !i; i <= nparens; i++) {
1367 PUSHs(sv_newmortal());
1369 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1370 len = rx->endp[i] - rx->startp[i];
1371 s = rx->startp[i] + truebase;
1372 if (rx->endp[i] < 0 || rx->startp[i] < 0 ||
1373 len < 0 || len > strend - s)
1374 DIE(aTHX_ "panic: pp_match start/end pointers");
1375 sv_setpvn(*SP, s, len);
1376 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
1381 if (dynpm->op_pmflags & PMf_CONTINUE) {
1383 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1384 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1386 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1387 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1389 if (rx->startp[0] != -1) {
1390 mg->mg_len = rx->endp[0];
1391 if (rx->startp[0] == rx->endp[0])
1392 mg->mg_flags |= MGf_MINMATCH;
1394 mg->mg_flags &= ~MGf_MINMATCH;
1397 had_zerolen = (rx->startp[0] != -1
1398 && rx->startp[0] == rx->endp[0]);
1399 PUTBACK; /* EVAL blocks may use stack */
1400 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
1405 LEAVE_SCOPE(oldsave);
1411 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1412 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1414 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1415 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1417 if (rx->startp[0] != -1) {
1418 mg->mg_len = rx->endp[0];
1419 if (rx->startp[0] == rx->endp[0])
1420 mg->mg_flags |= MGf_MINMATCH;
1422 mg->mg_flags &= ~MGf_MINMATCH;
1425 LEAVE_SCOPE(oldsave);
1429 yup: /* Confirmed by INTUIT */
1431 RX_MATCH_TAINTED_on(rx);
1432 TAINT_IF(RX_MATCH_TAINTED(rx));
1434 if (dynpm->op_pmflags & PMf_ONCE)
1435 dynpm->op_pmdynflags |= PMdf_USED;
1436 if (RX_MATCH_COPIED(rx))
1437 Safefree(rx->subbeg);
1438 RX_MATCH_COPIED_off(rx);
1439 rx->subbeg = Nullch;
1441 rx->subbeg = truebase;
1442 rx->startp[0] = s - truebase;
1443 if (RX_MATCH_UTF8(rx)) {
1444 char *t = (char*)utf8_hop((U8*)s, rx->minlen);
1445 rx->endp[0] = t - truebase;
1448 rx->endp[0] = s - truebase + rx->minlen;
1450 rx->sublen = strend - truebase;
1453 if (PL_sawampersand) {
1456 rx->subbeg = savepvn(t, strend - t);
1457 rx->sublen = strend - t;
1458 RX_MATCH_COPIED_on(rx);
1459 off = rx->startp[0] = s - t;
1460 rx->endp[0] = off + rx->minlen;
1462 else { /* startp/endp are used by @- @+. */
1463 rx->startp[0] = s - truebase;
1464 rx->endp[0] = s - truebase + rx->minlen;
1466 rx->nparens = rx->lastparen = rx->lastcloseparen = 0; /* used by @-, @+, and $^N */
1467 LEAVE_SCOPE(oldsave);
1472 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
1473 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1474 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1479 LEAVE_SCOPE(oldsave);
1480 if (gimme == G_ARRAY)
1486 Perl_do_readline(pTHX)
1488 dSP; dTARGETSTACKED;
1493 register IO *io = GvIO(PL_last_in_gv);
1494 register I32 type = PL_op->op_type;
1495 I32 gimme = GIMME_V;
1498 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
1500 XPUSHs(SvTIED_obj((SV*)io, mg));
1503 call_method("READLINE", gimme);
1506 if (gimme == G_SCALAR) {
1508 SvSetSV_nosteal(TARG, result);
1517 if (IoFLAGS(io) & IOf_ARGV) {
1518 if (IoFLAGS(io) & IOf_START) {
1520 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1521 IoFLAGS(io) &= ~IOf_START;
1522 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
1523 sv_setpvn(GvSV(PL_last_in_gv), "-", 1);
1524 SvSETMAGIC(GvSV(PL_last_in_gv));
1529 fp = nextargv(PL_last_in_gv);
1530 if (!fp) { /* Note: fp != IoIFP(io) */
1531 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
1534 else if (type == OP_GLOB)
1535 fp = Perl_start_glob(aTHX_ POPs, io);
1537 else if (type == OP_GLOB)
1539 else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) {
1540 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
1544 if (ckWARN2(WARN_GLOB, WARN_CLOSED)
1545 && (!io || !(IoFLAGS(io) & IOf_START))) {
1546 if (type == OP_GLOB)
1547 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1548 "glob failed (can't start child: %s)",
1551 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
1553 if (gimme == G_SCALAR) {
1554 /* undef TARG, and push that undefined value */
1555 if (type != OP_RCATLINE) {
1556 SV_CHECK_THINKFIRST(TARG);
1564 if (gimme == G_SCALAR) {
1568 (void)SvUPGRADE(sv, SVt_PV);
1569 tmplen = SvLEN(sv); /* remember if already alloced */
1570 if (!tmplen && !SvREADONLY(sv))
1571 Sv_Grow(sv, 80); /* try short-buffering it */
1573 if (type == OP_RCATLINE && SvOK(sv)) {
1576 (void)SvPV_force(sv, n_a);
1582 sv = sv_2mortal(NEWSV(57, 80));
1586 /* This should not be marked tainted if the fp is marked clean */
1587 #define MAYBE_TAINT_LINE(io, sv) \
1588 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1593 /* delay EOF state for a snarfed empty file */
1594 #define SNARF_EOF(gimme,rs,io,sv) \
1595 (gimme != G_SCALAR || SvCUR(sv) \
1596 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
1600 if (!sv_gets(sv, fp, offset)
1602 || SNARF_EOF(gimme, PL_rs, io, sv)
1603 || PerlIO_error(fp)))
1605 PerlIO_clearerr(fp);
1606 if (IoFLAGS(io) & IOf_ARGV) {
1607 fp = nextargv(PL_last_in_gv);
1610 (void)do_close(PL_last_in_gv, FALSE);
1612 else if (type == OP_GLOB) {
1613 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1614 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1615 "glob failed (child exited with status %d%s)",
1616 (int)(STATUS_CURRENT >> 8),
1617 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
1620 if (gimme == G_SCALAR) {
1621 if (type != OP_RCATLINE) {
1622 SV_CHECK_THINKFIRST(TARG);
1628 MAYBE_TAINT_LINE(io, sv);
1631 MAYBE_TAINT_LINE(io, sv);
1633 IoFLAGS(io) |= IOf_NOLINE;
1637 if (type == OP_GLOB) {
1640 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
1641 tmps = SvEND(sv) - 1;
1642 if (*tmps == *SvPVX(PL_rs)) {
1647 for (tmps = SvPVX(sv); *tmps; tmps++)
1648 if (!isALPHA(*tmps) && !isDIGIT(*tmps) &&
1649 strchr("$&*(){}[]'\";\\|?<>~`", *tmps))
1651 if (*tmps && PerlLIO_lstat(SvPVX(sv), &PL_statbuf) < 0) {
1652 (void)POPs; /* Unmatched wildcard? Chuck it... */
1655 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
1656 U8 *s = (U8*)SvPVX(sv) + offset;
1657 STRLEN len = SvCUR(sv) - offset;
1660 if (ckWARN(WARN_UTF8) &&
1661 !Perl_is_utf8_string_loc(aTHX_ s, len, &f))
1662 /* Emulate :encoding(utf8) warning in the same case. */
1663 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1664 "utf8 \"\\x%02X\" does not map to Unicode",
1665 f < (U8*)SvEND(sv) ? *f : 0);
1667 if (gimme == G_ARRAY) {
1668 if (SvLEN(sv) - SvCUR(sv) > 20) {
1669 SvLEN_set(sv, SvCUR(sv)+1);
1670 Renew(SvPVX(sv), SvLEN(sv), char);
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) */
1680 SvLEN_set(sv, SvCUR(sv)+40); /* allow some slop */
1681 Renew(SvPVX(sv), SvLEN(sv), char);
1690 register PERL_CONTEXT *cx;
1691 I32 gimme = OP_GIMME(PL_op, -1);
1694 if (cxstack_ix >= 0)
1695 gimme = cxstack[cxstack_ix].blk_gimme;
1703 PUSHBLOCK(cx, CXt_BLOCK, SP);
1715 U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
1716 U32 defer = PL_op->op_private & OPpLVAL_DEFER;
1718 U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
1721 if (SvTYPE(hv) == SVt_PVHV) {
1722 if (PL_op->op_private & OPpLVAL_INTRO) {
1725 /* does the element we're localizing already exist? */
1727 /* can we determine whether it exists? */
1729 || mg_find((SV*)hv, PERL_MAGIC_env)
1730 || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied))
1731 /* Try to preserve the existenceness of a tied hash
1732 * element by using EXISTS and DELETE if possible.
1733 * Fallback to FETCH and STORE otherwise */
1734 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
1735 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
1736 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
1738 ) ? hv_exists_ent(hv, keysv, 0) : 1;
1741 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
1742 svp = he ? &HeVAL(he) : 0;
1744 else if (SvTYPE(hv) == SVt_PVAV) {
1745 if (PL_op->op_private & OPpLVAL_INTRO)
1746 DIE(aTHX_ "Can't localize pseudo-hash element");
1747 svp = avhv_fetch_ent((AV*)hv, keysv, lval && !defer, hash);
1753 if (!svp || *svp == &PL_sv_undef) {
1758 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
1760 lv = sv_newmortal();
1761 sv_upgrade(lv, SVt_PVLV);
1763 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, Nullch, 0);
1764 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1765 LvTARG(lv) = SvREFCNT_inc(hv);
1770 if (PL_op->op_private & OPpLVAL_INTRO) {
1771 if (HvNAME(hv) && isGV(*svp))
1772 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1776 char *key = SvPV(keysv, keylen);
1777 SAVEDELETE(hv, savepvn(key,keylen), keylen);
1779 save_helem(hv, keysv, svp);
1782 else if (PL_op->op_private & OPpDEREF)
1783 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
1785 sv = (svp ? *svp : &PL_sv_undef);
1786 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1787 * Pushing the magical RHS on to the stack is useless, since
1788 * that magic is soon destined to be misled by the local(),
1789 * and thus the later pp_sassign() will fail to mg_get() the
1790 * old value. This should also cure problems with delayed
1791 * mg_get()s. GSAR 98-07-03 */
1792 if (!lval && SvGMAGICAL(sv))
1793 sv = sv_mortalcopy(sv);
1801 register PERL_CONTEXT *cx;
1807 if (PL_op->op_flags & OPf_SPECIAL) {
1808 cx = &cxstack[cxstack_ix];
1809 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
1814 gimme = OP_GIMME(PL_op, -1);
1816 if (cxstack_ix >= 0)
1817 gimme = cxstack[cxstack_ix].blk_gimme;
1823 if (gimme == G_VOID)
1825 else if (gimme == G_SCALAR) {
1828 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1831 *MARK = sv_mortalcopy(TOPs);
1834 *MARK = &PL_sv_undef;
1838 else if (gimme == G_ARRAY) {
1839 /* in case LEAVE wipes old return values */
1840 for (mark = newsp + 1; mark <= SP; mark++) {
1841 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
1842 *mark = sv_mortalcopy(*mark);
1843 TAINT_NOT; /* Each item is independent */
1847 PL_curpm = newpm; /* Don't pop $1 et al till now */
1857 register PERL_CONTEXT *cx;
1863 cx = &cxstack[cxstack_ix];
1864 if (CxTYPE(cx) != CXt_LOOP)
1865 DIE(aTHX_ "panic: pp_iter");
1867 itersvp = CxITERVAR(cx);
1868 av = cx->blk_loop.iterary;
1869 if (SvTYPE(av) != SVt_PVAV) {
1870 /* iterate ($min .. $max) */
1871 if (cx->blk_loop.iterlval) {
1872 /* string increment */
1873 register SV* cur = cx->blk_loop.iterlval;
1875 char *max = SvOK((SV*)av) ? SvPV((SV*)av, maxlen) : "";
1876 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1877 #ifndef USE_5005THREADS /* don't risk potential race */
1878 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1879 /* safe to reuse old SV */
1880 sv_setsv(*itersvp, cur);
1885 /* we need a fresh SV every time so that loop body sees a
1886 * completely new SV for closures/references to work as
1889 *itersvp = newSVsv(cur);
1890 SvREFCNT_dec(oldsv);
1892 if (strEQ(SvPVX(cur), max))
1893 sv_setiv(cur, 0); /* terminate next time */
1900 /* integer increment */
1901 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1904 #ifndef USE_5005THREADS /* don't risk potential race */
1905 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1906 /* safe to reuse old SV */
1907 sv_setiv(*itersvp, cx->blk_loop.iterix++);
1912 /* we need a fresh SV every time so that loop body sees a
1913 * completely new SV for closures/references to work as they
1916 *itersvp = newSViv(cx->blk_loop.iterix++);
1917 SvREFCNT_dec(oldsv);
1923 if (PL_op->op_private & OPpITER_REVERSED) {
1924 /* In reverse, use itermax as the min :-) */
1925 if (cx->blk_loop.iterix <= cx->blk_loop.itermax)
1928 if (SvMAGICAL(av) || AvREIFY(av)) {
1929 SV **svp = av_fetch(av, cx->blk_loop.iterix--, FALSE);
1936 sv = AvARRAY(av)[cx->blk_loop.iterix--];
1940 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp :
1944 if (SvMAGICAL(av) || AvREIFY(av)) {
1945 SV **svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE);
1952 sv = AvARRAY(av)[++cx->blk_loop.iterix];
1956 if (sv && SvREFCNT(sv) == 0) {
1958 Perl_croak(aTHX_ "Use of freed value in iteration");
1965 if (av != PL_curstack && sv == &PL_sv_undef) {
1966 SV *lv = cx->blk_loop.iterlval;
1967 if (lv && SvREFCNT(lv) > 1) {
1972 SvREFCNT_dec(LvTARG(lv));
1974 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
1975 sv_upgrade(lv, SVt_PVLV);
1977 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
1979 LvTARG(lv) = SvREFCNT_inc(av);
1980 LvTARGOFF(lv) = cx->blk_loop.iterix;
1981 LvTARGLEN(lv) = (STRLEN)UV_MAX;
1986 *itersvp = SvREFCNT_inc(sv);
1987 SvREFCNT_dec(oldsv);
1995 register PMOP *pm = cPMOP;
2011 register REGEXP *rx = PM_GETRE(pm);
2013 int force_on_match = 0;
2014 I32 oldsave = PL_savestack_ix;
2016 bool doutf8 = FALSE;
2019 /* known replacement string? */
2020 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
2021 if (PL_op->op_flags & OPf_STACKED)
2028 if (SvFAKE(TARG) && SvREADONLY(TARG))
2029 sv_force_normal(TARG);
2030 if (SvREADONLY(TARG)
2031 || (SvTYPE(TARG) > SVt_PVLV
2032 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG))))
2033 DIE(aTHX_ PL_no_modify);
2036 s = SvPV(TARG, len);
2037 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
2039 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
2040 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
2045 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
2049 DIE(aTHX_ "panic: pp_subst");
2052 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
2053 maxiters = 2 * slen + 10; /* We can match twice at each
2054 position, once with zero-length,
2055 second time with non-zero. */
2057 if (!rx->prelen && PL_curpm) {
2061 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
2062 ? REXEC_COPY_STR : 0;
2064 r_flags |= REXEC_SCREAM;
2065 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
2066 SAVEINT(PL_multiline);
2067 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
2070 if (rx->reganch & RE_USE_INTUIT) {
2072 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
2076 /* How to do it in subst? */
2077 /* if ( (rx->reganch & ROPT_CHECK_ALL)
2079 && ((rx->reganch & ROPT_NOSCAN)
2080 || !((rx->reganch & RE_INTUIT_TAIL)
2081 && (r_flags & REXEC_SCREAM))))
2086 /* only replace once? */
2087 once = !(rpm->op_pmflags & PMf_GLOBAL);
2089 /* known replacement string? */
2091 /* replacement needing upgrading? */
2092 if (DO_UTF8(TARG) && !doutf8) {
2093 nsv = sv_newmortal();
2096 sv_recode_to_utf8(nsv, PL_encoding);
2098 sv_utf8_upgrade(nsv);
2099 c = SvPV(nsv, clen);
2103 c = SvPV(dstr, clen);
2104 doutf8 = DO_UTF8(dstr);
2112 /* can do inplace substitution? */
2113 if (c && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
2114 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)
2115 && (!doutf8 || SvUTF8(TARG))) {
2116 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2117 r_flags | REXEC_CHECKED))
2121 LEAVE_SCOPE(oldsave);
2124 if (force_on_match) {
2126 s = SvPV_force(TARG, len);
2131 SvSCREAM_off(TARG); /* disable possible screamer */
2133 rxtainted |= RX_MATCH_TAINTED(rx);
2134 m = orig + rx->startp[0];
2135 d = orig + rx->endp[0];
2137 if (m - s > strend - d) { /* faster to shorten from end */
2139 Copy(c, m, clen, char);
2144 Move(d, m, i, char);
2148 SvCUR_set(TARG, m - s);
2151 else if ((i = m - s)) { /* faster from front */
2159 Copy(c, m, clen, char);
2164 Copy(c, d, clen, char);
2169 TAINT_IF(rxtainted & 1);
2175 if (iters++ > maxiters)
2176 DIE(aTHX_ "Substitution loop");
2177 rxtainted |= RX_MATCH_TAINTED(rx);
2178 m = rx->startp[0] + orig;
2182 Move(s, d, i, char);
2186 Copy(c, d, clen, char);
2189 s = rx->endp[0] + orig;
2190 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2192 /* don't match same null twice */
2193 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
2196 SvCUR_set(TARG, d - SvPVX(TARG) + i);
2197 Move(s, d, i+1, char); /* include the NUL */
2199 TAINT_IF(rxtainted & 1);
2201 PUSHs(sv_2mortal(newSViv((I32)iters)));
2203 (void)SvPOK_only_UTF8(TARG);
2204 TAINT_IF(rxtainted);
2205 if (SvSMAGICAL(TARG)) {
2213 LEAVE_SCOPE(oldsave);
2217 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2218 r_flags | REXEC_CHECKED))
2220 if (force_on_match) {
2222 s = SvPV_force(TARG, len);
2225 rxtainted |= RX_MATCH_TAINTED(rx);
2226 dstr = newSVpvn(m, s-m);
2231 register PERL_CONTEXT *cx;
2235 RETURNOP(cPMOP->op_pmreplroot);
2237 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
2239 if (iters++ > maxiters)
2240 DIE(aTHX_ "Substitution loop");
2241 rxtainted |= RX_MATCH_TAINTED(rx);
2242 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
2247 strend = s + (strend - m);
2249 m = rx->startp[0] + orig;
2250 if (doutf8 && !SvUTF8(dstr))
2251 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
2253 sv_catpvn(dstr, s, m-s);
2254 s = rx->endp[0] + orig;
2256 sv_catpvn(dstr, c, clen);
2259 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2260 TARG, NULL, r_flags));
2261 if (doutf8 && !DO_UTF8(TARG))
2262 sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv);
2264 sv_catpvn(dstr, s, strend - s);
2268 Safefree(SvPVX(TARG));
2269 SvPVX(TARG) = SvPVX(dstr);
2270 SvCUR_set(TARG, SvCUR(dstr));
2271 SvLEN_set(TARG, SvLEN(dstr));
2272 doutf8 |= DO_UTF8(dstr);
2276 TAINT_IF(rxtainted & 1);
2278 PUSHs(sv_2mortal(newSViv((I32)iters)));
2280 (void)SvPOK_only(TARG);
2283 TAINT_IF(rxtainted);
2286 LEAVE_SCOPE(oldsave);
2295 LEAVE_SCOPE(oldsave);
2304 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2305 ++*PL_markstack_ptr;
2306 LEAVE; /* exit inner scope */
2309 if (PL_stack_base + *PL_markstack_ptr > SP) {
2311 I32 gimme = GIMME_V;
2313 LEAVE; /* exit outer scope */
2314 (void)POPMARK; /* pop src */
2315 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
2316 (void)POPMARK; /* pop dst */
2317 SP = PL_stack_base + POPMARK; /* pop original mark */
2318 if (gimme == G_SCALAR) {
2322 else if (gimme == G_ARRAY)
2329 ENTER; /* enter inner scope */
2332 src = PL_stack_base[*PL_markstack_ptr];
2336 RETURNOP(cLOGOP->op_other);
2347 register PERL_CONTEXT *cx;
2351 cxstack_ix++; /* temporarily protect top context */
2354 if (gimme == G_SCALAR) {
2357 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2359 *MARK = SvREFCNT_inc(TOPs);
2364 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2366 *MARK = sv_mortalcopy(sv);
2371 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2375 *MARK = &PL_sv_undef;
2379 else if (gimme == G_ARRAY) {
2380 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2381 if (!SvTEMP(*MARK)) {
2382 *MARK = sv_mortalcopy(*MARK);
2383 TAINT_NOT; /* Each item is independent */
2391 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2392 PL_curpm = newpm; /* ... and pop $1 et al */
2395 return pop_return();
2398 /* This duplicates the above code because the above code must not
2399 * get any slower by more conditions */
2407 register PERL_CONTEXT *cx;
2411 cxstack_ix++; /* temporarily protect top context */
2415 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2416 /* We are an argument to a function or grep().
2417 * This kind of lvalueness was legal before lvalue
2418 * subroutines too, so be backward compatible:
2419 * cannot report errors. */
2421 /* Scalar context *is* possible, on the LHS of -> only,
2422 * as in f()->meth(). But this is not an lvalue. */
2423 if (gimme == G_SCALAR)
2425 if (gimme == G_ARRAY) {
2426 if (!CvLVALUE(cx->blk_sub.cv))
2427 goto temporise_array;
2428 EXTEND_MORTAL(SP - newsp);
2429 for (mark = newsp + 1; mark <= SP; mark++) {
2432 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2433 *mark = sv_mortalcopy(*mark);
2435 /* Can be a localized value subject to deletion. */
2436 PL_tmps_stack[++PL_tmps_ix] = *mark;
2437 (void)SvREFCNT_inc(*mark);
2442 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2443 /* Here we go for robustness, not for speed, so we change all
2444 * the refcounts so the caller gets a live guy. Cannot set
2445 * TEMP, so sv_2mortal is out of question. */
2446 if (!CvLVALUE(cx->blk_sub.cv)) {
2452 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2454 if (gimme == G_SCALAR) {
2458 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2464 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2465 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2466 : "a readonly value" : "a temporary");
2468 else { /* Can be a localized value
2469 * subject to deletion. */
2470 PL_tmps_stack[++PL_tmps_ix] = *mark;
2471 (void)SvREFCNT_inc(*mark);
2474 else { /* Should not happen? */
2480 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
2481 (MARK > SP ? "Empty array" : "Array"));
2485 else if (gimme == G_ARRAY) {
2486 EXTEND_MORTAL(SP - newsp);
2487 for (mark = newsp + 1; mark <= SP; mark++) {
2488 if (*mark != &PL_sv_undef
2489 && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2490 /* Might be flattened array after $#array = */
2497 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2498 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2501 /* Can be a localized value subject to deletion. */
2502 PL_tmps_stack[++PL_tmps_ix] = *mark;
2503 (void)SvREFCNT_inc(*mark);
2509 if (gimme == G_SCALAR) {
2513 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2515 *MARK = SvREFCNT_inc(TOPs);
2520 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2522 *MARK = sv_mortalcopy(sv);
2527 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2531 *MARK = &PL_sv_undef;
2535 else if (gimme == G_ARRAY) {
2537 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2538 if (!SvTEMP(*MARK)) {
2539 *MARK = sv_mortalcopy(*MARK);
2540 TAINT_NOT; /* Each item is independent */
2549 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2550 PL_curpm = newpm; /* ... and pop $1 et al */
2553 return pop_return();
2558 S_get_db_sub(pTHX_ SV **svp, CV *cv)
2560 SV *dbsv = GvSV(PL_DBsub);
2562 if (!PERLDB_SUB_NN) {
2566 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2567 || strEQ(GvNAME(gv), "END")
2568 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2569 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2570 && (gv = (GV*)*svp) ))) {
2571 /* Use GV from the stack as a fallback. */
2572 /* GV is potentially non-unique, or contain different CV. */
2573 SV *tmp = newRV((SV*)cv);
2574 sv_setsv(dbsv, tmp);
2578 gv_efullname3(dbsv, gv, Nullch);
2582 (void)SvUPGRADE(dbsv, SVt_PVIV);
2583 (void)SvIOK_on(dbsv);
2584 SAVEIV(SvIVX(dbsv));
2585 SvIVX(dbsv) = PTR2IV(cv); /* Do it the quickest way */
2589 PL_curcopdb = PL_curcop;
2590 cv = GvCV(PL_DBsub);
2600 register PERL_CONTEXT *cx;
2602 bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
2605 DIE(aTHX_ "Not a CODE reference");
2606 switch (SvTYPE(sv)) {
2612 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2614 SP = PL_stack_base + POPMARK;
2617 if (SvGMAGICAL(sv)) {
2621 sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
2624 sym = SvPV(sv, n_a);
2626 DIE(aTHX_ PL_no_usym, "a subroutine");
2627 if (PL_op->op_private & HINT_STRICT_REFS)
2628 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
2629 cv = get_cv(sym, TRUE);
2634 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2635 tryAMAGICunDEREF(to_cv);
2638 if (SvTYPE(cv) == SVt_PVCV)
2643 DIE(aTHX_ "Not a CODE reference");
2648 if (!(cv = GvCVu((GV*)sv)))
2649 cv = sv_2cv(sv, &stash, &gv, FALSE);
2662 if (!CvROOT(cv) && !CvXSUB(cv)) {
2666 /* anonymous or undef'd function leaves us no recourse */
2667 if (CvANON(cv) || !(gv = CvGV(cv)))
2668 DIE(aTHX_ "Undefined subroutine called");
2670 /* autoloaded stub? */
2671 if (cv != GvCV(gv)) {
2674 /* should call AUTOLOAD now? */
2677 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2684 sub_name = sv_newmortal();
2685 gv_efullname3(sub_name, gv, Nullch);
2686 DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name);
2690 DIE(aTHX_ "Not a CODE reference");
2695 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
2696 cv = get_db_sub(&sv, cv);
2697 if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
2698 DIE(aTHX_ "No DB::sub routine defined");
2701 #ifdef USE_5005THREADS
2703 * First we need to check if the sub or method requires locking.
2704 * If so, we gain a lock on the CV, the first argument or the
2705 * stash (for static methods), as appropriate. This has to be
2706 * inline because for FAKE_THREADS, COND_WAIT inlines code to
2707 * reschedule by returning a new op.
2709 MUTEX_LOCK(CvMUTEXP(cv));
2710 if (CvFLAGS(cv) & CVf_LOCKED) {
2712 if (CvFLAGS(cv) & CVf_METHOD) {
2713 if (SP > PL_stack_base + TOPMARK)
2714 sv = *(PL_stack_base + TOPMARK + 1);
2716 AV *av = (AV*)PAD_SVl(0);
2717 if (hasargs || !av || AvFILLp(av) < 0
2718 || !(sv = AvARRAY(av)[0]))
2720 MUTEX_UNLOCK(CvMUTEXP(cv));
2721 DIE(aTHX_ "no argument for locked method call");
2728 char *stashname = SvPV(sv, len);
2729 sv = (SV*)gv_stashpvn(stashname, len, TRUE);
2735 MUTEX_UNLOCK(CvMUTEXP(cv));
2736 mg = condpair_magic(sv);
2737 MUTEX_LOCK(MgMUTEXP(mg));
2738 if (MgOWNER(mg) == thr)
2739 MUTEX_UNLOCK(MgMUTEXP(mg));
2742 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
2744 DEBUG_S(PerlIO_printf(Perl_debug_log, "%p: pp_entersub lock %p\n",
2746 MUTEX_UNLOCK(MgMUTEXP(mg));
2747 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
2749 MUTEX_LOCK(CvMUTEXP(cv));
2752 * Now we have permission to enter the sub, we must distinguish
2753 * four cases. (0) It's an XSUB (in which case we don't care
2754 * about ownership); (1) it's ours already (and we're recursing);
2755 * (2) it's free (but we may already be using a cached clone);
2756 * (3) another thread owns it. Case (1) is easy: we just use it.
2757 * Case (2) means we look for a clone--if we have one, use it
2758 * otherwise grab ownership of cv. Case (3) means we look for a
2759 * clone (for non-XSUBs) and have to create one if we don't
2761 * Why look for a clone in case (2) when we could just grab
2762 * ownership of cv straight away? Well, we could be recursing,
2763 * i.e. we originally tried to enter cv while another thread
2764 * owned it (hence we used a clone) but it has been freed up
2765 * and we're now recursing into it. It may or may not be "better"
2766 * to use the clone but at least CvDEPTH can be trusted.
2768 if (CvOWNER(cv) == thr || CvXSUB(cv))
2769 MUTEX_UNLOCK(CvMUTEXP(cv));
2771 /* Case (2) or (3) */
2775 * XXX Might it be better to release CvMUTEXP(cv) while we
2776 * do the hv_fetch? We might find someone has pinched it
2777 * when we look again, in which case we would be in case
2778 * (3) instead of (2) so we'd have to clone. Would the fact
2779 * that we released the mutex more quickly make up for this?
2781 if ((svp = hv_fetch(thr->cvcache, (char *)cv, sizeof(cv), FALSE)))
2783 /* We already have a clone to use */
2784 MUTEX_UNLOCK(CvMUTEXP(cv));
2786 DEBUG_S(PerlIO_printf(Perl_debug_log,
2787 "entersub: %p already has clone %p:%s\n",
2788 thr, cv, SvPEEK((SV*)cv)));
2791 if (CvDEPTH(cv) == 0)
2792 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
2795 /* (2) => grab ownership of cv. (3) => make clone */
2799 MUTEX_UNLOCK(CvMUTEXP(cv));
2800 DEBUG_S(PerlIO_printf(Perl_debug_log,
2801 "entersub: %p grabbing %p:%s in stash %s\n",
2802 thr, cv, SvPEEK((SV*)cv), CvSTASH(cv) ?
2803 HvNAME(CvSTASH(cv)) : "(none)"));
2806 /* Make a new clone. */
2808 SvREFCNT_inc(cv); /* don't let it vanish from under us */
2809 MUTEX_UNLOCK(CvMUTEXP(cv));
2810 DEBUG_S((PerlIO_printf(Perl_debug_log,
2811 "entersub: %p cloning %p:%s\n",
2812 thr, cv, SvPEEK((SV*)cv))));
2814 * We're creating a new clone so there's no race
2815 * between the original MUTEX_UNLOCK and the
2816 * SvREFCNT_inc since no one will be trying to undef
2817 * it out from underneath us. At least, I don't think
2820 clonecv = cv_clone(cv);
2821 SvREFCNT_dec(cv); /* finished with this */
2822 hv_store(thr->cvcache, (char*)cv, sizeof(cv), (SV*)clonecv,0);
2823 CvOWNER(clonecv) = thr;
2827 DEBUG_S(if (CvDEPTH(cv) != 0)
2828 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
2830 SAVEDESTRUCTOR_X(unset_cvowner, (void*) cv);
2833 #endif /* USE_5005THREADS */
2836 #ifdef PERL_XSUB_OLDSTYLE
2837 if (CvOLDSTYLE(cv)) {
2838 I32 (*fp3)(int,int,int);
2840 register I32 items = SP - MARK;
2841 /* We dont worry to copy from @_. */
2846 PL_stack_sp = mark + 1;
2847 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2848 items = (*fp3)(CvXSUBANY(cv).any_i32,
2849 MARK - PL_stack_base + 1,
2851 PL_stack_sp = PL_stack_base + items;
2854 #endif /* PERL_XSUB_OLDSTYLE */
2856 I32 markix = TOPMARK;
2861 /* Need to copy @_ to stack. Alternative may be to
2862 * switch stack to @_, and copy return values
2863 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2866 #ifdef USE_5005THREADS
2867 av = (AV*)PAD_SVl(0);
2869 av = GvAV(PL_defgv);
2870 #endif /* USE_5005THREADS */
2871 items = AvFILLp(av) + 1; /* @_ is not tieable */
2874 /* Mark is at the end of the stack. */
2876 Copy(AvARRAY(av), SP + 1, items, SV*);
2881 /* We assume first XSUB in &DB::sub is the called one. */
2883 SAVEVPTR(PL_curcop);
2884 PL_curcop = PL_curcopdb;
2887 /* Do we need to open block here? XXXX */
2888 (void)(*CvXSUB(cv))(aTHX_ cv);
2890 /* Enforce some sanity in scalar context. */
2891 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2892 if (markix > PL_stack_sp - PL_stack_base)
2893 *(PL_stack_base + markix) = &PL_sv_undef;
2895 *(PL_stack_base + markix) = *PL_stack_sp;
2896 PL_stack_sp = PL_stack_base + markix;
2904 register I32 items = SP - MARK;
2905 AV* padlist = CvPADLIST(cv);
2906 push_return(PL_op->op_next);
2907 PUSHBLOCK(cx, CXt_SUB, MARK);
2910 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2911 * that eval'' ops within this sub know the correct lexical space.
2912 * Owing the speed considerations, we choose instead to search for
2913 * the cv using find_runcv() when calling doeval().
2915 if (CvDEPTH(cv) >= 2) {
2916 PERL_STACK_OVERFLOW_CHECK();
2917 pad_push(padlist, CvDEPTH(cv), 1);
2919 #ifdef USE_5005THREADS
2921 AV* av = (AV*)PAD_SVl(0);
2923 items = AvFILLp(av) + 1;
2925 /* Mark is at the end of the stack. */
2927 Copy(AvARRAY(av), SP + 1, items, SV*);
2932 #endif /* USE_5005THREADS */
2933 PAD_SET_CUR(padlist, CvDEPTH(cv));
2934 #ifndef USE_5005THREADS
2936 #endif /* USE_5005THREADS */
2942 DEBUG_S(PerlIO_printf(Perl_debug_log,
2943 "%p entersub preparing @_\n", thr));
2945 av = (AV*)PAD_SVl(0);
2947 /* @_ is normally not REAL--this should only ever
2948 * happen when DB::sub() calls things that modify @_ */
2953 #ifndef USE_5005THREADS
2954 cx->blk_sub.savearray = GvAV(PL_defgv);
2955 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2956 #endif /* USE_5005THREADS */
2957 CX_CURPAD_SAVE(cx->blk_sub);
2958 cx->blk_sub.argarray = av;
2961 if (items > AvMAX(av) + 1) {
2963 if (AvARRAY(av) != ary) {
2964 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2965 SvPVX(av) = (char*)ary;
2967 if (items > AvMAX(av) + 1) {
2968 AvMAX(av) = items - 1;
2969 Renew(ary,items,SV*);
2971 SvPVX(av) = (char*)ary;
2974 Copy(MARK,AvARRAY(av),items,SV*);
2975 AvFILLp(av) = items - 1;
2983 /* warning must come *after* we fully set up the context
2984 * stuff so that __WARN__ handlers can safely dounwind()
2987 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2988 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2989 sub_crush_depth(cv);
2991 DEBUG_S(PerlIO_printf(Perl_debug_log,
2992 "%p entersub returning %p\n", thr, CvSTART(cv)));
2994 RETURNOP(CvSTART(cv));
2999 Perl_sub_crush_depth(pTHX_ CV *cv)
3002 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
3004 SV* tmpstr = sv_newmortal();
3005 gv_efullname3(tmpstr, CvGV(cv), Nullch);
3006 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
3016 IV elem = SvIV(elemsv);
3018 U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
3019 U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av));
3022 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
3023 Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv);
3025 elem -= PL_curcop->cop_arybase;
3026 if (SvTYPE(av) != SVt_PVAV)
3028 svp = av_fetch(av, elem, lval && !defer);
3030 #ifdef PERL_MALLOC_WRAP
3031 static const char oom_array_extend[] =
3032 "Out of memory during array extend"; /* Duplicated in av.c */
3033 if (SvUOK(elemsv)) {
3034 UV uv = SvUV(elemsv);
3035 elem = uv > IV_MAX ? IV_MAX : uv;
3037 else if (SvNOK(elemsv))
3038 elem = (IV)SvNV(elemsv);
3040 MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
3042 if (!svp || *svp == &PL_sv_undef) {
3045 DIE(aTHX_ PL_no_aelem, elem);
3046 lv = sv_newmortal();
3047 sv_upgrade(lv, SVt_PVLV);
3049 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
3050 LvTARG(lv) = SvREFCNT_inc(av);
3051 LvTARGOFF(lv) = elem;
3056 if (PL_op->op_private & OPpLVAL_INTRO)
3057 save_aelem(av, elem, svp);
3058 else if (PL_op->op_private & OPpDEREF)
3059 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
3061 sv = (svp ? *svp : &PL_sv_undef);
3062 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
3063 sv = sv_mortalcopy(sv);
3069 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
3075 Perl_croak(aTHX_ PL_no_modify);
3076 if (SvTYPE(sv) < SVt_RV)
3077 sv_upgrade(sv, SVt_RV);
3078 else if (SvTYPE(sv) >= SVt_PV) {
3080 Safefree(SvPVX(sv));
3081 SvLEN(sv) = SvCUR(sv) = 0;
3085 SvRV(sv) = NEWSV(355,0);
3088 SvRV(sv) = (SV*)newAV();
3091 SvRV(sv) = (SV*)newHV();
3106 if (SvTYPE(rsv) == SVt_PVCV) {
3112 SETs(method_common(sv, Null(U32*)));
3120 U32 hash = SvUVX(sv);
3122 XPUSHs(method_common(sv, &hash));
3127 S_method_common(pTHX_ SV* meth, U32* hashp)
3136 SV *packsv = Nullsv;
3139 name = SvPV(meth, namelen);
3140 sv = *(PL_stack_base + TOPMARK + 1);
3143 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
3152 /* this isn't a reference */
3155 if(SvOK(sv) && (packname = SvPV(sv, packlen))) {
3157 he = hv_fetch_ent(PL_stashcache, sv, 0, 0);
3159 stash = INT2PTR(HV*,SvIV(HeVAL(he)));
3166 !(iogv = gv_fetchpv(packname, FALSE, SVt_PVIO)) ||
3167 !(ob=(SV*)GvIO(iogv)))
3169 /* this isn't the name of a filehandle either */
3171 ((UTF8_IS_START(*packname) && DO_UTF8(sv))
3172 ? !isIDFIRST_utf8((U8*)packname)
3173 : !isIDFIRST(*packname)
3176 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
3177 SvOK(sv) ? "without a package or object reference"
3178 : "on an undefined value");
3180 /* assume it's a package name */
3181 stash = gv_stashpvn(packname, packlen, FALSE);
3185 SV* ref = newSViv(PTR2IV(stash));
3186 hv_store(PL_stashcache, packname, packlen, ref, 0);
3190 /* it _is_ a filehandle name -- replace with a reference */
3191 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
3194 /* if we got here, ob should be a reference or a glob */
3195 if (!ob || !(SvOBJECT(ob)
3196 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
3199 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
3203 stash = SvSTASH(ob);
3206 /* NOTE: stash may be null, hope hv_fetch_ent and
3207 gv_fetchmethod can cope (it seems they can) */
3209 /* shortcut for simple names */
3211 HE* he = hv_fetch_ent(stash, meth, 0, *hashp);
3213 gv = (GV*)HeVAL(he);
3214 if (isGV(gv) && GvCV(gv) &&
3215 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
3216 return (SV*)GvCV(gv);
3220 gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name);
3223 /* This code tries to figure out just what went wrong with
3224 gv_fetchmethod. It therefore needs to duplicate a lot of
3225 the internals of that function. We can't move it inside
3226 Perl_gv_fetchmethod_autoload(), however, since that would
3227 cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we
3234 for (p = name; *p; p++) {
3236 sep = p, leaf = p + 1;
3237 else if (*p == ':' && *(p + 1) == ':')
3238 sep = p, leaf = p + 2;
3240 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3241 /* the method name is unqualified or starts with SUPER:: */
3242 packname = sep ? CopSTASHPV(PL_curcop) :
3243 stash ? HvNAME(stash) : packname;
3246 "Can't use anonymous symbol table for method lookup");
3248 packlen = strlen(packname);
3251 /* the method name is qualified */
3253 packlen = sep - name;
3256 /* we're relying on gv_fetchmethod not autovivifying the stash */
3257 if (gv_stashpvn(packname, packlen, FALSE)) {
3259 "Can't locate object method \"%s\" via package \"%.*s\"",
3260 leaf, (int)packlen, packname);
3264 "Can't locate object method \"%s\" via package \"%.*s\""
3265 " (perhaps you forgot to load \"%.*s\"?)",
3266 leaf, (int)packlen, packname, (int)packlen, packname);
3269 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
3272 #ifdef USE_5005THREADS
3274 unset_cvowner(pTHX_ void *cvarg)
3276 register CV* cv = (CV *) cvarg;
3278 DEBUG_S((PerlIO_printf(Perl_debug_log, "%p unsetting CvOWNER of %p:%s\n",
3279 thr, cv, SvPEEK((SV*)cv))));
3280 MUTEX_LOCK(CvMUTEXP(cv));
3281 DEBUG_S(if (CvDEPTH(cv) != 0)
3282 PerlIO_printf(Perl_debug_log, "depth %ld != 0\n",
3284 assert(thr == CvOWNER(cv));
3286 MUTEX_UNLOCK(CvMUTEXP(cv));
3289 #endif /* USE_5005THREADS */
3293 * c-indentation-style: bsd
3295 * indent-tabs-mode: t
3298 * vim: shiftwidth=4: