3 * Copyright (c) 1991-2002, 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
22 /* variations on pp_null */
24 /* XXX I can't imagine anyone who doesn't have this actually _needs_
25 it, since pid_t is an integral type.
28 #ifdef NEED_GETPID_PROTO
29 extern Pid_t getpid (void);
35 if (GIMME_V == G_SCALAR)
50 if (PL_op->op_private & OPpLVAL_INTRO)
51 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
53 if (PL_op->op_flags & OPf_REF) {
57 if (GIMME == G_SCALAR)
58 Perl_croak(aTHX_ "Can't return array to lvalue scalar context");
62 if (GIMME == G_ARRAY) {
63 I32 maxarg = AvFILL((AV*)TARG) + 1;
65 if (SvMAGICAL(TARG)) {
67 for (i=0; i < (U32)maxarg; i++) {
68 SV **svp = av_fetch((AV*)TARG, i, FALSE);
69 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
73 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
78 SV* sv = sv_newmortal();
79 I32 maxarg = AvFILL((AV*)TARG) + 1;
92 if (PL_op->op_private & OPpLVAL_INTRO)
93 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
94 if (PL_op->op_flags & OPf_REF)
97 if (GIMME == G_SCALAR)
98 Perl_croak(aTHX_ "Can't return hash to lvalue scalar context");
102 if (gimme == G_ARRAY) {
105 else if (gimme == G_SCALAR) {
106 SV* sv = sv_newmortal();
107 if (HvFILL((HV*)TARG))
108 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
109 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
119 DIE(aTHX_ "NOT IMPL LINE %d",__LINE__);
130 tryAMAGICunDEREF(to_gv);
133 if (SvTYPE(sv) == SVt_PVIO) {
134 GV *gv = (GV*) sv_newmortal();
135 gv_init(gv, 0, "", 0, 0);
136 GvIOp(gv) = (IO *)sv;
137 (void)SvREFCNT_inc(sv);
140 else if (SvTYPE(sv) != SVt_PVGV)
141 DIE(aTHX_ "Not a GLOB reference");
144 if (SvTYPE(sv) != SVt_PVGV) {
148 if (SvGMAGICAL(sv)) {
153 if (!SvOK(sv) && sv != &PL_sv_undef) {
154 /* If this is a 'my' scalar and flag is set then vivify
157 if (PL_op->op_private & OPpDEREF) {
160 if (cUNOP->op_targ) {
162 SV *namesv = PL_curpad[cUNOP->op_targ];
163 name = SvPV(namesv, len);
164 gv = (GV*)NEWSV(0,0);
165 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
168 name = CopSTASHPV(PL_curcop);
171 if (SvTYPE(sv) < SVt_RV)
172 sv_upgrade(sv, SVt_RV);
178 if (PL_op->op_flags & OPf_REF ||
179 PL_op->op_private & HINT_STRICT_REFS)
180 DIE(aTHX_ PL_no_usym, "a symbol");
181 if (ckWARN(WARN_UNINITIALIZED))
186 if ((PL_op->op_flags & OPf_SPECIAL) &&
187 !(PL_op->op_flags & OPf_MOD))
189 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
191 && (!is_gv_magical(sym,len,0)
192 || !(sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV))))
198 if (PL_op->op_private & HINT_STRICT_REFS)
199 DIE(aTHX_ PL_no_symref, sym, "a symbol");
200 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
204 if (PL_op->op_private & OPpLVAL_INTRO)
205 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
216 tryAMAGICunDEREF(to_sv);
219 switch (SvTYPE(sv)) {
223 DIE(aTHX_ "Not a SCALAR reference");
231 if (SvTYPE(gv) != SVt_PVGV) {
232 if (SvGMAGICAL(sv)) {
238 if (PL_op->op_flags & OPf_REF ||
239 PL_op->op_private & HINT_STRICT_REFS)
240 DIE(aTHX_ PL_no_usym, "a SCALAR");
241 if (ckWARN(WARN_UNINITIALIZED))
246 if ((PL_op->op_flags & OPf_SPECIAL) &&
247 !(PL_op->op_flags & OPf_MOD))
249 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
251 && (!is_gv_magical(sym,len,0)
252 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV))))
258 if (PL_op->op_private & HINT_STRICT_REFS)
259 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
260 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
265 if (PL_op->op_flags & OPf_MOD) {
266 if (PL_op->op_private & OPpLVAL_INTRO)
267 sv = save_scalar((GV*)TOPs);
268 else if (PL_op->op_private & OPpDEREF)
269 vivify_ref(sv, PL_op->op_private & OPpDEREF);
279 SV *sv = AvARYLEN(av);
281 AvARYLEN(av) = sv = NEWSV(0,0);
282 sv_upgrade(sv, SVt_IV);
283 sv_magic(sv, (SV*)av, PERL_MAGIC_arylen, Nullch, 0);
291 dSP; dTARGET; dPOPss;
293 if (PL_op->op_flags & OPf_MOD || LVRET) {
294 if (SvTYPE(TARG) < SVt_PVLV) {
295 sv_upgrade(TARG, SVt_PVLV);
296 sv_magic(TARG, Nullsv, PERL_MAGIC_pos, Nullch, 0);
300 if (LvTARG(TARG) != sv) {
302 SvREFCNT_dec(LvTARG(TARG));
303 LvTARG(TARG) = SvREFCNT_inc(sv);
305 PUSHs(TARG); /* no SvSETMAGIC */
311 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
312 mg = mg_find(sv, PERL_MAGIC_regex_global);
313 if (mg && mg->mg_len >= 0) {
317 PUSHi(i + PL_curcop->cop_arybase);
331 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
332 /* (But not in defined().) */
333 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
336 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
337 if ((PL_op->op_private & OPpLVAL_INTRO)) {
338 if (gv && GvCV(gv) == cv && (gv = gv_autoload4(GvSTASH(gv), GvNAME(gv), GvNAMELEN(gv), FALSE)))
341 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
345 cv = (CV*)&PL_sv_undef;
359 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
360 char *s = SvPVX(TOPs);
361 if (strnEQ(s, "CORE::", 6)) {
364 code = keyword(s + 6, SvCUR(TOPs) - 6);
365 if (code < 0) { /* Overridable. */
366 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
367 int i = 0, n = 0, seen_question = 0;
369 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
371 if (code == -KEY_chop || code == -KEY_chomp)
373 while (i < MAXO) { /* The slow way. */
374 if (strEQ(s + 6, PL_op_name[i])
375 || strEQ(s + 6, PL_op_desc[i]))
381 goto nonesuch; /* Should not happen... */
383 oa = PL_opargs[i] >> OASHIFT;
385 if (oa & OA_OPTIONAL && !seen_question) {
389 else if (n && str[0] == ';' && seen_question)
390 goto set; /* XXXX system, exec */
391 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
392 && (oa & (OA_OPTIONAL - 1)) <= OA_SCALARREF
393 /* But globs are already references (kinda) */
394 && (oa & (OA_OPTIONAL - 1)) != OA_FILEREF
398 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
402 ret = sv_2mortal(newSVpvn(str, n - 1));
404 else if (code) /* Non-Overridable */
406 else { /* None such */
408 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
412 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
414 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
423 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
425 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
441 if (GIMME != G_ARRAY) {
445 *MARK = &PL_sv_undef;
446 *MARK = refto(*MARK);
450 EXTEND_MORTAL(SP - MARK);
452 *MARK = refto(*MARK);
457 S_refto(pTHX_ SV *sv)
461 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
464 if (!(sv = LvTARG(sv)))
467 (void)SvREFCNT_inc(sv);
469 else if (SvTYPE(sv) == SVt_PVAV) {
470 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
473 (void)SvREFCNT_inc(sv);
475 else if (SvPADTMP(sv) && !IS_PADGV(sv))
479 (void)SvREFCNT_inc(sv);
482 sv_upgrade(rv, SVt_RV);
496 if (sv && SvGMAGICAL(sv))
499 if (!sv || !SvROK(sv))
503 pv = sv_reftype(sv,TRUE);
504 PUSHp(pv, strlen(pv));
514 stash = CopSTASH(PL_curcop);
520 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
521 Perl_croak(aTHX_ "Attempt to bless into a reference");
523 if (ckWARN(WARN_MISC) && len == 0)
524 Perl_warner(aTHX_ packWARN(WARN_MISC),
525 "Explicit blessing to '' (assuming package main)");
526 stash = gv_stashpvn(ptr, len, TRUE);
529 (void)sv_bless(TOPs, stash);
543 elem = SvPV(sv, n_a);
547 switch (elem ? *elem : '\0')
550 if (strEQ(elem, "ARRAY"))
551 tmpRef = (SV*)GvAV(gv);
554 if (strEQ(elem, "CODE"))
555 tmpRef = (SV*)GvCVu(gv);
558 if (strEQ(elem, "FILEHANDLE")) {
559 /* finally deprecated in 5.8.0 */
560 deprecate("*glob{FILEHANDLE}");
561 tmpRef = (SV*)GvIOp(gv);
564 if (strEQ(elem, "FORMAT"))
565 tmpRef = (SV*)GvFORM(gv);
568 if (strEQ(elem, "GLOB"))
572 if (strEQ(elem, "HASH"))
573 tmpRef = (SV*)GvHV(gv);
576 if (strEQ(elem, "IO"))
577 tmpRef = (SV*)GvIOp(gv);
580 if (strEQ(elem, "NAME"))
581 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
584 if (strEQ(elem, "PACKAGE"))
585 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
588 if (strEQ(elem, "SCALAR"))
602 /* Pattern matching */
607 register unsigned char *s;
610 register I32 *sfirst;
614 if (sv == PL_lastscream) {
620 SvSCREAM_off(PL_lastscream);
621 SvREFCNT_dec(PL_lastscream);
623 PL_lastscream = SvREFCNT_inc(sv);
626 s = (unsigned char*)(SvPV(sv, len));
630 if (pos > PL_maxscream) {
631 if (PL_maxscream < 0) {
632 PL_maxscream = pos + 80;
633 New(301, PL_screamfirst, 256, I32);
634 New(302, PL_screamnext, PL_maxscream, I32);
637 PL_maxscream = pos + pos / 4;
638 Renew(PL_screamnext, PL_maxscream, I32);
642 sfirst = PL_screamfirst;
643 snext = PL_screamnext;
645 if (!sfirst || !snext)
646 DIE(aTHX_ "do_study: out of memory");
648 for (ch = 256; ch; --ch)
655 snext[pos] = sfirst[ch] - pos;
662 /* piggyback on m//g magic */
663 sv_magic(sv, Nullsv, PERL_MAGIC_regex_global, Nullch, 0);
672 if (PL_op->op_flags & OPf_STACKED)
678 TARG = sv_newmortal();
683 /* Lvalue operators. */
695 dSP; dMARK; dTARGET; dORIGMARK;
697 do_chop(TARG, *++MARK);
706 SETi(do_chomp(TOPs));
713 register I32 count = 0;
716 count += do_chomp(POPs);
727 if (!sv || !SvANY(sv))
729 switch (SvTYPE(sv)) {
731 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
732 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
736 if (HvARRAY(sv) || SvGMAGICAL(sv)
737 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
741 if (CvROOT(sv) || CvXSUB(sv))
758 if (!PL_op->op_private) {
767 if (SvTHINKFIRST(sv))
770 switch (SvTYPE(sv)) {
780 if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv))
781 Perl_warner(aTHX_ packWARN(WARN_MISC), "Constant subroutine %s undefined",
782 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
786 /* let user-undef'd sub keep its identity */
787 GV* gv = CvGV((CV*)sv);
794 SvSetMagicSV(sv, &PL_sv_undef);
798 Newz(602, gp, 1, GP);
799 GvGP(sv) = gp_ref(gp);
800 GvSV(sv) = NEWSV(72,0);
801 GvLINE(sv) = CopLINE(PL_curcop);
807 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
810 SvPV_set(sv, Nullch);
823 if (SvTYPE(TOPs) > SVt_PVLV)
824 DIE(aTHX_ PL_no_modify);
825 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
826 && SvIVX(TOPs) != IV_MIN)
829 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
840 if (SvTYPE(TOPs) > SVt_PVLV)
841 DIE(aTHX_ PL_no_modify);
842 sv_setsv(TARG, TOPs);
843 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
844 && SvIVX(TOPs) != IV_MAX)
847 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
861 if (SvTYPE(TOPs) > SVt_PVLV)
862 DIE(aTHX_ PL_no_modify);
863 sv_setsv(TARG, TOPs);
864 if (!SvREADONLY(TOPs) && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)
865 && SvIVX(TOPs) != IV_MIN)
868 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
877 /* Ordinary operators. */
881 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
882 #ifdef PERL_PRESERVE_IVUV
883 /* ** is implemented with pow. pow is floating point. Perl programmers
884 write 2 ** 31 and expect it to be 2147483648
885 pow never made any guarantee to deliver a result to 53 (or whatever)
886 bits of accuracy. Which is unfortunate, as perl programmers expect it
887 to, and on some platforms (eg Irix with long doubles) it doesn't in
888 a very visible case. (2 ** 31, which a regression test uses)
889 So we'll implement power-of-2 ** +ve integer with multiplies, to avoid
894 bool baseuok = SvUOK(TOPm1s);
898 baseuv = SvUVX(TOPm1s);
900 IV iv = SvIVX(TOPm1s);
903 baseuok = TRUE; /* effectively it's a UV now */
905 baseuv = -iv; /* abs, baseuok == false records sign */
919 goto float_it; /* Can't do negative powers this way. */
922 /* now we have integer ** positive integer.
923 foo & (foo - 1) is zero only for a power of 2. */
924 if (!(baseuv & (baseuv - 1))) {
925 /* We are raising power-of-2 to postive integer.
926 The logic here will work for any base (even non-integer
927 bases) but it can be less accurate than
928 pow (base,power) or exp (power * log (base)) when the
929 intermediate values start to spill out of the mantissa.
930 With powers of 2 we know this can't happen.
931 And powers of 2 are the favourite thing for perl
932 programmers to notice ** not doing what they mean. */
934 NV base = baseuok ? baseuv : -(NV)baseuv;
937 /* The logic is this.
938 x ** n === x ** m1 * x ** m2 where n = m1 + m2
939 so as 42 is 32 + 8 + 2
940 x ** 42 can be written as
941 x ** 32 * x ** 8 * x ** 2
942 I can calculate x ** 2, x ** 4, x ** 8 etc trivially:
943 x ** 2n is x ** n * x ** n
944 So I loop round, squaring x each time
945 (x, x ** 2, x ** 4, x ** 8) and multiply the result
946 by the x-value whenever that bit is set in the power.
947 To finish as soon as possible I zero bits in the power
948 when I've done them, so that power becomes zero when
949 I clear the last bit (no more to do), and the loop
951 for (; power; base *= base, n++) {
952 /* Do I look like I trust gcc with long longs here?
954 UV bit = (UV)1 << (UV)n;
957 /* Only bother to clear the bit if it is set. */
972 SETn( Perl_pow( left, right) );
979 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
980 #ifdef PERL_PRESERVE_IVUV
983 /* Unless the left argument is integer in range we are going to have to
984 use NV maths. Hence only attempt to coerce the right argument if
985 we know the left is integer. */
986 /* Left operand is defined, so is it IV? */
989 bool auvok = SvUOK(TOPm1s);
990 bool buvok = SvUOK(TOPs);
991 const UV topmask = (~ (UV)0) << (4 * sizeof (UV));
992 const UV botmask = ~((~ (UV)0) << (4 * sizeof (UV)));
999 alow = SvUVX(TOPm1s);
1001 IV aiv = SvIVX(TOPm1s);
1004 auvok = TRUE; /* effectively it's a UV now */
1006 alow = -aiv; /* abs, auvok == false records sign */
1012 IV biv = SvIVX(TOPs);
1015 buvok = TRUE; /* effectively it's a UV now */
1017 blow = -biv; /* abs, buvok == false records sign */
1021 /* If this does sign extension on unsigned it's time for plan B */
1022 ahigh = alow >> (4 * sizeof (UV));
1024 bhigh = blow >> (4 * sizeof (UV));
1026 if (ahigh && bhigh) {
1027 /* eg 32 bit is at least 0x10000 * 0x10000 == 0x100000000
1028 which is overflow. Drop to NVs below. */
1029 } else if (!ahigh && !bhigh) {
1030 /* eg 32 bit is at most 0xFFFF * 0xFFFF == 0xFFFE0001
1031 so the unsigned multiply cannot overflow. */
1032 UV product = alow * blow;
1033 if (auvok == buvok) {
1034 /* -ve * -ve or +ve * +ve gives a +ve result. */
1038 } else if (product <= (UV)IV_MIN) {
1039 /* 2s complement assumption that (UV)-IV_MIN is correct. */
1040 /* -ve result, which could overflow an IV */
1042 SETi( -(IV)product );
1044 } /* else drop to NVs below. */
1046 /* One operand is large, 1 small */
1049 /* swap the operands */
1051 bhigh = blow; /* bhigh now the temp var for the swap */
1055 /* now, ((ahigh * blow) << half_UV_len) + (alow * blow)
1056 multiplies can't overflow. shift can, add can, -ve can. */
1057 product_middle = ahigh * blow;
1058 if (!(product_middle & topmask)) {
1059 /* OK, (ahigh * blow) won't lose bits when we shift it. */
1061 product_middle <<= (4 * sizeof (UV));
1062 product_low = alow * blow;
1064 /* as for pp_add, UV + something mustn't get smaller.
1065 IIRC ANSI mandates this wrapping *behaviour* for
1066 unsigned whatever the actual representation*/
1067 product_low += product_middle;
1068 if (product_low >= product_middle) {
1069 /* didn't overflow */
1070 if (auvok == buvok) {
1071 /* -ve * -ve or +ve * +ve gives a +ve result. */
1073 SETu( product_low );
1075 } else if (product_low <= (UV)IV_MIN) {
1076 /* 2s complement assumption again */
1077 /* -ve result, which could overflow an IV */
1079 SETi( -(IV)product_low );
1081 } /* else drop to NVs below. */
1083 } /* product_middle too large */
1084 } /* ahigh && bhigh */
1085 } /* SvIOK(TOPm1s) */
1090 SETn( left * right );
1097 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1098 /* Only try to do UV divide first
1099 if ((SLOPPYDIVIDE is true) or
1100 (PERL_PRESERVE_IVUV is true and one or both SV is a UV too large
1102 The assumption is that it is better to use floating point divide
1103 whenever possible, only doing integer divide first if we can't be sure.
1104 If NV_PRESERVES_UV is true then we know at compile time that no UV
1105 can be too large to preserve, so don't need to compile the code to
1106 test the size of UVs. */
1109 # define PERL_TRY_UV_DIVIDE
1110 /* ensure that 20./5. == 4. */
1112 # ifdef PERL_PRESERVE_IVUV
1113 # ifndef NV_PRESERVES_UV
1114 # define PERL_TRY_UV_DIVIDE
1119 #ifdef PERL_TRY_UV_DIVIDE
1122 SvIV_please(TOPm1s);
1123 if (SvIOK(TOPm1s)) {
1124 bool left_non_neg = SvUOK(TOPm1s);
1125 bool right_non_neg = SvUOK(TOPs);
1129 if (right_non_neg) {
1130 right = SvUVX(TOPs);
1133 IV biv = SvIVX(TOPs);
1136 right_non_neg = TRUE; /* effectively it's a UV now */
1142 /* historically undef()/0 gives a "Use of uninitialized value"
1143 warning before dieing, hence this test goes here.
1144 If it were immediately before the second SvIV_please, then
1145 DIE() would be invoked before left was even inspected, so
1146 no inpsection would give no warning. */
1148 DIE(aTHX_ "Illegal division by zero");
1151 left = SvUVX(TOPm1s);
1154 IV aiv = SvIVX(TOPm1s);
1157 left_non_neg = TRUE; /* effectively it's a UV now */
1166 /* For sloppy divide we always attempt integer division. */
1168 /* Otherwise we only attempt it if either or both operands
1169 would not be preserved by an NV. If both fit in NVs
1170 we fall through to the NV divide code below. However,
1171 as left >= right to ensure integer result here, we know that
1172 we can skip the test on the right operand - right big
1173 enough not to be preserved can't get here unless left is
1176 && (left > ((UV)1 << NV_PRESERVES_UV_BITS))
1179 /* Integer division can't overflow, but it can be imprecise. */
1180 UV result = left / right;
1181 if (result * right == left) {
1182 SP--; /* result is valid */
1183 if (left_non_neg == right_non_neg) {
1184 /* signs identical, result is positive. */
1188 /* 2s complement assumption */
1189 if (result <= (UV)IV_MIN)
1190 SETi( -(IV)result );
1192 /* It's exact but too negative for IV. */
1193 SETn( -(NV)result );
1196 } /* tried integer divide but it was not an integer result */
1197 } /* else (abs(result) < 1.0) or (both UVs in range for NV) */
1198 } /* left wasn't SvIOK */
1199 } /* right wasn't SvIOK */
1200 #endif /* PERL_TRY_UV_DIVIDE */
1204 DIE(aTHX_ "Illegal division by zero");
1205 PUSHn( left / right );
1212 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1216 bool left_neg = FALSE;
1217 bool right_neg = FALSE;
1218 bool use_double = FALSE;
1219 bool dright_valid = FALSE;
1225 right_neg = !SvUOK(TOPs);
1227 right = SvUVX(POPs);
1229 IV biv = SvIVX(POPs);
1232 right_neg = FALSE; /* effectively it's a UV now */
1240 right_neg = dright < 0;
1243 if (dright < UV_MAX_P1) {
1244 right = U_V(dright);
1245 dright_valid = TRUE; /* In case we need to use double below. */
1251 /* At this point use_double is only true if right is out of range for
1252 a UV. In range NV has been rounded down to nearest UV and
1253 use_double false. */
1255 if (!use_double && SvIOK(TOPs)) {
1257 left_neg = !SvUOK(TOPs);
1261 IV aiv = SvIVX(POPs);
1264 left_neg = FALSE; /* effectively it's a UV now */
1273 left_neg = dleft < 0;
1277 /* This should be exactly the 5.6 behaviour - if left and right are
1278 both in range for UV then use U_V() rather than floor. */
1280 if (dleft < UV_MAX_P1) {
1281 /* right was in range, so is dleft, so use UVs not double.
1285 /* left is out of range for UV, right was in range, so promote
1286 right (back) to double. */
1288 /* The +0.5 is used in 5.6 even though it is not strictly
1289 consistent with the implicit +0 floor in the U_V()
1290 inside the #if 1. */
1291 dleft = Perl_floor(dleft + 0.5);
1294 dright = Perl_floor(dright + 0.5);
1304 DIE(aTHX_ "Illegal modulus zero");
1306 dans = Perl_fmod(dleft, dright);
1307 if ((left_neg != right_neg) && dans)
1308 dans = dright - dans;
1311 sv_setnv(TARG, dans);
1317 DIE(aTHX_ "Illegal modulus zero");
1320 if ((left_neg != right_neg) && ans)
1323 /* XXX may warn: unary minus operator applied to unsigned type */
1324 /* could change -foo to be (~foo)+1 instead */
1325 if (ans <= ~((UV)IV_MAX)+1)
1326 sv_setiv(TARG, ~ans+1);
1328 sv_setnv(TARG, -(NV)ans);
1331 sv_setuv(TARG, ans);
1340 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1342 register IV count = POPi;
1343 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1345 I32 items = SP - MARK;
1348 max = items * count;
1353 /* This code was intended to fix 20010809.028:
1356 for (($x =~ /./g) x 2) {
1357 print chop; # "abcdabcd" expected as output.
1360 * but that change (#11635) broke this code:
1362 $x = [("foo")x2]; # only one "foo" ended up in the anonlist.
1364 * I can't think of a better fix that doesn't introduce
1365 * an efficiency hit by copying the SVs. The stack isn't
1366 * refcounted, and mortalisation obviously doesn't
1367 * Do The Right Thing when the stack has more than
1368 * one pointer to the same mortal value.
1372 *SP = sv_2mortal(newSVsv(*SP));
1382 repeatcpy((char*)(MARK + items), (char*)MARK,
1383 items * sizeof(SV*), count - 1);
1386 else if (count <= 0)
1389 else { /* Note: mark already snarfed by pp_list */
1394 SvSetSV(TARG, tmpstr);
1395 SvPV_force(TARG, len);
1396 isutf = DO_UTF8(TARG);
1401 SvGROW(TARG, (count * len) + 1);
1402 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1403 SvCUR(TARG) *= count;
1405 *SvEND(TARG) = '\0';
1408 (void)SvPOK_only_UTF8(TARG);
1410 (void)SvPOK_only(TARG);
1412 if (PL_op->op_private & OPpREPEAT_DOLIST) {
1413 /* The parser saw this as a list repeat, and there
1414 are probably several items on the stack. But we're
1415 in scalar context, and there's no pp_list to save us
1416 now. So drop the rest of the items -- robin@kitsite.com
1429 dSP; dATARGET; bool useleft; tryAMAGICbin(subtr,opASSIGN);
1430 useleft = USE_LEFT(TOPm1s);
1431 #ifdef PERL_PRESERVE_IVUV
1432 /* See comments in pp_add (in pp_hot.c) about Overflow, and how
1433 "bad things" happen if you rely on signed integers wrapping. */
1436 /* Unless the left argument is integer in range we are going to have to
1437 use NV maths. Hence only attempt to coerce the right argument if
1438 we know the left is integer. */
1439 register UV auv = 0;
1445 a_valid = auvok = 1;
1446 /* left operand is undef, treat as zero. */
1448 /* Left operand is defined, so is it IV? */
1449 SvIV_please(TOPm1s);
1450 if (SvIOK(TOPm1s)) {
1451 if ((auvok = SvUOK(TOPm1s)))
1452 auv = SvUVX(TOPm1s);
1454 register IV aiv = SvIVX(TOPm1s);
1457 auvok = 1; /* Now acting as a sign flag. */
1458 } else { /* 2s complement assumption for IV_MIN */
1466 bool result_good = 0;
1469 bool buvok = SvUOK(TOPs);
1474 register IV biv = SvIVX(TOPs);
1481 /* ?uvok if value is >= 0. basically, flagged as UV if it's +ve,
1482 else "IV" now, independent of how it came in.
1483 if a, b represents positive, A, B negative, a maps to -A etc
1488 all UV maths. negate result if A negative.
1489 subtract if signs same, add if signs differ. */
1491 if (auvok ^ buvok) {
1500 /* Must get smaller */
1505 if (result <= buv) {
1506 /* result really should be -(auv-buv). as its negation
1507 of true value, need to swap our result flag */
1519 if (result <= (UV)IV_MIN)
1520 SETi( -(IV)result );
1522 /* result valid, but out of range for IV. */
1523 SETn( -(NV)result );
1527 } /* Overflow, drop through to NVs. */
1531 useleft = USE_LEFT(TOPm1s);
1535 /* left operand is undef, treat as zero - value */
1539 SETn( TOPn - value );
1546 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1549 if (PL_op->op_private & HINT_INTEGER) {
1563 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1566 if (PL_op->op_private & HINT_INTEGER) {
1580 dSP; tryAMAGICbinSET(lt,0);
1581 #ifdef PERL_PRESERVE_IVUV
1584 SvIV_please(TOPm1s);
1585 if (SvIOK(TOPm1s)) {
1586 bool auvok = SvUOK(TOPm1s);
1587 bool buvok = SvUOK(TOPs);
1589 if (!auvok && !buvok) { /* ## IV < IV ## */
1590 IV aiv = SvIVX(TOPm1s);
1591 IV biv = SvIVX(TOPs);
1594 SETs(boolSV(aiv < biv));
1597 if (auvok && buvok) { /* ## UV < UV ## */
1598 UV auv = SvUVX(TOPm1s);
1599 UV buv = SvUVX(TOPs);
1602 SETs(boolSV(auv < buv));
1605 if (auvok) { /* ## UV < IV ## */
1612 /* As (a) is a UV, it's >=0, so it cannot be < */
1617 SETs(boolSV(auv < (UV)biv));
1620 { /* ## IV < UV ## */
1624 aiv = SvIVX(TOPm1s);
1626 /* As (b) is a UV, it's >=0, so it must be < */
1633 SETs(boolSV((UV)aiv < buv));
1639 #ifndef NV_PRESERVES_UV
1640 #ifdef PERL_PRESERVE_IVUV
1643 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1645 SETs(boolSV(SvRV(TOPs) < SvRV(TOPp1s)));
1651 SETs(boolSV(TOPn < value));
1658 dSP; tryAMAGICbinSET(gt,0);
1659 #ifdef PERL_PRESERVE_IVUV
1662 SvIV_please(TOPm1s);
1663 if (SvIOK(TOPm1s)) {
1664 bool auvok = SvUOK(TOPm1s);
1665 bool buvok = SvUOK(TOPs);
1667 if (!auvok && !buvok) { /* ## IV > IV ## */
1668 IV aiv = SvIVX(TOPm1s);
1669 IV biv = SvIVX(TOPs);
1672 SETs(boolSV(aiv > biv));
1675 if (auvok && buvok) { /* ## UV > UV ## */
1676 UV auv = SvUVX(TOPm1s);
1677 UV buv = SvUVX(TOPs);
1680 SETs(boolSV(auv > buv));
1683 if (auvok) { /* ## UV > IV ## */
1690 /* As (a) is a UV, it's >=0, so it must be > */
1695 SETs(boolSV(auv > (UV)biv));
1698 { /* ## IV > UV ## */
1702 aiv = SvIVX(TOPm1s);
1704 /* As (b) is a UV, it's >=0, so it cannot be > */
1711 SETs(boolSV((UV)aiv > buv));
1717 #ifndef NV_PRESERVES_UV
1718 #ifdef PERL_PRESERVE_IVUV
1721 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1723 SETs(boolSV(SvRV(TOPs) > SvRV(TOPp1s)));
1729 SETs(boolSV(TOPn > value));
1736 dSP; tryAMAGICbinSET(le,0);
1737 #ifdef PERL_PRESERVE_IVUV
1740 SvIV_please(TOPm1s);
1741 if (SvIOK(TOPm1s)) {
1742 bool auvok = SvUOK(TOPm1s);
1743 bool buvok = SvUOK(TOPs);
1745 if (!auvok && !buvok) { /* ## IV <= IV ## */
1746 IV aiv = SvIVX(TOPm1s);
1747 IV biv = SvIVX(TOPs);
1750 SETs(boolSV(aiv <= biv));
1753 if (auvok && buvok) { /* ## UV <= UV ## */
1754 UV auv = SvUVX(TOPm1s);
1755 UV buv = SvUVX(TOPs);
1758 SETs(boolSV(auv <= buv));
1761 if (auvok) { /* ## UV <= IV ## */
1768 /* As (a) is a UV, it's >=0, so a cannot be <= */
1773 SETs(boolSV(auv <= (UV)biv));
1776 { /* ## IV <= UV ## */
1780 aiv = SvIVX(TOPm1s);
1782 /* As (b) is a UV, it's >=0, so a must be <= */
1789 SETs(boolSV((UV)aiv <= buv));
1795 #ifndef NV_PRESERVES_UV
1796 #ifdef PERL_PRESERVE_IVUV
1799 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1801 SETs(boolSV(SvRV(TOPs) <= SvRV(TOPp1s)));
1807 SETs(boolSV(TOPn <= value));
1814 dSP; tryAMAGICbinSET(ge,0);
1815 #ifdef PERL_PRESERVE_IVUV
1818 SvIV_please(TOPm1s);
1819 if (SvIOK(TOPm1s)) {
1820 bool auvok = SvUOK(TOPm1s);
1821 bool buvok = SvUOK(TOPs);
1823 if (!auvok && !buvok) { /* ## IV >= IV ## */
1824 IV aiv = SvIVX(TOPm1s);
1825 IV biv = SvIVX(TOPs);
1828 SETs(boolSV(aiv >= biv));
1831 if (auvok && buvok) { /* ## UV >= UV ## */
1832 UV auv = SvUVX(TOPm1s);
1833 UV buv = SvUVX(TOPs);
1836 SETs(boolSV(auv >= buv));
1839 if (auvok) { /* ## UV >= IV ## */
1846 /* As (a) is a UV, it's >=0, so it must be >= */
1851 SETs(boolSV(auv >= (UV)biv));
1854 { /* ## IV >= UV ## */
1858 aiv = SvIVX(TOPm1s);
1860 /* As (b) is a UV, it's >=0, so a cannot be >= */
1867 SETs(boolSV((UV)aiv >= buv));
1873 #ifndef NV_PRESERVES_UV
1874 #ifdef PERL_PRESERVE_IVUV
1877 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1879 SETs(boolSV(SvRV(TOPs) >= SvRV(TOPp1s)));
1885 SETs(boolSV(TOPn >= value));
1892 dSP; tryAMAGICbinSET(ne,0);
1893 #ifndef NV_PRESERVES_UV
1894 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1896 SETs(boolSV(SvRV(TOPs) != SvRV(TOPp1s)));
1900 #ifdef PERL_PRESERVE_IVUV
1903 SvIV_please(TOPm1s);
1904 if (SvIOK(TOPm1s)) {
1905 bool auvok = SvUOK(TOPm1s);
1906 bool buvok = SvUOK(TOPs);
1908 if (auvok == buvok) { /* ## IV == IV or UV == UV ## */
1909 /* Casting IV to UV before comparison isn't going to matter
1910 on 2s complement. On 1s complement or sign&magnitude
1911 (if we have any of them) it could make negative zero
1912 differ from normal zero. As I understand it. (Need to
1913 check - is negative zero implementation defined behaviour
1915 UV buv = SvUVX(POPs);
1916 UV auv = SvUVX(TOPs);
1918 SETs(boolSV(auv != buv));
1921 { /* ## Mixed IV,UV ## */
1925 /* != is commutative so swap if needed (save code) */
1927 /* swap. top of stack (b) is the iv */
1931 /* As (a) is a UV, it's >0, so it cannot be == */
1940 /* As (b) is a UV, it's >0, so it cannot be == */
1944 uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
1946 SETs(boolSV((UV)iv != uv));
1954 SETs(boolSV(TOPn != value));
1961 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1962 #ifndef NV_PRESERVES_UV
1963 if (SvROK(TOPs) && SvROK(TOPm1s)) {
1964 UV right = PTR2UV(SvRV(POPs));
1965 UV left = PTR2UV(SvRV(TOPs));
1966 SETi((left > right) - (left < right));
1970 #ifdef PERL_PRESERVE_IVUV
1971 /* Fortunately it seems NaN isn't IOK */
1974 SvIV_please(TOPm1s);
1975 if (SvIOK(TOPm1s)) {
1976 bool leftuvok = SvUOK(TOPm1s);
1977 bool rightuvok = SvUOK(TOPs);
1979 if (!leftuvok && !rightuvok) { /* ## IV <=> IV ## */
1980 IV leftiv = SvIVX(TOPm1s);
1981 IV rightiv = SvIVX(TOPs);
1983 if (leftiv > rightiv)
1985 else if (leftiv < rightiv)
1989 } else if (leftuvok && rightuvok) { /* ## UV <=> UV ## */
1990 UV leftuv = SvUVX(TOPm1s);
1991 UV rightuv = SvUVX(TOPs);
1993 if (leftuv > rightuv)
1995 else if (leftuv < rightuv)
1999 } else if (leftuvok) { /* ## UV <=> IV ## */
2003 rightiv = SvIVX(TOPs);
2005 /* As (a) is a UV, it's >=0, so it cannot be < */
2008 leftuv = SvUVX(TOPm1s);
2009 if (leftuv > (UV)rightiv) {
2011 } else if (leftuv < (UV)rightiv) {
2017 } else { /* ## IV <=> UV ## */
2021 leftiv = SvIVX(TOPm1s);
2023 /* As (b) is a UV, it's >=0, so it must be < */
2026 rightuv = SvUVX(TOPs);
2027 if ((UV)leftiv > rightuv) {
2029 } else if ((UV)leftiv < rightuv) {
2047 if (Perl_isnan(left) || Perl_isnan(right)) {
2051 value = (left > right) - (left < right);
2055 else if (left < right)
2057 else if (left > right)
2071 dSP; tryAMAGICbinSET(slt,0);
2074 int cmp = (IN_LOCALE_RUNTIME
2075 ? sv_cmp_locale(left, right)
2076 : sv_cmp(left, right));
2077 SETs(boolSV(cmp < 0));
2084 dSP; tryAMAGICbinSET(sgt,0);
2087 int cmp = (IN_LOCALE_RUNTIME
2088 ? sv_cmp_locale(left, right)
2089 : sv_cmp(left, right));
2090 SETs(boolSV(cmp > 0));
2097 dSP; tryAMAGICbinSET(sle,0);
2100 int cmp = (IN_LOCALE_RUNTIME
2101 ? sv_cmp_locale(left, right)
2102 : sv_cmp(left, right));
2103 SETs(boolSV(cmp <= 0));
2110 dSP; tryAMAGICbinSET(sge,0);
2113 int cmp = (IN_LOCALE_RUNTIME
2114 ? sv_cmp_locale(left, right)
2115 : sv_cmp(left, right));
2116 SETs(boolSV(cmp >= 0));
2123 dSP; tryAMAGICbinSET(seq,0);
2126 SETs(boolSV(sv_eq(left, right)));
2133 dSP; tryAMAGICbinSET(sne,0);
2136 SETs(boolSV(!sv_eq(left, right)));
2143 dSP; dTARGET; tryAMAGICbin(scmp,0);
2146 int cmp = (IN_LOCALE_RUNTIME
2147 ? sv_cmp_locale(left, right)
2148 : sv_cmp(left, right));
2156 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
2159 if (SvNIOKp(left) || SvNIOKp(right)) {
2160 if (PL_op->op_private & HINT_INTEGER) {
2161 IV i = SvIV(left) & SvIV(right);
2165 UV u = SvUV(left) & SvUV(right);
2170 do_vop(PL_op->op_type, TARG, left, right);
2179 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
2182 if (SvNIOKp(left) || SvNIOKp(right)) {
2183 if (PL_op->op_private & HINT_INTEGER) {
2184 IV i = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
2188 UV u = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
2193 do_vop(PL_op->op_type, TARG, left, right);
2202 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
2205 if (SvNIOKp(left) || SvNIOKp(right)) {
2206 if (PL_op->op_private & HINT_INTEGER) {
2207 IV i = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
2211 UV u = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
2216 do_vop(PL_op->op_type, TARG, left, right);
2225 dSP; dTARGET; tryAMAGICun(neg);
2228 int flags = SvFLAGS(sv);
2231 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
2232 /* It's publicly an integer, or privately an integer-not-float */
2235 if (SvIVX(sv) == IV_MIN) {
2236 /* 2s complement assumption. */
2237 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
2240 else if (SvUVX(sv) <= IV_MAX) {
2245 else if (SvIVX(sv) != IV_MIN) {
2249 #ifdef PERL_PRESERVE_IVUV
2258 else if (SvPOKp(sv)) {
2260 char *s = SvPV(sv, len);
2261 if (isIDFIRST(*s)) {
2262 sv_setpvn(TARG, "-", 1);
2265 else if (*s == '+' || *s == '-') {
2267 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
2269 else if (DO_UTF8(sv)) {
2272 goto oops_its_an_int;
2274 sv_setnv(TARG, -SvNV(sv));
2276 sv_setpvn(TARG, "-", 1);
2283 goto oops_its_an_int;
2284 sv_setnv(TARG, -SvNV(sv));
2296 dSP; tryAMAGICunSET(not);
2297 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
2303 dSP; dTARGET; tryAMAGICun(compl);
2307 if (PL_op->op_private & HINT_INTEGER) {
2322 tmps = (U8*)SvPV_force(TARG, len);
2325 /* Calculate exact length, let's not estimate. */
2334 while (tmps < send) {
2335 UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2336 tmps += UTF8SKIP(tmps);
2337 targlen += UNISKIP(~c);
2343 /* Now rewind strings and write them. */
2347 Newz(0, result, targlen + 1, U8);
2348 while (tmps < send) {
2349 UV c = utf8n_to_uvchr(tmps, send-tmps, &l, UTF8_ALLOW_ANYUV);
2350 tmps += UTF8SKIP(tmps);
2351 result = uvchr_to_utf8_flags(result, ~c, UNICODE_ALLOW_ANY);
2355 sv_setpvn(TARG, (char*)result, targlen);
2359 Newz(0, result, nchar + 1, U8);
2360 while (tmps < send) {
2361 U8 c = (U8)utf8n_to_uvchr(tmps, 0, &l, UTF8_ALLOW_ANY);
2362 tmps += UTF8SKIP(tmps);
2367 sv_setpvn(TARG, (char*)result, nchar);
2375 register long *tmpl;
2376 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
2379 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
2384 for ( ; anum > 0; anum--, tmps++)
2393 /* integer versions of some of the above */
2397 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
2400 SETi( left * right );
2407 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
2411 DIE(aTHX_ "Illegal division by zero");
2412 value = POPi / value;
2420 dSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
2424 DIE(aTHX_ "Illegal modulus zero");
2425 SETi( left % right );
2432 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
2435 SETi( left + right );
2442 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
2445 SETi( left - right );
2452 dSP; tryAMAGICbinSET(lt,0);
2455 SETs(boolSV(left < right));
2462 dSP; tryAMAGICbinSET(gt,0);
2465 SETs(boolSV(left > right));
2472 dSP; tryAMAGICbinSET(le,0);
2475 SETs(boolSV(left <= right));
2482 dSP; tryAMAGICbinSET(ge,0);
2485 SETs(boolSV(left >= right));
2492 dSP; tryAMAGICbinSET(eq,0);
2495 SETs(boolSV(left == right));
2502 dSP; tryAMAGICbinSET(ne,0);
2505 SETs(boolSV(left != right));
2512 dSP; dTARGET; tryAMAGICbin(ncmp,0);
2519 else if (left < right)
2530 dSP; dTARGET; tryAMAGICun(neg);
2535 /* High falutin' math. */
2539 dSP; dTARGET; tryAMAGICbin(atan2,0);
2542 SETn(Perl_atan2(left, right));
2549 dSP; dTARGET; tryAMAGICun(sin);
2553 value = Perl_sin(value);
2561 dSP; dTARGET; tryAMAGICun(cos);
2565 value = Perl_cos(value);
2571 /* Support Configure command-line overrides for rand() functions.
2572 After 5.005, perhaps we should replace this by Configure support
2573 for drand48(), random(), or rand(). For 5.005, though, maintain
2574 compatibility by calling rand() but allow the user to override it.
2575 See INSTALL for details. --Andy Dougherty 15 July 1998
2577 /* Now it's after 5.005, and Configure supports drand48() and random(),
2578 in addition to rand(). So the overrides should not be needed any more.
2579 --Jarkko Hietaniemi 27 September 1998
2582 #ifndef HAS_DRAND48_PROTO
2583 extern double drand48 (void);
2596 if (!PL_srand_called) {
2597 (void)seedDrand01((Rand_seed_t)seed());
2598 PL_srand_called = TRUE;
2613 (void)seedDrand01((Rand_seed_t)anum);
2614 PL_srand_called = TRUE;
2623 * This is really just a quick hack which grabs various garbage
2624 * values. It really should be a real hash algorithm which
2625 * spreads the effect of every input bit onto every output bit,
2626 * if someone who knows about such things would bother to write it.
2627 * Might be a good idea to add that function to CORE as well.
2628 * No numbers below come from careful analysis or anything here,
2629 * except they are primes and SEED_C1 > 1E6 to get a full-width
2630 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
2631 * probably be bigger too.
2634 # define SEED_C1 1000003
2635 #define SEED_C4 73819
2637 # define SEED_C1 25747
2638 #define SEED_C4 20639
2642 #define SEED_C5 26107
2644 #ifndef PERL_NO_DEV_RANDOM
2649 # include <starlet.h>
2650 /* when[] = (low 32 bits, high 32 bits) of time since epoch
2651 * in 100-ns units, typically incremented ever 10 ms. */
2652 unsigned int when[2];
2654 # ifdef HAS_GETTIMEOFDAY
2655 struct timeval when;
2661 /* This test is an escape hatch, this symbol isn't set by Configure. */
2662 #ifndef PERL_NO_DEV_RANDOM
2663 #ifndef PERL_RANDOM_DEVICE
2664 /* /dev/random isn't used by default because reads from it will block
2665 * if there isn't enough entropy available. You can compile with
2666 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
2667 * is enough real entropy to fill the seed. */
2668 # define PERL_RANDOM_DEVICE "/dev/urandom"
2670 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
2672 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
2681 _ckvmssts(sys$gettim(when));
2682 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
2684 # ifdef HAS_GETTIMEOFDAY
2685 PerlProc_gettimeofday(&when,NULL);
2686 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
2689 u = (U32)SEED_C1 * when;
2692 u += SEED_C3 * (U32)PerlProc_getpid();
2693 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
2694 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
2695 u += SEED_C5 * (U32)PTR2UV(&when);
2702 dSP; dTARGET; tryAMAGICun(exp);
2706 value = Perl_exp(value);
2714 dSP; dTARGET; tryAMAGICun(log);
2719 SET_NUMERIC_STANDARD();
2720 DIE(aTHX_ "Can't take log of %"NVgf, value);
2722 value = Perl_log(value);
2730 dSP; dTARGET; tryAMAGICun(sqrt);
2735 SET_NUMERIC_STANDARD();
2736 DIE(aTHX_ "Can't take sqrt of %"NVgf, value);
2738 value = Perl_sqrt(value);
2745 * There are strange code-generation bugs caused on sparc64 by gcc-2.95.2.
2746 * These need to be revisited when a newer toolchain becomes available.
2748 #if defined(__sparc64__) && defined(__GNUC__)
2749 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
2750 # undef SPARC64_MODF_WORKAROUND
2751 # define SPARC64_MODF_WORKAROUND 1
2755 #if defined(SPARC64_MODF_WORKAROUND)
2757 sparc64_workaround_modf(NV theVal, NV *theIntRes)
2760 ret = Perl_modf(theVal, &res);
2768 dSP; dTARGET; tryAMAGICun(int);
2771 IV iv = TOPi; /* attempt to convert to IV if possible. */
2772 /* XXX it's arguable that compiler casting to IV might be subtly
2773 different from modf (for numbers inside (IV_MIN,UV_MAX)) in which
2774 else preferring IV has introduced a subtle behaviour change bug. OTOH
2775 relying on floating point to be accurate is a bug. */
2786 if (value < (NV)UV_MAX + 0.5) {
2789 #if defined(SPARC64_MODF_WORKAROUND)
2790 (void)sparc64_workaround_modf(value, &value);
2792 # if defined(HAS_MODFL) || defined(LONG_DOUBLE_EQUALS_DOUBLE)
2793 # ifdef HAS_MODFL_POW32_BUG
2794 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
2796 NV offset = Perl_modf(value, &value);
2797 (void)Perl_modf(offset, &offset);
2801 (void)Perl_modf(value, &value);
2804 double tmp = (double)value;
2805 (void)Perl_modf(tmp, &tmp);
2813 if (value > (NV)IV_MIN - 0.5) {
2816 #if defined(HAS_MODFL) || defined(LONG_DOUBLE_EQUALS_DOUBLE)
2817 # ifdef HAS_MODFL_POW32_BUG
2818 /* some versions of glibc split (i + d) into (i-1, d+1) for 2^32 <= i < 2^64 */
2820 NV offset = Perl_modf(-value, &value);
2821 (void)Perl_modf(offset, &offset);
2825 (void)Perl_modf(-value, &value);
2829 double tmp = (double)value;
2830 (void)Perl_modf(-tmp, &tmp);
2843 dSP; dTARGET; tryAMAGICun(abs);
2845 /* This will cache the NV value if string isn't actually integer */
2849 /* IVX is precise */
2851 SETu(TOPu); /* force it to be numeric only */
2859 /* 2s complement assumption. Also, not really needed as
2860 IV_MIN and -IV_MIN should both be %100...00 and NV-able */
2880 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2886 tmps = (SvPVx(sv, len));
2888 /* If Unicode, try to downgrade
2889 * If not possible, croak. */
2890 SV* tsv = sv_2mortal(newSVsv(sv));
2893 sv_utf8_downgrade(tsv, FALSE);
2896 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2897 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2910 I32 flags = PERL_SCAN_ALLOW_UNDERSCORES;
2916 tmps = (SvPVx(sv, len));
2918 /* If Unicode, try to downgrade
2919 * If not possible, croak. */
2920 SV* tsv = sv_2mortal(newSVsv(sv));
2923 sv_utf8_downgrade(tsv, FALSE);
2926 while (*tmps && len && isSPACE(*tmps))
2931 result_uv = grok_hex (tmps, &len, &flags, &result_nv);
2932 else if (*tmps == 'b')
2933 result_uv = grok_bin (tmps, &len, &flags, &result_nv);
2935 result_uv = grok_oct (tmps, &len, &flags, &result_nv);
2937 if (flags & PERL_SCAN_GREATER_THAN_UV_MAX) {
2954 SETi(sv_len_utf8(sv));
2970 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
2972 I32 arybase = PL_curcop->cop_arybase;
2976 int num_args = PL_op->op_private & 7;
2977 bool repl_need_utf8_upgrade = FALSE;
2978 bool repl_is_utf8 = FALSE;
2980 SvTAINTED_off(TARG); /* decontaminate */
2981 SvUTF8_off(TARG); /* decontaminate */
2985 repl = SvPV(repl_sv, repl_len);
2986 repl_is_utf8 = DO_UTF8(repl_sv) && SvCUR(repl_sv);
2996 sv_utf8_upgrade(sv);
2998 else if (DO_UTF8(sv))
2999 repl_need_utf8_upgrade = TRUE;
3001 tmps = SvPV(sv, curlen);
3003 utf8_curlen = sv_len_utf8(sv);
3004 if (utf8_curlen == curlen)
3007 curlen = utf8_curlen;
3012 if (pos >= arybase) {
3030 else if (len >= 0) {
3032 if (rem > (I32)curlen)
3047 Perl_croak(aTHX_ "substr outside of string");
3048 if (ckWARN(WARN_SUBSTR))
3049 Perl_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of string");
3056 sv_pos_u2b(sv, &pos, &rem);
3058 sv_setpvn(TARG, tmps, rem);
3059 #ifdef USE_LOCALE_COLLATE
3060 sv_unmagic(TARG, PERL_MAGIC_collxfrm);
3065 SV* repl_sv_copy = NULL;
3067 if (repl_need_utf8_upgrade) {
3068 repl_sv_copy = newSVsv(repl_sv);
3069 sv_utf8_upgrade(repl_sv_copy);
3070 repl = SvPV(repl_sv_copy, repl_len);
3071 repl_is_utf8 = DO_UTF8(repl_sv_copy) && SvCUR(sv);
3073 sv_insert(sv, pos, rem, repl, repl_len);
3077 SvREFCNT_dec(repl_sv_copy);
3079 else if (lvalue) { /* it's an lvalue! */
3080 if (!SvGMAGICAL(sv)) {
3084 if (ckWARN(WARN_SUBSTR))
3085 Perl_warner(aTHX_ packWARN(WARN_SUBSTR),
3086 "Attempt to use reference as lvalue in substr");
3088 if (SvOK(sv)) /* is it defined ? */
3089 (void)SvPOK_only_UTF8(sv);
3091 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
3094 if (SvTYPE(TARG) < SVt_PVLV) {
3095 sv_upgrade(TARG, SVt_PVLV);
3096 sv_magic(TARG, Nullsv, PERL_MAGIC_substr, Nullch, 0);
3100 if (LvTARG(TARG) != sv) {
3102 SvREFCNT_dec(LvTARG(TARG));
3103 LvTARG(TARG) = SvREFCNT_inc(sv);
3105 LvTARGOFF(TARG) = upos;
3106 LvTARGLEN(TARG) = urem;
3110 PUSHs(TARG); /* avoid SvSETMAGIC here */
3117 register IV size = POPi;
3118 register IV offset = POPi;
3119 register SV *src = POPs;
3120 I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET;
3122 SvTAINTED_off(TARG); /* decontaminate */
3123 if (lvalue) { /* it's an lvalue! */
3124 if (SvTYPE(TARG) < SVt_PVLV) {
3125 sv_upgrade(TARG, SVt_PVLV);
3126 sv_magic(TARG, Nullsv, PERL_MAGIC_vec, Nullch, 0);
3129 if (LvTARG(TARG) != src) {
3131 SvREFCNT_dec(LvTARG(TARG));
3132 LvTARG(TARG) = SvREFCNT_inc(src);
3134 LvTARGOFF(TARG) = offset;
3135 LvTARGLEN(TARG) = size;
3138 sv_setuv(TARG, do_vecget(src, offset, size));
3153 I32 arybase = PL_curcop->cop_arybase;
3158 offset = POPi - arybase;
3161 tmps = SvPV(big, biglen);
3162 if (offset > 0 && DO_UTF8(big))
3163 sv_pos_u2b(big, &offset, 0);
3166 else if (offset > (I32)biglen)
3168 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
3169 (unsigned char*)tmps + biglen, little, 0)))
3172 retval = tmps2 - tmps;
3173 if (retval > 0 && DO_UTF8(big))
3174 sv_pos_b2u(big, &retval);
3175 PUSHi(retval + arybase);
3190 I32 arybase = PL_curcop->cop_arybase;
3196 tmps2 = SvPV(little, llen);
3197 tmps = SvPV(big, blen);
3201 if (offset > 0 && DO_UTF8(big))
3202 sv_pos_u2b(big, &offset, 0);
3203 offset = offset - arybase + llen;
3207 else if (offset > (I32)blen)
3209 if (!(tmps2 = rninstr(tmps, tmps + offset,
3210 tmps2, tmps2 + llen)))
3213 retval = tmps2 - tmps;
3214 if (retval > 0 && DO_UTF8(big))
3215 sv_pos_b2u(big, &retval);
3216 PUSHi(retval + arybase);
3222 dSP; dMARK; dORIGMARK; dTARGET;
3223 do_sprintf(TARG, SP-MARK, MARK+1);
3224 TAINT_IF(SvTAINTED(TARG));
3225 if (DO_UTF8(*(MARK+1)))
3237 U8 *s = (U8*)SvPVx(argsv, len);
3240 if (PL_encoding && SvPOK(argsv) && !DO_UTF8(argsv)) {
3241 tmpsv = sv_2mortal(newSVsv(argsv));
3242 s = (U8*)sv_recode_to_utf8(tmpsv, PL_encoding);
3246 XPUSHu(DO_UTF8(argsv) ?
3247 utf8n_to_uvchr(s, UTF8_MAXLEN, 0, UTF8_ALLOW_ANYUV) :
3259 (void)SvUPGRADE(TARG,SVt_PV);
3261 if (value > 255 && !IN_BYTES) {
3262 SvGROW(TARG, (STRLEN)UNISKIP(value)+1);
3263 tmps = (char*)uvchr_to_utf8_flags((U8*)SvPVX(TARG), value, 0);
3264 SvCUR_set(TARG, tmps - SvPVX(TARG));
3266 (void)SvPOK_only(TARG);
3275 *tmps++ = (char)value;
3277 (void)SvPOK_only(TARG);
3279 sv_recode_to_utf8(TARG, PL_encoding);
3291 char *tmps = SvPV(left, len);
3293 if (DO_UTF8(left)) {
3294 /* If Unicode, try to downgrade.
3295 * If not possible, croak.
3296 * Yes, we made this up. */
3297 SV* tsv = sv_2mortal(newSVsv(left));
3300 sv_utf8_downgrade(tsv, FALSE);
3304 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
3306 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
3312 "The crypt() function is unimplemented due to excessive paranoia.");
3324 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3328 s = (U8*)SvPV(sv, slen);
3329 utf8_to_uvchr(s, &ulen);
3331 toTITLE_utf8(s, tmpbuf, &tculen);
3332 utf8_to_uvchr(tmpbuf, 0);
3334 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3336 sv_setpvn(TARG, (char*)tmpbuf, tculen);
3337 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3342 s = (U8*)SvPV_force(sv, slen);
3343 Copy(tmpbuf, s, tculen, U8);
3347 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3349 SvUTF8_off(TARG); /* decontaminate */
3354 s = (U8*)SvPV_force(sv, slen);
3356 if (IN_LOCALE_RUNTIME) {
3359 *s = toUPPER_LC(*s);
3377 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && UTF8_IS_START(*s)) {
3379 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3383 toLOWER_utf8(s, tmpbuf, &ulen);
3384 uv = utf8_to_uvchr(tmpbuf, 0);
3386 tend = uvchr_to_utf8(tmpbuf, uv);
3388 if (!SvPADTMP(sv) || (STRLEN)(tend - tmpbuf) != ulen || SvREADONLY(sv)) {
3390 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
3391 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
3396 s = (U8*)SvPV_force(sv, slen);
3397 Copy(tmpbuf, s, ulen, U8);
3401 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3403 SvUTF8_off(TARG); /* decontaminate */
3408 s = (U8*)SvPV_force(sv, slen);
3410 if (IN_LOCALE_RUNTIME) {
3413 *s = toLOWER_LC(*s);
3436 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3438 s = (U8*)SvPV(sv,len);
3440 SvUTF8_off(TARG); /* decontaminate */
3441 sv_setpvn(TARG, "", 0);
3445 STRLEN nchar = utf8_length(s, s + len);
3447 (void)SvUPGRADE(TARG, SVt_PV);
3448 SvGROW(TARG, (nchar * UTF8_MAXLEN_UCLC) + 1);
3449 (void)SvPOK_only(TARG);
3450 d = (U8*)SvPVX(TARG);
3453 toUPPER_utf8(s, tmpbuf, &ulen);
3454 Copy(tmpbuf, d, ulen, U8);
3460 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3465 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3467 SvUTF8_off(TARG); /* decontaminate */
3472 s = (U8*)SvPV_force(sv, len);
3474 register U8 *send = s + len;
3476 if (IN_LOCALE_RUNTIME) {
3479 for (; s < send; s++)
3480 *s = toUPPER_LC(*s);
3483 for (; s < send; s++)
3505 U8 tmpbuf[UTF8_MAXLEN_UCLC+1];
3507 s = (U8*)SvPV(sv,len);
3509 SvUTF8_off(TARG); /* decontaminate */
3510 sv_setpvn(TARG, "", 0);
3514 STRLEN nchar = utf8_length(s, s + len);
3516 (void)SvUPGRADE(TARG, SVt_PV);
3517 SvGROW(TARG, (nchar * UTF8_MAXLEN_UCLC) + 1);
3518 (void)SvPOK_only(TARG);
3519 d = (U8*)SvPVX(TARG);
3522 UV uv = toLOWER_utf8(s, tmpbuf, &ulen);
3523 #define GREEK_CAPITAL_LETTER_SIGMA 0x03A3 /* Unicode */
3524 if (uv == GREEK_CAPITAL_LETTER_SIGMA) {
3526 * Now if the sigma is NOT followed by
3527 * /$ignorable_sequence$cased_letter/;
3528 * and it IS preceded by
3529 * /$cased_letter$ignorable_sequence/;
3530 * where $ignorable_sequence is
3531 * [\x{2010}\x{AD}\p{Mn}]*
3532 * and $cased_letter is
3533 * [\p{Ll}\p{Lo}\p{Lt}]
3534 * then it should be mapped to 0x03C2,
3535 * (GREEK SMALL LETTER FINAL SIGMA),
3536 * instead of staying 0x03A3.
3537 * See lib/unicore/SpecCase.txt.
3540 Copy(tmpbuf, d, ulen, U8);
3546 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
3551 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
3553 SvUTF8_off(TARG); /* decontaminate */
3559 s = (U8*)SvPV_force(sv, len);
3561 register U8 *send = s + len;
3563 if (IN_LOCALE_RUNTIME) {
3566 for (; s < send; s++)
3567 *s = toLOWER_LC(*s);
3570 for (; s < send; s++)
3585 register char *s = SvPV(sv,len);
3588 SvUTF8_off(TARG); /* decontaminate */
3590 (void)SvUPGRADE(TARG, SVt_PV);
3591 SvGROW(TARG, (len * 2) + 1);
3595 if (UTF8_IS_CONTINUED(*s)) {
3596 STRLEN ulen = UTF8SKIP(s);
3620 SvCUR_set(TARG, d - SvPVX(TARG));
3621 (void)SvPOK_only_UTF8(TARG);
3624 sv_setpvn(TARG, s, len);
3626 if (SvSMAGICAL(TARG))
3635 dSP; dMARK; dORIGMARK;
3637 register AV* av = (AV*)POPs;
3638 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3639 I32 arybase = PL_curcop->cop_arybase;
3642 if (SvTYPE(av) == SVt_PVAV) {
3643 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
3645 for (svp = MARK + 1; svp <= SP; svp++) {
3650 if (max > AvMAX(av))
3653 while (++MARK <= SP) {
3654 elem = SvIVx(*MARK);
3658 svp = av_fetch(av, elem, lval);
3660 if (!svp || *svp == &PL_sv_undef)
3661 DIE(aTHX_ PL_no_aelem, elem);
3662 if (PL_op->op_private & OPpLVAL_INTRO)
3663 save_aelem(av, elem, svp);
3665 *MARK = svp ? *svp : &PL_sv_undef;
3668 if (GIMME != G_ARRAY) {
3676 /* Associative arrays. */
3681 HV *hash = (HV*)POPs;
3683 I32 gimme = GIMME_V;
3684 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
3687 /* might clobber stack_sp */
3688 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
3693 SV* sv = hv_iterkeysv(entry);
3694 PUSHs(sv); /* won't clobber stack_sp */
3695 if (gimme == G_ARRAY) {
3698 /* might clobber stack_sp */
3700 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
3705 else if (gimme == G_SCALAR)
3724 I32 gimme = GIMME_V;
3725 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
3729 if (PL_op->op_private & OPpSLICE) {
3733 hvtype = SvTYPE(hv);
3734 if (hvtype == SVt_PVHV) { /* hash element */
3735 while (++MARK <= SP) {
3736 sv = hv_delete_ent(hv, *MARK, discard, 0);
3737 *MARK = sv ? sv : &PL_sv_undef;
3740 else if (hvtype == SVt_PVAV) {
3741 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3742 while (++MARK <= SP) {
3743 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
3744 *MARK = sv ? sv : &PL_sv_undef;
3747 else { /* pseudo-hash element */
3748 while (++MARK <= SP) {
3749 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
3750 *MARK = sv ? sv : &PL_sv_undef;
3755 DIE(aTHX_ "Not a HASH reference");
3758 else if (gimme == G_SCALAR) {
3767 if (SvTYPE(hv) == SVt_PVHV)
3768 sv = hv_delete_ent(hv, keysv, discard, 0);
3769 else if (SvTYPE(hv) == SVt_PVAV) {
3770 if (PL_op->op_flags & OPf_SPECIAL)
3771 sv = av_delete((AV*)hv, SvIV(keysv), discard);
3773 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
3776 DIE(aTHX_ "Not a HASH reference");
3791 if (PL_op->op_private & OPpEXISTS_SUB) {
3795 cv = sv_2cv(sv, &hv, &gv, FALSE);
3798 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
3804 if (SvTYPE(hv) == SVt_PVHV) {
3805 if (hv_exists_ent(hv, tmpsv, 0))
3808 else if (SvTYPE(hv) == SVt_PVAV) {
3809 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
3810 if (av_exists((AV*)hv, SvIV(tmpsv)))
3813 else if (avhv_exists_ent((AV*)hv, tmpsv, 0)) /* pseudo-hash element */
3817 DIE(aTHX_ "Not a HASH reference");
3824 dSP; dMARK; dORIGMARK;
3825 register HV *hv = (HV*)POPs;
3826 register I32 lval = (PL_op->op_flags & OPf_MOD || LVRET);
3827 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
3828 bool localizing = PL_op->op_private & OPpLVAL_INTRO ? TRUE : FALSE;
3829 bool other_magic = FALSE;
3835 other_magic = mg_find((SV*)hv, PERL_MAGIC_env) ||
3836 ((mg = mg_find((SV*)hv, PERL_MAGIC_tied))
3837 /* Try to preserve the existenceness of a tied hash
3838 * element by using EXISTS and DELETE if possible.
3839 * Fallback to FETCH and STORE otherwise */
3840 && (stash = SvSTASH(SvRV(SvTIED_obj((SV*)hv, mg))))
3841 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE)
3842 && gv_fetchmethod_autoload(stash, "DELETE", TRUE));
3845 if (!realhv && localizing)
3846 DIE(aTHX_ "Can't localize pseudo-hash element");
3848 if (realhv || SvTYPE(hv) == SVt_PVAV) {
3849 while (++MARK <= SP) {
3852 bool preeminent = FALSE;
3855 preeminent = SvRMAGICAL(hv) && !other_magic ? 1 :
3856 realhv ? hv_exists_ent(hv, keysv, 0)
3857 : avhv_exists_ent((AV*)hv, keysv, 0);
3861 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
3862 svp = he ? &HeVAL(he) : 0;
3865 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
3868 if (!svp || *svp == &PL_sv_undef) {
3870 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
3874 save_helem(hv, keysv, svp);
3877 char *key = SvPV(keysv, keylen);
3878 SAVEDELETE(hv, savepvn(key,keylen), keylen);
3882 *MARK = svp ? *svp : &PL_sv_undef;
3885 if (GIMME != G_ARRAY) {
3893 /* List operators. */
3898 if (GIMME != G_ARRAY) {
3900 *MARK = *SP; /* unwanted list, return last item */
3902 *MARK = &PL_sv_undef;
3911 SV **lastrelem = PL_stack_sp;
3912 SV **lastlelem = PL_stack_base + POPMARK;
3913 SV **firstlelem = PL_stack_base + POPMARK + 1;
3914 register SV **firstrelem = lastlelem + 1;
3915 I32 arybase = PL_curcop->cop_arybase;
3916 I32 lval = PL_op->op_flags & OPf_MOD;
3917 I32 is_something_there = lval;
3919 register I32 max = lastrelem - lastlelem;
3920 register SV **lelem;
3923 if (GIMME != G_ARRAY) {
3924 ix = SvIVx(*lastlelem);
3929 if (ix < 0 || ix >= max)
3930 *firstlelem = &PL_sv_undef;
3932 *firstlelem = firstrelem[ix];
3938 SP = firstlelem - 1;
3942 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
3948 if (ix < 0 || ix >= max)
3949 *lelem = &PL_sv_undef;
3951 is_something_there = TRUE;
3952 if (!(*lelem = firstrelem[ix]))
3953 *lelem = &PL_sv_undef;
3956 if (is_something_there)
3959 SP = firstlelem - 1;
3965 dSP; dMARK; dORIGMARK;
3966 I32 items = SP - MARK;
3967 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
3968 SP = ORIGMARK; /* av_make() might realloc stack_sp */
3975 dSP; dMARK; dORIGMARK;
3976 HV* hv = (HV*)sv_2mortal((SV*)newHV());
3980 SV *val = NEWSV(46, 0);
3982 sv_setsv(val, *++MARK);
3983 else if (ckWARN(WARN_MISC))
3984 Perl_warner(aTHX_ packWARN(WARN_MISC), "Odd number of elements in anonymous hash");
3985 (void)hv_store_ent(hv,key,val,0);
3994 dSP; dMARK; dORIGMARK;
3995 register AV *ary = (AV*)*++MARK;
3999 register I32 offset;
4000 register I32 length;
4007 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4008 *MARK-- = SvTIED_obj((SV*)ary, mg);
4012 call_method("SPLICE",GIMME_V);
4021 offset = i = SvIVx(*MARK);
4023 offset += AvFILLp(ary) + 1;
4025 offset -= PL_curcop->cop_arybase;
4027 DIE(aTHX_ PL_no_aelem, i);
4029 length = SvIVx(*MARK++);
4031 length += AvFILLp(ary) - offset + 1;
4037 length = AvMAX(ary) + 1; /* close enough to infinity */
4041 length = AvMAX(ary) + 1;
4043 if (offset > AvFILLp(ary) + 1) {
4044 if (ckWARN(WARN_MISC))
4045 Perl_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
4046 offset = AvFILLp(ary) + 1;
4048 after = AvFILLp(ary) + 1 - (offset + length);
4049 if (after < 0) { /* not that much array */
4050 length += after; /* offset+length now in array */
4056 /* At this point, MARK .. SP-1 is our new LIST */
4059 diff = newlen - length;
4060 if (newlen && !AvREAL(ary) && AvREIFY(ary))
4063 if (diff < 0) { /* shrinking the area */
4065 New(451, tmparyval, newlen, SV*); /* so remember insertion */
4066 Copy(MARK, tmparyval, newlen, SV*);
4069 MARK = ORIGMARK + 1;
4070 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4071 MEXTEND(MARK, length);
4072 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
4074 EXTEND_MORTAL(length);
4075 for (i = length, dst = MARK; i; i--) {
4076 sv_2mortal(*dst); /* free them eventualy */
4083 *MARK = AvARRAY(ary)[offset+length-1];
4086 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
4087 SvREFCNT_dec(*dst++); /* free them now */
4090 AvFILLp(ary) += diff;
4092 /* pull up or down? */
4094 if (offset < after) { /* easier to pull up */
4095 if (offset) { /* esp. if nothing to pull */
4096 src = &AvARRAY(ary)[offset-1];
4097 dst = src - diff; /* diff is negative */
4098 for (i = offset; i > 0; i--) /* can't trust Copy */
4102 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
4106 if (after) { /* anything to pull down? */
4107 src = AvARRAY(ary) + offset + length;
4108 dst = src + diff; /* diff is negative */
4109 Move(src, dst, after, SV*);
4111 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
4112 /* avoid later double free */
4116 dst[--i] = &PL_sv_undef;
4119 for (src = tmparyval, dst = AvARRAY(ary) + offset;
4121 *dst = NEWSV(46, 0);
4122 sv_setsv(*dst++, *src++);
4124 Safefree(tmparyval);
4127 else { /* no, expanding (or same) */
4129 New(452, tmparyval, length, SV*); /* so remember deletion */
4130 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
4133 if (diff > 0) { /* expanding */
4135 /* push up or down? */
4137 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
4141 Move(src, dst, offset, SV*);
4143 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
4145 AvFILLp(ary) += diff;
4148 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
4149 av_extend(ary, AvFILLp(ary) + diff);
4150 AvFILLp(ary) += diff;
4153 dst = AvARRAY(ary) + AvFILLp(ary);
4155 for (i = after; i; i--) {
4162 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
4163 *dst = NEWSV(46, 0);
4164 sv_setsv(*dst++, *src++);
4166 MARK = ORIGMARK + 1;
4167 if (GIMME == G_ARRAY) { /* copy return vals to stack */
4169 Copy(tmparyval, MARK, length, SV*);
4171 EXTEND_MORTAL(length);
4172 for (i = length, dst = MARK; i; i--) {
4173 sv_2mortal(*dst); /* free them eventualy */
4177 Safefree(tmparyval);
4181 else if (length--) {
4182 *MARK = tmparyval[length];
4185 while (length-- > 0)
4186 SvREFCNT_dec(tmparyval[length]);
4188 Safefree(tmparyval);
4191 *MARK = &PL_sv_undef;
4199 dSP; dMARK; dORIGMARK; dTARGET;
4200 register AV *ary = (AV*)*++MARK;
4201 register SV *sv = &PL_sv_undef;
4204 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4205 *MARK-- = SvTIED_obj((SV*)ary, mg);
4209 call_method("PUSH",G_SCALAR|G_DISCARD);
4214 /* Why no pre-extend of ary here ? */
4215 for (++MARK; MARK <= SP; MARK++) {
4218 sv_setsv(sv, *MARK);
4223 PUSHi( AvFILL(ary) + 1 );
4231 SV *sv = av_pop(av);
4233 (void)sv_2mortal(sv);
4242 SV *sv = av_shift(av);
4247 (void)sv_2mortal(sv);
4254 dSP; dMARK; dORIGMARK; dTARGET;
4255 register AV *ary = (AV*)*++MARK;
4260 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4261 *MARK-- = SvTIED_obj((SV*)ary, mg);
4265 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
4270 av_unshift(ary, SP - MARK);
4273 sv_setsv(sv, *++MARK);
4274 (void)av_store(ary, i++, sv);
4278 PUSHi( AvFILL(ary) + 1 );
4288 if (GIMME == G_ARRAY) {
4295 /* safe as long as stack cannot get extended in the above */
4300 register char *down;
4305 SvUTF8_off(TARG); /* decontaminate */
4307 do_join(TARG, &PL_sv_no, MARK, SP);
4309 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
4310 up = SvPV_force(TARG, len);
4312 if (DO_UTF8(TARG)) { /* first reverse each character */
4313 U8* s = (U8*)SvPVX(TARG);
4314 U8* send = (U8*)(s + len);
4316 if (UTF8_IS_INVARIANT(*s)) {
4321 if (!utf8_to_uvchr(s, 0))
4325 down = (char*)(s - 1);
4326 /* reverse this character */
4330 *down-- = (char)tmp;
4336 down = SvPVX(TARG) + len - 1;
4340 *down-- = (char)tmp;
4342 (void)SvPOK_only_UTF8(TARG);
4354 register IV limit = POPi; /* note, negative is forever */
4357 register char *s = SvPV(sv, len);
4358 bool do_utf8 = DO_UTF8(sv);
4359 char *strend = s + len;
4361 register REGEXP *rx;
4365 STRLEN slen = do_utf8 ? utf8_length((U8*)s, (U8*)strend) : (strend - s);
4366 I32 maxiters = slen + 10;
4369 I32 origlimit = limit;
4372 AV *oldstack = PL_curstack;
4373 I32 gimme = GIMME_V;
4374 I32 oldsave = PL_savestack_ix;
4375 I32 make_mortal = 1;
4376 MAGIC *mg = (MAGIC *) NULL;
4379 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4384 DIE(aTHX_ "panic: pp_split");
4387 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4388 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4390 PL_reg_match_utf8 = do_utf8;
4392 if (pm->op_pmreplroot) {
4394 ary = GvAVn((GV*)PL_curpad[INT2PTR(PADOFFSET, pm->op_pmreplroot)]);
4396 ary = GvAVn((GV*)pm->op_pmreplroot);
4399 else if (gimme != G_ARRAY)
4400 #ifdef USE_5005THREADS
4401 ary = (AV*)PL_curpad[0];
4403 ary = GvAVn(PL_defgv);
4404 #endif /* USE_5005THREADS */
4407 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4413 if ((mg = SvTIED_mg((SV*)ary, PERL_MAGIC_tied))) {
4415 XPUSHs(SvTIED_obj((SV*)ary, mg));
4421 for (i = AvFILLp(ary); i >= 0; i--)
4422 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4424 /* temporarily switch stacks */
4425 SWITCHSTACK(PL_curstack, ary);
4429 base = SP - PL_stack_base;
4431 if (pm->op_pmflags & PMf_SKIPWHITE) {
4432 if (pm->op_pmflags & PMf_LOCALE) {
4433 while (isSPACE_LC(*s))
4441 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4442 SAVEINT(PL_multiline);
4443 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4447 limit = maxiters + 2;
4448 if (pm->op_pmflags & PMf_WHITE) {
4451 while (m < strend &&
4452 !((pm->op_pmflags & PMf_LOCALE)
4453 ? isSPACE_LC(*m) : isSPACE(*m)))
4458 dstr = NEWSV(30, m-s);
4459 sv_setpvn(dstr, s, m-s);
4463 (void)SvUTF8_on(dstr);
4467 while (s < strend &&
4468 ((pm->op_pmflags & PMf_LOCALE)
4469 ? isSPACE_LC(*s) : isSPACE(*s)))
4473 else if (strEQ("^", rx->precomp)) {
4476 for (m = s; m < strend && *m != '\n'; m++) ;
4480 dstr = NEWSV(30, m-s);
4481 sv_setpvn(dstr, s, m-s);
4485 (void)SvUTF8_on(dstr);
4490 else if (do_utf8 == ((rx->reganch & ROPT_UTF8) != 0) &&
4491 (rx->reganch & RE_USE_INTUIT) && !rx->nparens
4492 && (rx->reganch & ROPT_CHECK_ALL)
4493 && !(rx->reganch & ROPT_ANCH)) {
4494 int tail = (rx->reganch & RE_INTUIT_TAIL);
4495 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
4498 if (len == 1 && !(rx->reganch & ROPT_UTF8) && !tail) {
4500 char c = *SvPV(csv, n_a);
4503 for (m = s; m < strend && *m != c; m++) ;
4506 dstr = NEWSV(30, m-s);
4507 sv_setpvn(dstr, s, m-s);
4511 (void)SvUTF8_on(dstr);
4513 /* The rx->minlen is in characters but we want to step
4514 * s ahead by bytes. */
4516 s = (char*)utf8_hop((U8*)m, len);
4518 s = m + len; /* Fake \n at the end */
4523 while (s < strend && --limit &&
4524 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
4525 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
4528 dstr = NEWSV(31, m-s);
4529 sv_setpvn(dstr, s, m-s);
4533 (void)SvUTF8_on(dstr);
4535 /* The rx->minlen is in characters but we want to step
4536 * s ahead by bytes. */
4538 s = (char*)utf8_hop((U8*)m, len);
4540 s = m + len; /* Fake \n at the end */
4545 maxiters += slen * rx->nparens;
4546 while (s < strend && --limit
4547 /* && (!rx->check_substr
4548 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
4550 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
4551 1 /* minend */, sv, NULL, 0))
4553 TAINT_IF(RX_MATCH_TAINTED(rx));
4554 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
4559 strend = s + (strend - m);
4561 m = rx->startp[0] + orig;
4562 dstr = NEWSV(32, m-s);
4563 sv_setpvn(dstr, s, m-s);
4567 (void)SvUTF8_on(dstr);
4570 for (i = 1; i <= (I32)rx->nparens; i++) {
4571 s = rx->startp[i] + orig;
4572 m = rx->endp[i] + orig;
4574 /* japhy (07/27/01) -- the (m && s) test doesn't catch
4575 parens that didn't match -- they should be set to
4576 undef, not the empty string */
4577 if (m >= orig && s >= orig) {
4578 dstr = NEWSV(33, m-s);
4579 sv_setpvn(dstr, s, m-s);
4582 dstr = &PL_sv_undef; /* undef, not "" */
4586 (void)SvUTF8_on(dstr);
4590 s = rx->endp[0] + orig;
4594 LEAVE_SCOPE(oldsave);
4595 iters = (SP - PL_stack_base) - base;
4596 if (iters > maxiters)
4597 DIE(aTHX_ "Split loop");
4599 /* keep field after final delim? */
4600 if (s < strend || (iters && origlimit)) {
4601 STRLEN l = strend - s;
4602 dstr = NEWSV(34, l);
4603 sv_setpvn(dstr, s, l);
4607 (void)SvUTF8_on(dstr);
4611 else if (!origlimit) {
4612 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4618 SWITCHSTACK(ary, oldstack);
4619 if (SvSMAGICAL(ary)) {
4624 if (gimme == G_ARRAY) {
4626 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4634 call_method("PUSH",G_SCALAR|G_DISCARD);
4637 if (gimme == G_ARRAY) {
4638 /* EXTEND should not be needed - we just popped them */
4640 for (i=0; i < iters; i++) {
4641 SV **svp = av_fetch(ary, i, FALSE);
4642 PUSHs((svp) ? *svp : &PL_sv_undef);
4649 if (gimme == G_ARRAY)
4652 if (iters || !pm->op_pmreplroot) {
4660 #ifdef USE_5005THREADS
4662 Perl_unlock_condpair(pTHX_ void *svv)
4664 MAGIC *mg = mg_find((SV*)svv, PERL_MAGIC_mutex);
4667 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
4668 MUTEX_LOCK(MgMUTEXP(mg));
4669 if (MgOWNER(mg) != thr)
4670 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
4672 COND_SIGNAL(MgOWNERCONDP(mg));
4673 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": unlock 0x%"UVxf"\n",
4674 PTR2UV(thr), PTR2UV(svv)));
4675 MUTEX_UNLOCK(MgMUTEXP(mg));
4677 #endif /* USE_5005THREADS */
4685 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4686 || SvTYPE(retsv) == SVt_PVCV) {
4687 retsv = refto(retsv);
4695 #ifdef USE_5005THREADS
4698 if (PL_op->op_private & OPpLVAL_INTRO)
4699 PUSHs(*save_threadsv(PL_op->op_targ));
4701 PUSHs(THREADSV(PL_op->op_targ));
4704 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
4705 #endif /* USE_5005THREADS */