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
49 PL_curcop = (COP*)PL_op;
50 TAINT_NOT; /* Each statement is presumed innocent */
51 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
61 if (PL_op->op_private & OPpLVAL_INTRO)
62 PUSHs(save_scalar(cGVOP_gv));
64 PUSHs(GvSVn(cGVOP_gv));
77 PL_curcop = (COP*)PL_op;
84 PUSHMARK(PL_stack_sp);
99 XPUSHs((SV*)cGVOP_gv);
109 if (PL_op->op_type == OP_AND)
111 RETURNOP(cLOGOP->op_other);
117 dVAR; dSP; dPOPTOPssrl;
119 if (PL_op->op_private & OPpASSIGN_BACKWARDS) {
120 SV * const temp = left;
121 left = right; right = temp;
123 if (PL_tainting && PL_tainted && !SvTAINTED(left))
125 if (PL_op->op_private & OPpASSIGN_CV_TO_GV) {
127 const U32 cv_type = SvTYPE(cv);
128 const U32 gv_type = SvTYPE(right);
129 bool got_coderef = cv_type == SVt_PVCV || cv_type == SVt_PVFM;
135 /* Can do the optimisation if right (LVAUE) is not a typeglob,
136 left (RVALUE) is a reference to something, and we're in void
138 if (!got_coderef && gv_type != SVt_PVGV && GIMME_V == G_VOID) {
139 /* Is the target symbol table currently empty? */
140 GV *gv = gv_fetchsv(right, GV_NOINIT, SVt_PVGV);
141 if (SvTYPE(gv) != SVt_PVGV && !SvOK(gv)) {
142 /* Good. Create a new proxy constant subroutine in the target.
143 The gv becomes a(nother) reference to the constant. */
144 SV *const value = SvRV(cv);
146 SvUPGRADE((SV *)gv, SVt_RV);
155 /* Need to fix things up. */
156 if (gv_type != SVt_PVGV) {
157 /* Need to fix GV. */
158 right = (SV*)gv_fetchsv(right, GV_ADD, SVt_PVGV);
162 /* We've been returned a constant rather than a full subroutine,
163 but they expect a subroutine reference to apply. */
165 SvREFCNT_inc(SvRV(cv));
166 /* newCONSTSUB takes a reference count on the passed in SV
167 from us. We set the name to NULL, otherwise we get into
168 all sorts of fun as the reference to our new sub is
169 donated to the GV that we're about to assign to.
171 SvRV_set(left, (SV *)newCONSTSUB(GvSTASH(right), NULL,
178 SvSetMagicSV(right, left);
187 RETURNOP(cLOGOP->op_other);
189 RETURNOP(cLOGOP->op_next);
196 TAINT_NOT; /* Each statement is presumed innocent */
197 PL_stack_sp = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
199 oldsave = PL_scopestack[PL_scopestack_ix - 1];
200 LEAVE_SCOPE(oldsave);
206 dVAR; dSP; dATARGET; tryAMAGICbin(concat,opASSIGN);
213 bool rcopied = FALSE;
215 if (TARG == right && right != left) {
216 /* mg_get(right) may happen here ... */
217 rpv = SvPV_const(right, rlen);
218 rbyte = !DO_UTF8(right);
219 right = sv_2mortal(newSVpvn(rpv, rlen));
220 rpv = SvPV_const(right, rlen); /* no point setting UTF-8 here */
226 const char* const lpv = SvPV_const(left, llen); /* mg_get(left) may happen here */
227 lbyte = !DO_UTF8(left);
228 sv_setpvn(TARG, lpv, llen);
234 else { /* TARG == left */
236 SvGETMAGIC(left); /* or mg_get(left) may happen here */
238 if (left == right && ckWARN(WARN_UNINITIALIZED))
239 report_uninit(right);
240 sv_setpvn(left, "", 0);
242 (void)SvPV_nomg_const(left, llen); /* Needed to set UTF8 flag */
243 lbyte = !DO_UTF8(left);
248 /* or mg_get(right) may happen here */
250 rpv = SvPV_const(right, rlen);
251 rbyte = !DO_UTF8(right);
253 if (lbyte != rbyte) {
255 sv_utf8_upgrade_nomg(TARG);
258 right = sv_2mortal(newSVpvn(rpv, rlen));
259 sv_utf8_upgrade_nomg(right);
260 rpv = SvPV_const(right, rlen);
263 sv_catpvn_nomg(TARG, rpv, rlen);
274 if (PL_op->op_flags & OPf_MOD) {
275 if (PL_op->op_private & OPpLVAL_INTRO)
276 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
277 if (PL_op->op_private & OPpDEREF) {
279 vivify_ref(PAD_SVl(PL_op->op_targ), PL_op->op_private & OPpDEREF);
289 tryAMAGICunTARGET(iter, 0);
290 PL_last_in_gv = (GV*)(*PL_stack_sp--);
291 if (SvTYPE(PL_last_in_gv) != SVt_PVGV) {
292 if (SvROK(PL_last_in_gv) && SvTYPE(SvRV(PL_last_in_gv)) == SVt_PVGV)
293 PL_last_in_gv = (GV*)SvRV(PL_last_in_gv);
296 XPUSHs((SV*)PL_last_in_gv);
299 PL_last_in_gv = (GV*)(*PL_stack_sp--);
302 return do_readline();
307 dVAR; dSP; tryAMAGICbinSET(eq,0);
308 #ifndef NV_PRESERVES_UV
309 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
311 SETs(boolSV(SvRV(TOPs) == SvRV(TOPp1s)));
315 #ifdef PERL_PRESERVE_IVUV
318 /* Unless the left argument is integer in range we are going
319 to have to use NV maths. Hence only attempt to coerce the
320 right argument if we know the left is integer. */
323 const bool auvok = SvUOK(TOPm1s);
324 const bool buvok = SvUOK(TOPs);
326 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
327 /* Casting IV to UV before comparison isn't going to matter
328 on 2s complement. On 1s complement or sign&magnitude
329 (if we have any of them) it could to make negative zero
330 differ from normal zero. As I understand it. (Need to
331 check - is negative zero implementation defined behaviour
333 const UV buv = SvUVX(POPs);
334 const UV auv = SvUVX(TOPs);
336 SETs(boolSV(auv == buv));
339 { /* ## Mixed IV,UV ## */
343 /* == is commutative so doesn't matter which is left or right */
345 /* top of stack (b) is the iv */
354 /* As uv is a UV, it's >0, so it cannot be == */
358 /* we know iv is >= 0 */
359 SETs(boolSV((UV)iv == SvUVX(uvp)));
367 SETs(boolSV(TOPn == value));
375 if (SvTYPE(TOPs) >= SVt_PVGV && SvTYPE(TOPs) != SVt_PVLV)
376 DIE(aTHX_ PL_no_modify);
377 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
378 && SvIVX(TOPs) != IV_MAX)
380 SvIV_set(TOPs, SvIVX(TOPs) + 1);
381 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
383 else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
395 if (PL_op->op_type == OP_OR)
397 RETURNOP(cLOGOP->op_other);
404 register SV* sv = NULL;
405 bool defined = FALSE;
406 const int op_type = PL_op->op_type;
408 if(op_type == OP_DOR || op_type == OP_DORASSIGN) {
410 if (!sv || !SvANY(sv)) {
411 if (op_type == OP_DOR)
413 RETURNOP(cLOGOP->op_other);
415 } else if (op_type == OP_DEFINED) {
417 if (!sv || !SvANY(sv))
420 DIE(aTHX_ "panic: Invalid op (%s) in pp_defined()", OP_NAME(PL_op));
422 switch (SvTYPE(sv)) {
424 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
428 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
432 if (CvROOT(sv) || CvXSUB(sv))
441 if(op_type == OP_DOR || op_type == OP_DORASSIGN) {
444 if(op_type == OP_DOR)
446 RETURNOP(cLOGOP->op_other);
448 /* assuming OP_DEFINED */
456 dVAR; dSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
457 useleft = USE_LEFT(TOPm1s);
458 #ifdef PERL_PRESERVE_IVUV
459 /* We must see if we can perform the addition with integers if possible,
460 as the integer code detects overflow while the NV code doesn't.
461 If either argument hasn't had a numeric conversion yet attempt to get
462 the IV. It's important to do this now, rather than just assuming that
463 it's not IOK as a PV of "9223372036854775806" may not take well to NV
464 addition, and an SV which is NOK, NV=6.0 ought to be coerced to
465 integer in case the second argument is IV=9223372036854775806
466 We can (now) rely on sv_2iv to do the right thing, only setting the
467 public IOK flag if the value in the NV (or PV) slot is truly integer.
469 A side effect is that this also aggressively prefers integer maths over
470 fp maths for integer values.
472 How to detect overflow?
474 C 99 section 6.2.6.1 says
476 The range of nonnegative values of a signed integer type is a subrange
477 of the corresponding unsigned integer type, and the representation of
478 the same value in each type is the same. A computation involving
479 unsigned operands can never overflow, because a result that cannot be
480 represented by the resulting unsigned integer type is reduced modulo
481 the number that is one greater than the largest value that can be
482 represented by the resulting type.
486 which I read as "unsigned ints wrap."
488 signed integer overflow seems to be classed as "exception condition"
490 If an exceptional condition occurs during the evaluation of an
491 expression (that is, if the result is not mathematically defined or not
492 in the range of representable values for its type), the behavior is
495 (6.5, the 5th paragraph)
497 I had assumed that on 2s complement machines signed arithmetic would
498 wrap, hence coded pp_add and pp_subtract on the assumption that
499 everything perl builds on would be happy. After much wailing and
500 gnashing of teeth it would seem that irix64 knows its ANSI spec well,
501 knows that it doesn't need to, and doesn't. Bah. Anyway, the all-
502 unsigned code below is actually shorter than the old code. :-)
507 /* Unless the left argument is integer in range we are going to have to
508 use NV maths. Hence only attempt to coerce the right argument if
509 we know the left is integer. */
517 /* left operand is undef, treat as zero. + 0 is identity,
518 Could SETi or SETu right now, but space optimise by not adding
519 lots of code to speed up what is probably a rarish case. */
521 /* Left operand is defined, so is it IV? */
524 if ((auvok = SvUOK(TOPm1s)))
527 register const IV aiv = SvIVX(TOPm1s);
530 auvok = 1; /* Now acting as a sign flag. */
531 } else { /* 2s complement assumption for IV_MIN */
539 bool result_good = 0;
542 bool buvok = SvUOK(TOPs);
547 register const IV biv = SvIVX(TOPs);
554 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
555 else "IV" now, independent of how it came in.
556 if a, b represents positive, A, B negative, a maps to -A etc
561 all UV maths. negate result if A negative.
562 add if signs same, subtract if signs differ. */
568 /* Must get smaller */
574 /* result really should be -(auv-buv). as its negation
575 of true value, need to swap our result flag */
592 if (result <= (UV)IV_MIN)
595 /* result valid, but out of range for IV. */
600 } /* Overflow, drop through to NVs. */
607 /* left operand is undef, treat as zero. + 0.0 is identity. */
611 SETn( value + TOPn );
619 AV * const av = PL_op->op_flags & OPf_SPECIAL ?
620 (AV*)PAD_SV(PL_op->op_targ) : GvAV(cGVOP_gv);
621 const U32 lval = PL_op->op_flags & OPf_MOD;
622 SV** const svp = av_fetch(av, PL_op->op_private, lval);
623 SV *sv = (svp ? *svp : &PL_sv_undef);
625 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
626 sv = sv_mortalcopy(sv);
633 dVAR; dSP; dMARK; dTARGET;
635 do_join(TARG, *MARK, MARK, SP);
646 * We ass_u_me that LvTARGOFF() comes first, and that two STRLENs
647 * will be enough to hold an OP*.
649 SV* const sv = sv_newmortal();
650 sv_upgrade(sv, SVt_PVLV);
652 Copy(&PL_op, &LvTARGOFF(sv), 1, OP*);
660 /* Oversized hot code. */
664 dVAR; dSP; dMARK; dORIGMARK;
668 GV * const gv = (PL_op->op_flags & OPf_STACKED) ? (GV*)*++MARK : PL_defoutgv;
670 if (gv && (io = GvIO(gv))
671 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
674 if (MARK == ORIGMARK) {
675 /* If using default handle then we need to make space to
676 * pass object as 1st arg, so move other args up ...
680 Move(MARK, MARK + 1, (SP - MARK) + 1, SV*);
684 *MARK = SvTIED_obj((SV*)io, mg);
687 call_method("PRINT", G_SCALAR);
695 if (!(io = GvIO(gv))) {
696 if ((GvEGV(gv)) && (io = GvIO(GvEGV(gv)))
697 && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar)))
699 if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
700 report_evil_fh(gv, io, PL_op->op_type);
701 SETERRNO(EBADF,RMS_IFI);
704 else if (!(fp = IoOFP(io))) {
705 if (ckWARN2(WARN_CLOSED, WARN_IO)) {
707 report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
708 else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
709 report_evil_fh(gv, io, PL_op->op_type);
711 SETERRNO(EBADF,IoIFP(io)?RMS_FAC:RMS_IFI);
716 if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
718 if (!do_print(*MARK, fp))
722 if (!do_print(PL_ofs_sv, fp)) { /* $, */
731 if (!do_print(*MARK, fp))
739 if (PL_ors_sv && SvOK(PL_ors_sv))
740 if (!do_print(PL_ors_sv, fp)) /* $\ */
743 if (IoFLAGS(io) & IOf_FLUSH)
744 if (PerlIO_flush(fp) == EOF)
754 XPUSHs(&PL_sv_undef);
765 tryAMAGICunDEREF(to_av);
768 if (SvTYPE(av) != SVt_PVAV)
769 DIE(aTHX_ "Not an ARRAY reference");
770 if (PL_op->op_flags & OPf_REF) {
775 if (GIMME == G_SCALAR)
776 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
780 else if (PL_op->op_flags & OPf_MOD
781 && PL_op->op_private & OPpLVAL_INTRO)
782 Perl_croak(aTHX_ PL_no_localize_ref);
785 if (SvTYPE(sv) == SVt_PVAV) {
787 if (PL_op->op_flags & OPf_REF) {
792 if (GIMME == G_SCALAR)
793 Perl_croak(aTHX_ "Can't return array to lvalue"
802 if (SvTYPE(sv) != SVt_PVGV) {
803 if (SvGMAGICAL(sv)) {
809 if (PL_op->op_flags & OPf_REF ||
810 PL_op->op_private & HINT_STRICT_REFS)
811 DIE(aTHX_ PL_no_usym, "an ARRAY");
812 if (ckWARN(WARN_UNINITIALIZED))
814 if (GIMME == G_ARRAY) {
820 if ((PL_op->op_flags & OPf_SPECIAL) &&
821 !(PL_op->op_flags & OPf_MOD))
823 gv = (GV*)gv_fetchsv(sv, 0, SVt_PVAV);
825 && (!is_gv_magical_sv(sv,0)
826 || !(gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PVAV))))
832 if (PL_op->op_private & HINT_STRICT_REFS)
833 DIE(aTHX_ PL_no_symref_sv, sv, "an ARRAY");
834 gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PVAV);
841 if (PL_op->op_private & OPpLVAL_INTRO)
843 if (PL_op->op_flags & OPf_REF) {
848 if (GIMME == G_SCALAR)
849 Perl_croak(aTHX_ "Can't return array to lvalue"
857 if (GIMME == G_ARRAY) {
858 const I32 maxarg = AvFILL(av) + 1;
859 (void)POPs; /* XXXX May be optimized away? */
861 if (SvRMAGICAL(av)) {
863 for (i=0; i < (U32)maxarg; i++) {
864 SV ** const svp = av_fetch(av, i, FALSE);
865 /* See note in pp_helem, and bug id #27839 */
867 ? SvGMAGICAL(*svp) ? sv_mortalcopy(*svp) : *svp
872 Copy(AvARRAY(av), SP+1, maxarg, SV*);
876 else if (GIMME_V == G_SCALAR) {
878 const I32 maxarg = AvFILL(av) + 1;
888 const I32 gimme = GIMME_V;
889 static const char return_hash_to_lvalue_scalar[] = "Can't return hash to lvalue scalar context";
893 tryAMAGICunDEREF(to_hv);
896 if (SvTYPE(hv) != SVt_PVHV)
897 DIE(aTHX_ "Not a HASH reference");
898 if (PL_op->op_flags & OPf_REF) {
903 if (gimme != G_ARRAY)
904 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
908 else if (PL_op->op_flags & OPf_MOD
909 && PL_op->op_private & OPpLVAL_INTRO)
910 Perl_croak(aTHX_ PL_no_localize_ref);
913 if (SvTYPE(sv) == SVt_PVHV) {
915 if (PL_op->op_flags & OPf_REF) {
920 if (gimme != G_ARRAY)
921 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
929 if (SvTYPE(sv) != SVt_PVGV) {
930 if (SvGMAGICAL(sv)) {
936 if (PL_op->op_flags & OPf_REF ||
937 PL_op->op_private & HINT_STRICT_REFS)
938 DIE(aTHX_ PL_no_usym, "a HASH");
939 if (ckWARN(WARN_UNINITIALIZED))
941 if (gimme == G_ARRAY) {
947 if ((PL_op->op_flags & OPf_SPECIAL) &&
948 !(PL_op->op_flags & OPf_MOD))
950 gv = (GV*)gv_fetchsv(sv, 0, SVt_PVHV);
952 && (!is_gv_magical_sv(sv,0)
953 || !(gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PVHV))))
959 if (PL_op->op_private & HINT_STRICT_REFS)
960 DIE(aTHX_ PL_no_symref_sv, sv, "a HASH");
961 gv = (GV*)gv_fetchsv(sv, GV_ADD, SVt_PVHV);
968 if (PL_op->op_private & OPpLVAL_INTRO)
970 if (PL_op->op_flags & OPf_REF) {
975 if (gimme != G_ARRAY)
976 Perl_croak(aTHX_ return_hash_to_lvalue_scalar );
983 if (gimme == G_ARRAY) { /* array wanted */
984 *PL_stack_sp = (SV*)hv;
987 else if (gimme == G_SCALAR) {
989 TARG = Perl_hv_scalar(aTHX_ hv);
996 S_do_oddball(pTHX_ HV *hash, SV **relem, SV **firstrelem)
1003 if (ckWARN(WARN_MISC)) {
1005 if (relem == firstrelem &&
1007 (SvTYPE(SvRV(*relem)) == SVt_PVAV ||
1008 SvTYPE(SvRV(*relem)) == SVt_PVHV))
1010 err = "Reference found where even-sized list expected";
1013 err = "Odd number of elements in hash assignment";
1014 Perl_warner(aTHX_ packWARN(WARN_MISC), err);
1017 tmpstr = NEWSV(29,0);
1018 didstore = hv_store_ent(hash,*relem,tmpstr,0);
1019 if (SvMAGICAL(hash)) {
1020 if (SvSMAGICAL(tmpstr))
1032 SV **lastlelem = PL_stack_sp;
1033 SV **lastrelem = PL_stack_base + POPMARK;
1034 SV **firstrelem = PL_stack_base + POPMARK + 1;
1035 SV **firstlelem = lastrelem + 1;
1037 register SV **relem;
1038 register SV **lelem;
1048 SV **firsthashrelem = NULL; /* "= 0" keeps gcc 2.95 quiet */
1051 PL_delaymagic = DM_DELAY; /* catch simultaneous items */
1054 /* If there's a common identifier on both sides we have to take
1055 * special care that assigning the identifier on the left doesn't
1056 * clobber a value on the right that's used later in the list.
1058 if (PL_op->op_private & (OPpASSIGN_COMMON)) {
1059 EXTEND_MORTAL(lastrelem - firstrelem + 1);
1060 for (relem = firstrelem; relem <= lastrelem; relem++) {
1061 if ((sv = *relem)) {
1062 TAINT_NOT; /* Each item is independent */
1063 *relem = sv_mortalcopy(sv);
1073 while (lelem <= lastlelem) {
1074 TAINT_NOT; /* Each item stands on its own, taintwise. */
1076 switch (SvTYPE(sv)) {
1079 magic = SvMAGICAL(ary) != 0;
1081 av_extend(ary, lastrelem - relem);
1083 while (relem <= lastrelem) { /* gobble up all the rest */
1086 sv = newSVsv(*relem);
1088 didstore = av_store(ary,i++,sv);
1098 case SVt_PVHV: { /* normal hash */
1102 magic = SvMAGICAL(hash) != 0;
1104 firsthashrelem = relem;
1106 while (relem < lastrelem) { /* gobble up all the rest */
1111 sv = &PL_sv_no, relem++;
1112 tmpstr = NEWSV(29,0);
1114 sv_setsv(tmpstr,*relem); /* value */
1115 *(relem++) = tmpstr;
1116 if (gimme != G_VOID && hv_exists_ent(hash, sv, 0))
1117 /* key overwrites an existing entry */
1119 didstore = hv_store_ent(hash,sv,tmpstr,0);
1121 if (SvSMAGICAL(tmpstr))
1128 if (relem == lastrelem) {
1129 do_oddball(hash, relem, firstrelem);
1135 if (SvIMMORTAL(sv)) {
1136 if (relem <= lastrelem)
1140 if (relem <= lastrelem) {
1141 sv_setsv(sv, *relem);
1145 sv_setsv(sv, &PL_sv_undef);
1150 if (PL_delaymagic & ~DM_DELAY) {
1151 if (PL_delaymagic & DM_UID) {
1152 #ifdef HAS_SETRESUID
1153 (void)setresuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1154 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1,
1157 # ifdef HAS_SETREUID
1158 (void)setreuid((PL_delaymagic & DM_RUID) ? PL_uid : (Uid_t)-1,
1159 (PL_delaymagic & DM_EUID) ? PL_euid : (Uid_t)-1);
1162 if ((PL_delaymagic & DM_UID) == DM_RUID) {
1163 (void)setruid(PL_uid);
1164 PL_delaymagic &= ~DM_RUID;
1166 # endif /* HAS_SETRUID */
1168 if ((PL_delaymagic & DM_UID) == DM_EUID) {
1169 (void)seteuid(PL_euid);
1170 PL_delaymagic &= ~DM_EUID;
1172 # endif /* HAS_SETEUID */
1173 if (PL_delaymagic & DM_UID) {
1174 if (PL_uid != PL_euid)
1175 DIE(aTHX_ "No setreuid available");
1176 (void)PerlProc_setuid(PL_uid);
1178 # endif /* HAS_SETREUID */
1179 #endif /* HAS_SETRESUID */
1180 PL_uid = PerlProc_getuid();
1181 PL_euid = PerlProc_geteuid();
1183 if (PL_delaymagic & DM_GID) {
1184 #ifdef HAS_SETRESGID
1185 (void)setresgid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1186 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1,
1189 # ifdef HAS_SETREGID
1190 (void)setregid((PL_delaymagic & DM_RGID) ? PL_gid : (Gid_t)-1,
1191 (PL_delaymagic & DM_EGID) ? PL_egid : (Gid_t)-1);
1194 if ((PL_delaymagic & DM_GID) == DM_RGID) {
1195 (void)setrgid(PL_gid);
1196 PL_delaymagic &= ~DM_RGID;
1198 # endif /* HAS_SETRGID */
1200 if ((PL_delaymagic & DM_GID) == DM_EGID) {
1201 (void)setegid(PL_egid);
1202 PL_delaymagic &= ~DM_EGID;
1204 # endif /* HAS_SETEGID */
1205 if (PL_delaymagic & DM_GID) {
1206 if (PL_gid != PL_egid)
1207 DIE(aTHX_ "No setregid available");
1208 (void)PerlProc_setgid(PL_gid);
1210 # endif /* HAS_SETREGID */
1211 #endif /* HAS_SETRESGID */
1212 PL_gid = PerlProc_getgid();
1213 PL_egid = PerlProc_getegid();
1215 PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid));
1219 if (gimme == G_VOID)
1220 SP = firstrelem - 1;
1221 else if (gimme == G_SCALAR) {
1224 SETi(lastrelem - firstrelem + 1 - duplicates);
1231 /* Removes from the stack the entries which ended up as
1232 * duplicated keys in the hash (fix for [perl #24380]) */
1233 Move(firsthashrelem + duplicates,
1234 firsthashrelem, duplicates, SV**);
1235 lastrelem -= duplicates;
1240 SP = firstrelem + (lastlelem - firstlelem);
1241 lelem = firstlelem + (relem - firstrelem);
1243 *relem++ = (lelem <= lastlelem) ? *lelem++ : &PL_sv_undef;
1251 register PMOP * const pm = cPMOP;
1252 SV * const rv = sv_newmortal();
1253 SV * const sv = newSVrv(rv, "Regexp");
1254 if (pm->op_pmdynflags & PMdf_TAINTED)
1256 sv_magic(sv,(SV*)ReREFCNT_inc(PM_GETRE(pm)), PERL_MAGIC_qr,0,0);
1263 register PMOP *pm = cPMOP;
1265 register const char *t;
1266 register const char *s;
1269 I32 r_flags = REXEC_CHECKED;
1270 const char *truebase; /* Start of string */
1271 register REGEXP *rx = PM_GETRE(pm);
1273 const I32 gimme = GIMME;
1276 const I32 oldsave = PL_savestack_ix;
1277 I32 update_minmatch = 1;
1278 I32 had_zerolen = 0;
1280 if (PL_op->op_flags & OPf_STACKED)
1282 else if (PL_op->op_private & OPpTARGET_MY)
1289 PUTBACK; /* EVAL blocks need stack_sp. */
1290 s = SvPV_const(TARG, len);
1292 DIE(aTHX_ "panic: pp_match");
1294 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
1295 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
1298 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
1300 /* PMdf_USED is set after a ?? matches once */
1301 if (pm->op_pmdynflags & PMdf_USED) {
1303 if (gimme == G_ARRAY)
1308 /* empty pattern special-cased to use last successful pattern if possible */
1309 if (!rx->prelen && PL_curpm) {
1314 if (rx->minlen > (I32)len)
1319 /* XXXX What part of this is needed with true \G-support? */
1320 if ((global = dynpm->op_pmflags & PMf_GLOBAL)) {
1322 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1323 MAGIC* const mg = mg_find(TARG, PERL_MAGIC_regex_global);
1324 if (mg && mg->mg_len >= 0) {
1325 if (!(rx->reganch & ROPT_GPOS_SEEN))
1326 rx->endp[0] = rx->startp[0] = mg->mg_len;
1327 else if (rx->reganch & ROPT_ANCH_GPOS) {
1328 r_flags |= REXEC_IGNOREPOS;
1329 rx->endp[0] = rx->startp[0] = mg->mg_len;
1331 minmatch = (mg->mg_flags & MGf_MINMATCH);
1332 update_minmatch = 0;
1336 if ((!global && rx->nparens)
1337 || SvTEMP(TARG) || PL_sawampersand)
1338 r_flags |= REXEC_COPY_STR;
1340 r_flags |= REXEC_SCREAM;
1343 if (global && rx->startp[0] != -1) {
1344 t = s = rx->endp[0] + truebase;
1345 if ((s + rx->minlen) > strend)
1347 if (update_minmatch++)
1348 minmatch = had_zerolen;
1350 if (rx->reganch & RE_USE_INTUIT &&
1351 DO_UTF8(TARG) == ((rx->reganch & ROPT_UTF8) != 0)) {
1352 /* FIXME - can PL_bostr be made const char *? */
1353 PL_bostr = (char *)truebase;
1354 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, (char *)s, (char *)strend, r_flags, NULL);
1358 if ( (rx->reganch & ROPT_CHECK_ALL)
1360 && ((rx->reganch & ROPT_NOSCAN)
1361 || !((rx->reganch & RE_INTUIT_TAIL)
1362 && (r_flags & REXEC_SCREAM)))
1363 && !SvROK(TARG)) /* Cannot trust since INTUIT cannot guess ^ */
1366 if (CALLREGEXEC(aTHX_ rx, (char*)s, (char *)strend, (char*)truebase, minmatch, TARG, NULL, r_flags))
1369 if (dynpm->op_pmflags & PMf_ONCE)
1370 dynpm->op_pmdynflags |= PMdf_USED;
1379 RX_MATCH_TAINTED_on(rx);
1380 TAINT_IF(RX_MATCH_TAINTED(rx));
1381 if (gimme == G_ARRAY) {
1382 const I32 nparens = rx->nparens;
1383 I32 i = (global && !nparens) ? 1 : 0;
1385 SPAGAIN; /* EVAL blocks could move the stack. */
1386 EXTEND(SP, nparens + i);
1387 EXTEND_MORTAL(nparens + i);
1388 for (i = !i; i <= nparens; i++) {
1389 PUSHs(sv_newmortal());
1390 if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
1391 const I32 len = rx->endp[i] - rx->startp[i];
1392 s = rx->startp[i] + truebase;
1393 if (rx->endp[i] < 0 || rx->startp[i] < 0 ||
1394 len < 0 || len > strend - s)
1395 DIE(aTHX_ "panic: pp_match start/end pointers");
1396 sv_setpvn(*SP, s, len);
1397 if (DO_UTF8(TARG) && is_utf8_string((U8*)s, len))
1402 if (dynpm->op_pmflags & PMf_CONTINUE) {
1404 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1405 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1407 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1408 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1410 if (rx->startp[0] != -1) {
1411 mg->mg_len = rx->endp[0];
1412 if (rx->startp[0] == rx->endp[0])
1413 mg->mg_flags |= MGf_MINMATCH;
1415 mg->mg_flags &= ~MGf_MINMATCH;
1418 had_zerolen = (rx->startp[0] != -1
1419 && rx->startp[0] == rx->endp[0]);
1420 PUTBACK; /* EVAL blocks may use stack */
1421 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
1426 LEAVE_SCOPE(oldsave);
1432 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG))
1433 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1437 sv_magic(TARG, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0);
1438 mg = mg_find(TARG, PERL_MAGIC_regex_global);
1440 if (rx->startp[0] != -1) {
1441 mg->mg_len = rx->endp[0];
1442 if (rx->startp[0] == rx->endp[0])
1443 mg->mg_flags |= MGf_MINMATCH;
1445 mg->mg_flags &= ~MGf_MINMATCH;
1448 LEAVE_SCOPE(oldsave);
1452 yup: /* Confirmed by INTUIT */
1454 RX_MATCH_TAINTED_on(rx);
1455 TAINT_IF(RX_MATCH_TAINTED(rx));
1457 if (dynpm->op_pmflags & PMf_ONCE)
1458 dynpm->op_pmdynflags |= PMdf_USED;
1459 if (RX_MATCH_COPIED(rx))
1460 Safefree(rx->subbeg);
1461 RX_MATCH_COPIED_off(rx);
1462 rx->subbeg = Nullch;
1464 /* FIXME - should rx->subbeg be const char *? */
1465 rx->subbeg = (char *) truebase;
1466 rx->startp[0] = s - truebase;
1467 if (RX_MATCH_UTF8(rx)) {
1468 char * const t = (char*)utf8_hop((U8*)s, rx->minlen);
1469 rx->endp[0] = t - truebase;
1472 rx->endp[0] = s - truebase + rx->minlen;
1474 rx->sublen = strend - truebase;
1477 if (PL_sawampersand) {
1479 #ifdef PERL_OLD_COPY_ON_WRITE
1480 if (SvIsCOW(TARG) || (SvFLAGS(TARG) & CAN_COW_MASK) == CAN_COW_FLAGS) {
1482 PerlIO_printf(Perl_debug_log,
1483 "Copy on write: pp_match $& capture, type %d, truebase=%p, t=%p, difference %d\n",
1484 (int) SvTYPE(TARG), truebase, t,
1487 rx->saved_copy = sv_setsv_cow(rx->saved_copy, TARG);
1488 rx->subbeg = (char *) SvPVX_const(rx->saved_copy) + (t - truebase);
1489 assert (SvPOKp(rx->saved_copy));
1494 rx->subbeg = savepvn(t, strend - t);
1495 #ifdef PERL_OLD_COPY_ON_WRITE
1496 rx->saved_copy = Nullsv;
1499 rx->sublen = strend - t;
1500 RX_MATCH_COPIED_on(rx);
1501 off = rx->startp[0] = s - t;
1502 rx->endp[0] = off + rx->minlen;
1504 else { /* startp/endp are used by @- @+. */
1505 rx->startp[0] = s - truebase;
1506 rx->endp[0] = s - truebase + rx->minlen;
1508 rx->nparens = rx->lastparen = rx->lastcloseparen = 0; /* used by @-, @+, and $^N */
1509 LEAVE_SCOPE(oldsave);
1514 if (global && !(dynpm->op_pmflags & PMf_CONTINUE)) {
1515 if (SvTYPE(TARG) >= SVt_PVMG && SvMAGIC(TARG)) {
1516 MAGIC* mg = mg_find(TARG, PERL_MAGIC_regex_global);
1521 LEAVE_SCOPE(oldsave);
1522 if (gimme == G_ARRAY)
1528 Perl_do_readline(pTHX)
1530 dVAR; dSP; dTARGETSTACKED;
1535 register IO * const io = GvIO(PL_last_in_gv);
1536 register const I32 type = PL_op->op_type;
1537 const I32 gimme = GIMME_V;
1540 if (io && (mg = SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar))) {
1542 XPUSHs(SvTIED_obj((SV*)io, mg));
1545 call_method("READLINE", gimme);
1548 if (gimme == G_SCALAR) {
1550 SvSetSV_nosteal(TARG, result);
1559 if (IoFLAGS(io) & IOf_ARGV) {
1560 if (IoFLAGS(io) & IOf_START) {
1562 if (av_len(GvAVn(PL_last_in_gv)) < 0) {
1563 IoFLAGS(io) &= ~IOf_START;
1564 do_open(PL_last_in_gv,"-",1,FALSE,O_RDONLY,0,Nullfp);
1565 sv_setpvn(GvSVn(PL_last_in_gv), "-", 1);
1566 SvSETMAGIC(GvSV(PL_last_in_gv));
1571 fp = nextargv(PL_last_in_gv);
1572 if (!fp) { /* Note: fp != IoIFP(io) */
1573 (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
1576 else if (type == OP_GLOB)
1577 fp = Perl_start_glob(aTHX_ POPs, io);
1579 else if (type == OP_GLOB)
1581 else if (ckWARN(WARN_IO) && IoTYPE(io) == IoTYPE_WRONLY) {
1582 report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
1586 if ((!io || !(IoFLAGS(io) & IOf_START))
1587 && ckWARN2(WARN_GLOB, WARN_CLOSED))
1589 if (type == OP_GLOB)
1590 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1591 "glob failed (can't start child: %s)",
1594 report_evil_fh(PL_last_in_gv, io, PL_op->op_type);
1596 if (gimme == G_SCALAR) {
1597 /* undef TARG, and push that undefined value */
1598 if (type != OP_RCATLINE) {
1599 SV_CHECK_THINKFIRST_COW_DROP(TARG);
1607 if (gimme == G_SCALAR) {
1611 SvUPGRADE(sv, SVt_PV);
1612 tmplen = SvLEN(sv); /* remember if already alloced */
1613 if (!tmplen && !SvREADONLY(sv))
1614 Sv_Grow(sv, 80); /* try short-buffering it */
1616 if (type == OP_RCATLINE && SvOK(sv)) {
1618 SvPV_force_nolen(sv);
1624 sv = sv_2mortal(NEWSV(57, 80));
1628 /* This should not be marked tainted if the fp is marked clean */
1629 #define MAYBE_TAINT_LINE(io, sv) \
1630 if (!(IoFLAGS(io) & IOf_UNTAINT)) { \
1635 /* delay EOF state for a snarfed empty file */
1636 #define SNARF_EOF(gimme,rs,io,sv) \
1637 (gimme != G_SCALAR || SvCUR(sv) \
1638 || (IoFLAGS(io) & IOf_NOLINE) || !RsSNARF(rs))
1642 if (!sv_gets(sv, fp, offset)
1644 || SNARF_EOF(gimme, PL_rs, io, sv)
1645 || PerlIO_error(fp)))
1647 PerlIO_clearerr(fp);
1648 if (IoFLAGS(io) & IOf_ARGV) {
1649 fp = nextargv(PL_last_in_gv);
1652 (void)do_close(PL_last_in_gv, FALSE);
1654 else if (type == OP_GLOB) {
1655 if (!do_close(PL_last_in_gv, FALSE) && ckWARN(WARN_GLOB)) {
1656 Perl_warner(aTHX_ packWARN(WARN_GLOB),
1657 "glob failed (child exited with status %d%s)",
1658 (int)(STATUS_CURRENT >> 8),
1659 (STATUS_CURRENT & 0x80) ? ", core dumped" : "");
1662 if (gimme == G_SCALAR) {
1663 if (type != OP_RCATLINE) {
1664 SV_CHECK_THINKFIRST_COW_DROP(TARG);
1670 MAYBE_TAINT_LINE(io, sv);
1673 MAYBE_TAINT_LINE(io, sv);
1675 IoFLAGS(io) |= IOf_NOLINE;
1679 if (type == OP_GLOB) {
1683 if (SvCUR(sv) > 0 && SvCUR(PL_rs) > 0) {
1684 tmps = SvEND(sv) - 1;
1685 if (*tmps == *SvPVX_const(PL_rs)) {
1687 SvCUR_set(sv, SvCUR(sv) - 1);
1690 for (t1 = SvPVX_const(sv); *t1; t1++)
1691 if (!isALPHA(*t1) && !isDIGIT(*t1) &&
1692 strchr("$&*(){}[]'\";\\|?<>~`", *t1))
1694 if (*t1 && PerlLIO_lstat(SvPVX_const(sv), &PL_statbuf) < 0) {
1695 (void)POPs; /* Unmatched wildcard? Chuck it... */
1698 } else if (SvUTF8(sv)) { /* OP_READLINE, OP_RCATLINE */
1699 const U8 * const s = (const U8*)SvPVX_const(sv) + offset;
1700 const STRLEN len = SvCUR(sv) - offset;
1703 if (ckWARN(WARN_UTF8) &&
1704 !is_utf8_string_loc(s, len, &f))
1705 /* Emulate :encoding(utf8) warning in the same case. */
1706 Perl_warner(aTHX_ packWARN(WARN_UTF8),
1707 "utf8 \"\\x%02X\" does not map to Unicode",
1708 f < (U8*)SvEND(sv) ? *f : 0);
1710 if (gimme == G_ARRAY) {
1711 if (SvLEN(sv) - SvCUR(sv) > 20) {
1712 SvPV_shrink_to_cur(sv);
1714 sv = sv_2mortal(NEWSV(58, 80));
1717 else if (gimme == G_SCALAR && !tmplen && SvLEN(sv) - SvCUR(sv) > 80) {
1718 /* try to reclaim a bit of scalar space (only on 1st alloc) */
1719 const STRLEN new_len
1720 = SvCUR(sv) < 60 ? 80 : SvCUR(sv)+40; /* allow some slop */
1721 SvPV_renew(sv, new_len);
1730 register PERL_CONTEXT *cx;
1731 I32 gimme = OP_GIMME(PL_op, -1);
1734 if (cxstack_ix >= 0)
1735 gimme = cxstack[cxstack_ix].blk_gimme;
1743 PUSHBLOCK(cx, CXt_BLOCK, SP);
1753 SV * const keysv = POPs;
1754 HV * const hv = (HV*)POPs;
1755 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
1756 const U32 defer = PL_op->op_private & OPpLVAL_DEFER;
1758 const U32 hash = (SvIsCOW_shared_hash(keysv)) ? SvSHARED_HASH(keysv) : 0;
1761 if (SvTYPE(hv) == SVt_PVHV) {
1762 if (PL_op->op_private & OPpLVAL_INTRO) {
1765 /* does the element we're localizing already exist? */
1767 /* can we determine whether it exists? */
1769 || mg_find((SV*)hv, PERL_MAGIC_env)
1770 || ( (mg = mg_find((SV*)hv, PERL_MAGIC_tied))
1771 /* Try to preserve the existenceness of a tied hash
1772 * element by using EXISTS and DELETE if possible.
1773 * Fallback to FETCH and STORE otherwise */
1774 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
1775 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
1776 && gv_fetchmethod_autoload(stash, "DELETE", TRUE)
1778 ) ? hv_exists_ent(hv, keysv, 0) : 1;
1781 he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
1782 svp = he ? &HeVAL(he) : 0;
1788 if (!svp || *svp == &PL_sv_undef) {
1792 DIE(aTHX_ PL_no_helem_sv, keysv);
1794 lv = sv_newmortal();
1795 sv_upgrade(lv, SVt_PVLV);
1797 sv_magic(lv, key2 = newSVsv(keysv), PERL_MAGIC_defelem, Nullch, 0);
1798 SvREFCNT_dec(key2); /* sv_magic() increments refcount */
1799 LvTARG(lv) = SvREFCNT_inc(hv);
1804 if (PL_op->op_private & OPpLVAL_INTRO) {
1805 if (HvNAME_get(hv) && isGV(*svp))
1806 save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
1810 const char * const key = SvPV_const(keysv, keylen);
1811 SAVEDELETE(hv, savepvn(key,keylen), keylen);
1813 save_helem(hv, keysv, svp);
1816 else if (PL_op->op_private & OPpDEREF)
1817 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
1819 sv = (svp ? *svp : &PL_sv_undef);
1820 /* This makes C<local $tied{foo} = $tied{foo}> possible.
1821 * Pushing the magical RHS on to the stack is useless, since
1822 * that magic is soon destined to be misled by the local(),
1823 * and thus the later pp_sassign() will fail to mg_get() the
1824 * old value. This should also cure problems with delayed
1825 * mg_get()s. GSAR 98-07-03 */
1826 if (!lval && SvGMAGICAL(sv))
1827 sv = sv_mortalcopy(sv);
1835 register PERL_CONTEXT *cx;
1840 if (PL_op->op_flags & OPf_SPECIAL) {
1841 cx = &cxstack[cxstack_ix];
1842 cx->blk_oldpm = PL_curpm; /* fake block should preserve $1 et al */
1847 gimme = OP_GIMME(PL_op, -1);
1849 if (cxstack_ix >= 0)
1850 gimme = cxstack[cxstack_ix].blk_gimme;
1856 if (gimme == G_VOID)
1858 else if (gimme == G_SCALAR) {
1862 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
1865 *MARK = sv_mortalcopy(TOPs);
1868 *MARK = &PL_sv_undef;
1872 else if (gimme == G_ARRAY) {
1873 /* in case LEAVE wipes old return values */
1875 for (mark = newsp + 1; mark <= SP; mark++) {
1876 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
1877 *mark = sv_mortalcopy(*mark);
1878 TAINT_NOT; /* Each item is independent */
1882 PL_curpm = newpm; /* Don't pop $1 et al till now */
1892 register PERL_CONTEXT *cx;
1898 cx = &cxstack[cxstack_ix];
1899 if (CxTYPE(cx) != CXt_LOOP)
1900 DIE(aTHX_ "panic: pp_iter");
1902 itersvp = CxITERVAR(cx);
1903 av = cx->blk_loop.iterary;
1904 if (SvTYPE(av) != SVt_PVAV) {
1905 /* iterate ($min .. $max) */
1906 if (cx->blk_loop.iterlval) {
1907 /* string increment */
1908 register SV* cur = cx->blk_loop.iterlval;
1910 const char *max = SvOK((SV*)av) ? SvPV_const((SV*)av, maxlen) : "";
1911 if (!SvNIOK(cur) && SvCUR(cur) <= maxlen) {
1912 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1913 /* safe to reuse old SV */
1914 sv_setsv(*itersvp, cur);
1918 /* we need a fresh SV every time so that loop body sees a
1919 * completely new SV for closures/references to work as
1922 *itersvp = newSVsv(cur);
1923 SvREFCNT_dec(oldsv);
1925 if (strEQ(SvPVX_const(cur), max))
1926 sv_setiv(cur, 0); /* terminate next time */
1933 /* integer increment */
1934 if (cx->blk_loop.iterix > cx->blk_loop.itermax)
1937 /* don't risk potential race */
1938 if (SvREFCNT(*itersvp) == 1 && !SvMAGICAL(*itersvp)) {
1939 /* safe to reuse old SV */
1940 sv_setiv(*itersvp, cx->blk_loop.iterix++);
1944 /* we need a fresh SV every time so that loop body sees a
1945 * completely new SV for closures/references to work as they
1948 *itersvp = newSViv(cx->blk_loop.iterix++);
1949 SvREFCNT_dec(oldsv);
1955 if (PL_op->op_private & OPpITER_REVERSED) {
1956 /* In reverse, use itermax as the min :-) */
1957 if (cx->blk_loop.iterix <= cx->blk_loop.itermax)
1960 if (SvMAGICAL(av) || AvREIFY(av)) {
1961 SV * const * const svp = av_fetch(av, --cx->blk_loop.iterix, FALSE);
1962 sv = svp ? *svp : Nullsv;
1965 sv = AvARRAY(av)[--cx->blk_loop.iterix];
1969 if (cx->blk_loop.iterix >= (av == PL_curstack ? cx->blk_oldsp :
1973 if (SvMAGICAL(av) || AvREIFY(av)) {
1974 SV * const * const svp = av_fetch(av, ++cx->blk_loop.iterix, FALSE);
1975 sv = svp ? *svp : Nullsv;
1978 sv = AvARRAY(av)[++cx->blk_loop.iterix];
1982 if (sv && SvIS_FREED(sv)) {
1984 Perl_croak(aTHX_ "Use of freed value in iteration");
1991 if (av != PL_curstack && sv == &PL_sv_undef) {
1992 SV *lv = cx->blk_loop.iterlval;
1993 if (lv && SvREFCNT(lv) > 1) {
1998 SvREFCNT_dec(LvTARG(lv));
2000 lv = cx->blk_loop.iterlval = NEWSV(26, 0);
2001 sv_upgrade(lv, SVt_PVLV);
2003 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
2005 LvTARG(lv) = SvREFCNT_inc(av);
2006 LvTARGOFF(lv) = cx->blk_loop.iterix;
2007 LvTARGLEN(lv) = (STRLEN)UV_MAX;
2012 *itersvp = SvREFCNT_inc(sv);
2013 SvREFCNT_dec(oldsv);
2021 register PMOP *pm = cPMOP;
2037 register REGEXP *rx = PM_GETRE(pm);
2039 int force_on_match = 0;
2040 const I32 oldsave = PL_savestack_ix;
2042 bool doutf8 = FALSE;
2043 #ifdef PERL_OLD_COPY_ON_WRITE
2048 /* known replacement string? */
2049 dstr = (pm->op_pmflags & PMf_CONST) ? POPs : Nullsv;
2050 if (PL_op->op_flags & OPf_STACKED)
2052 else if (PL_op->op_private & OPpTARGET_MY)
2059 #ifdef PERL_OLD_COPY_ON_WRITE
2060 /* Awooga. Awooga. "bool" types that are actually char are dangerous,
2061 because they make integers such as 256 "false". */
2062 is_cow = SvIsCOW(TARG) ? TRUE : FALSE;
2065 sv_force_normal_flags(TARG,0);
2068 #ifdef PERL_OLD_COPY_ON_WRITE
2072 || ( (SvTYPE(TARG) == SVt_PVGV || SvTYPE(TARG) > SVt_PVLV)
2073 && !(SvTYPE(TARG) == SVt_PVGV && SvFAKE(TARG)))))
2074 DIE(aTHX_ PL_no_modify);
2077 s = SvPV_mutable(TARG, len);
2078 if (!SvPOKp(TARG) || SvTYPE(TARG) == SVt_PVGV)
2080 rxtainted = ((pm->op_pmdynflags & PMdf_TAINTED) ||
2081 (PL_tainted && (pm->op_pmflags & PMf_RETAINT)));
2086 RX_MATCH_UTF8_set(rx, DO_UTF8(TARG));
2090 DIE(aTHX_ "panic: pp_subst");
2093 slen = RX_MATCH_UTF8(rx) ? utf8_length((U8*)s, (U8*)strend) : len;
2094 maxiters = 2 * slen + 10; /* We can match twice at each
2095 position, once with zero-length,
2096 second time with non-zero. */
2098 if (!rx->prelen && PL_curpm) {
2102 r_flags = (rx->nparens || SvTEMP(TARG) || PL_sawampersand)
2103 ? REXEC_COPY_STR : 0;
2105 r_flags |= REXEC_SCREAM;
2108 if (rx->reganch & RE_USE_INTUIT) {
2110 s = CALLREG_INTUIT_START(aTHX_ rx, TARG, s, strend, r_flags, NULL);
2114 /* How to do it in subst? */
2115 /* if ( (rx->reganch & ROPT_CHECK_ALL)
2117 && ((rx->reganch & ROPT_NOSCAN)
2118 || !((rx->reganch & RE_INTUIT_TAIL)
2119 && (r_flags & REXEC_SCREAM))))
2124 /* only replace once? */
2125 once = !(rpm->op_pmflags & PMf_GLOBAL);
2127 /* known replacement string? */
2129 /* replacement needing upgrading? */
2130 if (DO_UTF8(TARG) && !doutf8) {
2131 nsv = sv_newmortal();
2134 sv_recode_to_utf8(nsv, PL_encoding);
2136 sv_utf8_upgrade(nsv);
2137 c = SvPV_const(nsv, clen);
2141 c = SvPV_const(dstr, clen);
2142 doutf8 = DO_UTF8(dstr);
2150 /* can do inplace substitution? */
2152 #ifdef PERL_OLD_COPY_ON_WRITE
2155 && (I32)clen <= rx->minlen && (once || !(r_flags & REXEC_COPY_STR))
2156 && !(rx->reganch & ROPT_LOOKBEHIND_SEEN)
2157 && (!doutf8 || SvUTF8(TARG))) {
2158 if (!CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2159 r_flags | REXEC_CHECKED))
2163 LEAVE_SCOPE(oldsave);
2166 #ifdef PERL_OLD_COPY_ON_WRITE
2167 if (SvIsCOW(TARG)) {
2168 assert (!force_on_match);
2172 if (force_on_match) {
2174 s = SvPV_force(TARG, len);
2179 SvSCREAM_off(TARG); /* disable possible screamer */
2181 rxtainted |= RX_MATCH_TAINTED(rx);
2182 m = orig + rx->startp[0];
2183 d = orig + rx->endp[0];
2185 if (m - s > strend - d) { /* faster to shorten from end */
2187 Copy(c, m, clen, char);
2192 Move(d, m, i, char);
2196 SvCUR_set(TARG, m - s);
2198 else if ((i = m - s)) { /* faster from front */
2206 Copy(c, m, clen, char);
2211 Copy(c, d, clen, char);
2216 TAINT_IF(rxtainted & 1);
2222 if (iters++ > maxiters)
2223 DIE(aTHX_ "Substitution loop");
2224 rxtainted |= RX_MATCH_TAINTED(rx);
2225 m = rx->startp[0] + orig;
2228 Move(s, d, i, char);
2232 Copy(c, d, clen, char);
2235 s = rx->endp[0] + orig;
2236 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2238 /* don't match same null twice */
2239 REXEC_NOT_FIRST|REXEC_IGNOREPOS));
2242 SvCUR_set(TARG, d - SvPVX_const(TARG) + i);
2243 Move(s, d, i+1, char); /* include the NUL */
2245 TAINT_IF(rxtainted & 1);
2247 PUSHs(sv_2mortal(newSViv((I32)iters)));
2249 (void)SvPOK_only_UTF8(TARG);
2250 TAINT_IF(rxtainted);
2251 if (SvSMAGICAL(TARG)) {
2259 LEAVE_SCOPE(oldsave);
2263 if (CALLREGEXEC(aTHX_ rx, s, strend, orig, 0, TARG, NULL,
2264 r_flags | REXEC_CHECKED))
2266 if (force_on_match) {
2268 s = SvPV_force(TARG, len);
2271 #ifdef PERL_OLD_COPY_ON_WRITE
2274 rxtainted |= RX_MATCH_TAINTED(rx);
2275 dstr = newSVpvn(m, s-m);
2280 register PERL_CONTEXT *cx;
2282 (void)ReREFCNT_inc(rx);
2284 RETURNOP(cPMOP->op_pmreplroot);
2286 r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
2288 if (iters++ > maxiters)
2289 DIE(aTHX_ "Substitution loop");
2290 rxtainted |= RX_MATCH_TAINTED(rx);
2291 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
2296 strend = s + (strend - m);
2298 m = rx->startp[0] + orig;
2299 if (doutf8 && !SvUTF8(dstr))
2300 sv_catpvn_utf8_upgrade(dstr, s, m - s, nsv);
2302 sv_catpvn(dstr, s, m-s);
2303 s = rx->endp[0] + orig;
2305 sv_catpvn(dstr, c, clen);
2308 } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
2309 TARG, NULL, r_flags));
2310 if (doutf8 && !DO_UTF8(TARG))
2311 sv_catpvn_utf8_upgrade(dstr, s, strend - s, nsv);
2313 sv_catpvn(dstr, s, strend - s);
2315 #ifdef PERL_OLD_COPY_ON_WRITE
2316 /* The match may make the string COW. If so, brilliant, because that's
2317 just saved us one malloc, copy and free - the regexp has donated
2318 the old buffer, and we malloc an entirely new one, rather than the
2319 regexp malloc()ing a buffer and copying our original, only for
2320 us to throw it away here during the substitution. */
2321 if (SvIsCOW(TARG)) {
2322 sv_force_normal_flags(TARG, SV_COW_DROP_PV);
2328 SvPV_set(TARG, SvPVX(dstr));
2329 SvCUR_set(TARG, SvCUR(dstr));
2330 SvLEN_set(TARG, SvLEN(dstr));
2331 doutf8 |= DO_UTF8(dstr);
2332 SvPV_set(dstr, (char*)0);
2335 TAINT_IF(rxtainted & 1);
2337 PUSHs(sv_2mortal(newSViv((I32)iters)));
2339 (void)SvPOK_only(TARG);
2342 TAINT_IF(rxtainted);
2345 LEAVE_SCOPE(oldsave);
2354 LEAVE_SCOPE(oldsave);
2363 PL_stack_base[PL_markstack_ptr[-1]++] = PL_stack_base[*PL_markstack_ptr];
2364 ++*PL_markstack_ptr;
2365 LEAVE; /* exit inner scope */
2368 if (PL_stack_base + *PL_markstack_ptr > SP) {
2370 const I32 gimme = GIMME_V;
2372 LEAVE; /* exit outer scope */
2373 (void)POPMARK; /* pop src */
2374 items = --*PL_markstack_ptr - PL_markstack_ptr[-1];
2375 (void)POPMARK; /* pop dst */
2376 SP = PL_stack_base + POPMARK; /* pop original mark */
2377 if (gimme == G_SCALAR) {
2378 if (PL_op->op_private & OPpGREP_LEX) {
2379 SV* const sv = sv_newmortal();
2380 sv_setiv(sv, items);
2388 else if (gimme == G_ARRAY)
2395 ENTER; /* enter inner scope */
2398 src = PL_stack_base[*PL_markstack_ptr];
2400 if (PL_op->op_private & OPpGREP_LEX)
2401 PAD_SVl(PL_op->op_targ) = src;
2405 RETURNOP(cLOGOP->op_other);
2416 register PERL_CONTEXT *cx;
2419 if (CxMULTICALL(&cxstack[cxstack_ix]))
2423 cxstack_ix++; /* temporarily protect top context */
2426 if (gimme == G_SCALAR) {
2429 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2431 *MARK = SvREFCNT_inc(TOPs);
2436 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2438 *MARK = sv_mortalcopy(sv);
2443 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2447 *MARK = &PL_sv_undef;
2451 else if (gimme == G_ARRAY) {
2452 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2453 if (!SvTEMP(*MARK)) {
2454 *MARK = sv_mortalcopy(*MARK);
2455 TAINT_NOT; /* Each item is independent */
2463 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2464 PL_curpm = newpm; /* ... and pop $1 et al */
2467 return cx->blk_sub.retop;
2470 /* This duplicates the above code because the above code must not
2471 * get any slower by more conditions */
2479 register PERL_CONTEXT *cx;
2482 if (CxMULTICALL(&cxstack[cxstack_ix]))
2486 cxstack_ix++; /* temporarily protect top context */
2490 if (cx->blk_sub.lval & OPpENTERSUB_INARGS) {
2491 /* We are an argument to a function or grep().
2492 * This kind of lvalueness was legal before lvalue
2493 * subroutines too, so be backward compatible:
2494 * cannot report errors. */
2496 /* Scalar context *is* possible, on the LHS of -> only,
2497 * as in f()->meth(). But this is not an lvalue. */
2498 if (gimme == G_SCALAR)
2500 if (gimme == G_ARRAY) {
2501 if (!CvLVALUE(cx->blk_sub.cv))
2502 goto temporise_array;
2503 EXTEND_MORTAL(SP - newsp);
2504 for (mark = newsp + 1; mark <= SP; mark++) {
2507 else if (SvFLAGS(*mark) & (SVs_PADTMP | SVf_READONLY))
2508 *mark = sv_mortalcopy(*mark);
2510 /* Can be a localized value subject to deletion. */
2511 PL_tmps_stack[++PL_tmps_ix] = *mark;
2512 (void)SvREFCNT_inc(*mark);
2517 else if (cx->blk_sub.lval) { /* Leave it as it is if we can. */
2518 /* Here we go for robustness, not for speed, so we change all
2519 * the refcounts so the caller gets a live guy. Cannot set
2520 * TEMP, so sv_2mortal is out of question. */
2521 if (!CvLVALUE(cx->blk_sub.cv)) {
2527 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
2529 if (gimme == G_SCALAR) {
2533 /* Temporaries are bad unless they happen to be elements
2534 * of a tied hash or array */
2535 if (SvFLAGS(TOPs) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY) &&
2536 !(SvRMAGICAL(TOPs) && mg_find(TOPs, PERL_MAGIC_tiedelem))) {
2542 DIE(aTHX_ "Can't return %s from lvalue subroutine",
2543 SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
2544 : "a readonly value" : "a temporary");
2546 else { /* Can be a localized value
2547 * subject to deletion. */
2548 PL_tmps_stack[++PL_tmps_ix] = *mark;
2549 (void)SvREFCNT_inc(*mark);
2552 else { /* Should not happen? */
2558 DIE(aTHX_ "%s returned from lvalue subroutine in scalar context",
2559 (MARK > SP ? "Empty array" : "Array"));
2563 else if (gimme == G_ARRAY) {
2564 EXTEND_MORTAL(SP - newsp);
2565 for (mark = newsp + 1; mark <= SP; mark++) {
2566 if (*mark != &PL_sv_undef
2567 && SvFLAGS(*mark) & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)) {
2568 /* Might be flattened array after $#array = */
2575 DIE(aTHX_ "Can't return a %s from lvalue subroutine",
2576 SvREADONLY(TOPs) ? "readonly value" : "temporary");
2579 /* Can be a localized value subject to deletion. */
2580 PL_tmps_stack[++PL_tmps_ix] = *mark;
2581 (void)SvREFCNT_inc(*mark);
2587 if (gimme == G_SCALAR) {
2591 if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
2593 *MARK = SvREFCNT_inc(TOPs);
2598 sv = SvREFCNT_inc(TOPs); /* FREETMPS could clobber it */
2600 *MARK = sv_mortalcopy(sv);
2605 *MARK = SvTEMP(TOPs) ? TOPs : sv_mortalcopy(TOPs);
2609 *MARK = &PL_sv_undef;
2613 else if (gimme == G_ARRAY) {
2615 for (MARK = newsp + 1; MARK <= SP; MARK++) {
2616 if (!SvTEMP(*MARK)) {
2617 *MARK = sv_mortalcopy(*MARK);
2618 TAINT_NOT; /* Each item is independent */
2627 POPSUB(cx,sv); /* Stack values are safe: release CV and @_ ... */
2628 PL_curpm = newpm; /* ... and pop $1 et al */
2631 return cx->blk_sub.retop;
2636 S_get_db_sub(pTHX_ SV **svp, CV *cv)
2639 SV * const dbsv = GvSVn(PL_DBsub);
2642 if (!PERLDB_SUB_NN) {
2645 if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED))
2646 || strEQ(GvNAME(gv), "END")
2647 || ((GvCV(gv) != cv) && /* Could be imported, and old sub redefined. */
2648 !( (SvTYPE(*svp) == SVt_PVGV) && (GvCV((GV*)*svp) == cv)
2649 && (gv = (GV*)*svp) ))) {
2650 /* Use GV from the stack as a fallback. */
2651 /* GV is potentially non-unique, or contain different CV. */
2652 SV * const tmp = newRV((SV*)cv);
2653 sv_setsv(dbsv, tmp);
2657 gv_efullname3(dbsv, gv, Nullch);
2661 const int type = SvTYPE(dbsv);
2662 if (type < SVt_PVIV && type != SVt_IV)
2663 sv_upgrade(dbsv, SVt_PVIV);
2664 (void)SvIOK_on(dbsv);
2665 SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */
2669 PL_curcopdb = PL_curcop;
2670 cv = GvCV(PL_DBsub);
2679 register PERL_CONTEXT *cx;
2681 const bool hasargs = (PL_op->op_flags & OPf_STACKED) != 0;
2684 DIE(aTHX_ "Not a CODE reference");
2685 switch (SvTYPE(sv)) {
2686 /* This is overwhelming the most common case: */
2688 if (!(cv = GvCVu((GV*)sv))) {
2690 cv = sv_2cv(sv, &stash, &gv, 0);
2701 if (sv == &PL_sv_yes) { /* unfound import, ignore */
2703 SP = PL_stack_base + POPMARK;
2706 if (SvGMAGICAL(sv)) {
2710 sym = SvPOKp(sv) ? SvPVX_const(sv) : Nullch;
2713 sym = SvPV_nolen_const(sv);
2716 DIE(aTHX_ PL_no_usym, "a subroutine");
2717 if (PL_op->op_private & HINT_STRICT_REFS)
2718 DIE(aTHX_ PL_no_symref, sym, "a subroutine");
2719 cv = get_cv(sym, TRUE);
2724 SV * const * sp = &sv; /* Used in tryAMAGICunDEREF macro. */
2725 tryAMAGICunDEREF(to_cv);
2728 if (SvTYPE(cv) == SVt_PVCV)
2733 DIE(aTHX_ "Not a CODE reference");
2734 /* This is the second most common case: */
2744 if (!CvROOT(cv) && !CvXSUB(cv)) {
2748 /* anonymous or undef'd function leaves us no recourse */
2749 if (CvANON(cv) || !(gv = CvGV(cv)))
2750 DIE(aTHX_ "Undefined subroutine called");
2752 /* autoloaded stub? */
2753 if (cv != GvCV(gv)) {
2756 /* should call AUTOLOAD now? */
2759 if ((autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv),
2766 sub_name = sv_newmortal();
2767 gv_efullname3(sub_name, gv, Nullch);
2768 DIE(aTHX_ "Undefined subroutine &%"SVf" called", sub_name);
2772 DIE(aTHX_ "Not a CODE reference");
2777 if ((PL_op->op_private & OPpENTERSUB_DB) && GvCV(PL_DBsub) && !CvNODEBUG(cv)) {
2778 if (CvASSERTION(cv) && PL_DBassertion)
2779 sv_setiv(PL_DBassertion, 1);
2781 cv = get_db_sub(&sv, cv);
2782 if (!cv || (!CvXSUB(cv) && !CvSTART(cv)))
2783 DIE(aTHX_ "No DB::sub routine defined");
2786 if (!(CvXSUB(cv))) {
2787 /* This path taken at least 75% of the time */
2789 register I32 items = SP - MARK;
2790 AV* const padlist = CvPADLIST(cv);
2791 PUSHBLOCK(cx, CXt_SUB, MARK);
2793 cx->blk_sub.retop = PL_op->op_next;
2795 /* XXX This would be a natural place to set C<PL_compcv = cv> so
2796 * that eval'' ops within this sub know the correct lexical space.
2797 * Owing the speed considerations, we choose instead to search for
2798 * the cv using find_runcv() when calling doeval().
2800 if (CvDEPTH(cv) >= 2) {
2801 PERL_STACK_OVERFLOW_CHECK();
2802 pad_push(padlist, CvDEPTH(cv));
2805 PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
2808 AV* const av = (AV*)PAD_SVl(0);
2810 /* @_ is normally not REAL--this should only ever
2811 * happen when DB::sub() calls things that modify @_ */
2816 cx->blk_sub.savearray = GvAV(PL_defgv);
2817 GvAV(PL_defgv) = (AV*)SvREFCNT_inc(av);
2818 CX_CURPAD_SAVE(cx->blk_sub);
2819 cx->blk_sub.argarray = av;
2822 if (items > AvMAX(av) + 1) {
2823 SV **ary = AvALLOC(av);
2824 if (AvARRAY(av) != ary) {
2825 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
2826 SvPV_set(av, (char*)ary);
2828 if (items > AvMAX(av) + 1) {
2829 AvMAX(av) = items - 1;
2830 Renew(ary,items,SV*);
2832 SvPV_set(av, (char*)ary);
2835 Copy(MARK,AvARRAY(av),items,SV*);
2836 AvFILLp(av) = items - 1;
2844 /* warning must come *after* we fully set up the context
2845 * stuff so that __WARN__ handlers can safely dounwind()
2848 if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)
2849 && !(PERLDB_SUB && cv == GvCV(PL_DBsub)))
2850 sub_crush_depth(cv);
2852 DEBUG_S(PerlIO_printf(Perl_debug_log,
2853 "%p entersub returning %p\n", thr, CvSTART(cv)));
2855 RETURNOP(CvSTART(cv));
2858 #ifdef PERL_XSUB_OLDSTYLE
2859 if (CvOLDSTYLE(cv)) {
2860 I32 (*fp3)(int,int,int);
2862 register I32 items = SP - MARK;
2863 /* We dont worry to copy from @_. */
2868 PL_stack_sp = mark + 1;
2869 fp3 = (I32(*)(int,int,int))CvXSUB(cv);
2870 items = (*fp3)(CvXSUBANY(cv).any_i32,
2871 MARK - PL_stack_base + 1,
2873 PL_stack_sp = PL_stack_base + items;
2876 #endif /* PERL_XSUB_OLDSTYLE */
2878 I32 markix = TOPMARK;
2883 /* Need to copy @_ to stack. Alternative may be to
2884 * switch stack to @_, and copy return values
2885 * back. This would allow popping @_ in XSUB, e.g.. XXXX */
2886 AV * const av = GvAV(PL_defgv);
2887 const I32 items = AvFILLp(av) + 1; /* @_ is not tieable */
2890 /* Mark is at the end of the stack. */
2892 Copy(AvARRAY(av), SP + 1, items, SV*);
2897 /* We assume first XSUB in &DB::sub is the called one. */
2899 SAVEVPTR(PL_curcop);
2900 PL_curcop = PL_curcopdb;
2903 /* Do we need to open block here? XXXX */
2904 (void)(*CvXSUB(cv))(aTHX_ cv);
2906 /* Enforce some sanity in scalar context. */
2907 if (gimme == G_SCALAR && ++markix != PL_stack_sp - PL_stack_base ) {
2908 if (markix > PL_stack_sp - PL_stack_base)
2909 *(PL_stack_base + markix) = &PL_sv_undef;
2911 *(PL_stack_base + markix) = *PL_stack_sp;
2912 PL_stack_sp = PL_stack_base + markix;
2921 Perl_sub_crush_depth(pTHX_ CV *cv)
2924 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on anonymous subroutine");
2926 SV* const tmpstr = sv_newmortal();
2927 gv_efullname3(tmpstr, CvGV(cv), Nullch);
2928 Perl_warner(aTHX_ packWARN(WARN_RECURSION), "Deep recursion on subroutine \"%"SVf"\"",
2937 SV* const elemsv = POPs;
2938 IV elem = SvIV(elemsv);
2939 AV* const av = (AV*)POPs;
2940 const U32 lval = PL_op->op_flags & OPf_MOD || LVRET;
2941 const U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > av_len(av));
2944 if (SvROK(elemsv) && !SvGAMAGIC(elemsv) && ckWARN(WARN_MISC))
2945 Perl_warner(aTHX_ packWARN(WARN_MISC), "Use of reference \"%"SVf"\" as array index", elemsv);
2947 elem -= PL_curcop->cop_arybase;
2948 if (SvTYPE(av) != SVt_PVAV)
2950 svp = av_fetch(av, elem, lval && !defer);
2952 #ifdef PERL_MALLOC_WRAP
2953 if (SvUOK(elemsv)) {
2954 const UV uv = SvUV(elemsv);
2955 elem = uv > IV_MAX ? IV_MAX : uv;
2957 else if (SvNOK(elemsv))
2958 elem = (IV)SvNV(elemsv);
2960 static const char oom_array_extend[] =
2961 "Out of memory during array extend"; /* Duplicated in av.c */
2962 MEM_WRAP_CHECK_1(elem,SV*,oom_array_extend);
2965 if (!svp || *svp == &PL_sv_undef) {
2968 DIE(aTHX_ PL_no_aelem, elem);
2969 lv = sv_newmortal();
2970 sv_upgrade(lv, SVt_PVLV);
2972 sv_magic(lv, Nullsv, PERL_MAGIC_defelem, Nullch, 0);
2973 LvTARG(lv) = SvREFCNT_inc(av);
2974 LvTARGOFF(lv) = elem;
2979 if (PL_op->op_private & OPpLVAL_INTRO)
2980 save_aelem(av, elem, svp);
2981 else if (PL_op->op_private & OPpDEREF)
2982 vivify_ref(*svp, PL_op->op_private & OPpDEREF);
2984 sv = (svp ? *svp : &PL_sv_undef);
2985 if (!lval && SvGMAGICAL(sv)) /* see note in pp_helem() */
2986 sv = sv_mortalcopy(sv);
2992 Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
2997 Perl_croak(aTHX_ PL_no_modify);
2998 if (SvTYPE(sv) < SVt_RV)
2999 sv_upgrade(sv, SVt_RV);
3000 else if (SvTYPE(sv) >= SVt_PV) {
3007 SvRV_set(sv, NEWSV(355,0));
3010 SvRV_set(sv, (SV*)newAV());
3013 SvRV_set(sv, (SV*)newHV());
3024 SV* const sv = TOPs;
3027 SV* const rsv = SvRV(sv);
3028 if (SvTYPE(rsv) == SVt_PVCV) {
3034 SETs(method_common(sv, Null(U32*)));
3041 SV* const sv = cSVOP_sv;
3042 U32 hash = SvSHARED_HASH(sv);
3044 XPUSHs(method_common(sv, &hash));
3049 S_method_common(pTHX_ SV* meth, U32* hashp)
3056 const char* packname = Nullch;
3057 SV *packsv = Nullsv;
3059 const char * const name = SvPV_const(meth, namelen);
3060 SV * const sv = *(PL_stack_base + TOPMARK + 1);
3063 Perl_croak(aTHX_ "Can't call method \"%s\" on an undefined value", name);
3071 /* this isn't a reference */
3072 if(SvOK(sv) && (packname = SvPV_const(sv, packlen))) {
3073 const HE* const he = hv_fetch_ent(PL_stashcache, sv, 0, 0);
3075 stash = INT2PTR(HV*,SvIV(HeVAL(he)));
3082 !(iogv = gv_fetchsv(sv, 0, SVt_PVIO)) ||
3083 !(ob=(SV*)GvIO(iogv)))
3085 /* this isn't the name of a filehandle either */
3087 ((UTF8_IS_START(*packname) && DO_UTF8(sv))
3088 ? !isIDFIRST_utf8((U8*)packname)
3089 : !isIDFIRST(*packname)
3092 Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
3093 SvOK(sv) ? "without a package or object reference"
3094 : "on an undefined value");
3096 /* assume it's a package name */
3097 stash = gv_stashpvn(packname, packlen, FALSE);
3101 SV* ref = newSViv(PTR2IV(stash));
3102 hv_store(PL_stashcache, packname, packlen, ref, 0);
3106 /* it _is_ a filehandle name -- replace with a reference */
3107 *(PL_stack_base + TOPMARK + 1) = sv_2mortal(newRV((SV*)iogv));
3110 /* if we got here, ob should be a reference or a glob */
3111 if (!ob || !(SvOBJECT(ob)
3112 || (SvTYPE(ob) == SVt_PVGV && (ob = (SV*)GvIO((GV*)ob))
3115 Perl_croak(aTHX_ "Can't call method \"%s\" on unblessed reference",
3119 stash = SvSTASH(ob);
3122 /* NOTE: stash may be null, hope hv_fetch_ent and
3123 gv_fetchmethod can cope (it seems they can) */
3125 /* shortcut for simple names */
3127 const HE* const he = hv_fetch_ent(stash, meth, 0, *hashp);
3129 gv = (GV*)HeVAL(he);
3130 if (isGV(gv) && GvCV(gv) &&
3131 (!GvCVGEN(gv) || GvCVGEN(gv) == PL_sub_generation))
3132 return (SV*)GvCV(gv);
3136 gv = gv_fetchmethod(stash ? stash : (HV*)packsv, name);
3139 /* This code tries to figure out just what went wrong with
3140 gv_fetchmethod. It therefore needs to duplicate a lot of
3141 the internals of that function. We can't move it inside
3142 Perl_gv_fetchmethod_autoload(), however, since that would
3143 cause UNIVERSAL->can("NoSuchPackage::foo") to croak, and we
3146 const char* leaf = name;
3147 const char* sep = Nullch;
3150 for (p = name; *p; p++) {
3152 sep = p, leaf = p + 1;
3153 else if (*p == ':' && *(p + 1) == ':')
3154 sep = p, leaf = p + 2;
3156 if (!sep || ((sep - name) == 5 && strnEQ(name, "SUPER", 5))) {
3157 /* the method name is unqualified or starts with SUPER:: */
3158 bool need_strlen = 1;
3160 packname = CopSTASHPV(PL_curcop);
3163 HEK * const packhek = HvNAME_HEK(stash);
3165 packname = HEK_KEY(packhek);
3166 packlen = HEK_LEN(packhek);
3176 "Can't use anonymous symbol table for method lookup");
3178 else if (need_strlen)
3179 packlen = strlen(packname);
3183 /* the method name is qualified */
3185 packlen = sep - name;
3188 /* we're relying on gv_fetchmethod not autovivifying the stash */
3189 if (gv_stashpvn(packname, packlen, FALSE)) {
3191 "Can't locate object method \"%s\" via package \"%.*s\"",
3192 leaf, (int)packlen, packname);
3196 "Can't locate object method \"%s\" via package \"%.*s\""
3197 " (perhaps you forgot to load \"%.*s\"?)",
3198 leaf, (int)packlen, packname, (int)packlen, packname);
3201 return isGV(gv) ? (SV*)GvCV(gv) : (SV*)gv;
3206 * c-indentation-style: bsd
3208 * indent-tabs-mode: t
3211 * ex: set ts=8 sts=4 sw=4 noet: