3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "It's a big house this, and very peculiar. Always a bit more to discover,
12 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
19 /* variations on pp_null */
21 /* XXX I can't imagine anyone who doesn't have this actually _needs_
22 it, since pid_t is an integral type.
25 #ifdef NEED_GETPID_PROTO
26 extern Pid_t getpid (void);
32 if (GIMME_V == G_SCALAR)
47 if (PL_op->op_private & OPpLVAL_INTRO)
48 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
50 if (PL_op->op_flags & OPf_REF) {
54 if (GIMME == G_SCALAR)
55 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
59 if (GIMME == G_ARRAY) {
60 I32 maxarg = AvFILL((AV*)TARG) + 1;
62 if (SvMAGICAL(TARG)) {
64 for (i=0; i < maxarg; i++) {
65 SV **svp = av_fetch((AV*)TARG, i, FALSE);
66 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
70 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
75 SV* sv = sv_newmortal();
76 I32 maxarg = AvFILL((AV*)TARG) + 1;
89 if (PL_op->op_private & OPpLVAL_INTRO)
90 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
91 if (PL_op->op_flags & OPf_REF)
94 if (GIMME == G_SCALAR)
95 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
99 if (gimme == G_ARRAY) {
102 else if (gimme == G_SCALAR) {
103 SV* sv = sv_newmortal();
104 if (HvFILL((HV*)TARG))
105 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
106 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
116 DIE(aTHX_ "NOT IMPL LINE %d",__LINE__);
127 tryAMAGICunDEREF(to_gv);
130 if (SvTYPE(sv) == SVt_PVIO) {
131 GV *gv = (GV*) sv_newmortal();
132 gv_init(gv, 0, "", 0, 0);
133 GvIOp(gv) = (IO *)sv;
134 (void)SvREFCNT_inc(sv);
137 else if (SvTYPE(sv) != SVt_PVGV)
138 DIE(aTHX_ "Not a GLOB reference");
141 if (SvTYPE(sv) != SVt_PVGV) {
145 if (SvGMAGICAL(sv)) {
150 if (!SvOK(sv) && sv != &PL_sv_undef) {
151 /* If this is a 'my' scalar and flag is set then vivify
154 if (PL_op->op_private & OPpDEREF) {
157 if (cUNOP->op_targ) {
159 SV *namesv = PL_curpad[cUNOP->op_targ];
160 name = SvPV(namesv, len);
161 gv = (GV*)NEWSV(0,0);
162 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
165 name = CopSTASHPV(PL_curcop);
168 if (SvTYPE(sv) < SVt_RV)
169 sv_upgrade(sv, SVt_RV);
175 if (PL_op->op_flags & OPf_REF ||
176 PL_op->op_private & HINT_STRICT_REFS)
177 DIE(aTHX_ PL_no_usym, "a symbol");
178 if (ckWARN(WARN_UNINITIALIZED))
183 if ((PL_op->op_flags & OPf_SPECIAL) &&
184 !(PL_op->op_flags & OPf_MOD))
186 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
188 && (!is_gv_magical(sym,len,0)
189 || !(sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV))))
195 if (PL_op->op_private & HINT_STRICT_REFS)
196 DIE(aTHX_ PL_no_symref, sym, "a symbol");
197 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
201 if (PL_op->op_private & OPpLVAL_INTRO)
202 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
213 tryAMAGICunDEREF(to_sv);
216 switch (SvTYPE(sv)) {
220 DIE(aTHX_ "Not a SCALAR reference");
228 if (SvTYPE(gv) != SVt_PVGV) {
229 if (SvGMAGICAL(sv)) {
235 if (PL_op->op_flags & OPf_REF ||
236 PL_op->op_private & HINT_STRICT_REFS)
237 DIE(aTHX_ PL_no_usym, "a SCALAR");
238 if (ckWARN(WARN_UNINITIALIZED))
243 if ((PL_op->op_flags & OPf_SPECIAL) &&
244 !(PL_op->op_flags & OPf_MOD))
246 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
248 && (!is_gv_magical(sym,len,0)
249 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV))))
255 if (PL_op->op_private & HINT_STRICT_REFS)
256 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
257 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
262 if (PL_op->op_flags & OPf_MOD) {
263 if (PL_op->op_private & OPpLVAL_INTRO)
264 sv = save_scalar((GV*)TOPs);
265 else if (PL_op->op_private & OPpDEREF)
266 vivify_ref(sv, PL_op->op_private & OPpDEREF);
276 SV *sv = AvARYLEN(av);
278 AvARYLEN(av) = sv = NEWSV(0,0);
279 sv_upgrade(sv, SVt_IV);
280 sv_magic(sv, (SV*)av, PERL_MAGIC_arylen, Nullch, 0);
288 dSP; dTARGET; dPOPss;
290 if (PL_op->op_flags & OPf_MOD || LVRET) {
291 if (SvTYPE(TARG) < SVt_PVLV) {
292 sv_upgrade(TARG, SVt_PVLV);
293 sv_magic(TARG, Nullsv, PERL_MAGIC_pos, Nullch, 0);
297 if (LvTARG(TARG) != sv) {
299 SvREFCNT_dec(LvTARG(TARG));
300 LvTARG(TARG) = SvREFCNT_inc(sv);
302 PUSHs(TARG); /* no SvSETMAGIC */
308 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
309 mg = mg_find(sv, PERL_MAGIC_regex_global);
310 if (mg && mg->mg_len >= 0) {
314 PUSHi(i + PL_curcop->cop_arybase);
328 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
329 /* (But not in defined().) */
330 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
333 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
334 if ((PL_op->op_private & OPpLVAL_INTRO)) {
335 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
338 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
342 cv = (CV*)&PL_sv_undef;
356 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
357 char *s = SvPVX(TOPs);
358 if (strnEQ(s, "CORE::", 6)) {
361 code = keyword(s + 6, SvCUR(TOPs) - 6);
362 if (code < 0) { /* Overridable. */
363 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
364 int i = 0, n = 0, seen_question = 0;
366 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
368 while (i < MAXO) { /* The slow way. */
369 if (strEQ(s + 6, PL_op_name[i])
370 || strEQ(s + 6, PL_op_desc[i]))
376 goto nonesuch; /* Should not happen... */
378 oa = PL_opargs[i] >> OASHIFT;
380 if (oa & OA_OPTIONAL && !seen_question) {
384 else if (n && str[0] == ';' && seen_question)
385 goto set; /* XXXX system, exec */
386 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
387 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
388 /* But globs are already references (kinda) */
389 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
393 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
397 ret = sv_2mortal(newSVpvn(str, n - 1));
399 else if (code) /* Non-Overridable */
401 else { /* None such */
403 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
407 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
409 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
418 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
420 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
436 if (GIMME != G_ARRAY) {
440 *MARK = &PL_sv_undef;
441 *MARK = refto(*MARK);
445 EXTEND_MORTAL(SP - MARK);
447 *MARK = refto(*MARK);
452 S_refto(pTHX_ SV *sv)
456 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
459 if (!(sv = LvTARG(sv)))
462 (void)SvREFCNT_inc(sv);
464 else if (SvTYPE(sv) == SVt_PVAV) {
465 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
468 (void)SvREFCNT_inc(sv);
470 else if (SvPADTMP(sv) && !IS_PADGV(sv))
474 (void)SvREFCNT_inc(sv);
477 sv_upgrade(rv, SVt_RV);
491 if (sv && SvGMAGICAL(sv))
494 if (!sv || !SvROK(sv))
498 pv = sv_reftype(sv,TRUE);
499 PUSHp(pv, strlen(pv));
509 stash = CopSTASH(PL_curcop);
515 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
516 Perl_croak(aTHX_ "Attempt to bless into a reference");
518 if (ckWARN(WARN_MISC) && len == 0)
519 Perl_warner(aTHX_ WARN_MISC,
520 "Explicit blessing to '' (assuming package main)");
521 stash = gv_stashpvn(ptr, len, TRUE);
524 (void)sv_bless(TOPs, stash);
538 elem = SvPV(sv, n_a);
542 switch (elem ? *elem : '\0')
545 if (strEQ(elem, "ARRAY"))
546 tmpRef = (SV*)GvAV(gv);
549 if (strEQ(elem, "CODE"))
550 tmpRef = (SV*)GvCVu(gv);
553 if (strEQ(elem, "FILEHANDLE")) {
554 /* finally deprecated in 5.8.0 */
555 deprecate("*glob{FILEHANDLE}");
556 tmpRef = (SV*)GvIOp(gv);
559 if (strEQ(elem, "FORMAT"))
560 tmpRef = (SV*)GvFORM(gv);
563 if (strEQ(elem, "GLOB"))
567 if (strEQ(elem, "HASH"))
568 tmpRef = (SV*)GvHV(gv);
571 if (strEQ(elem, "IO"))
572 tmpRef = (SV*)GvIOp(gv);
575 if (strEQ(elem, "NAME"))
576 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
579 if (strEQ(elem, "PACKAGE"))
580 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
583 if (strEQ(elem, "SCALAR"))
597 /* Pattern matching */
602 register unsigned char *s;
605 register I32 *sfirst;
609 if (sv == PL_lastscream) {
615 SvSCREAM_off(PL_lastscream);
616 SvREFCNT_dec(PL_lastscream);
618 PL_lastscream = SvREFCNT_inc(sv);
621 s = (unsigned char*)(SvPV(sv, len));
625 if (pos > PL_maxscream) {
626 if (PL_maxscream < 0) {
627 PL_maxscream = pos + 80;
628 New(301, PL_screamfirst, 256, I32);
629 New(302, PL_screamnext, PL_maxscream, I32);
632 PL_maxscream = pos + pos / 4;
633 Renew(PL_screamnext, PL_maxscream, I32);
637 sfirst = PL_screamfirst;
638 snext = PL_screamnext;
640 if (!sfirst || !snext)
641 DIE(aTHX_ "do_study: out of memory");
643 for (ch = 256; ch; --ch)
650 snext[pos] = sfirst[ch] - pos;
657 /* piggyback on m//g magic */
658 sv_magic(sv, Nullsv, PERL_MAGIC_regex_global, Nullch, 0);
667 if (PL_op->op_flags & OPf_STACKED)
673 TARG = sv_newmortal();
678 /* Lvalue operators. */
690 dSP; dMARK; dTARGET; dORIGMARK;
692 do_chop(TARG, *++MARK);
701 SETi(do_chomp(TOPs));
708 register I32 count = 0;
711 count += do_chomp(POPs);
722 if (!sv || !SvANY(sv))
724 switch (SvTYPE(sv)) {
726 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
727 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
731 if (HvARRAY(sv) || SvGMAGICAL(sv)
732 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
736 if (CvROOT(sv) || CvXSUB(sv))
753 if (!PL_op->op_private) {
762 if (SvTHINKFIRST(sv))
765 switch (SvTYPE(sv)) {
775 if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv))
776 Perl_warner(aTHX_ WARN_MISC, "Constant subroutine %s undefined",
777 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
781 /* let user-undef'd sub keep its identity */
782 GV* gv = CvGV((CV*)sv);
789 SvSetMagicSV(sv, &PL_sv_undef);
793 Newz(602, gp, 1, GP);
794 GvGP(sv) = gp_ref(gp);
795 GvSV(sv) = NEWSV(72,0);
796 GvLINE(sv) = CopLINE(PL_curcop);
802 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
805 SvPV_set(sv, Nullch);
818 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
819 DIE(aTHX_ PL_no_modify);
820 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
821 SvIVX(TOPs) != IV_MIN)
824 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
835 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
836 DIE(aTHX_ PL_no_modify);
837 sv_setsv(TARG, TOPs);
838 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
839 SvIVX(TOPs) != IV_MAX)
842 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
856 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
857 DIE(aTHX_ PL_no_modify);
858 sv_setsv(TARG, TOPs);
859 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
860 SvIVX(TOPs) != IV_MIN)
863 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
872 /* Ordinary operators. */
876 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
879 SETn( Perl_pow( left, right) );
886 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
887 #ifdef PERL_PRESERVE_IVUV
890 /* Unless the left argument is integer in range we are going to have to
891 use NV maths. Hence only attempt to coerce the right argument if
892 we know the left is integer. */
893 /* Left operand is defined, so is it IV? */
896 bool auvok = SvUOK(TOPm1s);
897 bool buvok = SvUOK(TOPs);
898 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
899 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
906 alow = SvUVX(TOPm1s);
908 IV aiv = SvIVX(TOPm1s);
911 auvok = TRUE; /* effectively it's a UV now */
913 alow = -aiv; /* abs, auvok == false records sign */
919 IV biv = SvIVX(TOPs);
922 buvok = TRUE; /* effectively it's a UV now */
924 blow = -biv; /* abs, buvok == false records sign */
928 /* If this does sign extension on unsigned it's time for plan B */
929 ahigh = alow >> (4 * sizeof (UV));
931 bhigh = blow >> (4 * sizeof (UV));
933 if (ahigh && bhigh) {
934 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
935 which is overflow. Drop to NVs below. */
936 } else if (!ahigh && !bhigh) {
937 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
938 so the unsigned multiply cannot overflow. */
939 UV product = alow * blow;
940 if (auvok == buvok) {
941 /* -ve * -ve or +ve * +ve gives a +ve result. */
945 } else if (product <= (UV)IV_MIN) {
946 /* 2s complement assumption that (UV)-IV_MIN is correct. */
947 /* -ve result, which could overflow an IV */
949 SETi( -(IV)product );
951 } /* else drop to NVs below. */
953 /* One operand is large, 1 small */
956 /* swap the operands */
958 bhigh = blow; /* bhigh now the temp var for the swap */
962 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
963 multiplies can't overflow. shift can, add can, -ve can. */
964 product_middle = ahigh * blow;
965 if (!(product_middle & topmask)) {
966 /* OK, (ahigh * blow) won't lose bits when we shift it. */
968 product_middle <<= (4 * sizeof (UV));
969 product_low = alow * blow;
971 /* as for pp_add, UV + something mustn't get smaller.
972 IIRC ANSI mandates this wrapping *behaviour* for
973 unsigned whatever the actual representation*/
974 product_low += product_middle;
975 if (product_low >= product_middle) {
976 /* didn't overflow */
977 if (auvok == buvok) {
978 /* -ve * -ve or +ve * +ve gives a +ve result. */
982 } else if (product_low <= (UV)IV_MIN) {
983 /* 2s complement assumption again */
984 /* -ve result, which could overflow an IV */
986 SETi( -(IV)product_low );
988 } /* else drop to NVs below. */
990 } /* product_middle too large */
991 } /* ahigh && bhigh */
992 } /* SvIOK(TOPm1s) */
997 SETn( left * right );
1004 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1005 /* Only try to do UV divide first
1006 if ((SLOPPYDIVIDE is true) or
1007 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1009 The assumption is that it is better to use floating point divide
1010 whenever possible, only doing integer divide first if we can't be sure.
1011 If NV_PRESERVES_UV is true then we know at compile time that no UV
1012 can be too large to preserve, so don't need to compile the code to
1013 test the size of UVs. */
1016 # define PERL_TRY_UV_DIVIDE
1017 /* ensure that 20./5. == 4. */
1019 # ifdef PERL_PRESERVE_IVUV
1020 # ifndef NV_PRESERVES_UV
1021 # define PERL_TRY_UV_DIVIDE
1026 #ifdef PERL_TRY_UV_DIVIDE
1029 SvIV_please(TOPm1s);
1030 if (SvIOK(TOPm1s)) {
1031 bool left_non_neg = SvUOK(TOPm1s);
1032 bool right_non_neg = SvUOK(TOPs);
1036 if (right_non_neg) {
1037 right = SvUVX(TOPs);
1040 IV biv = SvIVX(TOPs);
1043 right_non_neg = TRUE; /* effectively it's a UV now */
1049 /* historically undef()/0 gives a "Use of uninitialized value"
1050 warning before dieing, hence this test goes here.
1051 If it were immediately before the second SvIV_please, then
1052 DIE() would be invoked before left was even inspected, so
1053 no inpsection would give no warning. */
1055 DIE(aTHX_ "Illegal division by zero");
1058 left = SvUVX(TOPm1s);
1061 IV aiv = SvIVX(TOPm1s);
1064 left_non_neg = TRUE; /* effectively it's a UV now */
1073 /* For sloppy divide we always attempt integer division. */
1075 /* Otherwise we only attempt it if either or both operands
1076 would not be preserved by an NV. If both fit in NVs
1077 we fall through to the NV divide code below. However,
1078 as left >= right to ensure integer result here, we know that
1079 we can skip the test on the right operand - right big
1080 enough not to be preserved can't get here unless left is
1083 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1086 /* Integer division can't overflow, but it can be imprecise. */
1087 UV result = left / right;
1088 if (result * right == left) {
1089 SP--; /* result is valid */
1090 if (left_non_neg == right_non_neg) {
1091 /* signs identical, result is positive. */
1095 /* 2s complement assumption */
1096 if (result <= (UV)IV_MIN)
1099 /* It's exact but too negative for IV. */
1100 SETn( -(NV)result );
1103 } /* tried integer divide but it was not an integer result */
1104 } /* else (abs(result) < 1.0) or (both UVs in range for NV) */
1105 } /* left wasn't SvIOK */
1106 } /* right wasn't SvIOK */
1107 #endif /* PERL_TRY_UV_DIVIDE */
1111 DIE(aTHX_ "Illegal division by zero");
1112 PUSHn( left / right );
1119 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1125 bool use_double = FALSE;
1126 bool dright_valid = FALSE;
1132 right_neg = !SvUOK(TOPs);
1134 right = SvUVX(POPs);
1136 IV biv = SvIVX(POPs);
1139 right_neg = FALSE; /* effectively it's a UV now */
1147 right_neg = dright < 0;
1150 if (dright < UV_MAX_P1) {
1151 right = U_V(dright);
1152 dright_valid = TRUE; /* In case we need to use double below. */
1158 /* At this point use_double is only true if right is out of range for
1159 a UV. In range NV has been rounded down to nearest UV and
1160 use_double false. */
1162 if (!use_double && SvIOK(TOPs)) {
1164 left_neg = !SvUOK(TOPs);
1168 IV aiv = SvIVX(POPs);
1171 left_neg = FALSE; /* effectively it's a UV now */
1180 left_neg = dleft < 0;
1184 /* This should be exactly the 5.6 behaviour - if left and right are
1185 both in range for UV then use U_V() rather than floor. */
1187 if (dleft < UV_MAX_P1) {
1188 /* right was in range, so is dleft, so use UVs not double.
1192 /* left is out of range for UV, right was in range, so promote
1193 right (back) to double. */
1195 /* The +0.5 is used in 5.6 even though it is not strictly
1196 consistent with the implicit +0 floor in the U_V()
1197 inside the #if 1. */
1198 dleft = Perl_floor(dleft + 0.5);
1201 dright = Perl_floor(dright + 0.5);
1211 DIE(aTHX_ "Illegal modulus zero");
1213 dans = Perl_fmod(dleft, dright);
1214 if ((left_neg != right_neg) && dans)
1215 dans = dright - dans;
1218 sv_setnv(TARG, dans);
1224 DIE(aTHX_ "Illegal modulus zero");
1227 if ((left_neg != right_neg) && ans)
1230 /* XXX may warn: unary minus operator applied to unsigned type */
1231 /* could change -foo to be (~foo)+1 instead */
1232 if (ans <= ~((UV)IV_MAX)+1)
1233 sv_setiv(TARG, ~ans+1);
1235 sv_setnv(TARG, -(NV)ans);
1238 sv_setuv(TARG, ans);
1247 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1249 register IV count = POPi;
1250 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1252 I32 items = SP - MARK;
1255 max = items * count;
1260 /* This code was intended to fix 20010809.028:
1263 for (($x =~ /./g) x 2) {
1264 print chop; # "abcdabcd" expected as output.
1267 * but that change (#11635) broke this code:
1269 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1271 * I can't think of a better fix that doesn't introduce
1272 * an efficiency hit by copying the SVs. The stack isn't
1273 * refcounted, and mortalisation obviously doesn't
1274 * Do The Right Thing when the stack has more than
1275 * one pointer to the same mortal value.
1279 *SP = sv_2mortal(newSVsv(*SP));
1289 repeatcpy((char*)(MARK + items), (char*)MARK,
1290 items * sizeof(SV*), count - 1);
1293 else if (count <= 0)
1296 else { /* Note: mark already snarfed by pp_list */
1301 SvSetSV(TARG, tmpstr);
1302 SvPV_force(TARG, len);
1303 isutf = DO_UTF8(TARG);
1308 SvGROW(TARG, (count * len) + 1);
1309 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1310 SvCUR(TARG) *= count;
1312 *SvEND(TARG) = '\0';
1315 (void)SvPOK_only_UTF8(TARG);
1317 (void)SvPOK_only(TARG);
1319 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1320 /* The parser saw this as a list repeat, and there
1321 are probably several items on the stack. But we're
1322 in scalar context, and there's no pp_list to save us
1323 now. So drop the rest of the items -- robin@kitsite.com
1336 dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN);
1337 useleft = USE_LEFT(TOPm1s);
1338 #ifdef PERL_PRESERVE_IVUV
1339 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1340 "bad things" happen if you rely on signed integers wrapping. */
1343 /* Unless the left argument is integer in range we are going to have to
1344 use NV maths. Hence only attempt to coerce the right argument if
1345 we know the left is integer. */
1346 register UV auv = 0;
1352 a_valid = auvok = 1;
1353 /* left operand is undef, treat as zero. */
1355 /* Left operand is defined, so is it IV? */
1356 SvIV_please(TOPm1s);
1357 if (SvIOK(TOPm1s)) {
1358 if ((auvok = SvUOK(TOPm1s)))
1359 auv = SvUVX(TOPm1s);
1361 register IV aiv = SvIVX(TOPm1s);
1364 auvok = 1; /* Now acting as a sign flag. */
1365 } else { /* 2s complement assumption for IV_MIN */
1373 bool result_good = 0;
1376 bool buvok = SvUOK(TOPs);
1381 register IV biv = SvIVX(TOPs);
1388 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1389 else "IV" now, independant of how it came in.
1390 if a, b represents positive, A, B negative, a maps to -A etc
1395 all UV maths. negate result if A negative.
1396 subtract if signs same, add if signs differ. */
1398 if (auvok ^ buvok) {
1407 /* Must get smaller */
1412 if (result <= buv) {
1413 /* result really should be -(auv-buv). as its negation
1414 of true value, need to swap our result flag */
1426 if (result <= (UV)IV_MIN)
1427 SETi( -(IV)result );
1429 /* result valid, but out of range for IV. */
1430 SETn( -(NV)result );
1434 } /* Overflow, drop through to NVs. */
1438 useleft = USE_LEFT(TOPm1s);
1442 /* left operand is undef, treat as zero - value */
1446 SETn( TOPn - value );
1453 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1456 if (PL_op->op_private & HINT_INTEGER) {
1470 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1473 if (PL_op->op_private & HINT_INTEGER) {
1487 dSP; tryAMAGICbinSET(lt,0);
1488 #ifdef PERL_PRESERVE_IVUV
1491 SvIV_please(TOPm1s);
1492 if (SvIOK(TOPm1s)) {
1493 bool auvok = SvUOK(TOPm1s);
1494 bool buvok = SvUOK(TOPs);
1496 if (!auvok && !buvok) { /* ## IV < IV ## */
1497 IV aiv = SvIVX(TOPm1s);
1498 IV biv = SvIVX(TOPs);
1501 SETs(boolSV(aiv < biv));
1504 if (auvok && buvok) { /* ## UV < UV ## */
1505 UV auv = SvUVX(TOPm1s);
1506 UV buv = SvUVX(TOPs);
1509 SETs(boolSV(auv < buv));
1512 if (auvok) { /* ## UV < IV ## */
1519 /* As (a) is a UV, it's >=0, so it cannot be < */
1524 SETs(boolSV(auv < (UV)biv));
1527 { /* ## IV < UV ## */
1531 aiv = SvIVX(TOPm1s);
1533 /* As (b) is a UV, it's >=0, so it must be < */
1540 SETs(boolSV((UV)aiv < buv));
1546 #ifndef NV_PRESERVES_UV
1547 else if (SvROK(TOPs) && SvROK(TOPm1s)) {
1549 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1555 SETs(boolSV(TOPn < value));
1562 dSP; tryAMAGICbinSET(gt,0);
1563 #ifdef PERL_PRESERVE_IVUV
1566 SvIV_please(TOPm1s);
1567 if (SvIOK(TOPm1s)) {
1568 bool auvok = SvUOK(TOPm1s);
1569 bool buvok = SvUOK(TOPs);
1571 if (!auvok && !buvok) { /* ## IV > IV ## */
1572 IV aiv = SvIVX(TOPm1s);
1573 IV biv = SvIVX(TOPs);
1576 SETs(boolSV(aiv > biv));
1579 if (auvok && buvok) { /* ## UV > UV ## */
1580 UV auv = SvUVX(TOPm1s);
1581 UV buv = SvUVX(TOPs);
1584 SETs(boolSV(auv > buv));
1587 if (auvok) { /* ## UV > IV ## */
1594 /* As (a) is a UV, it's >=0, so it must be > */
1599 SETs(boolSV(auv > (UV)biv));
1602 { /* ## IV > UV ## */
1606 aiv = SvIVX(TOPm1s);
1608 /* As (b) is a UV, it's >=0, so it cannot be > */
1615 SETs(boolSV((UV)aiv > buv));
1621 #ifndef NV_PRESERVES_UV
1622 else if (SvROK(TOPs) && SvROK(TOPm1s)) {
1624 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1630 SETs(boolSV(TOPn > value));
1637 dSP; tryAMAGICbinSET(le,0);
1638 #ifdef PERL_PRESERVE_IVUV
1641 SvIV_please(TOPm1s);
1642 if (SvIOK(TOPm1s)) {
1643 bool auvok = SvUOK(TOPm1s);
1644 bool buvok = SvUOK(TOPs);
1646 if (!auvok && !buvok) { /* ## IV <= IV ## */
1647 IV aiv = SvIVX(TOPm1s);
1648 IV biv = SvIVX(TOPs);
1651 SETs(boolSV(aiv <= biv));
1654 if (auvok && buvok) { /* ## UV <= UV ## */
1655 UV auv = SvUVX(TOPm1s);
1656 UV buv = SvUVX(TOPs);
1659 SETs(boolSV(auv <= buv));
1662 if (auvok) { /* ## UV <= IV ## */
1669 /* As (a) is a UV, it's >=0, so a cannot be <= */
1674 SETs(boolSV(auv <= (UV)biv));
1677 { /* ## IV <= UV ## */
1681 aiv = SvIVX(TOPm1s);
1683 /* As (b) is a UV, it's >=0, so a must be <= */
1690 SETs(boolSV((UV)aiv <= buv));
1696 #ifndef NV_PRESERVES_UV
1697 else if (SvROK(TOPs) && SvROK(TOPm1s)) {
1699 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1705 SETs(boolSV(TOPn <= value));
1712 dSP; tryAMAGICbinSET(ge,0);
1713 #ifdef PERL_PRESERVE_IVUV
1716 SvIV_please(TOPm1s);
1717 if (SvIOK(TOPm1s)) {
1718 bool auvok = SvUOK(TOPm1s);
1719 bool buvok = SvUOK(TOPs);
1721 if (!auvok && !buvok) { /* ## IV >= IV ## */
1722 IV aiv = SvIVX(TOPm1s);
1723 IV biv = SvIVX(TOPs);
1726 SETs(boolSV(aiv >= biv));
1729 if (auvok && buvok) { /* ## UV >= UV ## */
1730 UV auv = SvUVX(TOPm1s);
1731 UV buv = SvUVX(TOPs);
1734 SETs(boolSV(auv >= buv));
1737 if (auvok) { /* ## UV >= IV ## */
1744 /* As (a) is a UV, it's >=0, so it must be >= */
1749 SETs(boolSV(auv >= (UV)biv));
1752 { /* ## IV >= UV ## */
1756 aiv = SvIVX(TOPm1s);
1758 /* As (b) is a UV, it's >=0, so a cannot be >= */
1765 SETs(boolSV((UV)aiv >= buv));
1771 #ifndef NV_PRESERVES_UV
1772 else if (SvROK(TOPs) && SvROK(TOPm1s)) {
1774 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
1780 SETs(boolSV(TOPn >= value));
1787 dSP; tryAMAGICbinSET(ne,0);
1788 #ifndef NV_PRESERVES_UV
1789 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1791 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
1795 #ifdef PERL_PRESERVE_IVUV
1798 SvIV_please(TOPm1s);
1799 if (SvIOK(TOPm1s)) {
1800 bool auvok = SvUOK(TOPm1s);
1801 bool buvok = SvUOK(TOPs);
1803 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
1804 /* Casting IV to UV before comparison isn't going to matter
1805 on 2s complement. On 1s complement or sign&magnitude
1806 (if we have any of them) it could make negative zero
1807 differ from normal zero. As I understand it. (Need to
1808 check - is negative zero implementation defined behaviour
1810 UV buv = SvUVX(POPs);
1811 UV auv = SvUVX(TOPs);
1813 SETs(boolSV(auv != buv));
1816 { /* ## Mixed IV,UV ## */
1820 /* != is commutative so swap if needed (save code) */
1822 /* swap. top of stack (b) is the iv */
1826 /* As (a) is a UV, it's >0, so it cannot be == */
1835 /* As (b) is a UV, it's >0, so it cannot be == */
1839 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
1841 SETs(boolSV((UV)iv != uv));
1849 SETs(boolSV(TOPn != value));
1856 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1857 #ifndef NV_PRESERVES_UV
1858 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1859 UV right = PTR2UV(SvRV(POPs));
1860 UV left = PTR2UV(SvRV(TOPs));
1861 SETi((left > right) - (left < right));
1865 #ifdef PERL_PRESERVE_IVUV
1866 /* Fortunately it seems NaN isn't IOK */
1869 SvIV_please(TOPm1s);
1870 if (SvIOK(TOPm1s)) {
1871 bool leftuvok = SvUOK(TOPm1s);
1872 bool rightuvok = SvUOK(TOPs);
1874 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
1875 IV leftiv = SvIVX(TOPm1s);
1876 IV rightiv = SvIVX(TOPs);
1878 if (leftiv > rightiv)
1880 else if (leftiv < rightiv)
1884 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
1885 UV leftuv = SvUVX(TOPm1s);
1886 UV rightuv = SvUVX(TOPs);
1888 if (leftuv > rightuv)
1890 else if (leftuv < rightuv)
1894 } else if (leftuvok) { /* ## UV <=> IV ## */
1898 rightiv = SvIVX(TOPs);
1900 /* As (a) is a UV, it's >=0, so it cannot be < */
1903 leftuv = SvUVX(TOPm1s);
1904 if (leftuv > (UV)rightiv) {
1906 } else if (leftuv < (UV)rightiv) {
1912 } else { /* ## IV <=> UV ## */
1916 leftiv = SvIVX(TOPm1s);
1918 /* As (b) is a UV, it's >=0, so it must be < */
1921 rightuv = SvUVX(TOPs);
1922 if ((UV)leftiv > rightuv) {
1924 } else if ((UV)leftiv < rightuv) {
1942 if (Perl_isnan(left) || Perl_isnan(right)) {
1946 value = (left > right) - (left < right);
1950 else if (left < right)
1952 else if (left > right)
1966 dSP; tryAMAGICbinSET(slt,0);
1969 int cmp = (IN_LOCALE_RUNTIME
1970 ? sv_cmp_locale(left, right)
1971 : sv_cmp(left, right));
1972 SETs(boolSV(cmp < 0));
1979 dSP; tryAMAGICbinSET(sgt,0);
1982 int cmp = (IN_LOCALE_RUNTIME
1983 ? sv_cmp_locale(left, right)
1984 : sv_cmp(left, right));
1985 SETs(boolSV(cmp > 0));
1992 dSP; tryAMAGICbinSET(sle,0);
1995 int cmp = (IN_LOCALE_RUNTIME
1996 ? sv_cmp_locale(left, right)
1997 : sv_cmp(left, right));
1998 SETs(boolSV(cmp <= 0));
2005 dSP; tryAMAGICbinSET(sge,0);
2008 int cmp = (IN_LOCALE_RUNTIME
2009 ? sv_cmp_locale(left, right)
2010 : sv_cmp(left, right));
2011 SETs(boolSV(cmp >= 0));
2018 dSP; tryAMAGICbinSET(seq,0);
2021 SETs(boolSV(sv_eq(left, right)));
2028 dSP; tryAMAGICbinSET(sne,0);
2031 SETs(boolSV(!sv_eq(left, right)));
2038 dSP; dTARGET; tryAMAGICbin(scmp,0);
2041 int cmp = (IN_LOCALE_RUNTIME
2042 ? sv_cmp_locale(left, right)
2043 : sv_cmp(left, right));
2051 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2054 if (SvNIOKp(left) || SvNIOKp(right)) {
2055 if (PL_op->op_private & HINT_INTEGER) {
2056 IV i = SvIV(left) & SvIV(right);
2060 UV u = SvUV(left) & SvUV(right);
2065 do_vop(PL_op->op_type, TARG, left, right);
2074 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
2077 if (SvNIOKp(left) || SvNIOKp(right)) {
2078 if (PL_op->op_private & HINT_INTEGER) {
2079 IV i = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
2083 UV u = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
2088 do_vop(PL_op->op_type, TARG, left, right);
2097 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
2100 if (SvNIOKp(left) || SvNIOKp(right)) {
2101 if (PL_op->op_private & HINT_INTEGER) {
2102 IV i = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
2106 UV u = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
2111 do_vop(PL_op->op_type, TARG, left, right);
2120 dSP; dTARGET; tryAMAGICun(neg);
2123 int flags = SvFLAGS(sv);
2126 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2127 /* It's publicly an integer, or privately an integer-not-float */
2130 if (SvIVX(sv) == IV_MIN) {
2131 /* 2s complement assumption. */
2132 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2135 else if (SvUVX(sv) <= IV_MAX) {
2140 else if (SvIVX(sv) != IV_MIN) {
2144 #ifdef PERL_PRESERVE_IVUV
2153 else if (SvPOKp(sv)) {
2155 char *s = SvPV(sv, len);
2156 if (isIDFIRST(*s)) {
2157 sv_setpvn(TARG, "-", 1);
2160 else if (*s == '+' || *s == '-') {
2162 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2164 else if (DO_UTF8(sv)) {
2167 goto oops_its_an_int;
2169 sv_setnv(TARG, -SvNV(sv));
2171 sv_setpvn(TARG, "-", 1);
2178 goto oops_its_an_int;
2179 sv_setnv(TARG, -SvNV(sv));
2191 dSP; tryAMAGICunSET(not);
2192 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2198 dSP; dTARGET; tryAMAGICun(compl);
2202 if (PL_op->op_private & HINT_INTEGER) {
2217 tmps = (U8*)SvPV_force(TARG, len);
2220 /* Calculate exact length, let's not estimate. */
2229 while (tmps < send) {
2230 UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2231 tmps += UTF8SKIP(tmps);
2232 targlen += UNISKIP(~c);
2238 /* Now rewind strings and write them. */
2242 Newz(0, result, targlen + 1, U8);
2243 while (tmps < send) {
2244 UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2245 tmps += UTF8SKIP(tmps);
2246 result = uvchr_to_utf8(result, ~c);
2250 sv_setpvn(TARG, (char*)result, targlen);
2254 Newz(0, result, nchar + 1, U8);
2255 while (tmps < send) {
2256 U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY);
2257 tmps += UTF8SKIP(tmps);
2262 sv_setpvn(TARG, (char*)result, nchar);
2270 register long *tmpl;
2271 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2274 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
2279 for ( ; anum > 0; anum--, tmps++)
2288 /* integer versions of some of the above */
2292 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2295 SETi( left * right );
2302 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2306 DIE(aTHX_ "Illegal division by zero");
2307 value = POPi / value;
2315 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2319 DIE(aTHX_ "Illegal modulus zero");
2320 SETi( left % right );
2327 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2330 SETi( left + right );
2337 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2340 SETi( left - right );
2347 dSP; tryAMAGICbinSET(lt,0);
2350 SETs(boolSV(left < right));
2357 dSP; tryAMAGICbinSET(gt,0);
2360 SETs(boolSV(left > right));
2367 dSP; tryAMAGICbinSET(le,0);
2370 SETs(boolSV(left <= right));
2377 dSP; tryAMAGICbinSET(ge,0);
2380 SETs(boolSV(left >= right));
2387 dSP; tryAMAGICbinSET(eq,0);
2390 SETs(boolSV(left == right));
2397 dSP; tryAMAGICbinSET(ne,0);
2400 SETs(boolSV(left != right));
2407 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2414 else if (left < right)
2425 dSP; dTARGET; tryAMAGICun(neg);
2430 /* High falutin' math. */
2434 dSP; dTARGET; tryAMAGICbin(atan2,0);
2437 SETn(Perl_atan2(left, right));
2444 dSP; dTARGET; tryAMAGICun(sin);
2448 value = Perl_sin(value);
2456 dSP; dTARGET; tryAMAGICun(cos);
2460 value = Perl_cos(value);
2466 /* Support Configure command-line overrides for rand() functions.
2467 After 5.005, perhaps we should replace this by Configure support
2468 for drand48(), random(), or rand(). For 5.005, though, maintain
2469 compatibility by calling rand() but allow the user to override it.
2470 See INSTALL for details. --Andy Dougherty 15 July 1998
2472 /* Now it's after 5.005, and Configure supports drand48() and random(),
2473 in addition to rand(). So the overrides should not be needed any more.
2474 --Jarkko Hietaniemi 27 September 1998
2477 #ifndef HAS_DRAND48_PROTO
2478 extern double drand48 (void);
2491 if (!PL_srand_called) {
2492 (void)seedDrand01((Rand_seed_t)seed());
2493 PL_srand_called = TRUE;
2508 (void)seedDrand01((Rand_seed_t)anum);
2509 PL_srand_called = TRUE;
2518 * This is really just a quick hack which grabs various garbage
2519 * values. It really should be a real hash algorithm which
2520 * spreads the effect of every input bit onto every output bit,
2521 * if someone who knows about such things would bother to write it.
2522 * Might be a good idea to add that function to CORE as well.
2523 * No numbers below come from careful analysis or anything here,
2524 * except they are primes and SEED_C1 > 1E6 to get a full-width
2525 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
2526 * probably be bigger too.
2529 # define SEED_C1 1000003
2530 #define SEED_C4 73819
2532 # define SEED_C1 25747
2533 #define SEED_C4 20639
2537 #define SEED_C5 26107
2539 #ifndef PERL_NO_DEV_RANDOM
2544 # include <starlet.h>
2545 /* when[] = (low 32 bits, high 32 bits) of time since epoch
2546 * in 100-ns units, typically incremented ever 10 ms. */
2547 unsigned int when[2];
2549 # ifdef HAS_GETTIMEOFDAY
2550 struct timeval when;
2556 /* This test is an escape hatch, this symbol isn't set by Configure. */
2557 #ifndef PERL_NO_DEV_RANDOM
2558 #ifndef PERL_RANDOM_DEVICE
2559 /* /dev/random isn't used by default because reads from it will block
2560 * if there isn't enough entropy available. You can compile with
2561 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
2562 * is enough real entropy to fill the seed. */
2563 # define PERL_RANDOM_DEVICE "/dev/urandom"
2565 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
2567 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
2576 _ckvmssts(sys$gettim(when));
2577 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
2579 # ifdef HAS_GETTIMEOFDAY
2580 gettimeofday(&when,(struct timezone *) 0);
2581 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
2584 u = (U32)SEED_C1 * when;
2587 u += SEED_C3 * (U32)PerlProc_getpid();
2588 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
2589 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
2590 u += SEED_C5 * (U32)PTR2UV(&when);
2597 dSP; dTARGET; tryAMAGICun(exp);
2601 value = Perl_exp(value);
2609 dSP; dTARGET; tryAMAGICun(log);
2614 SET_NUMERIC_STANDARD();
2615 DIE(aTHX_ "Can't take log of %g", value);
2617 value = Perl_log(value);
2625 dSP; dTARGET; tryAMAGICun(sqrt);
2630 SET_NUMERIC_STANDARD();
2631 DIE(aTHX_ "Can't take sqrt of %g", value);
2633 value = Perl_sqrt(value);
2641 dSP; dTARGET; tryAMAGICun(int);
2644 IV iv = TOPi; /* attempt to convert to IV if possible. */
2645 /* XXX it's arguable that compiler casting to IV might be subtly
2646 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2647 else preferring IV has introduced a subtle behaviour change bug. OTOH
2648 relying on floating point to be accurate is a bug. */
2659 if (value < (NV)UV_MAX + 0.5) {
2662 #if defined(HAS_MODFL) || defined(LONG_DOUBLE_EQUALS_DOUBLE)
2663 # ifdef HAS_MODFL_POW32_BUG
2664 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
2666 NV offset = Perl_modf(value, &value);
2667 (void)Perl_modf(offset, &offset);
2671 (void)Perl_modf(value, &value);
2674 double tmp = (double)value;
2675 (void)Perl_modf(tmp, &tmp);
2682 if (value > (NV)IV_MIN - 0.5) {
2685 #if defined(HAS_MODFL) || defined(LONG_DOUBLE_EQUALS_DOUBLE)
2686 # ifdef HAS_MODFL_POW32_BUG
2687 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
2689 NV offset = Perl_modf(-value, &value);
2690 (void)Perl_modf(offset, &offset);
2694 (void)Perl_modf(-value, &value);
2698 double tmp = (double)value;
2699 (void)Perl_modf(-tmp, &tmp);
2712 dSP; dTARGET; tryAMAGICun(abs);
2714 /* This will cache the NV value if string isn't actually integer */
2718 /* IVX is precise */
2720 SETu(TOPu); /* force it to be numeric only */
2728 /* 2s complement assumption. Also, not really needed as
2729 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2749 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2754 tmps = (SvPVx(POPs, len));
2755 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2756 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2769 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2774 tmps = (SvPVx(POPs, len));
2775 while (*tmps && len && isSPACE(*tmps))
2780 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2781 else if (*tmps == 'b')
2782 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2784 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2786 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2803 SETi(sv_len_utf8(sv));
2819 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2821 I32 arybase = PL_curcop->cop_arybase;
2825 int num_args = PL_op->op_private & 7;
2826 bool repl_need_utf8_upgrade = FALSE;
2827 bool repl_is_utf8 = FALSE;
2829 SvTAINTED_off(TARG); /* decontaminate */
2830 SvUTF8_off(TARG); /* decontaminate */
2834 repl = SvPV(repl_sv, repl_len);
2835 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
2845 sv_utf8_upgrade(sv);
2847 else if (DO_UTF8(sv))
2848 repl_need_utf8_upgrade = TRUE;
2850 tmps = SvPV(sv, curlen);
2852 utf8_curlen = sv_len_utf8(sv);
2853 if (utf8_curlen == curlen)
2856 curlen = utf8_curlen;
2861 if (pos >= arybase) {
2879 else if (len >= 0) {
2881 if (rem > (I32)curlen)
2896 Perl_croak(aTHX_ "substr outside of string");
2897 if (ckWARN(WARN_SUBSTR))
2898 Perl_warner(aTHX_ WARN_SUBSTR, "substr outside of string");
2905 sv_pos_u2b(sv, &pos, &rem);
2907 sv_setpvn(TARG, tmps, rem);
2908 #ifdef USE_LOCALE_COLLATE
2909 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
2914 SV* repl_sv_copy = NULL;
2916 if (repl_need_utf8_upgrade) {
2917 repl_sv_copy = newSVsv(repl_sv);
2918 sv_utf8_upgrade(repl_sv_copy);
2919 repl = SvPV(repl_sv_copy, repl_len);
2920 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
2922 sv_insert(sv, pos, rem, repl, repl_len);
2926 SvREFCNT_dec(repl_sv_copy);
2928 else if (lvalue) { /* it's an lvalue! */
2929 if (!SvGMAGICAL(sv)) {
2933 if (ckWARN(WARN_SUBSTR))
2934 Perl_warner(aTHX_ WARN_SUBSTR,
2935 "Attempt to use reference as lvalue in substr");
2937 if (SvOK(sv)) /* is it defined ? */
2938 (void)SvPOK_only_UTF8(sv);
2940 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2943 if (SvTYPE(TARG) < SVt_PVLV) {
2944 sv_upgrade(TARG, SVt_PVLV);
2945 sv_magic(TARG, Nullsv, PERL_MAGIC_substr, Nullch, 0);
2949 if (LvTARG(TARG) != sv) {
2951 SvREFCNT_dec(LvTARG(TARG));
2952 LvTARG(TARG) = SvREFCNT_inc(sv);
2954 LvTARGOFF(TARG) = upos;
2955 LvTARGLEN(TARG) = urem;
2959 PUSHs(TARG); /* avoid SvSETMAGIC here */
2966 register IV size = POPi;
2967 register IV offset = POPi;
2968 register SV *src = POPs;
2969 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2971 SvTAINTED_off(TARG); /* decontaminate */
2972 if (lvalue) { /* it's an lvalue! */
2973 if (SvTYPE(TARG) < SVt_PVLV) {
2974 sv_upgrade(TARG, SVt_PVLV);
2975 sv_magic(TARG, Nullsv, PERL_MAGIC_vec, Nullch, 0);
2978 if (LvTARG(TARG) != src) {
2980 SvREFCNT_dec(LvTARG(TARG));
2981 LvTARG(TARG) = SvREFCNT_inc(src);
2983 LvTARGOFF(TARG) = offset;
2984 LvTARGLEN(TARG) = size;
2987 sv_setuv(TARG, do_vecget(src, offset, size));
3002 I32 arybase = PL_curcop->cop_arybase;
3007 offset = POPi - arybase;
3010 tmps = SvPV(big, biglen);
3011 if (offset > 0 && DO_UTF8(big))
3012 sv_pos_u2b(big, &offset, 0);
3015 else if (offset > biglen)
3017 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
3018 (unsigned char*)tmps + biglen, little, 0)))
3021 retval = tmps2 - tmps;
3022 if (retval > 0 && DO_UTF8(big))
3023 sv_pos_b2u(big, &retval);
3024 PUSHi(retval + arybase);
3039 I32 arybase = PL_curcop->cop_arybase;
3045 tmps2 = SvPV(little, llen);
3046 tmps = SvPV(big, blen);
3050 if (offset > 0 && DO_UTF8(big))
3051 sv_pos_u2b(big, &offset, 0);
3052 offset = offset - arybase + llen;
3056 else if (offset > blen)
3058 if (!(tmps2 = rninstr(tmps, tmps + offset,
3059 tmps2, tmps2 + llen)))
3062 retval = tmps2 - tmps;
3063 if (retval > 0 && DO_UTF8(big))
3064 sv_pos_b2u(big, &retval);
3065 PUSHi(retval + arybase);
3071 dSP; dMARK; dORIGMARK; dTARGET;
3072 do_sprintf(TARG, SP-MARK, MARK+1);
3073 TAINT_IF(SvTAINTED(TARG));
3074 if (DO_UTF8(*(MARK+1)))
3086 U8 *s = (U8*)SvPVx(argsv, len);
3089 if (PL_encoding && !DO_UTF8(argsv)) {
3090 tmpsv = sv_2mortal(newSVsv(argsv));
3091 s = (U8*)Perl_sv_recode_to_utf8(aTHX_ tmpsv, PL_encoding);
3095 XPUSHu(DO_UTF8(argsv) ? utf8_to_uvchr(s, 0) : (*s & 0xff));
3106 (void)SvUPGRADE(TARG,SVt_PV);
3108 if (value > 255 && !IN_BYTES) {
3109 SvGROW(TARG, UNISKIP(value)+1);
3110 tmps = (char*)uvchr_to_utf8((U8*)SvPVX(TARG), value);
3111 SvCUR_set(TARG, tmps - SvPVX(TARG));
3113 (void)SvPOK_only(TARG);
3124 (void)SvPOK_only(TARG);
3126 Perl_sv_recode_to_utf8(aTHX_ TARG, PL_encoding);
3133 dSP; dTARGET; dPOPTOPssrl;
3137 char *tmps = SvPV(left, len);
3139 if (DO_UTF8(left)) {
3140 /* If Unicode take the crypt() of the low 8 bits
3141 * of the characters of the string. */
3143 char *send = tmps + len;
3145 Newz(688, t, len, char);
3147 t[i++] = utf8_to_uvchr((U8*)s, 0) & 0xFF;
3153 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
3155 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
3160 "The crypt() function is unimplemented due to excessive paranoia.");
3174 U8 tmpbuf[UTF8_MAXLEN*2+1];
3178 s = (U8*)SvPV(sv, slen);
3179 utf8_to_uvchr(s, &ulen);
3181 toTITLE_utf8(s, tmpbuf, &tculen);
3182 utf8_to_uvchr(tmpbuf, 0);
3184 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3186 sv_setpvn(TARG, (char*)tmpbuf, tculen);
3187 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3192 s = (U8*)SvPV_force(sv, slen);
3193 Copy(tmpbuf, s, tculen, U8);
3197 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3199 SvUTF8_off(TARG); /* decontaminate */
3204 s = (U8*)SvPV_force(sv, slen);
3206 if (IN_LOCALE_RUNTIME) {
3209 *s = toUPPER_LC(*s);
3227 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && UTF8_IS_START(*s)) {
3229 U8 tmpbuf[UTF8_MAXLEN*2+1];
3233 toLOWER_utf8(s, tmpbuf, &ulen);
3234 uv = utf8_to_uvchr(tmpbuf, 0);
3236 tend = uvchr_to_utf8(tmpbuf, uv);
3238 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
3240 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
3241 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3246 s = (U8*)SvPV_force(sv, slen);
3247 Copy(tmpbuf, s, ulen, U8);
3251 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3253 SvUTF8_off(TARG); /* decontaminate */
3258 s = (U8*)SvPV_force(sv, slen);
3260 if (IN_LOCALE_RUNTIME) {
3263 *s = toLOWER_LC(*s);
3286 U8 tmpbuf[UTF8_MAXLEN*2+1];
3288 s = (U8*)SvPV(sv,len);
3290 SvUTF8_off(TARG); /* decontaminate */
3291 sv_setpvn(TARG, "", 0);
3295 (void)SvUPGRADE(TARG, SVt_PV);
3296 SvGROW(TARG, (len * 2) + 1);
3297 (void)SvPOK_only(TARG);
3298 d = (U8*)SvPVX(TARG);
3301 toUPPER_utf8(s, tmpbuf, &ulen);
3302 Copy(tmpbuf, d, ulen, U8);
3308 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3313 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3315 SvUTF8_off(TARG); /* decontaminate */
3320 s = (U8*)SvPV_force(sv, len);
3322 register U8 *send = s + len;
3324 if (IN_LOCALE_RUNTIME) {
3327 for (; s < send; s++)
3328 *s = toUPPER_LC(*s);
3331 for (; s < send; s++)
3353 U8 tmpbuf[UTF8_MAXLEN*2+1];
3355 s = (U8*)SvPV(sv,len);
3357 SvUTF8_off(TARG); /* decontaminate */
3358 sv_setpvn(TARG, "", 0);
3362 (void)SvUPGRADE(TARG, SVt_PV);
3363 SvGROW(TARG, (len * 2) + 1);
3364 (void)SvPOK_only(TARG);
3365 d = (U8*)SvPVX(TARG);
3368 UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
3369 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode */
3370 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3372 * Now if the sigma is NOT followed by
3373 * /$ignorable_sequence$cased_letter/;
3374 * and it IS preceded by
3375 * /$cased_letter$ignorable_sequence/;
3376 * where $ignorable_sequence is
3377 * [\x{2010}\x{AD}\p{Mn}]*
3378 * and $cased_letter is
3379 * [\p{Ll}\p{Lo}\p{Lt}]
3380 * then it should be mapped to 0x03C2,
3381 * (GREEK SMALL LETTER FINAL SIGMA),
3382 * instead of staying 0x03A3.
3383 * See lib/unicore/SpecCase.txt.
3386 Copy(tmpbuf, d, ulen, U8);
3392 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3397 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3399 SvUTF8_off(TARG); /* decontaminate */
3405 s = (U8*)SvPV_force(sv, len);
3407 register U8 *send = s + len;
3409 if (IN_LOCALE_RUNTIME) {
3412 for (; s < send; s++)
3413 *s = toLOWER_LC(*s);
3416 for (; s < send; s++)
3431 register char *s = SvPV(sv,len);
3434 SvUTF8_off(TARG); /* decontaminate */
3436 (void)SvUPGRADE(TARG, SVt_PV);
3437 SvGROW(TARG, (len * 2) + 1);
3441 if (UTF8_IS_CONTINUED(*s)) {
3442 STRLEN ulen = UTF8SKIP(s);
3466 SvCUR_set(TARG, d - SvPVX(TARG));
3467 (void)SvPOK_only_UTF8(TARG);
3470 sv_setpvn(TARG, s, len);
3472 if (SvSMAGICAL(TARG))
3481 dSP; dMARK; dORIGMARK;
3483 register AV* av = (AV*)POPs;
3484 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3485 I32 arybase = PL_curcop->cop_arybase;
3488 if (SvTYPE(av) == SVt_PVAV) {
3489 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3491 for (svp = MARK + 1; svp <= SP; svp++) {
3496 if (max > AvMAX(av))
3499 while (++MARK <= SP) {
3500 elem = SvIVx(*MARK);
3504 svp = av_fetch(av, elem, lval);
3506 if (!svp || *svp == &PL_sv_undef)
3507 DIE(aTHX_ PL_no_aelem, elem);
3508 if (PL_op->op_private & OPpLVAL_INTRO)
3509 save_aelem(av, elem, svp);
3511 *MARK = svp ? *svp : &PL_sv_undef;
3514 if (GIMME != G_ARRAY) {
3522 /* Associative arrays. */
3527 HV *hash = (HV*)POPs;
3529 I32 gimme = GIMME_V;
3530 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
3533 /* might clobber stack_sp */
3534 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
3539 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
3540 if (gimme == G_ARRAY) {
3543 /* might clobber stack_sp */
3545 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
3550 else if (gimme == G_SCALAR)
3569 I32 gimme = GIMME_V;
3570 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3574 if (PL_op->op_private & OPpSLICE) {
3578 hvtype = SvTYPE(hv);
3579 if (hvtype == SVt_PVHV) { /* hash element */
3580 while (++MARK <= SP) {
3581 sv = hv_delete_ent(hv, *MARK, discard, 0);
3582 *MARK = sv ? sv : &PL_sv_undef;
3585 else if (hvtype == SVt_PVAV) {
3586 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3587 while (++MARK <= SP) {
3588 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3589 *MARK = sv ? sv : &PL_sv_undef;
3592 else { /* pseudo-hash element */
3593 while (++MARK <= SP) {
3594 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
3595 *MARK = sv ? sv : &PL_sv_undef;
3600 DIE(aTHX_ "Not a HASH reference");
3603 else if (gimme == G_SCALAR) {
3612 if (SvTYPE(hv) == SVt_PVHV)
3613 sv = hv_delete_ent(hv, keysv, discard, 0);
3614 else if (SvTYPE(hv) == SVt_PVAV) {
3615 if (PL_op->op_flags & OPf_SPECIAL)
3616 sv = av_delete((AV*)hv, SvIV(keysv), discard);
3618 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
3621 DIE(aTHX_ "Not a HASH reference");
3636 if (PL_op->op_private & OPpEXISTS_SUB) {
3640 cv = sv_2cv(sv, &hv, &gv, FALSE);
3643 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
3649 if (SvTYPE(hv) == SVt_PVHV) {
3650 if (hv_exists_ent(hv, tmpsv, 0))
3653 else if (SvTYPE(hv) == SVt_PVAV) {
3654 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3655 if (av_exists((AV*)hv, SvIV(tmpsv)))
3658 else if (avhv_exists_ent((AV*)hv, tmpsv, 0)) /* pseudo-hash element */
3662 DIE(aTHX_ "Not a HASH reference");
3669 dSP; dMARK; dORIGMARK;
3670 register HV *hv = (HV*)POPs;
3671 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3672 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
3674 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
3675 DIE(aTHX_ "Can't localize pseudo-hash element");
3677 if (realhv || SvTYPE(hv) == SVt_PVAV) {
3678 while (++MARK <= SP) {
3681 I32 preeminent = SvRMAGICAL(hv) ? 1 :
3682 realhv ? hv_exists_ent(hv, keysv, 0)
3683 : avhv_exists_ent((AV*)hv, keysv, 0);
3685 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
3686 svp = he ? &HeVAL(he) : 0;
3689 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
3692 if (!svp || *svp == &PL_sv_undef) {
3694 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
3696 if (PL_op->op_private & OPpLVAL_INTRO) {
3698 save_helem(hv, keysv, svp);
3701 char *key = SvPV(keysv, keylen);
3702 SAVEDELETE(hv, savepvn(key,keylen), keylen);
3706 *MARK = svp ? *svp : &PL_sv_undef;
3709 if (GIMME != G_ARRAY) {
3717 /* List operators. */
3722 if (GIMME != G_ARRAY) {
3724 *MARK = *SP; /* unwanted list, return last item */
3726 *MARK = &PL_sv_undef;
3735 SV **lastrelem = PL_stack_sp;
3736 SV **lastlelem = PL_stack_base + POPMARK;
3737 SV **firstlelem = PL_stack_base + POPMARK + 1;
3738 register SV **firstrelem = lastlelem + 1;
3739 I32 arybase = PL_curcop->cop_arybase;
3740 I32 lval = PL_op->op_flags & OPf_MOD;
3741 I32 is_something_there = lval;
3743 register I32 max = lastrelem - lastlelem;
3744 register SV **lelem;
3747 if (GIMME != G_ARRAY) {
3748 ix = SvIVx(*lastlelem);
3753 if (ix < 0 || ix >= max)
3754 *firstlelem = &PL_sv_undef;
3756 *firstlelem = firstrelem[ix];
3762 SP = firstlelem - 1;
3766 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
3772 if (ix < 0 || ix >= max)
3773 *lelem = &PL_sv_undef;
3775 is_something_there = TRUE;
3776 if (!(*lelem = firstrelem[ix]))
3777 *lelem = &PL_sv_undef;
3780 if (is_something_there)
3783 SP = firstlelem - 1;
3789 dSP; dMARK; dORIGMARK;
3790 I32 items = SP - MARK;
3791 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
3792 SP = ORIGMARK; /* av_make() might realloc stack_sp */
3799 dSP; dMARK; dORIGMARK;
3800 HV* hv = (HV*)sv_2mortal((SV*)newHV());
3804 SV *val = NEWSV(46, 0);
3806 sv_setsv(val, *++MARK);
3807 else if (ckWARN(WARN_MISC))
3808 Perl_warner(aTHX_ WARN_MISC, "Odd number of elements in hash assignment");
3809 (void)hv_store_ent(hv,key,val,0);
3818 dSP; dMARK; dORIGMARK;
3819 register AV *ary = (AV*)*++MARK;
3823 register I32 offset;
3824 register I32 length;
3831 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
3832 *MARK-- = SvTIED_obj((SV*)ary, mg);
3836 call_method("SPLICE",GIMME_V);
3845 offset = i = SvIVx(*MARK);
3847 offset += AvFILLp(ary) + 1;
3849 offset -= PL_curcop->cop_arybase;
3851 DIE(aTHX_ PL_no_aelem, i);
3853 length = SvIVx(*MARK++);
3855 length += AvFILLp(ary) - offset + 1;
3861 length = AvMAX(ary) + 1; /* close enough to infinity */
3865 length = AvMAX(ary) + 1;
3867 if (offset > AvFILLp(ary) + 1)
3868 offset = AvFILLp(ary) + 1;
3869 after = AvFILLp(ary) + 1 - (offset + length);
3870 if (after < 0) { /* not that much array */
3871 length += after; /* offset+length now in array */
3877 /* At this point, MARK .. SP-1 is our new LIST */
3880 diff = newlen - length;
3881 if (newlen && !AvREAL(ary) && AvREIFY(ary))
3884 if (diff < 0) { /* shrinking the area */
3886 New(451, tmparyval, newlen, SV*); /* so remember insertion */
3887 Copy(MARK, tmparyval, newlen, SV*);
3890 MARK = ORIGMARK + 1;
3891 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3892 MEXTEND(MARK, length);
3893 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
3895 EXTEND_MORTAL(length);
3896 for (i = length, dst = MARK; i; i--) {
3897 sv_2mortal(*dst); /* free them eventualy */
3904 *MARK = AvARRAY(ary)[offset+length-1];
3907 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
3908 SvREFCNT_dec(*dst++); /* free them now */
3911 AvFILLp(ary) += diff;
3913 /* pull up or down? */
3915 if (offset < after) { /* easier to pull up */
3916 if (offset) { /* esp. if nothing to pull */
3917 src = &AvARRAY(ary)[offset-1];
3918 dst = src - diff; /* diff is negative */
3919 for (i = offset; i > 0; i--) /* can't trust Copy */
3923 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
3927 if (after) { /* anything to pull down? */
3928 src = AvARRAY(ary) + offset + length;
3929 dst = src + diff; /* diff is negative */
3930 Move(src, dst, after, SV*);
3932 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
3933 /* avoid later double free */
3937 dst[--i] = &PL_sv_undef;
3940 for (src = tmparyval, dst = AvARRAY(ary) + offset;
3942 *dst = NEWSV(46, 0);
3943 sv_setsv(*dst++, *src++);
3945 Safefree(tmparyval);
3948 else { /* no, expanding (or same) */
3950 New(452, tmparyval, length, SV*); /* so remember deletion */
3951 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
3954 if (diff > 0) { /* expanding */
3956 /* push up or down? */
3958 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
3962 Move(src, dst, offset, SV*);
3964 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
3966 AvFILLp(ary) += diff;
3969 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
3970 av_extend(ary, AvFILLp(ary) + diff);
3971 AvFILLp(ary) += diff;
3974 dst = AvARRAY(ary) + AvFILLp(ary);
3976 for (i = after; i; i--) {
3983 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
3984 *dst = NEWSV(46, 0);
3985 sv_setsv(*dst++, *src++);
3987 MARK = ORIGMARK + 1;
3988 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3990 Copy(tmparyval, MARK, length, SV*);
3992 EXTEND_MORTAL(length);
3993 for (i = length, dst = MARK; i; i--) {
3994 sv_2mortal(*dst); /* free them eventualy */
3998 Safefree(tmparyval);
4002 else if (length--) {
4003 *MARK = tmparyval[length];
4006 while (length-- > 0)
4007 SvREFCNT_dec(tmparyval[length]);
4009 Safefree(tmparyval);
4012 *MARK = &PL_sv_undef;
4020 dSP; dMARK; dORIGMARK; dTARGET;
4021 register AV *ary = (AV*)*++MARK;
4022 register SV *sv = &PL_sv_undef;
4025 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4026 *MARK-- = SvTIED_obj((SV*)ary, mg);
4030 call_method("PUSH",G_SCALAR|G_DISCARD);
4035 /* Why no pre-extend of ary here ? */
4036 for (++MARK; MARK <= SP; MARK++) {
4039 sv_setsv(sv, *MARK);
4044 PUSHi( AvFILL(ary) + 1 );
4052 SV *sv = av_pop(av);
4054 (void)sv_2mortal(sv);
4063 SV *sv = av_shift(av);
4068 (void)sv_2mortal(sv);
4075 dSP; dMARK; dORIGMARK; dTARGET;
4076 register AV *ary = (AV*)*++MARK;
4081 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4082 *MARK-- = SvTIED_obj((SV*)ary, mg);
4086 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4091 av_unshift(ary, SP - MARK);
4094 sv_setsv(sv, *++MARK);
4095 (void)av_store(ary, i++, sv);
4099 PUSHi( AvFILL(ary) + 1 );
4109 if (GIMME == G_ARRAY) {
4116 /* safe as long as stack cannot get extended in the above */
4121 register char *down;
4126 SvUTF8_off(TARG); /* decontaminate */
4128 do_join(TARG, &PL_sv_no, MARK, SP);
4130 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
4131 up = SvPV_force(TARG, len);
4133 if (DO_UTF8(TARG)) { /* first reverse each character */
4134 U8* s = (U8*)SvPVX(TARG);
4135 U8* send = (U8*)(s + len);
4137 if (UTF8_IS_INVARIANT(*s)) {
4142 if (!utf8_to_uvchr(s, 0))
4146 down = (char*)(s - 1);
4147 /* reverse this character */
4157 down = SvPVX(TARG) + len - 1;
4163 (void)SvPOK_only_UTF8(TARG);
4175 register IV limit = POPi; /* note, negative is forever */
4178 register char *s = SvPV(sv, len);
4179 bool do_utf8 = DO_UTF8(sv);
4180 char *strend = s + len;
4182 register REGEXP *rx;
4186 STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (strend - s);
4187 I32 maxiters = slen + 10;
4190 I32 origlimit = limit;
4193 AV *oldstack = PL_curstack;
4194 I32 gimme = GIMME_V;
4195 I32 oldsave = PL_savestack_ix;
4196 I32 make_mortal = 1;
4197 MAGIC *mg = (MAGIC *) NULL;
4200 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4205 DIE(aTHX_ "panic: pp_split");
4208 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4209 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4211 PL_reg_match_utf8 = do_utf8;
4213 if (pm->op_pmreplroot) {
4215 ary = GvAVn((GV*)PL_curpad[INT2PTR(PADOFFSET, pm->op_pmreplroot)]);
4217 ary = GvAVn((GV*)pm->op_pmreplroot);
4220 else if (gimme != G_ARRAY)
4221 #ifdef USE_5005THREADS
4222 ary = (AV*)PL_curpad[0];
4224 ary = GvAVn(PL_defgv);
4225 #endif /* USE_5005THREADS */
4228 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4234 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4236 XPUSHs(SvTIED_obj((SV*)ary, mg));
4242 for (i = AvFILLp(ary); i >= 0; i--)
4243 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4245 /* temporarily switch stacks */
4246 SWITCHSTACK(PL_curstack, ary);
4250 base = SP - PL_stack_base;
4252 if (pm->op_pmflags & PMf_SKIPWHITE) {
4253 if (pm->op_pmflags & PMf_LOCALE) {
4254 while (isSPACE_LC(*s))
4262 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4263 SAVEINT(PL_multiline);
4264 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4268 limit = maxiters + 2;
4269 if (pm->op_pmflags & PMf_WHITE) {
4272 while (m < strend &&
4273 !((pm->op_pmflags & PMf_LOCALE)
4274 ? isSPACE_LC(*m) : isSPACE(*m)))
4279 dstr = NEWSV(30, m-s);
4280 sv_setpvn(dstr, s, m-s);
4284 (void)SvUTF8_on(dstr);
4288 while (s < strend &&
4289 ((pm->op_pmflags & PMf_LOCALE)
4290 ? isSPACE_LC(*s) : isSPACE(*s)))
4294 else if (strEQ("^", rx->precomp)) {
4297 for (m = s; m < strend && *m != '\n'; m++) ;
4301 dstr = NEWSV(30, m-s);
4302 sv_setpvn(dstr, s, m-s);
4306 (void)SvUTF8_on(dstr);
4311 else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) &&
4312 (rx->reganch & RE_USE_INTUIT) && !rx->nparens
4313 && (rx->reganch & ROPT_CHECK_ALL)
4314 && !(rx->reganch & ROPT_ANCH)) {
4315 int tail = (rx->reganch & RE_INTUIT_TAIL);
4316 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
4319 if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) {
4321 char c = *SvPV(csv, n_a);
4324 for (m = s; m < strend && *m != c; m++) ;
4327 dstr = NEWSV(30, m-s);
4328 sv_setpvn(dstr, s, m-s);
4332 (void)SvUTF8_on(dstr);
4334 /* The rx->minlen is in characters but we want to step
4335 * s ahead by bytes. */
4337 s = (char*)utf8_hop((U8*)m, len);
4339 s = m + len; /* Fake \n at the end */
4344 while (s < strend && --limit &&
4345 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4346 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
4349 dstr = NEWSV(31, m-s);
4350 sv_setpvn(dstr, s, m-s);
4354 (void)SvUTF8_on(dstr);
4356 /* The rx->minlen is in characters but we want to step
4357 * s ahead by bytes. */
4359 s = (char*)utf8_hop((U8*)m, len);
4361 s = m + len; /* Fake \n at the end */
4366 maxiters += slen * rx->nparens;
4367 while (s < strend && --limit
4368 /* && (!rx->check_substr
4369 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
4371 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
4372 1 /* minend */, sv, NULL, 0))
4374 TAINT_IF(RX_MATCH_TAINTED(rx));
4375 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4380 strend = s + (strend - m);
4382 m = rx->startp[0] + orig;
4383 dstr = NEWSV(32, m-s);
4384 sv_setpvn(dstr, s, m-s);
4388 (void)SvUTF8_on(dstr);
4391 for (i = 1; i <= rx->nparens; i++) {
4392 s = rx->startp[i] + orig;
4393 m = rx->endp[i] + orig;
4395 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4396 parens that didn't match -- they should be set to
4397 undef, not the empty string */
4398 if (m >= orig && s >= orig) {
4399 dstr = NEWSV(33, m-s);
4400 sv_setpvn(dstr, s, m-s);
4403 dstr = &PL_sv_undef; /* undef, not "" */
4407 (void)SvUTF8_on(dstr);
4411 s = rx->endp[0] + orig;
4415 LEAVE_SCOPE(oldsave);
4416 iters = (SP - PL_stack_base) - base;
4417 if (iters > maxiters)
4418 DIE(aTHX_ "Split loop");
4420 /* keep field after final delim? */
4421 if (s < strend || (iters && origlimit)) {
4422 STRLEN l = strend - s;
4423 dstr = NEWSV(34, l);
4424 sv_setpvn(dstr, s, l);
4428 (void)SvUTF8_on(dstr);
4432 else if (!origlimit) {
4433 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4439 SWITCHSTACK(ary, oldstack);
4440 if (SvSMAGICAL(ary)) {
4445 if (gimme == G_ARRAY) {
4447 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4455 call_method("PUSH",G_SCALAR|G_DISCARD);
4458 if (gimme == G_ARRAY) {
4459 /* EXTEND should not be needed - we just popped them */
4461 for (i=0; i < iters; i++) {
4462 SV **svp = av_fetch(ary, i, FALSE);
4463 PUSHs((svp) ? *svp : &PL_sv_undef);
4470 if (gimme == G_ARRAY)
4473 if (iters || !pm->op_pmreplroot) {
4481 #ifdef USE_5005THREADS
4483 Perl_unlock_condpair(pTHX_ void *svv)
4485 MAGIC *mg = mg_find((SV*)svv, PERL_MAGIC_mutex);
4488 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
4489 MUTEX_LOCK(MgMUTEXP(mg));
4490 if (MgOWNER(mg) != thr)
4491 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
4493 COND_SIGNAL(MgOWNERCONDP(mg));
4494 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": unlock 0x%"UVxf"\n",
4495 PTR2UV(thr), PTR2UV(svv)));
4496 MUTEX_UNLOCK(MgMUTEXP(mg));
4498 #endif /* USE_5005THREADS */
4505 #ifdef USE_5005THREADS
4507 #endif /* USE_5005THREADS */
4509 shared_sv *ssv = Perl_sharedsv_find(aTHX_ sv);
4511 Perl_sharedsv_lock(aTHX_ ssv);
4512 #endif /* USE_ITHREADS */
4513 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4514 || SvTYPE(retsv) == SVt_PVCV) {
4515 retsv = refto(retsv);
4523 #ifdef USE_5005THREADS
4526 if (PL_op->op_private & OPpLVAL_INTRO)
4527 PUSHs(*save_threadsv(PL_op->op_targ));
4529 PUSHs(THREADSV(PL_op->op_targ));
4532 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
4533 #endif /* USE_5005THREADS */