3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 * 'It's a big house this, and very peculiar. Always a bit more
13 * to discover, and no knowing what you'll find round a corner.
14 * And Elves, sir!' --Samwise Gamgee
16 * [p.225 of _The Lord of the Rings_, II/i: "Many Meetings"]
19 /* This file contains general pp ("push/pop") functions that execute the
20 * opcodes that make up a perl program. A typical pp function expects to
21 * find its arguments on the stack, and usually pushes its results onto
22 * the stack, hence the 'pp' terminology. Each OP structure contains
23 * a pointer to the relevant pp_foo() function.
33 /* XXX I can't imagine anyone who doesn't have this actually _needs_
34 it, since pid_t is an integral type.
37 #ifdef NEED_GETPID_PROTO
38 extern Pid_t getpid (void);
42 * Some BSDs and Cygwin default to POSIX math instead of IEEE.
43 * This switches them over to IEEE.
45 #if defined(LIBM_LIB_VERSION)
46 _LIB_VERSION_TYPE _LIB_VERSION = _IEEE_;
49 /* variations on pp_null */
55 if (GIMME_V == G_SCALAR)
66 assert(SvTYPE(TARG) == SVt_PVAV);
67 if (PL_op->op_private & OPpLVAL_INTRO)
68 if (!(PL_op->op_private & OPpPAD_STATE))
69 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
71 if (PL_op->op_flags & OPf_REF) {
75 if (GIMME == G_SCALAR)
76 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
81 if (gimme == G_ARRAY) {
82 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
84 if (SvMAGICAL(TARG)) {
86 for (i=0; i < (U32)maxarg; i++) {
87 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
88 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
92 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
96 else if (gimme == G_SCALAR) {
97 SV* const sv = sv_newmortal();
98 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
110 assert(SvTYPE(TARG) == SVt_PVHV);
112 if (PL_op->op_private & OPpLVAL_INTRO)
113 if (!(PL_op->op_private & OPpPAD_STATE))
114 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
115 if (PL_op->op_flags & OPf_REF)
118 if (GIMME == G_SCALAR)
119 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
123 if (gimme == G_ARRAY) {
126 else if (gimme == G_SCALAR) {
127 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
135 static const char S_no_symref_sv[] =
136 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
142 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
146 sv = amagic_deref_call(sv, to_gv_amg);
150 if (SvTYPE(sv) == SVt_PVIO) {
151 GV * const gv = MUTABLE_GV(sv_newmortal());
152 gv_init(gv, 0, "", 0, 0);
153 GvIOp(gv) = MUTABLE_IO(sv);
154 SvREFCNT_inc_void_NN(sv);
157 else if (!isGV_with_GP(sv))
158 DIE(aTHX_ "Not a GLOB reference");
161 if (!isGV_with_GP(sv)) {
162 if (!SvOK(sv) && sv != &PL_sv_undef) {
163 /* If this is a 'my' scalar and flag is set then vivify
167 Perl_croak_no_modify(aTHX);
168 if (PL_op->op_private & OPpDEREF) {
170 if (cUNOP->op_targ) {
172 SV * const namesv = PAD_SV(cUNOP->op_targ);
173 const char * const name = SvPV(namesv, len);
174 gv = MUTABLE_GV(newSV(0));
175 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
178 const char * const name = CopSTASHPV(PL_curcop);
181 prepare_SV_for_RV(sv);
182 SvRV_set(sv, MUTABLE_SV(gv));
187 if (PL_op->op_flags & OPf_REF ||
188 PL_op->op_private & HINT_STRICT_REFS)
189 DIE(aTHX_ PL_no_usym, "a symbol");
190 if (ckWARN(WARN_UNINITIALIZED))
194 if ((PL_op->op_flags & OPf_SPECIAL) &&
195 !(PL_op->op_flags & OPf_MOD))
197 SV * const temp = MUTABLE_SV(gv_fetchsv(sv, 0, SVt_PVGV));
199 && (!is_gv_magical_sv(sv,0)
200 || !(sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD,
207 if (PL_op->op_private & HINT_STRICT_REFS)
208 DIE(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), "a symbol");
209 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
210 == OPpDONT_INIT_GV) {
211 /* We are the target of a coderef assignment. Return
212 the scalar unchanged, and let pp_sasssign deal with
216 sv = MUTABLE_SV(gv_fetchsv(sv, GV_ADD, SVt_PVGV));
218 /* FAKE globs in the symbol table cause weird bugs (#77810) */
219 if (sv) SvFAKE_off(sv);
222 if (sv && SvFAKE(sv)) {
223 SV *newsv = sv_newmortal();
224 sv_setsv_flags(newsv, sv, 0);
228 if (PL_op->op_private & OPpLVAL_INTRO)
229 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
234 /* Helper function for pp_rv2sv and pp_rv2av */
236 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
237 const svtype type, SV ***spp)
242 PERL_ARGS_ASSERT_SOFTREF2XV;
244 if (PL_op->op_private & HINT_STRICT_REFS) {
246 Perl_die(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
248 Perl_die(aTHX_ PL_no_usym, what);
251 if (PL_op->op_flags & OPf_REF)
252 Perl_die(aTHX_ PL_no_usym, what);
253 if (ckWARN(WARN_UNINITIALIZED))
255 if (type != SVt_PV && GIMME_V == G_ARRAY) {
259 **spp = &PL_sv_undef;
262 if ((PL_op->op_flags & OPf_SPECIAL) &&
263 !(PL_op->op_flags & OPf_MOD))
265 gv = gv_fetchsv(sv, 0, type);
267 && (!is_gv_magical_sv(sv,0)
268 || !(gv = gv_fetchsv(sv, GV_ADD, type))))
270 **spp = &PL_sv_undef;
275 gv = gv_fetchsv(sv, GV_ADD, type);
285 if (!(PL_op->op_private & OPpDEREFed))
289 sv = amagic_deref_call(sv, to_sv_amg);
294 switch (SvTYPE(sv)) {
300 DIE(aTHX_ "Not a SCALAR reference");
307 if (!isGV_with_GP(gv)) {
308 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
314 if (PL_op->op_flags & OPf_MOD) {
315 if (PL_op->op_private & OPpLVAL_INTRO) {
316 if (cUNOP->op_first->op_type == OP_NULL)
317 sv = save_scalar(MUTABLE_GV(TOPs));
319 sv = save_scalar(gv);
321 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
323 else if (PL_op->op_private & OPpDEREF)
324 vivify_ref(sv, PL_op->op_private & OPpDEREF);
333 AV * const av = MUTABLE_AV(TOPs);
334 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
336 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
338 *sv = newSV_type(SVt_PVMG);
339 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
343 SETs(sv_2mortal(newSViv(
344 AvFILL(MUTABLE_AV(av)) + CopARYBASE_get(PL_curcop)
354 if (PL_op->op_flags & OPf_MOD || LVRET) {
355 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
356 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
358 LvTARG(ret) = SvREFCNT_inc_simple(sv);
359 PUSHs(ret); /* no SvSETMAGIC */
363 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
364 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
365 if (mg && mg->mg_len >= 0) {
370 PUSHi(i + CopARYBASE_get(PL_curcop));
383 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
385 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
388 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
389 /* (But not in defined().) */
391 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
394 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
395 if ((PL_op->op_private & OPpLVAL_INTRO)) {
396 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
399 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
402 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
406 cv = MUTABLE_CV(&PL_sv_undef);
407 SETs(MUTABLE_SV(cv));
417 SV *ret = &PL_sv_undef;
419 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
420 const char * s = SvPVX_const(TOPs);
421 if (strnEQ(s, "CORE::", 6)) {
422 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
423 if (code < 0) { /* Overridable. */
424 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
425 int i = 0, n = 0, seen_question = 0, defgv = 0;
427 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
429 if (code == -KEY_chop || code == -KEY_chomp
430 || code == -KEY_exec || code == -KEY_system)
432 if (code == -KEY_mkdir) {
433 ret = newSVpvs_flags("_;$", SVs_TEMP);
436 if (code == -KEY_keys || code == -KEY_values || code == -KEY_each) {
437 ret = newSVpvs_flags("+", SVs_TEMP);
440 if (code == -KEY_push || code == -KEY_unshift) {
441 ret = newSVpvs_flags("+@", SVs_TEMP);
444 if (code == -KEY_pop || code == -KEY_shift) {
445 ret = newSVpvs_flags(";+", SVs_TEMP);
448 if (code == -KEY_splice) {
449 ret = newSVpvs_flags("+;$$@", SVs_TEMP);
452 if (code == -KEY_tied || code == -KEY_untie) {
453 ret = newSVpvs_flags("\\[$@%*]", SVs_TEMP);
456 if (code == -KEY_tie) {
457 ret = newSVpvs_flags("\\[$@%*]$@", SVs_TEMP);
460 if (code == -KEY_readpipe) {
461 s = "CORE::backtick";
463 while (i < MAXO) { /* The slow way. */
464 if (strEQ(s + 6, PL_op_name[i])
465 || strEQ(s + 6, PL_op_desc[i]))
471 goto nonesuch; /* Should not happen... */
473 defgv = PL_opargs[i] & OA_DEFGV;
474 oa = PL_opargs[i] >> OASHIFT;
476 if (oa & OA_OPTIONAL && !seen_question && !defgv) {
480 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
481 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
482 /* But globs are already references (kinda) */
483 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
487 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
490 if (defgv && str[n - 1] == '$')
493 ret = newSVpvn_flags(str, n - 1, SVs_TEMP);
495 else if (code) /* Non-Overridable */
497 else { /* None such */
499 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
503 cv = sv_2cv(TOPs, &stash, &gv, 0);
505 ret = newSVpvn_flags(SvPVX_const(cv), SvCUR(cv), SVs_TEMP);
514 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
516 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
518 PUSHs(MUTABLE_SV(cv));
532 if (GIMME != G_ARRAY) {
536 *MARK = &PL_sv_undef;
537 *MARK = refto(*MARK);
541 EXTEND_MORTAL(SP - MARK);
543 *MARK = refto(*MARK);
548 S_refto(pTHX_ SV *sv)
553 PERL_ARGS_ASSERT_REFTO;
555 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
558 if (!(sv = LvTARG(sv)))
561 SvREFCNT_inc_void_NN(sv);
563 else if (SvTYPE(sv) == SVt_PVAV) {
564 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
565 av_reify(MUTABLE_AV(sv));
567 SvREFCNT_inc_void_NN(sv);
569 else if (SvPADTMP(sv) && !IS_PADGV(sv))
573 SvREFCNT_inc_void_NN(sv);
576 sv_upgrade(rv, SVt_IV);
586 SV * const sv = POPs;
591 if (!sv || !SvROK(sv))
594 pv = sv_reftype(SvRV(sv),TRUE);
595 PUSHp(pv, strlen(pv));
605 stash = CopSTASH(PL_curcop);
607 SV * const ssv = POPs;
611 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
612 Perl_croak(aTHX_ "Attempt to bless into a reference");
613 ptr = SvPV_const(ssv,len);
615 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
616 "Explicit blessing to '' (assuming package main)");
617 stash = gv_stashpvn(ptr, len, GV_ADD);
620 (void)sv_bless(TOPs, stash);
629 const char * const elem = SvPV_nolen_const(sv);
630 GV * const gv = MUTABLE_GV(POPs);
635 /* elem will always be NUL terminated. */
636 const char * const second_letter = elem + 1;
639 if (strEQ(second_letter, "RRAY"))
640 tmpRef = MUTABLE_SV(GvAV(gv));
643 if (strEQ(second_letter, "ODE"))
644 tmpRef = MUTABLE_SV(GvCVu(gv));
647 if (strEQ(second_letter, "ILEHANDLE")) {
648 /* finally deprecated in 5.8.0 */
649 deprecate("*glob{FILEHANDLE}");
650 tmpRef = MUTABLE_SV(GvIOp(gv));
653 if (strEQ(second_letter, "ORMAT"))
654 tmpRef = MUTABLE_SV(GvFORM(gv));
657 if (strEQ(second_letter, "LOB"))
658 tmpRef = MUTABLE_SV(gv);
661 if (strEQ(second_letter, "ASH"))
662 tmpRef = MUTABLE_SV(GvHV(gv));
665 if (*second_letter == 'O' && !elem[2])
666 tmpRef = MUTABLE_SV(GvIOp(gv));
669 if (strEQ(second_letter, "AME"))
670 sv = newSVhek(GvNAME_HEK(gv));
673 if (strEQ(second_letter, "ACKAGE")) {
674 const HV * const stash = GvSTASH(gv);
675 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
676 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
680 if (strEQ(second_letter, "CALAR"))
695 /* Pattern matching */
700 register unsigned char *s;
703 register I32 *sfirst;
707 if (sv == PL_lastscream) {
711 s = (unsigned char*)(SvPV(sv, len));
713 if (pos <= 0 || !SvPOK(sv) || SvUTF8(sv)) {
714 /* No point in studying a zero length string, and not safe to study
715 anything that doesn't appear to be a simple scalar (and hence might
716 change between now and when the regexp engine runs without our set
717 magic ever running) such as a reference to an object with overloaded
723 SvSCREAM_off(PL_lastscream);
724 SvREFCNT_dec(PL_lastscream);
726 PL_lastscream = SvREFCNT_inc_simple(sv);
728 s = (unsigned char*)(SvPV(sv, len));
732 if (pos > PL_maxscream) {
733 if (PL_maxscream < 0) {
734 PL_maxscream = pos + 80;
735 Newx(PL_screamfirst, 256, I32);
736 Newx(PL_screamnext, PL_maxscream, I32);
739 PL_maxscream = pos + pos / 4;
740 Renew(PL_screamnext, PL_maxscream, I32);
744 sfirst = PL_screamfirst;
745 snext = PL_screamnext;
747 if (!sfirst || !snext)
748 DIE(aTHX_ "do_study: out of memory");
750 for (ch = 256; ch; --ch)
755 register const I32 ch = s[pos];
757 snext[pos] = sfirst[ch] - pos;
764 /* piggyback on m//g magic */
765 sv_magic(sv, NULL, PERL_MAGIC_regex_global, NULL, 0);
774 if (PL_op->op_flags & OPf_STACKED)
776 else if (PL_op->op_private & OPpTARGET_MY)
782 TARG = sv_newmortal();
783 if(PL_op->op_type == OP_TRANSR) {
784 SV * const newsv = newSVsv(sv);
788 else PUSHi(do_trans(sv));
792 /* Lvalue operators. */
804 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
806 do_chop(TARG, *++MARK);
815 SETi(do_chomp(TOPs));
821 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
822 register I32 count = 0;
825 count += do_chomp(*++MARK);
836 if (!PL_op->op_private) {
845 SV_CHECK_THINKFIRST_COW_DROP(sv);
847 switch (SvTYPE(sv)) {
851 av_undef(MUTABLE_AV(sv));
854 hv_undef(MUTABLE_HV(sv));
857 if (cv_const_sv((const CV *)sv))
858 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
859 CvANON((const CV *)sv) ? "(anonymous)"
860 : GvENAME(CvGV((const CV *)sv)));
864 /* let user-undef'd sub keep its identity */
865 GV* const gv = CvGV((const CV *)sv);
866 cv_undef(MUTABLE_CV(sv));
867 CvGV_set(MUTABLE_CV(sv), gv);
872 SvSetMagicSV(sv, &PL_sv_undef);
875 else if (isGV_with_GP(sv)) {
879 /* undef *Pkg::meth_name ... */
881 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
882 && HvENAME_get(stash);
884 if((stash = GvHV((const GV *)sv))) {
885 if(HvENAME_get(stash))
886 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
890 gp_free(MUTABLE_GV(sv));
892 GvGP(sv) = gp_ref(gp);
894 GvLINE(sv) = CopLINE(PL_curcop);
895 GvEGV(sv) = MUTABLE_GV(sv);
899 mro_package_moved(NULL, stash, (const GV *)sv, 0);
901 /* undef *Foo::ISA */
902 if( strEQ(GvNAME((const GV *)sv), "ISA")
903 && (stash = GvSTASH((const GV *)sv))
904 && (method_changed || HvENAME(stash)) )
905 mro_isa_changed_in(stash);
906 else if(method_changed)
907 mro_method_changed_in(
908 GvSTASH((const GV *)sv)
915 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
930 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
931 Perl_croak_no_modify(aTHX);
932 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
933 && SvIVX(TOPs) != IV_MIN)
935 SvIV_set(TOPs, SvIVX(TOPs) - 1);
936 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
947 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
948 Perl_croak_no_modify(aTHX);
950 TARG = sv_newmortal();
951 sv_setsv(TARG, TOPs);
952 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
953 && SvIVX(TOPs) != IV_MAX)
955 SvIV_set(TOPs, SvIVX(TOPs) + 1);
956 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
961 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
971 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
972 Perl_croak_no_modify(aTHX);
974 TARG = sv_newmortal();
975 sv_setsv(TARG, TOPs);
976 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
977 && SvIVX(TOPs) != IV_MIN)
979 SvIV_set(TOPs, SvIVX(TOPs) - 1);
980 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
989 /* Ordinary operators. */
993 dVAR; dSP; dATARGET; SV *svl, *svr;
994 #ifdef PERL_PRESERVE_IVUV
997 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1000 #ifdef PERL_PRESERVE_IVUV
1001 /* For integer to integer power, we do the calculation by hand wherever
1002 we're sure it is safe; otherwise we call pow() and try to convert to
1003 integer afterwards. */
1005 SvIV_please_nomg(svr);
1007 SvIV_please_nomg(svl);
1016 const IV iv = SvIVX(svr);
1020 goto float_it; /* Can't do negative powers this way. */
1024 baseuok = SvUOK(svl);
1026 baseuv = SvUVX(svl);
1028 const IV iv = SvIVX(svl);
1031 baseuok = TRUE; /* effectively it's a UV now */
1033 baseuv = -iv; /* abs, baseuok == false records sign */
1036 /* now we have integer ** positive integer. */
1039 /* foo & (foo - 1) is zero only for a power of 2. */
1040 if (!(baseuv & (baseuv - 1))) {
1041 /* We are raising power-of-2 to a positive integer.
1042 The logic here will work for any base (even non-integer
1043 bases) but it can be less accurate than
1044 pow (base,power) or exp (power * log (base)) when the
1045 intermediate values start to spill out of the mantissa.
1046 With powers of 2 we know this can't happen.
1047 And powers of 2 are the favourite thing for perl
1048 programmers to notice ** not doing what they mean. */
1050 NV base = baseuok ? baseuv : -(NV)baseuv;
1055 while (power >>= 1) {
1063 SvIV_please_nomg(svr);
1066 register unsigned int highbit = 8 * sizeof(UV);
1067 register unsigned int diff = 8 * sizeof(UV);
1068 while (diff >>= 1) {
1070 if (baseuv >> highbit) {
1074 /* we now have baseuv < 2 ** highbit */
1075 if (power * highbit <= 8 * sizeof(UV)) {
1076 /* result will definitely fit in UV, so use UV math
1077 on same algorithm as above */
1078 register UV result = 1;
1079 register UV base = baseuv;
1080 const bool odd_power = cBOOL(power & 1);
1084 while (power >>= 1) {
1091 if (baseuok || !odd_power)
1092 /* answer is positive */
1094 else if (result <= (UV)IV_MAX)
1095 /* answer negative, fits in IV */
1096 SETi( -(IV)result );
1097 else if (result == (UV)IV_MIN)
1098 /* 2's complement assumption: special case IV_MIN */
1101 /* answer negative, doesn't fit */
1102 SETn( -(NV)result );
1112 NV right = SvNV_nomg(svr);
1113 NV left = SvNV_nomg(svl);
1116 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1118 We are building perl with long double support and are on an AIX OS
1119 afflicted with a powl() function that wrongly returns NaNQ for any
1120 negative base. This was reported to IBM as PMR #23047-379 on
1121 03/06/2006. The problem exists in at least the following versions
1122 of AIX and the libm fileset, and no doubt others as well:
1124 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1125 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1126 AIX 5.2.0 bos.adt.libm 5.2.0.85
1128 So, until IBM fixes powl(), we provide the following workaround to
1129 handle the problem ourselves. Our logic is as follows: for
1130 negative bases (left), we use fmod(right, 2) to check if the
1131 exponent is an odd or even integer:
1133 - if odd, powl(left, right) == -powl(-left, right)
1134 - if even, powl(left, right) == powl(-left, right)
1136 If the exponent is not an integer, the result is rightly NaNQ, so
1137 we just return that (as NV_NAN).
1141 NV mod2 = Perl_fmod( right, 2.0 );
1142 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1143 SETn( -Perl_pow( -left, right) );
1144 } else if (mod2 == 0.0) { /* even integer */
1145 SETn( Perl_pow( -left, right) );
1146 } else { /* fractional power */
1150 SETn( Perl_pow( left, right) );
1153 SETn( Perl_pow( left, right) );
1154 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1156 #ifdef PERL_PRESERVE_IVUV
1158 SvIV_please_nomg(svr);
1166 dVAR; dSP; dATARGET; SV *svl, *svr;
1167 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1170 #ifdef PERL_PRESERVE_IVUV
1171 SvIV_please_nomg(svr);
1173 /* Unless the left argument is integer in range we are going to have to
1174 use NV maths. Hence only attempt to coerce the right argument if
1175 we know the left is integer. */
1176 /* Left operand is defined, so is it IV? */
1177 SvIV_please_nomg(svl);
1179 bool auvok = SvUOK(svl);
1180 bool buvok = SvUOK(svr);
1181 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1182 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1191 const IV aiv = SvIVX(svl);
1194 auvok = TRUE; /* effectively it's a UV now */
1196 alow = -aiv; /* abs, auvok == false records sign */
1202 const IV biv = SvIVX(svr);
1205 buvok = TRUE; /* effectively it's a UV now */
1207 blow = -biv; /* abs, buvok == false records sign */
1211 /* If this does sign extension on unsigned it's time for plan B */
1212 ahigh = alow >> (4 * sizeof (UV));
1214 bhigh = blow >> (4 * sizeof (UV));
1216 if (ahigh && bhigh) {
1218 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1219 which is overflow. Drop to NVs below. */
1220 } else if (!ahigh && !bhigh) {
1221 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1222 so the unsigned multiply cannot overflow. */
1223 const UV product = alow * blow;
1224 if (auvok == buvok) {
1225 /* -ve * -ve or +ve * +ve gives a +ve result. */
1229 } else if (product <= (UV)IV_MIN) {
1230 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1231 /* -ve result, which could overflow an IV */
1233 SETi( -(IV)product );
1235 } /* else drop to NVs below. */
1237 /* One operand is large, 1 small */
1240 /* swap the operands */
1242 bhigh = blow; /* bhigh now the temp var for the swap */
1246 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1247 multiplies can't overflow. shift can, add can, -ve can. */
1248 product_middle = ahigh * blow;
1249 if (!(product_middle & topmask)) {
1250 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1252 product_middle <<= (4 * sizeof (UV));
1253 product_low = alow * blow;
1255 /* as for pp_add, UV + something mustn't get smaller.
1256 IIRC ANSI mandates this wrapping *behaviour* for
1257 unsigned whatever the actual representation*/
1258 product_low += product_middle;
1259 if (product_low >= product_middle) {
1260 /* didn't overflow */
1261 if (auvok == buvok) {
1262 /* -ve * -ve or +ve * +ve gives a +ve result. */
1264 SETu( product_low );
1266 } else if (product_low <= (UV)IV_MIN) {
1267 /* 2s complement assumption again */
1268 /* -ve result, which could overflow an IV */
1270 SETi( -(IV)product_low );
1272 } /* else drop to NVs below. */
1274 } /* product_middle too large */
1275 } /* ahigh && bhigh */
1280 NV right = SvNV_nomg(svr);
1281 NV left = SvNV_nomg(svl);
1283 SETn( left * right );
1290 dVAR; dSP; dATARGET; SV *svl, *svr;
1291 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1294 /* Only try to do UV divide first
1295 if ((SLOPPYDIVIDE is true) or
1296 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1298 The assumption is that it is better to use floating point divide
1299 whenever possible, only doing integer divide first if we can't be sure.
1300 If NV_PRESERVES_UV is true then we know at compile time that no UV
1301 can be too large to preserve, so don't need to compile the code to
1302 test the size of UVs. */
1305 # define PERL_TRY_UV_DIVIDE
1306 /* ensure that 20./5. == 4. */
1308 # ifdef PERL_PRESERVE_IVUV
1309 # ifndef NV_PRESERVES_UV
1310 # define PERL_TRY_UV_DIVIDE
1315 #ifdef PERL_TRY_UV_DIVIDE
1316 SvIV_please_nomg(svr);
1318 SvIV_please_nomg(svl);
1320 bool left_non_neg = SvUOK(svl);
1321 bool right_non_neg = SvUOK(svr);
1325 if (right_non_neg) {
1329 const IV biv = SvIVX(svr);
1332 right_non_neg = TRUE; /* effectively it's a UV now */
1338 /* historically undef()/0 gives a "Use of uninitialized value"
1339 warning before dieing, hence this test goes here.
1340 If it were immediately before the second SvIV_please, then
1341 DIE() would be invoked before left was even inspected, so
1342 no inpsection would give no warning. */
1344 DIE(aTHX_ "Illegal division by zero");
1350 const IV aiv = SvIVX(svl);
1353 left_non_neg = TRUE; /* effectively it's a UV now */
1362 /* For sloppy divide we always attempt integer division. */
1364 /* Otherwise we only attempt it if either or both operands
1365 would not be preserved by an NV. If both fit in NVs
1366 we fall through to the NV divide code below. However,
1367 as left >= right to ensure integer result here, we know that
1368 we can skip the test on the right operand - right big
1369 enough not to be preserved can't get here unless left is
1372 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1375 /* Integer division can't overflow, but it can be imprecise. */
1376 const UV result = left / right;
1377 if (result * right == left) {
1378 SP--; /* result is valid */
1379 if (left_non_neg == right_non_neg) {
1380 /* signs identical, result is positive. */
1384 /* 2s complement assumption */
1385 if (result <= (UV)IV_MIN)
1386 SETi( -(IV)result );
1388 /* It's exact but too negative for IV. */
1389 SETn( -(NV)result );
1392 } /* tried integer divide but it was not an integer result */
1393 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1394 } /* left wasn't SvIOK */
1395 } /* right wasn't SvIOK */
1396 #endif /* PERL_TRY_UV_DIVIDE */
1398 NV right = SvNV_nomg(svr);
1399 NV left = SvNV_nomg(svl);
1400 (void)POPs;(void)POPs;
1401 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1402 if (! Perl_isnan(right) && right == 0.0)
1406 DIE(aTHX_ "Illegal division by zero");
1407 PUSHn( left / right );
1414 dVAR; dSP; dATARGET;
1415 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1419 bool left_neg = FALSE;
1420 bool right_neg = FALSE;
1421 bool use_double = FALSE;
1422 bool dright_valid = FALSE;
1425 SV * const svr = TOPs;
1426 SV * const svl = TOPm1s;
1427 SvIV_please_nomg(svr);
1429 right_neg = !SvUOK(svr);
1433 const IV biv = SvIVX(svr);
1436 right_neg = FALSE; /* effectively it's a UV now */
1443 dright = SvNV_nomg(svr);
1444 right_neg = dright < 0;
1447 if (dright < UV_MAX_P1) {
1448 right = U_V(dright);
1449 dright_valid = TRUE; /* In case we need to use double below. */
1455 /* At this point use_double is only true if right is out of range for
1456 a UV. In range NV has been rounded down to nearest UV and
1457 use_double false. */
1458 SvIV_please_nomg(svl);
1459 if (!use_double && SvIOK(svl)) {
1461 left_neg = !SvUOK(svl);
1465 const IV aiv = SvIVX(svl);
1468 left_neg = FALSE; /* effectively it's a UV now */
1476 dleft = SvNV_nomg(svl);
1477 left_neg = dleft < 0;
1481 /* This should be exactly the 5.6 behaviour - if left and right are
1482 both in range for UV then use U_V() rather than floor. */
1484 if (dleft < UV_MAX_P1) {
1485 /* right was in range, so is dleft, so use UVs not double.
1489 /* left is out of range for UV, right was in range, so promote
1490 right (back) to double. */
1492 /* The +0.5 is used in 5.6 even though it is not strictly
1493 consistent with the implicit +0 floor in the U_V()
1494 inside the #if 1. */
1495 dleft = Perl_floor(dleft + 0.5);
1498 dright = Perl_floor(dright + 0.5);
1509 DIE(aTHX_ "Illegal modulus zero");
1511 dans = Perl_fmod(dleft, dright);
1512 if ((left_neg != right_neg) && dans)
1513 dans = dright - dans;
1516 sv_setnv(TARG, dans);
1522 DIE(aTHX_ "Illegal modulus zero");
1525 if ((left_neg != right_neg) && ans)
1528 /* XXX may warn: unary minus operator applied to unsigned type */
1529 /* could change -foo to be (~foo)+1 instead */
1530 if (ans <= ~((UV)IV_MAX)+1)
1531 sv_setiv(TARG, ~ans+1);
1533 sv_setnv(TARG, -(NV)ans);
1536 sv_setuv(TARG, ans);
1545 dVAR; dSP; dATARGET;
1549 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1550 /* TODO: think of some way of doing list-repeat overloading ??? */
1555 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1561 const UV uv = SvUV_nomg(sv);
1563 count = IV_MAX; /* The best we can do? */
1567 const IV iv = SvIV_nomg(sv);
1574 else if (SvNOKp(sv)) {
1575 const NV nv = SvNV_nomg(sv);
1582 count = SvIV_nomg(sv);
1584 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1586 static const char oom_list_extend[] = "Out of memory during list extend";
1587 const I32 items = SP - MARK;
1588 const I32 max = items * count;
1590 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1591 /* Did the max computation overflow? */
1592 if (items > 0 && max > 0 && (max < items || max < count))
1593 Perl_croak(aTHX_ oom_list_extend);
1598 /* This code was intended to fix 20010809.028:
1601 for (($x =~ /./g) x 2) {
1602 print chop; # "abcdabcd" expected as output.
1605 * but that change (#11635) broke this code:
1607 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1609 * I can't think of a better fix that doesn't introduce
1610 * an efficiency hit by copying the SVs. The stack isn't
1611 * refcounted, and mortalisation obviously doesn't
1612 * Do The Right Thing when the stack has more than
1613 * one pointer to the same mortal value.
1617 *SP = sv_2mortal(newSVsv(*SP));
1627 repeatcpy((char*)(MARK + items), (char*)MARK,
1628 items * sizeof(const SV *), count - 1);
1631 else if (count <= 0)
1634 else { /* Note: mark already snarfed by pp_list */
1635 SV * const tmpstr = POPs;
1638 static const char oom_string_extend[] =
1639 "Out of memory during string extend";
1642 sv_setsv_nomg(TARG, tmpstr);
1643 SvPV_force_nomg(TARG, len);
1644 isutf = DO_UTF8(TARG);
1649 const STRLEN max = (UV)count * len;
1650 if (len > MEM_SIZE_MAX / count)
1651 Perl_croak(aTHX_ oom_string_extend);
1652 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1653 SvGROW(TARG, max + 1);
1654 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1655 SvCUR_set(TARG, SvCUR(TARG) * count);
1657 *SvEND(TARG) = '\0';
1660 (void)SvPOK_only_UTF8(TARG);
1662 (void)SvPOK_only(TARG);
1664 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1665 /* The parser saw this as a list repeat, and there
1666 are probably several items on the stack. But we're
1667 in scalar context, and there's no pp_list to save us
1668 now. So drop the rest of the items -- robin@kitsite.com
1680 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1681 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1684 useleft = USE_LEFT(svl);
1685 #ifdef PERL_PRESERVE_IVUV
1686 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1687 "bad things" happen if you rely on signed integers wrapping. */
1688 SvIV_please_nomg(svr);
1690 /* Unless the left argument is integer in range we are going to have to
1691 use NV maths. Hence only attempt to coerce the right argument if
1692 we know the left is integer. */
1693 register UV auv = 0;
1699 a_valid = auvok = 1;
1700 /* left operand is undef, treat as zero. */
1702 /* Left operand is defined, so is it IV? */
1703 SvIV_please_nomg(svl);
1705 if ((auvok = SvUOK(svl)))
1708 register const IV aiv = SvIVX(svl);
1711 auvok = 1; /* Now acting as a sign flag. */
1712 } else { /* 2s complement assumption for IV_MIN */
1720 bool result_good = 0;
1723 bool buvok = SvUOK(svr);
1728 register const IV biv = SvIVX(svr);
1735 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1736 else "IV" now, independent of how it came in.
1737 if a, b represents positive, A, B negative, a maps to -A etc
1742 all UV maths. negate result if A negative.
1743 subtract if signs same, add if signs differ. */
1745 if (auvok ^ buvok) {
1754 /* Must get smaller */
1759 if (result <= buv) {
1760 /* result really should be -(auv-buv). as its negation
1761 of true value, need to swap our result flag */
1773 if (result <= (UV)IV_MIN)
1774 SETi( -(IV)result );
1776 /* result valid, but out of range for IV. */
1777 SETn( -(NV)result );
1781 } /* Overflow, drop through to NVs. */
1786 NV value = SvNV_nomg(svr);
1790 /* left operand is undef, treat as zero - value */
1794 SETn( SvNV_nomg(svl) - value );
1801 dVAR; dSP; dATARGET; SV *svl, *svr;
1802 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1806 const IV shift = SvIV_nomg(svr);
1807 if (PL_op->op_private & HINT_INTEGER) {
1808 const IV i = SvIV_nomg(svl);
1812 const UV u = SvUV_nomg(svl);
1821 dVAR; dSP; dATARGET; SV *svl, *svr;
1822 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1826 const IV shift = SvIV_nomg(svr);
1827 if (PL_op->op_private & HINT_INTEGER) {
1828 const IV i = SvIV_nomg(svl);
1832 const UV u = SvUV_nomg(svl);
1842 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1843 #ifdef PERL_PRESERVE_IVUV
1844 SvIV_please_nomg(TOPs);
1846 SvIV_please_nomg(TOPm1s);
1847 if (SvIOK(TOPm1s)) {
1848 bool auvok = SvUOK(TOPm1s);
1849 bool buvok = SvUOK(TOPs);
1851 if (!auvok && !buvok) { /* ## IV < IV ## */
1852 const IV aiv = SvIVX(TOPm1s);
1853 const IV biv = SvIVX(TOPs);
1856 SETs(boolSV(aiv < biv));
1859 if (auvok && buvok) { /* ## UV < UV ## */
1860 const UV auv = SvUVX(TOPm1s);
1861 const UV buv = SvUVX(TOPs);
1864 SETs(boolSV(auv < buv));
1867 if (auvok) { /* ## UV < IV ## */
1869 const IV biv = SvIVX(TOPs);
1872 /* As (a) is a UV, it's >=0, so it cannot be < */
1877 SETs(boolSV(auv < (UV)biv));
1880 { /* ## IV < UV ## */
1881 const IV aiv = SvIVX(TOPm1s);
1885 /* As (b) is a UV, it's >=0, so it must be < */
1892 SETs(boolSV((UV)aiv < buv));
1898 #ifndef NV_PRESERVES_UV
1899 #ifdef PERL_PRESERVE_IVUV
1902 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1904 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1909 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1911 if (Perl_isnan(left) || Perl_isnan(right))
1913 SETs(boolSV(left < right));
1916 SETs(boolSV(SvNV_nomg(TOPs) < value));
1925 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1926 #ifdef PERL_PRESERVE_IVUV
1927 SvIV_please_nomg(TOPs);
1929 SvIV_please_nomg(TOPm1s);
1930 if (SvIOK(TOPm1s)) {
1931 bool auvok = SvUOK(TOPm1s);
1932 bool buvok = SvUOK(TOPs);
1934 if (!auvok && !buvok) { /* ## IV > IV ## */
1935 const IV aiv = SvIVX(TOPm1s);
1936 const IV biv = SvIVX(TOPs);
1939 SETs(boolSV(aiv > biv));
1942 if (auvok && buvok) { /* ## UV > UV ## */
1943 const UV auv = SvUVX(TOPm1s);
1944 const UV buv = SvUVX(TOPs);
1947 SETs(boolSV(auv > buv));
1950 if (auvok) { /* ## UV > IV ## */
1952 const IV biv = SvIVX(TOPs);
1956 /* As (a) is a UV, it's >=0, so it must be > */
1961 SETs(boolSV(auv > (UV)biv));
1964 { /* ## IV > UV ## */
1965 const IV aiv = SvIVX(TOPm1s);
1969 /* As (b) is a UV, it's >=0, so it cannot be > */
1976 SETs(boolSV((UV)aiv > buv));
1982 #ifndef NV_PRESERVES_UV
1983 #ifdef PERL_PRESERVE_IVUV
1986 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
1988 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1993 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1995 if (Perl_isnan(left) || Perl_isnan(right))
1997 SETs(boolSV(left > right));
2000 SETs(boolSV(SvNV_nomg(TOPs) > value));
2009 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
2010 #ifdef PERL_PRESERVE_IVUV
2011 SvIV_please_nomg(TOPs);
2013 SvIV_please_nomg(TOPm1s);
2014 if (SvIOK(TOPm1s)) {
2015 bool auvok = SvUOK(TOPm1s);
2016 bool buvok = SvUOK(TOPs);
2018 if (!auvok && !buvok) { /* ## IV <= IV ## */
2019 const IV aiv = SvIVX(TOPm1s);
2020 const IV biv = SvIVX(TOPs);
2023 SETs(boolSV(aiv <= biv));
2026 if (auvok && buvok) { /* ## UV <= UV ## */
2027 UV auv = SvUVX(TOPm1s);
2028 UV buv = SvUVX(TOPs);
2031 SETs(boolSV(auv <= buv));
2034 if (auvok) { /* ## UV <= IV ## */
2036 const IV biv = SvIVX(TOPs);
2040 /* As (a) is a UV, it's >=0, so a cannot be <= */
2045 SETs(boolSV(auv <= (UV)biv));
2048 { /* ## IV <= UV ## */
2049 const IV aiv = SvIVX(TOPm1s);
2053 /* As (b) is a UV, it's >=0, so a must be <= */
2060 SETs(boolSV((UV)aiv <= buv));
2066 #ifndef NV_PRESERVES_UV
2067 #ifdef PERL_PRESERVE_IVUV
2070 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2072 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
2077 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2079 if (Perl_isnan(left) || Perl_isnan(right))
2081 SETs(boolSV(left <= right));
2084 SETs(boolSV(SvNV_nomg(TOPs) <= value));
2093 tryAMAGICbin_MG(ge_amg,AMGf_set|AMGf_numeric);
2094 #ifdef PERL_PRESERVE_IVUV
2095 SvIV_please_nomg(TOPs);
2097 SvIV_please_nomg(TOPm1s);
2098 if (SvIOK(TOPm1s)) {
2099 bool auvok = SvUOK(TOPm1s);
2100 bool buvok = SvUOK(TOPs);
2102 if (!auvok && !buvok) { /* ## IV >= IV ## */
2103 const IV aiv = SvIVX(TOPm1s);
2104 const IV biv = SvIVX(TOPs);
2107 SETs(boolSV(aiv >= biv));
2110 if (auvok && buvok) { /* ## UV >= UV ## */
2111 const UV auv = SvUVX(TOPm1s);
2112 const UV buv = SvUVX(TOPs);
2115 SETs(boolSV(auv >= buv));
2118 if (auvok) { /* ## UV >= IV ## */
2120 const IV biv = SvIVX(TOPs);
2124 /* As (a) is a UV, it's >=0, so it must be >= */
2129 SETs(boolSV(auv >= (UV)biv));
2132 { /* ## IV >= UV ## */
2133 const IV aiv = SvIVX(TOPm1s);
2137 /* As (b) is a UV, it's >=0, so a cannot be >= */
2144 SETs(boolSV((UV)aiv >= buv));
2150 #ifndef NV_PRESERVES_UV
2151 #ifdef PERL_PRESERVE_IVUV
2154 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2156 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2161 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2163 if (Perl_isnan(left) || Perl_isnan(right))
2165 SETs(boolSV(left >= right));
2168 SETs(boolSV(SvNV_nomg(TOPs) >= value));
2177 tryAMAGICbin_MG(ne_amg,AMGf_set|AMGf_numeric);
2178 #ifndef NV_PRESERVES_UV
2179 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2181 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
2185 #ifdef PERL_PRESERVE_IVUV
2186 SvIV_please_nomg(TOPs);
2188 SvIV_please_nomg(TOPm1s);
2189 if (SvIOK(TOPm1s)) {
2190 const bool auvok = SvUOK(TOPm1s);
2191 const bool buvok = SvUOK(TOPs);
2193 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2194 /* Casting IV to UV before comparison isn't going to matter
2195 on 2s complement. On 1s complement or sign&magnitude
2196 (if we have any of them) it could make negative zero
2197 differ from normal zero. As I understand it. (Need to
2198 check - is negative zero implementation defined behaviour
2200 const UV buv = SvUVX(POPs);
2201 const UV auv = SvUVX(TOPs);
2203 SETs(boolSV(auv != buv));
2206 { /* ## Mixed IV,UV ## */
2210 /* != is commutative so swap if needed (save code) */
2212 /* swap. top of stack (b) is the iv */
2216 /* As (a) is a UV, it's >0, so it cannot be == */
2225 /* As (b) is a UV, it's >0, so it cannot be == */
2229 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2231 SETs(boolSV((UV)iv != uv));
2238 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2240 if (Perl_isnan(left) || Perl_isnan(right))
2242 SETs(boolSV(left != right));
2245 SETs(boolSV(SvNV_nomg(TOPs) != value));
2254 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2255 #ifndef NV_PRESERVES_UV
2256 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2257 const UV right = PTR2UV(SvRV(POPs));
2258 const UV left = PTR2UV(SvRV(TOPs));
2259 SETi((left > right) - (left < right));
2263 #ifdef PERL_PRESERVE_IVUV
2264 /* Fortunately it seems NaN isn't IOK */
2265 SvIV_please_nomg(TOPs);
2267 SvIV_please_nomg(TOPm1s);
2268 if (SvIOK(TOPm1s)) {
2269 const bool leftuvok = SvUOK(TOPm1s);
2270 const bool rightuvok = SvUOK(TOPs);
2272 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2273 const IV leftiv = SvIVX(TOPm1s);
2274 const IV rightiv = SvIVX(TOPs);
2276 if (leftiv > rightiv)
2278 else if (leftiv < rightiv)
2282 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2283 const UV leftuv = SvUVX(TOPm1s);
2284 const UV rightuv = SvUVX(TOPs);
2286 if (leftuv > rightuv)
2288 else if (leftuv < rightuv)
2292 } else if (leftuvok) { /* ## UV <=> IV ## */
2293 const IV rightiv = SvIVX(TOPs);
2295 /* As (a) is a UV, it's >=0, so it cannot be < */
2298 const UV leftuv = SvUVX(TOPm1s);
2299 if (leftuv > (UV)rightiv) {
2301 } else if (leftuv < (UV)rightiv) {
2307 } else { /* ## IV <=> UV ## */
2308 const IV leftiv = SvIVX(TOPm1s);
2310 /* As (b) is a UV, it's >=0, so it must be < */
2313 const UV rightuv = SvUVX(TOPs);
2314 if ((UV)leftiv > rightuv) {
2316 } else if ((UV)leftiv < rightuv) {
2334 if (Perl_isnan(left) || Perl_isnan(right)) {
2338 value = (left > right) - (left < right);
2342 else if (left < right)
2344 else if (left > right)
2360 int amg_type = sle_amg;
2364 switch (PL_op->op_type) {
2383 tryAMAGICbin_MG(amg_type, AMGf_set);
2386 const int cmp = (IN_LOCALE_RUNTIME
2387 ? sv_cmp_locale_flags(left, right, 0)
2388 : sv_cmp_flags(left, right, 0));
2389 SETs(boolSV(cmp * multiplier < rhs));
2397 tryAMAGICbin_MG(seq_amg, AMGf_set);
2400 SETs(boolSV(sv_eq_flags(left, right, 0)));
2408 tryAMAGICbin_MG(sne_amg, AMGf_set);
2411 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2419 tryAMAGICbin_MG(scmp_amg, 0);
2422 const int cmp = (IN_LOCALE_RUNTIME
2423 ? sv_cmp_locale_flags(left, right, 0)
2424 : sv_cmp_flags(left, right, 0));
2432 dVAR; dSP; dATARGET;
2433 tryAMAGICbin_MG(band_amg, AMGf_assign);
2436 if (SvNIOKp(left) || SvNIOKp(right)) {
2437 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2438 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2439 if (PL_op->op_private & HINT_INTEGER) {
2440 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2444 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2447 if (left_ro_nonnum) SvNIOK_off(left);
2448 if (right_ro_nonnum) SvNIOK_off(right);
2451 do_vop(PL_op->op_type, TARG, left, right);
2460 dVAR; dSP; dATARGET;
2461 const int op_type = PL_op->op_type;
2463 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2466 if (SvNIOKp(left) || SvNIOKp(right)) {
2467 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2468 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2469 if (PL_op->op_private & HINT_INTEGER) {
2470 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2471 const IV r = SvIV_nomg(right);
2472 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2476 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2477 const UV r = SvUV_nomg(right);
2478 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2481 if (left_ro_nonnum) SvNIOK_off(left);
2482 if (right_ro_nonnum) SvNIOK_off(right);
2485 do_vop(op_type, TARG, left, right);
2495 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2497 SV * const sv = TOPs;
2498 const int flags = SvFLAGS(sv);
2500 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
2504 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2505 /* It's publicly an integer, or privately an integer-not-float */
2508 if (SvIVX(sv) == IV_MIN) {
2509 /* 2s complement assumption. */
2510 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2513 else if (SvUVX(sv) <= IV_MAX) {
2518 else if (SvIVX(sv) != IV_MIN) {
2522 #ifdef PERL_PRESERVE_IVUV
2530 SETn(-SvNV_nomg(sv));
2531 else if (SvPOKp(sv)) {
2533 const char * const s = SvPV_nomg_const(sv, len);
2534 if (isIDFIRST(*s)) {
2535 sv_setpvs(TARG, "-");
2538 else if (*s == '+' || *s == '-') {
2539 sv_setsv_nomg(TARG, sv);
2540 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2542 else if (DO_UTF8(sv)) {
2543 SvIV_please_nomg(sv);
2545 goto oops_its_an_int;
2547 sv_setnv(TARG, -SvNV_nomg(sv));
2549 sv_setpvs(TARG, "-");
2554 SvIV_please_nomg(sv);
2556 goto oops_its_an_int;
2557 sv_setnv(TARG, -SvNV_nomg(sv));
2562 SETn(-SvNV_nomg(sv));
2570 tryAMAGICun_MG(not_amg, AMGf_set);
2571 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2578 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2582 if (PL_op->op_private & HINT_INTEGER) {
2583 const IV i = ~SvIV_nomg(sv);
2587 const UV u = ~SvUV_nomg(sv);
2596 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2597 sv_setsv_nomg(TARG, sv);
2598 tmps = (U8*)SvPV_force_nomg(TARG, len);
2601 /* Calculate exact length, let's not estimate. */
2606 U8 * const send = tmps + len;
2607 U8 * const origtmps = tmps;
2608 const UV utf8flags = UTF8_ALLOW_ANYUV;
2610 while (tmps < send) {
2611 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2613 targlen += UNISKIP(~c);
2619 /* Now rewind strings and write them. */
2626 Newx(result, targlen + 1, U8);
2628 while (tmps < send) {
2629 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2631 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2634 sv_usepvn_flags(TARG, (char*)result, targlen,
2635 SV_HAS_TRAILING_NUL);
2642 Newx(result, nchar + 1, U8);
2644 while (tmps < send) {
2645 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2650 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2658 register long *tmpl;
2659 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2662 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2667 for ( ; anum > 0; anum--, tmps++)
2675 /* integer versions of some of the above */
2679 dVAR; dSP; dATARGET;
2680 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2683 SETi( left * right );
2691 dVAR; dSP; dATARGET;
2692 tryAMAGICbin_MG(div_amg, AMGf_assign);
2695 IV value = SvIV_nomg(right);
2697 DIE(aTHX_ "Illegal division by zero");
2698 num = SvIV_nomg(left);
2700 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2704 value = num / value;
2710 #if defined(__GLIBC__) && IVSIZE == 8
2717 /* This is the vanilla old i_modulo. */
2718 dVAR; dSP; dATARGET;
2719 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2723 DIE(aTHX_ "Illegal modulus zero");
2724 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2728 SETi( left % right );
2733 #if defined(__GLIBC__) && IVSIZE == 8
2738 /* This is the i_modulo with the workaround for the _moddi3 bug
2739 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2740 * See below for pp_i_modulo. */
2741 dVAR; dSP; dATARGET;
2742 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2746 DIE(aTHX_ "Illegal modulus zero");
2747 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2751 SETi( left % PERL_ABS(right) );
2758 dVAR; dSP; dATARGET;
2759 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2763 DIE(aTHX_ "Illegal modulus zero");
2764 /* The assumption is to use hereafter the old vanilla version... */
2766 PL_ppaddr[OP_I_MODULO] =
2768 /* .. but if we have glibc, we might have a buggy _moddi3
2769 * (at least glicb 2.2.5 is known to have this bug), in other
2770 * words our integer modulus with negative quad as the second
2771 * argument might be broken. Test for this and re-patch the
2772 * opcode dispatch table if that is the case, remembering to
2773 * also apply the workaround so that this first round works
2774 * right, too. See [perl #9402] for more information. */
2778 /* Cannot do this check with inlined IV constants since
2779 * that seems to work correctly even with the buggy glibc. */
2781 /* Yikes, we have the bug.
2782 * Patch in the workaround version. */
2784 PL_ppaddr[OP_I_MODULO] =
2785 &Perl_pp_i_modulo_1;
2786 /* Make certain we work right this time, too. */
2787 right = PERL_ABS(right);
2790 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2794 SETi( left % right );
2802 dVAR; dSP; dATARGET;
2803 tryAMAGICbin_MG(add_amg, AMGf_assign);
2805 dPOPTOPiirl_ul_nomg;
2806 SETi( left + right );
2813 dVAR; dSP; dATARGET;
2814 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2816 dPOPTOPiirl_ul_nomg;
2817 SETi( left - right );
2825 tryAMAGICbin_MG(lt_amg, AMGf_set);
2828 SETs(boolSV(left < right));
2836 tryAMAGICbin_MG(gt_amg, AMGf_set);
2839 SETs(boolSV(left > right));
2847 tryAMAGICbin_MG(le_amg, AMGf_set);
2850 SETs(boolSV(left <= right));
2858 tryAMAGICbin_MG(ge_amg, AMGf_set);
2861 SETs(boolSV(left >= right));
2869 tryAMAGICbin_MG(eq_amg, AMGf_set);
2872 SETs(boolSV(left == right));
2880 tryAMAGICbin_MG(ne_amg, AMGf_set);
2883 SETs(boolSV(left != right));
2891 tryAMAGICbin_MG(ncmp_amg, 0);
2898 else if (left < right)
2910 tryAMAGICun_MG(neg_amg, 0);
2912 SV * const sv = TOPs;
2913 IV const i = SvIV_nomg(sv);
2919 /* High falutin' math. */
2924 tryAMAGICbin_MG(atan2_amg, 0);
2927 SETn(Perl_atan2(left, right));
2935 int amg_type = sin_amg;
2936 const char *neg_report = NULL;
2937 NV (*func)(NV) = Perl_sin;
2938 const int op_type = PL_op->op_type;
2955 amg_type = sqrt_amg;
2957 neg_report = "sqrt";
2962 tryAMAGICun_MG(amg_type, 0);
2964 SV * const arg = POPs;
2965 const NV value = SvNV_nomg(arg);
2967 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2968 SET_NUMERIC_STANDARD();
2969 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2972 XPUSHn(func(value));
2977 /* Support Configure command-line overrides for rand() functions.
2978 After 5.005, perhaps we should replace this by Configure support
2979 for drand48(), random(), or rand(). For 5.005, though, maintain
2980 compatibility by calling rand() but allow the user to override it.
2981 See INSTALL for details. --Andy Dougherty 15 July 1998
2983 /* Now it's after 5.005, and Configure supports drand48() and random(),
2984 in addition to rand(). So the overrides should not be needed any more.
2985 --Jarkko Hietaniemi 27 September 1998
2988 #ifndef HAS_DRAND48_PROTO
2989 extern double drand48 (void);
3002 if (!PL_srand_called) {
3003 (void)seedDrand01((Rand_seed_t)seed());
3004 PL_srand_called = TRUE;
3014 const UV anum = (MAXARG < 1) ? seed() : POPu;
3015 (void)seedDrand01((Rand_seed_t)anum);
3016 PL_srand_called = TRUE;
3020 /* Historically srand always returned true. We can avoid breaking
3022 sv_setpvs(TARG, "0 but true");
3031 tryAMAGICun_MG(int_amg, AMGf_numeric);
3033 SV * const sv = TOPs;
3034 const IV iv = SvIV_nomg(sv);
3035 /* XXX it's arguable that compiler casting to IV might be subtly
3036 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
3037 else preferring IV has introduced a subtle behaviour change bug. OTOH
3038 relying on floating point to be accurate is a bug. */
3043 else if (SvIOK(sv)) {
3045 SETu(SvUV_nomg(sv));
3050 const NV value = SvNV_nomg(sv);
3052 if (value < (NV)UV_MAX + 0.5) {
3055 SETn(Perl_floor(value));
3059 if (value > (NV)IV_MIN - 0.5) {
3062 SETn(Perl_ceil(value));
3073 tryAMAGICun_MG(abs_amg, AMGf_numeric);
3075 SV * const sv = TOPs;
3076 /* This will cache the NV value if string isn't actually integer */
3077 const IV iv = SvIV_nomg(sv);
3082 else if (SvIOK(sv)) {
3083 /* IVX is precise */
3085 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
3093 /* 2s complement assumption. Also, not really needed as
3094 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
3100 const NV value = SvNV_nomg(sv);
3114 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3118 SV* const sv = POPs;
3120 tmps = (SvPV_const(sv, len));
3122 /* If Unicode, try to downgrade
3123 * If not possible, croak. */
3124 SV* const tsv = sv_2mortal(newSVsv(sv));
3127 sv_utf8_downgrade(tsv, FALSE);
3128 tmps = SvPV_const(tsv, len);
3130 if (PL_op->op_type == OP_HEX)
3133 while (*tmps && len && isSPACE(*tmps))
3137 if (*tmps == 'x' || *tmps == 'X') {
3139 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3141 else if (*tmps == 'b' || *tmps == 'B')
3142 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3144 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3146 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3160 SV * const sv = TOPs;
3162 if (SvGAMAGIC(sv)) {
3163 /* For an overloaded or magic scalar, we can't know in advance if
3164 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
3165 it likes to cache the length. Maybe that should be a documented
3170 = sv_2pv_flags(sv, &len,
3171 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
3174 sv_setsv(TARG, &PL_sv_undef);
3177 else if (DO_UTF8(sv)) {
3178 SETi(utf8_length((U8*)p, (U8*)p + len));
3182 } else if (SvOK(sv)) {
3183 /* Neither magic nor overloaded. */
3185 SETi(sv_len_utf8(sv));
3189 sv_setsv_nomg(TARG, &PL_sv_undef);
3209 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3211 const IV arybase = CopARYBASE_get(PL_curcop);
3213 const char *repl = NULL;
3215 const int num_args = PL_op->op_private & 7;
3216 bool repl_need_utf8_upgrade = FALSE;
3217 bool repl_is_utf8 = FALSE;
3222 repl = SvPV_const(repl_sv, repl_len);
3223 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3226 len_iv = SvIV(len_sv);
3227 len_is_uv = SvIOK_UV(len_sv);
3230 pos1_iv = SvIV(pos_sv);
3231 pos1_is_uv = SvIOK_UV(pos_sv);
3237 sv_utf8_upgrade(sv);
3239 else if (DO_UTF8(sv))
3240 repl_need_utf8_upgrade = TRUE;
3242 tmps = SvPV_const(sv, curlen);
3244 utf8_curlen = sv_len_utf8(sv);
3245 if (utf8_curlen == curlen)
3248 curlen = utf8_curlen;
3253 if ( (pos1_is_uv && arybase < 0) || (pos1_iv >= arybase) ) { /* pos >= $[ */
3254 UV pos1_uv = pos1_iv-arybase;
3255 /* Overflow can occur when $[ < 0 */
3256 if (arybase < 0 && pos1_uv < (UV)pos1_iv)
3261 else if (pos1_is_uv ? (UV)pos1_iv > 0 : pos1_iv > 0) {
3262 goto bound_fail; /* $[=3; substr($_,2,...) */
3264 else { /* pos < $[ */
3265 if (pos1_iv == 0) { /* $[=1; substr($_,0,...) */
3270 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3275 if (pos1_is_uv || pos1_iv > 0) {
3276 if ((UV)pos1_iv > curlen)
3281 if (!len_is_uv && len_iv < 0) {
3282 pos2_iv = curlen + len_iv;
3284 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3287 } else { /* len_iv >= 0 */
3288 if (!pos1_is_uv && pos1_iv < 0) {
3289 pos2_iv = pos1_iv + len_iv;
3290 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3292 if ((UV)len_iv > curlen-(UV)pos1_iv)
3295 pos2_iv = pos1_iv+len_iv;
3305 if (!pos2_is_uv && pos2_iv < 0) {
3306 if (!pos1_is_uv && pos1_iv < 0)
3310 else if (!pos1_is_uv && pos1_iv < 0)
3313 if ((UV)pos2_iv < (UV)pos1_iv)
3315 if ((UV)pos2_iv > curlen)
3319 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3320 const STRLEN pos = (STRLEN)( (UV)pos1_iv );
3321 const STRLEN len = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3322 STRLEN byte_len = len;
3323 STRLEN byte_pos = utf8_curlen
3324 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3326 if (lvalue && !repl) {
3329 if (!SvGMAGICAL(sv)) {
3331 SvPV_force_nolen(sv);
3332 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3333 "Attempt to use reference as lvalue in substr");
3335 if (isGV_with_GP(sv))
3336 SvPV_force_nolen(sv);
3337 else if (SvOK(sv)) /* is it defined ? */
3338 (void)SvPOK_only_UTF8(sv);
3340 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
3343 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3344 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3346 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3347 LvTARGOFF(ret) = pos;
3348 LvTARGLEN(ret) = len;
3351 PUSHs(ret); /* avoid SvSETMAGIC here */
3355 SvTAINTED_off(TARG); /* decontaminate */
3356 SvUTF8_off(TARG); /* decontaminate */
3359 sv_setpvn(TARG, tmps, byte_len);
3360 #ifdef USE_LOCALE_COLLATE
3361 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3367 SV* repl_sv_copy = NULL;
3369 if (repl_need_utf8_upgrade) {
3370 repl_sv_copy = newSVsv(repl_sv);
3371 sv_utf8_upgrade(repl_sv_copy);
3372 repl = SvPV_const(repl_sv_copy, repl_len);
3373 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3377 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3380 SvREFCNT_dec(repl_sv_copy);
3384 PUSHs(TARG); /* avoid SvSETMAGIC here */
3389 Perl_croak(aTHX_ "substr outside of string");
3390 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3397 register const IV size = POPi;
3398 register const IV offset = POPi;
3399 register SV * const src = POPs;
3400 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3403 if (lvalue) { /* it's an lvalue! */
3404 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3405 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3407 LvTARG(ret) = SvREFCNT_inc_simple(src);
3408 LvTARGOFF(ret) = offset;
3409 LvTARGLEN(ret) = size;
3413 SvTAINTED_off(TARG); /* decontaminate */
3417 sv_setuv(ret, do_vecget(src, offset, size));
3433 const char *little_p;
3434 const I32 arybase = CopARYBASE_get(PL_curcop);
3437 const bool is_index = PL_op->op_type == OP_INDEX;
3440 /* arybase is in characters, like offset, so combine prior to the
3441 UTF-8 to bytes calculation. */
3442 offset = POPi - arybase;
3446 big_p = SvPV_const(big, biglen);
3447 little_p = SvPV_const(little, llen);
3449 big_utf8 = DO_UTF8(big);
3450 little_utf8 = DO_UTF8(little);
3451 if (big_utf8 ^ little_utf8) {
3452 /* One needs to be upgraded. */
3453 if (little_utf8 && !PL_encoding) {
3454 /* Well, maybe instead we might be able to downgrade the small
3456 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3459 /* If the large string is ISO-8859-1, and it's not possible to
3460 convert the small string to ISO-8859-1, then there is no
3461 way that it could be found anywhere by index. */
3466 /* At this point, pv is a malloc()ed string. So donate it to temp
3467 to ensure it will get free()d */
3468 little = temp = newSV(0);
3469 sv_usepvn(temp, pv, llen);
3470 little_p = SvPVX(little);
3473 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3476 sv_recode_to_utf8(temp, PL_encoding);
3478 sv_utf8_upgrade(temp);
3483 big_p = SvPV_const(big, biglen);
3486 little_p = SvPV_const(little, llen);
3490 if (SvGAMAGIC(big)) {
3491 /* Life just becomes a lot easier if I use a temporary here.
3492 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3493 will trigger magic and overloading again, as will fbm_instr()
3495 big = newSVpvn_flags(big_p, biglen,
3496 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3499 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3500 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3501 warn on undef, and we've already triggered a warning with the
3502 SvPV_const some lines above. We can't remove that, as we need to
3503 call some SvPV to trigger overloading early and find out if the
3505 This is all getting to messy. The API isn't quite clean enough,
3506 because data access has side effects.
3508 little = newSVpvn_flags(little_p, llen,
3509 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3510 little_p = SvPVX(little);
3514 offset = is_index ? 0 : biglen;
3516 if (big_utf8 && offset > 0)
3517 sv_pos_u2b(big, &offset, 0);
3523 else if (offset > (I32)biglen)
3525 if (!(little_p = is_index
3526 ? fbm_instr((unsigned char*)big_p + offset,
3527 (unsigned char*)big_p + biglen, little, 0)
3528 : rninstr(big_p, big_p + offset,
3529 little_p, little_p + llen)))
3532 retval = little_p - big_p;
3533 if (retval > 0 && big_utf8)
3534 sv_pos_b2u(big, &retval);
3538 PUSHi(retval + arybase);
3544 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3545 if (SvTAINTED(MARK[1]))
3546 TAINT_PROPER("sprintf");
3547 SvTAINTED_off(TARG);
3548 do_sprintf(TARG, SP-MARK, MARK+1);
3549 TAINT_IF(SvTAINTED(TARG));
3561 const U8 *s = (U8*)SvPV_const(argsv, len);
3563 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3564 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3565 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3569 XPUSHu(DO_UTF8(argsv) ?
3570 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3582 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3584 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3586 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3588 (void) POPs; /* Ignore the argument value. */
3589 value = UNICODE_REPLACEMENT;
3595 SvUPGRADE(TARG,SVt_PV);
3597 if (value > 255 && !IN_BYTES) {
3598 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3599 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3600 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3602 (void)SvPOK_only(TARG);
3611 *tmps++ = (char)value;
3613 (void)SvPOK_only(TARG);
3615 if (PL_encoding && !IN_BYTES) {
3616 sv_recode_to_utf8(TARG, PL_encoding);
3618 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3619 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3623 *tmps++ = (char)value;
3639 const char *tmps = SvPV_const(left, len);
3641 if (DO_UTF8(left)) {
3642 /* If Unicode, try to downgrade.
3643 * If not possible, croak.
3644 * Yes, we made this up. */
3645 SV* const tsv = sv_2mortal(newSVsv(left));
3648 sv_utf8_downgrade(tsv, FALSE);
3649 tmps = SvPV_const(tsv, len);
3651 # ifdef USE_ITHREADS
3653 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3654 /* This should be threadsafe because in ithreads there is only
3655 * one thread per interpreter. If this would not be true,
3656 * we would need a mutex to protect this malloc. */
3657 PL_reentrant_buffer->_crypt_struct_buffer =
3658 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3659 #if defined(__GLIBC__) || defined(__EMX__)
3660 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3661 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3662 /* work around glibc-2.2.5 bug */
3663 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3667 # endif /* HAS_CRYPT_R */
3668 # endif /* USE_ITHREADS */
3670 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3672 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3678 "The crypt() function is unimplemented due to excessive paranoia.");
3682 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3683 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3685 /* Both the characters below can be stored in two UTF-8 bytes. In UTF-8 the max
3686 * character that 2 bytes can hold is U+07FF, and in UTF-EBCDIC it is U+03FF.
3687 * See http://www.unicode.org/unicode/reports/tr16 */
3688 #define LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS 0x0178 /* Also is title case */
3689 #define GREEK_CAPITAL_LETTER_MU 0x039C /* Upper and title case of MICRON */
3691 /* Below are several macros that generate code */
3692 /* Generates code to store a unicode codepoint c that is known to occupy
3693 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1. */
3694 #define STORE_UNI_TO_UTF8_TWO_BYTE(p, c) \
3696 *(p) = UTF8_TWO_BYTE_HI(c); \
3697 *((p)+1) = UTF8_TWO_BYTE_LO(c); \
3700 /* Like STORE_UNI_TO_UTF8_TWO_BYTE, but advances p to point to the next
3701 * available byte after the two bytes */
3702 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3704 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3705 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3708 /* Generates code to store the upper case of latin1 character l which is known
3709 * to have its upper case be non-latin1 into the two bytes p and p+1. There
3710 * are only two characters that fit this description, and this macro knows
3711 * about them, and that the upper case values fit into two UTF-8 or UTF-EBCDIC
3713 #define STORE_NON_LATIN1_UC(p, l) \
3715 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3716 STORE_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3717 } else { /* Must be the following letter */ \
3718 STORE_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3722 /* Like STORE_NON_LATIN1_UC, but advances p to point to the next available byte
3723 * after the character stored */
3724 #define CAT_NON_LATIN1_UC(p, l) \
3726 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3727 CAT_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3729 CAT_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3733 /* Generates code to add the two UTF-8 bytes (probably u) that are the upper
3734 * case of l into p and p+1. u must be the result of toUPPER_LATIN1_MOD(l),
3735 * and must require two bytes to store it. Advances p to point to the next
3736 * available position */
3737 #define CAT_TWO_BYTE_UNI_UPPER_MOD(p, l, u) \
3739 if ((u) != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3740 CAT_UNI_TO_UTF8_TWO_BYTE((p), (u)); /* not special, just save it */ \
3741 } else if (l == LATIN_SMALL_LETTER_SHARP_S) { \
3742 *(p)++ = 'S'; *(p)++ = 'S'; /* upper case is 'SS' */ \
3743 } else {/* else is one of the other two special cases */ \
3744 CAT_NON_LATIN1_UC((p), (l)); \
3750 /* Actually is both lcfirst() and ucfirst(). Only the first character
3751 * changes. This means that possibly we can change in-place, ie., just
3752 * take the source and change that one character and store it back, but not
3753 * if read-only etc, or if the length changes */
3758 STRLEN slen; /* slen is the byte length of the whole SV. */
3761 bool inplace; /* ? Convert first char only, in-place */
3762 bool doing_utf8 = FALSE; /* ? using utf8 */
3763 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3764 const int op_type = PL_op->op_type;
3767 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3768 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3769 * stored as UTF-8 at s. */
3770 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3771 * lowercased) character stored in tmpbuf. May be either
3772 * UTF-8 or not, but in either case is the number of bytes */
3776 s = (const U8*)SvPV_nomg_const(source, slen);
3778 if (ckWARN(WARN_UNINITIALIZED))
3779 report_uninit(source);
3784 /* We may be able to get away with changing only the first character, in
3785 * place, but not if read-only, etc. Later we may discover more reasons to
3786 * not convert in-place. */
3787 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3789 /* First calculate what the changed first character should be. This affects
3790 * whether we can just swap it out, leaving the rest of the string unchanged,
3791 * or even if have to convert the dest to UTF-8 when the source isn't */
3793 if (! slen) { /* If empty */
3794 need = 1; /* still need a trailing NUL */
3796 else if (DO_UTF8(source)) { /* Is the source utf8? */
3799 /* TODO: This is #ifdefd out because it has hard-coded the standard mappings,
3800 * and doesn't allow for the user to specify their own. When code is added to
3801 * detect if there is a user-defined mapping in force here, and if so to use
3802 * that, then the code below can be compiled. The detection would be a good
3803 * thing anyway, as currently the user-defined mappings only work on utf8
3804 * strings, and thus depend on the chosen internal storage method, which is a
3806 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3807 if (UTF8_IS_INVARIANT(*s)) {
3809 /* An invariant source character is either ASCII or, in EBCDIC, an
3810 * ASCII equivalent or a caseless C1 control. In both these cases,
3811 * the lower and upper cases of any character are also invariants
3812 * (and title case is the same as upper case). So it is safe to
3813 * use the simple case change macros which avoid the overhead of
3814 * the general functions. Note that if perl were to be extended to
3815 * do locale handling in UTF-8 strings, this wouldn't be true in,
3816 * for example, Lithuanian or Turkic. */
3817 *tmpbuf = (op_type == OP_LCFIRST) ? toLOWER(*s) : toUPPER(*s);
3821 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3824 /* Similarly, if the source character isn't invariant but is in the
3825 * latin1 range (or EBCDIC equivalent thereof), we have the case
3826 * changes compiled into perl, and can avoid the overhead of the
3827 * general functions. In this range, the characters are stored as
3828 * two UTF-8 bytes, and it so happens that any changed-case version
3829 * is also two bytes (in both ASCIIish and EBCDIC machines). */
3833 /* Convert the two source bytes to a single Unicode code point
3834 * value, change case and save for below */
3835 chr = TWO_BYTE_UTF8_TO_UNI(*s, *(s+1));
3836 if (op_type == OP_LCFIRST) { /* lower casing is easy */
3837 U8 lower = toLOWER_LATIN1(chr);
3838 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, lower);
3840 else { /* ucfirst */
3841 U8 upper = toUPPER_LATIN1_MOD(chr);
3843 /* Most of the latin1 range characters are well-behaved. Their
3844 * title and upper cases are the same, and are also in the
3845 * latin1 range. The macro above returns their upper (hence
3846 * title) case, and all that need be done is to save the result
3847 * for below. However, several characters are problematic, and
3848 * have to be handled specially. The MOD in the macro name
3849 * above means that these tricky characters all get mapped to
3850 * the single character LATIN_SMALL_LETTER_Y_WITH_DIAERESIS.
3851 * This mapping saves some tests for the majority of the
3854 if (upper != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3856 /* Not tricky. Just save it. */
3857 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, upper);
3859 else if (chr == LATIN_SMALL_LETTER_SHARP_S) {
3861 /* This one is tricky because it is two characters long,
3862 * though the UTF-8 is still two bytes, so the stored
3863 * length doesn't change */
3864 *tmpbuf = 'S'; /* The UTF-8 is 'Ss' */
3865 *(tmpbuf + 1) = 's';
3869 /* The other two have their title and upper cases the same,
3870 * but are tricky because the changed-case characters
3871 * aren't in the latin1 range. They, however, do fit into
3872 * two UTF-8 bytes */
3873 STORE_NON_LATIN1_UC(tmpbuf, chr);
3878 #endif /* end of dont want to break user-defined casing */
3880 /* Here, can't short-cut the general case */
3882 utf8_to_uvchr(s, &ulen);
3883 if (op_type == OP_UCFIRST) toTITLE_utf8(s, tmpbuf, &tculen);
3884 else toLOWER_utf8(s, tmpbuf, &tculen);
3886 /* we can't do in-place if the length changes. */
3887 if (ulen != tculen) inplace = FALSE;
3888 need = slen + 1 - ulen + tculen;
3889 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3893 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3894 * latin1 is treated as caseless. Note that a locale takes
3896 tculen = 1; /* Most characters will require one byte, but this will
3897 * need to be overridden for the tricky ones */
3900 if (op_type == OP_LCFIRST) {
3902 /* lower case the first letter: no trickiness for any character */
3903 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3904 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3907 else if (IN_LOCALE_RUNTIME) {
3908 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3909 * have upper and title case different
3912 else if (! IN_UNI_8_BIT) {
3913 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3914 * on EBCDIC machines whatever the
3915 * native function does */
3917 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3918 *tmpbuf = toUPPER_LATIN1_MOD(*s);
3920 /* tmpbuf now has the correct title case for all latin1 characters
3921 * except for the several ones that have tricky handling. All
3922 * of these are mapped by the MOD to the letter below. */
3923 if (*tmpbuf == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
3925 /* The length is going to change, with all three of these, so
3926 * can't replace just the first character */
3929 /* We use the original to distinguish between these tricky
3931 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3932 /* Two character title case 'Ss', but can remain non-UTF-8 */
3935 *(tmpbuf + 1) = 's'; /* Assert: length(tmpbuf) >= 2 */
3940 /* The other two tricky ones have their title case outside
3941 * latin1. It is the same as their upper case. */
3943 STORE_NON_LATIN1_UC(tmpbuf, *s);
3945 /* The UTF-8 and UTF-EBCDIC lengths of both these characters
3946 * and their upper cases is 2. */
3949 /* The entire result will have to be in UTF-8. Assume worst
3950 * case sizing in conversion. (all latin1 characters occupy
3951 * at most two bytes in utf8) */
3952 convert_source_to_utf8 = TRUE;
3953 need = slen * 2 + 1;
3955 } /* End of is one of the three special chars */
3956 } /* End of use Unicode (Latin1) semantics */
3957 } /* End of changing the case of the first character */
3959 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3960 * generate the result */
3963 /* We can convert in place. This means we change just the first
3964 * character without disturbing the rest; no need to grow */
3966 s = d = (U8*)SvPV_force_nomg(source, slen);
3972 /* Here, we can't convert in place; we earlier calculated how much
3973 * space we will need, so grow to accommodate that */
3974 SvUPGRADE(dest, SVt_PV);
3975 d = (U8*)SvGROW(dest, need);
3976 (void)SvPOK_only(dest);
3983 if (! convert_source_to_utf8) {
3985 /* Here both source and dest are in UTF-8, but have to create
3986 * the entire output. We initialize the result to be the
3987 * title/lower cased first character, and then append the rest
3989 sv_setpvn(dest, (char*)tmpbuf, tculen);
3991 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3995 const U8 *const send = s + slen;
3997 /* Here the dest needs to be in UTF-8, but the source isn't,
3998 * except we earlier UTF-8'd the first character of the source
3999 * into tmpbuf. First put that into dest, and then append the
4000 * rest of the source, converting it to UTF-8 as we go. */
4002 /* Assert tculen is 2 here because the only two characters that
4003 * get to this part of the code have 2-byte UTF-8 equivalents */
4005 *d++ = *(tmpbuf + 1);
4006 s++; /* We have just processed the 1st char */
4008 for (; s < send; s++) {
4009 d = uvchr_to_utf8(d, *s);
4012 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4016 else { /* in-place UTF-8. Just overwrite the first character */
4017 Copy(tmpbuf, d, tculen, U8);
4018 SvCUR_set(dest, need - 1);
4021 else { /* Neither source nor dest are in or need to be UTF-8 */
4023 if (IN_LOCALE_RUNTIME) {
4027 if (inplace) { /* in-place, only need to change the 1st char */
4030 else { /* Not in-place */
4032 /* Copy the case-changed character(s) from tmpbuf */
4033 Copy(tmpbuf, d, tculen, U8);
4034 d += tculen - 1; /* Code below expects d to point to final
4035 * character stored */
4038 else { /* empty source */
4039 /* See bug #39028: Don't taint if empty */
4043 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
4044 * the destination to retain that flag */
4048 if (!inplace) { /* Finish the rest of the string, unchanged */
4049 /* This will copy the trailing NUL */
4050 Copy(s + 1, d + 1, slen, U8);
4051 SvCUR_set(dest, need - 1);
4058 /* There's so much setup/teardown code common between uc and lc, I wonder if
4059 it would be worth merging the two, and just having a switch outside each
4060 of the three tight loops. There is less and less commonality though */
4074 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4075 && SvTEMP(source) && !DO_UTF8(source)
4076 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
4078 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
4079 * make the loop tight, so we overwrite the source with the dest before
4080 * looking at it, and we need to look at the original source
4081 * afterwards. There would also need to be code added to handle
4082 * switching to not in-place in midstream if we run into characters
4083 * that change the length.
4086 s = d = (U8*)SvPV_force_nomg(source, len);
4093 /* The old implementation would copy source into TARG at this point.
4094 This had the side effect that if source was undef, TARG was now
4095 an undefined SV with PADTMP set, and they don't warn inside
4096 sv_2pv_flags(). However, we're now getting the PV direct from
4097 source, which doesn't have PADTMP set, so it would warn. Hence the
4101 s = (const U8*)SvPV_nomg_const(source, len);
4103 if (ckWARN(WARN_UNINITIALIZED))
4104 report_uninit(source);
4110 SvUPGRADE(dest, SVt_PV);
4111 d = (U8*)SvGROW(dest, min);
4112 (void)SvPOK_only(dest);
4117 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4118 to check DO_UTF8 again here. */
4120 if (DO_UTF8(source)) {
4121 const U8 *const send = s + len;
4122 U8 tmpbuf[UTF8_MAXBYTES+1];
4124 /* All occurrences of these are to be moved to follow any other marks.
4125 * This is context-dependent. We may not be passed enough context to
4126 * move the iota subscript beyond all of them, but we do the best we can
4127 * with what we're given. The result is always better than if we
4128 * hadn't done this. And, the problem would only arise if we are
4129 * passed a character without all its combining marks, which would be
4130 * the caller's mistake. The information this is based on comes from a
4131 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4132 * itself) and so can't be checked properly to see if it ever gets
4133 * revised. But the likelihood of it changing is remote */
4134 bool in_iota_subscript = FALSE;
4137 if (in_iota_subscript && ! is_utf8_mark(s)) {
4138 /* A non-mark. Time to output the iota subscript */
4139 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
4140 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
4142 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4143 in_iota_subscript = FALSE;
4147 /* See comments at the first instance in this file of this ifdef */
4148 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4150 /* If the UTF-8 character is invariant, then it is in the range
4151 * known by the standard macro; result is only one byte long */
4152 if (UTF8_IS_INVARIANT(*s)) {
4156 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4158 /* Likewise, if it fits in a byte, its case change is in our
4160 U8 orig = TWO_BYTE_UTF8_TO_UNI(*s, *s++);
4161 U8 upper = toUPPER_LATIN1_MOD(orig);
4162 CAT_TWO_BYTE_UNI_UPPER_MOD(d, orig, upper);
4170 /* Otherwise, need the general UTF-8 case. Get the changed
4171 * case value and copy it to the output buffer */
4173 const STRLEN u = UTF8SKIP(s);
4176 const UV uv = toUPPER_utf8(s, tmpbuf, &ulen);
4177 if (uv == GREEK_CAPITAL_LETTER_IOTA
4178 && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
4180 in_iota_subscript = TRUE;
4183 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4184 /* If the eventually required minimum size outgrows
4185 * the available space, we need to grow. */
4186 const UV o = d - (U8*)SvPVX_const(dest);
4188 /* If someone uppercases one million U+03B0s we
4189 * SvGROW() one million times. Or we could try
4190 * guessing how much to allocate without allocating too
4191 * much. Such is life. See corresponding comment in
4192 * lc code for another option */
4194 d = (U8*)SvPVX(dest) + o;
4196 Copy(tmpbuf, d, ulen, U8);
4202 if (in_iota_subscript) {
4203 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4207 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4209 else { /* Not UTF-8 */
4211 const U8 *const send = s + len;
4213 /* Use locale casing if in locale; regular style if not treating
4214 * latin1 as having case; otherwise the latin1 casing. Do the
4215 * whole thing in a tight loop, for speed, */
4216 if (IN_LOCALE_RUNTIME) {
4219 for (; s < send; d++, s++)
4220 *d = toUPPER_LC(*s);
4222 else if (! IN_UNI_8_BIT) {
4223 for (; s < send; d++, s++) {
4228 for (; s < send; d++, s++) {
4229 *d = toUPPER_LATIN1_MOD(*s);
4230 if (*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) continue;
4232 /* The mainstream case is the tight loop above. To avoid
4233 * extra tests in that, all three characters that require
4234 * special handling are mapped by the MOD to the one tested
4236 * Use the source to distinguish between the three cases */
4238 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4240 /* uc() of this requires 2 characters, but they are
4241 * ASCII. If not enough room, grow the string */
4242 if (SvLEN(dest) < ++min) {
4243 const UV o = d - (U8*)SvPVX_const(dest);
4245 d = (U8*)SvPVX(dest) + o;
4247 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4248 continue; /* Back to the tight loop; still in ASCII */
4251 /* The other two special handling characters have their
4252 * upper cases outside the latin1 range, hence need to be
4253 * in UTF-8, so the whole result needs to be in UTF-8. So,
4254 * here we are somewhere in the middle of processing a
4255 * non-UTF-8 string, and realize that we will have to convert
4256 * the whole thing to UTF-8. What to do? There are
4257 * several possibilities. The simplest to code is to
4258 * convert what we have so far, set a flag, and continue on
4259 * in the loop. The flag would be tested each time through
4260 * the loop, and if set, the next character would be
4261 * converted to UTF-8 and stored. But, I (khw) didn't want
4262 * to slow down the mainstream case at all for this fairly
4263 * rare case, so I didn't want to add a test that didn't
4264 * absolutely have to be there in the loop, besides the
4265 * possibility that it would get too complicated for
4266 * optimizers to deal with. Another possibility is to just
4267 * give up, convert the source to UTF-8, and restart the
4268 * function that way. Another possibility is to convert
4269 * both what has already been processed and what is yet to
4270 * come separately to UTF-8, then jump into the loop that
4271 * handles UTF-8. But the most efficient time-wise of the
4272 * ones I could think of is what follows, and turned out to
4273 * not require much extra code. */
4275 /* Convert what we have so far into UTF-8, telling the
4276 * function that we know it should be converted, and to
4277 * allow extra space for what we haven't processed yet.
4278 * Assume the worst case space requirements for converting
4279 * what we haven't processed so far: that it will require
4280 * two bytes for each remaining source character, plus the
4281 * NUL at the end. This may cause the string pointer to
4282 * move, so re-find it. */
4284 len = d - (U8*)SvPVX_const(dest);
4285 SvCUR_set(dest, len);
4286 len = sv_utf8_upgrade_flags_grow(dest,
4287 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4289 d = (U8*)SvPVX(dest) + len;
4291 /* And append the current character's upper case in UTF-8 */
4292 CAT_NON_LATIN1_UC(d, *s);
4294 /* Now process the remainder of the source, converting to
4295 * upper and UTF-8. If a resulting byte is invariant in
4296 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4297 * append it to the output. */
4300 for (; s < send; s++) {
4301 U8 upper = toUPPER_LATIN1_MOD(*s);
4302 if UTF8_IS_INVARIANT(upper) {
4306 CAT_TWO_BYTE_UNI_UPPER_MOD(d, *s, upper);
4310 /* Here have processed the whole source; no need to continue
4311 * with the outer loop. Each character has been converted
4312 * to upper case and converted to UTF-8 */
4315 } /* End of processing all latin1-style chars */
4316 } /* End of processing all chars */
4317 } /* End of source is not empty */
4319 if (source != dest) {
4320 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4321 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4323 } /* End of isn't utf8 */
4341 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4342 && SvTEMP(source) && !DO_UTF8(source)) {
4344 /* We can convert in place, as lowercasing anything in the latin1 range
4345 * (or else DO_UTF8 would have been on) doesn't lengthen it */
4347 s = d = (U8*)SvPV_force_nomg(source, len);
4354 /* The old implementation would copy source into TARG at this point.
4355 This had the side effect that if source was undef, TARG was now
4356 an undefined SV with PADTMP set, and they don't warn inside
4357 sv_2pv_flags(). However, we're now getting the PV direct from
4358 source, which doesn't have PADTMP set, so it would warn. Hence the
4362 s = (const U8*)SvPV_nomg_const(source, len);
4364 if (ckWARN(WARN_UNINITIALIZED))
4365 report_uninit(source);
4371 SvUPGRADE(dest, SVt_PV);
4372 d = (U8*)SvGROW(dest, min);
4373 (void)SvPOK_only(dest);
4378 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4379 to check DO_UTF8 again here. */
4381 if (DO_UTF8(source)) {
4382 const U8 *const send = s + len;
4383 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4386 /* See comments at the first instance in this file of this ifdef */
4387 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4388 if (UTF8_IS_INVARIANT(*s)) {
4390 /* Invariant characters use the standard mappings compiled in.
4395 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4397 /* As do the ones in the Latin1 range */
4398 U8 lower = toLOWER_LATIN1(TWO_BYTE_UTF8_TO_UNI(*s, *s++));
4399 CAT_UNI_TO_UTF8_TWO_BYTE(d, lower);
4404 /* Here, is utf8 not in Latin-1 range, have to go out and get
4405 * the mappings from the tables. */
4407 const STRLEN u = UTF8SKIP(s);
4410 #ifndef CONTEXT_DEPENDENT_CASING
4411 toLOWER_utf8(s, tmpbuf, &ulen);
4413 /* This is ifdefd out because it needs more work and thought. It isn't clear
4414 * that we should do it.
4415 * A minor objection is that this is based on a hard-coded rule from the
4416 * Unicode standard, and may change, but this is not very likely at all.
4417 * mktables should check and warn if it does.
4418 * More importantly, if the sigma occurs at the end of the string, we don't
4419 * have enough context to know whether it is part of a larger string or going
4420 * to be or not. It may be that we are passed a subset of the context, via
4421 * a \U...\E, for example, and we could conceivably know the larger context if
4422 * code were changed to pass that in. But, if the string passed in is an
4423 * intermediate result, and the user concatenates two strings together
4424 * after we have made a final sigma, that would be wrong. If the final sigma
4425 * occurs in the middle of the string we are working on, then we know that it
4426 * should be a final sigma, but otherwise we can't be sure. */
4428 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
4430 /* If the lower case is a small sigma, it may be that we need
4431 * to change it to a final sigma. This happens at the end of
4432 * a word that contains more than just this character, and only
4433 * when we started with a capital sigma. */
4434 if (uv == UNICODE_GREEK_SMALL_LETTER_SIGMA &&
4435 s > send - len && /* Makes sure not the first letter */
4436 utf8_to_uvchr(s, 0) == UNICODE_GREEK_CAPITAL_LETTER_SIGMA
4439 /* We use the algorithm in:
4440 * http://www.unicode.org/versions/Unicode5.0.0/ch03.pdf (C
4441 * is a CAPITAL SIGMA): If C is preceded by a sequence
4442 * consisting of a cased letter and a case-ignorable
4443 * sequence, and C is not followed by a sequence consisting
4444 * of a case ignorable sequence and then a cased letter,
4445 * then when lowercasing C, C becomes a final sigma */
4447 /* To determine if this is the end of a word, need to peek
4448 * ahead. Look at the next character */
4449 const U8 *peek = s + u;
4451 /* Skip any case ignorable characters */
4452 while (peek < send && is_utf8_case_ignorable(peek)) {
4453 peek += UTF8SKIP(peek);
4456 /* If we reached the end of the string without finding any
4457 * non-case ignorable characters, or if the next such one
4458 * is not-cased, then we have met the conditions for it
4459 * being a final sigma with regards to peek ahead, and so
4460 * must do peek behind for the remaining conditions. (We
4461 * know there is stuff behind to look at since we tested
4462 * above that this isn't the first letter) */
4463 if (peek >= send || ! is_utf8_cased(peek)) {
4464 peek = utf8_hop(s, -1);
4466 /* Here are at the beginning of the first character
4467 * before the original upper case sigma. Keep backing
4468 * up, skipping any case ignorable characters */
4469 while (is_utf8_case_ignorable(peek)) {
4470 peek = utf8_hop(peek, -1);
4473 /* Here peek points to the first byte of the closest
4474 * non-case-ignorable character before the capital
4475 * sigma. If it is cased, then by the Unicode
4476 * algorithm, we should use a small final sigma instead
4477 * of what we have */
4478 if (is_utf8_cased(peek)) {
4479 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf,
4480 UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA);
4484 else { /* Not a context sensitive mapping */
4485 #endif /* End of commented out context sensitive */
4486 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4488 /* If the eventually required minimum size outgrows
4489 * the available space, we need to grow. */
4490 const UV o = d - (U8*)SvPVX_const(dest);
4492 /* If someone lowercases one million U+0130s we
4493 * SvGROW() one million times. Or we could try
4494 * guessing how much to allocate without allocating too
4495 * much. Such is life. Another option would be to
4496 * grow an extra byte or two more each time we need to
4497 * grow, which would cut down the million to 500K, with
4500 d = (U8*)SvPVX(dest) + o;
4502 #ifdef CONTEXT_DEPENDENT_CASING
4505 /* Copy the newly lowercased letter to the output buffer we're
4507 Copy(tmpbuf, d, ulen, U8);
4510 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4513 } /* End of looping through the source string */
4516 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4517 } else { /* Not utf8 */
4519 const U8 *const send = s + len;
4521 /* Use locale casing if in locale; regular style if not treating
4522 * latin1 as having case; otherwise the latin1 casing. Do the
4523 * whole thing in a tight loop, for speed, */
4524 if (IN_LOCALE_RUNTIME) {
4527 for (; s < send; d++, s++)
4528 *d = toLOWER_LC(*s);
4530 else if (! IN_UNI_8_BIT) {
4531 for (; s < send; d++, s++) {
4536 for (; s < send; d++, s++) {
4537 *d = toLOWER_LATIN1(*s);
4541 if (source != dest) {
4543 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4553 SV * const sv = TOPs;
4555 register const char *s = SvPV_const(sv,len);
4557 SvUTF8_off(TARG); /* decontaminate */
4560 SvUPGRADE(TARG, SVt_PV);
4561 SvGROW(TARG, (len * 2) + 1);
4565 if (UTF8_IS_CONTINUED(*s)) {
4566 STRLEN ulen = UTF8SKIP(s);
4590 SvCUR_set(TARG, d - SvPVX_const(TARG));
4591 (void)SvPOK_only_UTF8(TARG);
4594 sv_setpvn(TARG, s, len);
4603 dVAR; dSP; dMARK; dORIGMARK;
4604 register AV *const av = MUTABLE_AV(POPs);
4605 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4607 if (SvTYPE(av) == SVt_PVAV) {
4608 const I32 arybase = CopARYBASE_get(PL_curcop);
4609 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4610 bool can_preserve = FALSE;
4616 can_preserve = SvCANEXISTDELETE(av);
4619 if (lval && localizing) {
4622 for (svp = MARK + 1; svp <= SP; svp++) {
4623 const I32 elem = SvIV(*svp);
4627 if (max > AvMAX(av))
4631 while (++MARK <= SP) {
4633 I32 elem = SvIV(*MARK);
4634 bool preeminent = TRUE;
4638 if (localizing && can_preserve) {
4639 /* If we can determine whether the element exist,
4640 * Try to preserve the existenceness of a tied array
4641 * element by using EXISTS and DELETE if possible.
4642 * Fallback to FETCH and STORE otherwise. */
4643 preeminent = av_exists(av, elem);
4646 svp = av_fetch(av, elem, lval);
4648 if (!svp || *svp == &PL_sv_undef)
4649 DIE(aTHX_ PL_no_aelem, elem);
4652 save_aelem(av, elem, svp);
4654 SAVEADELETE(av, elem);
4657 *MARK = svp ? *svp : &PL_sv_undef;
4660 if (GIMME != G_ARRAY) {
4662 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4668 /* Smart dereferencing for keys, values and each */
4681 /* N.B.: AMG macros return sv if no overloading is found */
4682 SV *maybe_hv = AMG_CALLun(sv,to_hv);
4683 SV *maybe_av = AMG_CALLun(sv,to_av);
4684 if ( maybe_hv != sv && maybe_av != sv ) {
4685 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4686 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as %%{}",
4687 PL_op_desc[PL_op->op_type]
4692 else if ( maybe_av != sv ) {
4693 if ( SvTYPE(SvRV(sv)) == SVt_PVHV ) {
4694 /* @{} overload, but underlying reftype is HV */
4695 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4696 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as @{}",
4697 PL_op_desc[PL_op->op_type]
4703 else if ( maybe_hv != sv ) {
4704 if ( SvTYPE(SvRV(sv)) == SVt_PVAV ) {
4705 /* %{} overload, but underlying reftype is AV */
4706 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4707 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as %%{}",
4708 PL_op_desc[PL_op->op_type]
4718 if ( SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV ) {
4719 DIE(aTHX_ "Type of argument to %s must be hashref or arrayref",
4720 PL_op_desc[PL_op->op_type] );
4723 /* Delegate to correct function for op type */
4725 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4726 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4729 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4737 AV *array = MUTABLE_AV(POPs);
4738 const I32 gimme = GIMME_V;
4739 IV *iterp = Perl_av_iter_p(aTHX_ array);
4740 const IV current = (*iterp)++;
4742 if (current > av_len(array)) {
4744 if (gimme == G_SCALAR)
4751 mPUSHi(CopARYBASE_get(PL_curcop) + current);
4752 if (gimme == G_ARRAY) {
4753 SV **const element = av_fetch(array, current, 0);
4754 PUSHs(element ? *element : &PL_sv_undef);
4763 AV *array = MUTABLE_AV(POPs);
4764 const I32 gimme = GIMME_V;
4766 *Perl_av_iter_p(aTHX_ array) = 0;
4768 if (gimme == G_SCALAR) {
4770 PUSHi(av_len(array) + 1);
4772 else if (gimme == G_ARRAY) {
4773 IV n = Perl_av_len(aTHX_ array);
4774 IV i = CopARYBASE_get(PL_curcop);
4778 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4780 for (; i <= n; i++) {
4785 for (i = 0; i <= n; i++) {
4786 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4787 PUSHs(elem ? *elem : &PL_sv_undef);
4794 /* Associative arrays. */
4800 HV * hash = MUTABLE_HV(POPs);
4802 const I32 gimme = GIMME_V;
4805 /* might clobber stack_sp */
4806 entry = hv_iternext(hash);
4811 SV* const sv = hv_iterkeysv(entry);
4812 PUSHs(sv); /* won't clobber stack_sp */
4813 if (gimme == G_ARRAY) {
4816 /* might clobber stack_sp */
4817 val = hv_iterval(hash, entry);
4822 else if (gimme == G_SCALAR)
4829 S_do_delete_local(pTHX)
4833 const I32 gimme = GIMME_V;
4837 if (PL_op->op_private & OPpSLICE) {
4839 SV * const osv = POPs;
4840 const bool tied = SvRMAGICAL(osv)
4841 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4842 const bool can_preserve = SvCANEXISTDELETE(osv)
4843 || mg_find((const SV *)osv, PERL_MAGIC_env);
4844 const U32 type = SvTYPE(osv);
4845 if (type == SVt_PVHV) { /* hash element */
4846 HV * const hv = MUTABLE_HV(osv);
4847 while (++MARK <= SP) {
4848 SV * const keysv = *MARK;
4850 bool preeminent = TRUE;
4852 preeminent = hv_exists_ent(hv, keysv, 0);
4854 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4861 sv = hv_delete_ent(hv, keysv, 0, 0);
4862 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4865 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4867 *MARK = sv_mortalcopy(sv);
4873 SAVEHDELETE(hv, keysv);
4874 *MARK = &PL_sv_undef;
4878 else if (type == SVt_PVAV) { /* array element */
4879 if (PL_op->op_flags & OPf_SPECIAL) {
4880 AV * const av = MUTABLE_AV(osv);
4881 while (++MARK <= SP) {
4882 I32 idx = SvIV(*MARK);
4884 bool preeminent = TRUE;
4886 preeminent = av_exists(av, idx);
4888 SV **svp = av_fetch(av, idx, 1);
4895 sv = av_delete(av, idx, 0);
4896 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4899 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4901 *MARK = sv_mortalcopy(sv);
4907 SAVEADELETE(av, idx);
4908 *MARK = &PL_sv_undef;
4914 DIE(aTHX_ "Not a HASH reference");
4915 if (gimme == G_VOID)
4917 else if (gimme == G_SCALAR) {
4922 *++MARK = &PL_sv_undef;
4927 SV * const keysv = POPs;
4928 SV * const osv = POPs;
4929 const bool tied = SvRMAGICAL(osv)
4930 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4931 const bool can_preserve = SvCANEXISTDELETE(osv)
4932 || mg_find((const SV *)osv, PERL_MAGIC_env);
4933 const U32 type = SvTYPE(osv);
4935 if (type == SVt_PVHV) {
4936 HV * const hv = MUTABLE_HV(osv);
4937 bool preeminent = TRUE;
4939 preeminent = hv_exists_ent(hv, keysv, 0);
4941 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4948 sv = hv_delete_ent(hv, keysv, 0, 0);
4949 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4952 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4954 SV *nsv = sv_mortalcopy(sv);
4960 SAVEHDELETE(hv, keysv);
4962 else if (type == SVt_PVAV) {
4963 if (PL_op->op_flags & OPf_SPECIAL) {
4964 AV * const av = MUTABLE_AV(osv);
4965 I32 idx = SvIV(keysv);
4966 bool preeminent = TRUE;
4968 preeminent = av_exists(av, idx);
4970 SV **svp = av_fetch(av, idx, 1);
4977 sv = av_delete(av, idx, 0);
4978 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4981 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4983 SV *nsv = sv_mortalcopy(sv);
4989 SAVEADELETE(av, idx);
4992 DIE(aTHX_ "panic: avhv_delete no longer supported");
4995 DIE(aTHX_ "Not a HASH reference");
4998 if (gimme != G_VOID)
5012 if (PL_op->op_private & OPpLVAL_INTRO)
5013 return do_delete_local();
5016 discard = (gimme == G_VOID) ? G_DISCARD : 0;
5018 if (PL_op->op_private & OPpSLICE) {
5020 HV * const hv = MUTABLE_HV(POPs);
5021 const U32 hvtype = SvTYPE(hv);
5022 if (hvtype == SVt_PVHV) { /* hash element */
5023 while (++MARK <= SP) {
5024 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
5025 *MARK = sv ? sv : &PL_sv_undef;
5028 else if (hvtype == SVt_PVAV) { /* array element */
5029 if (PL_op->op_flags & OPf_SPECIAL) {
5030 while (++MARK <= SP) {
5031 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
5032 *MARK = sv ? sv : &PL_sv_undef;
5037 DIE(aTHX_ "Not a HASH reference");
5040 else if (gimme == G_SCALAR) {
5045 *++MARK = &PL_sv_undef;
5051 HV * const hv = MUTABLE_HV(POPs);
5053 if (SvTYPE(hv) == SVt_PVHV)
5054 sv = hv_delete_ent(hv, keysv, discard, 0);
5055 else if (SvTYPE(hv) == SVt_PVAV) {
5056 if (PL_op->op_flags & OPf_SPECIAL)
5057 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
5059 DIE(aTHX_ "panic: avhv_delete no longer supported");
5062 DIE(aTHX_ "Not a HASH reference");
5078 if (PL_op->op_private & OPpEXISTS_SUB) {
5080 SV * const sv = POPs;
5081 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
5084 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
5089 hv = MUTABLE_HV(POPs);
5090 if (SvTYPE(hv) == SVt_PVHV) {
5091 if (hv_exists_ent(hv, tmpsv, 0))
5094 else if (SvTYPE(hv) == SVt_PVAV) {
5095 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
5096 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
5101 DIE(aTHX_ "Not a HASH reference");
5108 dVAR; dSP; dMARK; dORIGMARK;
5109 register HV * const hv = MUTABLE_HV(POPs);
5110 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
5111 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
5112 bool can_preserve = FALSE;
5118 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
5119 can_preserve = TRUE;
5122 while (++MARK <= SP) {
5123 SV * const keysv = *MARK;
5126 bool preeminent = TRUE;
5128 if (localizing && can_preserve) {
5129 /* If we can determine whether the element exist,
5130 * try to preserve the existenceness of a tied hash
5131 * element by using EXISTS and DELETE if possible.
5132 * Fallback to FETCH and STORE otherwise. */
5133 preeminent = hv_exists_ent(hv, keysv, 0);
5136 he = hv_fetch_ent(hv, keysv, lval, 0);
5137 svp = he ? &HeVAL(he) : NULL;
5140 if (!svp || *svp == &PL_sv_undef) {
5141 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5144 if (HvNAME_get(hv) && isGV(*svp))
5145 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
5146 else if (preeminent)
5147 save_helem_flags(hv, keysv, svp,
5148 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5150 SAVEHDELETE(hv, keysv);
5153 *MARK = svp ? *svp : &PL_sv_undef;
5155 if (GIMME != G_ARRAY) {
5157 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5163 /* List operators. */
5168 if (GIMME != G_ARRAY) {
5170 *MARK = *SP; /* unwanted list, return last item */
5172 *MARK = &PL_sv_undef;
5182 SV ** const lastrelem = PL_stack_sp;
5183 SV ** const lastlelem = PL_stack_base + POPMARK;
5184 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
5185 register SV ** const firstrelem = lastlelem + 1;
5186 const I32 arybase = CopARYBASE_get(PL_curcop);
5187 I32 is_something_there = FALSE;
5189 register const I32 max = lastrelem - lastlelem;
5190 register SV **lelem;
5192 if (GIMME != G_ARRAY) {
5193 I32 ix = SvIV(*lastlelem);
5198 if (ix < 0 || ix >= max)
5199 *firstlelem = &PL_sv_undef;
5201 *firstlelem = firstrelem[ix];
5207 SP = firstlelem - 1;
5211 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
5212 I32 ix = SvIV(*lelem);
5217 if (ix < 0 || ix >= max)
5218 *lelem = &PL_sv_undef;
5220 is_something_there = TRUE;
5221 if (!(*lelem = firstrelem[ix]))
5222 *lelem = &PL_sv_undef;
5225 if (is_something_there)
5228 SP = firstlelem - 1;
5234 dVAR; dSP; dMARK; dORIGMARK;
5235 const I32 items = SP - MARK;
5236 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5237 SP = ORIGMARK; /* av_make() might realloc stack_sp */
5238 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5239 ? newRV_noinc(av) : av);
5245 dVAR; dSP; dMARK; dORIGMARK;
5246 HV* const hv = newHV();
5249 SV * const key = *++MARK;
5250 SV * const val = newSV(0);
5252 sv_setsv(val, *++MARK);
5254 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5255 (void)hv_store_ent(hv,key,val,0);
5258 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5259 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
5265 dVAR; dSP; dMARK; dORIGMARK;
5266 register AV *ary = MUTABLE_AV(*++MARK);
5270 register I32 offset;
5271 register I32 length;
5275 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5278 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5281 ENTER_with_name("call_SPLICE");
5282 call_method("SPLICE",GIMME_V);
5283 LEAVE_with_name("call_SPLICE");
5291 offset = i = SvIV(*MARK);
5293 offset += AvFILLp(ary) + 1;
5295 offset -= CopARYBASE_get(PL_curcop);
5297 DIE(aTHX_ PL_no_aelem, i);
5299 length = SvIVx(*MARK++);
5301 length += AvFILLp(ary) - offset + 1;
5307 length = AvMAX(ary) + 1; /* close enough to infinity */
5311 length = AvMAX(ary) + 1;
5313 if (offset > AvFILLp(ary) + 1) {
5314 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5315 offset = AvFILLp(ary) + 1;
5317 after = AvFILLp(ary) + 1 - (offset + length);
5318 if (after < 0) { /* not that much array */
5319 length += after; /* offset+length now in array */
5325 /* At this point, MARK .. SP-1 is our new LIST */
5328 diff = newlen - length;
5329 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5332 /* make new elements SVs now: avoid problems if they're from the array */
5333 for (dst = MARK, i = newlen; i; i--) {
5334 SV * const h = *dst;
5335 *dst++ = newSVsv(h);
5338 if (diff < 0) { /* shrinking the area */
5339 SV **tmparyval = NULL;
5341 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5342 Copy(MARK, tmparyval, newlen, SV*);
5345 MARK = ORIGMARK + 1;
5346 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5347 MEXTEND(MARK, length);
5348 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
5350 EXTEND_MORTAL(length);
5351 for (i = length, dst = MARK; i; i--) {
5352 sv_2mortal(*dst); /* free them eventualy */
5359 *MARK = AvARRAY(ary)[offset+length-1];
5362 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5363 SvREFCNT_dec(*dst++); /* free them now */
5366 AvFILLp(ary) += diff;
5368 /* pull up or down? */
5370 if (offset < after) { /* easier to pull up */
5371 if (offset) { /* esp. if nothing to pull */
5372 src = &AvARRAY(ary)[offset-1];
5373 dst = src - diff; /* diff is negative */
5374 for (i = offset; i > 0; i--) /* can't trust Copy */
5378 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5382 if (after) { /* anything to pull down? */
5383 src = AvARRAY(ary) + offset + length;
5384 dst = src + diff; /* diff is negative */
5385 Move(src, dst, after, SV*);
5387 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5388 /* avoid later double free */
5392 dst[--i] = &PL_sv_undef;
5395 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5396 Safefree(tmparyval);
5399 else { /* no, expanding (or same) */
5400 SV** tmparyval = NULL;
5402 Newx(tmparyval, length, SV*); /* so remember deletion */
5403 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5406 if (diff > 0) { /* expanding */
5407 /* push up or down? */
5408 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5412 Move(src, dst, offset, SV*);
5414 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5416 AvFILLp(ary) += diff;
5419 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5420 av_extend(ary, AvFILLp(ary) + diff);
5421 AvFILLp(ary) += diff;
5424 dst = AvARRAY(ary) + AvFILLp(ary);
5426 for (i = after; i; i--) {
5434 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5437 MARK = ORIGMARK + 1;
5438 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5440 Copy(tmparyval, MARK, length, SV*);
5442 EXTEND_MORTAL(length);
5443 for (i = length, dst = MARK; i; i--) {
5444 sv_2mortal(*dst); /* free them eventualy */
5451 else if (length--) {
5452 *MARK = tmparyval[length];
5455 while (length-- > 0)
5456 SvREFCNT_dec(tmparyval[length]);
5460 *MARK = &PL_sv_undef;
5461 Safefree(tmparyval);
5465 mg_set(MUTABLE_SV(ary));
5473 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5474 register AV * const ary = MUTABLE_AV(*++MARK);
5475 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5478 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5481 ENTER_with_name("call_PUSH");
5482 call_method("PUSH",G_SCALAR|G_DISCARD);
5483 LEAVE_with_name("call_PUSH");
5487 PL_delaymagic = DM_DELAY;
5488 for (++MARK; MARK <= SP; MARK++) {
5489 SV * const sv = newSV(0);
5491 sv_setsv(sv, *MARK);
5492 av_store(ary, AvFILLp(ary)+1, sv);
5494 if (PL_delaymagic & DM_ARRAY_ISA)
5495 mg_set(MUTABLE_SV(ary));
5500 if (OP_GIMME(PL_op, 0) != G_VOID) {
5501 PUSHi( AvFILL(ary) + 1 );
5510 AV * const av = PL_op->op_flags & OPf_SPECIAL
5511 ? MUTABLE_AV(GvAV(PL_defgv)) : MUTABLE_AV(POPs);
5512 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5516 (void)sv_2mortal(sv);
5523 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5524 register AV *ary = MUTABLE_AV(*++MARK);
5525 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5528 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5531 ENTER_with_name("call_UNSHIFT");
5532 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5533 LEAVE_with_name("call_UNSHIFT");
5538 av_unshift(ary, SP - MARK);
5540 SV * const sv = newSVsv(*++MARK);
5541 (void)av_store(ary, i++, sv);
5545 if (OP_GIMME(PL_op, 0) != G_VOID) {
5546 PUSHi( AvFILL(ary) + 1 );
5555 if (GIMME == G_ARRAY) {
5556 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5560 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5561 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5562 av = MUTABLE_AV((*SP));
5563 /* In-place reversing only happens in void context for the array
5564 * assignment. We don't need to push anything on the stack. */
5567 if (SvMAGICAL(av)) {
5569 register SV *tmp = sv_newmortal();
5570 /* For SvCANEXISTDELETE */
5573 bool can_preserve = SvCANEXISTDELETE(av);
5575 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5576 register SV *begin, *end;
5579 if (!av_exists(av, i)) {
5580 if (av_exists(av, j)) {
5581 register SV *sv = av_delete(av, j, 0);
5582 begin = *av_fetch(av, i, TRUE);
5583 sv_setsv_mg(begin, sv);
5587 else if (!av_exists(av, j)) {
5588 register SV *sv = av_delete(av, i, 0);
5589 end = *av_fetch(av, j, TRUE);
5590 sv_setsv_mg(end, sv);
5595 begin = *av_fetch(av, i, TRUE);
5596 end = *av_fetch(av, j, TRUE);
5597 sv_setsv(tmp, begin);
5598 sv_setsv_mg(begin, end);
5599 sv_setsv_mg(end, tmp);
5603 SV **begin = AvARRAY(av);
5606 SV **end = begin + AvFILLp(av);
5608 while (begin < end) {
5609 register SV * const tmp = *begin;
5620 register SV * const tmp = *MARK;
5624 /* safe as long as stack cannot get extended in the above */
5630 register char *down;
5635 SvUTF8_off(TARG); /* decontaminate */
5637 do_join(TARG, &PL_sv_no, MARK, SP);
5639 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5640 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5641 report_uninit(TARG);
5644 up = SvPV_force(TARG, len);
5646 if (DO_UTF8(TARG)) { /* first reverse each character */
5647 U8* s = (U8*)SvPVX(TARG);
5648 const U8* send = (U8*)(s + len);
5650 if (UTF8_IS_INVARIANT(*s)) {
5655 if (!utf8_to_uvchr(s, 0))
5659 down = (char*)(s - 1);
5660 /* reverse this character */
5664 *down-- = (char)tmp;
5670 down = SvPVX(TARG) + len - 1;
5674 *down-- = (char)tmp;
5676 (void)SvPOK_only_UTF8(TARG);
5688 register IV limit = POPi; /* note, negative is forever */
5689 SV * const sv = POPs;
5691 register const char *s = SvPV_const(sv, len);
5692 const bool do_utf8 = DO_UTF8(sv);
5693 const char *strend = s + len;
5695 register REGEXP *rx;
5697 register const char *m;
5699 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5700 I32 maxiters = slen + 10;
5701 I32 trailing_empty = 0;
5703 const I32 origlimit = limit;
5706 const I32 gimme = GIMME_V;
5708 const I32 oldsave = PL_savestack_ix;
5709 U32 make_mortal = SVs_TEMP;
5714 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5719 DIE(aTHX_ "panic: pp_split");
5722 TAINT_IF((RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) &&
5723 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5725 RX_MATCH_UTF8_set(rx, do_utf8);
5728 if (pm->op_pmreplrootu.op_pmtargetoff) {
5729 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5732 if (pm->op_pmreplrootu.op_pmtargetgv) {
5733 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5738 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5744 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5746 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5753 for (i = AvFILLp(ary); i >= 0; i--)
5754 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5756 /* temporarily switch stacks */
5757 SAVESWITCHSTACK(PL_curstack, ary);
5761 base = SP - PL_stack_base;
5763 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5765 while (*s == ' ' || is_utf8_space((U8*)s))
5768 else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5769 while (isSPACE_LC(*s))
5777 if (RX_EXTFLAGS(rx) & PMf_MULTILINE) {
5781 gimme_scalar = gimme == G_SCALAR && !ary;
5784 limit = maxiters + 2;
5785 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5788 /* this one uses 'm' and is a negative test */
5790 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5791 const int t = UTF8SKIP(m);
5792 /* is_utf8_space returns FALSE for malform utf8 */
5798 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5799 while (m < strend && !isSPACE_LC(*m))
5802 while (m < strend && !isSPACE(*m))
5815 dstr = newSVpvn_flags(s, m-s,
5816 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5820 /* skip the whitespace found last */
5822 s = m + UTF8SKIP(m);
5826 /* this one uses 's' and is a positive test */
5828 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5830 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5831 while (s < strend && isSPACE_LC(*s))
5834 while (s < strend && isSPACE(*s))
5839 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5841 for (m = s; m < strend && *m != '\n'; m++)
5854 dstr = newSVpvn_flags(s, m-s,
5855 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5861 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5863 Pre-extend the stack, either the number of bytes or
5864 characters in the string or a limited amount, triggered by:
5866 my ($x, $y) = split //, $str;
5870 if (!gimme_scalar) {
5871 const U32 items = limit - 1;
5880 /* keep track of how many bytes we skip over */
5890 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5903 dstr = newSVpvn(s, 1);
5919 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5920 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5921 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5922 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5923 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5924 SV * const csv = CALLREG_INTUIT_STRING(rx);
5926 len = RX_MINLENRET(rx);
5927 if (len == 1 && !RX_UTF8(rx) && !tail) {
5928 const char c = *SvPV_nolen_const(csv);
5930 for (m = s; m < strend && *m != c; m++)
5941 dstr = newSVpvn_flags(s, m-s,
5942 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5945 /* The rx->minlen is in characters but we want to step
5946 * s ahead by bytes. */
5948 s = (char*)utf8_hop((U8*)m, len);
5950 s = m + len; /* Fake \n at the end */
5954 while (s < strend && --limit &&
5955 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5956 csv, multiline ? FBMrf_MULTILINE : 0)) )
5965 dstr = newSVpvn_flags(s, m-s,
5966 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5969 /* The rx->minlen is in characters but we want to step
5970 * s ahead by bytes. */
5972 s = (char*)utf8_hop((U8*)m, len);
5974 s = m + len; /* Fake \n at the end */
5979 maxiters += slen * RX_NPARENS(rx);
5980 while (s < strend && --limit)
5984 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5987 if (rex_return == 0)
5989 TAINT_IF(RX_MATCH_TAINTED(rx));
5990 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5993 orig = RX_SUBBEG(rx);
5995 strend = s + (strend - m);
5997 m = RX_OFFS(rx)[0].start + orig;
6006 dstr = newSVpvn_flags(s, m-s,
6007 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6010 if (RX_NPARENS(rx)) {
6012 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
6013 s = RX_OFFS(rx)[i].start + orig;
6014 m = RX_OFFS(rx)[i].end + orig;
6016 /* japhy (07/27/01) -- the (m && s) test doesn't catch
6017 parens that didn't match -- they should be set to
6018 undef, not the empty string */
6026 if (m >= orig && s >= orig) {
6027 dstr = newSVpvn_flags(s, m-s,
6028 (do_utf8 ? SVf_UTF8 : 0)
6032 dstr = &PL_sv_undef; /* undef, not "" */
6038 s = RX_OFFS(rx)[0].end + orig;
6042 if (!gimme_scalar) {
6043 iters = (SP - PL_stack_base) - base;
6045 if (iters > maxiters)
6046 DIE(aTHX_ "Split loop");
6048 /* keep field after final delim? */
6049 if (s < strend || (iters && origlimit)) {
6050 if (!gimme_scalar) {
6051 const STRLEN l = strend - s;
6052 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6057 else if (!origlimit) {
6059 iters -= trailing_empty;
6061 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
6062 if (TOPs && !make_mortal)
6064 *SP-- = &PL_sv_undef;
6071 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
6075 if (SvSMAGICAL(ary)) {
6077 mg_set(MUTABLE_SV(ary));
6080 if (gimme == G_ARRAY) {
6082 Copy(AvARRAY(ary), SP + 1, iters, SV*);
6089 ENTER_with_name("call_PUSH");
6090 call_method("PUSH",G_SCALAR|G_DISCARD);
6091 LEAVE_with_name("call_PUSH");
6093 if (gimme == G_ARRAY) {
6095 /* EXTEND should not be needed - we just popped them */
6097 for (i=0; i < iters; i++) {
6098 SV **svp = av_fetch(ary, i, FALSE);
6099 PUSHs((svp) ? *svp : &PL_sv_undef);
6106 if (gimme == G_ARRAY)
6118 SV *const sv = PAD_SVl(PL_op->op_targ);
6120 if (SvPADSTALE(sv)) {
6123 RETURNOP(cLOGOP->op_other);
6125 RETURNOP(cLOGOP->op_next);
6134 assert(SvTYPE(retsv) != SVt_PVCV);
6136 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV) {
6137 retsv = refto(retsv);
6144 PP(unimplemented_op)
6147 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", OP_NAME(PL_op),
6155 HV * const hv = (HV*)POPs;
6157 if (SvRMAGICAL(hv)) {
6158 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
6160 XPUSHs(magic_scalarpack(hv, mg));
6165 XPUSHs(boolSV(HvKEYS(hv) != 0));
6171 * c-indentation-style: bsd
6173 * indent-tabs-mode: t
6176 * ex: set ts=8 sts=4 sw=4 noet: