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) {
74 } else if (PL_op->op_private & OPpMAYBE_LVSUB) {
75 const I32 flags = is_lvalue_sub();
76 if (flags && !(flags & OPpENTERSUB_INARGS)) {
77 if (GIMME == G_SCALAR)
78 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
84 if (gimme == G_ARRAY) {
85 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
87 if (SvMAGICAL(TARG)) {
89 for (i=0; i < (U32)maxarg; i++) {
90 SV * const * const svp = av_fetch(MUTABLE_AV(TARG), i, FALSE);
91 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
95 Copy(AvARRAY((const AV *)TARG), SP+1, maxarg, SV*);
99 else if (gimme == G_SCALAR) {
100 SV* const sv = sv_newmortal();
101 const I32 maxarg = AvFILL(MUTABLE_AV(TARG)) + 1;
102 sv_setiv(sv, maxarg);
113 assert(SvTYPE(TARG) == SVt_PVHV);
115 if (PL_op->op_private & OPpLVAL_INTRO)
116 if (!(PL_op->op_private & OPpPAD_STATE))
117 SAVECLEARSV(PAD_SVl(PL_op->op_targ));
118 if (PL_op->op_flags & OPf_REF)
120 else if (PL_op->op_private & OPpMAYBE_LVSUB) {
121 const I32 flags = is_lvalue_sub();
122 if (flags && !(flags & OPpENTERSUB_INARGS)) {
123 if (GIMME == G_SCALAR)
124 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
129 if (gimme == G_ARRAY) {
130 RETURNOP(Perl_do_kv(aTHX));
132 else if (gimme == G_SCALAR) {
133 SV* const sv = Perl_hv_scalar(aTHX_ MUTABLE_HV(TARG));
141 static const char S_no_symref_sv[] =
142 "Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use";
144 /* In some cases this function inspects PL_op. If this function is called
145 for new op types, more bool parameters may need to be added in place of
148 When noinit is true, the absence of a gv will cause a retval of undef.
149 This is unrelated to the cv-to-gv assignment case.
153 S_rv2gv(pTHX_ SV *sv, const bool vivify_sv, const bool strict,
157 if (!isGV(sv) || SvFAKE(sv)) SvGETMAGIC(sv);
160 sv = amagic_deref_call(sv, to_gv_amg);
164 if (SvTYPE(sv) == SVt_PVIO) {
165 GV * const gv = MUTABLE_GV(sv_newmortal());
166 gv_init(gv, 0, "__ANONIO__", 10, 0);
167 GvIOp(gv) = MUTABLE_IO(sv);
168 SvREFCNT_inc_void_NN(sv);
171 else if (!isGV_with_GP(sv))
172 return (SV *)Perl_die(aTHX_ "Not a GLOB reference");
175 if (!isGV_with_GP(sv)) {
177 /* If this is a 'my' scalar and flag is set then vivify
180 if (vivify_sv && sv != &PL_sv_undef) {
183 Perl_croak_no_modify(aTHX);
184 if (cUNOP->op_targ) {
185 SV * const namesv = PAD_SV(cUNOP->op_targ);
186 gv = MUTABLE_GV(newSV(0));
187 gv_init_sv(gv, CopSTASH(PL_curcop), namesv, 0);
190 const char * const name = CopSTASHPV(PL_curcop);
191 gv = newGVgen_flags(name,
192 HvNAMEUTF8(CopSTASH(PL_curcop)) ? SVf_UTF8 : 0 );
194 prepare_SV_for_RV(sv);
195 SvRV_set(sv, MUTABLE_SV(gv));
200 if (PL_op->op_flags & OPf_REF || strict)
201 return (SV *)Perl_die(aTHX_ PL_no_usym, "a symbol");
202 if (ckWARN(WARN_UNINITIALIZED))
208 if (!(sv = MUTABLE_SV(gv_fetchsv_nomg(
209 sv, GV_ADDMG, SVt_PVGV
219 (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""),
222 if ((PL_op->op_private & (OPpLVAL_INTRO|OPpDONT_INIT_GV))
223 == OPpDONT_INIT_GV) {
224 /* We are the target of a coderef assignment. Return
225 the scalar unchanged, and let pp_sasssign deal with
229 sv = MUTABLE_SV(gv_fetchsv_nomg(sv, GV_ADD, SVt_PVGV));
231 /* FAKE globs in the symbol table cause weird bugs (#77810) */
235 if (SvFAKE(sv) && !(PL_op->op_private & OPpALLOW_FAKE)) {
236 SV *newsv = sv_newmortal();
237 sv_setsv_flags(newsv, sv, 0);
249 sv, PL_op->op_private & OPpDEREF,
250 PL_op->op_private & HINT_STRICT_REFS,
251 ((PL_op->op_flags & OPf_SPECIAL) && !(PL_op->op_flags & OPf_MOD))
252 || PL_op->op_type == OP_READLINE
254 if (PL_op->op_private & OPpLVAL_INTRO)
255 save_gp(MUTABLE_GV(sv), !(PL_op->op_flags & OPf_SPECIAL));
260 /* Helper function for pp_rv2sv and pp_rv2av */
262 Perl_softref2xv(pTHX_ SV *const sv, const char *const what,
263 const svtype type, SV ***spp)
268 PERL_ARGS_ASSERT_SOFTREF2XV;
270 if (PL_op->op_private & HINT_STRICT_REFS) {
272 Perl_die(aTHX_ S_no_symref_sv, sv, (SvPOK(sv) && SvCUR(sv)>32 ? "..." : ""), what);
274 Perl_die(aTHX_ PL_no_usym, what);
278 PL_op->op_flags & OPf_REF &&
279 PL_op->op_next->op_type != OP_BOOLKEYS
281 Perl_die(aTHX_ PL_no_usym, what);
282 if (ckWARN(WARN_UNINITIALIZED))
284 if (type != SVt_PV && GIMME_V == G_ARRAY) {
288 **spp = &PL_sv_undef;
291 if ((PL_op->op_flags & OPf_SPECIAL) &&
292 !(PL_op->op_flags & OPf_MOD))
294 if (!(gv = gv_fetchsv_nomg(sv, GV_ADDMG, type)))
296 **spp = &PL_sv_undef;
301 gv = gv_fetchsv_nomg(sv, GV_ADD, type);
314 sv = amagic_deref_call(sv, to_sv_amg);
318 switch (SvTYPE(sv)) {
324 DIE(aTHX_ "Not a SCALAR reference");
331 if (!isGV_with_GP(gv)) {
332 gv = Perl_softref2xv(aTHX_ sv, "a SCALAR", SVt_PV, &sp);
338 if (PL_op->op_flags & OPf_MOD) {
339 if (PL_op->op_private & OPpLVAL_INTRO) {
340 if (cUNOP->op_first->op_type == OP_NULL)
341 sv = save_scalar(MUTABLE_GV(TOPs));
343 sv = save_scalar(gv);
345 Perl_croak(aTHX_ "%s", PL_no_localize_ref);
347 else if (PL_op->op_private & OPpDEREF)
348 sv = vivify_ref(sv, PL_op->op_private & OPpDEREF);
357 AV * const av = MUTABLE_AV(TOPs);
358 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
360 SV ** const sv = Perl_av_arylen_p(aTHX_ MUTABLE_AV(av));
362 *sv = newSV_type(SVt_PVMG);
363 sv_magic(*sv, MUTABLE_SV(av), PERL_MAGIC_arylen, NULL, 0);
367 SETs(sv_2mortal(newSViv(AvFILL(MUTABLE_AV(av)))));
376 if (PL_op->op_flags & OPf_MOD || LVRET) {
377 SV * const ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
378 sv_magic(ret, NULL, PERL_MAGIC_pos, NULL, 0);
380 LvTARG(ret) = SvREFCNT_inc_simple(sv);
381 PUSHs(ret); /* no SvSETMAGIC */
385 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
386 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_regex_global);
387 if (mg && mg->mg_len >= 0) {
405 const I32 flags = (PL_op->op_flags & OPf_SPECIAL)
407 : ((PL_op->op_private & (OPpLVAL_INTRO|OPpMAY_RETURN_CONSTANT)) == OPpMAY_RETURN_CONSTANT)
410 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
411 /* (But not in defined().) */
413 CV *cv = sv_2cv(TOPs, &stash_unused, &gv, flags);
416 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
417 if ((PL_op->op_private & OPpLVAL_INTRO)) {
418 if (gv && GvCV(gv) == cv && (gv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), GvNAMEUTF8(gv) ? SVf_UTF8 : 0)))
421 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
424 else if ((flags == (GV_ADD|GV_NOEXPAND)) && gv && SvROK(gv)) {
428 cv = MUTABLE_CV(&PL_sv_undef);
429 SETs(MUTABLE_SV(cv));
439 SV *ret = &PL_sv_undef;
441 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
442 const char * s = SvPVX_const(TOPs);
443 if (strnEQ(s, "CORE::", 6)) {
444 const int code = keyword(s + 6, SvCUR(TOPs) - 6, 1);
445 if (!code || code == -KEY_CORE)
446 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
447 if (code < 0) { /* Overridable. */
448 SV * const sv = core_prototype(NULL, s + 6, code, NULL);
454 cv = sv_2cv(TOPs, &stash, &gv, 0);
456 ret = newSVpvn_flags(
457 CvPROTO(cv), CvPROTOLEN(cv), SVs_TEMP | SvUTF8(cv)
467 CV *cv = MUTABLE_CV(PAD_SV(PL_op->op_targ));
469 cv = MUTABLE_CV(sv_2mortal(MUTABLE_SV(cv_clone(cv))));
471 PUSHs(MUTABLE_SV(cv));
485 if (GIMME != G_ARRAY) {
489 *MARK = &PL_sv_undef;
490 *MARK = refto(*MARK);
494 EXTEND_MORTAL(SP - MARK);
496 *MARK = refto(*MARK);
501 S_refto(pTHX_ SV *sv)
506 PERL_ARGS_ASSERT_REFTO;
508 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
511 if (!(sv = LvTARG(sv)))
514 SvREFCNT_inc_void_NN(sv);
516 else if (SvTYPE(sv) == SVt_PVAV) {
517 if (!AvREAL((const AV *)sv) && AvREIFY((const AV *)sv))
518 av_reify(MUTABLE_AV(sv));
520 SvREFCNT_inc_void_NN(sv);
522 else if (SvPADTMP(sv) && !IS_PADGV(sv))
526 SvREFCNT_inc_void_NN(sv);
529 sv_upgrade(rv, SVt_IV);
538 SV * const sv = POPs;
543 if (!sv || !SvROK(sv))
546 (void)sv_ref(TARG,SvRV(sv),TRUE);
558 stash = CopSTASH(PL_curcop);
560 SV * const ssv = POPs;
564 if (!ssv) goto curstash;
565 if (!SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
566 Perl_croak(aTHX_ "Attempt to bless into a reference");
567 ptr = SvPV_const(ssv,len);
569 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
570 "Explicit blessing to '' (assuming package main)");
571 stash = gv_stashpvn(ptr, len, GV_ADD|SvUTF8(ssv));
574 (void)sv_bless(TOPs, stash);
584 const char * const elem = SvPV_const(sv, len);
585 GV * const gv = MUTABLE_GV(POPs);
590 /* elem will always be NUL terminated. */
591 const char * const second_letter = elem + 1;
594 if (len == 5 && strEQ(second_letter, "RRAY"))
595 tmpRef = MUTABLE_SV(GvAV(gv));
598 if (len == 4 && strEQ(second_letter, "ODE"))
599 tmpRef = MUTABLE_SV(GvCVu(gv));
602 if (len == 10 && strEQ(second_letter, "ILEHANDLE")) {
603 /* finally deprecated in 5.8.0 */
604 deprecate("*glob{FILEHANDLE}");
605 tmpRef = MUTABLE_SV(GvIOp(gv));
608 if (len == 6 && strEQ(second_letter, "ORMAT"))
609 tmpRef = MUTABLE_SV(GvFORM(gv));
612 if (len == 4 && strEQ(second_letter, "LOB"))
613 tmpRef = MUTABLE_SV(gv);
616 if (len == 4 && strEQ(second_letter, "ASH"))
617 tmpRef = MUTABLE_SV(GvHV(gv));
620 if (*second_letter == 'O' && !elem[2] && len == 2)
621 tmpRef = MUTABLE_SV(GvIOp(gv));
624 if (len == 4 && strEQ(second_letter, "AME"))
625 sv = newSVhek(GvNAME_HEK(gv));
628 if (len == 7 && strEQ(second_letter, "ACKAGE")) {
629 const HV * const stash = GvSTASH(gv);
630 const HEK * const hek = stash ? HvNAME_HEK(stash) : NULL;
631 sv = hek ? newSVhek(hek) : newSVpvs("__ANON__");
635 if (len == 6 && strEQ(second_letter, "CALAR"))
650 /* Pattern matching */
655 register unsigned char *s;
658 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_study) : NULL;
662 if (mg && SvSCREAM(sv))
665 s = (unsigned char*)(SvPV(sv, len));
666 if (len == 0 || len > I32_MAX || !SvPOK(sv) || SvUTF8(sv) || SvVALID(sv)) {
667 /* No point in studying a zero length string, and not safe to study
668 anything that doesn't appear to be a simple scalar (and hence might
669 change between now and when the regexp engine runs without our set
670 magic ever running) such as a reference to an object with overloaded
671 stringification. Also refuse to study an FBM scalar, as this gives
672 more flexibility in SV flag usage. No real-world code would ever
673 end up studying an FBM scalar, so this isn't a real pessimisation.
674 Endemic use of I32 in Perl_screaminstr makes it hard to safely push
675 the study length limit from I32_MAX to U32_MAX - 1.
682 } else if (len < 0xFFFF) {
687 size = (256 + len) * quanta;
688 sfirst_raw = (char *)safemalloc(size);
691 DIE(aTHX_ "do_study: out of memory");
695 mg = sv_magicext(sv, NULL, PERL_MAGIC_study, &PL_vtbl_regexp, NULL, 0);
696 mg->mg_ptr = sfirst_raw;
698 mg->mg_private = quanta;
700 memset(sfirst_raw, ~0, 256 * quanta);
702 /* The assumption here is that most studied strings are fairly short, hence
703 the pain of the extra code is worth it, given the memory savings.
704 80 character string, 336 bytes as U8, down from 1344 as U32
705 800 character string, 2112 bytes as U16, down from 4224 as U32
709 U8 *const sfirst = (U8 *)sfirst_raw;
710 U8 *const snext = sfirst + 256;
712 const U8 ch = s[len];
713 snext[len] = sfirst[ch];
716 } else if (quanta == 2) {
717 U16 *const sfirst = (U16 *)sfirst_raw;
718 U16 *const snext = sfirst + 256;
720 const U8 ch = s[len];
721 snext[len] = sfirst[ch];
725 U32 *const sfirst = (U32 *)sfirst_raw;
726 U32 *const snext = sfirst + 256;
728 const U8 ch = s[len];
729 snext[len] = sfirst[ch];
742 if (PL_op->op_flags & OPf_STACKED)
744 else if (PL_op->op_private & OPpTARGET_MY)
750 TARG = sv_newmortal();
751 if(PL_op->op_type == OP_TRANSR) {
753 const char * const pv = SvPV(sv,len);
754 SV * const newsv = newSVpvn_flags(pv, len, SVs_TEMP|SvUTF8(sv));
758 else PUSHi(do_trans(sv));
762 /* Lvalue operators. */
765 S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
771 PERL_ARGS_ASSERT_DO_CHOMP;
773 if (chomping && (RsSNARF(PL_rs) || RsRECORD(PL_rs)))
775 if (SvTYPE(sv) == SVt_PVAV) {
777 AV *const av = MUTABLE_AV(sv);
778 const I32 max = AvFILL(av);
780 for (i = 0; i <= max; i++) {
781 sv = MUTABLE_SV(av_fetch(av, i, FALSE));
782 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
783 do_chomp(retval, sv, chomping);
787 else if (SvTYPE(sv) == SVt_PVHV) {
788 HV* const hv = MUTABLE_HV(sv);
790 (void)hv_iterinit(hv);
791 while ((entry = hv_iternext(hv)))
792 do_chomp(retval, hv_iterval(hv,entry), chomping);
795 else if (SvREADONLY(sv)) {
797 /* SV is copy-on-write */
798 sv_force_normal_flags(sv, 0);
801 Perl_croak_no_modify(aTHX);
806 /* XXX, here sv is utf8-ized as a side-effect!
807 If encoding.pm is used properly, almost string-generating
808 operations, including literal strings, chr(), input data, etc.
809 should have been utf8-ized already, right?
811 sv_recode_to_utf8(sv, PL_encoding);
817 char *temp_buffer = NULL;
826 while (len && s[-1] == '\n') {
833 STRLEN rslen, rs_charlen;
834 const char *rsptr = SvPV_const(PL_rs, rslen);
836 rs_charlen = SvUTF8(PL_rs)
840 if (SvUTF8(PL_rs) != SvUTF8(sv)) {
841 /* Assumption is that rs is shorter than the scalar. */
843 /* RS is utf8, scalar is 8 bit. */
845 temp_buffer = (char*)bytes_from_utf8((U8*)rsptr,
848 /* Cannot downgrade, therefore cannot possibly match
850 assert (temp_buffer == rsptr);
856 else if (PL_encoding) {
857 /* RS is 8 bit, encoding.pm is used.
858 * Do not recode PL_rs as a side-effect. */
859 svrecode = newSVpvn(rsptr, rslen);
860 sv_recode_to_utf8(svrecode, PL_encoding);
861 rsptr = SvPV_const(svrecode, rslen);
862 rs_charlen = sv_len_utf8(svrecode);
865 /* RS is 8 bit, scalar is utf8. */
866 temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen);
880 if (memNE(s, rsptr, rslen))
882 SvIVX(retval) += rs_charlen;
885 s = SvPV_force_nomg_nolen(sv);
893 SvREFCNT_dec(svrecode);
895 Safefree(temp_buffer);
897 if (len && !SvPOK(sv))
898 s = SvPV_force_nomg(sv, len);
901 char * const send = s + len;
902 char * const start = s;
904 while (s > start && UTF8_IS_CONTINUATION(*s))
906 if (is_utf8_string((U8*)s, send - s)) {
907 sv_setpvn(retval, s, send - s);
909 SvCUR_set(sv, s - start);
915 sv_setpvs(retval, "");
919 sv_setpvn(retval, s, 1);
926 sv_setpvs(retval, "");
934 const bool chomping = PL_op->op_type == OP_SCHOMP;
938 do_chomp(TARG, TOPs, chomping);
945 dVAR; dSP; dMARK; dTARGET; dORIGMARK;
946 const bool chomping = PL_op->op_type == OP_CHOMP;
951 do_chomp(TARG, *++MARK, chomping);
962 if (!PL_op->op_private) {
971 SV_CHECK_THINKFIRST_COW_DROP(sv);
973 switch (SvTYPE(sv)) {
977 av_undef(MUTABLE_AV(sv));
980 hv_undef(MUTABLE_HV(sv));
983 if (cv_const_sv((const CV *)sv))
984 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
985 "Constant subroutine %"SVf" undefined",
986 SVfARG(CvANON((const CV *)sv)
987 ? newSVpvs_flags("(anonymous)", SVs_TEMP)
988 : sv_2mortal(newSVhek(GvENAME_HEK(CvGV((const CV *)sv))))));
992 /* let user-undef'd sub keep its identity */
993 GV* const gv = CvGV((const CV *)sv);
994 cv_undef(MUTABLE_CV(sv));
995 CvGV_set(MUTABLE_CV(sv), gv);
1000 SvSetMagicSV(sv, &PL_sv_undef);
1003 else if (isGV_with_GP(sv)) {
1007 /* undef *Pkg::meth_name ... */
1009 = GvCVu((const GV *)sv) && (stash = GvSTASH((const GV *)sv))
1010 && HvENAME_get(stash);
1012 if((stash = GvHV((const GV *)sv))) {
1013 if(HvENAME_get(stash))
1014 SvREFCNT_inc_simple_void_NN(sv_2mortal((SV *)stash));
1018 gp_free(MUTABLE_GV(sv));
1020 GvGP_set(sv, gp_ref(gp));
1021 GvSV(sv) = newSV(0);
1022 GvLINE(sv) = CopLINE(PL_curcop);
1023 GvEGV(sv) = MUTABLE_GV(sv);
1027 mro_package_moved(NULL, stash, (const GV *)sv, 0);
1029 /* undef *Foo::ISA */
1030 if( strEQ(GvNAME((const GV *)sv), "ISA")
1031 && (stash = GvSTASH((const GV *)sv))
1032 && (method_changed || HvENAME(stash)) )
1033 mro_isa_changed_in(stash);
1034 else if(method_changed)
1035 mro_method_changed_in(
1036 GvSTASH((const GV *)sv)
1043 if (SvTYPE(sv) >= SVt_PV && SvPVX_const(sv) && SvLEN(sv)) {
1059 PL_op->op_type == OP_POSTINC || PL_op->op_type == OP_I_POSTINC;
1060 if (SvTYPE(TOPs) >= SVt_PVAV || (isGV_with_GP(TOPs) && !SvFAKE(TOPs)))
1061 Perl_croak_no_modify(aTHX);
1063 TARG = sv_newmortal();
1064 sv_setsv(TARG, TOPs);
1065 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
1066 && SvIVX(TOPs) != (inc ? IV_MAX : IV_MIN))
1068 SvIV_set(TOPs, SvIVX(TOPs) + (inc ? 1 : -1));
1069 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
1073 else sv_dec_nomg(TOPs);
1075 /* special case for undef: see thread at 2003-03/msg00536.html in archive */
1076 if (inc && !SvOK(TARG))
1082 /* Ordinary operators. */
1086 dVAR; dSP; dATARGET; SV *svl, *svr;
1087 #ifdef PERL_PRESERVE_IVUV
1090 tryAMAGICbin_MG(pow_amg, AMGf_assign|AMGf_numeric);
1093 #ifdef PERL_PRESERVE_IVUV
1094 /* For integer to integer power, we do the calculation by hand wherever
1095 we're sure it is safe; otherwise we call pow() and try to convert to
1096 integer afterwards. */
1098 SvIV_please_nomg(svr);
1100 SvIV_please_nomg(svl);
1109 const IV iv = SvIVX(svr);
1113 goto float_it; /* Can't do negative powers this way. */
1117 baseuok = SvUOK(svl);
1119 baseuv = SvUVX(svl);
1121 const IV iv = SvIVX(svl);
1124 baseuok = TRUE; /* effectively it's a UV now */
1126 baseuv = -iv; /* abs, baseuok == false records sign */
1129 /* now we have integer ** positive integer. */
1132 /* foo & (foo - 1) is zero only for a power of 2. */
1133 if (!(baseuv & (baseuv - 1))) {
1134 /* We are raising power-of-2 to a positive integer.
1135 The logic here will work for any base (even non-integer
1136 bases) but it can be less accurate than
1137 pow (base,power) or exp (power * log (base)) when the
1138 intermediate values start to spill out of the mantissa.
1139 With powers of 2 we know this can't happen.
1140 And powers of 2 are the favourite thing for perl
1141 programmers to notice ** not doing what they mean. */
1143 NV base = baseuok ? baseuv : -(NV)baseuv;
1148 while (power >>= 1) {
1156 SvIV_please_nomg(svr);
1159 register unsigned int highbit = 8 * sizeof(UV);
1160 register unsigned int diff = 8 * sizeof(UV);
1161 while (diff >>= 1) {
1163 if (baseuv >> highbit) {
1167 /* we now have baseuv < 2 ** highbit */
1168 if (power * highbit <= 8 * sizeof(UV)) {
1169 /* result will definitely fit in UV, so use UV math
1170 on same algorithm as above */
1171 register UV result = 1;
1172 register UV base = baseuv;
1173 const bool odd_power = cBOOL(power & 1);
1177 while (power >>= 1) {
1184 if (baseuok || !odd_power)
1185 /* answer is positive */
1187 else if (result <= (UV)IV_MAX)
1188 /* answer negative, fits in IV */
1189 SETi( -(IV)result );
1190 else if (result == (UV)IV_MIN)
1191 /* 2's complement assumption: special case IV_MIN */
1194 /* answer negative, doesn't fit */
1195 SETn( -(NV)result );
1205 NV right = SvNV_nomg(svr);
1206 NV left = SvNV_nomg(svl);
1209 #if defined(USE_LONG_DOUBLE) && defined(HAS_AIX_POWL_NEG_BASE_BUG)
1211 We are building perl with long double support and are on an AIX OS
1212 afflicted with a powl() function that wrongly returns NaNQ for any
1213 negative base. This was reported to IBM as PMR #23047-379 on
1214 03/06/2006. The problem exists in at least the following versions
1215 of AIX and the libm fileset, and no doubt others as well:
1217 AIX 4.3.3-ML10 bos.adt.libm 4.3.3.50
1218 AIX 5.1.0-ML04 bos.adt.libm 5.1.0.29
1219 AIX 5.2.0 bos.adt.libm 5.2.0.85
1221 So, until IBM fixes powl(), we provide the following workaround to
1222 handle the problem ourselves. Our logic is as follows: for
1223 negative bases (left), we use fmod(right, 2) to check if the
1224 exponent is an odd or even integer:
1226 - if odd, powl(left, right) == -powl(-left, right)
1227 - if even, powl(left, right) == powl(-left, right)
1229 If the exponent is not an integer, the result is rightly NaNQ, so
1230 we just return that (as NV_NAN).
1234 NV mod2 = Perl_fmod( right, 2.0 );
1235 if (mod2 == 1.0 || mod2 == -1.0) { /* odd integer */
1236 SETn( -Perl_pow( -left, right) );
1237 } else if (mod2 == 0.0) { /* even integer */
1238 SETn( Perl_pow( -left, right) );
1239 } else { /* fractional power */
1243 SETn( Perl_pow( left, right) );
1246 SETn( Perl_pow( left, right) );
1247 #endif /* HAS_AIX_POWL_NEG_BASE_BUG */
1249 #ifdef PERL_PRESERVE_IVUV
1251 SvIV_please_nomg(svr);
1259 dVAR; dSP; dATARGET; SV *svl, *svr;
1260 tryAMAGICbin_MG(mult_amg, AMGf_assign|AMGf_numeric);
1263 #ifdef PERL_PRESERVE_IVUV
1264 SvIV_please_nomg(svr);
1266 /* Unless the left argument is integer in range we are going to have to
1267 use NV maths. Hence only attempt to coerce the right argument if
1268 we know the left is integer. */
1269 /* Left operand is defined, so is it IV? */
1270 SvIV_please_nomg(svl);
1272 bool auvok = SvUOK(svl);
1273 bool buvok = SvUOK(svr);
1274 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
1275 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
1284 const IV aiv = SvIVX(svl);
1287 auvok = TRUE; /* effectively it's a UV now */
1289 alow = -aiv; /* abs, auvok == false records sign */
1295 const IV biv = SvIVX(svr);
1298 buvok = TRUE; /* effectively it's a UV now */
1300 blow = -biv; /* abs, buvok == false records sign */
1304 /* If this does sign extension on unsigned it's time for plan B */
1305 ahigh = alow >> (4 * sizeof (UV));
1307 bhigh = blow >> (4 * sizeof (UV));
1309 if (ahigh && bhigh) {
1311 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1312 which is overflow. Drop to NVs below. */
1313 } else if (!ahigh && !bhigh) {
1314 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1315 so the unsigned multiply cannot overflow. */
1316 const UV product = alow * blow;
1317 if (auvok == buvok) {
1318 /* -ve * -ve or +ve * +ve gives a +ve result. */
1322 } else if (product <= (UV)IV_MIN) {
1323 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1324 /* -ve result, which could overflow an IV */
1326 SETi( -(IV)product );
1328 } /* else drop to NVs below. */
1330 /* One operand is large, 1 small */
1333 /* swap the operands */
1335 bhigh = blow; /* bhigh now the temp var for the swap */
1339 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1340 multiplies can't overflow. shift can, add can, -ve can. */
1341 product_middle = ahigh * blow;
1342 if (!(product_middle & topmask)) {
1343 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1345 product_middle <<= (4 * sizeof (UV));
1346 product_low = alow * blow;
1348 /* as for pp_add, UV + something mustn't get smaller.
1349 IIRC ANSI mandates this wrapping *behaviour* for
1350 unsigned whatever the actual representation*/
1351 product_low += product_middle;
1352 if (product_low >= product_middle) {
1353 /* didn't overflow */
1354 if (auvok == buvok) {
1355 /* -ve * -ve or +ve * +ve gives a +ve result. */
1357 SETu( product_low );
1359 } else if (product_low <= (UV)IV_MIN) {
1360 /* 2s complement assumption again */
1361 /* -ve result, which could overflow an IV */
1363 SETi( -(IV)product_low );
1365 } /* else drop to NVs below. */
1367 } /* product_middle too large */
1368 } /* ahigh && bhigh */
1373 NV right = SvNV_nomg(svr);
1374 NV left = SvNV_nomg(svl);
1376 SETn( left * right );
1383 dVAR; dSP; dATARGET; SV *svl, *svr;
1384 tryAMAGICbin_MG(div_amg, AMGf_assign|AMGf_numeric);
1387 /* Only try to do UV divide first
1388 if ((SLOPPYDIVIDE is true) or
1389 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1391 The assumption is that it is better to use floating point divide
1392 whenever possible, only doing integer divide first if we can't be sure.
1393 If NV_PRESERVES_UV is true then we know at compile time that no UV
1394 can be too large to preserve, so don't need to compile the code to
1395 test the size of UVs. */
1398 # define PERL_TRY_UV_DIVIDE
1399 /* ensure that 20./5. == 4. */
1401 # ifdef PERL_PRESERVE_IVUV
1402 # ifndef NV_PRESERVES_UV
1403 # define PERL_TRY_UV_DIVIDE
1408 #ifdef PERL_TRY_UV_DIVIDE
1409 SvIV_please_nomg(svr);
1411 SvIV_please_nomg(svl);
1413 bool left_non_neg = SvUOK(svl);
1414 bool right_non_neg = SvUOK(svr);
1418 if (right_non_neg) {
1422 const IV biv = SvIVX(svr);
1425 right_non_neg = TRUE; /* effectively it's a UV now */
1431 /* historically undef()/0 gives a "Use of uninitialized value"
1432 warning before dieing, hence this test goes here.
1433 If it were immediately before the second SvIV_please, then
1434 DIE() would be invoked before left was even inspected, so
1435 no inspection would give no warning. */
1437 DIE(aTHX_ "Illegal division by zero");
1443 const IV aiv = SvIVX(svl);
1446 left_non_neg = TRUE; /* effectively it's a UV now */
1455 /* For sloppy divide we always attempt integer division. */
1457 /* Otherwise we only attempt it if either or both operands
1458 would not be preserved by an NV. If both fit in NVs
1459 we fall through to the NV divide code below. However,
1460 as left >= right to ensure integer result here, we know that
1461 we can skip the test on the right operand - right big
1462 enough not to be preserved can't get here unless left is
1465 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1468 /* Integer division can't overflow, but it can be imprecise. */
1469 const UV result = left / right;
1470 if (result * right == left) {
1471 SP--; /* result is valid */
1472 if (left_non_neg == right_non_neg) {
1473 /* signs identical, result is positive. */
1477 /* 2s complement assumption */
1478 if (result <= (UV)IV_MIN)
1479 SETi( -(IV)result );
1481 /* It's exact but too negative for IV. */
1482 SETn( -(NV)result );
1485 } /* tried integer divide but it was not an integer result */
1486 } /* else (PERL_ABS(result) < 1.0) or (both UVs in range for NV) */
1487 } /* left wasn't SvIOK */
1488 } /* right wasn't SvIOK */
1489 #endif /* PERL_TRY_UV_DIVIDE */
1491 NV right = SvNV_nomg(svr);
1492 NV left = SvNV_nomg(svl);
1493 (void)POPs;(void)POPs;
1494 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1495 if (! Perl_isnan(right) && right == 0.0)
1499 DIE(aTHX_ "Illegal division by zero");
1500 PUSHn( left / right );
1507 dVAR; dSP; dATARGET;
1508 tryAMAGICbin_MG(modulo_amg, AMGf_assign|AMGf_numeric);
1512 bool left_neg = FALSE;
1513 bool right_neg = FALSE;
1514 bool use_double = FALSE;
1515 bool dright_valid = FALSE;
1518 SV * const svr = TOPs;
1519 SV * const svl = TOPm1s;
1520 SvIV_please_nomg(svr);
1522 right_neg = !SvUOK(svr);
1526 const IV biv = SvIVX(svr);
1529 right_neg = FALSE; /* effectively it's a UV now */
1536 dright = SvNV_nomg(svr);
1537 right_neg = dright < 0;
1540 if (dright < UV_MAX_P1) {
1541 right = U_V(dright);
1542 dright_valid = TRUE; /* In case we need to use double below. */
1548 /* At this point use_double is only true if right is out of range for
1549 a UV. In range NV has been rounded down to nearest UV and
1550 use_double false. */
1551 SvIV_please_nomg(svl);
1552 if (!use_double && SvIOK(svl)) {
1554 left_neg = !SvUOK(svl);
1558 const IV aiv = SvIVX(svl);
1561 left_neg = FALSE; /* effectively it's a UV now */
1569 dleft = SvNV_nomg(svl);
1570 left_neg = dleft < 0;
1574 /* This should be exactly the 5.6 behaviour - if left and right are
1575 both in range for UV then use U_V() rather than floor. */
1577 if (dleft < UV_MAX_P1) {
1578 /* right was in range, so is dleft, so use UVs not double.
1582 /* left is out of range for UV, right was in range, so promote
1583 right (back) to double. */
1585 /* The +0.5 is used in 5.6 even though it is not strictly
1586 consistent with the implicit +0 floor in the U_V()
1587 inside the #if 1. */
1588 dleft = Perl_floor(dleft + 0.5);
1591 dright = Perl_floor(dright + 0.5);
1602 DIE(aTHX_ "Illegal modulus zero");
1604 dans = Perl_fmod(dleft, dright);
1605 if ((left_neg != right_neg) && dans)
1606 dans = dright - dans;
1609 sv_setnv(TARG, dans);
1615 DIE(aTHX_ "Illegal modulus zero");
1618 if ((left_neg != right_neg) && ans)
1621 /* XXX may warn: unary minus operator applied to unsigned type */
1622 /* could change -foo to be (~foo)+1 instead */
1623 if (ans <= ~((UV)IV_MAX)+1)
1624 sv_setiv(TARG, ~ans+1);
1626 sv_setnv(TARG, -(NV)ans);
1629 sv_setuv(TARG, ans);
1638 dVAR; dSP; dATARGET;
1642 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1643 /* TODO: think of some way of doing list-repeat overloading ??? */
1648 tryAMAGICbin_MG(repeat_amg, AMGf_assign);
1654 const UV uv = SvUV_nomg(sv);
1656 count = IV_MAX; /* The best we can do? */
1660 const IV iv = SvIV_nomg(sv);
1667 else if (SvNOKp(sv)) {
1668 const NV nv = SvNV_nomg(sv);
1675 count = SvIV_nomg(sv);
1677 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1679 static const char oom_list_extend[] = "Out of memory during list extend";
1680 const I32 items = SP - MARK;
1681 const I32 max = items * count;
1683 MEM_WRAP_CHECK_1(max, SV*, oom_list_extend);
1684 /* Did the max computation overflow? */
1685 if (items > 0 && max > 0 && (max < items || max < count))
1686 Perl_croak(aTHX_ oom_list_extend);
1691 /* This code was intended to fix 20010809.028:
1694 for (($x =~ /./g) x 2) {
1695 print chop; # "abcdabcd" expected as output.
1698 * but that change (#11635) broke this code:
1700 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1702 * I can't think of a better fix that doesn't introduce
1703 * an efficiency hit by copying the SVs. The stack isn't
1704 * refcounted, and mortalisation obviously doesn't
1705 * Do The Right Thing when the stack has more than
1706 * one pointer to the same mortal value.
1710 *SP = sv_2mortal(newSVsv(*SP));
1720 repeatcpy((char*)(MARK + items), (char*)MARK,
1721 items * sizeof(const SV *), count - 1);
1724 else if (count <= 0)
1727 else { /* Note: mark already snarfed by pp_list */
1728 SV * const tmpstr = POPs;
1731 static const char oom_string_extend[] =
1732 "Out of memory during string extend";
1735 sv_setsv_nomg(TARG, tmpstr);
1736 SvPV_force_nomg(TARG, len);
1737 isutf = DO_UTF8(TARG);
1742 const STRLEN max = (UV)count * len;
1743 if (len > MEM_SIZE_MAX / count)
1744 Perl_croak(aTHX_ oom_string_extend);
1745 MEM_WRAP_CHECK_1(max, char, oom_string_extend);
1746 SvGROW(TARG, max + 1);
1747 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1748 SvCUR_set(TARG, SvCUR(TARG) * count);
1750 *SvEND(TARG) = '\0';
1753 (void)SvPOK_only_UTF8(TARG);
1755 (void)SvPOK_only(TARG);
1757 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1758 /* The parser saw this as a list repeat, and there
1759 are probably several items on the stack. But we're
1760 in scalar context, and there's no pp_list to save us
1761 now. So drop the rest of the items -- robin@kitsite.com
1773 dVAR; dSP; dATARGET; bool useleft; SV *svl, *svr;
1774 tryAMAGICbin_MG(subtr_amg, AMGf_assign|AMGf_numeric);
1777 useleft = USE_LEFT(svl);
1778 #ifdef PERL_PRESERVE_IVUV
1779 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1780 "bad things" happen if you rely on signed integers wrapping. */
1781 SvIV_please_nomg(svr);
1783 /* Unless the left argument is integer in range we are going to have to
1784 use NV maths. Hence only attempt to coerce the right argument if
1785 we know the left is integer. */
1786 register UV auv = 0;
1792 a_valid = auvok = 1;
1793 /* left operand is undef, treat as zero. */
1795 /* Left operand is defined, so is it IV? */
1796 SvIV_please_nomg(svl);
1798 if ((auvok = SvUOK(svl)))
1801 register const IV aiv = SvIVX(svl);
1804 auvok = 1; /* Now acting as a sign flag. */
1805 } else { /* 2s complement assumption for IV_MIN */
1813 bool result_good = 0;
1816 bool buvok = SvUOK(svr);
1821 register const IV biv = SvIVX(svr);
1828 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1829 else "IV" now, independent of how it came in.
1830 if a, b represents positive, A, B negative, a maps to -A etc
1835 all UV maths. negate result if A negative.
1836 subtract if signs same, add if signs differ. */
1838 if (auvok ^ buvok) {
1847 /* Must get smaller */
1852 if (result <= buv) {
1853 /* result really should be -(auv-buv). as its negation
1854 of true value, need to swap our result flag */
1866 if (result <= (UV)IV_MIN)
1867 SETi( -(IV)result );
1869 /* result valid, but out of range for IV. */
1870 SETn( -(NV)result );
1874 } /* Overflow, drop through to NVs. */
1879 NV value = SvNV_nomg(svr);
1883 /* left operand is undef, treat as zero - value */
1887 SETn( SvNV_nomg(svl) - value );
1894 dVAR; dSP; dATARGET; SV *svl, *svr;
1895 tryAMAGICbin_MG(lshift_amg, AMGf_assign|AMGf_numeric);
1899 const IV shift = SvIV_nomg(svr);
1900 if (PL_op->op_private & HINT_INTEGER) {
1901 const IV i = SvIV_nomg(svl);
1905 const UV u = SvUV_nomg(svl);
1914 dVAR; dSP; dATARGET; SV *svl, *svr;
1915 tryAMAGICbin_MG(rshift_amg, AMGf_assign|AMGf_numeric);
1919 const IV shift = SvIV_nomg(svr);
1920 if (PL_op->op_private & HINT_INTEGER) {
1921 const IV i = SvIV_nomg(svl);
1925 const UV u = SvUV_nomg(svl);
1937 tryAMAGICbin_MG(lt_amg, AMGf_set|AMGf_numeric);
1941 (SvIOK_notUV(left) && SvIOK_notUV(right))
1942 ? (SvIVX(left) < SvIVX(right))
1943 : (do_ncmp(left, right) == -1)
1953 tryAMAGICbin_MG(gt_amg, AMGf_set|AMGf_numeric);
1957 (SvIOK_notUV(left) && SvIOK_notUV(right))
1958 ? (SvIVX(left) > SvIVX(right))
1959 : (do_ncmp(left, right) == 1)
1969 tryAMAGICbin_MG(le_amg, AMGf_set|AMGf_numeric);
1973 (SvIOK_notUV(left) && SvIOK_notUV(right))
1974 ? (SvIVX(left) <= SvIVX(right))
1975 : (do_ncmp(left, right) <= 0)
1985 tryAMAGICbin_MG(ge_amg, AMGf_set|AMGf_numeric);
1989 (SvIOK_notUV(left) && SvIOK_notUV(right))
1990 ? (SvIVX(left) >= SvIVX(right))
1991 : ( (do_ncmp(left, right) & 2) == 0)
2001 tryAMAGICbin_MG(ne_amg, AMGf_set|AMGf_numeric);
2005 (SvIOK_notUV(left) && SvIOK_notUV(right))
2006 ? (SvIVX(left) != SvIVX(right))
2007 : (do_ncmp(left, right) != 0)
2012 /* compare left and right SVs. Returns:
2016 * 2: left or right was a NaN
2019 Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
2023 PERL_ARGS_ASSERT_DO_NCMP;
2024 #ifdef PERL_PRESERVE_IVUV
2025 SvIV_please_nomg(right);
2026 /* Fortunately it seems NaN isn't IOK */
2028 SvIV_please_nomg(left);
2031 const IV leftiv = SvIVX(left);
2032 if (!SvUOK(right)) {
2033 /* ## IV <=> IV ## */
2034 const IV rightiv = SvIVX(right);
2035 return (leftiv > rightiv) - (leftiv < rightiv);
2037 /* ## IV <=> UV ## */
2039 /* As (b) is a UV, it's >=0, so it must be < */
2042 const UV rightuv = SvUVX(right);
2043 return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
2048 /* ## UV <=> UV ## */
2049 const UV leftuv = SvUVX(left);
2050 const UV rightuv = SvUVX(right);
2051 return (leftuv > rightuv) - (leftuv < rightuv);
2053 /* ## UV <=> IV ## */
2055 const IV rightiv = SvIVX(right);
2057 /* As (a) is a UV, it's >=0, so it cannot be < */
2060 const UV leftuv = SvUVX(left);
2061 return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
2069 NV const rnv = SvNV_nomg(right);
2070 NV const lnv = SvNV_nomg(left);
2072 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2073 if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
2076 return (lnv > rnv) - (lnv < rnv);
2095 tryAMAGICbin_MG(ncmp_amg, AMGf_numeric);
2098 value = do_ncmp(left, right);
2113 int amg_type = sle_amg;
2117 switch (PL_op->op_type) {
2136 tryAMAGICbin_MG(amg_type, AMGf_set);
2139 const int cmp = (IN_LOCALE_RUNTIME
2140 ? sv_cmp_locale_flags(left, right, 0)
2141 : sv_cmp_flags(left, right, 0));
2142 SETs(boolSV(cmp * multiplier < rhs));
2150 tryAMAGICbin_MG(seq_amg, AMGf_set);
2153 SETs(boolSV(sv_eq_flags(left, right, 0)));
2161 tryAMAGICbin_MG(sne_amg, AMGf_set);
2164 SETs(boolSV(!sv_eq_flags(left, right, 0)));
2172 tryAMAGICbin_MG(scmp_amg, 0);
2175 const int cmp = (IN_LOCALE_RUNTIME
2176 ? sv_cmp_locale_flags(left, right, 0)
2177 : sv_cmp_flags(left, right, 0));
2185 dVAR; dSP; dATARGET;
2186 tryAMAGICbin_MG(band_amg, AMGf_assign);
2189 if (SvNIOKp(left) || SvNIOKp(right)) {
2190 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2191 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2192 if (PL_op->op_private & HINT_INTEGER) {
2193 const IV i = SvIV_nomg(left) & SvIV_nomg(right);
2197 const UV u = SvUV_nomg(left) & SvUV_nomg(right);
2200 if (left_ro_nonnum) SvNIOK_off(left);
2201 if (right_ro_nonnum) SvNIOK_off(right);
2204 do_vop(PL_op->op_type, TARG, left, right);
2213 dVAR; dSP; dATARGET;
2214 const int op_type = PL_op->op_type;
2216 tryAMAGICbin_MG((op_type == OP_BIT_OR ? bor_amg : bxor_amg), AMGf_assign);
2219 if (SvNIOKp(left) || SvNIOKp(right)) {
2220 const bool left_ro_nonnum = !SvNIOKp(left) && SvREADONLY(left);
2221 const bool right_ro_nonnum = !SvNIOKp(right) && SvREADONLY(right);
2222 if (PL_op->op_private & HINT_INTEGER) {
2223 const IV l = (USE_LEFT(left) ? SvIV_nomg(left) : 0);
2224 const IV r = SvIV_nomg(right);
2225 const IV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2229 const UV l = (USE_LEFT(left) ? SvUV_nomg(left) : 0);
2230 const UV r = SvUV_nomg(right);
2231 const UV result = op_type == OP_BIT_OR ? (l | r) : (l ^ r);
2234 if (left_ro_nonnum) SvNIOK_off(left);
2235 if (right_ro_nonnum) SvNIOK_off(right);
2238 do_vop(op_type, TARG, left, right);
2248 tryAMAGICun_MG(neg_amg, AMGf_numeric);
2250 SV * const sv = TOPs;
2251 const int flags = SvFLAGS(sv);
2253 if( !SvNIOK( sv ) && looks_like_number( sv ) ){
2257 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2258 /* It's publicly an integer, or privately an integer-not-float */
2261 if (SvIVX(sv) == IV_MIN) {
2262 /* 2s complement assumption. */
2263 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2266 else if (SvUVX(sv) <= IV_MAX) {
2271 else if (SvIVX(sv) != IV_MIN) {
2275 #ifdef PERL_PRESERVE_IVUV
2283 SETn(-SvNV_nomg(sv));
2284 else if (SvPOKp(sv)) {
2286 const char * const s = SvPV_nomg_const(sv, len);
2287 if (isIDFIRST(*s)) {
2288 sv_setpvs(TARG, "-");
2291 else if (*s == '+' || *s == '-') {
2292 sv_setsv_nomg(TARG, sv);
2293 *SvPV_force_nomg(TARG, len) = *s == '-' ? '+' : '-';
2295 else if (DO_UTF8(sv)) {
2296 SvIV_please_nomg(sv);
2298 goto oops_its_an_int;
2300 sv_setnv(TARG, -SvNV_nomg(sv));
2302 sv_setpvs(TARG, "-");
2307 SvIV_please_nomg(sv);
2309 goto oops_its_an_int;
2310 sv_setnv(TARG, -SvNV_nomg(sv));
2315 SETn(-SvNV_nomg(sv));
2323 tryAMAGICun_MG(not_amg, AMGf_set);
2324 *PL_stack_sp = boolSV(!SvTRUE_nomg(*PL_stack_sp));
2331 tryAMAGICun_MG(compl_amg, AMGf_numeric);
2335 if (PL_op->op_private & HINT_INTEGER) {
2336 const IV i = ~SvIV_nomg(sv);
2340 const UV u = ~SvUV_nomg(sv);
2349 (void)SvPV_nomg_const(sv,len); /* force check for uninit var */
2350 sv_setsv_nomg(TARG, sv);
2351 tmps = (U8*)SvPV_force_nomg(TARG, len);
2354 /* Calculate exact length, let's not estimate. */
2359 U8 * const send = tmps + len;
2360 U8 * const origtmps = tmps;
2361 const UV utf8flags = UTF8_ALLOW_ANYUV;
2363 while (tmps < send) {
2364 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2366 targlen += UNISKIP(~c);
2372 /* Now rewind strings and write them. */
2379 Newx(result, targlen + 1, U8);
2381 while (tmps < send) {
2382 const UV c = utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2384 p = uvchr_to_utf8_flags(p, ~c, UNICODE_ALLOW_ANY);
2387 sv_usepvn_flags(TARG, (char*)result, targlen,
2388 SV_HAS_TRAILING_NUL);
2395 Newx(result, nchar + 1, U8);
2397 while (tmps < send) {
2398 const U8 c = (U8)utf8n_to_uvchr(tmps, send-tmps, &l, utf8flags);
2403 sv_usepvn_flags(TARG, (char*)result, nchar, SV_HAS_TRAILING_NUL);
2411 register long *tmpl;
2412 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2415 for ( ; anum >= (I32)sizeof(long); anum -= (I32)sizeof(long), tmpl++)
2420 for ( ; anum > 0; anum--, tmps++)
2428 /* integer versions of some of the above */
2432 dVAR; dSP; dATARGET;
2433 tryAMAGICbin_MG(mult_amg, AMGf_assign);
2436 SETi( left * right );
2444 dVAR; dSP; dATARGET;
2445 tryAMAGICbin_MG(div_amg, AMGf_assign);
2448 IV value = SvIV_nomg(right);
2450 DIE(aTHX_ "Illegal division by zero");
2451 num = SvIV_nomg(left);
2453 /* avoid FPE_INTOVF on some platforms when num is IV_MIN */
2457 value = num / value;
2463 #if defined(__GLIBC__) && IVSIZE == 8
2470 /* This is the vanilla old i_modulo. */
2471 dVAR; dSP; dATARGET;
2472 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2476 DIE(aTHX_ "Illegal modulus zero");
2477 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2481 SETi( left % right );
2486 #if defined(__GLIBC__) && IVSIZE == 8
2491 /* This is the i_modulo with the workaround for the _moddi3 bug
2492 * in (at least) glibc 2.2.5 (the PERL_ABS() the workaround).
2493 * See below for pp_i_modulo. */
2494 dVAR; dSP; dATARGET;
2495 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2499 DIE(aTHX_ "Illegal modulus zero");
2500 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2504 SETi( left % PERL_ABS(right) );
2511 dVAR; dSP; dATARGET;
2512 tryAMAGICbin_MG(modulo_amg, AMGf_assign);
2516 DIE(aTHX_ "Illegal modulus zero");
2517 /* The assumption is to use hereafter the old vanilla version... */
2519 PL_ppaddr[OP_I_MODULO] =
2521 /* .. but if we have glibc, we might have a buggy _moddi3
2522 * (at least glicb 2.2.5 is known to have this bug), in other
2523 * words our integer modulus with negative quad as the second
2524 * argument might be broken. Test for this and re-patch the
2525 * opcode dispatch table if that is the case, remembering to
2526 * also apply the workaround so that this first round works
2527 * right, too. See [perl #9402] for more information. */
2531 /* Cannot do this check with inlined IV constants since
2532 * that seems to work correctly even with the buggy glibc. */
2534 /* Yikes, we have the bug.
2535 * Patch in the workaround version. */
2537 PL_ppaddr[OP_I_MODULO] =
2538 &Perl_pp_i_modulo_1;
2539 /* Make certain we work right this time, too. */
2540 right = PERL_ABS(right);
2543 /* avoid FPE_INTOVF on some platforms when left is IV_MIN */
2547 SETi( left % right );
2555 dVAR; dSP; dATARGET;
2556 tryAMAGICbin_MG(add_amg, AMGf_assign);
2558 dPOPTOPiirl_ul_nomg;
2559 SETi( left + right );
2566 dVAR; dSP; dATARGET;
2567 tryAMAGICbin_MG(subtr_amg, AMGf_assign);
2569 dPOPTOPiirl_ul_nomg;
2570 SETi( left - right );
2578 tryAMAGICbin_MG(lt_amg, AMGf_set);
2581 SETs(boolSV(left < right));
2589 tryAMAGICbin_MG(gt_amg, AMGf_set);
2592 SETs(boolSV(left > right));
2600 tryAMAGICbin_MG(le_amg, AMGf_set);
2603 SETs(boolSV(left <= right));
2611 tryAMAGICbin_MG(ge_amg, AMGf_set);
2614 SETs(boolSV(left >= right));
2622 tryAMAGICbin_MG(eq_amg, AMGf_set);
2625 SETs(boolSV(left == right));
2633 tryAMAGICbin_MG(ne_amg, AMGf_set);
2636 SETs(boolSV(left != right));
2644 tryAMAGICbin_MG(ncmp_amg, 0);
2651 else if (left < right)
2663 tryAMAGICun_MG(neg_amg, 0);
2665 SV * const sv = TOPs;
2666 IV const i = SvIV_nomg(sv);
2672 /* High falutin' math. */
2677 tryAMAGICbin_MG(atan2_amg, 0);
2680 SETn(Perl_atan2(left, right));
2688 int amg_type = sin_amg;
2689 const char *neg_report = NULL;
2690 NV (*func)(NV) = Perl_sin;
2691 const int op_type = PL_op->op_type;
2708 amg_type = sqrt_amg;
2710 neg_report = "sqrt";
2715 tryAMAGICun_MG(amg_type, 0);
2717 SV * const arg = POPs;
2718 const NV value = SvNV_nomg(arg);
2720 if (op_type == OP_LOG ? (value <= 0.0) : (value < 0.0)) {
2721 SET_NUMERIC_STANDARD();
2722 DIE(aTHX_ "Can't take %s of %"NVgf, neg_report, value);
2725 XPUSHn(func(value));
2730 /* Support Configure command-line overrides for rand() functions.
2731 After 5.005, perhaps we should replace this by Configure support
2732 for drand48(), random(), or rand(). For 5.005, though, maintain
2733 compatibility by calling rand() but allow the user to override it.
2734 See INSTALL for details. --Andy Dougherty 15 July 1998
2736 /* Now it's after 5.005, and Configure supports drand48() and random(),
2737 in addition to rand(). So the overrides should not be needed any more.
2738 --Jarkko Hietaniemi 27 September 1998
2741 #ifndef HAS_DRAND48_PROTO
2742 extern double drand48 (void);
2752 value = 1.0; (void)POPs;
2758 if (!PL_srand_called) {
2759 (void)seedDrand01((Rand_seed_t)seed());
2760 PL_srand_called = TRUE;
2770 const UV anum = (MAXARG < 1 || (!TOPs && !POPs)) ? seed() : POPu;
2771 (void)seedDrand01((Rand_seed_t)anum);
2772 PL_srand_called = TRUE;
2776 /* Historically srand always returned true. We can avoid breaking
2778 sv_setpvs(TARG, "0 but true");
2787 tryAMAGICun_MG(int_amg, AMGf_numeric);
2789 SV * const sv = TOPs;
2790 const IV iv = SvIV_nomg(sv);
2791 /* XXX it's arguable that compiler casting to IV might be subtly
2792 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2793 else preferring IV has introduced a subtle behaviour change bug. OTOH
2794 relying on floating point to be accurate is a bug. */
2799 else if (SvIOK(sv)) {
2801 SETu(SvUV_nomg(sv));
2806 const NV value = SvNV_nomg(sv);
2808 if (value < (NV)UV_MAX + 0.5) {
2811 SETn(Perl_floor(value));
2815 if (value > (NV)IV_MIN - 0.5) {
2818 SETn(Perl_ceil(value));
2829 tryAMAGICun_MG(abs_amg, AMGf_numeric);
2831 SV * const sv = TOPs;
2832 /* This will cache the NV value if string isn't actually integer */
2833 const IV iv = SvIV_nomg(sv);
2838 else if (SvIOK(sv)) {
2839 /* IVX is precise */
2841 SETu(SvUV_nomg(sv)); /* force it to be numeric only */
2849 /* 2s complement assumption. Also, not really needed as
2850 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2856 const NV value = SvNV_nomg(sv);
2870 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2874 SV* const sv = POPs;
2876 tmps = (SvPV_const(sv, len));
2878 /* If Unicode, try to downgrade
2879 * If not possible, croak. */
2880 SV* const tsv = sv_2mortal(newSVsv(sv));
2883 sv_utf8_downgrade(tsv, FALSE);
2884 tmps = SvPV_const(tsv, len);
2886 if (PL_op->op_type == OP_HEX)
2889 while (*tmps && len && isSPACE(*tmps))
2893 if (*tmps == 'x' || *tmps == 'X') {
2895 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2897 else if (*tmps == 'b' || *tmps == 'B')
2898 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2900 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2902 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2916 SV * const sv = TOPs;
2918 if (SvGAMAGIC(sv)) {
2919 /* For an overloaded or magic scalar, we can't know in advance if
2920 it's going to be UTF-8 or not. Also, we can't call sv_len_utf8 as
2921 it likes to cache the length. Maybe that should be a documented
2926 = sv_2pv_flags(sv, &len,
2927 SV_UNDEF_RETURNS_NULL|SV_CONST_RETURN|SV_GMAGIC);
2930 if (!SvPADTMP(TARG)) {
2931 sv_setsv(TARG, &PL_sv_undef);
2936 else if (DO_UTF8(sv)) {
2937 SETi(utf8_length((U8*)p, (U8*)p + len));
2941 } else if (SvOK(sv)) {
2942 /* Neither magic nor overloaded. */
2944 SETi(sv_len_utf8(sv));
2948 if (!SvPADTMP(TARG)) {
2949 sv_setsv_nomg(TARG, &PL_sv_undef);
2957 /* Returns false if substring is completely outside original string.
2958 No length is indicated by len_iv = 0 and len_is_uv = 0. len_is_uv must
2959 always be true for an explicit 0.
2962 Perl_translate_substr_offsets(pTHX_ STRLEN curlen, IV pos1_iv,
2963 bool pos1_is_uv, IV len_iv,
2964 bool len_is_uv, STRLEN *posp,
2970 PERL_ARGS_ASSERT_TRANSLATE_SUBSTR_OFFSETS;
2972 if (!pos1_is_uv && pos1_iv < 0 && curlen) {
2973 pos1_is_uv = curlen-1 > ~(UV)pos1_iv;
2976 if ((pos1_is_uv || pos1_iv > 0) && (UV)pos1_iv > curlen)
2979 if (len_iv || len_is_uv) {
2980 if (!len_is_uv && len_iv < 0) {
2981 pos2_iv = curlen + len_iv;
2983 pos2_is_uv = curlen-1 > ~(UV)len_iv;
2986 } else { /* len_iv >= 0 */
2987 if (!pos1_is_uv && pos1_iv < 0) {
2988 pos2_iv = pos1_iv + len_iv;
2989 pos2_is_uv = (UV)len_iv > (UV)IV_MAX;
2991 if ((UV)len_iv > curlen-(UV)pos1_iv)
2994 pos2_iv = pos1_iv+len_iv;
3004 if (!pos2_is_uv && pos2_iv < 0) {
3005 if (!pos1_is_uv && pos1_iv < 0)
3009 else if (!pos1_is_uv && pos1_iv < 0)
3012 if ((UV)pos2_iv < (UV)pos1_iv)
3014 if ((UV)pos2_iv > curlen)
3017 /* pos1_iv and pos2_iv both in 0..curlen, so the cast is safe */
3018 *posp = (STRLEN)( (UV)pos1_iv );
3019 *lenp = (STRLEN)( (UV)pos2_iv - (UV)pos1_iv );
3036 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3037 const bool rvalue = (GIMME_V != G_VOID);
3040 const char *repl = NULL;
3042 int num_args = PL_op->op_private & 7;
3043 bool repl_need_utf8_upgrade = FALSE;
3044 bool repl_is_utf8 = FALSE;
3048 if(!(repl_sv = POPs)) num_args--;
3050 if ((len_sv = POPs)) {
3051 len_iv = SvIV(len_sv);
3052 len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1;
3057 pos1_iv = SvIV(pos_sv);
3058 pos1_is_uv = SvIOK_UV(pos_sv);
3060 if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) {
3066 repl = SvPV_const(repl_sv, repl_len);
3067 repl_is_utf8 = DO_UTF8(repl_sv) && repl_len;
3070 sv_utf8_upgrade(sv);
3072 else if (DO_UTF8(sv))
3073 repl_need_utf8_upgrade = TRUE;
3077 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3078 sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0);
3080 LvTARG(ret) = SvREFCNT_inc_simple(sv);
3082 pos1_is_uv || pos1_iv >= 0
3083 ? (STRLEN)(UV)pos1_iv
3084 : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv);
3086 len_is_uv || len_iv > 0
3087 ? (STRLEN)(UV)len_iv
3088 : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv);
3091 PUSHs(ret); /* avoid SvSETMAGIC here */
3094 tmps = SvPV_const(sv, curlen);
3096 utf8_curlen = sv_len_utf8(sv);
3097 if (utf8_curlen == curlen)
3100 curlen = utf8_curlen;
3106 STRLEN pos, len, byte_len, byte_pos;
3108 if (!translate_substr_offsets(
3109 curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len
3113 byte_pos = utf8_curlen
3114 ? sv_pos_u2b_flags(sv, pos, &byte_len, SV_CONST_RETURN) : pos;
3119 SvTAINTED_off(TARG); /* decontaminate */
3120 SvUTF8_off(TARG); /* decontaminate */
3121 sv_setpvn(TARG, tmps, byte_len);
3122 #ifdef USE_LOCALE_COLLATE
3123 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3130 SV* repl_sv_copy = NULL;
3132 if (repl_need_utf8_upgrade) {
3133 repl_sv_copy = newSVsv(repl_sv);
3134 sv_utf8_upgrade(repl_sv_copy);
3135 repl = SvPV_const(repl_sv_copy, repl_len);
3136 repl_is_utf8 = DO_UTF8(repl_sv_copy) && repl_len;
3139 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR),
3140 "Attempt to use reference as lvalue in substr"
3144 sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0);
3147 SvREFCNT_dec(repl_sv_copy);
3159 Perl_croak(aTHX_ "substr outside of string");
3160 Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3167 register const IV size = POPi;
3168 register const IV offset = POPi;
3169 register SV * const src = POPs;
3170 const I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3173 if (lvalue) { /* it's an lvalue! */
3174 ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */
3175 sv_magic(ret, NULL, PERL_MAGIC_vec, NULL, 0);
3177 LvTARG(ret) = SvREFCNT_inc_simple(src);
3178 LvTARGOFF(ret) = offset;
3179 LvTARGLEN(ret) = size;
3183 SvTAINTED_off(TARG); /* decontaminate */
3187 sv_setuv(ret, do_vecget(src, offset, size));
3203 const char *little_p;
3206 const bool is_index = PL_op->op_type == OP_INDEX;
3207 const bool threeargs = MAXARG >= 3 && (TOPs || ((void)POPs,0));
3213 big_p = SvPV_const(big, biglen);
3214 little_p = SvPV_const(little, llen);
3216 big_utf8 = DO_UTF8(big);
3217 little_utf8 = DO_UTF8(little);
3218 if (big_utf8 ^ little_utf8) {
3219 /* One needs to be upgraded. */
3220 if (little_utf8 && !PL_encoding) {
3221 /* Well, maybe instead we might be able to downgrade the small
3223 char * const pv = (char*)bytes_from_utf8((U8 *)little_p, &llen,
3226 /* If the large string is ISO-8859-1, and it's not possible to
3227 convert the small string to ISO-8859-1, then there is no
3228 way that it could be found anywhere by index. */
3233 /* At this point, pv is a malloc()ed string. So donate it to temp
3234 to ensure it will get free()d */
3235 little = temp = newSV(0);
3236 sv_usepvn(temp, pv, llen);
3237 little_p = SvPVX(little);
3240 ? newSVpvn(big_p, biglen) : newSVpvn(little_p, llen);
3243 sv_recode_to_utf8(temp, PL_encoding);
3245 sv_utf8_upgrade(temp);
3250 big_p = SvPV_const(big, biglen);
3253 little_p = SvPV_const(little, llen);
3257 if (SvGAMAGIC(big)) {
3258 /* Life just becomes a lot easier if I use a temporary here.
3259 Otherwise I need to avoid calls to sv_pos_u2b(), which (dangerously)
3260 will trigger magic and overloading again, as will fbm_instr()
3262 big = newSVpvn_flags(big_p, biglen,
3263 SVs_TEMP | (big_utf8 ? SVf_UTF8 : 0));
3266 if (SvGAMAGIC(little) || (is_index && !SvOK(little))) {
3267 /* index && SvOK() is a hack. fbm_instr() calls SvPV_const, which will
3268 warn on undef, and we've already triggered a warning with the
3269 SvPV_const some lines above. We can't remove that, as we need to
3270 call some SvPV to trigger overloading early and find out if the
3272 This is all getting to messy. The API isn't quite clean enough,
3273 because data access has side effects.
3275 little = newSVpvn_flags(little_p, llen,
3276 SVs_TEMP | (little_utf8 ? SVf_UTF8 : 0));
3277 little_p = SvPVX(little);
3281 offset = is_index ? 0 : biglen;
3283 if (big_utf8 && offset > 0)
3284 sv_pos_u2b(big, &offset, 0);
3290 else if (offset > (I32)biglen)
3292 if (!(little_p = is_index
3293 ? fbm_instr((unsigned char*)big_p + offset,
3294 (unsigned char*)big_p + biglen, little, 0)
3295 : rninstr(big_p, big_p + offset,
3296 little_p, little_p + llen)))
3299 retval = little_p - big_p;
3300 if (retval > 0 && big_utf8)
3301 sv_pos_b2u(big, &retval);
3311 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3312 SvTAINTED_off(TARG);
3313 do_sprintf(TARG, SP-MARK, MARK+1);
3314 TAINT_IF(SvTAINTED(TARG));
3326 const U8 *s = (U8*)SvPV_const(argsv, len);
3328 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3329 SV * const tmpsv = sv_2mortal(newSVsv(argsv));
3330 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3334 XPUSHu(DO_UTF8(argsv) ?
3335 utf8n_to_uvchr(s, UTF8_MAXBYTES, 0, UTF8_ALLOW_ANYUV) :
3347 if (((SvIOK_notUV(TOPs) && SvIV(TOPs) < 0)
3349 (SvNOK(TOPs) && SvNV(TOPs) < 0.0))) {
3351 value = POPu; /* chr(-1) eq chr(0xff), etc. */
3353 (void) POPs; /* Ignore the argument value. */
3354 value = UNICODE_REPLACEMENT;
3360 SvUPGRADE(TARG,SVt_PV);
3362 if (value > 255 && !IN_BYTES) {
3363 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3364 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3365 SvCUR_set(TARG, tmps - SvPVX_const(TARG));
3367 (void)SvPOK_only(TARG);
3376 *tmps++ = (char)value;
3378 (void)SvPOK_only(TARG);
3380 if (PL_encoding && !IN_BYTES) {
3381 sv_recode_to_utf8(TARG, PL_encoding);
3383 if (SvCUR(TARG) == 0 || !is_utf8_string((U8*)tmps, SvCUR(TARG)) ||
3384 UNICODE_IS_REPLACEMENT(utf8_to_uvchr((U8*)tmps, NULL))) {
3388 *tmps++ = (char)value;
3404 const char *tmps = SvPV_const(left, len);
3406 if (DO_UTF8(left)) {
3407 /* If Unicode, try to downgrade.
3408 * If not possible, croak.
3409 * Yes, we made this up. */
3410 SV* const tsv = sv_2mortal(newSVsv(left));
3413 sv_utf8_downgrade(tsv, FALSE);
3414 tmps = SvPV_const(tsv, len);
3416 # ifdef USE_ITHREADS
3418 if (!PL_reentrant_buffer->_crypt_struct_buffer) {
3419 /* This should be threadsafe because in ithreads there is only
3420 * one thread per interpreter. If this would not be true,
3421 * we would need a mutex to protect this malloc. */
3422 PL_reentrant_buffer->_crypt_struct_buffer =
3423 (struct crypt_data *)safemalloc(sizeof(struct crypt_data));
3424 #if defined(__GLIBC__) || defined(__EMX__)
3425 if (PL_reentrant_buffer->_crypt_struct_buffer) {
3426 PL_reentrant_buffer->_crypt_struct_buffer->initialized = 0;
3427 /* work around glibc-2.2.5 bug */
3428 PL_reentrant_buffer->_crypt_struct_buffer->current_saltbits = 0;
3432 # endif /* HAS_CRYPT_R */
3433 # endif /* USE_ITHREADS */
3435 sv_setpv(TARG, fcrypt(tmps, SvPV_nolen_const(right)));
3437 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV_nolen_const(right)));
3443 "The crypt() function is unimplemented due to excessive paranoia.");
3447 /* Generally UTF-8 and UTF-EBCDIC are indistinguishable at this level. So
3448 * most comments below say UTF-8, when in fact they mean UTF-EBCDIC as well */
3450 /* Generates code to store a unicode codepoint c that is known to occupy
3451 * exactly two UTF-8 and UTF-EBCDIC bytes; it is stored into p and p+1,
3452 * and p is advanced to point to the next available byte after the two bytes */
3453 #define CAT_UNI_TO_UTF8_TWO_BYTE(p, c) \
3455 *(p)++ = UTF8_TWO_BYTE_HI(c); \
3456 *((p)++) = UTF8_TWO_BYTE_LO(c); \
3461 /* Actually is both lcfirst() and ucfirst(). Only the first character
3462 * changes. This means that possibly we can change in-place, ie., just
3463 * take the source and change that one character and store it back, but not
3464 * if read-only etc, or if the length changes */
3469 STRLEN slen; /* slen is the byte length of the whole SV. */
3472 bool inplace; /* ? Convert first char only, in-place */
3473 bool doing_utf8 = FALSE; /* ? using utf8 */
3474 bool convert_source_to_utf8 = FALSE; /* ? need to convert */
3475 const int op_type = PL_op->op_type;
3478 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3479 STRLEN ulen; /* ulen is the byte length of the original Unicode character
3480 * stored as UTF-8 at s. */
3481 STRLEN tculen; /* tculen is the byte length of the freshly titlecased (or
3482 * lowercased) character stored in tmpbuf. May be either
3483 * UTF-8 or not, but in either case is the number of bytes */
3484 bool tainted = FALSE;
3488 s = (const U8*)SvPV_nomg_const(source, slen);
3490 if (ckWARN(WARN_UNINITIALIZED))
3491 report_uninit(source);
3496 /* We may be able to get away with changing only the first character, in
3497 * place, but not if read-only, etc. Later we may discover more reasons to
3498 * not convert in-place. */
3499 inplace = SvPADTMP(source) && !SvREADONLY(source) && SvTEMP(source);
3501 /* First calculate what the changed first character should be. This affects
3502 * whether we can just swap it out, leaving the rest of the string unchanged,
3503 * or even if have to convert the dest to UTF-8 when the source isn't */
3505 if (! slen) { /* If empty */
3506 need = 1; /* still need a trailing NUL */
3509 else if (DO_UTF8(source)) { /* Is the source utf8? */
3512 if (op_type == OP_UCFIRST) {
3513 _to_utf8_title_flags(s, tmpbuf, &tculen,
3514 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3517 _to_utf8_lower_flags(s, tmpbuf, &tculen,
3518 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3521 /* we can't do in-place if the length changes. */
3522 if (ulen != tculen) inplace = FALSE;
3523 need = slen + 1 - ulen + tculen;
3525 else { /* Non-zero length, non-UTF-8, Need to consider locale and if
3526 * latin1 is treated as caseless. Note that a locale takes
3528 ulen = 1; /* Original character is 1 byte */
3529 tculen = 1; /* Most characters will require one byte, but this will
3530 * need to be overridden for the tricky ones */
3533 if (op_type == OP_LCFIRST) {
3535 /* lower case the first letter: no trickiness for any character */
3536 *tmpbuf = (IN_LOCALE_RUNTIME) ? toLOWER_LC(*s) :
3537 ((IN_UNI_8_BIT) ? toLOWER_LATIN1(*s) : toLOWER(*s));
3540 else if (IN_LOCALE_RUNTIME) {
3541 *tmpbuf = toUPPER_LC(*s); /* This would be a bug if any locales
3542 * have upper and title case different
3545 else if (! IN_UNI_8_BIT) {
3546 *tmpbuf = toUPPER(*s); /* Returns caseless for non-ascii, or
3547 * on EBCDIC machines whatever the
3548 * native function does */
3550 else { /* is ucfirst non-UTF-8, not in locale, and cased latin1 */
3551 UV title_ord = _to_upper_title_latin1(*s, tmpbuf, &tculen, 's');
3553 assert(tculen == 2);
3555 /* If the result is an upper Latin1-range character, it can
3556 * still be represented in one byte, which is its ordinal */
3557 if (UTF8_IS_DOWNGRADEABLE_START(*tmpbuf)) {
3558 *tmpbuf = (U8) title_ord;
3562 /* Otherwise it became more than one ASCII character (in
3563 * the case of LATIN_SMALL_LETTER_SHARP_S) or changed to
3564 * beyond Latin1, so the number of bytes changed, so can't
3565 * replace just the first character in place. */
3568 /* If the result won't fit in a byte, the entire result will
3569 * have to be in UTF-8. Assume worst case sizing in
3570 * conversion. (all latin1 characters occupy at most two bytes
3572 if (title_ord > 255) {
3574 convert_source_to_utf8 = TRUE;
3575 need = slen * 2 + 1;
3577 /* The (converted) UTF-8 and UTF-EBCDIC lengths of all
3578 * (both) characters whose title case is above 255 is
3582 else { /* LATIN_SMALL_LETTER_SHARP_S expands by 1 byte */
3583 need = slen + 1 + 1;
3587 } /* End of use Unicode (Latin1) semantics */
3588 } /* End of changing the case of the first character */
3590 /* Here, have the first character's changed case stored in tmpbuf. Ready to
3591 * generate the result */
3594 /* We can convert in place. This means we change just the first
3595 * character without disturbing the rest; no need to grow */
3597 s = d = (U8*)SvPV_force_nomg(source, slen);
3603 /* Here, we can't convert in place; we earlier calculated how much
3604 * space we will need, so grow to accommodate that */
3605 SvUPGRADE(dest, SVt_PV);
3606 d = (U8*)SvGROW(dest, need);
3607 (void)SvPOK_only(dest);
3614 if (! convert_source_to_utf8) {
3616 /* Here both source and dest are in UTF-8, but have to create
3617 * the entire output. We initialize the result to be the
3618 * title/lower cased first character, and then append the rest
3620 sv_setpvn(dest, (char*)tmpbuf, tculen);
3622 sv_catpvn(dest, (char*)(s + ulen), slen - ulen);
3626 const U8 *const send = s + slen;
3628 /* Here the dest needs to be in UTF-8, but the source isn't,
3629 * except we earlier UTF-8'd the first character of the source
3630 * into tmpbuf. First put that into dest, and then append the
3631 * rest of the source, converting it to UTF-8 as we go. */
3633 /* Assert tculen is 2 here because the only two characters that
3634 * get to this part of the code have 2-byte UTF-8 equivalents */
3636 *d++ = *(tmpbuf + 1);
3637 s++; /* We have just processed the 1st char */
3639 for (; s < send; s++) {
3640 d = uvchr_to_utf8(d, *s);
3643 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3647 else { /* in-place UTF-8. Just overwrite the first character */
3648 Copy(tmpbuf, d, tculen, U8);
3649 SvCUR_set(dest, need - 1);
3657 else { /* Neither source nor dest are in or need to be UTF-8 */
3659 if (IN_LOCALE_RUNTIME) {
3663 if (inplace) { /* in-place, only need to change the 1st char */
3666 else { /* Not in-place */
3668 /* Copy the case-changed character(s) from tmpbuf */
3669 Copy(tmpbuf, d, tculen, U8);
3670 d += tculen - 1; /* Code below expects d to point to final
3671 * character stored */
3674 else { /* empty source */
3675 /* See bug #39028: Don't taint if empty */
3679 /* In a "use bytes" we don't treat the source as UTF-8, but, still want
3680 * the destination to retain that flag */
3684 if (!inplace) { /* Finish the rest of the string, unchanged */
3685 /* This will copy the trailing NUL */
3686 Copy(s + 1, d + 1, slen, U8);
3687 SvCUR_set(dest, need - 1);
3690 if (dest != source && SvTAINTED(source))
3696 /* There's so much setup/teardown code common between uc and lc, I wonder if
3697 it would be worth merging the two, and just having a switch outside each
3698 of the three tight loops. There is less and less commonality though */
3712 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3713 && SvTEMP(source) && !DO_UTF8(source)
3714 && (IN_LOCALE_RUNTIME || ! IN_UNI_8_BIT)) {
3716 /* We can convert in place. The reason we can't if in UNI_8_BIT is to
3717 * make the loop tight, so we overwrite the source with the dest before
3718 * looking at it, and we need to look at the original source
3719 * afterwards. There would also need to be code added to handle
3720 * switching to not in-place in midstream if we run into characters
3721 * that change the length.
3724 s = d = (U8*)SvPV_force_nomg(source, len);
3731 /* The old implementation would copy source into TARG at this point.
3732 This had the side effect that if source was undef, TARG was now
3733 an undefined SV with PADTMP set, and they don't warn inside
3734 sv_2pv_flags(). However, we're now getting the PV direct from
3735 source, which doesn't have PADTMP set, so it would warn. Hence the
3739 s = (const U8*)SvPV_nomg_const(source, len);
3741 if (ckWARN(WARN_UNINITIALIZED))
3742 report_uninit(source);
3748 SvUPGRADE(dest, SVt_PV);
3749 d = (U8*)SvGROW(dest, min);
3750 (void)SvPOK_only(dest);
3755 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3756 to check DO_UTF8 again here. */
3758 if (DO_UTF8(source)) {
3759 const U8 *const send = s + len;
3760 U8 tmpbuf[UTF8_MAXBYTES+1];
3761 bool tainted = FALSE;
3763 /* All occurrences of these are to be moved to follow any other marks.
3764 * This is context-dependent. We may not be passed enough context to
3765 * move the iota subscript beyond all of them, but we do the best we can
3766 * with what we're given. The result is always better than if we
3767 * hadn't done this. And, the problem would only arise if we are
3768 * passed a character without all its combining marks, which would be
3769 * the caller's mistake. The information this is based on comes from a
3770 * comment in Unicode SpecialCasing.txt, (and the Standard's text
3771 * itself) and so can't be checked properly to see if it ever gets
3772 * revised. But the likelihood of it changing is remote */
3773 bool in_iota_subscript = FALSE;
3779 if (in_iota_subscript && ! is_utf8_mark(s)) {
3781 /* A non-mark. Time to output the iota subscript */
3782 #define GREEK_CAPITAL_LETTER_IOTA 0x0399
3783 #define COMBINING_GREEK_YPOGEGRAMMENI 0x0345
3785 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3786 in_iota_subscript = FALSE;
3789 /* Then handle the current character. Get the changed case value
3790 * and copy it to the output buffer */
3793 uv = _to_utf8_upper_flags(s, tmpbuf, &ulen,
3794 cBOOL(IN_LOCALE_RUNTIME), &tainted);
3795 if (uv == GREEK_CAPITAL_LETTER_IOTA
3796 && utf8_to_uvchr(s, 0) == COMBINING_GREEK_YPOGEGRAMMENI)
3798 in_iota_subscript = TRUE;
3801 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
3802 /* If the eventually required minimum size outgrows the
3803 * available space, we need to grow. */
3804 const UV o = d - (U8*)SvPVX_const(dest);
3806 /* If someone uppercases one million U+03B0s we SvGROW()
3807 * one million times. Or we could try guessing how much to
3808 * allocate without allocating too much. Such is life.
3809 * See corresponding comment in lc code for another option
3812 d = (U8*)SvPVX(dest) + o;
3814 Copy(tmpbuf, d, ulen, U8);
3819 if (in_iota_subscript) {
3820 CAT_UNI_TO_UTF8_TWO_BYTE(d, GREEK_CAPITAL_LETTER_IOTA);
3825 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3831 else { /* Not UTF-8 */
3833 const U8 *const send = s + len;
3835 /* Use locale casing if in locale; regular style if not treating
3836 * latin1 as having case; otherwise the latin1 casing. Do the
3837 * whole thing in a tight loop, for speed, */
3838 if (IN_LOCALE_RUNTIME) {
3841 for (; s < send; d++, s++)
3842 *d = toUPPER_LC(*s);
3844 else if (! IN_UNI_8_BIT) {
3845 for (; s < send; d++, s++) {
3850 for (; s < send; d++, s++) {
3851 *d = toUPPER_LATIN1_MOD(*s);
3852 if (LIKELY(*d != LATIN_SMALL_LETTER_Y_WITH_DIAERESIS)) continue;
3854 /* The mainstream case is the tight loop above. To avoid
3855 * extra tests in that, all three characters that require
3856 * special handling are mapped by the MOD to the one tested
3858 * Use the source to distinguish between the three cases */
3860 if (*s == LATIN_SMALL_LETTER_SHARP_S) {
3862 /* uc() of this requires 2 characters, but they are
3863 * ASCII. If not enough room, grow the string */
3864 if (SvLEN(dest) < ++min) {
3865 const UV o = d - (U8*)SvPVX_const(dest);
3867 d = (U8*)SvPVX(dest) + o;
3869 *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
3870 continue; /* Back to the tight loop; still in ASCII */
3873 /* The other two special handling characters have their
3874 * upper cases outside the latin1 range, hence need to be
3875 * in UTF-8, so the whole result needs to be in UTF-8. So,
3876 * here we are somewhere in the middle of processing a
3877 * non-UTF-8 string, and realize that we will have to convert
3878 * the whole thing to UTF-8. What to do? There are
3879 * several possibilities. The simplest to code is to
3880 * convert what we have so far, set a flag, and continue on
3881 * in the loop. The flag would be tested each time through
3882 * the loop, and if set, the next character would be
3883 * converted to UTF-8 and stored. But, I (khw) didn't want
3884 * to slow down the mainstream case at all for this fairly
3885 * rare case, so I didn't want to add a test that didn't
3886 * absolutely have to be there in the loop, besides the
3887 * possibility that it would get too complicated for
3888 * optimizers to deal with. Another possibility is to just
3889 * give up, convert the source to UTF-8, and restart the
3890 * function that way. Another possibility is to convert
3891 * both what has already been processed and what is yet to
3892 * come separately to UTF-8, then jump into the loop that
3893 * handles UTF-8. But the most efficient time-wise of the
3894 * ones I could think of is what follows, and turned out to
3895 * not require much extra code. */
3897 /* Convert what we have so far into UTF-8, telling the
3898 * function that we know it should be converted, and to
3899 * allow extra space for what we haven't processed yet.
3900 * Assume the worst case space requirements for converting
3901 * what we haven't processed so far: that it will require
3902 * two bytes for each remaining source character, plus the
3903 * NUL at the end. This may cause the string pointer to
3904 * move, so re-find it. */
3906 len = d - (U8*)SvPVX_const(dest);
3907 SvCUR_set(dest, len);
3908 len = sv_utf8_upgrade_flags_grow(dest,
3909 SV_GMAGIC|SV_FORCE_UTF8_UPGRADE,
3911 d = (U8*)SvPVX(dest) + len;
3913 /* Now process the remainder of the source, converting to
3914 * upper and UTF-8. If a resulting byte is invariant in
3915 * UTF-8, output it as-is, otherwise convert to UTF-8 and
3916 * append it to the output. */
3917 for (; s < send; s++) {
3918 (void) _to_upper_title_latin1(*s, d, &len, 'S');
3922 /* Here have processed the whole source; no need to continue
3923 * with the outer loop. Each character has been converted
3924 * to upper case and converted to UTF-8 */
3927 } /* End of processing all latin1-style chars */
3928 } /* End of processing all chars */
3929 } /* End of source is not empty */
3931 if (source != dest) {
3932 *d = '\0'; /* Here d points to 1 after last char, add NUL */
3933 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
3935 } /* End of isn't utf8 */
3936 if (dest != source && SvTAINTED(source))
3955 if (SvPADTMP(source) && !SvREADONLY(source) && !SvAMAGIC(source)
3956 && SvTEMP(source) && !DO_UTF8(source)) {
3958 /* We can convert in place, as lowercasing anything in the latin1 range
3959 * (or else DO_UTF8 would have been on) doesn't lengthen it */
3961 s = d = (U8*)SvPV_force_nomg(source, len);
3968 /* The old implementation would copy source into TARG at this point.
3969 This had the side effect that if source was undef, TARG was now
3970 an undefined SV with PADTMP set, and they don't warn inside
3971 sv_2pv_flags(). However, we're now getting the PV direct from
3972 source, which doesn't have PADTMP set, so it would warn. Hence the
3976 s = (const U8*)SvPV_nomg_const(source, len);
3978 if (ckWARN(WARN_UNINITIALIZED))
3979 report_uninit(source);
3985 SvUPGRADE(dest, SVt_PV);
3986 d = (U8*)SvGROW(dest, min);
3987 (void)SvPOK_only(dest);
3992 /* Overloaded values may have toggled the UTF-8 flag on source, so we need
3993 to check DO_UTF8 again here. */
3995 if (DO_UTF8(source)) {
3996 const U8 *const send = s + len;
3997 U8 tmpbuf[UTF8_MAXBYTES_CASE+1];
3998 bool tainted = FALSE;
4001 const STRLEN u = UTF8SKIP(s);
4004 _to_utf8_lower_flags(s, tmpbuf, &ulen,
4005 cBOOL(IN_LOCALE_RUNTIME), &tainted);
4007 /* Here is where we would do context-sensitive actions. See the
4008 * commit message for this comment for why there isn't any */
4010 if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
4012 /* If the eventually required minimum size outgrows the
4013 * available space, we need to grow. */
4014 const UV o = d - (U8*)SvPVX_const(dest);
4016 /* If someone lowercases one million U+0130s we SvGROW() one
4017 * million times. Or we could try guessing how much to
4018 * allocate without allocating too much. Such is life.
4019 * Another option would be to grow an extra byte or two more
4020 * each time we need to grow, which would cut down the million
4021 * to 500K, with little waste */
4023 d = (U8*)SvPVX(dest) + o;
4026 /* Copy the newly lowercased letter to the output buffer we're
4028 Copy(tmpbuf, d, ulen, U8);
4031 } /* End of looping through the source string */
4034 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4039 } else { /* Not utf8 */
4041 const U8 *const send = s + len;
4043 /* Use locale casing if in locale; regular style if not treating
4044 * latin1 as having case; otherwise the latin1 casing. Do the
4045 * whole thing in a tight loop, for speed, */
4046 if (IN_LOCALE_RUNTIME) {
4049 for (; s < send; d++, s++)
4050 *d = toLOWER_LC(*s);
4052 else if (! IN_UNI_8_BIT) {
4053 for (; s < send; d++, s++) {
4058 for (; s < send; d++, s++) {
4059 *d = toLOWER_LATIN1(*s);
4063 if (source != dest) {
4065 SvCUR_set(dest, d - (U8*)SvPVX_const(dest));
4068 if (dest != source && SvTAINTED(source))
4077 SV * const sv = TOPs;
4079 register const char *s = SvPV_const(sv,len);
4081 SvUTF8_off(TARG); /* decontaminate */
4084 SvUPGRADE(TARG, SVt_PV);
4085 SvGROW(TARG, (len * 2) + 1);
4089 if (UTF8_IS_CONTINUED(*s)) {
4090 STRLEN ulen = UTF8SKIP(s);
4114 SvCUR_set(TARG, d - SvPVX_const(TARG));
4115 (void)SvPOK_only_UTF8(TARG);
4118 sv_setpvn(TARG, s, len);
4127 dVAR; dSP; dMARK; dORIGMARK;
4128 register AV *const av = MUTABLE_AV(POPs);
4129 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4131 if (SvTYPE(av) == SVt_PVAV) {
4132 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4133 bool can_preserve = FALSE;
4139 can_preserve = SvCANEXISTDELETE(av);
4142 if (lval && localizing) {
4145 for (svp = MARK + 1; svp <= SP; svp++) {
4146 const I32 elem = SvIV(*svp);
4150 if (max > AvMAX(av))
4154 while (++MARK <= SP) {
4156 I32 elem = SvIV(*MARK);
4157 bool preeminent = TRUE;
4159 if (localizing && can_preserve) {
4160 /* If we can determine whether the element exist,
4161 * Try to preserve the existenceness of a tied array
4162 * element by using EXISTS and DELETE if possible.
4163 * Fallback to FETCH and STORE otherwise. */
4164 preeminent = av_exists(av, elem);
4167 svp = av_fetch(av, elem, lval);
4169 if (!svp || *svp == &PL_sv_undef)
4170 DIE(aTHX_ PL_no_aelem, elem);
4173 save_aelem(av, elem, svp);
4175 SAVEADELETE(av, elem);
4178 *MARK = svp ? *svp : &PL_sv_undef;
4181 if (GIMME != G_ARRAY) {
4183 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4189 /* Smart dereferencing for keys, values and each */
4201 (SvTYPE(sv) != SVt_PVHV && SvTYPE(sv) != SVt_PVAV)
4206 "Type of argument to %s must be unblessed hashref or arrayref",
4207 PL_op_desc[PL_op->op_type] );
4210 if (PL_op->op_flags & OPf_SPECIAL && SvTYPE(sv) == SVt_PVAV)
4212 "Can't modify %s in %s",
4213 PL_op_desc[PL_op->op_type], PL_op_desc[PL_op->op_next->op_type]
4216 /* Delegate to correct function for op type */
4218 if (PL_op->op_type == OP_RKEYS || PL_op->op_type == OP_RVALUES) {
4219 return (SvTYPE(sv) == SVt_PVHV) ? Perl_do_kv(aTHX) : Perl_pp_akeys(aTHX);
4222 return (SvTYPE(sv) == SVt_PVHV) ? Perl_pp_each(aTHX) : Perl_pp_aeach(aTHX);
4230 AV *array = MUTABLE_AV(POPs);
4231 const I32 gimme = GIMME_V;
4232 IV *iterp = Perl_av_iter_p(aTHX_ array);
4233 const IV current = (*iterp)++;
4235 if (current > av_len(array)) {
4237 if (gimme == G_SCALAR)
4245 if (gimme == G_ARRAY) {
4246 SV **const element = av_fetch(array, current, 0);
4247 PUSHs(element ? *element : &PL_sv_undef);
4256 AV *array = MUTABLE_AV(POPs);
4257 const I32 gimme = GIMME_V;
4259 *Perl_av_iter_p(aTHX_ array) = 0;
4261 if (gimme == G_SCALAR) {
4263 PUSHi(av_len(array) + 1);
4265 else if (gimme == G_ARRAY) {
4266 IV n = Perl_av_len(aTHX_ array);
4271 if (PL_op->op_type == OP_AKEYS || PL_op->op_type == OP_RKEYS) {
4272 for (i = 0; i <= n; i++) {
4277 for (i = 0; i <= n; i++) {
4278 SV *const *const elem = Perl_av_fetch(aTHX_ array, i, 0);
4279 PUSHs(elem ? *elem : &PL_sv_undef);
4286 /* Associative arrays. */
4292 HV * hash = MUTABLE_HV(POPs);
4294 const I32 gimme = GIMME_V;
4297 /* might clobber stack_sp */
4298 entry = hv_iternext(hash);
4303 SV* const sv = hv_iterkeysv(entry);
4304 PUSHs(sv); /* won't clobber stack_sp */
4305 if (gimme == G_ARRAY) {
4308 /* might clobber stack_sp */
4309 val = hv_iterval(hash, entry);
4314 else if (gimme == G_SCALAR)
4321 S_do_delete_local(pTHX)
4325 const I32 gimme = GIMME_V;
4329 if (PL_op->op_private & OPpSLICE) {
4331 SV * const osv = POPs;
4332 const bool tied = SvRMAGICAL(osv)
4333 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4334 const bool can_preserve = SvCANEXISTDELETE(osv)
4335 || mg_find((const SV *)osv, PERL_MAGIC_env);
4336 const U32 type = SvTYPE(osv);
4337 if (type == SVt_PVHV) { /* hash element */
4338 HV * const hv = MUTABLE_HV(osv);
4339 while (++MARK <= SP) {
4340 SV * const keysv = *MARK;
4342 bool preeminent = TRUE;
4344 preeminent = hv_exists_ent(hv, keysv, 0);
4346 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4353 sv = hv_delete_ent(hv, keysv, 0, 0);
4354 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4357 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4359 *MARK = sv_mortalcopy(sv);
4365 SAVEHDELETE(hv, keysv);
4366 *MARK = &PL_sv_undef;
4370 else if (type == SVt_PVAV) { /* array element */
4371 if (PL_op->op_flags & OPf_SPECIAL) {
4372 AV * const av = MUTABLE_AV(osv);
4373 while (++MARK <= SP) {
4374 I32 idx = SvIV(*MARK);
4376 bool preeminent = TRUE;
4378 preeminent = av_exists(av, idx);
4380 SV **svp = av_fetch(av, idx, 1);
4387 sv = av_delete(av, idx, 0);
4388 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4391 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4393 *MARK = sv_mortalcopy(sv);
4399 SAVEADELETE(av, idx);
4400 *MARK = &PL_sv_undef;
4406 DIE(aTHX_ "Not a HASH reference");
4407 if (gimme == G_VOID)
4409 else if (gimme == G_SCALAR) {
4414 *++MARK = &PL_sv_undef;
4419 SV * const keysv = POPs;
4420 SV * const osv = POPs;
4421 const bool tied = SvRMAGICAL(osv)
4422 && mg_find((const SV *)osv, PERL_MAGIC_tied);
4423 const bool can_preserve = SvCANEXISTDELETE(osv)
4424 || mg_find((const SV *)osv, PERL_MAGIC_env);
4425 const U32 type = SvTYPE(osv);
4427 if (type == SVt_PVHV) {
4428 HV * const hv = MUTABLE_HV(osv);
4429 bool preeminent = TRUE;
4431 preeminent = hv_exists_ent(hv, keysv, 0);
4433 HE *he = hv_fetch_ent(hv, keysv, 1, 0);
4440 sv = hv_delete_ent(hv, keysv, 0, 0);
4441 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4444 save_helem_flags(hv, keysv, &sv, SAVEf_KEEPOLDELEM);
4446 SV *nsv = sv_mortalcopy(sv);
4452 SAVEHDELETE(hv, keysv);
4454 else if (type == SVt_PVAV) {
4455 if (PL_op->op_flags & OPf_SPECIAL) {
4456 AV * const av = MUTABLE_AV(osv);
4457 I32 idx = SvIV(keysv);
4458 bool preeminent = TRUE;
4460 preeminent = av_exists(av, idx);
4462 SV **svp = av_fetch(av, idx, 1);
4469 sv = av_delete(av, idx, 0);
4470 SvREFCNT_inc_simple_void(sv); /* De-mortalize */
4473 save_aelem_flags(av, idx, &sv, SAVEf_KEEPOLDELEM);
4475 SV *nsv = sv_mortalcopy(sv);
4481 SAVEADELETE(av, idx);
4484 DIE(aTHX_ "panic: avhv_delete no longer supported");
4487 DIE(aTHX_ "Not a HASH reference");
4490 if (gimme != G_VOID)
4504 if (PL_op->op_private & OPpLVAL_INTRO)
4505 return do_delete_local();
4508 discard = (gimme == G_VOID) ? G_DISCARD : 0;
4510 if (PL_op->op_private & OPpSLICE) {
4512 HV * const hv = MUTABLE_HV(POPs);
4513 const U32 hvtype = SvTYPE(hv);
4514 if (hvtype == SVt_PVHV) { /* hash element */
4515 while (++MARK <= SP) {
4516 SV * const sv = hv_delete_ent(hv, *MARK, discard, 0);
4517 *MARK = sv ? sv : &PL_sv_undef;
4520 else if (hvtype == SVt_PVAV) { /* array element */
4521 if (PL_op->op_flags & OPf_SPECIAL) {
4522 while (++MARK <= SP) {
4523 SV * const sv = av_delete(MUTABLE_AV(hv), SvIV(*MARK), discard);
4524 *MARK = sv ? sv : &PL_sv_undef;
4529 DIE(aTHX_ "Not a HASH reference");
4532 else if (gimme == G_SCALAR) {
4537 *++MARK = &PL_sv_undef;
4543 HV * const hv = MUTABLE_HV(POPs);
4545 if (SvTYPE(hv) == SVt_PVHV)
4546 sv = hv_delete_ent(hv, keysv, discard, 0);
4547 else if (SvTYPE(hv) == SVt_PVAV) {
4548 if (PL_op->op_flags & OPf_SPECIAL)
4549 sv = av_delete(MUTABLE_AV(hv), SvIV(keysv), discard);
4551 DIE(aTHX_ "panic: avhv_delete no longer supported");
4554 DIE(aTHX_ "Not a HASH reference");
4570 if (PL_op->op_private & OPpEXISTS_SUB) {
4572 SV * const sv = POPs;
4573 CV * const cv = sv_2cv(sv, &hv, &gv, 0);
4576 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
4581 hv = MUTABLE_HV(POPs);
4582 if (SvTYPE(hv) == SVt_PVHV) {
4583 if (hv_exists_ent(hv, tmpsv, 0))
4586 else if (SvTYPE(hv) == SVt_PVAV) {
4587 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
4588 if (av_exists(MUTABLE_AV(hv), SvIV(tmpsv)))
4593 DIE(aTHX_ "Not a HASH reference");
4600 dVAR; dSP; dMARK; dORIGMARK;
4601 register HV * const hv = MUTABLE_HV(POPs);
4602 register const I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
4603 const bool localizing = PL_op->op_private & OPpLVAL_INTRO;
4604 bool can_preserve = FALSE;
4610 if (SvCANEXISTDELETE(hv) || mg_find((const SV *)hv, PERL_MAGIC_env))
4611 can_preserve = TRUE;
4614 while (++MARK <= SP) {
4615 SV * const keysv = *MARK;
4618 bool preeminent = TRUE;
4620 if (localizing && can_preserve) {
4621 /* If we can determine whether the element exist,
4622 * try to preserve the existenceness of a tied hash
4623 * element by using EXISTS and DELETE if possible.
4624 * Fallback to FETCH and STORE otherwise. */
4625 preeminent = hv_exists_ent(hv, keysv, 0);
4628 he = hv_fetch_ent(hv, keysv, lval, 0);
4629 svp = he ? &HeVAL(he) : NULL;
4632 if (!svp || *svp == &PL_sv_undef) {
4633 DIE(aTHX_ PL_no_helem_sv, SVfARG(keysv));
4636 if (HvNAME_get(hv) && isGV(*svp))
4637 save_gp(MUTABLE_GV(*svp), !(PL_op->op_flags & OPf_SPECIAL));
4638 else if (preeminent)
4639 save_helem_flags(hv, keysv, svp,
4640 (PL_op->op_flags & OPf_SPECIAL) ? 0 : SAVEf_SETMAGIC);
4642 SAVEHDELETE(hv, keysv);
4645 *MARK = svp ? *svp : &PL_sv_undef;
4647 if (GIMME != G_ARRAY) {
4649 *++MARK = SP > ORIGMARK ? *SP : &PL_sv_undef;
4655 /* List operators. */
4660 if (GIMME != G_ARRAY) {
4662 *MARK = *SP; /* unwanted list, return last item */
4664 *MARK = &PL_sv_undef;
4674 SV ** const lastrelem = PL_stack_sp;
4675 SV ** const lastlelem = PL_stack_base + POPMARK;
4676 SV ** const firstlelem = PL_stack_base + POPMARK + 1;
4677 register SV ** const firstrelem = lastlelem + 1;
4678 I32 is_something_there = FALSE;
4680 register const I32 max = lastrelem - lastlelem;
4681 register SV **lelem;
4683 if (GIMME != G_ARRAY) {
4684 I32 ix = SvIV(*lastlelem);
4687 if (ix < 0 || ix >= max)
4688 *firstlelem = &PL_sv_undef;
4690 *firstlelem = firstrelem[ix];
4696 SP = firstlelem - 1;
4700 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
4701 I32 ix = SvIV(*lelem);
4704 if (ix < 0 || ix >= max)
4705 *lelem = &PL_sv_undef;
4707 is_something_there = TRUE;
4708 if (!(*lelem = firstrelem[ix]))
4709 *lelem = &PL_sv_undef;
4712 if (is_something_there)
4715 SP = firstlelem - 1;
4721 dVAR; dSP; dMARK; dORIGMARK;
4722 const I32 items = SP - MARK;
4723 SV * const av = MUTABLE_SV(av_make(items, MARK+1));
4724 SP = ORIGMARK; /* av_make() might realloc stack_sp */
4725 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4726 ? newRV_noinc(av) : av);
4732 dVAR; dSP; dMARK; dORIGMARK;
4733 HV* const hv = newHV();
4736 SV * const key = *++MARK;
4737 SV * const val = newSV(0);
4739 sv_setsv(val, *++MARK);
4741 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
4742 (void)hv_store_ent(hv,key,val,0);
4745 mXPUSHs((PL_op->op_flags & OPf_SPECIAL)
4746 ? newRV_noinc(MUTABLE_SV(hv)) : MUTABLE_SV(hv));
4751 S_deref_plain_array(pTHX_ AV *ary)
4753 if (SvTYPE(ary) == SVt_PVAV) return ary;
4754 SvGETMAGIC((SV *)ary);
4755 if (!SvROK(ary) || SvTYPE(SvRV(ary)) != SVt_PVAV)
4756 Perl_die(aTHX_ "Not an ARRAY reference");
4757 else if (SvOBJECT(SvRV(ary)))
4758 Perl_die(aTHX_ "Not an unblessed ARRAY reference");
4759 return (AV *)SvRV(ary);
4762 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
4763 # define DEREF_PLAIN_ARRAY(ary) \
4766 SvTYPE(aRrRay) == SVt_PVAV \
4768 : S_deref_plain_array(aTHX_ aRrRay); \
4771 # define DEREF_PLAIN_ARRAY(ary) \
4773 PL_Sv = (SV *)(ary), \
4774 SvTYPE(PL_Sv) == SVt_PVAV \
4776 : S_deref_plain_array(aTHX_ (AV *)PL_Sv) \
4782 dVAR; dSP; dMARK; dORIGMARK;
4783 int num_args = (SP - MARK);
4784 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4788 register I32 offset;
4789 register I32 length;
4793 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4796 return Perl_tied_method(aTHX_ "SPLICE", mark - 1, MUTABLE_SV(ary), mg,
4797 GIMME_V | TIED_METHOD_ARGUMENTS_ON_STACK,
4804 offset = i = SvIV(*MARK);
4806 offset += AvFILLp(ary) + 1;
4808 DIE(aTHX_ PL_no_aelem, i);
4810 length = SvIVx(*MARK++);
4812 length += AvFILLp(ary) - offset + 1;
4818 length = AvMAX(ary) + 1; /* close enough to infinity */
4822 length = AvMAX(ary) + 1;
4824 if (offset > AvFILLp(ary) + 1) {
4826 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4827 offset = AvFILLp(ary) + 1;
4829 after = AvFILLp(ary) + 1 - (offset + length);
4830 if (after < 0) { /* not that much array */
4831 length += after; /* offset+length now in array */
4837 /* At this point, MARK .. SP-1 is our new LIST */
4840 diff = newlen - length;
4841 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4844 /* make new elements SVs now: avoid problems if they're from the array */
4845 for (dst = MARK, i = newlen; i; i--) {
4846 SV * const h = *dst;
4847 *dst++ = newSVsv(h);
4850 if (diff < 0) { /* shrinking the area */
4851 SV **tmparyval = NULL;
4853 Newx(tmparyval, newlen, SV*); /* so remember insertion */
4854 Copy(MARK, tmparyval, newlen, SV*);
4857 MARK = ORIGMARK + 1;
4858 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4859 MEXTEND(MARK, length);
4860 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4862 EXTEND_MORTAL(length);
4863 for (i = length, dst = MARK; i; i--) {
4864 sv_2mortal(*dst); /* free them eventually */
4871 *MARK = AvARRAY(ary)[offset+length-1];
4874 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4875 SvREFCNT_dec(*dst++); /* free them now */
4878 AvFILLp(ary) += diff;
4880 /* pull up or down? */
4882 if (offset < after) { /* easier to pull up */
4883 if (offset) { /* esp. if nothing to pull */
4884 src = &AvARRAY(ary)[offset-1];
4885 dst = src - diff; /* diff is negative */
4886 for (i = offset; i > 0; i--) /* can't trust Copy */
4890 AvARRAY(ary) = AvARRAY(ary) - diff; /* diff is negative */
4894 if (after) { /* anything to pull down? */
4895 src = AvARRAY(ary) + offset + length;
4896 dst = src + diff; /* diff is negative */
4897 Move(src, dst, after, SV*);
4899 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4900 /* avoid later double free */
4904 dst[--i] = &PL_sv_undef;
4907 Copy( tmparyval, AvARRAY(ary) + offset, newlen, SV* );
4908 Safefree(tmparyval);
4911 else { /* no, expanding (or same) */
4912 SV** tmparyval = NULL;
4914 Newx(tmparyval, length, SV*); /* so remember deletion */
4915 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4918 if (diff > 0) { /* expanding */
4919 /* push up or down? */
4920 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4924 Move(src, dst, offset, SV*);
4926 AvARRAY(ary) = AvARRAY(ary) - diff;/* diff is positive */
4928 AvFILLp(ary) += diff;
4931 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4932 av_extend(ary, AvFILLp(ary) + diff);
4933 AvFILLp(ary) += diff;
4936 dst = AvARRAY(ary) + AvFILLp(ary);
4938 for (i = after; i; i--) {
4946 Copy( MARK, AvARRAY(ary) + offset, newlen, SV* );
4949 MARK = ORIGMARK + 1;
4950 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4952 Copy(tmparyval, MARK, length, SV*);
4954 EXTEND_MORTAL(length);
4955 for (i = length, dst = MARK; i; i--) {
4956 sv_2mortal(*dst); /* free them eventually */
4963 else if (length--) {
4964 *MARK = tmparyval[length];
4967 while (length-- > 0)
4968 SvREFCNT_dec(tmparyval[length]);
4972 *MARK = &PL_sv_undef;
4973 Safefree(tmparyval);
4977 mg_set(MUTABLE_SV(ary));
4985 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
4986 register AV * const ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
4987 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
4990 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
4993 ENTER_with_name("call_PUSH");
4994 call_method("PUSH",G_SCALAR|G_DISCARD);
4995 LEAVE_with_name("call_PUSH");
4999 PL_delaymagic = DM_DELAY;
5000 for (++MARK; MARK <= SP; MARK++) {
5001 SV * const sv = newSV(0);
5003 sv_setsv(sv, *MARK);
5004 av_store(ary, AvFILLp(ary)+1, sv);
5006 if (PL_delaymagic & DM_ARRAY_ISA)
5007 mg_set(MUTABLE_SV(ary));
5012 if (OP_GIMME(PL_op, 0) != G_VOID) {
5013 PUSHi( AvFILL(ary) + 1 );
5022 AV * const av = PL_op->op_flags & OPf_SPECIAL
5023 ? MUTABLE_AV(GvAV(PL_defgv)) : DEREF_PLAIN_ARRAY(MUTABLE_AV(POPs));
5024 SV * const sv = PL_op->op_type == OP_SHIFT ? av_shift(av) : av_pop(av);
5028 (void)sv_2mortal(sv);
5035 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
5036 register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
5037 const MAGIC * const mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied);
5040 *MARK-- = SvTIED_obj(MUTABLE_SV(ary), mg);
5043 ENTER_with_name("call_UNSHIFT");
5044 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
5045 LEAVE_with_name("call_UNSHIFT");
5050 av_unshift(ary, SP - MARK);
5052 SV * const sv = newSVsv(*++MARK);
5053 (void)av_store(ary, i++, sv);
5057 if (OP_GIMME(PL_op, 0) != G_VOID) {
5058 PUSHi( AvFILL(ary) + 1 );
5067 if (GIMME == G_ARRAY) {
5068 if (PL_op->op_private & OPpREVERSE_INPLACE) {
5072 assert( MARK+1 == SP && *SP && SvTYPE(*SP) == SVt_PVAV);
5073 (void)POPMARK; /* remove mark associated with ex-OP_AASSIGN */
5074 av = MUTABLE_AV((*SP));
5075 /* In-place reversing only happens in void context for the array
5076 * assignment. We don't need to push anything on the stack. */
5079 if (SvMAGICAL(av)) {
5081 register SV *tmp = sv_newmortal();
5082 /* For SvCANEXISTDELETE */
5085 bool can_preserve = SvCANEXISTDELETE(av);
5087 for (i = 0, j = av_len(av); i < j; ++i, --j) {
5088 register SV *begin, *end;
5091 if (!av_exists(av, i)) {
5092 if (av_exists(av, j)) {
5093 register SV *sv = av_delete(av, j, 0);
5094 begin = *av_fetch(av, i, TRUE);
5095 sv_setsv_mg(begin, sv);
5099 else if (!av_exists(av, j)) {
5100 register SV *sv = av_delete(av, i, 0);
5101 end = *av_fetch(av, j, TRUE);
5102 sv_setsv_mg(end, sv);
5107 begin = *av_fetch(av, i, TRUE);
5108 end = *av_fetch(av, j, TRUE);
5109 sv_setsv(tmp, begin);
5110 sv_setsv_mg(begin, end);
5111 sv_setsv_mg(end, tmp);
5115 SV **begin = AvARRAY(av);
5118 SV **end = begin + AvFILLp(av);
5120 while (begin < end) {
5121 register SV * const tmp = *begin;
5132 register SV * const tmp = *MARK;
5136 /* safe as long as stack cannot get extended in the above */
5142 register char *down;
5147 SvUTF8_off(TARG); /* decontaminate */
5149 do_join(TARG, &PL_sv_no, MARK, SP);
5151 sv_setsv(TARG, SP > MARK ? *SP : find_rundefsv());
5152 if (! SvOK(TARG) && ckWARN(WARN_UNINITIALIZED))
5153 report_uninit(TARG);
5156 up = SvPV_force(TARG, len);
5158 if (DO_UTF8(TARG)) { /* first reverse each character */
5159 U8* s = (U8*)SvPVX(TARG);
5160 const U8* send = (U8*)(s + len);
5162 if (UTF8_IS_INVARIANT(*s)) {
5167 if (!utf8_to_uvchr(s, 0))
5171 down = (char*)(s - 1);
5172 /* reverse this character */
5176 *down-- = (char)tmp;
5182 down = SvPVX(TARG) + len - 1;
5186 *down-- = (char)tmp;
5188 (void)SvPOK_only_UTF8(TARG);
5200 register IV limit = POPi; /* note, negative is forever */
5201 SV * const sv = POPs;
5203 register const char *s = SvPV_const(sv, len);
5204 const bool do_utf8 = DO_UTF8(sv);
5205 const char *strend = s + len;
5207 register REGEXP *rx;
5209 register const char *m;
5211 const STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (STRLEN)(strend - s);
5212 I32 maxiters = slen + 10;
5213 I32 trailing_empty = 0;
5215 const I32 origlimit = limit;
5218 const I32 gimme = GIMME_V;
5220 const I32 oldsave = PL_savestack_ix;
5221 U32 make_mortal = SVs_TEMP;
5226 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5231 DIE(aTHX_ "panic: pp_split");
5234 TAINT_IF(get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET &&
5235 (RX_EXTFLAGS(rx) & (RXf_WHITE | RXf_SKIPWHITE)));
5237 RX_MATCH_UTF8_set(rx, do_utf8);
5240 if (pm->op_pmreplrootu.op_pmtargetoff) {
5241 ary = GvAVn(MUTABLE_GV(PAD_SVl(pm->op_pmreplrootu.op_pmtargetoff)));
5244 if (pm->op_pmreplrootu.op_pmtargetgv) {
5245 ary = GvAVn(pm->op_pmreplrootu.op_pmtargetgv);
5250 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5256 if ((mg = SvTIED_mg((const SV *)ary, PERL_MAGIC_tied))) {
5258 XPUSHs(SvTIED_obj(MUTABLE_SV(ary), mg));
5265 for (i = AvFILLp(ary); i >= 0; i--)
5266 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5268 /* temporarily switch stacks */
5269 SAVESWITCHSTACK(PL_curstack, ary);
5273 base = SP - PL_stack_base;
5275 if (RX_EXTFLAGS(rx) & RXf_SKIPWHITE) {
5277 while (*s == ' ' || is_utf8_space((U8*)s))
5280 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5281 while (isSPACE_LC(*s))
5289 if (RX_EXTFLAGS(rx) & RXf_PMf_MULTILINE) {
5293 gimme_scalar = gimme == G_SCALAR && !ary;
5296 limit = maxiters + 2;
5297 if (RX_EXTFLAGS(rx) & RXf_WHITE) {
5300 /* this one uses 'm' and is a negative test */
5302 while (m < strend && !( *m == ' ' || is_utf8_space((U8*)m) )) {
5303 const int t = UTF8SKIP(m);
5304 /* is_utf8_space returns FALSE for malform utf8 */
5311 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5312 while (m < strend && !isSPACE_LC(*m))
5315 while (m < strend && !isSPACE(*m))
5328 dstr = newSVpvn_flags(s, m-s,
5329 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5333 /* skip the whitespace found last */
5335 s = m + UTF8SKIP(m);
5339 /* this one uses 's' and is a positive test */
5341 while (s < strend && ( *s == ' ' || is_utf8_space((U8*)s) ))
5344 else if (get_regex_charset(RX_EXTFLAGS(rx)) == REGEX_LOCALE_CHARSET) {
5345 while (s < strend && isSPACE_LC(*s))
5348 while (s < strend && isSPACE(*s))
5353 else if (RX_EXTFLAGS(rx) & RXf_START_ONLY) {
5355 for (m = s; m < strend && *m != '\n'; m++)
5368 dstr = newSVpvn_flags(s, m-s,
5369 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5375 else if (RX_EXTFLAGS(rx) & RXf_NULL && !(s >= strend)) {
5377 Pre-extend the stack, either the number of bytes or
5378 characters in the string or a limited amount, triggered by:
5380 my ($x, $y) = split //, $str;
5384 if (!gimme_scalar) {
5385 const U32 items = limit - 1;
5394 /* keep track of how many bytes we skip over */
5404 dstr = newSVpvn_flags(m, s-m, SVf_UTF8 | make_mortal);
5417 dstr = newSVpvn(s, 1);
5433 else if (do_utf8 == (RX_UTF8(rx) != 0) &&
5434 (RX_EXTFLAGS(rx) & RXf_USE_INTUIT) && !RX_NPARENS(rx)
5435 && (RX_EXTFLAGS(rx) & RXf_CHECK_ALL)
5436 && !(RX_EXTFLAGS(rx) & RXf_ANCH)) {
5437 const int tail = (RX_EXTFLAGS(rx) & RXf_INTUIT_TAIL);
5438 SV * const csv = CALLREG_INTUIT_STRING(rx);
5440 len = RX_MINLENRET(rx);
5441 if (len == 1 && !RX_UTF8(rx) && !tail) {
5442 const char c = *SvPV_nolen_const(csv);
5444 for (m = s; m < strend && *m != c; m++)
5455 dstr = newSVpvn_flags(s, m-s,
5456 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5459 /* The rx->minlen is in characters but we want to step
5460 * s ahead by bytes. */
5462 s = (char*)utf8_hop((U8*)m, len);
5464 s = m + len; /* Fake \n at the end */
5468 while (s < strend && --limit &&
5469 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5470 csv, multiline ? FBMrf_MULTILINE : 0)) )
5479 dstr = newSVpvn_flags(s, m-s,
5480 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5483 /* The rx->minlen is in characters but we want to step
5484 * s ahead by bytes. */
5486 s = (char*)utf8_hop((U8*)m, len);
5488 s = m + len; /* Fake \n at the end */
5493 maxiters += slen * RX_NPARENS(rx);
5494 while (s < strend && --limit)
5498 rex_return = CALLREGEXEC(rx, (char*)s, (char*)strend, (char*)orig, 1 ,
5499 sv, NULL, SvSCREAM(sv) ? REXEC_SCREAM : 0);
5501 if (rex_return == 0)
5503 TAINT_IF(RX_MATCH_TAINTED(rx));
5504 if (RX_MATCH_COPIED(rx) && RX_SUBBEG(rx) != orig) {
5507 orig = RX_SUBBEG(rx);
5509 strend = s + (strend - m);
5511 m = RX_OFFS(rx)[0].start + orig;
5520 dstr = newSVpvn_flags(s, m-s,
5521 (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5524 if (RX_NPARENS(rx)) {
5526 for (i = 1; i <= (I32)RX_NPARENS(rx); i++) {
5527 s = RX_OFFS(rx)[i].start + orig;
5528 m = RX_OFFS(rx)[i].end + orig;
5530 /* japhy (07/27/01) -- the (m && s) test doesn't catch
5531 parens that didn't match -- they should be set to
5532 undef, not the empty string */
5540 if (m >= orig && s >= orig) {
5541 dstr = newSVpvn_flags(s, m-s,
5542 (do_utf8 ? SVf_UTF8 : 0)
5546 dstr = &PL_sv_undef; /* undef, not "" */
5552 s = RX_OFFS(rx)[0].end + orig;
5556 if (!gimme_scalar) {
5557 iters = (SP - PL_stack_base) - base;
5559 if (iters > maxiters)
5560 DIE(aTHX_ "Split loop");
5562 /* keep field after final delim? */
5563 if (s < strend || (iters && origlimit)) {
5564 if (!gimme_scalar) {
5565 const STRLEN l = strend - s;
5566 dstr = newSVpvn_flags(s, l, (do_utf8 ? SVf_UTF8 : 0) | make_mortal);
5571 else if (!origlimit) {
5573 iters -= trailing_empty;
5575 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0)) {
5576 if (TOPs && !make_mortal)
5578 *SP-- = &PL_sv_undef;
5585 LEAVE_SCOPE(oldsave); /* may undo an earlier SWITCHSTACK */
5589 if (SvSMAGICAL(ary)) {
5591 mg_set(MUTABLE_SV(ary));
5594 if (gimme == G_ARRAY) {
5596 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5603 ENTER_with_name("call_PUSH");
5604 call_method("PUSH",G_SCALAR|G_DISCARD);
5605 LEAVE_with_name("call_PUSH");
5607 if (gimme == G_ARRAY) {
5609 /* EXTEND should not be needed - we just popped them */
5611 for (i=0; i < iters; i++) {
5612 SV **svp = av_fetch(ary, i, FALSE);
5613 PUSHs((svp) ? *svp : &PL_sv_undef);
5620 if (gimme == G_ARRAY)
5632 SV *const sv = PAD_SVl(PL_op->op_targ);
5634 if (SvPADSTALE(sv)) {
5637 RETURNOP(cLOGOP->op_other);
5639 RETURNOP(cLOGOP->op_next);
5649 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5650 || SvTYPE(retsv) == SVt_PVCV) {
5651 retsv = refto(retsv);
5658 PP(unimplemented_op)
5661 const Optype op_type = PL_op->op_type;
5662 /* Using OP_NAME() isn't going to be helpful here. Firstly, it doesn't cope
5663 with out of range op numbers - it only "special" cases op_custom.
5664 Secondly, as the three ops we "panic" on are padmy, mapstart and custom,
5665 if we get here for a custom op then that means that the custom op didn't
5666 have an implementation. Given that OP_NAME() looks up the custom op
5667 by its pp_addr, likely it will return NULL, unless someone (unhelpfully)
5668 registers &PL_unimplemented_op as the address of their custom op.
5669 NULL doesn't generate a useful error message. "custom" does. */
5670 const char *const name = op_type >= OP_max
5671 ? "[out of range]" : PL_op_name[PL_op->op_type];
5672 if(OP_IS_SOCKET(op_type))
5673 DIE(aTHX_ PL_no_sock_func, name);
5674 DIE(aTHX_ "panic: unimplemented op %s (#%d) called", name, op_type);
5681 HV * const hv = (HV*)POPs;
5683 if (SvTYPE(hv) != SVt_PVHV) { XPUSHs(&PL_sv_no); RETURN; }
5685 if (SvRMAGICAL(hv)) {
5686 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
5688 XPUSHs(magic_scalarpack(hv, mg));
5693 XPUSHs(boolSV(HvUSEDKEYS(hv) != 0));
5697 /* For sorting out arguments passed to a &CORE:: subroutine */
5701 int opnum = SvIOK(cSVOP_sv) ? (int)SvUV(cSVOP_sv) : 0;
5702 int defgv = PL_opargs[opnum] & OA_DEFGV, whicharg = 0;
5703 AV * const at_ = GvAV(PL_defgv);
5704 SV **svp = AvARRAY(at_);
5705 I32 minargs = 0, maxargs = 0, numargs = AvFILLp(at_)+1;
5706 I32 oa = opnum ? PL_opargs[opnum] >> OASHIFT : 0;
5707 bool seen_question = 0;
5708 const char *err = NULL;
5709 const bool pushmark = PL_op->op_private & OPpCOREARGS_PUSHMARK;
5711 /* Count how many args there are first, to get some idea how far to
5712 extend the stack. */
5714 if ((oa & 7) == OA_LIST) { maxargs = I32_MAX; break; }
5716 if (oa & OA_OPTIONAL) seen_question = 1;
5717 if (!seen_question) minargs++;
5721 if(numargs < minargs) err = "Not enough";
5722 else if(numargs > maxargs) err = "Too many";
5724 /* diag_listed_as: Too many arguments for %s */
5726 "%s arguments for %s", err,
5727 opnum ? OP_DESC(PL_op->op_next) : SvPV_nolen_const(cSVOP_sv)
5730 /* Reset the stack pointer. Without this, we end up returning our own
5731 arguments in list context, in addition to the values we are supposed
5732 to return. nextstate usually does this on sub entry, but we need
5733 to run the next op with the caller's hints, so we cannot have a
5735 SP = PL_stack_base + cxstack[cxstack_ix].blk_oldsp;
5737 if(!maxargs) RETURN;
5739 /* We do this here, rather than with a separate pushmark op, as it has
5740 to come in between two things this function does (stack reset and
5741 arg pushing). This seems the easiest way to do it. */
5744 (void)Perl_pp_pushmark(aTHX);
5747 EXTEND(SP, maxargs == I32_MAX ? numargs : maxargs);
5748 PUTBACK; /* The code below can die in various places. */
5750 oa = PL_opargs[opnum] >> OASHIFT;
5751 for (; oa&&(numargs||!pushmark); (void)(numargs&&(++svp,--numargs))) {
5755 if (!numargs && defgv && whicharg == minargs + 1) {
5756 PERL_SI * const oldsi = PL_curstackinfo;
5757 I32 const oldcxix = oldsi->si_cxix;
5759 if (oldcxix) oldsi->si_cxix--;
5760 else PL_curstackinfo = oldsi->si_prev;
5761 caller = find_runcv(NULL);
5762 PL_curstackinfo = oldsi;
5763 oldsi->si_cxix = oldcxix;
5764 PUSHs(find_rundefsv2(
5765 caller,cxstack[cxstack_ix].blk_oldcop->cop_seq
5768 else PUSHs(numargs ? svp && *svp ? *svp : &PL_sv_undef : NULL);
5772 PUSHs(svp && *svp ? *svp : &PL_sv_undef);
5777 if (!svp || !*svp || !SvROK(*svp)
5778 || SvTYPE(SvRV(*svp)) != SVt_PVHV)
5780 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5781 "Type of arg %d to &CORE::%s must be hash reference",
5782 whicharg, OP_DESC(PL_op->op_next)
5787 if (!numargs) PUSHs(NULL);
5788 else if(svp && *svp && SvROK(*svp) && isGV_with_GP(SvRV(*svp)))
5789 /* no magic here, as the prototype will have added an extra
5790 refgen and we just want what was there before that */
5793 const bool constr = PL_op->op_private & whicharg;
5795 svp && *svp ? *svp : &PL_sv_undef,
5796 constr, CopHINTS_get(PL_curcop) & HINT_STRICT_REFS,
5803 const bool wantscalar =
5804 PL_op->op_private & OPpCOREARGS_SCALARMOD;
5805 if (!svp || !*svp || !SvROK(*svp)
5806 /* We have to permit globrefs even for the \$ proto, as
5807 *foo is indistinguishable from ${\*foo}, and the proto-
5808 type permits the latter. */
5809 || SvTYPE(SvRV(*svp)) > (
5810 wantscalar ? SVt_PVLV
5811 : opnum == OP_LOCK ? SVt_PVCV
5816 /* diag_listed_as: Type of arg %d to &CORE::%s must be %s*/
5817 "Type of arg %d to &CORE::%s must be %s",
5818 whicharg, OP_DESC(PL_op->op_next),
5820 ? "scalar reference"
5822 ? "reference to one of [$@%&*]"
5823 : "reference to one of [$@%*]"
5829 DIE(aTHX_ "panic: unknown OA_*: %x", (unsigned)(oa&7));
5841 if (PL_op->op_private & OPpOFFBYONE) {
5842 PERL_SI * const oldsi = PL_curstackinfo;
5843 I32 const oldcxix = oldsi->si_cxix;
5844 if (oldcxix) oldsi->si_cxix--;
5845 else PL_curstackinfo = oldsi->si_prev;
5846 cv = find_runcv(NULL);
5847 PL_curstackinfo = oldsi;
5848 oldsi->si_cxix = oldcxix;
5850 else cv = find_runcv(NULL);
5851 XPUSHs(CvUNIQUE(cv) ? &PL_sv_undef : sv_2mortal(newRV((SV *)cv)));
5858 * c-indentation-style: bsd
5860 * indent-tabs-mode: t
5863 * ex: set ts=8 sts=4 sw=4 noet: