3 * Copyright (c) 1991-2000, 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
20 * The compiler on Concurrent CX/UX systems has a subtle bug which only
21 * seems to show up when compiling pp.c - it generates the wrong double
22 * precision constant value for (double)UV_MAX when used inline in the body
23 * of the code below, so this makes a static variable up front (which the
24 * compiler seems to get correct) and uses it in place of UV_MAX below.
26 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
27 static double UV_MAX_cxux = ((double)UV_MAX);
31 * Offset for integer pack/unpack.
33 * On architectures where I16 and I32 aren't really 16 and 32 bits,
34 * which for now are all Crays, pack and unpack have to play games.
38 * These values are required for portability of pack() output.
39 * If they're not right on your machine, then pack() and unpack()
40 * wouldn't work right anyway; you'll need to apply the Cray hack.
41 * (I'd like to check them with #if, but you can't use sizeof() in
42 * the preprocessor.) --???
45 The appropriate SHORTSIZE, INTSIZE, LONGSIZE, and LONGLONGSIZE
46 defines are now in config.h. --Andy Dougherty April 1998
51 /* CROSSCOMPILE and MULTIARCH are going to affect pp_pack() and pp_unpack().
54 #if SHORTSIZE != SIZE16 || LONGSIZE != SIZE32
55 # define PERL_NATINT_PACK
58 #if LONGSIZE > 4 && defined(_CRAY)
59 # if BYTEORDER == 0x12345678
60 # define OFF16(p) (char*)(p)
61 # define OFF32(p) (char*)(p)
63 # if BYTEORDER == 0x87654321
64 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
65 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
67 }}}} bad cray byte order
70 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
71 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
72 # define COPYNN(s,p,n) (*(p) = 0, Copy(s, (char *)(p), n, char))
73 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
74 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
76 # define COPY16(s,p) Copy(s, p, SIZE16, char)
77 # define COPY32(s,p) Copy(s, p, SIZE32, char)
78 # define COPYNN(s,p,n) Copy(s, (char *)(p), n, char)
79 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
80 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
83 /* variations on pp_null */
89 /* XXX I can't imagine anyone who doesn't have this actually _needs_
90 it, since pid_t is an integral type.
93 #ifdef NEED_GETPID_PROTO
94 extern Pid_t getpid (void);
100 if (GIMME_V == G_SCALAR)
101 XPUSHs(&PL_sv_undef);
115 if (PL_op->op_private & OPpLVAL_INTRO)
116 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
118 if (PL_op->op_flags & OPf_REF) {
122 if (GIMME == G_ARRAY) {
123 I32 maxarg = AvFILL((AV*)TARG) + 1;
125 if (SvMAGICAL(TARG)) {
127 for (i=0; i < maxarg; i++) {
128 SV **svp = av_fetch((AV*)TARG, i, FALSE);
129 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
133 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
138 SV* sv = sv_newmortal();
139 I32 maxarg = AvFILL((AV*)TARG) + 1;
140 sv_setiv(sv, maxarg);
152 if (PL_op->op_private & OPpLVAL_INTRO)
153 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
154 if (PL_op->op_flags & OPf_REF)
157 if (gimme == G_ARRAY) {
160 else if (gimme == G_SCALAR) {
161 SV* sv = sv_newmortal();
162 if (HvFILL((HV*)TARG))
163 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
164 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
174 DIE(aTHX_ "NOT IMPL LINE %d",__LINE__);
185 tryAMAGICunDEREF(to_gv);
188 if (SvTYPE(sv) == SVt_PVIO) {
189 GV *gv = (GV*) sv_newmortal();
190 gv_init(gv, 0, "", 0, 0);
191 GvIOp(gv) = (IO *)sv;
192 (void)SvREFCNT_inc(sv);
195 else if (SvTYPE(sv) != SVt_PVGV)
196 DIE(aTHX_ "Not a GLOB reference");
199 if (SvTYPE(sv) != SVt_PVGV) {
203 if (SvGMAGICAL(sv)) {
208 if (!SvOK(sv) && sv != &PL_sv_undef) {
209 /* If this is a 'my' scalar and flag is set then vivify
212 if (PL_op->op_private & OPpDEREF) {
215 if (cUNOP->op_targ) {
217 SV *namesv = PL_curpad[cUNOP->op_targ];
218 name = SvPV(namesv, len);
219 gv = (GV*)NEWSV(0,0);
220 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
223 name = CopSTASHPV(PL_curcop);
226 sv_upgrade(sv, SVt_RV);
232 if (PL_op->op_flags & OPf_REF ||
233 PL_op->op_private & HINT_STRICT_REFS)
234 DIE(aTHX_ PL_no_usym, "a symbol");
235 if (ckWARN(WARN_UNINITIALIZED))
240 if ((PL_op->op_flags & OPf_SPECIAL) &&
241 !(PL_op->op_flags & OPf_MOD))
243 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
245 && (!is_gv_magical(sym,len,0)
246 || !(sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV))))
252 if (PL_op->op_private & HINT_STRICT_REFS)
253 DIE(aTHX_ PL_no_symref, sym, "a symbol");
254 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
258 if (PL_op->op_private & OPpLVAL_INTRO)
259 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
270 tryAMAGICunDEREF(to_sv);
273 switch (SvTYPE(sv)) {
277 DIE(aTHX_ "Not a SCALAR reference");
285 if (SvTYPE(gv) != SVt_PVGV) {
286 if (SvGMAGICAL(sv)) {
292 if (PL_op->op_flags & OPf_REF ||
293 PL_op->op_private & HINT_STRICT_REFS)
294 DIE(aTHX_ PL_no_usym, "a SCALAR");
295 if (ckWARN(WARN_UNINITIALIZED))
300 if ((PL_op->op_flags & OPf_SPECIAL) &&
301 !(PL_op->op_flags & OPf_MOD))
303 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
305 && (!is_gv_magical(sym,len,0)
306 || !(gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV))))
312 if (PL_op->op_private & HINT_STRICT_REFS)
313 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
314 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
319 if (PL_op->op_flags & OPf_MOD) {
320 if (PL_op->op_private & OPpLVAL_INTRO)
321 sv = save_scalar((GV*)TOPs);
322 else if (PL_op->op_private & OPpDEREF)
323 vivify_ref(sv, PL_op->op_private & OPpDEREF);
333 SV *sv = AvARYLEN(av);
335 AvARYLEN(av) = sv = NEWSV(0,0);
336 sv_upgrade(sv, SVt_IV);
337 sv_magic(sv, (SV*)av, '#', Nullch, 0);
345 djSP; dTARGET; dPOPss;
347 if (PL_op->op_flags & OPf_MOD) {
348 if (SvTYPE(TARG) < SVt_PVLV) {
349 sv_upgrade(TARG, SVt_PVLV);
350 sv_magic(TARG, Nullsv, '.', Nullch, 0);
354 if (LvTARG(TARG) != sv) {
356 SvREFCNT_dec(LvTARG(TARG));
357 LvTARG(TARG) = SvREFCNT_inc(sv);
359 PUSHs(TARG); /* no SvSETMAGIC */
365 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
366 mg = mg_find(sv, 'g');
367 if (mg && mg->mg_len >= 0) {
371 PUSHi(i + PL_curcop->cop_arybase);
385 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
386 /* (But not in defined().) */
387 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
390 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
391 if ((PL_op->op_private & OPpLVAL_INTRO) && !CvLVALUE(cv))
392 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
395 cv = (CV*)&PL_sv_undef;
409 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
410 char *s = SvPVX(TOPs);
411 if (strnEQ(s, "CORE::", 6)) {
414 code = keyword(s + 6, SvCUR(TOPs) - 6);
415 if (code < 0) { /* Overridable. */
416 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
417 int i = 0, n = 0, seen_question = 0;
419 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
421 while (i < MAXO) { /* The slow way. */
422 if (strEQ(s + 6, PL_op_name[i])
423 || strEQ(s + 6, PL_op_desc[i]))
429 goto nonesuch; /* Should not happen... */
431 oa = PL_opargs[i] >> OASHIFT;
433 if (oa & OA_OPTIONAL) {
437 else if (n && str[0] == ';' && seen_question)
438 goto set; /* XXXX system, exec */
439 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
440 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
443 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
444 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
448 ret = sv_2mortal(newSVpvn(str, n - 1));
450 else if (code) /* Non-Overridable */
452 else { /* None such */
454 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
458 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
460 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
469 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
471 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
487 if (GIMME != G_ARRAY) {
491 *MARK = &PL_sv_undef;
492 *MARK = refto(*MARK);
496 EXTEND_MORTAL(SP - MARK);
498 *MARK = refto(*MARK);
503 S_refto(pTHX_ SV *sv)
507 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
510 if (!(sv = LvTARG(sv)))
513 (void)SvREFCNT_inc(sv);
515 else if (SvTYPE(sv) == SVt_PVAV) {
516 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
519 (void)SvREFCNT_inc(sv);
521 else if (SvPADTMP(sv))
525 (void)SvREFCNT_inc(sv);
528 sv_upgrade(rv, SVt_RV);
542 if (sv && SvGMAGICAL(sv))
545 if (!sv || !SvROK(sv))
549 pv = sv_reftype(sv,TRUE);
550 PUSHp(pv, strlen(pv));
560 stash = CopSTASH(PL_curcop);
566 if (ssv && !SvGMAGICAL(ssv) && !SvAMAGIC(ssv) && SvROK(ssv))
567 Perl_croak(aTHX_ "Attempt to bless into a reference");
569 if (ckWARN(WARN_MISC) && len == 0)
570 Perl_warner(aTHX_ WARN_MISC,
571 "Explicit blessing to '' (assuming package main)");
572 stash = gv_stashpvn(ptr, len, TRUE);
575 (void)sv_bless(TOPs, stash);
589 elem = SvPV(sv, n_a);
593 switch (elem ? *elem : '\0')
596 if (strEQ(elem, "ARRAY"))
597 tmpRef = (SV*)GvAV(gv);
600 if (strEQ(elem, "CODE"))
601 tmpRef = (SV*)GvCVu(gv);
604 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
605 tmpRef = (SV*)GvIOp(gv);
607 if (strEQ(elem, "FORMAT"))
608 tmpRef = (SV*)GvFORM(gv);
611 if (strEQ(elem, "GLOB"))
615 if (strEQ(elem, "HASH"))
616 tmpRef = (SV*)GvHV(gv);
619 if (strEQ(elem, "IO"))
620 tmpRef = (SV*)GvIOp(gv);
623 if (strEQ(elem, "NAME"))
624 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
627 if (strEQ(elem, "PACKAGE"))
628 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
631 if (strEQ(elem, "SCALAR"))
645 /* Pattern matching */
650 register unsigned char *s;
653 register I32 *sfirst;
657 if (sv == PL_lastscream) {
663 SvSCREAM_off(PL_lastscream);
664 SvREFCNT_dec(PL_lastscream);
666 PL_lastscream = SvREFCNT_inc(sv);
669 s = (unsigned char*)(SvPV(sv, len));
673 if (pos > PL_maxscream) {
674 if (PL_maxscream < 0) {
675 PL_maxscream = pos + 80;
676 New(301, PL_screamfirst, 256, I32);
677 New(302, PL_screamnext, PL_maxscream, I32);
680 PL_maxscream = pos + pos / 4;
681 Renew(PL_screamnext, PL_maxscream, I32);
685 sfirst = PL_screamfirst;
686 snext = PL_screamnext;
688 if (!sfirst || !snext)
689 DIE(aTHX_ "do_study: out of memory");
691 for (ch = 256; ch; --ch)
698 snext[pos] = sfirst[ch] - pos;
705 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
714 if (PL_op->op_flags & OPf_STACKED)
720 TARG = sv_newmortal();
725 /* Lvalue operators. */
737 djSP; dMARK; dTARGET;
747 SETi(do_chomp(TOPs));
753 djSP; dMARK; dTARGET;
754 register I32 count = 0;
757 count += do_chomp(POPs);
768 if (!sv || !SvANY(sv))
770 switch (SvTYPE(sv)) {
772 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
776 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
780 if (CvROOT(sv) || CvXSUB(sv))
797 if (!PL_op->op_private) {
806 if (SvTHINKFIRST(sv))
809 switch (SvTYPE(sv)) {
819 if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv))
820 Perl_warner(aTHX_ WARN_MISC, "Constant subroutine %s undefined",
821 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
825 /* let user-undef'd sub keep its identity */
826 GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
833 SvSetMagicSV(sv, &PL_sv_undef);
837 Newz(602, gp, 1, GP);
838 GvGP(sv) = gp_ref(gp);
839 GvSV(sv) = NEWSV(72,0);
840 GvLINE(sv) = CopLINE(PL_curcop);
846 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
849 SvPV_set(sv, Nullch);
862 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
863 DIE(aTHX_ PL_no_modify);
864 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
865 SvIVX(TOPs) != IV_MIN)
868 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
879 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
880 DIE(aTHX_ PL_no_modify);
881 sv_setsv(TARG, TOPs);
882 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
883 SvIVX(TOPs) != IV_MAX)
886 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
900 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
901 DIE(aTHX_ PL_no_modify);
902 sv_setsv(TARG, TOPs);
903 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
904 SvIVX(TOPs) != IV_MIN)
907 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
916 /* Ordinary operators. */
920 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
923 SETn( Perl_pow( left, right) );
930 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
933 SETn( left * right );
940 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
945 DIE(aTHX_ "Illegal division by zero");
947 /* insure that 20./5. == 4. */
950 if ((NV)I_V(left) == left &&
951 (NV)I_V(right) == right &&
952 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
956 value = left / right;
960 value = left / right;
969 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
979 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
981 right = (right_neg = (i < 0)) ? -i : i;
986 right_neg = dright < 0;
991 if (!use_double && SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
993 left = (left_neg = (i < 0)) ? -i : i;
1001 left_neg = dleft < 0;
1010 /* Somehow U_V is pessimized even if CASTFLAGS is 0 */
1012 # define CAST_D2UV(d) U_V(d)
1014 # define CAST_D2UV(d) ((UV)(d))
1016 /* Tried to do this only in the case DOUBLESIZE <= UV_SIZE,
1017 * or, in other words, precision of UV more than of NV.
1018 * But in fact the approach below turned out to be an
1019 * optimization - floor() may be slow */
1020 if (dright <= UV_MAX && dleft <= UV_MAX) {
1021 right = CAST_D2UV(dright);
1022 left = CAST_D2UV(dleft);
1027 /* Backward-compatibility clause: */
1028 dright = Perl_floor(dright + 0.5);
1029 dleft = Perl_floor(dleft + 0.5);
1032 DIE(aTHX_ "Illegal modulus zero");
1034 dans = Perl_fmod(dleft, dright);
1035 if ((left_neg != right_neg) && dans)
1036 dans = dright - dans;
1039 sv_setnv(TARG, dans);
1046 DIE(aTHX_ "Illegal modulus zero");
1049 if ((left_neg != right_neg) && ans)
1052 /* XXX may warn: unary minus operator applied to unsigned type */
1053 /* could change -foo to be (~foo)+1 instead */
1054 if (ans <= ~((UV)IV_MAX)+1)
1055 sv_setiv(TARG, ~ans+1);
1057 sv_setnv(TARG, -(NV)ans);
1060 sv_setuv(TARG, ans);
1069 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1071 register I32 count = POPi;
1072 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1074 I32 items = SP - MARK;
1077 max = items * count;
1086 repeatcpy((char*)(MARK + items), (char*)MARK,
1087 items * sizeof(SV*), count - 1);
1090 else if (count <= 0)
1093 else { /* Note: mark already snarfed by pp_list */
1096 bool isutf = DO_UTF8(tmpstr);
1098 SvSetSV(TARG, tmpstr);
1099 SvPV_force(TARG, len);
1104 SvGROW(TARG, (count * len) + 1);
1105 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1106 SvCUR(TARG) *= count;
1108 *SvEND(TARG) = '\0';
1111 (void)SvPOK_only_UTF8(TARG);
1113 (void)SvPOK_only(TARG);
1122 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1125 SETn( left - right );
1132 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1135 if (PL_op->op_private & HINT_INTEGER) {
1149 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1152 if (PL_op->op_private & HINT_INTEGER) {
1166 djSP; tryAMAGICbinSET(lt,0);
1169 SETs(boolSV(TOPn < value));
1176 djSP; tryAMAGICbinSET(gt,0);
1179 SETs(boolSV(TOPn > value));
1186 djSP; tryAMAGICbinSET(le,0);
1189 SETs(boolSV(TOPn <= value));
1196 djSP; tryAMAGICbinSET(ge,0);
1199 SETs(boolSV(TOPn >= value));
1206 djSP; tryAMAGICbinSET(ne,0);
1209 SETs(boolSV(TOPn != value));
1216 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1222 if (Perl_isnan(left) || Perl_isnan(right)) {
1226 value = (left > right) - (left < right);
1230 else if (left < right)
1232 else if (left > right)
1246 djSP; tryAMAGICbinSET(slt,0);
1249 int cmp = ((PL_op->op_private & OPpLOCALE)
1250 ? sv_cmp_locale(left, right)
1251 : sv_cmp(left, right));
1252 SETs(boolSV(cmp < 0));
1259 djSP; tryAMAGICbinSET(sgt,0);
1262 int cmp = ((PL_op->op_private & OPpLOCALE)
1263 ? sv_cmp_locale(left, right)
1264 : sv_cmp(left, right));
1265 SETs(boolSV(cmp > 0));
1272 djSP; tryAMAGICbinSET(sle,0);
1275 int cmp = ((PL_op->op_private & OPpLOCALE)
1276 ? sv_cmp_locale(left, right)
1277 : sv_cmp(left, right));
1278 SETs(boolSV(cmp <= 0));
1285 djSP; tryAMAGICbinSET(sge,0);
1288 int cmp = ((PL_op->op_private & OPpLOCALE)
1289 ? sv_cmp_locale(left, right)
1290 : sv_cmp(left, right));
1291 SETs(boolSV(cmp >= 0));
1298 djSP; tryAMAGICbinSET(seq,0);
1301 SETs(boolSV(sv_eq(left, right)));
1308 djSP; tryAMAGICbinSET(sne,0);
1311 SETs(boolSV(!sv_eq(left, right)));
1318 djSP; dTARGET; tryAMAGICbin(scmp,0);
1321 int cmp = ((PL_op->op_private & OPpLOCALE)
1322 ? sv_cmp_locale(left, right)
1323 : sv_cmp(left, right));
1331 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1334 if (SvNIOKp(left) || SvNIOKp(right)) {
1335 if (PL_op->op_private & HINT_INTEGER) {
1336 IV i = SvIV(left) & SvIV(right);
1340 UV u = SvUV(left) & SvUV(right);
1345 do_vop(PL_op->op_type, TARG, left, right);
1354 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1357 if (SvNIOKp(left) || SvNIOKp(right)) {
1358 if (PL_op->op_private & HINT_INTEGER) {
1359 IV i = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1363 UV u = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1368 do_vop(PL_op->op_type, TARG, left, right);
1377 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1380 if (SvNIOKp(left) || SvNIOKp(right)) {
1381 if (PL_op->op_private & HINT_INTEGER) {
1382 IV i = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1386 UV u = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1391 do_vop(PL_op->op_type, TARG, left, right);
1400 djSP; dTARGET; tryAMAGICun(neg);
1405 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv)) {
1407 if (SvIVX(sv) == IV_MIN) {
1408 SETi(SvIVX(sv)); /* special case: -((UV)IV_MAX+1) == IV_MIN */
1411 else if (SvUVX(sv) <= IV_MAX) {
1416 else if (SvIVX(sv) != IV_MIN) {
1423 else if (SvPOKp(sv)) {
1425 char *s = SvPV(sv, len);
1426 if (isIDFIRST(*s)) {
1427 sv_setpvn(TARG, "-", 1);
1430 else if (*s == '+' || *s == '-') {
1432 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1434 else if (DO_UTF8(sv) && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1435 sv_setpvn(TARG, "-", 1);
1439 sv_setnv(TARG, -SvNV(sv));
1450 djSP; tryAMAGICunSET(not);
1451 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1457 djSP; dTARGET; tryAMAGICun(compl);
1461 if (PL_op->op_private & HINT_INTEGER) {
1471 register char *tmps;
1472 register long *tmpl;
1477 tmps = SvPV_force(TARG, len);
1480 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1483 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1487 for ( ; anum > 0; anum--, tmps++)
1496 /* integer versions of some of the above */
1500 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1503 SETi( left * right );
1510 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1514 DIE(aTHX_ "Illegal division by zero");
1515 value = POPi / value;
1523 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1527 DIE(aTHX_ "Illegal modulus zero");
1528 SETi( left % right );
1535 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1538 SETi( left + right );
1545 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1548 SETi( left - right );
1555 djSP; tryAMAGICbinSET(lt,0);
1558 SETs(boolSV(left < right));
1565 djSP; tryAMAGICbinSET(gt,0);
1568 SETs(boolSV(left > right));
1575 djSP; tryAMAGICbinSET(le,0);
1578 SETs(boolSV(left <= right));
1585 djSP; tryAMAGICbinSET(ge,0);
1588 SETs(boolSV(left >= right));
1595 djSP; tryAMAGICbinSET(eq,0);
1598 SETs(boolSV(left == right));
1605 djSP; tryAMAGICbinSET(ne,0);
1608 SETs(boolSV(left != right));
1615 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1622 else if (left < right)
1633 djSP; dTARGET; tryAMAGICun(neg);
1638 /* High falutin' math. */
1642 djSP; dTARGET; tryAMAGICbin(atan2,0);
1645 SETn(Perl_atan2(left, right));
1652 djSP; dTARGET; tryAMAGICun(sin);
1656 value = Perl_sin(value);
1664 djSP; dTARGET; tryAMAGICun(cos);
1668 value = Perl_cos(value);
1674 /* Support Configure command-line overrides for rand() functions.
1675 After 5.005, perhaps we should replace this by Configure support
1676 for drand48(), random(), or rand(). For 5.005, though, maintain
1677 compatibility by calling rand() but allow the user to override it.
1678 See INSTALL for details. --Andy Dougherty 15 July 1998
1680 /* Now it's after 5.005, and Configure supports drand48() and random(),
1681 in addition to rand(). So the overrides should not be needed any more.
1682 --Jarkko Hietaniemi 27 September 1998
1685 #ifndef HAS_DRAND48_PROTO
1686 extern double drand48 (void);
1699 if (!PL_srand_called) {
1700 (void)seedDrand01((Rand_seed_t)seed());
1701 PL_srand_called = TRUE;
1716 (void)seedDrand01((Rand_seed_t)anum);
1717 PL_srand_called = TRUE;
1726 * This is really just a quick hack which grabs various garbage
1727 * values. It really should be a real hash algorithm which
1728 * spreads the effect of every input bit onto every output bit,
1729 * if someone who knows about such things would bother to write it.
1730 * Might be a good idea to add that function to CORE as well.
1731 * No numbers below come from careful analysis or anything here,
1732 * except they are primes and SEED_C1 > 1E6 to get a full-width
1733 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1734 * probably be bigger too.
1737 # define SEED_C1 1000003
1738 #define SEED_C4 73819
1740 # define SEED_C1 25747
1741 #define SEED_C4 20639
1745 #define SEED_C5 26107
1748 #ifndef PERL_NO_DEV_RANDOM
1753 # include <starlet.h>
1754 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1755 * in 100-ns units, typically incremented ever 10 ms. */
1756 unsigned int when[2];
1758 # ifdef HAS_GETTIMEOFDAY
1759 struct timeval when;
1765 /* This test is an escape hatch, this symbol isn't set by Configure. */
1766 #ifndef PERL_NO_DEV_RANDOM
1767 #ifndef PERL_RANDOM_DEVICE
1768 /* /dev/random isn't used by default because reads from it will block
1769 * if there isn't enough entropy available. You can compile with
1770 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1771 * is enough real entropy to fill the seed. */
1772 # define PERL_RANDOM_DEVICE "/dev/urandom"
1774 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1776 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1785 _ckvmssts(sys$gettim(when));
1786 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1788 # ifdef HAS_GETTIMEOFDAY
1789 gettimeofday(&when,(struct timezone *) 0);
1790 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1793 u = (U32)SEED_C1 * when;
1796 u += SEED_C3 * (U32)PerlProc_getpid();
1797 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
1798 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1799 u += SEED_C5 * (U32)PTR2UV(&when);
1806 djSP; dTARGET; tryAMAGICun(exp);
1810 value = Perl_exp(value);
1818 djSP; dTARGET; tryAMAGICun(log);
1823 RESTORE_NUMERIC_STANDARD();
1824 DIE(aTHX_ "Can't take log of %g", value);
1826 value = Perl_log(value);
1834 djSP; dTARGET; tryAMAGICun(sqrt);
1839 RESTORE_NUMERIC_STANDARD();
1840 DIE(aTHX_ "Can't take sqrt of %g", value);
1842 value = Perl_sqrt(value);
1855 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1861 (void)Perl_modf(value, &value);
1863 (void)Perl_modf(-value, &value);
1878 djSP; dTARGET; tryAMAGICun(abs);
1883 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1884 (iv = SvIVX(TOPs)) != IV_MIN) {
1906 argtype = 1; /* allow underscores */
1907 XPUSHn(scan_hex(tmps, 99, &argtype));
1920 while (*tmps && isSPACE(*tmps))
1924 argtype = 1; /* allow underscores */
1926 value = scan_hex(++tmps, 99, &argtype);
1927 else if (*tmps == 'b')
1928 value = scan_bin(++tmps, 99, &argtype);
1930 value = scan_oct(tmps, 99, &argtype);
1943 SETi(sv_len_utf8(sv));
1959 I32 lvalue = PL_op->op_flags & OPf_MOD;
1961 I32 arybase = PL_curcop->cop_arybase;
1965 SvTAINTED_off(TARG); /* decontaminate */
1966 SvUTF8_off(TARG); /* decontaminate */
1970 repl = SvPV(sv, repl_len);
1977 tmps = SvPV(sv, curlen);
1979 utfcurlen = sv_len_utf8(sv);
1980 if (utfcurlen == curlen)
1988 if (pos >= arybase) {
2006 else if (len >= 0) {
2008 if (rem > (I32)curlen)
2023 Perl_croak(aTHX_ "substr outside of string");
2024 if (ckWARN(WARN_SUBSTR))
2025 Perl_warner(aTHX_ WARN_SUBSTR, "substr outside of string");
2030 sv_pos_u2b(sv, &pos, &rem);
2032 sv_setpvn(TARG, tmps, rem);
2036 sv_insert(sv, pos, rem, repl, repl_len);
2037 else if (lvalue) { /* it's an lvalue! */
2038 if (!SvGMAGICAL(sv)) {
2042 if (ckWARN(WARN_SUBSTR))
2043 Perl_warner(aTHX_ WARN_SUBSTR,
2044 "Attempt to use reference as lvalue in substr");
2046 if (SvOK(sv)) /* is it defined ? */
2047 (void)SvPOK_only_UTF8(sv);
2049 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2052 if (SvTYPE(TARG) < SVt_PVLV) {
2053 sv_upgrade(TARG, SVt_PVLV);
2054 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
2058 if (LvTARG(TARG) != sv) {
2060 SvREFCNT_dec(LvTARG(TARG));
2061 LvTARG(TARG) = SvREFCNT_inc(sv);
2063 LvTARGOFF(TARG) = pos;
2064 LvTARGLEN(TARG) = rem;
2068 PUSHs(TARG); /* avoid SvSETMAGIC here */
2075 register I32 size = POPi;
2076 register I32 offset = POPi;
2077 register SV *src = POPs;
2078 I32 lvalue = PL_op->op_flags & OPf_MOD;
2080 SvTAINTED_off(TARG); /* decontaminate */
2081 if (lvalue) { /* it's an lvalue! */
2082 if (SvTYPE(TARG) < SVt_PVLV) {
2083 sv_upgrade(TARG, SVt_PVLV);
2084 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2087 if (LvTARG(TARG) != src) {
2089 SvREFCNT_dec(LvTARG(TARG));
2090 LvTARG(TARG) = SvREFCNT_inc(src);
2092 LvTARGOFF(TARG) = offset;
2093 LvTARGLEN(TARG) = size;
2096 sv_setuv(TARG, do_vecget(src, offset, size));
2111 I32 arybase = PL_curcop->cop_arybase;
2116 offset = POPi - arybase;
2119 tmps = SvPV(big, biglen);
2120 if (offset > 0 && DO_UTF8(big))
2121 sv_pos_u2b(big, &offset, 0);
2124 else if (offset > biglen)
2126 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2127 (unsigned char*)tmps + biglen, little, 0)))
2130 retval = tmps2 - tmps;
2131 if (retval > 0 && DO_UTF8(big))
2132 sv_pos_b2u(big, &retval);
2133 PUSHi(retval + arybase);
2148 I32 arybase = PL_curcop->cop_arybase;
2154 tmps2 = SvPV(little, llen);
2155 tmps = SvPV(big, blen);
2159 if (offset > 0 && DO_UTF8(big))
2160 sv_pos_u2b(big, &offset, 0);
2161 offset = offset - arybase + llen;
2165 else if (offset > blen)
2167 if (!(tmps2 = rninstr(tmps, tmps + offset,
2168 tmps2, tmps2 + llen)))
2171 retval = tmps2 - tmps;
2172 if (retval > 0 && DO_UTF8(big))
2173 sv_pos_b2u(big, &retval);
2174 PUSHi(retval + arybase);
2180 djSP; dMARK; dORIGMARK; dTARGET;
2181 do_sprintf(TARG, SP-MARK, MARK+1);
2182 TAINT_IF(SvTAINTED(TARG));
2194 U8 *tmps = (U8*)SvPVx(tmpsv,n_a);
2197 if ((*tmps & 0x80) && DO_UTF8(tmpsv))
2198 value = utf8_to_uv(tmps, &retlen);
2200 value = (UV)(*tmps & 255);
2211 (void)SvUPGRADE(TARG,SVt_PV);
2213 if ((value > 255 && !IN_BYTE) || (value & 0x80 && PL_hints & HINT_UTF8) ) {
2214 SvGROW(TARG, UTF8_MAXLEN+1);
2216 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2217 SvCUR_set(TARG, tmps - SvPVX(TARG));
2219 (void)SvPOK_only(TARG);
2230 (void)SvPOK_only(TARG);
2237 djSP; dTARGET; dPOPTOPssrl;
2240 char *tmps = SvPV(left, n_a);
2242 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2244 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2248 "The crypt() function is unimplemented due to excessive paranoia.");
2261 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2263 U8 tmpbuf[UTF8_MAXLEN];
2265 UV uv = utf8_to_uv(s, &ulen);
2267 if (PL_op->op_private & OPpLOCALE) {
2270 uv = toTITLE_LC_uni(uv);
2273 uv = toTITLE_utf8(s);
2275 tend = uv_to_utf8(tmpbuf, uv);
2277 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
2279 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2280 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2285 s = (U8*)SvPV_force(sv, slen);
2286 Copy(tmpbuf, s, ulen, U8);
2290 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2292 SvUTF8_off(TARG); /* decontaminate */
2297 s = (U8*)SvPV_force(sv, slen);
2299 if (PL_op->op_private & OPpLOCALE) {
2302 *s = toUPPER_LC(*s);
2320 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2322 U8 tmpbuf[UTF8_MAXLEN];
2324 UV uv = utf8_to_uv(s, &ulen);
2326 if (PL_op->op_private & OPpLOCALE) {
2329 uv = toLOWER_LC_uni(uv);
2332 uv = toLOWER_utf8(s);
2334 tend = uv_to_utf8(tmpbuf, uv);
2336 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
2338 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2339 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2344 s = (U8*)SvPV_force(sv, slen);
2345 Copy(tmpbuf, s, ulen, U8);
2349 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2351 SvUTF8_off(TARG); /* decontaminate */
2356 s = (U8*)SvPV_force(sv, slen);
2358 if (PL_op->op_private & OPpLOCALE) {
2361 *s = toLOWER_LC(*s);
2385 s = (U8*)SvPV(sv,len);
2387 SvUTF8_off(TARG); /* decontaminate */
2388 sv_setpvn(TARG, "", 0);
2392 (void)SvUPGRADE(TARG, SVt_PV);
2393 SvGROW(TARG, (len * 2) + 1);
2394 (void)SvPOK_only(TARG);
2395 d = (U8*)SvPVX(TARG);
2397 if (PL_op->op_private & OPpLOCALE) {
2401 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2407 d = uv_to_utf8(d, toUPPER_utf8( s ));
2413 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2418 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2420 SvUTF8_off(TARG); /* decontaminate */
2425 s = (U8*)SvPV_force(sv, len);
2427 register U8 *send = s + len;
2429 if (PL_op->op_private & OPpLOCALE) {
2432 for (; s < send; s++)
2433 *s = toUPPER_LC(*s);
2436 for (; s < send; s++)
2459 s = (U8*)SvPV(sv,len);
2461 SvUTF8_off(TARG); /* decontaminate */
2462 sv_setpvn(TARG, "", 0);
2466 (void)SvUPGRADE(TARG, SVt_PV);
2467 SvGROW(TARG, (len * 2) + 1);
2468 (void)SvPOK_only(TARG);
2469 d = (U8*)SvPVX(TARG);
2471 if (PL_op->op_private & OPpLOCALE) {
2475 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2481 d = uv_to_utf8(d, toLOWER_utf8(s));
2487 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2492 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2494 SvUTF8_off(TARG); /* decontaminate */
2500 s = (U8*)SvPV_force(sv, len);
2502 register U8 *send = s + len;
2504 if (PL_op->op_private & OPpLOCALE) {
2507 for (; s < send; s++)
2508 *s = toLOWER_LC(*s);
2511 for (; s < send; s++)
2526 register char *s = SvPV(sv,len);
2529 SvUTF8_off(TARG); /* decontaminate */
2531 (void)SvUPGRADE(TARG, SVt_PV);
2532 SvGROW(TARG, (len * 2) + 1);
2537 STRLEN ulen = UTF8SKIP(s);
2561 SvCUR_set(TARG, d - SvPVX(TARG));
2562 (void)SvPOK_only_UTF8(TARG);
2565 sv_setpvn(TARG, s, len);
2567 if (SvSMAGICAL(TARG))
2576 djSP; dMARK; dORIGMARK;
2578 register AV* av = (AV*)POPs;
2579 register I32 lval = PL_op->op_flags & OPf_MOD;
2580 I32 arybase = PL_curcop->cop_arybase;
2583 if (SvTYPE(av) == SVt_PVAV) {
2584 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2586 for (svp = MARK + 1; svp <= SP; svp++) {
2591 if (max > AvMAX(av))
2594 while (++MARK <= SP) {
2595 elem = SvIVx(*MARK);
2599 svp = av_fetch(av, elem, lval);
2601 if (!svp || *svp == &PL_sv_undef)
2602 DIE(aTHX_ PL_no_aelem, elem);
2603 if (PL_op->op_private & OPpLVAL_INTRO)
2604 save_aelem(av, elem, svp);
2606 *MARK = svp ? *svp : &PL_sv_undef;
2609 if (GIMME != G_ARRAY) {
2617 /* Associative arrays. */
2622 HV *hash = (HV*)POPs;
2624 I32 gimme = GIMME_V;
2625 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2628 /* might clobber stack_sp */
2629 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2634 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2635 if (gimme == G_ARRAY) {
2638 /* might clobber stack_sp */
2640 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
2645 else if (gimme == G_SCALAR)
2664 I32 gimme = GIMME_V;
2665 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2669 if (PL_op->op_private & OPpSLICE) {
2673 hvtype = SvTYPE(hv);
2674 if (hvtype == SVt_PVHV) { /* hash element */
2675 while (++MARK <= SP) {
2676 sv = hv_delete_ent(hv, *MARK, discard, 0);
2677 *MARK = sv ? sv : &PL_sv_undef;
2680 else if (hvtype == SVt_PVAV) {
2681 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
2682 while (++MARK <= SP) {
2683 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
2684 *MARK = sv ? sv : &PL_sv_undef;
2687 else { /* pseudo-hash element */
2688 while (++MARK <= SP) {
2689 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2690 *MARK = sv ? sv : &PL_sv_undef;
2695 DIE(aTHX_ "Not a HASH reference");
2698 else if (gimme == G_SCALAR) {
2707 if (SvTYPE(hv) == SVt_PVHV)
2708 sv = hv_delete_ent(hv, keysv, discard, 0);
2709 else if (SvTYPE(hv) == SVt_PVAV) {
2710 if (PL_op->op_flags & OPf_SPECIAL)
2711 sv = av_delete((AV*)hv, SvIV(keysv), discard);
2713 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2716 DIE(aTHX_ "Not a HASH reference");
2731 if (PL_op->op_private & OPpEXISTS_SUB) {
2735 cv = sv_2cv(sv, &hv, &gv, FALSE);
2738 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
2744 if (SvTYPE(hv) == SVt_PVHV) {
2745 if (hv_exists_ent(hv, tmpsv, 0))
2748 else if (SvTYPE(hv) == SVt_PVAV) {
2749 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
2750 if (av_exists((AV*)hv, SvIV(tmpsv)))
2753 else if (avhv_exists_ent((AV*)hv, tmpsv, 0)) /* pseudo-hash element */
2757 DIE(aTHX_ "Not a HASH reference");
2764 djSP; dMARK; dORIGMARK;
2765 register HV *hv = (HV*)POPs;
2766 register I32 lval = PL_op->op_flags & OPf_MOD;
2767 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2769 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2770 DIE(aTHX_ "Can't localize pseudo-hash element");
2772 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2773 while (++MARK <= SP) {
2777 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2778 svp = he ? &HeVAL(he) : 0;
2781 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2784 if (!svp || *svp == &PL_sv_undef) {
2786 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2788 if (PL_op->op_private & OPpLVAL_INTRO)
2789 save_helem(hv, keysv, svp);
2791 *MARK = svp ? *svp : &PL_sv_undef;
2794 if (GIMME != G_ARRAY) {
2802 /* List operators. */
2807 if (GIMME != G_ARRAY) {
2809 *MARK = *SP; /* unwanted list, return last item */
2811 *MARK = &PL_sv_undef;
2820 SV **lastrelem = PL_stack_sp;
2821 SV **lastlelem = PL_stack_base + POPMARK;
2822 SV **firstlelem = PL_stack_base + POPMARK + 1;
2823 register SV **firstrelem = lastlelem + 1;
2824 I32 arybase = PL_curcop->cop_arybase;
2825 I32 lval = PL_op->op_flags & OPf_MOD;
2826 I32 is_something_there = lval;
2828 register I32 max = lastrelem - lastlelem;
2829 register SV **lelem;
2832 if (GIMME != G_ARRAY) {
2833 ix = SvIVx(*lastlelem);
2838 if (ix < 0 || ix >= max)
2839 *firstlelem = &PL_sv_undef;
2841 *firstlelem = firstrelem[ix];
2847 SP = firstlelem - 1;
2851 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2857 if (ix < 0 || ix >= max)
2858 *lelem = &PL_sv_undef;
2860 is_something_there = TRUE;
2861 if (!(*lelem = firstrelem[ix]))
2862 *lelem = &PL_sv_undef;
2865 if (is_something_there)
2868 SP = firstlelem - 1;
2874 djSP; dMARK; dORIGMARK;
2875 I32 items = SP - MARK;
2876 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2877 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2884 djSP; dMARK; dORIGMARK;
2885 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2889 SV *val = NEWSV(46, 0);
2891 sv_setsv(val, *++MARK);
2892 else if (ckWARN(WARN_MISC))
2893 Perl_warner(aTHX_ WARN_MISC, "Odd number of elements in hash assignment");
2894 (void)hv_store_ent(hv,key,val,0);
2903 djSP; dMARK; dORIGMARK;
2904 register AV *ary = (AV*)*++MARK;
2908 register I32 offset;
2909 register I32 length;
2916 if ((mg = SvTIED_mg((SV*)ary, 'P'))) {
2917 *MARK-- = SvTIED_obj((SV*)ary, mg);
2921 call_method("SPLICE",GIMME_V);
2930 offset = i = SvIVx(*MARK);
2932 offset += AvFILLp(ary) + 1;
2934 offset -= PL_curcop->cop_arybase;
2936 DIE(aTHX_ PL_no_aelem, i);
2938 length = SvIVx(*MARK++);
2940 length += AvFILLp(ary) - offset + 1;
2946 length = AvMAX(ary) + 1; /* close enough to infinity */
2950 length = AvMAX(ary) + 1;
2952 if (offset > AvFILLp(ary) + 1)
2953 offset = AvFILLp(ary) + 1;
2954 after = AvFILLp(ary) + 1 - (offset + length);
2955 if (after < 0) { /* not that much array */
2956 length += after; /* offset+length now in array */
2962 /* At this point, MARK .. SP-1 is our new LIST */
2965 diff = newlen - length;
2966 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2969 if (diff < 0) { /* shrinking the area */
2971 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2972 Copy(MARK, tmparyval, newlen, SV*);
2975 MARK = ORIGMARK + 1;
2976 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2977 MEXTEND(MARK, length);
2978 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2980 EXTEND_MORTAL(length);
2981 for (i = length, dst = MARK; i; i--) {
2982 sv_2mortal(*dst); /* free them eventualy */
2989 *MARK = AvARRAY(ary)[offset+length-1];
2992 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2993 SvREFCNT_dec(*dst++); /* free them now */
2996 AvFILLp(ary) += diff;
2998 /* pull up or down? */
3000 if (offset < after) { /* easier to pull up */
3001 if (offset) { /* esp. if nothing to pull */
3002 src = &AvARRAY(ary)[offset-1];
3003 dst = src - diff; /* diff is negative */
3004 for (i = offset; i > 0; i--) /* can't trust Copy */
3008 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
3012 if (after) { /* anything to pull down? */
3013 src = AvARRAY(ary) + offset + length;
3014 dst = src + diff; /* diff is negative */
3015 Move(src, dst, after, SV*);
3017 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
3018 /* avoid later double free */
3022 dst[--i] = &PL_sv_undef;
3025 for (src = tmparyval, dst = AvARRAY(ary) + offset;
3027 *dst = NEWSV(46, 0);
3028 sv_setsv(*dst++, *src++);
3030 Safefree(tmparyval);
3033 else { /* no, expanding (or same) */
3035 New(452, tmparyval, length, SV*); /* so remember deletion */
3036 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
3039 if (diff > 0) { /* expanding */
3041 /* push up or down? */
3043 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
3047 Move(src, dst, offset, SV*);
3049 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
3051 AvFILLp(ary) += diff;
3054 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
3055 av_extend(ary, AvFILLp(ary) + diff);
3056 AvFILLp(ary) += diff;
3059 dst = AvARRAY(ary) + AvFILLp(ary);
3061 for (i = after; i; i--) {
3068 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
3069 *dst = NEWSV(46, 0);
3070 sv_setsv(*dst++, *src++);
3072 MARK = ORIGMARK + 1;
3073 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3075 Copy(tmparyval, MARK, length, SV*);
3077 EXTEND_MORTAL(length);
3078 for (i = length, dst = MARK; i; i--) {
3079 sv_2mortal(*dst); /* free them eventualy */
3083 Safefree(tmparyval);
3087 else if (length--) {
3088 *MARK = tmparyval[length];
3091 while (length-- > 0)
3092 SvREFCNT_dec(tmparyval[length]);
3094 Safefree(tmparyval);
3097 *MARK = &PL_sv_undef;
3105 djSP; dMARK; dORIGMARK; dTARGET;
3106 register AV *ary = (AV*)*++MARK;
3107 register SV *sv = &PL_sv_undef;
3110 if ((mg = SvTIED_mg((SV*)ary, 'P'))) {
3111 *MARK-- = SvTIED_obj((SV*)ary, mg);
3115 call_method("PUSH",G_SCALAR|G_DISCARD);
3120 /* Why no pre-extend of ary here ? */
3121 for (++MARK; MARK <= SP; MARK++) {
3124 sv_setsv(sv, *MARK);
3129 PUSHi( AvFILL(ary) + 1 );
3137 SV *sv = av_pop(av);
3139 (void)sv_2mortal(sv);
3148 SV *sv = av_shift(av);
3153 (void)sv_2mortal(sv);
3160 djSP; dMARK; dORIGMARK; dTARGET;
3161 register AV *ary = (AV*)*++MARK;
3166 if ((mg = SvTIED_mg((SV*)ary, 'P'))) {
3167 *MARK-- = SvTIED_obj((SV*)ary, mg);
3171 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3176 av_unshift(ary, SP - MARK);
3179 sv_setsv(sv, *++MARK);
3180 (void)av_store(ary, i++, sv);
3184 PUSHi( AvFILL(ary) + 1 );
3194 if (GIMME == G_ARRAY) {
3201 /* safe as long as stack cannot get extended in the above */
3206 register char *down;
3211 SvUTF8_off(TARG); /* decontaminate */
3213 do_join(TARG, &PL_sv_no, MARK, SP);
3215 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3216 up = SvPV_force(TARG, len);
3218 if (DO_UTF8(TARG)) { /* first reverse each character */
3219 U8* s = (U8*)SvPVX(TARG);
3220 U8* send = (U8*)(s + len);
3229 down = (char*)(s - 1);
3230 if (s > send || !((*down & 0xc0) == 0x80)) {
3231 if (ckWARN_d(WARN_UTF8))
3232 Perl_warner(aTHX_ WARN_UTF8,
3233 "Malformed UTF-8 character");
3245 down = SvPVX(TARG) + len - 1;
3251 (void)SvPOK_only_UTF8(TARG);
3260 S_mul128(pTHX_ SV *sv, U8 m)
3263 char *s = SvPV(sv, len);
3267 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3268 SV *tmpNew = newSVpvn("0000000000", 10);
3270 sv_catsv(tmpNew, sv);
3271 SvREFCNT_dec(sv); /* free old sv */
3276 while (!*t) /* trailing '\0'? */
3279 i = ((*t - '0') << 7) + m;
3280 *(t--) = '0' + (i % 10);
3286 /* Explosives and implosives. */
3288 #if 'I' == 73 && 'J' == 74
3289 /* On an ASCII/ISO kind of system */
3290 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3293 Some other sort of character set - use memchr() so we don't match
3296 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3303 I32 start_sp_offset = SP - PL_stack_base;
3304 I32 gimme = GIMME_V;
3308 register char *pat = SvPV(left, llen);
3309 register char *s = SvPV(right, rlen);
3310 char *strend = s + rlen;
3312 register char *patend = pat + llen;
3318 /* These must not be in registers: */
3335 register U32 culong;
3339 #ifdef PERL_NATINT_PACK
3340 int natint; /* native integer */
3341 int unatint; /* unsigned native integer */
3344 if (gimme != G_ARRAY) { /* arrange to do first one only */
3346 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3347 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3349 while (isDIGIT(*patend) || *patend == '*')
3355 while (pat < patend) {
3357 datumtype = *pat++ & 0xFF;
3358 #ifdef PERL_NATINT_PACK
3361 if (isSPACE(datumtype))
3363 if (datumtype == '#') {
3364 while (pat < patend && *pat != '\n')
3369 char *natstr = "sSiIlL";
3371 if (strchr(natstr, datumtype)) {
3372 #ifdef PERL_NATINT_PACK
3378 DIE(aTHX_ "'!' allowed only after types %s", natstr);
3383 else if (*pat == '*') {
3384 len = strend - strbeg; /* long enough */
3388 else if (isDIGIT(*pat)) {
3390 while (isDIGIT(*pat)) {
3391 len = (len * 10) + (*pat++ - '0');
3393 DIE(aTHX_ "Repeat count in unpack overflows");
3397 len = (datumtype != '@');
3401 DIE(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
3402 case ',': /* grandfather in commas but with a warning */
3403 if (commas++ == 0 && ckWARN(WARN_UNPACK))
3404 Perl_warner(aTHX_ WARN_UNPACK,
3405 "Invalid type in unpack: '%c'", (int)datumtype);
3408 if (len == 1 && pat[-1] != '1')
3417 if (len > strend - strbeg)
3418 DIE(aTHX_ "@ outside of string");
3422 if (len > s - strbeg)
3423 DIE(aTHX_ "X outside of string");
3427 if (len > strend - s)
3428 DIE(aTHX_ "x outside of string");
3432 if (start_sp_offset >= SP - PL_stack_base)
3433 DIE(aTHX_ "/ must follow a numeric type");
3436 pat++; /* ignore '*' for compatibility with pack */
3438 DIE(aTHX_ "/ cannot take a count" );
3445 if (len > strend - s)
3448 goto uchar_checksum;
3449 sv = NEWSV(35, len);
3450 sv_setpvn(sv, s, len);
3452 if (datumtype == 'A' || datumtype == 'Z') {
3453 aptr = s; /* borrow register */
3454 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3459 else { /* 'A' strips both nulls and spaces */
3460 s = SvPVX(sv) + len - 1;
3461 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3465 SvCUR_set(sv, s - SvPVX(sv));
3466 s = aptr; /* unborrow register */
3468 XPUSHs(sv_2mortal(sv));
3472 if (star || len > (strend - s) * 8)
3473 len = (strend - s) * 8;
3476 Newz(601, PL_bitcount, 256, char);
3477 for (bits = 1; bits < 256; bits++) {
3478 if (bits & 1) PL_bitcount[bits]++;
3479 if (bits & 2) PL_bitcount[bits]++;
3480 if (bits & 4) PL_bitcount[bits]++;
3481 if (bits & 8) PL_bitcount[bits]++;
3482 if (bits & 16) PL_bitcount[bits]++;
3483 if (bits & 32) PL_bitcount[bits]++;
3484 if (bits & 64) PL_bitcount[bits]++;
3485 if (bits & 128) PL_bitcount[bits]++;
3489 culong += PL_bitcount[*(unsigned char*)s++];
3494 if (datumtype == 'b') {
3496 if (bits & 1) culong++;
3502 if (bits & 128) culong++;
3509 sv = NEWSV(35, len + 1);
3513 if (datumtype == 'b') {
3515 for (len = 0; len < aint; len++) {
3516 if (len & 7) /*SUPPRESS 595*/
3520 *str++ = '0' + (bits & 1);
3525 for (len = 0; len < aint; len++) {
3530 *str++ = '0' + ((bits & 128) != 0);
3534 XPUSHs(sv_2mortal(sv));
3538 if (star || len > (strend - s) * 2)
3539 len = (strend - s) * 2;
3540 sv = NEWSV(35, len + 1);
3544 if (datumtype == 'h') {
3546 for (len = 0; len < aint; len++) {
3551 *str++ = PL_hexdigit[bits & 15];
3556 for (len = 0; len < aint; len++) {
3561 *str++ = PL_hexdigit[(bits >> 4) & 15];
3565 XPUSHs(sv_2mortal(sv));
3568 if (len > strend - s)
3573 if (aint >= 128) /* fake up signed chars */
3583 if (aint >= 128) /* fake up signed chars */
3586 sv_setiv(sv, (IV)aint);
3587 PUSHs(sv_2mortal(sv));
3592 if (len > strend - s)
3607 sv_setiv(sv, (IV)auint);
3608 PUSHs(sv_2mortal(sv));
3613 if (len > strend - s)
3616 while (len-- > 0 && s < strend) {
3617 auint = utf8_to_uv((U8*)s, &along);
3620 cdouble += (NV)auint;
3628 while (len-- > 0 && s < strend) {
3629 auint = utf8_to_uv((U8*)s, &along);
3632 sv_setuv(sv, (UV)auint);
3633 PUSHs(sv_2mortal(sv));
3638 #if SHORTSIZE == SIZE16
3639 along = (strend - s) / SIZE16;
3641 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3646 #if SHORTSIZE != SIZE16
3650 COPYNN(s, &ashort, sizeof(short));
3661 #if SHORTSIZE > SIZE16
3673 #if SHORTSIZE != SIZE16
3677 COPYNN(s, &ashort, sizeof(short));
3680 sv_setiv(sv, (IV)ashort);
3681 PUSHs(sv_2mortal(sv));
3689 #if SHORTSIZE > SIZE16
3695 sv_setiv(sv, (IV)ashort);
3696 PUSHs(sv_2mortal(sv));
3704 #if SHORTSIZE == SIZE16
3705 along = (strend - s) / SIZE16;
3707 unatint = natint && datumtype == 'S';
3708 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3713 #if SHORTSIZE != SIZE16
3715 unsigned short aushort;
3717 COPYNN(s, &aushort, sizeof(unsigned short));
3718 s += sizeof(unsigned short);
3726 COPY16(s, &aushort);
3729 if (datumtype == 'n')
3730 aushort = PerlSock_ntohs(aushort);
3733 if (datumtype == 'v')
3734 aushort = vtohs(aushort);
3743 #if SHORTSIZE != SIZE16
3745 unsigned short aushort;
3747 COPYNN(s, &aushort, sizeof(unsigned short));
3748 s += sizeof(unsigned short);
3750 sv_setiv(sv, (UV)aushort);
3751 PUSHs(sv_2mortal(sv));
3758 COPY16(s, &aushort);
3762 if (datumtype == 'n')
3763 aushort = PerlSock_ntohs(aushort);
3766 if (datumtype == 'v')
3767 aushort = vtohs(aushort);
3769 sv_setiv(sv, (UV)aushort);
3770 PUSHs(sv_2mortal(sv));
3776 along = (strend - s) / sizeof(int);
3781 Copy(s, &aint, 1, int);
3784 cdouble += (NV)aint;
3793 Copy(s, &aint, 1, int);
3797 /* Without the dummy below unpack("i", pack("i",-1))
3798 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3799 * cc with optimization turned on.
3801 * The bug was detected in
3802 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3803 * with optimization (-O4) turned on.
3804 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3805 * does not have this problem even with -O4.
3807 * This bug was reported as DECC_BUGS 1431
3808 * and tracked internally as GEM_BUGS 7775.
3810 * The bug is fixed in
3811 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3812 * UNIX V4.0F support: DEC C V5.9-006 or later
3813 * UNIX V4.0E support: DEC C V5.8-011 or later
3816 * See also few lines later for the same bug.
3819 sv_setiv(sv, (IV)aint) :
3821 sv_setiv(sv, (IV)aint);
3822 PUSHs(sv_2mortal(sv));
3827 along = (strend - s) / sizeof(unsigned int);
3832 Copy(s, &auint, 1, unsigned int);
3833 s += sizeof(unsigned int);
3835 cdouble += (NV)auint;
3844 Copy(s, &auint, 1, unsigned int);
3845 s += sizeof(unsigned int);
3848 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3849 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3850 * See details few lines earlier. */
3852 sv_setuv(sv, (UV)auint) :
3854 sv_setuv(sv, (UV)auint);
3855 PUSHs(sv_2mortal(sv));
3860 #if LONGSIZE == SIZE32
3861 along = (strend - s) / SIZE32;
3863 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3868 #if LONGSIZE != SIZE32
3872 COPYNN(s, &along, sizeof(long));
3875 cdouble += (NV)along;
3885 #if LONGSIZE > SIZE32
3886 if (along > 2147483647)
3887 along -= 4294967296;
3891 cdouble += (NV)along;
3900 #if LONGSIZE != SIZE32
3904 COPYNN(s, &along, sizeof(long));
3907 sv_setiv(sv, (IV)along);
3908 PUSHs(sv_2mortal(sv));
3916 #if LONGSIZE > SIZE32
3917 if (along > 2147483647)
3918 along -= 4294967296;
3922 sv_setiv(sv, (IV)along);
3923 PUSHs(sv_2mortal(sv));
3931 #if LONGSIZE == SIZE32
3932 along = (strend - s) / SIZE32;
3934 unatint = natint && datumtype == 'L';
3935 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3940 #if LONGSIZE != SIZE32
3942 unsigned long aulong;
3944 COPYNN(s, &aulong, sizeof(unsigned long));
3945 s += sizeof(unsigned long);
3947 cdouble += (NV)aulong;
3959 if (datumtype == 'N')
3960 aulong = PerlSock_ntohl(aulong);
3963 if (datumtype == 'V')
3964 aulong = vtohl(aulong);
3967 cdouble += (NV)aulong;
3976 #if LONGSIZE != SIZE32
3978 unsigned long aulong;
3980 COPYNN(s, &aulong, sizeof(unsigned long));
3981 s += sizeof(unsigned long);
3983 sv_setuv(sv, (UV)aulong);
3984 PUSHs(sv_2mortal(sv));
3994 if (datumtype == 'N')
3995 aulong = PerlSock_ntohl(aulong);
3998 if (datumtype == 'V')
3999 aulong = vtohl(aulong);
4002 sv_setuv(sv, (UV)aulong);
4003 PUSHs(sv_2mortal(sv));
4009 along = (strend - s) / sizeof(char*);
4015 if (sizeof(char*) > strend - s)
4018 Copy(s, &aptr, 1, char*);
4024 PUSHs(sv_2mortal(sv));
4034 while ((len > 0) && (s < strend)) {
4035 auv = (auv << 7) | (*s & 0x7f);
4036 if (!(*s++ & 0x80)) {
4040 PUSHs(sv_2mortal(sv));
4044 else if (++bytes >= sizeof(UV)) { /* promote to string */
4048 sv = Perl_newSVpvf(aTHX_ "%.*Vu", (int)TYPE_DIGITS(UV), auv);
4049 while (s < strend) {
4050 sv = mul128(sv, *s & 0x7f);
4051 if (!(*s++ & 0x80)) {
4060 PUSHs(sv_2mortal(sv));
4065 if ((s >= strend) && bytes)
4066 DIE(aTHX_ "Unterminated compressed integer");
4071 if (sizeof(char*) > strend - s)
4074 Copy(s, &aptr, 1, char*);
4079 sv_setpvn(sv, aptr, len);
4080 PUSHs(sv_2mortal(sv));
4084 along = (strend - s) / sizeof(Quad_t);
4090 if (s + sizeof(Quad_t) > strend)
4093 Copy(s, &aquad, 1, Quad_t);
4094 s += sizeof(Quad_t);
4097 if (aquad >= IV_MIN && aquad <= IV_MAX)
4098 sv_setiv(sv, (IV)aquad);
4100 sv_setnv(sv, (NV)aquad);
4101 PUSHs(sv_2mortal(sv));
4105 along = (strend - s) / sizeof(Quad_t);
4111 if (s + sizeof(Uquad_t) > strend)
4114 Copy(s, &auquad, 1, Uquad_t);
4115 s += sizeof(Uquad_t);
4118 if (auquad <= UV_MAX)
4119 sv_setuv(sv, (UV)auquad);
4121 sv_setnv(sv, (NV)auquad);
4122 PUSHs(sv_2mortal(sv));
4126 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4129 along = (strend - s) / sizeof(float);
4134 Copy(s, &afloat, 1, float);
4143 Copy(s, &afloat, 1, float);
4146 sv_setnv(sv, (NV)afloat);
4147 PUSHs(sv_2mortal(sv));
4153 along = (strend - s) / sizeof(double);
4158 Copy(s, &adouble, 1, double);
4159 s += sizeof(double);
4167 Copy(s, &adouble, 1, double);
4168 s += sizeof(double);
4170 sv_setnv(sv, (NV)adouble);
4171 PUSHs(sv_2mortal(sv));
4177 * Initialise the decode mapping. By using a table driven
4178 * algorithm, the code will be character-set independent
4179 * (and just as fast as doing character arithmetic)
4181 if (PL_uudmap['M'] == 0) {
4184 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4185 PL_uudmap[(U8)PL_uuemap[i]] = i;
4187 * Because ' ' and '`' map to the same value,
4188 * we need to decode them both the same.
4193 along = (strend - s) * 3 / 4;
4194 sv = NEWSV(42, along);
4197 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4202 len = PL_uudmap[*(U8*)s++] & 077;
4204 if (s < strend && ISUUCHAR(*s))
4205 a = PL_uudmap[*(U8*)s++] & 077;
4208 if (s < strend && ISUUCHAR(*s))
4209 b = PL_uudmap[*(U8*)s++] & 077;
4212 if (s < strend && ISUUCHAR(*s))
4213 c = PL_uudmap[*(U8*)s++] & 077;
4216 if (s < strend && ISUUCHAR(*s))
4217 d = PL_uudmap[*(U8*)s++] & 077;
4220 hunk[0] = (a << 2) | (b >> 4);
4221 hunk[1] = (b << 4) | (c >> 2);
4222 hunk[2] = (c << 6) | d;
4223 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4228 else if (s[1] == '\n') /* possible checksum byte */
4231 XPUSHs(sv_2mortal(sv));
4236 if (strchr("fFdD", datumtype) ||
4237 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4241 while (checksum >= 16) {
4245 while (checksum >= 4) {
4251 along = (1 << checksum) - 1;
4252 while (cdouble < 0.0)
4254 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
4255 sv_setnv(sv, cdouble);
4258 if (checksum < 32) {
4259 aulong = (1 << checksum) - 1;
4262 sv_setuv(sv, (UV)culong);
4264 XPUSHs(sv_2mortal(sv));
4268 if (SP - PL_stack_base == start_sp_offset && gimme == G_SCALAR)
4269 PUSHs(&PL_sv_undef);
4274 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
4278 *hunk = PL_uuemap[len];
4279 sv_catpvn(sv, hunk, 1);
4282 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4283 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4284 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4285 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4286 sv_catpvn(sv, hunk, 4);
4291 char r = (len > 1 ? s[1] : '\0');
4292 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4293 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4294 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4295 hunk[3] = PL_uuemap[0];
4296 sv_catpvn(sv, hunk, 4);
4298 sv_catpvn(sv, "\n", 1);
4302 S_is_an_int(pTHX_ char *s, STRLEN l)
4305 SV *result = newSVpvn(s, l);
4306 char *result_c = SvPV(result, n_a); /* convenience */
4307 char *out = result_c;
4317 SvREFCNT_dec(result);
4340 SvREFCNT_dec(result);
4346 SvCUR_set(result, out - result_c);
4350 /* pnum must be '\0' terminated */
4352 S_div128(pTHX_ SV *pnum, bool *done)
4355 char *s = SvPV(pnum, len);
4364 i = m * 10 + (*t - '0');
4366 r = (i >> 7); /* r < 10 */
4373 SvCUR_set(pnum, (STRLEN) (t - s));
4380 djSP; dMARK; dORIGMARK; dTARGET;
4381 register SV *cat = TARG;
4384 register char *pat = SvPVx(*++MARK, fromlen);
4386 register char *patend = pat + fromlen;
4391 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4392 static char *space10 = " ";
4394 /* These must not be in registers: */
4409 #ifdef PERL_NATINT_PACK
4410 int natint; /* native integer */
4415 sv_setpvn(cat, "", 0);
4417 while (pat < patend) {
4418 SV *lengthcode = Nullsv;
4419 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *MARK++ : &PL_sv_no)
4420 datumtype = *pat++ & 0xFF;
4421 #ifdef PERL_NATINT_PACK
4424 if (isSPACE(datumtype)) {
4428 if (datumtype == 'U' && pat == patcopy+1)
4430 if (datumtype == '#') {
4431 while (pat < patend && *pat != '\n')
4436 char *natstr = "sSiIlL";
4438 if (strchr(natstr, datumtype)) {
4439 #ifdef PERL_NATINT_PACK
4445 DIE(aTHX_ "'!' allowed only after types %s", natstr);
4448 len = strchr("@Xxu", datumtype) ? 0 : items;
4451 else if (isDIGIT(*pat)) {
4453 while (isDIGIT(*pat)) {
4454 len = (len * 10) + (*pat++ - '0');
4456 DIE(aTHX_ "Repeat count in pack overflows");
4463 if ((*pat != 'a' && *pat != 'A' && *pat != 'Z') || pat[1] != '*')
4464 DIE(aTHX_ "/ must be followed by a*, A* or Z*");
4465 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
4466 ? *MARK : &PL_sv_no)
4467 + (*pat == 'Z' ? 1 : 0)));
4471 DIE(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
4472 case ',': /* grandfather in commas but with a warning */
4473 if (commas++ == 0 && ckWARN(WARN_PACK))
4474 Perl_warner(aTHX_ WARN_PACK,
4475 "Invalid type in pack: '%c'", (int)datumtype);
4478 DIE(aTHX_ "%% may only be used in unpack");
4489 if (SvCUR(cat) < len)
4490 DIE(aTHX_ "X outside of string");
4497 sv_catpvn(cat, null10, 10);
4500 sv_catpvn(cat, null10, len);
4506 aptr = SvPV(fromstr, fromlen);
4507 if (pat[-1] == '*') {
4509 if (datumtype == 'Z')
4512 if (fromlen >= len) {
4513 sv_catpvn(cat, aptr, len);
4514 if (datumtype == 'Z')
4515 *(SvEND(cat)-1) = '\0';
4518 sv_catpvn(cat, aptr, fromlen);
4520 if (datumtype == 'A') {
4522 sv_catpvn(cat, space10, 10);
4525 sv_catpvn(cat, space10, len);
4529 sv_catpvn(cat, null10, 10);
4532 sv_catpvn(cat, null10, len);
4544 str = SvPV(fromstr, fromlen);
4548 SvCUR(cat) += (len+7)/8;
4549 SvGROW(cat, SvCUR(cat) + 1);
4550 aptr = SvPVX(cat) + aint;
4555 if (datumtype == 'B') {
4556 for (len = 0; len++ < aint;) {
4557 items |= *str++ & 1;
4561 *aptr++ = items & 0xff;
4567 for (len = 0; len++ < aint;) {
4573 *aptr++ = items & 0xff;
4579 if (datumtype == 'B')
4580 items <<= 7 - (aint & 7);
4582 items >>= 7 - (aint & 7);
4583 *aptr++ = items & 0xff;
4585 str = SvPVX(cat) + SvCUR(cat);
4600 str = SvPV(fromstr, fromlen);
4604 SvCUR(cat) += (len+1)/2;
4605 SvGROW(cat, SvCUR(cat) + 1);
4606 aptr = SvPVX(cat) + aint;
4611 if (datumtype == 'H') {
4612 for (len = 0; len++ < aint;) {
4614 items |= ((*str++ & 15) + 9) & 15;
4616 items |= *str++ & 15;
4620 *aptr++ = items & 0xff;
4626 for (len = 0; len++ < aint;) {
4628 items |= (((*str++ & 15) + 9) & 15) << 4;
4630 items |= (*str++ & 15) << 4;
4634 *aptr++ = items & 0xff;
4640 *aptr++ = items & 0xff;
4641 str = SvPVX(cat) + SvCUR(cat);
4652 aint = SvIV(fromstr);
4654 sv_catpvn(cat, &achar, sizeof(char));
4660 auint = SvUV(fromstr);
4661 SvGROW(cat, SvCUR(cat) + UTF8_MAXLEN);
4662 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4667 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4672 afloat = (float)SvNV(fromstr);
4673 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4680 adouble = (double)SvNV(fromstr);
4681 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4687 ashort = (I16)SvIV(fromstr);
4689 ashort = PerlSock_htons(ashort);
4691 CAT16(cat, &ashort);
4697 ashort = (I16)SvIV(fromstr);
4699 ashort = htovs(ashort);
4701 CAT16(cat, &ashort);
4705 #if SHORTSIZE != SIZE16
4707 unsigned short aushort;
4711 aushort = SvUV(fromstr);
4712 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4722 aushort = (U16)SvUV(fromstr);
4723 CAT16(cat, &aushort);
4729 #if SHORTSIZE != SIZE16
4735 ashort = SvIV(fromstr);
4736 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4744 ashort = (I16)SvIV(fromstr);
4745 CAT16(cat, &ashort);
4752 auint = SvUV(fromstr);
4753 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4759 adouble = Perl_floor(SvNV(fromstr));
4762 DIE(aTHX_ "Cannot compress negative numbers");
4765 #if UVSIZE > 4 && UVSIZE >= NVSIZE
4766 adouble <= 0xffffffff
4768 # ifdef CXUX_BROKEN_CONSTANT_CONVERT
4769 adouble <= UV_MAX_cxux
4776 char buf[1 + sizeof(UV)];
4777 char *in = buf + sizeof(buf);
4778 UV auv = U_V(adouble);
4781 *--in = (auv & 0x7f) | 0x80;
4784 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4785 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4787 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4788 char *from, *result, *in;
4793 /* Copy string and check for compliance */
4794 from = SvPV(fromstr, len);
4795 if ((norm = is_an_int(from, len)) == NULL)
4796 DIE(aTHX_ "can compress only unsigned integer");
4798 New('w', result, len, char);
4802 *--in = div128(norm, &done) | 0x80;
4803 result[len - 1] &= 0x7F; /* clear continue bit */
4804 sv_catpvn(cat, in, (result + len) - in);
4806 SvREFCNT_dec(norm); /* free norm */
4808 else if (SvNOKp(fromstr)) {
4809 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4810 char *in = buf + sizeof(buf);
4813 double next = floor(adouble / 128);
4814 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4815 if (--in < buf) /* this cannot happen ;-) */
4816 DIE(aTHX_ "Cannot compress integer");
4818 } while (adouble > 0);
4819 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4820 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4823 DIE(aTHX_ "Cannot compress non integer");
4829 aint = SvIV(fromstr);
4830 sv_catpvn(cat, (char*)&aint, sizeof(int));
4836 aulong = SvUV(fromstr);
4838 aulong = PerlSock_htonl(aulong);
4840 CAT32(cat, &aulong);
4846 aulong = SvUV(fromstr);
4848 aulong = htovl(aulong);
4850 CAT32(cat, &aulong);
4854 #if LONGSIZE != SIZE32
4856 unsigned long aulong;
4860 aulong = SvUV(fromstr);
4861 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4869 aulong = SvUV(fromstr);
4870 CAT32(cat, &aulong);
4875 #if LONGSIZE != SIZE32
4881 along = SvIV(fromstr);
4882 sv_catpvn(cat, (char *)&along, sizeof(long));
4890 along = SvIV(fromstr);
4899 auquad = (Uquad_t)SvUV(fromstr);
4900 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4906 aquad = (Quad_t)SvIV(fromstr);
4907 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4912 len = 1; /* assume SV is correct length */
4917 if (fromstr == &PL_sv_undef)
4921 /* XXX better yet, could spirit away the string to
4922 * a safe spot and hang on to it until the result
4923 * of pack() (and all copies of the result) are
4926 if (ckWARN(WARN_PACK) && (SvTEMP(fromstr)
4927 || (SvPADTMP(fromstr)
4928 && !SvREADONLY(fromstr))))
4930 Perl_warner(aTHX_ WARN_PACK,
4931 "Attempt to pack pointer to temporary value");
4933 if (SvPOK(fromstr) || SvNIOK(fromstr))
4934 aptr = SvPV(fromstr,n_a);
4936 aptr = SvPV_force(fromstr,n_a);
4938 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4943 aptr = SvPV(fromstr, fromlen);
4944 SvGROW(cat, fromlen * 4 / 3);
4949 while (fromlen > 0) {
4956 doencodes(cat, aptr, todo);
4975 register I32 limit = POPi; /* note, negative is forever */
4978 register char *s = SvPV(sv, len);
4979 char *strend = s + len;
4981 register REGEXP *rx;
4985 I32 maxiters = (strend - s) + 10;
4988 I32 origlimit = limit;
4991 AV *oldstack = PL_curstack;
4992 I32 gimme = GIMME_V;
4993 I32 oldsave = PL_savestack_ix;
4994 I32 make_mortal = 1;
4995 MAGIC *mg = (MAGIC *) NULL;
4998 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
5003 DIE(aTHX_ "panic: do_split");
5004 rx = pm->op_pmregexp;
5006 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
5007 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
5009 if (pm->op_pmreplroot) {
5011 ary = GvAVn((GV*)PL_curpad[(PADOFFSET)pm->op_pmreplroot]);
5013 ary = GvAVn((GV*)pm->op_pmreplroot);
5016 else if (gimme != G_ARRAY)
5018 ary = (AV*)PL_curpad[0];
5020 ary = GvAVn(PL_defgv);
5021 #endif /* USE_THREADS */
5024 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5030 if ((mg = SvTIED_mg((SV*)ary, 'P'))) {
5032 XPUSHs(SvTIED_obj((SV*)ary, mg));
5038 for (i = AvFILLp(ary); i >= 0; i--)
5039 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5041 /* temporarily switch stacks */
5042 SWITCHSTACK(PL_curstack, ary);
5046 base = SP - PL_stack_base;
5048 if (pm->op_pmflags & PMf_SKIPWHITE) {
5049 if (pm->op_pmflags & PMf_LOCALE) {
5050 while (isSPACE_LC(*s))
5058 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
5059 SAVEINT(PL_multiline);
5060 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
5064 limit = maxiters + 2;
5065 if (pm->op_pmflags & PMf_WHITE) {
5068 while (m < strend &&
5069 !((pm->op_pmflags & PMf_LOCALE)
5070 ? isSPACE_LC(*m) : isSPACE(*m)))
5075 dstr = NEWSV(30, m-s);
5076 sv_setpvn(dstr, s, m-s);
5082 while (s < strend &&
5083 ((pm->op_pmflags & PMf_LOCALE)
5084 ? isSPACE_LC(*s) : isSPACE(*s)))
5088 else if (strEQ("^", rx->precomp)) {
5091 for (m = s; m < strend && *m != '\n'; m++) ;
5095 dstr = NEWSV(30, m-s);
5096 sv_setpvn(dstr, s, m-s);
5103 else if ((rx->reganch & RE_USE_INTUIT) && !rx->nparens
5104 && (rx->reganch & ROPT_CHECK_ALL)
5105 && !(rx->reganch & ROPT_ANCH)) {
5106 int tail = (rx->reganch & RE_INTUIT_TAIL);
5107 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
5111 if (len == 1 && !tail) {
5115 for (m = s; m < strend && *m != c; m++) ;
5118 dstr = NEWSV(30, m-s);
5119 sv_setpvn(dstr, s, m-s);
5128 while (s < strend && --limit &&
5129 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5130 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
5133 dstr = NEWSV(31, m-s);
5134 sv_setpvn(dstr, s, m-s);
5138 s = m + len; /* Fake \n at the end */
5143 maxiters += (strend - s) * rx->nparens;
5144 while (s < strend && --limit
5145 /* && (!rx->check_substr
5146 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
5148 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
5149 1 /* minend */, sv, NULL, 0))
5151 TAINT_IF(RX_MATCH_TAINTED(rx));
5152 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5157 strend = s + (strend - m);
5159 m = rx->startp[0] + orig;
5160 dstr = NEWSV(32, m-s);
5161 sv_setpvn(dstr, s, m-s);
5166 for (i = 1; i <= rx->nparens; i++) {
5167 s = rx->startp[i] + orig;
5168 m = rx->endp[i] + orig;
5170 dstr = NEWSV(33, m-s);
5171 sv_setpvn(dstr, s, m-s);
5174 dstr = NEWSV(33, 0);
5180 s = rx->endp[0] + orig;
5184 LEAVE_SCOPE(oldsave);
5185 iters = (SP - PL_stack_base) - base;
5186 if (iters > maxiters)
5187 DIE(aTHX_ "Split loop");
5189 /* keep field after final delim? */
5190 if (s < strend || (iters && origlimit)) {
5191 dstr = NEWSV(34, strend-s);
5192 sv_setpvn(dstr, s, strend-s);
5198 else if (!origlimit) {
5199 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5205 SWITCHSTACK(ary, oldstack);
5206 if (SvSMAGICAL(ary)) {
5211 if (gimme == G_ARRAY) {
5213 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5221 call_method("PUSH",G_SCALAR|G_DISCARD);
5224 if (gimme == G_ARRAY) {
5225 /* EXTEND should not be needed - we just popped them */
5227 for (i=0; i < iters; i++) {
5228 SV **svp = av_fetch(ary, i, FALSE);
5229 PUSHs((svp) ? *svp : &PL_sv_undef);
5236 if (gimme == G_ARRAY)
5239 if (iters || !pm->op_pmreplroot) {
5249 Perl_unlock_condpair(pTHX_ void *svv)
5252 MAGIC *mg = mg_find((SV*)svv, 'm');
5255 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
5256 MUTEX_LOCK(MgMUTEXP(mg));
5257 if (MgOWNER(mg) != thr)
5258 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
5260 COND_SIGNAL(MgOWNERCONDP(mg));
5261 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": unlock 0x%"UVxf"\n",
5262 PTR2UV(thr), PTR2UV(svv));)
5263 MUTEX_UNLOCK(MgMUTEXP(mg));
5265 #endif /* USE_THREADS */
5274 #endif /* USE_THREADS */
5275 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5276 || SvTYPE(retsv) == SVt_PVCV) {
5277 retsv = refto(retsv);
5288 if (PL_op->op_private & OPpLVAL_INTRO)
5289 PUSHs(*save_threadsv(PL_op->op_targ));
5291 PUSHs(THREADSV(PL_op->op_targ));
5294 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
5295 #endif /* USE_THREADS */