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) {
124 RETURNOP(Perl_do_kv(aTHX));
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. */
795 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
801 PERL_ARGS_ASSERT_DO_CHOMP;
803 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
805 if (SvTYPE(sv) == SVt_PVAV) {
807 AV *const av = MUTABLE_AV(sv);
808 const I32 max = AvFILL(av);
810 for (i = 0; i <= max; i++) {
811 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
812 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
813 do_chomp(retval, sv, chomping);
817 else if (SvTYPE(sv) == SVt_PVHV) {
818 HV* const hv = MUTABLE_HV(sv);
820 (void)hv_iterinit(hv);
821 while ((entry = hv_iternext(hv)))
822 do_chomp(retval, hv_iterval(hv,entry), chomping);
825 else if (SvREADONLY(sv)) {
827 /* SV is copy-on-write */
828 sv_force_normal_flags(sv, 0);
831 Perl_croak_no_modify(aTHX);
836 /* XXX, here sv is utf8-ized as a side-effect!
837 If encoding.pm is used properly, almost string-generating
838 operations, including literal strings, chr(), input data, etc.
839 should have been utf8-ized already, right?
841 sv_recode_to_utf8(sv, PL_encoding);
847 char *temp_buffer = NULL;
856 while (len && s[-1] == '\n') {
863 STRLEN rslen, rs_charlen;
864 const char *rsptr = SvPV_const(PL_rs, rslen);
866 rs_charlen = SvUTF8(PL_rs)
870 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
871 /* Assumption is that rs is shorter than the scalar. */
873 /* RS is utf8, scalar is 8 bit. */
875 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
878 /* Cannot downgrade, therefore cannot possibly match
880 assert (temp_buffer == rsptr);
886 else if (PL_encoding) {
887 /* RS is 8 bit, encoding.pm is used.
888 * Do not recode PL_rs as a side-effect. */
889 svrecode = newSVpvn(rsptr, rslen);
890 sv_recode_to_utf8(svrecode, PL_encoding);
891 rsptr = SvPV_const(svrecode, rslen);
892 rs_charlen = sv_len_utf8(svrecode);
895 /* RS is 8 bit, scalar is utf8. */
896 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
910 if (memNE(s, rsptr, rslen))
912 SvIVX(retval) += rs_charlen;
915 s = SvPV_force_nolen(sv);
923 SvREFCNT_dec(svrecode);
925 Safefree(temp_buffer);
927 if (len && !SvPOK(sv))
928 s = SvPV_force_nomg(sv, len);
931 char * const send = s + len;
932 char * const start = s;
934 while (s > start && UTF8_IS_CONTINUATION(*s))
936 if (is_utf8_string((U8*)s, send - s)) {
937 sv_setpvn(retval, s, send - s);
939 SvCUR_set(sv, s - start);
945 sv_setpvs(retval, "");
949 sv_setpvn(retval, s, 1);
956 sv_setpvs(retval, "");
964 const bool chomping = PL_op->op_type == OP_SCHOMP;
968 do_chomp(TARG, TOPs, chomping);
975 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
976 const bool chomping = PL_op->op_type == OP_CHOMP;
981 do_chomp(TARG, *++MARK, chomping);
992 if (!PL_op->op_private) {
1001 SV_CHECK_THINKFIRST_COW_DROP(sv);
1003 switch (SvTYPE(sv)) {
1007 av_undef(MUTABLE_AV(sv));
1010 hv_undef(MUTABLE_HV(sv));
1013 if (cv_const_sv((const CV *)sv))
1014 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
1015 CvANON((const CV *)sv) ? "(anonymous)"
1016 : GvENAME(CvGV((const CV *)sv)));
1020 /* let user-undef'd sub keep its identity */
1021 GV* const gv = CvGV((const CV *)sv);
1022 cv_undef(MUTABLE_CV(sv));
1023 CvGV_set(MUTABLE_CV(sv), gv);
1028 SvSetMagicSV(sv, &PL_sv_undef);
1031 else if (isGV_with_GP(sv)) {
1035 /* undef *Pkg::meth_name ... */
1037 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1038 && HvENAME_get(stash);
1040 if((stash = GvHV((const GV *)sv))) {
1041 if(HvENAME_get(stash))
1042 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1046 gp_free(MUTABLE_GV(sv));
1048 GvGP(sv) = gp_ref(gp);
1049 GvSV(sv) = newSV(0);
1050 GvLINE(sv) = CopLINE(PL_curcop);
1051 GvEGV(sv) = MUTABLE_GV(sv);
1055 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1057 /* undef *Foo::ISA */
1058 if( strEQ(GvNAME((const GV *)sv), "ISA")
1059 && (stash = GvSTASH((const GV *)sv))
1060 && (method_changed || HvENAME(stash)) )
1061 mro_isa_changed_in(stash);
1062 else if(method_changed)
1063 mro_method_changed_in(
1064 GvSTASH((const GV *)sv)
1071 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1086 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
1087 Perl_croak_no_modify(aTHX);
1088 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1089 && SvIVX(TOPs) != IV_MIN)
1091 SvIV_set(TOPs, SvIVX(TOPs) - 1);
1092 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1103 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
1104 Perl_croak_no_modify(aTHX);
1106 TARG = sv_newmortal();
1107 sv_setsv(TARG, TOPs);
1108 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1109 && SvIVX(TOPs) != IV_MAX)
1111 SvIV_set(TOPs, SvIVX(TOPs) + 1);
1112 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1117 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1127 if (SvTYPE(TOPs) >= SVt_PVAV || isGV_with_GP(TOPs))
1128 Perl_croak_no_modify(aTHX);
1130 TARG = sv_newmortal();
1131 sv_setsv(TARG, TOPs);
1132 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1133 && SvIVX(TOPs) != IV_MIN)
1135 SvIV_set(TOPs, SvIVX(TOPs) - 1);
1136 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1145 /* Ordinary operators. */
1149 dVAR; dSP; dATARGET; SV *svl, *svr;
1150 #ifdef PERL_PRESERVE_IVUV
1153 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1156 #ifdef PERL_PRESERVE_IVUV
1157 /* For integer to integer power, we do the calculation by hand wherever
1158 we're sure it is safe; otherwise we call pow() and try to convert to
1159 integer afterwards. */
1161 SvIV_please_nomg(svr);
1163 SvIV_please_nomg(svl);
1172 const IV iv = SvIVX(svr);
1176 goto float_it; /* Can't do negative powers this way. */
1180 baseuok = SvUOK(svl);
1182 baseuv = SvUVX(svl);
1184 const IV iv = SvIVX(svl);
1187 baseuok = TRUE; /* effectively it's a UV now */
1189 baseuv = -iv; /* abs, baseuok == false records sign */
1192 /* now we have integer ** positive integer. */
1195 /* foo & (foo - 1) is zero only for a power of 2. */
1196 if (!(baseuv & (baseuv - 1))) {
1197 /* We are raising power-of-2 to a positive integer.
1198 The logic here will work for any base (even non-integer
1199 bases) but it can be less accurate than
1200 pow (base,power) or exp (power * log (base)) when the
1201 intermediate values start to spill out of the mantissa.
1202 With powers of 2 we know this can't happen.
1203 And powers of 2 are the favourite thing for perl
1204 programmers to notice ** not doing what they mean. */
1206 NV base = baseuok ? baseuv : -(NV)baseuv;
1211 while (power >>= 1) {
1219 SvIV_please_nomg(svr);
1222 register unsigned int highbit = 8 * sizeof(UV);
1223 register unsigned int diff = 8 * sizeof(UV);
1224 while (diff >>= 1) {
1226 if (baseuv >> highbit) {
1230 /* we now have baseuv < 2 ** highbit */
1231 if (power * highbit <= 8 * sizeof(UV)) {
1232 /* result will definitely fit in UV, so use UV math
1233 on same algorithm as above */
1234 register UV result = 1;
1235 register UV base = baseuv;
1236 const bool odd_power = cBOOL(power & 1);
1240 while (power >>= 1) {
1247 if (baseuok || !odd_power)
1248 /* answer is positive */
1250 else if (result <= (UV)IV_MAX)
1251 /* answer negative, fits in IV */
1252 SETi( -(IV)result );
1253 else if (result == (UV)IV_MIN)
1254 /* 2's complement assumption: special case IV_MIN */
1257 /* answer negative, doesn't fit */
1258 SETn( -(NV)result );
1268 NV right = SvNV_nomg(svr);
1269 NV left = SvNV_nomg(svl);
1272 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1274 We are building perl with long double support and are on an AIX OS
1275 afflicted with a powl() function that wrongly returns NaNQ for any
1276 negative base. This was reported to IBM as PMR #23047-379 on
1277 03/06/2006. The problem exists in at least the following versions
1278 of AIX and the libm fileset, and no doubt others as well:
1280 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1281 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1282 AIX 5.2.0 bos.adt.libm 5.2.0.85
1284 So, until IBM fixes powl(), we provide the following workaround to
1285 handle the problem ourselves. Our logic is as follows: for
1286 negative bases (left), we use fmod(right, 2) to check if the
1287 exponent is an odd or even integer:
1289 - if odd, powl(left, right) == -powl(-left, right)
1290 - if even, powl(left, right) == powl(-left, right)
1292 If the exponent is not an integer, the result is rightly NaNQ, so
1293 we just return that (as NV_NAN).
1297 NV mod2 = Perl_fmod( right, 2.0 );
1298 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1299 SETn( -Perl_pow( -left, right) );
1300 } else if (mod2 == 0.0) { /* even integer */
1301 SETn( Perl_pow( -left, right) );
1302 } else { /* fractional power */
1306 SETn( Perl_pow( left, right) );
1309 SETn( Perl_pow( left, right) );
1310 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1312 #ifdef PERL_PRESERVE_IVUV
1314 SvIV_please_nomg(svr);
1322 dVAR; dSP; dATARGET; SV *svl, *svr;
1323 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1326 #ifdef PERL_PRESERVE_IVUV
1327 SvIV_please_nomg(svr);
1329 /* Unless the left argument is integer in range we are going to have to
1330 use NV maths. Hence only attempt to coerce the right argument if
1331 we know the left is integer. */
1332 /* Left operand is defined, so is it IV? */
1333 SvIV_please_nomg(svl);
1335 bool auvok = SvUOK(svl);
1336 bool buvok = SvUOK(svr);
1337 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1338 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1347 const IV aiv = SvIVX(svl);
1350 auvok = TRUE; /* effectively it's a UV now */
1352 alow = -aiv; /* abs, auvok == false records sign */
1358 const IV biv = SvIVX(svr);
1361 buvok = TRUE; /* effectively it's a UV now */
1363 blow = -biv; /* abs, buvok == false records sign */
1367 /* If this does sign extension on unsigned it's time for plan B */
1368 ahigh = alow >> (4 * sizeof (UV));
1370 bhigh = blow >> (4 * sizeof (UV));
1372 if (ahigh && bhigh) {
1374 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1375 which is overflow. Drop to NVs below. */
1376 } else if (!ahigh && !bhigh) {
1377 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1378 so the unsigned multiply cannot overflow. */
1379 const UV product = alow * blow;
1380 if (auvok == buvok) {
1381 /* -ve * -ve or +ve * +ve gives a +ve result. */
1385 } else if (product <= (UV)IV_MIN) {
1386 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1387 /* -ve result, which could overflow an IV */
1389 SETi( -(IV)product );
1391 } /* else drop to NVs below. */
1393 /* One operand is large, 1 small */
1396 /* swap the operands */
1398 bhigh = blow; /* bhigh now the temp var for the swap */
1402 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1403 multiplies can't overflow. shift can, add can, -ve can. */
1404 product_middle = ahigh * blow;
1405 if (!(product_middle & topmask)) {
1406 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1408 product_middle <<= (4 * sizeof (UV));
1409 product_low = alow * blow;
1411 /* as for pp_add, UV + something mustn't get smaller.
1412 IIRC ANSI mandates this wrapping *behaviour* for
1413 unsigned whatever the actual representation*/
1414 product_low += product_middle;
1415 if (product_low >= product_middle) {
1416 /* didn't overflow */
1417 if (auvok == buvok) {
1418 /* -ve * -ve or +ve * +ve gives a +ve result. */
1420 SETu( product_low );
1422 } else if (product_low <= (UV)IV_MIN) {
1423 /* 2s complement assumption again */
1424 /* -ve result, which could overflow an IV */
1426 SETi( -(IV)product_low );
1428 } /* else drop to NVs below. */
1430 } /* product_middle too large */
1431 } /* ahigh && bhigh */
1436 NV right = SvNV_nomg(svr);
1437 NV left = SvNV_nomg(svl);
1439 SETn( left * right );
1446 dVAR; dSP; dATARGET; SV *svl, *svr;
1447 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1450 /* Only try to do UV divide first
1451 if ((SLOPPYDIVIDE is true) or
1452 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1454 The assumption is that it is better to use floating point divide
1455 whenever possible, only doing integer divide first if we can't be sure.
1456 If NV_PRESERVES_UV is true then we know at compile time that no UV
1457 can be too large to preserve, so don't need to compile the code to
1458 test the size of UVs. */
1461 # define PERL_TRY_UV_DIVIDE
1462 /* ensure that 20./5. == 4. */
1464 # ifdef PERL_PRESERVE_IVUV
1465 # ifndef NV_PRESERVES_UV
1466 # define PERL_TRY_UV_DIVIDE
1471 #ifdef PERL_TRY_UV_DIVIDE
1472 SvIV_please_nomg(svr);
1474 SvIV_please_nomg(svl);
1476 bool left_non_neg = SvUOK(svl);
1477 bool right_non_neg = SvUOK(svr);
1481 if (right_non_neg) {
1485 const IV biv = SvIVX(svr);
1488 right_non_neg = TRUE; /* effectively it's a UV now */
1494 /* historically undef()/0 gives a "Use of uninitialized value"
1495 warning before dieing, hence this test goes here.
1496 If it were immediately before the second SvIV_please, then
1497 DIE() would be invoked before left was even inspected, so
1498 no inspection would give no warning. */
1500 DIE(aTHX_ "Illegal division by zero");
1506 const IV aiv = SvIVX(svl);
1509 left_non_neg = TRUE; /* effectively it's a UV now */
1518 /* For sloppy divide we always attempt integer division. */
1520 /* Otherwise we only attempt it if either or both operands
1521 would not be preserved by an NV. If both fit in NVs
1522 we fall through to the NV divide code below. However,
1523 as left >= right to ensure integer result here, we know that
1524 we can skip the test on the right operand - right big
1525 enough not to be preserved can't get here unless left is
1528 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1531 /* Integer division can't overflow, but it can be imprecise. */
1532 const UV result = left / right;
1533 if (result * right == left) {
1534 SP--; /* result is valid */
1535 if (left_non_neg == right_non_neg) {
1536 /* signs identical, result is positive. */
1540 /* 2s complement assumption */
1541 if (result <= (UV)IV_MIN)
1542 SETi( -(IV)result );
1544 /* It's exact but too negative for IV. */
1545 SETn( -(NV)result );
1548 } /* tried integer divide but it was not an integer result */
1549 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1550 } /* left wasn't SvIOK */
1551 } /* right wasn't SvIOK */
1552 #endif /* PERL_TRY_UV_DIVIDE */
1554 NV right = SvNV_nomg(svr);
1555 NV left = SvNV_nomg(svl);
1556 (void)POPs;(void)POPs;
1557 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1558 if (! Perl_isnan(right) && right == 0.0)
1562 DIE(aTHX_ "Illegal division by zero");
1563 PUSHn( left / right );
1570 dVAR; dSP; dATARGET;
1571 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1575 bool left_neg = FALSE;
1576 bool right_neg = FALSE;
1577 bool use_double = FALSE;
1578 bool dright_valid = FALSE;
1581 SV * const svr = TOPs;
1582 SV * const svl = TOPm1s;
1583 SvIV_please_nomg(svr);
1585 right_neg = !SvUOK(svr);
1589 const IV biv = SvIVX(svr);
1592 right_neg = FALSE; /* effectively it's a UV now */
1599 dright = SvNV_nomg(svr);
1600 right_neg = dright < 0;
1603 if (dright < UV_MAX_P1) {
1604 right = U_V(dright);
1605 dright_valid = TRUE; /* In case we need to use double below. */
1611 /* At this point use_double is only true if right is out of range for
1612 a UV. In range NV has been rounded down to nearest UV and
1613 use_double false. */
1614 SvIV_please_nomg(svl);
1615 if (!use_double && SvIOK(svl)) {
1617 left_neg = !SvUOK(svl);
1621 const IV aiv = SvIVX(svl);
1624 left_neg = FALSE; /* effectively it's a UV now */
1632 dleft = SvNV_nomg(svl);
1633 left_neg = dleft < 0;
1637 /* This should be exactly the 5.6 behaviour - if left and right are
1638 both in range for UV then use U_V() rather than floor. */
1640 if (dleft < UV_MAX_P1) {
1641 /* right was in range, so is dleft, so use UVs not double.
1645 /* left is out of range for UV, right was in range, so promote
1646 right (back) to double. */
1648 /* The +0.5 is used in 5.6 even though it is not strictly
1649 consistent with the implicit +0 floor in the U_V()
1650 inside the #if 1. */
1651 dleft = Perl_floor(dleft + 0.5);
1654 dright = Perl_floor(dright + 0.5);
1665 DIE(aTHX_ "Illegal modulus zero");
1667 dans = Perl_fmod(dleft, dright);
1668 if ((left_neg != right_neg) && dans)
1669 dans = dright - dans;
1672 sv_setnv(TARG, dans);
1678 DIE(aTHX_ "Illegal modulus zero");
1681 if ((left_neg != right_neg) && ans)
1684 /* XXX may warn: unary minus operator applied to unsigned type */
1685 /* could change -foo to be (~foo)+1 instead */
1686 if (ans <= ~((UV)IV_MAX)+1)
1687 sv_setiv(TARG, ~ans+1);
1689 sv_setnv(TARG, -(NV)ans);
1692 sv_setuv(TARG, ans);
1701 dVAR; dSP; dATARGET;
1705 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1706 /* TODO: think of some way of doing list-repeat overloading ??? */
1711 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1717 const UV uv = SvUV_nomg(sv);
1719 count = IV_MAX; /* The best we can do? */
1723 const IV iv = SvIV_nomg(sv);
1730 else if (SvNOKp(sv)) {
1731 const NV nv = SvNV_nomg(sv);
1738 count = SvIV_nomg(sv);
1740 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1742 static const char oom_list_extend[] = "Out of memory during list extend";
1743 const I32 items = SP - MARK;
1744 const I32 max = items * count;
1746 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1747 /* Did the max computation overflow? */
1748 if (items > 0 && max > 0 && (max < items || max < count))
1749 Perl_croak(aTHX_ oom_list_extend);
1754 /* This code was intended to fix 20010809.028:
1757 for (($x =~ /./g) x 2) {
1758 print chop; # "abcdabcd" expected as output.
1761 * but that change (#11635) broke this code:
1763 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1765 * I can't think of a better fix that doesn't introduce
1766 * an efficiency hit by copying the SVs. The stack isn't
1767 * refcounted, and mortalisation obviously doesn't
1768 * Do The Right Thing when the stack has more than
1769 * one pointer to the same mortal value.
1773 *SP = sv_2mortal(newSVsv(*SP));
1783 repeatcpy((char*)(MARK + items), (char*)MARK,
1784 items * sizeof(const SV *), count - 1);
1787 else if (count <= 0)
1790 else { /* Note: mark already snarfed by pp_list */
1791 SV * const tmpstr = POPs;
1794 static const char oom_string_extend[] =
1795 "Out of memory during string extend";
1798 sv_setsv_nomg(TARG, tmpstr);
1799 SvPV_force_nomg(TARG, len);
1800 isutf = DO_UTF8(TARG);
1805 const STRLEN max = (UV)count * len;
1806 if (len > MEM_SIZE_MAX / count)
1807 Perl_croak(aTHX_ oom_string_extend);
1808 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1809 SvGROW(TARG, max + 1);
1810 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1811 SvCUR_set(TARG, SvCUR(TARG) * count);
1813 *SvEND(TARG) = '\0';
1816 (void)SvPOK_only_UTF8(TARG);
1818 (void)SvPOK_only(TARG);
1820 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1821 /* The parser saw this as a list repeat, and there
1822 are probably several items on the stack. But we're
1823 in scalar context, and there's no pp_list to save us
1824 now. So drop the rest of the items -- robin@kitsite.com
1836 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1837 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1840 useleft = USE_LEFT(svl);
1841 #ifdef PERL_PRESERVE_IVUV
1842 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1843 "bad things" happen if you rely on signed integers wrapping. */
1844 SvIV_please_nomg(svr);
1846 /* Unless the left argument is integer in range we are going to have to
1847 use NV maths. Hence only attempt to coerce the right argument if
1848 we know the left is integer. */
1849 register UV auv = 0;
1855 a_valid = auvok = 1;
1856 /* left operand is undef, treat as zero. */
1858 /* Left operand is defined, so is it IV? */
1859 SvIV_please_nomg(svl);
1861 if ((auvok = SvUOK(svl)))
1864 register const IV aiv = SvIVX(svl);
1867 auvok = 1; /* Now acting as a sign flag. */
1868 } else { /* 2s complement assumption for IV_MIN */
1876 bool result_good = 0;
1879 bool buvok = SvUOK(svr);
1884 register const IV biv = SvIVX(svr);
1891 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1892 else "IV" now, independent of how it came in.
1893 if a, b represents positive, A, B negative, a maps to -A etc
1898 all UV maths. negate result if A negative.
1899 subtract if signs same, add if signs differ. */
1901 if (auvok ^ buvok) {
1910 /* Must get smaller */
1915 if (result <= buv) {
1916 /* result really should be -(auv-buv). as its negation
1917 of true value, need to swap our result flag */
1929 if (result <= (UV)IV_MIN)
1930 SETi( -(IV)result );
1932 /* result valid, but out of range for IV. */
1933 SETn( -(NV)result );
1937 } /* Overflow, drop through to NVs. */
1942 NV value = SvNV_nomg(svr);
1946 /* left operand is undef, treat as zero - value */
1950 SETn( SvNV_nomg(svl) - value );
1957 dVAR; dSP; dATARGET; SV *svl, *svr;
1958 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1962 const IV shift = SvIV_nomg(svr);
1963 if (PL_op->op_private & HINT_INTEGER) {
1964 const IV i = SvIV_nomg(svl);
1968 const UV u = SvUV_nomg(svl);
1977 dVAR; dSP; dATARGET; SV *svl, *svr;
1978 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1982 const IV shift = SvIV_nomg(svr);
1983 if (PL_op->op_private & HINT_INTEGER) {
1984 const IV i = SvIV_nomg(svl);
1988 const UV u = SvUV_nomg(svl);
1998 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1999 #ifdef PERL_PRESERVE_IVUV
2000 SvIV_please_nomg(TOPs);
2002 SvIV_please_nomg(TOPm1s);
2003 if (SvIOK(TOPm1s)) {
2004 bool auvok = SvUOK(TOPm1s);
2005 bool buvok = SvUOK(TOPs);
2007 if (!auvok && !buvok) { /* ## IV < IV ## */
2008 const IV aiv = SvIVX(TOPm1s);
2009 const IV biv = SvIVX(TOPs);
2012 SETs(boolSV(aiv < biv));
2015 if (auvok && buvok) { /* ## UV < UV ## */
2016 const UV auv = SvUVX(TOPm1s);
2017 const UV buv = SvUVX(TOPs);
2020 SETs(boolSV(auv < buv));
2023 if (auvok) { /* ## UV < IV ## */
2025 const IV biv = SvIVX(TOPs);
2028 /* As (a) is a UV, it's >=0, so it cannot be < */
2033 SETs(boolSV(auv < (UV)biv));
2036 { /* ## IV < UV ## */
2037 const IV aiv = SvIVX(TOPm1s);
2041 /* As (b) is a UV, it's >=0, so it must be < */
2048 SETs(boolSV((UV)aiv < buv));
2054 #ifndef NV_PRESERVES_UV
2055 #ifdef PERL_PRESERVE_IVUV
2058 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2060 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
2065 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2067 if (Perl_isnan(left) || Perl_isnan(right))
2069 SETs(boolSV(left < right));
2072 SETs(boolSV(SvNV_nomg(TOPs) < value));
2081 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
2082 #ifdef PERL_PRESERVE_IVUV
2083 SvIV_please_nomg(TOPs);
2085 SvIV_please_nomg(TOPm1s);
2086 if (SvIOK(TOPm1s)) {
2087 bool auvok = SvUOK(TOPm1s);
2088 bool buvok = SvUOK(TOPs);
2090 if (!auvok && !buvok) { /* ## IV > IV ## */
2091 const IV aiv = SvIVX(TOPm1s);
2092 const IV biv = SvIVX(TOPs);
2095 SETs(boolSV(aiv > biv));
2098 if (auvok && buvok) { /* ## UV > UV ## */
2099 const UV auv = SvUVX(TOPm1s);
2100 const UV buv = SvUVX(TOPs);
2103 SETs(boolSV(auv > buv));
2106 if (auvok) { /* ## UV > IV ## */
2108 const IV biv = SvIVX(TOPs);
2112 /* As (a) is a UV, it's >=0, so it must be > */
2117 SETs(boolSV(auv > (UV)biv));
2120 { /* ## IV > UV ## */
2121 const IV aiv = SvIVX(TOPm1s);
2125 /* As (b) is a UV, it's >=0, so it cannot be > */
2132 SETs(boolSV((UV)aiv > buv));
2138 #ifndef NV_PRESERVES_UV
2139 #ifdef PERL_PRESERVE_IVUV
2142 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2144 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
2149 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2151 if (Perl_isnan(left) || Perl_isnan(right))
2153 SETs(boolSV(left > right));
2156 SETs(boolSV(SvNV_nomg(TOPs) > value));
2165 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
2166 #ifdef PERL_PRESERVE_IVUV
2167 SvIV_please_nomg(TOPs);
2169 SvIV_please_nomg(TOPm1s);
2170 if (SvIOK(TOPm1s)) {
2171 bool auvok = SvUOK(TOPm1s);
2172 bool buvok = SvUOK(TOPs);
2174 if (!auvok && !buvok) { /* ## IV <= IV ## */
2175 const IV aiv = SvIVX(TOPm1s);
2176 const IV biv = SvIVX(TOPs);
2179 SETs(boolSV(aiv <= biv));
2182 if (auvok && buvok) { /* ## UV <= UV ## */
2183 UV auv = SvUVX(TOPm1s);
2184 UV buv = SvUVX(TOPs);
2187 SETs(boolSV(auv <= buv));
2190 if (auvok) { /* ## UV <= IV ## */
2192 const IV biv = SvIVX(TOPs);
2196 /* As (a) is a UV, it's >=0, so a cannot be <= */
2201 SETs(boolSV(auv <= (UV)biv));
2204 { /* ## IV <= UV ## */
2205 const IV aiv = SvIVX(TOPm1s);
2209 /* As (b) is a UV, it's >=0, so a must be <= */
2216 SETs(boolSV((UV)aiv <= buv));
2222 #ifndef NV_PRESERVES_UV
2223 #ifdef PERL_PRESERVE_IVUV
2226 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2228 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
2233 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2235 if (Perl_isnan(left) || Perl_isnan(right))
2237 SETs(boolSV(left <= right));
2240 SETs(boolSV(SvNV_nomg(TOPs) <= value));
2249 tryAMAGICbin_MG(ge_amg,AMGf_set|AMGf_numeric);
2250 #ifdef PERL_PRESERVE_IVUV
2251 SvIV_please_nomg(TOPs);
2253 SvIV_please_nomg(TOPm1s);
2254 if (SvIOK(TOPm1s)) {
2255 bool auvok = SvUOK(TOPm1s);
2256 bool buvok = SvUOK(TOPs);
2258 if (!auvok && !buvok) { /* ## IV >= IV ## */
2259 const IV aiv = SvIVX(TOPm1s);
2260 const IV biv = SvIVX(TOPs);
2263 SETs(boolSV(aiv >= biv));
2266 if (auvok && buvok) { /* ## UV >= UV ## */
2267 const UV auv = SvUVX(TOPm1s);
2268 const UV buv = SvUVX(TOPs);
2271 SETs(boolSV(auv >= buv));
2274 if (auvok) { /* ## UV >= IV ## */
2276 const IV biv = SvIVX(TOPs);
2280 /* As (a) is a UV, it's >=0, so it must be >= */
2285 SETs(boolSV(auv >= (UV)biv));
2288 { /* ## IV >= UV ## */
2289 const IV aiv = SvIVX(TOPm1s);
2293 /* As (b) is a UV, it's >=0, so a cannot be >= */
2300 SETs(boolSV((UV)aiv >= buv));
2306 #ifndef NV_PRESERVES_UV
2307 #ifdef PERL_PRESERVE_IVUV
2310 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2312 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
2317 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2319 if (Perl_isnan(left) || Perl_isnan(right))
2321 SETs(boolSV(left >= right));
2324 SETs(boolSV(SvNV_nomg(TOPs) >= value));
2333 tryAMAGICbin_MG(ne_amg,AMGf_set|AMGf_numeric);
2334 #ifndef NV_PRESERVES_UV
2335 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2337 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
2341 #ifdef PERL_PRESERVE_IVUV
2342 SvIV_please_nomg(TOPs);
2344 SvIV_please_nomg(TOPm1s);
2345 if (SvIOK(TOPm1s)) {
2346 const bool auvok = SvUOK(TOPm1s);
2347 const bool buvok = SvUOK(TOPs);
2349 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
2350 /* Casting IV to UV before comparison isn't going to matter
2351 on 2s complement. On 1s complement or sign&magnitude
2352 (if we have any of them) it could make negative zero
2353 differ from normal zero. As I understand it. (Need to
2354 check - is negative zero implementation defined behaviour
2356 const UV buv = SvUVX(POPs);
2357 const UV auv = SvUVX(TOPs);
2359 SETs(boolSV(auv != buv));
2362 { /* ## Mixed IV,UV ## */
2366 /* != is commutative so swap if needed (save code) */
2368 /* swap. top of stack (b) is the iv */
2372 /* As (a) is a UV, it's >0, so it cannot be == */
2381 /* As (b) is a UV, it's >0, so it cannot be == */
2385 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
2387 SETs(boolSV((UV)iv != uv));
2394 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2396 if (Perl_isnan(left) || Perl_isnan(right))
2398 SETs(boolSV(left != right));
2401 SETs(boolSV(SvNV_nomg(TOPs) != value));
2410 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2411 #ifndef NV_PRESERVES_UV
2412 if (SvROK(TOPs) && !SvAMAGIC(TOPs) && SvROK(TOPm1s) && !SvAMAGIC(TOPm1s)) {
2413 const UV right = PTR2UV(SvRV(POPs));
2414 const UV left = PTR2UV(SvRV(TOPs));
2415 SETi((left > right) - (left < right));
2419 #ifdef PERL_PRESERVE_IVUV
2420 /* Fortunately it seems NaN isn't IOK */
2421 SvIV_please_nomg(TOPs);
2423 SvIV_please_nomg(TOPm1s);
2424 if (SvIOK(TOPm1s)) {
2425 const bool leftuvok = SvUOK(TOPm1s);
2426 const bool rightuvok = SvUOK(TOPs);
2428 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
2429 const IV leftiv = SvIVX(TOPm1s);
2430 const IV rightiv = SvIVX(TOPs);
2432 if (leftiv > rightiv)
2434 else if (leftiv < rightiv)
2438 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
2439 const UV leftuv = SvUVX(TOPm1s);
2440 const UV rightuv = SvUVX(TOPs);
2442 if (leftuv > rightuv)
2444 else if (leftuv < rightuv)
2448 } else if (leftuvok) { /* ## UV <=> IV ## */
2449 const IV rightiv = SvIVX(TOPs);
2451 /* As (a) is a UV, it's >=0, so it cannot be < */
2454 const UV leftuv = SvUVX(TOPm1s);
2455 if (leftuv > (UV)rightiv) {
2457 } else if (leftuv < (UV)rightiv) {
2463 } else { /* ## IV <=> UV ## */
2464 const IV leftiv = SvIVX(TOPm1s);
2466 /* As (b) is a UV, it's >=0, so it must be < */
2469 const UV rightuv = SvUVX(TOPs);
2470 if ((UV)leftiv > rightuv) {
2472 } else if ((UV)leftiv < rightuv) {
2490 if (Perl_isnan(left) || Perl_isnan(right)) {
2494 value = (left > right) - (left < right);
2498 else if (left < right)
2500 else if (left > right)
2516 int amg_type = sle_amg;
2520 switch (PL_op->op_type) {
2539 tryAMAGICbin_MG(amg_type, AMGf_set);
2542 const int cmp = (IN_LOCALE_RUNTIME
2543 ? sv_cmp_locale_flags(left, right, 0)
2544 : sv_cmp_flags(left, right, 0));
2545 SETs(boolSV(cmp * multiplier < rhs));
2553 tryAMAGICbin_MG(seq_amg, AMGf_set);
2556 SETs(boolSV(sv_eq_flags(left, right, 0)));
2564 tryAMAGICbin_MG(sne_amg, AMGf_set);
2567 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2575 tryAMAGICbin_MG(scmp_amg, 0);
2578 const int cmp = (IN_LOCALE_RUNTIME
2579 ? sv_cmp_locale_flags(left, right, 0)
2580 : sv_cmp_flags(left, right, 0));
2588 dVAR; dSP; dATARGET;
2589 tryAMAGICbin_MG(band_amg, AMGf_assign);
2592 if (SvNIOKp(left) || SvNIOKp(right)) {
2593 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2594 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2595 if (PL_op->op_private & HINT_INTEGER) {
2596 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2600 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2603 if (left_ro_nonnum) SvNIOK_off(left);
2604 if (right_ro_nonnum) SvNIOK_off(right);
2607 do_vop(PL_op->op_type, TARG, left, right);
2616 dVAR; dSP; dATARGET;
2617 const int op_type = PL_op->op_type;
2619 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2622 if (SvNIOKp(left) || SvNIOKp(right)) {
2623 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2624 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2625 if (PL_op->op_private & HINT_INTEGER) {
2626 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2627 const IV r = SvIV_nomg(right);
2628 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2632 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2633 const UV r = SvUV_nomg(right);
2634 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2637 if (left_ro_nonnum) SvNIOK_off(left);
2638 if (right_ro_nonnum) SvNIOK_off(right);
2641 do_vop(op_type, TARG, left, right);
2651 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2653 SV * const sv = TOPs;
2654 const int flags = SvFLAGS(sv);
2656 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
2660 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2661 /* It's publicly an integer, or privately an integer-not-float */
2664 if (SvIVX(sv) == IV_MIN) {
2665 /* 2s complement assumption. */
2666 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2669 else if (SvUVX(sv) <= IV_MAX) {
2674 else if (SvIVX(sv) != IV_MIN) {
2678 #ifdef PERL_PRESERVE_IVUV
2686 SETn(-SvNV_nomg(sv));
2687 else if (SvPOKp(sv)) {
2689 const char * const s = SvPV_nomg_const(sv, len);
2690 if (isIDFIRST(*s)) {
2691 sv_setpvs(TARG, "-");
2694 else if (*s == '+' || *s == '-') {
2695 sv_setsv_nomg(TARG, sv);
2696 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2698 else if (DO_UTF8(sv)) {
2699 SvIV_please_nomg(sv);
2701 goto oops_its_an_int;
2703 sv_setnv(TARG, -SvNV_nomg(sv));
2705 sv_setpvs(TARG, "-");
2710 SvIV_please_nomg(sv);
2712 goto oops_its_an_int;
2713 sv_setnv(TARG, -SvNV_nomg(sv));
2718 SETn(-SvNV_nomg(sv));
2726 tryAMAGICun_MG(not_amg, AMGf_set);
2727 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2734 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2738 if (PL_op->op_private & HINT_INTEGER) {
2739 const IV i = ~SvIV_nomg(sv);
2743 const UV u = ~SvUV_nomg(sv);
2752 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2753 sv_setsv_nomg(TARG, sv);
2754 tmps = (U8*)SvPV_force_nomg(TARG, len);
2757 /* Calculate exact length, let's not estimate. */
2762 U8 * const send = tmps + len;
2763 U8 * const origtmps = tmps;
2764 const UV utf8flags = UTF8_ALLOW_ANYUV;
2766 while (tmps < send) {
2767 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2769 targlen += UNISKIP(~c);
2775 /* Now rewind strings and write them. */
2782 Newx(result, targlen + 1, U8);
2784 while (tmps < send) {
2785 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2787 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2790 sv_usepvn_flags(TARG, (char*)result, targlen,
2791 SV_HAS_TRAILING_NUL);
2798 Newx(result, nchar + 1, U8);
2800 while (tmps < send) {
2801 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2806 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2814 register long *tmpl;
2815 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2818 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2823 for ( ; anum > 0; anum--, tmps++)
2831 /* integer versions of some of the above */
2835 dVAR; dSP; dATARGET;
2836 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2839 SETi( left * right );
2847 dVAR; dSP; dATARGET;
2848 tryAMAGICbin_MG(div_amg, AMGf_assign);
2851 IV value = SvIV_nomg(right);
2853 DIE(aTHX_ "Illegal division by zero");
2854 num = SvIV_nomg(left);
2856 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2860 value = num / value;
2866 #if defined(__GLIBC__) && IVSIZE == 8
2873 /* This is the vanilla old i_modulo. */
2874 dVAR; dSP; dATARGET;
2875 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2879 DIE(aTHX_ "Illegal modulus zero");
2880 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2884 SETi( left % right );
2889 #if defined(__GLIBC__) && IVSIZE == 8
2894 /* This is the i_modulo with the workaround for the _moddi3 bug
2895 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2896 * See below for pp_i_modulo. */
2897 dVAR; dSP; dATARGET;
2898 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2902 DIE(aTHX_ "Illegal modulus zero");
2903 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2907 SETi( left % PERL_ABS(right) );
2914 dVAR; dSP; dATARGET;
2915 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2919 DIE(aTHX_ "Illegal modulus zero");
2920 /* The assumption is to use hereafter the old vanilla version... */
2922 PL_ppaddr[OP_I_MODULO] =
2924 /* .. but if we have glibc, we might have a buggy _moddi3
2925 * (at least glicb 2.2.5 is known to have this bug), in other
2926 * words our integer modulus with negative quad as the second
2927 * argument might be broken. Test for this and re-patch the
2928 * opcode dispatch table if that is the case, remembering to
2929 * also apply the workaround so that this first round works
2930 * right, too. See [perl #9402] for more information. */
2934 /* Cannot do this check with inlined IV constants since
2935 * that seems to work correctly even with the buggy glibc. */
2937 /* Yikes, we have the bug.
2938 * Patch in the workaround version. */
2940 PL_ppaddr[OP_I_MODULO] =
2941 &Perl_pp_i_modulo_1;
2942 /* Make certain we work right this time, too. */
2943 right = PERL_ABS(right);
2946 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2950 SETi( left % right );
2958 dVAR; dSP; dATARGET;
2959 tryAMAGICbin_MG(add_amg, AMGf_assign);
2961 dPOPTOPiirl_ul_nomg;
2962 SETi( left + right );
2969 dVAR; dSP; dATARGET;
2970 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2972 dPOPTOPiirl_ul_nomg;
2973 SETi( left - right );
2981 tryAMAGICbin_MG(lt_amg, AMGf_set);
2984 SETs(boolSV(left < right));
2992 tryAMAGICbin_MG(gt_amg, AMGf_set);
2995 SETs(boolSV(left > right));
3003 tryAMAGICbin_MG(le_amg, AMGf_set);
3006 SETs(boolSV(left <= right));
3014 tryAMAGICbin_MG(ge_amg, AMGf_set);
3017 SETs(boolSV(left >= right));
3025 tryAMAGICbin_MG(eq_amg, AMGf_set);
3028 SETs(boolSV(left == right));
3036 tryAMAGICbin_MG(ne_amg, AMGf_set);
3039 SETs(boolSV(left != right));
3047 tryAMAGICbin_MG(ncmp_amg, 0);
3054 else if (left < right)
3066 tryAMAGICun_MG(neg_amg, 0);
3068 SV * const sv = TOPs;
3069 IV const i = SvIV_nomg(sv);
3075 /* High falutin' math. */
3080 tryAMAGICbin_MG(atan2_amg, 0);
3083 SETn(Perl_atan2(left, right));
3091 int amg_type = sin_amg;
3092 const char *neg_report = NULL;
3093 NV (*func)(NV) = Perl_sin;
3094 const int op_type = PL_op->op_type;
3111 amg_type = sqrt_amg;
3113 neg_report = "sqrt";
3118 tryAMAGICun_MG(amg_type, 0);
3120 SV * const arg = POPs;
3121 const NV value = SvNV_nomg(arg);
3123 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
3124 SET_NUMERIC_STANDARD();
3125 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
3128 XPUSHn(func(value));
3133 /* Support Configure command-line overrides for rand() functions.
3134 After 5.005, perhaps we should replace this by Configure support
3135 for drand48(), random(), or rand(). For 5.005, though, maintain
3136 compatibility by calling rand() but allow the user to override it.
3137 See INSTALL for details. --Andy Dougherty 15 July 1998
3139 /* Now it's after 5.005, and Configure supports drand48() and random(),
3140 in addition to rand(). So the overrides should not be needed any more.
3141 --Jarkko Hietaniemi 27 September 1998
3144 #ifndef HAS_DRAND48_PROTO
3145 extern double drand48 (void);
3158 if (!PL_srand_called) {
3159 (void)seedDrand01((Rand_seed_t)seed());
3160 PL_srand_called = TRUE;
3170 const UV anum = (MAXARG < 1) ? seed() : POPu;
3171 (void)seedDrand01((Rand_seed_t)anum);
3172 PL_srand_called = TRUE;
3176 /* Historically srand always returned true. We can avoid breaking
3178 sv_setpvs(TARG, "0 but true");
3187 tryAMAGICun_MG(int_amg, AMGf_numeric);
3189 SV * const sv = TOPs;
3190 const IV iv = SvIV_nomg(sv);
3191 /* XXX it's arguable that compiler casting to IV might be subtly
3192 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
3193 else preferring IV has introduced a subtle behaviour change bug. OTOH
3194 relying on floating point to be accurate is a bug. */
3199 else if (SvIOK(sv)) {
3201 SETu(SvUV_nomg(sv));
3206 const NV value = SvNV_nomg(sv);
3208 if (value < (NV)UV_MAX + 0.5) {
3211 SETn(Perl_floor(value));
3215 if (value > (NV)IV_MIN - 0.5) {
3218 SETn(Perl_ceil(value));
3229 tryAMAGICun_MG(abs_amg, AMGf_numeric);
3231 SV * const sv = TOPs;
3232 /* This will cache the NV value if string isn't actually integer */
3233 const IV iv = SvIV_nomg(sv);
3238 else if (SvIOK(sv)) {
3239 /* IVX is precise */
3241 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
3249 /* 2s complement assumption. Also, not really needed as
3250 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
3256 const NV value = SvNV_nomg(sv);
3270 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
3274 SV* const sv = POPs;
3276 tmps = (SvPV_const(sv, len));
3278 /* If Unicode, try to downgrade
3279 * If not possible, croak. */
3280 SV* const tsv = sv_2mortal(newSVsv(sv));
3283 sv_utf8_downgrade(tsv, FALSE);
3284 tmps = SvPV_const(tsv, len);
3286 if (PL_op->op_type == OP_HEX)
3289 while (*tmps && len && isSPACE(*tmps))
3293 if (*tmps == 'x' || *tmps == 'X') {
3295 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
3297 else if (*tmps == 'b' || *tmps == 'B')
3298 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
3300 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
3302 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
3316 SV * const sv = TOPs;
3318 if (SvGAMAGIC(sv)) {
3319 /* For an overloaded or magic scalar, we can't know in advance if
3320 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
3321 it likes to cache the length. Maybe that should be a documented
3326 = sv_2pv_flags(sv, &len,
3327 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
3330 sv_setsv(TARG, &PL_sv_undef);
3333 else if (DO_UTF8(sv)) {
3334 SETi(utf8_length((U8*)p, (U8*)p + len));
3338 } else if (SvOK(sv)) {
3339 /* Neither magic nor overloaded. */
3341 SETi(sv_len_utf8(sv));
3345 sv_setsv_nomg(TARG, &PL_sv_undef);
3365 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3367 const IV arybase = CopARYBASE_get(PL_curcop);
3369 const char *repl = NULL;
3371 const int num_args = PL_op->op_private & 7;
3372 bool repl_need_utf8_upgrade = FALSE;
3373 bool repl_is_utf8 = FALSE;
3378 repl = SvPV_const(repl_sv, repl_len);
3379 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
3382 len_iv = SvIV(len_sv);
3383 len_is_uv = SvIOK_UV(len_sv);
3386 pos1_iv = SvIV(pos_sv);
3387 pos1_is_uv = SvIOK_UV(pos_sv);
3393 sv_utf8_upgrade(sv);
3395 else if (DO_UTF8(sv))
3396 repl_need_utf8_upgrade = TRUE;
3398 tmps = SvPV_const(sv, curlen);
3400 utf8_curlen = sv_len_utf8(sv);
3401 if (utf8_curlen == curlen)
3404 curlen = utf8_curlen;
3409 if ( (pos1_is_uv && arybase < 0) || (pos1_iv >= arybase) ) { /* pos >= $[ */
3410 UV pos1_uv = pos1_iv-arybase;
3411 /* Overflow can occur when $[ < 0 */
3412 if (arybase < 0 && pos1_uv < (UV)pos1_iv)
3417 else if (pos1_is_uv ? (UV)pos1_iv > 0 : pos1_iv > 0) {
3418 goto bound_fail; /* $[=3; substr($_,2,...) */
3420 else { /* pos < $[ */
3421 if (pos1_iv == 0) { /* $[=1; substr($_,0,...) */
3426 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
3431 if (pos1_is_uv || pos1_iv > 0) {
3432 if ((UV)pos1_iv > curlen)
3437 if (!len_is_uv && len_iv < 0) {
3438 pos2_iv = curlen + len_iv;
3440 pos2_is_uv = curlen-1 > ~(UV)len_iv;
3443 } else { /* len_iv >= 0 */
3444 if (!pos1_is_uv && pos1_iv < 0) {
3445 pos2_iv = pos1_iv + len_iv;
3446 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
3448 if ((UV)len_iv > curlen-(UV)pos1_iv)
3451 pos2_iv = pos1_iv+len_iv;
3461 if (!pos2_is_uv && pos2_iv < 0) {
3462 if (!pos1_is_uv && pos1_iv < 0)
3466 else if (!pos1_is_uv && pos1_iv < 0)
3469 if ((UV)pos2_iv < (UV)pos1_iv)
3471 if ((UV)pos2_iv > curlen)
3475 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3476 const STRLEN pos = (STRLEN)( (UV)pos1_iv );
3477 const STRLEN len = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3478 STRLEN byte_len = len;
3479 STRLEN byte_pos = utf8_curlen
3480 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3482 if (lvalue && !repl) {
3485 if (!SvGMAGICAL(sv)) {
3487 SvPV_force_nolen(sv);
3488 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3489 "Attempt to use reference as lvalue in substr");
3491 if (isGV_with_GP(sv))
3492 SvPV_force_nolen(sv);
3493 else if (SvOK(sv)) /* is it defined ? */
3494 (void)SvPOK_only_UTF8(sv);
3496 sv_setpvs(sv, ""); /* avoid lexical reincarnation */
3499 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3500 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3502 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3503 LvTARGOFF(ret) = pos;
3504 LvTARGLEN(ret) = len;
3507 PUSHs(ret); /* avoid SvSETMAGIC here */
3511 SvTAINTED_off(TARG); /* decontaminate */
3512 SvUTF8_off(TARG); /* decontaminate */
3515 sv_setpvn(TARG, tmps, byte_len);
3516 #ifdef USE_LOCALE_COLLATE
3517 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3523 SV* repl_sv_copy = NULL;
3525 if (repl_need_utf8_upgrade) {
3526 repl_sv_copy = newSVsv(repl_sv);
3527 sv_utf8_upgrade(repl_sv_copy);
3528 repl = SvPV_const(repl_sv_copy, repl_len);
3529 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3533 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3536 SvREFCNT_dec(repl_sv_copy);
3540 PUSHs(TARG); /* avoid SvSETMAGIC here */
3545 Perl_croak(aTHX_ "substr outside of string");
3546 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3553 register const IV size = POPi;
3554 register const IV offset = POPi;
3555 register SV * const src = POPs;
3556 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3559 if (lvalue) { /* it's an lvalue! */
3560 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3561 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3563 LvTARG(ret) = SvREFCNT_inc_simple(src);
3564 LvTARGOFF(ret) = offset;
3565 LvTARGLEN(ret) = size;
3569 SvTAINTED_off(TARG); /* decontaminate */
3573 sv_setuv(ret, do_vecget(src, offset, size));
3589 const char *little_p;
3590 const I32 arybase = CopARYBASE_get(PL_curcop);
3593 const bool is_index = PL_op->op_type == OP_INDEX;
3596 /* arybase is in characters, like offset, so combine prior to the
3597 UTF-8 to bytes calculation. */
3598 offset = POPi - arybase;
3602 big_p = SvPV_const(big, biglen);
3603 little_p = SvPV_const(little, llen);
3605 big_utf8 = DO_UTF8(big);
3606 little_utf8 = DO_UTF8(little);
3607 if (big_utf8 ^ little_utf8) {
3608 /* One needs to be upgraded. */
3609 if (little_utf8 && !PL_encoding) {
3610 /* Well, maybe instead we might be able to downgrade the small
3612 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3615 /* If the large string is ISO-8859-1, and it's not possible to
3616 convert the small string to ISO-8859-1, then there is no
3617 way that it could be found anywhere by index. */
3622 /* At this point, pv is a malloc()ed string. So donate it to temp
3623 to ensure it will get free()d */
3624 little = temp = newSV(0);
3625 sv_usepvn(temp, pv, llen);
3626 little_p = SvPVX(little);
3629 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3632 sv_recode_to_utf8(temp, PL_encoding);
3634 sv_utf8_upgrade(temp);
3639 big_p = SvPV_const(big, biglen);
3642 little_p = SvPV_const(little, llen);
3646 if (SvGAMAGIC(big)) {
3647 /* Life just becomes a lot easier if I use a temporary here.
3648 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3649 will trigger magic and overloading again, as will fbm_instr()
3651 big = newSVpvn_flags(big_p, biglen,
3652 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3655 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3656 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3657 warn on undef, and we've already triggered a warning with the
3658 SvPV_const some lines above. We can't remove that, as we need to
3659 call some SvPV to trigger overloading early and find out if the
3661 This is all getting to messy. The API isn't quite clean enough,
3662 because data access has side effects.
3664 little = newSVpvn_flags(little_p, llen,
3665 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3666 little_p = SvPVX(little);
3670 offset = is_index ? 0 : biglen;
3672 if (big_utf8 && offset > 0)
3673 sv_pos_u2b(big, &offset, 0);
3679 else if (offset > (I32)biglen)
3681 if (!(little_p = is_index
3682 ? fbm_instr((unsigned char*)big_p + offset,
3683 (unsigned char*)big_p + biglen, little, 0)
3684 : rninstr(big_p, big_p + offset,
3685 little_p, little_p + llen)))
3688 retval = little_p - big_p;
3689 if (retval > 0 && big_utf8)
3690 sv_pos_b2u(big, &retval);
3694 PUSHi(retval + arybase);
3700 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3701 if (SvTAINTED(MARK[1]))
3702 TAINT_PROPER("sprintf");
3703 SvTAINTED_off(TARG);
3704 do_sprintf(TARG, SP-MARK, MARK+1);
3705 TAINT_IF(SvTAINTED(TARG));
3717 const U8 *s = (U8*)SvPV_const(argsv, len);
3719 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3720 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3721 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3725 XPUSHu(DO_UTF8(argsv) ?
3726 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3738 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3740 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3742 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3744 (void) POPs; /* Ignore the argument value. */
3745 value = UNICODE_REPLACEMENT;
3751 SvUPGRADE(TARG,SVt_PV);
3753 if (value > 255 && !IN_BYTES) {
3754 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3755 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3756 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3758 (void)SvPOK_only(TARG);
3767 *tmps++ = (char)value;
3769 (void)SvPOK_only(TARG);
3771 if (PL_encoding && !IN_BYTES) {
3772 sv_recode_to_utf8(TARG, PL_encoding);
3774 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3775 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3779 *tmps++ = (char)value;
3795 const char *tmps = SvPV_const(left, len);
3797 if (DO_UTF8(left)) {
3798 /* If Unicode, try to downgrade.
3799 * If not possible, croak.
3800 * Yes, we made this up. */
3801 SV* const tsv = sv_2mortal(newSVsv(left));
3804 sv_utf8_downgrade(tsv, FALSE);
3805 tmps = SvPV_const(tsv, len);
3807 # ifdef USE_ITHREADS
3809 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3810 /* This should be threadsafe because in ithreads there is only
3811 * one thread per interpreter. If this would not be true,
3812 * we would need a mutex to protect this malloc. */
3813 PL_reentrant_buffer->_crypt_struct_buffer =
3814 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3815 #if defined(__GLIBC__) || defined(__EMX__)
3816 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3817 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3818 /* work around glibc-2.2.5 bug */
3819 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3823 # endif /* HAS_CRYPT_R */
3824 # endif /* USE_ITHREADS */
3826 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3828 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3834 "The crypt() function is unimplemented due to excessive paranoia.");
3838 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3839 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3841 /* Both the characters below can be stored in two UTF-8 bytes. In UTF-8 the max
3842 * character that 2 bytes can hold is U+07FF, and in UTF-EBCDIC it is U+03FF.
3843 * See http://www.unicode.org/unicode/reports/tr16 */
3844 #define LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS 0x0178 /* Also is title case */
3845 #define GREEK_CAPITAL_LETTER_MU 0x039C /* Upper and title case of MICRON */
3847 /* Below are several macros that generate code */
3848 /* Generates code to store a unicode codepoint c that is known to occupy
3849 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1. */
3850 #define STORE_UNI_TO_UTF8_TWO_BYTE(p, c) \
3852 *(p) = UTF8_TWO_BYTE_HI(c); \
3853 *((p)+1) = UTF8_TWO_BYTE_LO(c); \
3856 /* Like STORE_UNI_TO_UTF8_TWO_BYTE, but advances p to point to the next
3857 * available byte after the two bytes */
3858 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3860 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3861 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3864 /* Generates code to store the upper case of latin1 character l which is known
3865 * to have its upper case be non-latin1 into the two bytes p and p+1. There
3866 * are only two characters that fit this description, and this macro knows
3867 * about them, and that the upper case values fit into two UTF-8 or UTF-EBCDIC
3869 #define STORE_NON_LATIN1_UC(p, l) \
3871 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3872 STORE_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3873 } else { /* Must be the following letter */ \
3874 STORE_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3878 /* Like STORE_NON_LATIN1_UC, but advances p to point to the next available byte
3879 * after the character stored */
3880 #define CAT_NON_LATIN1_UC(p, l) \
3882 if ((l) == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3883 CAT_UNI_TO_UTF8_TWO_BYTE((p), LATIN_CAPITAL_LETTER_Y_WITH_DIAERESIS); \
3885 CAT_UNI_TO_UTF8_TWO_BYTE((p), GREEK_CAPITAL_LETTER_MU); \
3889 /* Generates code to add the two UTF-8 bytes (probably u) that are the upper
3890 * case of l into p and p+1. u must be the result of toUPPER_LATIN1_MOD(l),
3891 * and must require two bytes to store it. Advances p to point to the next
3892 * available position */
3893 #define CAT_TWO_BYTE_UNI_UPPER_MOD(p, l, u) \
3895 if ((u) != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) { \
3896 CAT_UNI_TO_UTF8_TWO_BYTE((p), (u)); /* not special, just save it */ \
3897 } else if (l == LATIN_SMALL_LETTER_SHARP_S) { \
3898 *(p)++ = 'S'; *(p)++ = 'S'; /* upper case is 'SS' */ \
3899 } else {/* else is one of the other two special cases */ \
3900 CAT_NON_LATIN1_UC((p), (l)); \
3906 /* Actually is both lcfirst() and ucfirst(). Only the first character
3907 * changes. This means that possibly we can change in-place, ie., just
3908 * take the source and change that one character and store it back, but not
3909 * if read-only etc, or if the length changes */
3914 STRLEN slen; /* slen is the byte length of the whole SV. */
3917 bool inplace; /* ? Convert first char only, in-place */
3918 bool doing_utf8 = FALSE; /* ? using utf8 */
3919 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3920 const int op_type = PL_op->op_type;
3923 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3924 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3925 * stored as UTF-8 at s. */
3926 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3927 * lowercased) character stored in tmpbuf. May be either
3928 * UTF-8 or not, but in either case is the number of bytes */
3932 s = (const U8*)SvPV_nomg_const(source, slen);
3934 if (ckWARN(WARN_UNINITIALIZED))
3935 report_uninit(source);
3940 /* We may be able to get away with changing only the first character, in
3941 * place, but not if read-only, etc. Later we may discover more reasons to
3942 * not convert in-place. */
3943 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3945 /* First calculate what the changed first character should be. This affects
3946 * whether we can just swap it out, leaving the rest of the string unchanged,
3947 * or even if have to convert the dest to UTF-8 when the source isn't */
3949 if (! slen) { /* If empty */
3950 need = 1; /* still need a trailing NUL */
3952 else if (DO_UTF8(source)) { /* Is the source utf8? */
3955 /* TODO: This is #ifdefd out because it has hard-coded the standard mappings,
3956 * and doesn't allow for the user to specify their own. When code is added to
3957 * detect if there is a user-defined mapping in force here, and if so to use
3958 * that, then the code below can be compiled. The detection would be a good
3959 * thing anyway, as currently the user-defined mappings only work on utf8
3960 * strings, and thus depend on the chosen internal storage method, which is a
3962 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
3963 if (UTF8_IS_INVARIANT(*s)) {
3965 /* An invariant source character is either ASCII or, in EBCDIC, an
3966 * ASCII equivalent or a caseless C1 control. In both these cases,
3967 * the lower and upper cases of any character are also invariants
3968 * (and title case is the same as upper case). So it is safe to
3969 * use the simple case change macros which avoid the overhead of
3970 * the general functions. Note that if perl were to be extended to
3971 * do locale handling in UTF-8 strings, this wouldn't be true in,
3972 * for example, Lithuanian or Turkic. */
3973 *tmpbuf = (op_type == OP_LCFIRST) ? toLOWER(*s) : toUPPER(*s);
3977 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
3980 /* Similarly, if the source character isn't invariant but is in the
3981 * latin1 range (or EBCDIC equivalent thereof), we have the case
3982 * changes compiled into perl, and can avoid the overhead of the
3983 * general functions. In this range, the characters are stored as
3984 * two UTF-8 bytes, and it so happens that any changed-case version
3985 * is also two bytes (in both ASCIIish and EBCDIC machines). */
3989 /* Convert the two source bytes to a single Unicode code point
3990 * value, change case and save for below */
3991 chr = TWO_BYTE_UTF8_TO_UNI(*s, *(s+1));
3992 if (op_type == OP_LCFIRST) { /* lower casing is easy */
3993 U8 lower = toLOWER_LATIN1(chr);
3994 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, lower);
3996 else { /* ucfirst */
3997 U8 upper = toUPPER_LATIN1_MOD(chr);
3999 /* Most of the latin1 range characters are well-behaved. Their
4000 * title and upper cases are the same, and are also in the
4001 * latin1 range. The macro above returns their upper (hence
4002 * title) case, and all that need be done is to save the result
4003 * for below. However, several characters are problematic, and
4004 * have to be handled specially. The MOD in the macro name
4005 * above means that these tricky characters all get mapped to
4006 * the single character LATIN_SMALL_LETTER_Y_WITH_DIAERESIS.
4007 * This mapping saves some tests for the majority of the
4010 if (upper != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
4012 /* Not tricky. Just save it. */
4013 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf, upper);
4015 else if (chr == LATIN_SMALL_LETTER_SHARP_S) {
4017 /* This one is tricky because it is two characters long,
4018 * though the UTF-8 is still two bytes, so the stored
4019 * length doesn't change */
4020 *tmpbuf = 'S'; /* The UTF-8 is 'Ss' */
4021 *(tmpbuf + 1) = 's';
4025 /* The other two have their title and upper cases the same,
4026 * but are tricky because the changed-case characters
4027 * aren't in the latin1 range. They, however, do fit into
4028 * two UTF-8 bytes */
4029 STORE_NON_LATIN1_UC(tmpbuf, chr);
4034 #endif /* end of dont want to break user-defined casing */
4036 /* Here, can't short-cut the general case */
4038 utf8_to_uvchr(s, &ulen);
4039 if (op_type == OP_UCFIRST) toTITLE_utf8(s, tmpbuf, &tculen);
4040 else toLOWER_utf8(s, tmpbuf, &tculen);
4042 /* we can't do in-place if the length changes. */
4043 if (ulen != tculen) inplace = FALSE;
4044 need = slen + 1 - ulen + tculen;
4045 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4049 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
4050 * latin1 is treated as caseless. Note that a locale takes
4052 tculen = 1; /* Most characters will require one byte, but this will
4053 * need to be overridden for the tricky ones */
4056 if (op_type == OP_LCFIRST) {
4058 /* lower case the first letter: no trickiness for any character */
4059 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
4060 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
4063 else if (IN_LOCALE_RUNTIME) {
4064 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
4065 * have upper and title case different
4068 else if (! IN_UNI_8_BIT) {
4069 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
4070 * on EBCDIC machines whatever the
4071 * native function does */
4073 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
4074 *tmpbuf = toUPPER_LATIN1_MOD(*s);
4076 /* tmpbuf now has the correct title case for all latin1 characters
4077 * except for the several ones that have tricky handling. All
4078 * of these are mapped by the MOD to the letter below. */
4079 if (*tmpbuf == LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) {
4081 /* The length is going to change, with all three of these, so
4082 * can't replace just the first character */
4085 /* We use the original to distinguish between these tricky
4087 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4088 /* Two character title case 'Ss', but can remain non-UTF-8 */
4091 *(tmpbuf + 1) = 's'; /* Assert: length(tmpbuf) >= 2 */
4096 /* The other two tricky ones have their title case outside
4097 * latin1. It is the same as their upper case. */
4099 STORE_NON_LATIN1_UC(tmpbuf, *s);
4101 /* The UTF-8 and UTF-EBCDIC lengths of both these characters
4102 * and their upper cases is 2. */
4105 /* The entire result will have to be in UTF-8. Assume worst
4106 * case sizing in conversion. (all latin1 characters occupy
4107 * at most two bytes in utf8) */
4108 convert_source_to_utf8 = TRUE;
4109 need = slen * 2 + 1;
4111 } /* End of is one of the three special chars */
4112 } /* End of use Unicode (Latin1) semantics */
4113 } /* End of changing the case of the first character */
4115 /* Here, have the first character's changed case stored in tmpbuf. Ready to
4116 * generate the result */
4119 /* We can convert in place. This means we change just the first
4120 * character without disturbing the rest; no need to grow */
4122 s = d = (U8*)SvPV_force_nomg(source, slen);
4128 /* Here, we can't convert in place; we earlier calculated how much
4129 * space we will need, so grow to accommodate that */
4130 SvUPGRADE(dest, SVt_PV);
4131 d = (U8*)SvGROW(dest, need);
4132 (void)SvPOK_only(dest);
4139 if (! convert_source_to_utf8) {
4141 /* Here both source and dest are in UTF-8, but have to create
4142 * the entire output. We initialize the result to be the
4143 * title/lower cased first character, and then append the rest
4145 sv_setpvn(dest, (char*)tmpbuf, tculen);
4147 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
4151 const U8 *const send = s + slen;
4153 /* Here the dest needs to be in UTF-8, but the source isn't,
4154 * except we earlier UTF-8'd the first character of the source
4155 * into tmpbuf. First put that into dest, and then append the
4156 * rest of the source, converting it to UTF-8 as we go. */
4158 /* Assert tculen is 2 here because the only two characters that
4159 * get to this part of the code have 2-byte UTF-8 equivalents */
4161 *d++ = *(tmpbuf + 1);
4162 s++; /* We have just processed the 1st char */
4164 for (; s < send; s++) {
4165 d = uvchr_to_utf8(d, *s);
4168 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4172 else { /* in-place UTF-8. Just overwrite the first character */
4173 Copy(tmpbuf, d, tculen, U8);
4174 SvCUR_set(dest, need - 1);
4177 else { /* Neither source nor dest are in or need to be UTF-8 */
4179 if (IN_LOCALE_RUNTIME) {
4183 if (inplace) { /* in-place, only need to change the 1st char */
4186 else { /* Not in-place */
4188 /* Copy the case-changed character(s) from tmpbuf */
4189 Copy(tmpbuf, d, tculen, U8);
4190 d += tculen - 1; /* Code below expects d to point to final
4191 * character stored */
4194 else { /* empty source */
4195 /* See bug #39028: Don't taint if empty */
4199 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
4200 * the destination to retain that flag */
4204 if (!inplace) { /* Finish the rest of the string, unchanged */
4205 /* This will copy the trailing NUL */
4206 Copy(s + 1, d + 1, slen, U8);
4207 SvCUR_set(dest, need - 1);
4214 /* There's so much setup/teardown code common between uc and lc, I wonder if
4215 it would be worth merging the two, and just having a switch outside each
4216 of the three tight loops. There is less and less commonality though */
4230 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4231 && SvTEMP(source) && !DO_UTF8(source)
4232 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
4234 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
4235 * make the loop tight, so we overwrite the source with the dest before
4236 * looking at it, and we need to look at the original source
4237 * afterwards. There would also need to be code added to handle
4238 * switching to not in-place in midstream if we run into characters
4239 * that change the length.
4242 s = d = (U8*)SvPV_force_nomg(source, len);
4249 /* The old implementation would copy source into TARG at this point.
4250 This had the side effect that if source was undef, TARG was now
4251 an undefined SV with PADTMP set, and they don't warn inside
4252 sv_2pv_flags(). However, we're now getting the PV direct from
4253 source, which doesn't have PADTMP set, so it would warn. Hence the
4257 s = (const U8*)SvPV_nomg_const(source, len);
4259 if (ckWARN(WARN_UNINITIALIZED))
4260 report_uninit(source);
4266 SvUPGRADE(dest, SVt_PV);
4267 d = (U8*)SvGROW(dest, min);
4268 (void)SvPOK_only(dest);
4273 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4274 to check DO_UTF8 again here. */
4276 if (DO_UTF8(source)) {
4277 const U8 *const send = s + len;
4278 U8 tmpbuf[UTF8_MAXBYTES+1];
4280 /* All occurrences of these are to be moved to follow any other marks.
4281 * This is context-dependent. We may not be passed enough context to
4282 * move the iota subscript beyond all of them, but we do the best we can
4283 * with what we're given. The result is always better than if we
4284 * hadn't done this. And, the problem would only arise if we are
4285 * passed a character without all its combining marks, which would be
4286 * the caller's mistake. The information this is based on comes from a
4287 * comment in Unicode SpecialCasing.txt, (and the Standard's text
4288 * itself) and so can't be checked properly to see if it ever gets
4289 * revised. But the likelihood of it changing is remote */
4290 bool in_iota_subscript = FALSE;
4293 if (in_iota_subscript && ! is_utf8_mark(s)) {
4294 /* A non-mark. Time to output the iota subscript */
4295 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
4296 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
4298 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4299 in_iota_subscript = FALSE;
4303 /* See comments at the first instance in this file of this ifdef */
4304 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4306 /* If the UTF-8 character is invariant, then it is in the range
4307 * known by the standard macro; result is only one byte long */
4308 if (UTF8_IS_INVARIANT(*s)) {
4312 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4314 /* Likewise, if it fits in a byte, its case change is in our
4316 U8 orig = TWO_BYTE_UTF8_TO_UNI(*s, *s++);
4317 U8 upper = toUPPER_LATIN1_MOD(orig);
4318 CAT_TWO_BYTE_UNI_UPPER_MOD(d, orig, upper);
4326 /* Otherwise, need the general UTF-8 case. Get the changed
4327 * case value and copy it to the output buffer */
4329 const STRLEN u = UTF8SKIP(s);
4332 const UV uv = toUPPER_utf8(s, tmpbuf, &ulen);
4333 if (uv == GREEK_CAPITAL_LETTER_IOTA
4334 && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
4336 in_iota_subscript = TRUE;
4339 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4340 /* If the eventually required minimum size outgrows
4341 * the available space, we need to grow. */
4342 const UV o = d - (U8*)SvPVX_const(dest);
4344 /* If someone uppercases one million U+03B0s we
4345 * SvGROW() one million times. Or we could try
4346 * guessing how much to allocate without allocating too
4347 * much. Such is life. See corresponding comment in
4348 * lc code for another option */
4350 d = (U8*)SvPVX(dest) + o;
4352 Copy(tmpbuf, d, ulen, U8);
4358 if (in_iota_subscript) {
4359 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
4363 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4365 else { /* Not UTF-8 */
4367 const U8 *const send = s + len;
4369 /* Use locale casing if in locale; regular style if not treating
4370 * latin1 as having case; otherwise the latin1 casing. Do the
4371 * whole thing in a tight loop, for speed, */
4372 if (IN_LOCALE_RUNTIME) {
4375 for (; s < send; d++, s++)
4376 *d = toUPPER_LC(*s);
4378 else if (! IN_UNI_8_BIT) {
4379 for (; s < send; d++, s++) {
4384 for (; s < send; d++, s++) {
4385 *d = toUPPER_LATIN1_MOD(*s);
4386 if (*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS) continue;
4388 /* The mainstream case is the tight loop above. To avoid
4389 * extra tests in that, all three characters that require
4390 * special handling are mapped by the MOD to the one tested
4392 * Use the source to distinguish between the three cases */
4394 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
4396 /* uc() of this requires 2 characters, but they are
4397 * ASCII. If not enough room, grow the string */
4398 if (SvLEN(dest) < ++min) {
4399 const UV o = d - (U8*)SvPVX_const(dest);
4401 d = (U8*)SvPVX(dest) + o;
4403 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
4404 continue; /* Back to the tight loop; still in ASCII */
4407 /* The other two special handling characters have their
4408 * upper cases outside the latin1 range, hence need to be
4409 * in UTF-8, so the whole result needs to be in UTF-8. So,
4410 * here we are somewhere in the middle of processing a
4411 * non-UTF-8 string, and realize that we will have to convert
4412 * the whole thing to UTF-8. What to do? There are
4413 * several possibilities. The simplest to code is to
4414 * convert what we have so far, set a flag, and continue on
4415 * in the loop. The flag would be tested each time through
4416 * the loop, and if set, the next character would be
4417 * converted to UTF-8 and stored. But, I (khw) didn't want
4418 * to slow down the mainstream case at all for this fairly
4419 * rare case, so I didn't want to add a test that didn't
4420 * absolutely have to be there in the loop, besides the
4421 * possibility that it would get too complicated for
4422 * optimizers to deal with. Another possibility is to just
4423 * give up, convert the source to UTF-8, and restart the
4424 * function that way. Another possibility is to convert
4425 * both what has already been processed and what is yet to
4426 * come separately to UTF-8, then jump into the loop that
4427 * handles UTF-8. But the most efficient time-wise of the
4428 * ones I could think of is what follows, and turned out to
4429 * not require much extra code. */
4431 /* Convert what we have so far into UTF-8, telling the
4432 * function that we know it should be converted, and to
4433 * allow extra space for what we haven't processed yet.
4434 * Assume the worst case space requirements for converting
4435 * what we haven't processed so far: that it will require
4436 * two bytes for each remaining source character, plus the
4437 * NUL at the end. This may cause the string pointer to
4438 * move, so re-find it. */
4440 len = d - (U8*)SvPVX_const(dest);
4441 SvCUR_set(dest, len);
4442 len = sv_utf8_upgrade_flags_grow(dest,
4443 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
4445 d = (U8*)SvPVX(dest) + len;
4447 /* And append the current character's upper case in UTF-8 */
4448 CAT_NON_LATIN1_UC(d, *s);
4450 /* Now process the remainder of the source, converting to
4451 * upper and UTF-8. If a resulting byte is invariant in
4452 * UTF-8, output it as-is, otherwise convert to UTF-8 and
4453 * append it to the output. */
4456 for (; s < send; s++) {
4457 U8 upper = toUPPER_LATIN1_MOD(*s);
4458 if UTF8_IS_INVARIANT(upper) {
4462 CAT_TWO_BYTE_UNI_UPPER_MOD(d, *s, upper);
4466 /* Here have processed the whole source; no need to continue
4467 * with the outer loop. Each character has been converted
4468 * to upper case and converted to UTF-8 */
4471 } /* End of processing all latin1-style chars */
4472 } /* End of processing all chars */
4473 } /* End of source is not empty */
4475 if (source != dest) {
4476 *d = '\0'; /* Here d points to 1 after last char, add NUL */
4477 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4479 } /* End of isn't utf8 */
4497 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
4498 && SvTEMP(source) && !DO_UTF8(source)) {
4500 /* We can convert in place, as lowercasing anything in the latin1 range
4501 * (or else DO_UTF8 would have been on) doesn't lengthen it */
4503 s = d = (U8*)SvPV_force_nomg(source, len);
4510 /* The old implementation would copy source into TARG at this point.
4511 This had the side effect that if source was undef, TARG was now
4512 an undefined SV with PADTMP set, and they don't warn inside
4513 sv_2pv_flags(). However, we're now getting the PV direct from
4514 source, which doesn't have PADTMP set, so it would warn. Hence the
4518 s = (const U8*)SvPV_nomg_const(source, len);
4520 if (ckWARN(WARN_UNINITIALIZED))
4521 report_uninit(source);
4527 SvUPGRADE(dest, SVt_PV);
4528 d = (U8*)SvGROW(dest, min);
4529 (void)SvPOK_only(dest);
4534 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
4535 to check DO_UTF8 again here. */
4537 if (DO_UTF8(source)) {
4538 const U8 *const send = s + len;
4539 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
4542 /* See comments at the first instance in this file of this ifdef */
4543 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4544 if (UTF8_IS_INVARIANT(*s)) {
4546 /* Invariant characters use the standard mappings compiled in.
4551 else if (UTF8_IS_DOWNGRADEABLE_START(*s)) {
4553 /* As do the ones in the Latin1 range */
4554 U8 lower = toLOWER_LATIN1(TWO_BYTE_UTF8_TO_UNI(*s, *s++));
4555 CAT_UNI_TO_UTF8_TWO_BYTE(d, lower);
4560 /* Here, is utf8 not in Latin-1 range, have to go out and get
4561 * the mappings from the tables. */
4563 const STRLEN u = UTF8SKIP(s);
4566 #ifndef CONTEXT_DEPENDENT_CASING
4567 toLOWER_utf8(s, tmpbuf, &ulen);
4569 /* This is ifdefd out because it needs more work and thought. It isn't clear
4570 * that we should do it.
4571 * A minor objection is that this is based on a hard-coded rule from the
4572 * Unicode standard, and may change, but this is not very likely at all.
4573 * mktables should check and warn if it does.
4574 * More importantly, if the sigma occurs at the end of the string, we don't
4575 * have enough context to know whether it is part of a larger string or going
4576 * to be or not. It may be that we are passed a subset of the context, via
4577 * a \U...\E, for example, and we could conceivably know the larger context if
4578 * code were changed to pass that in. But, if the string passed in is an
4579 * intermediate result, and the user concatenates two strings together
4580 * after we have made a final sigma, that would be wrong. If the final sigma
4581 * occurs in the middle of the string we are working on, then we know that it
4582 * should be a final sigma, but otherwise we can't be sure. */
4584 const UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
4586 /* If the lower case is a small sigma, it may be that we need
4587 * to change it to a final sigma. This happens at the end of
4588 * a word that contains more than just this character, and only
4589 * when we started with a capital sigma. */
4590 if (uv == UNICODE_GREEK_SMALL_LETTER_SIGMA &&
4591 s > send - len && /* Makes sure not the first letter */
4592 utf8_to_uvchr(s, 0) == UNICODE_GREEK_CAPITAL_LETTER_SIGMA
4595 /* We use the algorithm in:
4596 * http://www.unicode.org/versions/Unicode5.0.0/ch03.pdf (C
4597 * is a CAPITAL SIGMA): If C is preceded by a sequence
4598 * consisting of a cased letter and a case-ignorable
4599 * sequence, and C is not followed by a sequence consisting
4600 * of a case ignorable sequence and then a cased letter,
4601 * then when lowercasing C, C becomes a final sigma */
4603 /* To determine if this is the end of a word, need to peek
4604 * ahead. Look at the next character */
4605 const U8 *peek = s + u;
4607 /* Skip any case ignorable characters */
4608 while (peek < send && is_utf8_case_ignorable(peek)) {
4609 peek += UTF8SKIP(peek);
4612 /* If we reached the end of the string without finding any
4613 * non-case ignorable characters, or if the next such one
4614 * is not-cased, then we have met the conditions for it
4615 * being a final sigma with regards to peek ahead, and so
4616 * must do peek behind for the remaining conditions. (We
4617 * know there is stuff behind to look at since we tested
4618 * above that this isn't the first letter) */
4619 if (peek >= send || ! is_utf8_cased(peek)) {
4620 peek = utf8_hop(s, -1);
4622 /* Here are at the beginning of the first character
4623 * before the original upper case sigma. Keep backing
4624 * up, skipping any case ignorable characters */
4625 while (is_utf8_case_ignorable(peek)) {
4626 peek = utf8_hop(peek, -1);
4629 /* Here peek points to the first byte of the closest
4630 * non-case-ignorable character before the capital
4631 * sigma. If it is cased, then by the Unicode
4632 * algorithm, we should use a small final sigma instead
4633 * of what we have */
4634 if (is_utf8_cased(peek)) {
4635 STORE_UNI_TO_UTF8_TWO_BYTE(tmpbuf,
4636 UNICODE_GREEK_SMALL_LETTER_FINAL_SIGMA);
4640 else { /* Not a context sensitive mapping */
4641 #endif /* End of commented out context sensitive */
4642 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4644 /* If the eventually required minimum size outgrows
4645 * the available space, we need to grow. */
4646 const UV o = d - (U8*)SvPVX_const(dest);
4648 /* If someone lowercases one million U+0130s we
4649 * SvGROW() one million times. Or we could try
4650 * guessing how much to allocate without allocating too
4651 * much. Such is life. Another option would be to
4652 * grow an extra byte or two more each time we need to
4653 * grow, which would cut down the million to 500K, with
4656 d = (U8*)SvPVX(dest) + o;
4658 #ifdef CONTEXT_DEPENDENT_CASING
4661 /* Copy the newly lowercased letter to the output buffer we're
4663 Copy(tmpbuf, d, ulen, U8);
4666 #ifdef GO_AHEAD_AND_BREAK_USER_DEFINED_CASE_MAPPINGS
4669 } /* End of looping through the source string */
4672 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4673 } else { /* Not utf8 */
4675 const U8 *const send = s + len;
4677 /* Use locale casing if in locale; regular style if not treating
4678 * latin1 as having case; otherwise the latin1 casing. Do the
4679 * whole thing in a tight loop, for speed, */
4680 if (IN_LOCALE_RUNTIME) {
4683 for (; s < send; d++, s++)
4684 *d = toLOWER_LC(*s);
4686 else if (! IN_UNI_8_BIT) {
4687 for (; s < send; d++, s++) {
4692 for (; s < send; d++, s++) {
4693 *d = toLOWER_LATIN1(*s);
4697 if (source != dest) {
4699 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4709 SV * const sv = TOPs;
4711 register const char *s = SvPV_const(sv,len);
4713 SvUTF8_off(TARG); /* decontaminate */
4716 SvUPGRADE(TARG, SVt_PV);
4717 SvGROW(TARG, (len * 2) + 1);
4721 if (UTF8_IS_CONTINUED(*s)) {
4722 STRLEN ulen = UTF8SKIP(s);
4746 SvCUR_set(TARG, d - SvPVX_const(TARG));
4747 (void)SvPOK_only_UTF8(TARG);
4750 sv_setpvn(TARG, s, len);
4759 dVAR; dSP; dMARK; dORIGMARK;
4760 register AV *const av = MUTABLE_AV(POPs);
4761 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4763 if (SvTYPE(av) == SVt_PVAV) {
4764 const I32 arybase = CopARYBASE_get(PL_curcop);
4765 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4766 bool can_preserve = FALSE;
4772 can_preserve = SvCANEXISTDELETE(av);
4775 if (lval && localizing) {
4778 for (svp = MARK + 1; svp <= SP; svp++) {
4779 const I32 elem = SvIV(*svp);
4783 if (max > AvMAX(av))
4787 while (++MARK <= SP) {
4789 I32 elem = SvIV(*MARK);
4790 bool preeminent = TRUE;
4794 if (localizing && can_preserve) {
4795 /* If we can determine whether the element exist,
4796 * Try to preserve the existenceness of a tied array
4797 * element by using EXISTS and DELETE if possible.
4798 * Fallback to FETCH and STORE otherwise. */
4799 preeminent = av_exists(av, elem);
4802 svp = av_fetch(av, elem, lval);
4804 if (!svp || *svp == &PL_sv_undef)
4805 DIE(aTHX_ PL_no_aelem, elem);
4808 save_aelem(av, elem, svp);
4810 SAVEADELETE(av, elem);
4813 *MARK = svp ? *svp : &PL_sv_undef;
4816 if (GIMME != G_ARRAY) {
4818 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4824 /* Smart dereferencing for keys, values and each */
4837 /* N.B.: AMG macros return sv if no overloading is found */
4838 SV *maybe_hv = AMG_CALLunary(sv, to_hv_amg);
4839 SV *maybe_av = AMG_CALLunary(sv, to_av_amg);
4840 if ( maybe_hv != sv && maybe_av != sv ) {
4841 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4842 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as %%{}",
4843 PL_op_desc[PL_op->op_type]
4848 else if ( maybe_av != sv ) {
4849 if ( SvTYPE(SvRV(sv)) == SVt_PVHV ) {
4850 /* @{} overload, but underlying reftype is HV */
4851 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4852 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as @{}",
4853 PL_op_desc[PL_op->op_type]
4859 else if ( maybe_hv != sv ) {
4860 if ( SvTYPE(SvRV(sv)) == SVt_PVAV ) {
4861 /* %{} overload, but underlying reftype is AV */
4862 Perl_ck_warner(aTHX_ packWARN(WARN_AMBIGUOUS), "%s",
4863 Perl_form(aTHX_ "Ambiguous overloaded argument to %s resolved as %%{}",
4864 PL_op_desc[PL_op->op_type]
4874 if ( SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV ) {
4875 DIE(aTHX_ "Type of argument to %s must be hashref or arrayref",
4876 PL_op_desc[PL_op->op_type] );
4879 /* Delegate to correct function for op type */
4881 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4882 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4885 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4893 AV *array = MUTABLE_AV(POPs);
4894 const I32 gimme = GIMME_V;
4895 IV *iterp = Perl_av_iter_p(aTHX_ array);
4896 const IV current = (*iterp)++;
4898 if (current > av_len(array)) {
4900 if (gimme == G_SCALAR)
4907 mPUSHi(CopARYBASE_get(PL_curcop) + current);
4908 if (gimme == G_ARRAY) {
4909 SV **const element = av_fetch(array, current, 0);
4910 PUSHs(element ? *element : &PL_sv_undef);
4919 AV *array = MUTABLE_AV(POPs);
4920 const I32 gimme = GIMME_V;
4922 *Perl_av_iter_p(aTHX_ array) = 0;
4924 if (gimme == G_SCALAR) {
4926 PUSHi(av_len(array) + 1);
4928 else if (gimme == G_ARRAY) {
4929 IV n = Perl_av_len(aTHX_ array);
4930 IV i = CopARYBASE_get(PL_curcop);
4934 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4936 for (; i <= n; i++) {
4941 for (i = 0; i <= n; i++) {
4942 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4943 PUSHs(elem ? *elem : &PL_sv_undef);
4950 /* Associative arrays. */
4956 HV * hash = MUTABLE_HV(POPs);
4958 const I32 gimme = GIMME_V;
4961 /* might clobber stack_sp */
4962 entry = hv_iternext(hash);
4967 SV* const sv = hv_iterkeysv(entry);
4968 PUSHs(sv); /* won't clobber stack_sp */
4969 if (gimme == G_ARRAY) {
4972 /* might clobber stack_sp */
4973 val = hv_iterval(hash, entry);
4978 else if (gimme == G_SCALAR)
4985 S_do_delete_local(pTHX)
4989 const I32 gimme = GIMME_V;
4993 if (PL_op->op_private & OPpSLICE) {
4995 SV * const osv = POPs;
4996 const bool tied = SvRMAGICAL(osv)
4997 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4998 const bool can_preserve = SvCANEXISTDELETE(osv)
4999 || mg_find((const SV *)osv, PERL_MAGIC_env);
5000 const U32 type = SvTYPE(osv);
5001 if (type == SVt_PVHV) { /* hash element */
5002 HV * const hv = MUTABLE_HV(osv);
5003 while (++MARK <= SP) {
5004 SV * const keysv = *MARK;
5006 bool preeminent = TRUE;
5008 preeminent = hv_exists_ent(hv, keysv, 0);
5010 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
5017 sv = hv_delete_ent(hv, keysv, 0, 0);
5018 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5021 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
5023 *MARK = sv_mortalcopy(sv);
5029 SAVEHDELETE(hv, keysv);
5030 *MARK = &PL_sv_undef;
5034 else if (type == SVt_PVAV) { /* array element */
5035 if (PL_op->op_flags & OPf_SPECIAL) {
5036 AV * const av = MUTABLE_AV(osv);
5037 while (++MARK <= SP) {
5038 I32 idx = SvIV(*MARK);
5040 bool preeminent = TRUE;
5042 preeminent = av_exists(av, idx);
5044 SV **svp = av_fetch(av, idx, 1);
5051 sv = av_delete(av, idx, 0);
5052 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5055 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
5057 *MARK = sv_mortalcopy(sv);
5063 SAVEADELETE(av, idx);
5064 *MARK = &PL_sv_undef;
5070 DIE(aTHX_ "Not a HASH reference");
5071 if (gimme == G_VOID)
5073 else if (gimme == G_SCALAR) {
5078 *++MARK = &PL_sv_undef;
5083 SV * const keysv = POPs;
5084 SV * const osv = POPs;
5085 const bool tied = SvRMAGICAL(osv)
5086 && mg_find((const SV *)osv, PERL_MAGIC_tied);
5087 const bool can_preserve = SvCANEXISTDELETE(osv)
5088 || mg_find((const SV *)osv, PERL_MAGIC_env);
5089 const U32 type = SvTYPE(osv);
5091 if (type == SVt_PVHV) {
5092 HV * const hv = MUTABLE_HV(osv);
5093 bool preeminent = TRUE;
5095 preeminent = hv_exists_ent(hv, keysv, 0);
5097 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
5104 sv = hv_delete_ent(hv, keysv, 0, 0);
5105 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5108 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
5110 SV *nsv = sv_mortalcopy(sv);
5116 SAVEHDELETE(hv, keysv);
5118 else if (type == SVt_PVAV) {
5119 if (PL_op->op_flags & OPf_SPECIAL) {
5120 AV * const av = MUTABLE_AV(osv);
5121 I32 idx = SvIV(keysv);
5122 bool preeminent = TRUE;
5124 preeminent = av_exists(av, idx);
5126 SV **svp = av_fetch(av, idx, 1);
5133 sv = av_delete(av, idx, 0);
5134 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
5137 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
5139 SV *nsv = sv_mortalcopy(sv);
5145 SAVEADELETE(av, idx);
5148 DIE(aTHX_ "panic: avhv_delete no longer supported");
5151 DIE(aTHX_ "Not a HASH reference");
5154 if (gimme != G_VOID)
5168 if (PL_op->op_private & OPpLVAL_INTRO)
5169 return do_delete_local();
5172 discard = (gimme == G_VOID) ? G_DISCARD : 0;
5174 if (PL_op->op_private & OPpSLICE) {
5176 HV * const hv = MUTABLE_HV(POPs);
5177 const U32 hvtype = SvTYPE(hv);
5178 if (hvtype == SVt_PVHV) { /* hash element */
5179 while (++MARK <= SP) {
5180 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
5181 *MARK = sv ? sv : &PL_sv_undef;
5184 else if (hvtype == SVt_PVAV) { /* array element */
5185 if (PL_op->op_flags & OPf_SPECIAL) {
5186 while (++MARK <= SP) {
5187 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
5188 *MARK = sv ? sv : &PL_sv_undef;
5193 DIE(aTHX_ "Not a HASH reference");
5196 else if (gimme == G_SCALAR) {
5201 *++MARK = &PL_sv_undef;
5207 HV * const hv = MUTABLE_HV(POPs);
5209 if (SvTYPE(hv) == SVt_PVHV)
5210 sv = hv_delete_ent(hv, keysv, discard, 0);
5211 else if (SvTYPE(hv) == SVt_PVAV) {
5212 if (PL_op->op_flags & OPf_SPECIAL)
5213 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
5215 DIE(aTHX_ "panic: avhv_delete no longer supported");
5218 DIE(aTHX_ "Not a HASH reference");
5234 if (PL_op->op_private & OPpEXISTS_SUB) {
5236 SV * const sv = POPs;
5237 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
5240 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
5245 hv = MUTABLE_HV(POPs);
5246 if (SvTYPE(hv) == SVt_PVHV) {
5247 if (hv_exists_ent(hv, tmpsv, 0))
5250 else if (SvTYPE(hv) == SVt_PVAV) {
5251 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
5252 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
5257 DIE(aTHX_ "Not a HASH reference");
5264 dVAR; dSP; dMARK; dORIGMARK;
5265 register HV * const hv = MUTABLE_HV(POPs);
5266 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
5267 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
5268 bool can_preserve = FALSE;
5274 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
5275 can_preserve = TRUE;
5278 while (++MARK <= SP) {
5279 SV * const keysv = *MARK;
5282 bool preeminent = TRUE;
5284 if (localizing && can_preserve) {
5285 /* If we can determine whether the element exist,
5286 * try to preserve the existenceness of a tied hash
5287 * element by using EXISTS and DELETE if possible.
5288 * Fallback to FETCH and STORE otherwise. */
5289 preeminent = hv_exists_ent(hv, keysv, 0);
5292 he = hv_fetch_ent(hv, keysv, lval, 0);
5293 svp = he ? &HeVAL(he) : NULL;
5296 if (!svp || *svp == &PL_sv_undef) {
5297 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
5300 if (HvNAME_get(hv) && isGV(*svp))
5301 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
5302 else if (preeminent)
5303 save_helem_flags(hv, keysv, svp,
5304 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
5306 SAVEHDELETE(hv, keysv);
5309 *MARK = svp ? *svp : &PL_sv_undef;
5311 if (GIMME != G_ARRAY) {
5313 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
5319 /* List operators. */
5324 if (GIMME != G_ARRAY) {
5326 *MARK = *SP; /* unwanted list, return last item */
5328 *MARK = &PL_sv_undef;
5338 SV ** const lastrelem = PL_stack_sp;
5339 SV ** const lastlelem = PL_stack_base + POPMARK;
5340 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
5341 register SV ** const firstrelem = lastlelem + 1;
5342 const I32 arybase = CopARYBASE_get(PL_curcop);
5343 I32 is_something_there = FALSE;
5345 register const I32 max = lastrelem - lastlelem;
5346 register SV **lelem;
5348 if (GIMME != G_ARRAY) {
5349 I32 ix = SvIV(*lastlelem);
5354 if (ix < 0 || ix >= max)
5355 *firstlelem = &PL_sv_undef;
5357 *firstlelem = firstrelem[ix];
5363 SP = firstlelem - 1;
5367 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
5368 I32 ix = SvIV(*lelem);
5373 if (ix < 0 || ix >= max)
5374 *lelem = &PL_sv_undef;
5376 is_something_there = TRUE;
5377 if (!(*lelem = firstrelem[ix]))
5378 *lelem = &PL_sv_undef;
5381 if (is_something_there)
5384 SP = firstlelem - 1;
5390 dVAR; dSP; dMARK; dORIGMARK;
5391 const I32 items = SP - MARK;
5392 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
5393 SP = ORIGMARK; /* av_make() might realloc stack_sp */
5394 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5395 ? newRV_noinc(av) : av);
5401 dVAR; dSP; dMARK; dORIGMARK;
5402 HV* const hv = newHV();
5405 SV * const key = *++MARK;
5406 SV * const val = newSV(0);
5408 sv_setsv(val, *++MARK);
5410 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
5411 (void)hv_store_ent(hv,key,val,0);
5414 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
5415 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
5421 dVAR; dSP; dMARK; dORIGMARK;
5422 register AV *ary = MUTABLE_AV(*++MARK);
5426 register I32 offset;
5427 register I32 length;
5431 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5434 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
5435 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
5442 offset = i = SvIV(*MARK);
5444 offset += AvFILLp(ary) + 1;
5446 offset -= CopARYBASE_get(PL_curcop);
5448 DIE(aTHX_ PL_no_aelem, i);
5450 length = SvIVx(*MARK++);
5452 length += AvFILLp(ary) - offset + 1;
5458 length = AvMAX(ary) + 1; /* close enough to infinity */
5462 length = AvMAX(ary) + 1;
5464 if (offset > AvFILLp(ary) + 1) {
5465 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
5466 offset = AvFILLp(ary) + 1;
5468 after = AvFILLp(ary) + 1 - (offset + length);
5469 if (after < 0) { /* not that much array */
5470 length += after; /* offset+length now in array */
5476 /* At this point, MARK .. SP-1 is our new LIST */
5479 diff = newlen - length;
5480 if (newlen && !AvREAL(ary) && AvREIFY(ary))
5483 /* make new elements SVs now: avoid problems if they're from the array */
5484 for (dst = MARK, i = newlen; i; i--) {
5485 SV * const h = *dst;
5486 *dst++ = newSVsv(h);
5489 if (diff < 0) { /* shrinking the area */
5490 SV **tmparyval = NULL;
5492 Newx(tmparyval, newlen, SV*); /* so remember insertion */
5493 Copy(MARK, tmparyval, newlen, SV*);
5496 MARK = ORIGMARK + 1;
5497 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5498 MEXTEND(MARK, length);
5499 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
5501 EXTEND_MORTAL(length);
5502 for (i = length, dst = MARK; i; i--) {
5503 sv_2mortal(*dst); /* free them eventually */
5510 *MARK = AvARRAY(ary)[offset+length-1];
5513 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
5514 SvREFCNT_dec(*dst++); /* free them now */
5517 AvFILLp(ary) += diff;
5519 /* pull up or down? */
5521 if (offset < after) { /* easier to pull up */
5522 if (offset) { /* esp. if nothing to pull */
5523 src = &AvARRAY(ary)[offset-1];
5524 dst = src - diff; /* diff is negative */
5525 for (i = offset; i > 0; i--) /* can't trust Copy */
5529 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
5533 if (after) { /* anything to pull down? */
5534 src = AvARRAY(ary) + offset + length;
5535 dst = src + diff; /* diff is negative */
5536 Move(src, dst, after, SV*);
5538 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
5539 /* avoid later double free */
5543 dst[--i] = &PL_sv_undef;
5546 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
5547 Safefree(tmparyval);
5550 else { /* no, expanding (or same) */
5551 SV** tmparyval = NULL;
5553 Newx(tmparyval, length, SV*); /* so remember deletion */
5554 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
5557 if (diff > 0) { /* expanding */
5558 /* push up or down? */
5559 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
5563 Move(src, dst, offset, SV*);
5565 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
5567 AvFILLp(ary) += diff;
5570 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
5571 av_extend(ary, AvFILLp(ary) + diff);
5572 AvFILLp(ary) += diff;
5575 dst = AvARRAY(ary) + AvFILLp(ary);
5577 for (i = after; i; i--) {
5585 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
5588 MARK = ORIGMARK + 1;
5589 if (GIMME == G_ARRAY) { /* copy return vals to stack */
5591 Copy(tmparyval, MARK, length, SV*);
5593 EXTEND_MORTAL(length);
5594 for (i = length, dst = MARK; i; i--) {
5595 sv_2mortal(*dst); /* free them eventually */
5602 else if (length--) {
5603 *MARK = tmparyval[length];
5606 while (length-- > 0)
5607 SvREFCNT_dec(tmparyval[length]);
5611 *MARK = &PL_sv_undef;
5612 Safefree(tmparyval);
5616 mg_set(MUTABLE_SV(ary));
5624 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5625 register AV * const ary = MUTABLE_AV(*++MARK);
5626 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5629 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5632 ENTER_with_name("call_PUSH");
5633 call_method("PUSH",G_SCALAR|G_DISCARD);
5634 LEAVE_with_name("call_PUSH");
5638 PL_delaymagic = DM_DELAY;
5639 for (++MARK; MARK <= SP; MARK++) {
5640 SV * const sv = newSV(0);
5642 sv_setsv(sv, *MARK);
5643 av_store(ary, AvFILLp(ary)+1, sv);
5645 if (PL_delaymagic & DM_ARRAY_ISA)
5646 mg_set(MUTABLE_SV(ary));
5651 if (OP_GIMME(PL_op, 0) != G_VOID) {
5652 PUSHi( AvFILL(ary) + 1 );
5661 AV * const av = PL_op->op_flags & OPf_SPECIAL
5662 ? MUTABLE_AV(GvAV(PL_defgv)) : MUTABLE_AV(POPs);
5663 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5667 (void)sv_2mortal(sv);
5674 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5675 register AV *ary = MUTABLE_AV(*++MARK);
5676 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5679 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5682 ENTER_with_name("call_UNSHIFT");
5683 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5684 LEAVE_with_name("call_UNSHIFT");
5689 av_unshift(ary, SP - MARK);
5691 SV * const sv = newSVsv(*++MARK);
5692 (void)av_store(ary, i++, sv);
5696 if (OP_GIMME(PL_op, 0) != G_VOID) {
5697 PUSHi( AvFILL(ary) + 1 );
5706 if (GIMME == G_ARRAY) {
5707 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5711 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5712 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5713 av = MUTABLE_AV((*SP));
5714 /* In-place reversing only happens in void context for the array
5715 * assignment. We don't need to push anything on the stack. */
5718 if (SvMAGICAL(av)) {
5720 register SV *tmp = sv_newmortal();
5721 /* For SvCANEXISTDELETE */
5724 bool can_preserve = SvCANEXISTDELETE(av);
5726 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5727 register SV *begin, *end;
5730 if (!av_exists(av, i)) {
5731 if (av_exists(av, j)) {
5732 register SV *sv = av_delete(av, j, 0);
5733 begin = *av_fetch(av, i, TRUE);
5734 sv_setsv_mg(begin, sv);
5738 else if (!av_exists(av, j)) {
5739 register SV *sv = av_delete(av, i, 0);
5740 end = *av_fetch(av, j, TRUE);
5741 sv_setsv_mg(end, sv);
5746 begin = *av_fetch(av, i, TRUE);
5747 end = *av_fetch(av, j, TRUE);
5748 sv_setsv(tmp, begin);
5749 sv_setsv_mg(begin, end);
5750 sv_setsv_mg(end, tmp);
5754 SV **begin = AvARRAY(av);
5757 SV **end = begin + AvFILLp(av);
5759 while (begin < end) {
5760 register SV * const tmp = *begin;
5771 register SV * const tmp = *MARK;
5775 /* safe as long as stack cannot get extended in the above */
5781 register char *down;
5786 SvUTF8_off(TARG); /* decontaminate */
5788 do_join(TARG, &PL_sv_no, MARK, SP);
5790 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5791 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5792 report_uninit(TARG);
5795 up = SvPV_force(TARG, len);
5797 if (DO_UTF8(TARG)) { /* first reverse each character */
5798 U8* s = (U8*)SvPVX(TARG);
5799 const U8* send = (U8*)(s + len);
5801 if (UTF8_IS_INVARIANT(*s)) {
5806 if (!utf8_to_uvchr(s, 0))
5810 down = (char*)(s - 1);
5811 /* reverse this character */
5815 *down-- = (char)tmp;
5821 down = SvPVX(TARG) + len - 1;
5825 *down-- = (char)tmp;
5827 (void)SvPOK_only_UTF8(TARG);
5839 register IV limit = POPi; /* note, negative is forever */
5840 SV * const sv = POPs;
5842 register const char *s = SvPV_const(sv, len);
5843 const bool do_utf8 = DO_UTF8(sv);
5844 const char *strend = s + len;
5846 register REGEXP *rx;
5848 register const char *m;
5850 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5851 I32 maxiters = slen + 10;
5852 I32 trailing_empty = 0;
5854 const I32 origlimit = limit;
5857 const I32 gimme = GIMME_V;
5859 const I32 oldsave = PL_savestack_ix;
5860 U32 make_mortal = SVs_TEMP;
5865 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5870 DIE(aTHX_ "panic: pp_split");
5873 TAINT_IF((RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) &&
5874 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5876 RX_MATCH_UTF8_set(rx, do_utf8);
5879 if (pm->op_pmreplrootu.op_pmtargetoff) {
5880 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5883 if (pm->op_pmreplrootu.op_pmtargetgv) {
5884 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5889 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5895 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5897 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5904 for (i = AvFILLp(ary); i >= 0; i--)
5905 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5907 /* temporarily switch stacks */
5908 SAVESWITCHSTACK(PL_curstack, ary);
5912 base = SP - PL_stack_base;
5914 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5916 while (*s == ' ' || is_utf8_space((U8*)s))
5919 else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5920 while (isSPACE_LC(*s))
5928 if (RX_EXTFLAGS(rx) & PMf_MULTILINE) {
5932 gimme_scalar = gimme == G_SCALAR && !ary;
5935 limit = maxiters + 2;
5936 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5939 /* this one uses 'm' and is a negative test */
5941 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5942 const int t = UTF8SKIP(m);
5943 /* is_utf8_space returns FALSE for malform utf8 */
5949 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5950 while (m < strend && !isSPACE_LC(*m))
5953 while (m < strend && !isSPACE(*m))
5966 dstr = newSVpvn_flags(s, m-s,
5967 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5971 /* skip the whitespace found last */
5973 s = m + UTF8SKIP(m);
5977 /* this one uses 's' and is a positive test */
5979 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5981 } else if (RX_EXTFLAGS(rx) & RXf_PMf_LOCALE) {
5982 while (s < strend && isSPACE_LC(*s))
5985 while (s < strend && isSPACE(*s))
5990 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5992 for (m = s; m < strend && *m != '\n'; m++)
6005 dstr = newSVpvn_flags(s, m-s,
6006 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6012 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
6014 Pre-extend the stack, either the number of bytes or
6015 characters in the string or a limited amount, triggered by:
6017 my ($x, $y) = split //, $str;
6021 if (!gimme_scalar) {
6022 const U32 items = limit - 1;
6031 /* keep track of how many bytes we skip over */
6041 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
6054 dstr = newSVpvn(s, 1);
6070 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
6071 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
6072 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
6073 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
6074 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
6075 SV * const csv = CALLREG_INTUIT_STRING(rx);
6077 len = RX_MINLENRET(rx);
6078 if (len == 1 && !RX_UTF8(rx) && !tail) {
6079 const char c = *SvPV_nolen_const(csv);
6081 for (m = s; m < strend && *m != c; m++)
6092 dstr = newSVpvn_flags(s, m-s,
6093 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6096 /* The rx->minlen is in characters but we want to step
6097 * s ahead by bytes. */
6099 s = (char*)utf8_hop((U8*)m, len);
6101 s = m + len; /* Fake \n at the end */
6105 while (s < strend && --limit &&
6106 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
6107 csv, multiline ? FBMrf_MULTILINE : 0)) )
6116 dstr = newSVpvn_flags(s, m-s,
6117 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6120 /* The rx->minlen is in characters but we want to step
6121 * s ahead by bytes. */
6123 s = (char*)utf8_hop((U8*)m, len);
6125 s = m + len; /* Fake \n at the end */
6130 maxiters += slen * RX_NPARENS(rx);
6131 while (s < strend && --limit)
6135 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
6138 if (rex_return == 0)
6140 TAINT_IF(RX_MATCH_TAINTED(rx));
6141 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
6144 orig = RX_SUBBEG(rx);
6146 strend = s + (strend - m);
6148 m = RX_OFFS(rx)[0].start + orig;
6157 dstr = newSVpvn_flags(s, m-s,
6158 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6161 if (RX_NPARENS(rx)) {
6163 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
6164 s = RX_OFFS(rx)[i].start + orig;
6165 m = RX_OFFS(rx)[i].end + orig;
6167 /* japhy (07/27/01) -- the (m && s) test doesn't catch
6168 parens that didn't match -- they should be set to
6169 undef, not the empty string */
6177 if (m >= orig && s >= orig) {
6178 dstr = newSVpvn_flags(s, m-s,
6179 (do_utf8 ? SVf_UTF8 : 0)
6183 dstr = &PL_sv_undef; /* undef, not "" */
6189 s = RX_OFFS(rx)[0].end + orig;
6193 if (!gimme_scalar) {
6194 iters = (SP - PL_stack_base) - base;
6196 if (iters > maxiters)
6197 DIE(aTHX_ "Split loop");
6199 /* keep field after final delim? */
6200 if (s < strend || (iters && origlimit)) {
6201 if (!gimme_scalar) {
6202 const STRLEN l = strend - s;
6203 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
6208 else if (!origlimit) {
6210 iters -= trailing_empty;
6212 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
6213 if (TOPs && !make_mortal)
6215 *SP-- = &PL_sv_undef;
6222 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
6226 if (SvSMAGICAL(ary)) {
6228 mg_set(MUTABLE_SV(ary));
6231 if (gimme == G_ARRAY) {
6233 Copy(AvARRAY(ary), SP + 1, iters, SV*);
6240 ENTER_with_name("call_PUSH");
6241 call_method("PUSH",G_SCALAR|G_DISCARD);
6242 LEAVE_with_name("call_PUSH");
6244 if (gimme == G_ARRAY) {
6246 /* EXTEND should not be needed - we just popped them */
6248 for (i=0; i < iters; i++) {
6249 SV **svp = av_fetch(ary, i, FALSE);
6250 PUSHs((svp) ? *svp : &PL_sv_undef);
6257 if (gimme == G_ARRAY)
6269 SV *const sv = PAD_SVl(PL_op->op_targ);
6271 if (SvPADSTALE(sv)) {
6274 RETURNOP(cLOGOP->op_other);
6276 RETURNOP(cLOGOP->op_next);
6285 assert(SvTYPE(retsv) != SVt_PVCV);
6287 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV) {
6288 retsv = refto(retsv);
6295 PP(unimplemented_op)
6298 const Optype op_type = PL_op->op_type;
6299 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
6300 with out of range op numbers - it only "special" cases op_custom.
6301 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
6302 if we get here for a custom op then that means that the custom op didn't
6303 have an implementation. Given that OP_NAME() looks up the custom op
6304 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
6305 registers &PL_unimplemented_op as the address of their custom op.
6306 NULL doesn't generate a useful error message. "custom" does. */
6307 const char *const name = op_type >= OP_max
6308 ? "[out of range]" : PL_op_name[PL_op->op_type];
6309 if(OP_IS_SOCKET(op_type))
6310 DIE(aTHX_ PL_no_sock_func, name);
6311 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
6318 HV * const hv = (HV*)POPs;
6320 if (SvRMAGICAL(hv)) {
6321 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
6323 XPUSHs(magic_scalarpack(hv, mg));
6328 XPUSHs(boolSV(HvKEYS(hv) != 0));
6334 * c-indentation-style: bsd
6336 * indent-tabs-mode: t
6339 * ex: set ts=8 sts=4 sw=4 noet: