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)) {
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);
248 if (PL_op->op_private & HINT_STRICT_REFS)
249 DIE(aTHX_ PL_no_symref, sym, "a symbol");
250 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
254 if (PL_op->op_private & OPpLVAL_INTRO)
255 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
266 tryAMAGICunDEREF(to_sv);
269 switch (SvTYPE(sv)) {
273 DIE(aTHX_ "Not a SCALAR reference");
281 if (SvTYPE(gv) != SVt_PVGV) {
282 if (SvGMAGICAL(sv)) {
288 if (PL_op->op_flags & OPf_REF ||
289 PL_op->op_private & HINT_STRICT_REFS)
290 DIE(aTHX_ PL_no_usym, "a SCALAR");
291 if (ckWARN(WARN_UNINITIALIZED))
296 if ((PL_op->op_flags & OPf_SPECIAL) &&
297 !(PL_op->op_flags & OPf_MOD))
299 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
304 if (PL_op->op_private & HINT_STRICT_REFS)
305 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
306 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
311 if (PL_op->op_flags & OPf_MOD) {
312 if (PL_op->op_private & OPpLVAL_INTRO)
313 sv = save_scalar((GV*)TOPs);
314 else if (PL_op->op_private & OPpDEREF)
315 vivify_ref(sv, PL_op->op_private & OPpDEREF);
325 SV *sv = AvARYLEN(av);
327 AvARYLEN(av) = sv = NEWSV(0,0);
328 sv_upgrade(sv, SVt_IV);
329 sv_magic(sv, (SV*)av, '#', Nullch, 0);
337 djSP; dTARGET; dPOPss;
339 if (PL_op->op_flags & OPf_MOD) {
340 if (SvTYPE(TARG) < SVt_PVLV) {
341 sv_upgrade(TARG, SVt_PVLV);
342 sv_magic(TARG, Nullsv, '.', Nullch, 0);
346 if (LvTARG(TARG) != sv) {
348 SvREFCNT_dec(LvTARG(TARG));
349 LvTARG(TARG) = SvREFCNT_inc(sv);
351 PUSHs(TARG); /* no SvSETMAGIC */
357 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
358 mg = mg_find(sv, 'g');
359 if (mg && mg->mg_len >= 0) {
363 PUSHi(i + PL_curcop->cop_arybase);
377 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
378 /* (But not in defined().) */
379 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
382 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
383 if ((PL_op->op_private & OPpLVAL_INTRO) && !CvLVALUE(cv))
384 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
387 cv = (CV*)&PL_sv_undef;
401 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
402 char *s = SvPVX(TOPs);
403 if (strnEQ(s, "CORE::", 6)) {
406 code = keyword(s + 6, SvCUR(TOPs) - 6);
407 if (code < 0) { /* Overridable. */
408 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
409 int i = 0, n = 0, seen_question = 0;
411 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
413 while (i < MAXO) { /* The slow way. */
414 if (strEQ(s + 6, PL_op_name[i])
415 || strEQ(s + 6, PL_op_desc[i]))
421 goto nonesuch; /* Should not happen... */
423 oa = PL_opargs[i] >> OASHIFT;
425 if (oa & OA_OPTIONAL) {
429 else if (seen_question)
430 goto set; /* XXXX system, exec */
431 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
432 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
435 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
436 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
440 ret = sv_2mortal(newSVpvn(str, n - 1));
442 else if (code) /* Non-Overridable */
444 else { /* None such */
446 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
450 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
452 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
461 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
463 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
479 if (GIMME != G_ARRAY) {
483 *MARK = &PL_sv_undef;
484 *MARK = refto(*MARK);
488 EXTEND_MORTAL(SP - MARK);
490 *MARK = refto(*MARK);
495 S_refto(pTHX_ SV *sv)
499 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
502 if (!(sv = LvTARG(sv)))
505 (void)SvREFCNT_inc(sv);
507 else if (SvTYPE(sv) == SVt_PVAV) {
508 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
511 (void)SvREFCNT_inc(sv);
513 else if (SvPADTMP(sv))
517 (void)SvREFCNT_inc(sv);
520 sv_upgrade(rv, SVt_RV);
534 if (sv && SvGMAGICAL(sv))
537 if (!sv || !SvROK(sv))
541 pv = sv_reftype(sv,TRUE);
542 PUSHp(pv, strlen(pv));
552 stash = CopSTASH(PL_curcop);
556 char *ptr = SvPV(ssv,len);
557 if (ckWARN(WARN_MISC) && len == 0)
558 Perl_warner(aTHX_ WARN_MISC,
559 "Explicit blessing to '' (assuming package main)");
560 stash = gv_stashpvn(ptr, len, TRUE);
563 (void)sv_bless(TOPs, stash);
577 elem = SvPV(sv, n_a);
581 switch (elem ? *elem : '\0')
584 if (strEQ(elem, "ARRAY"))
585 tmpRef = (SV*)GvAV(gv);
588 if (strEQ(elem, "CODE"))
589 tmpRef = (SV*)GvCVu(gv);
592 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
593 tmpRef = (SV*)GvIOp(gv);
596 if (strEQ(elem, "GLOB"))
600 if (strEQ(elem, "HASH"))
601 tmpRef = (SV*)GvHV(gv);
604 if (strEQ(elem, "IO"))
605 tmpRef = (SV*)GvIOp(gv);
608 if (strEQ(elem, "NAME"))
609 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
612 if (strEQ(elem, "PACKAGE"))
613 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
616 if (strEQ(elem, "SCALAR"))
630 /* Pattern matching */
635 register unsigned char *s;
638 register I32 *sfirst;
642 if (sv == PL_lastscream) {
648 SvSCREAM_off(PL_lastscream);
649 SvREFCNT_dec(PL_lastscream);
651 PL_lastscream = SvREFCNT_inc(sv);
654 s = (unsigned char*)(SvPV(sv, len));
658 if (pos > PL_maxscream) {
659 if (PL_maxscream < 0) {
660 PL_maxscream = pos + 80;
661 New(301, PL_screamfirst, 256, I32);
662 New(302, PL_screamnext, PL_maxscream, I32);
665 PL_maxscream = pos + pos / 4;
666 Renew(PL_screamnext, PL_maxscream, I32);
670 sfirst = PL_screamfirst;
671 snext = PL_screamnext;
673 if (!sfirst || !snext)
674 DIE(aTHX_ "do_study: out of memory");
676 for (ch = 256; ch; --ch)
683 snext[pos] = sfirst[ch] - pos;
690 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
699 if (PL_op->op_flags & OPf_STACKED)
705 TARG = sv_newmortal();
710 /* Lvalue operators. */
722 djSP; dMARK; dTARGET;
732 SETi(do_chomp(TOPs));
738 djSP; dMARK; dTARGET;
739 register I32 count = 0;
742 count += do_chomp(POPs);
753 if (!sv || !SvANY(sv))
755 switch (SvTYPE(sv)) {
757 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
761 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
765 if (CvROOT(sv) || CvXSUB(sv))
782 if (!PL_op->op_private) {
791 if (SvTHINKFIRST(sv))
794 switch (SvTYPE(sv)) {
804 if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv))
805 Perl_warner(aTHX_ WARN_MISC, "Constant subroutine %s undefined",
806 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
810 /* let user-undef'd sub keep its identity */
811 GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
818 SvSetMagicSV(sv, &PL_sv_undef);
822 Newz(602, gp, 1, GP);
823 GvGP(sv) = gp_ref(gp);
824 GvSV(sv) = NEWSV(72,0);
825 GvLINE(sv) = CopLINE(PL_curcop);
831 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
834 SvPV_set(sv, Nullch);
847 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
848 DIE(aTHX_ PL_no_modify);
849 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
850 SvIVX(TOPs) != IV_MIN)
853 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
864 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
865 DIE(aTHX_ PL_no_modify);
866 sv_setsv(TARG, TOPs);
867 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
868 SvIVX(TOPs) != IV_MAX)
871 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
885 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
886 DIE(aTHX_ PL_no_modify);
887 sv_setsv(TARG, TOPs);
888 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
889 SvIVX(TOPs) != IV_MIN)
892 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
901 /* Ordinary operators. */
905 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
908 SETn( Perl_pow( left, right) );
915 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
918 SETn( left * right );
925 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
930 DIE(aTHX_ "Illegal division by zero");
932 /* insure that 20./5. == 4. */
935 if ((NV)I_V(left) == left &&
936 (NV)I_V(right) == right &&
937 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
941 value = left / right;
945 value = left / right;
954 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
964 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
966 right = (right_neg = (i < 0)) ? -i : i;
971 right_neg = dright < 0;
976 if (!use_double && SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
978 left = (left_neg = (i < 0)) ? -i : i;
986 left_neg = dleft < 0;
995 /* Somehow U_V is pessimized even if CASTFLAGS is 0 */
997 # define CAST_D2UV(d) U_V(d)
999 # define CAST_D2UV(d) ((UV)(d))
1001 /* Tried to do this only in the case DOUBLESIZE <= UV_SIZE,
1002 * or, in other words, precision of UV more than of NV.
1003 * But in fact the approach below turned out to be an
1004 * optimization - floor() may be slow */
1005 if (dright <= UV_MAX && dleft <= UV_MAX) {
1006 right = CAST_D2UV(dright);
1007 left = CAST_D2UV(dleft);
1012 /* Backward-compatibility clause: */
1013 dright = Perl_floor(dright + 0.5);
1014 dleft = Perl_floor(dleft + 0.5);
1017 DIE(aTHX_ "Illegal modulus zero");
1019 dans = Perl_fmod(dleft, dright);
1020 if ((left_neg != right_neg) && dans)
1021 dans = dright - dans;
1024 sv_setnv(TARG, dans);
1031 DIE(aTHX_ "Illegal modulus zero");
1034 if ((left_neg != right_neg) && ans)
1037 /* XXX may warn: unary minus operator applied to unsigned type */
1038 /* could change -foo to be (~foo)+1 instead */
1039 if (ans <= ~((UV)IV_MAX)+1)
1040 sv_setiv(TARG, ~ans+1);
1042 sv_setnv(TARG, -(NV)ans);
1045 sv_setuv(TARG, ans);
1054 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1056 register I32 count = POPi;
1057 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1059 I32 items = SP - MARK;
1062 max = items * count;
1071 repeatcpy((char*)(MARK + items), (char*)MARK,
1072 items * sizeof(SV*), count - 1);
1075 else if (count <= 0)
1078 else { /* Note: mark already snarfed by pp_list */
1083 SvSetSV(TARG, tmpstr);
1084 SvPV_force(TARG, len);
1089 SvGROW(TARG, (count * len) + 1);
1090 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1091 SvCUR(TARG) *= count;
1093 *SvEND(TARG) = '\0';
1095 (void)SvPOK_only(TARG);
1104 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1107 SETn( left - right );
1114 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1117 if (PL_op->op_private & HINT_INTEGER) {
1131 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1134 if (PL_op->op_private & HINT_INTEGER) {
1148 djSP; tryAMAGICbinSET(lt,0);
1151 SETs(boolSV(TOPn < value));
1158 djSP; tryAMAGICbinSET(gt,0);
1161 SETs(boolSV(TOPn > value));
1168 djSP; tryAMAGICbinSET(le,0);
1171 SETs(boolSV(TOPn <= value));
1178 djSP; tryAMAGICbinSET(ge,0);
1181 SETs(boolSV(TOPn >= value));
1188 djSP; tryAMAGICbinSET(ne,0);
1191 SETs(boolSV(TOPn != value));
1198 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1205 else if (left < right)
1207 else if (left > right)
1220 djSP; tryAMAGICbinSET(slt,0);
1223 int cmp = ((PL_op->op_private & OPpLOCALE)
1224 ? sv_cmp_locale(left, right)
1225 : sv_cmp(left, right));
1226 SETs(boolSV(cmp < 0));
1233 djSP; tryAMAGICbinSET(sgt,0);
1236 int cmp = ((PL_op->op_private & OPpLOCALE)
1237 ? sv_cmp_locale(left, right)
1238 : sv_cmp(left, right));
1239 SETs(boolSV(cmp > 0));
1246 djSP; tryAMAGICbinSET(sle,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(sge,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(seq,0);
1275 SETs(boolSV(sv_eq(left, right)));
1282 djSP; tryAMAGICbinSET(sne,0);
1285 SETs(boolSV(!sv_eq(left, right)));
1292 djSP; dTARGET; tryAMAGICbin(scmp,0);
1295 int cmp = ((PL_op->op_private & OPpLOCALE)
1296 ? sv_cmp_locale(left, right)
1297 : sv_cmp(left, right));
1305 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1308 if (SvNIOKp(left) || SvNIOKp(right)) {
1309 if (PL_op->op_private & HINT_INTEGER) {
1310 IV i = SvIV(left) & SvIV(right);
1314 UV u = SvUV(left) & SvUV(right);
1319 do_vop(PL_op->op_type, TARG, left, right);
1328 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1331 if (SvNIOKp(left) || SvNIOKp(right)) {
1332 if (PL_op->op_private & HINT_INTEGER) {
1333 IV i = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1337 UV u = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1342 do_vop(PL_op->op_type, TARG, left, right);
1351 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1354 if (SvNIOKp(left) || SvNIOKp(right)) {
1355 if (PL_op->op_private & HINT_INTEGER) {
1356 IV i = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1360 UV u = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1365 do_vop(PL_op->op_type, TARG, left, right);
1374 djSP; dTARGET; tryAMAGICun(neg);
1379 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1381 else if (SvNIOKp(sv))
1383 else if (SvPOKp(sv)) {
1385 char *s = SvPV(sv, len);
1386 if (isIDFIRST(*s)) {
1387 sv_setpvn(TARG, "-", 1);
1390 else if (*s == '+' || *s == '-') {
1392 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1394 else if (DO_UTF8(sv) && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1395 sv_setpvn(TARG, "-", 1);
1399 sv_setnv(TARG, -SvNV(sv));
1410 djSP; tryAMAGICunSET(not);
1411 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1417 djSP; dTARGET; tryAMAGICun(compl);
1421 if (PL_op->op_private & HINT_INTEGER) {
1431 register char *tmps;
1432 register long *tmpl;
1437 tmps = SvPV_force(TARG, len);
1440 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1443 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1447 for ( ; anum > 0; anum--, tmps++)
1456 /* integer versions of some of the above */
1460 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1463 SETi( left * right );
1470 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1474 DIE(aTHX_ "Illegal division by zero");
1475 value = POPi / value;
1483 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1487 DIE(aTHX_ "Illegal modulus zero");
1488 SETi( left % right );
1495 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1498 SETi( left + right );
1505 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1508 SETi( left - right );
1515 djSP; tryAMAGICbinSET(lt,0);
1518 SETs(boolSV(left < right));
1525 djSP; tryAMAGICbinSET(gt,0);
1528 SETs(boolSV(left > right));
1535 djSP; tryAMAGICbinSET(le,0);
1538 SETs(boolSV(left <= right));
1545 djSP; tryAMAGICbinSET(ge,0);
1548 SETs(boolSV(left >= right));
1555 djSP; tryAMAGICbinSET(eq,0);
1558 SETs(boolSV(left == right));
1565 djSP; tryAMAGICbinSET(ne,0);
1568 SETs(boolSV(left != right));
1575 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1582 else if (left < right)
1593 djSP; dTARGET; tryAMAGICun(neg);
1598 /* High falutin' math. */
1602 djSP; dTARGET; tryAMAGICbin(atan2,0);
1605 SETn(Perl_atan2(left, right));
1612 djSP; dTARGET; tryAMAGICun(sin);
1616 value = Perl_sin(value);
1624 djSP; dTARGET; tryAMAGICun(cos);
1628 value = Perl_cos(value);
1634 /* Support Configure command-line overrides for rand() functions.
1635 After 5.005, perhaps we should replace this by Configure support
1636 for drand48(), random(), or rand(). For 5.005, though, maintain
1637 compatibility by calling rand() but allow the user to override it.
1638 See INSTALL for details. --Andy Dougherty 15 July 1998
1640 /* Now it's after 5.005, and Configure supports drand48() and random(),
1641 in addition to rand(). So the overrides should not be needed any more.
1642 --Jarkko Hietaniemi 27 September 1998
1645 #ifndef HAS_DRAND48_PROTO
1646 extern double drand48 (void);
1659 if (!PL_srand_called) {
1660 (void)seedDrand01((Rand_seed_t)seed());
1661 PL_srand_called = TRUE;
1676 (void)seedDrand01((Rand_seed_t)anum);
1677 PL_srand_called = TRUE;
1686 * This is really just a quick hack which grabs various garbage
1687 * values. It really should be a real hash algorithm which
1688 * spreads the effect of every input bit onto every output bit,
1689 * if someone who knows about such things would bother to write it.
1690 * Might be a good idea to add that function to CORE as well.
1691 * No numbers below come from careful analysis or anything here,
1692 * except they are primes and SEED_C1 > 1E6 to get a full-width
1693 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1694 * probably be bigger too.
1697 # define SEED_C1 1000003
1698 #define SEED_C4 73819
1700 # define SEED_C1 25747
1701 #define SEED_C4 20639
1705 #define SEED_C5 26107
1708 #ifndef PERL_NO_DEV_RANDOM
1713 # include <starlet.h>
1714 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1715 * in 100-ns units, typically incremented ever 10 ms. */
1716 unsigned int when[2];
1718 # ifdef HAS_GETTIMEOFDAY
1719 struct timeval when;
1725 /* This test is an escape hatch, this symbol isn't set by Configure. */
1726 #ifndef PERL_NO_DEV_RANDOM
1727 #ifndef PERL_RANDOM_DEVICE
1728 /* /dev/random isn't used by default because reads from it will block
1729 * if there isn't enough entropy available. You can compile with
1730 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1731 * is enough real entropy to fill the seed. */
1732 # define PERL_RANDOM_DEVICE "/dev/urandom"
1734 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1736 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1745 _ckvmssts(sys$gettim(when));
1746 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1748 # ifdef HAS_GETTIMEOFDAY
1749 gettimeofday(&when,(struct timezone *) 0);
1750 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1753 u = (U32)SEED_C1 * when;
1756 u += SEED_C3 * (U32)PerlProc_getpid();
1757 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
1758 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1759 u += SEED_C5 * (U32)PTR2UV(&when);
1766 djSP; dTARGET; tryAMAGICun(exp);
1770 value = Perl_exp(value);
1778 djSP; dTARGET; tryAMAGICun(log);
1783 RESTORE_NUMERIC_STANDARD();
1784 DIE(aTHX_ "Can't take log of %g", value);
1786 value = Perl_log(value);
1794 djSP; dTARGET; tryAMAGICun(sqrt);
1799 RESTORE_NUMERIC_STANDARD();
1800 DIE(aTHX_ "Can't take sqrt of %g", value);
1802 value = Perl_sqrt(value);
1815 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1821 (void)Perl_modf(value, &value);
1823 (void)Perl_modf(-value, &value);
1838 djSP; dTARGET; tryAMAGICun(abs);
1843 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1844 (iv = SvIVX(TOPs)) != IV_MIN) {
1866 XPUSHn(scan_hex(tmps, 99, &argtype));
1879 while (*tmps && isSPACE(*tmps))
1884 value = scan_hex(++tmps, 99, &argtype);
1885 else if (*tmps == 'b')
1886 value = scan_bin(++tmps, 99, &argtype);
1888 value = scan_oct(tmps, 99, &argtype);
1901 SETi(sv_len_utf8(sv));
1917 I32 lvalue = PL_op->op_flags & OPf_MOD;
1919 I32 arybase = PL_curcop->cop_arybase;
1923 SvTAINTED_off(TARG); /* decontaminate */
1924 SvUTF8_off(TARG); /* decontaminate */
1928 repl = SvPV(sv, repl_len);
1935 tmps = SvPV(sv, curlen);
1937 utfcurlen = sv_len_utf8(sv);
1938 if (utfcurlen == curlen)
1946 if (pos >= arybase) {
1964 else if (len >= 0) {
1966 if (rem > (I32)curlen)
1981 Perl_croak(aTHX_ "substr outside of string");
1982 if (ckWARN(WARN_SUBSTR))
1983 Perl_warner(aTHX_ WARN_SUBSTR, "substr outside of string");
1988 sv_pos_u2b(sv, &pos, &rem);
1992 sv_setpvn(TARG, tmps, rem);
1994 sv_insert(sv, pos, rem, repl, repl_len);
1995 else if (lvalue) { /* it's an lvalue! */
1996 if (!SvGMAGICAL(sv)) {
2000 if (ckWARN(WARN_SUBSTR))
2001 Perl_warner(aTHX_ WARN_SUBSTR,
2002 "Attempt to use reference as lvalue in substr");
2004 if (SvOK(sv)) /* is it defined ? */
2005 (void)SvPOK_only(sv);
2007 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2010 if (SvTYPE(TARG) < SVt_PVLV) {
2011 sv_upgrade(TARG, SVt_PVLV);
2012 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
2016 if (LvTARG(TARG) != sv) {
2018 SvREFCNT_dec(LvTARG(TARG));
2019 LvTARG(TARG) = SvREFCNT_inc(sv);
2021 LvTARGOFF(TARG) = pos;
2022 LvTARGLEN(TARG) = rem;
2026 PUSHs(TARG); /* avoid SvSETMAGIC here */
2033 register I32 size = POPi;
2034 register I32 offset = POPi;
2035 register SV *src = POPs;
2036 I32 lvalue = PL_op->op_flags & OPf_MOD;
2038 SvTAINTED_off(TARG); /* decontaminate */
2039 if (lvalue) { /* it's an lvalue! */
2040 if (SvTYPE(TARG) < SVt_PVLV) {
2041 sv_upgrade(TARG, SVt_PVLV);
2042 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2045 if (LvTARG(TARG) != src) {
2047 SvREFCNT_dec(LvTARG(TARG));
2048 LvTARG(TARG) = SvREFCNT_inc(src);
2050 LvTARGOFF(TARG) = offset;
2051 LvTARGLEN(TARG) = size;
2054 sv_setuv(TARG, do_vecget(src, offset, size));
2069 I32 arybase = PL_curcop->cop_arybase;
2074 offset = POPi - arybase;
2077 tmps = SvPV(big, biglen);
2078 if (offset > 0 && DO_UTF8(big))
2079 sv_pos_u2b(big, &offset, 0);
2082 else if (offset > biglen)
2084 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2085 (unsigned char*)tmps + biglen, little, 0)))
2088 retval = tmps2 - tmps;
2089 if (retval > 0 && DO_UTF8(big))
2090 sv_pos_b2u(big, &retval);
2091 PUSHi(retval + arybase);
2106 I32 arybase = PL_curcop->cop_arybase;
2112 tmps2 = SvPV(little, llen);
2113 tmps = SvPV(big, blen);
2117 if (offset > 0 && DO_UTF8(big))
2118 sv_pos_u2b(big, &offset, 0);
2119 offset = offset - arybase + llen;
2123 else if (offset > blen)
2125 if (!(tmps2 = rninstr(tmps, tmps + offset,
2126 tmps2, tmps2 + llen)))
2129 retval = tmps2 - tmps;
2130 if (retval > 0 && DO_UTF8(big))
2131 sv_pos_b2u(big, &retval);
2132 PUSHi(retval + arybase);
2138 djSP; dMARK; dORIGMARK; dTARGET;
2139 do_sprintf(TARG, SP-MARK, MARK+1);
2140 TAINT_IF(SvTAINTED(TARG));
2152 U8 *tmps = (U8*)SvPVx(tmpsv,n_a);
2155 if ((*tmps & 0x80) && DO_UTF8(tmpsv))
2156 value = utf8_to_uv(tmps, &retlen);
2158 value = (UV)(*tmps & 255);
2169 (void)SvUPGRADE(TARG,SVt_PV);
2171 if (value > 255 && !IN_BYTE) {
2172 SvGROW(TARG, UTF8_MAXLEN+1);
2174 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2175 SvCUR_set(TARG, tmps - SvPVX(TARG));
2177 (void)SvPOK_only(TARG);
2188 SvUTF8_off(TARG); /* decontaminate */
2189 (void)SvPOK_only(TARG);
2196 djSP; dTARGET; dPOPTOPssrl;
2199 char *tmps = SvPV(left, n_a);
2201 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2203 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2207 "The crypt() function is unimplemented due to excessive paranoia.");
2220 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2222 U8 tmpbuf[UTF8_MAXLEN];
2224 UV uv = utf8_to_uv(s, &ulen);
2226 if (PL_op->op_private & OPpLOCALE) {
2229 uv = toTITLE_LC_uni(uv);
2232 uv = toTITLE_utf8(s);
2234 tend = uv_to_utf8(tmpbuf, uv);
2236 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
2238 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2239 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2244 s = (U8*)SvPV_force(sv, slen);
2245 Copy(tmpbuf, s, ulen, U8);
2249 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2251 SvUTF8_off(TARG); /* decontaminate */
2256 s = (U8*)SvPV_force(sv, slen);
2258 if (PL_op->op_private & OPpLOCALE) {
2261 *s = toUPPER_LC(*s);
2279 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2281 U8 tmpbuf[UTF8_MAXLEN];
2283 UV uv = utf8_to_uv(s, &ulen);
2285 if (PL_op->op_private & OPpLOCALE) {
2288 uv = toLOWER_LC_uni(uv);
2291 uv = toLOWER_utf8(s);
2293 tend = uv_to_utf8(tmpbuf, uv);
2295 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
2297 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2298 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2303 s = (U8*)SvPV_force(sv, slen);
2304 Copy(tmpbuf, s, ulen, U8);
2308 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2310 SvUTF8_off(TARG); /* decontaminate */
2315 s = (U8*)SvPV_force(sv, slen);
2317 if (PL_op->op_private & OPpLOCALE) {
2320 *s = toLOWER_LC(*s);
2344 s = (U8*)SvPV(sv,len);
2346 SvUTF8_off(TARG); /* decontaminate */
2347 sv_setpvn(TARG, "", 0);
2351 (void)SvUPGRADE(TARG, SVt_PV);
2352 SvGROW(TARG, (len * 2) + 1);
2353 (void)SvPOK_only(TARG);
2354 d = (U8*)SvPVX(TARG);
2356 if (PL_op->op_private & OPpLOCALE) {
2360 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2366 d = uv_to_utf8(d, toUPPER_utf8( s ));
2372 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2377 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2379 SvUTF8_off(TARG); /* decontaminate */
2384 s = (U8*)SvPV_force(sv, len);
2386 register U8 *send = s + len;
2388 if (PL_op->op_private & OPpLOCALE) {
2391 for (; s < send; s++)
2392 *s = toUPPER_LC(*s);
2395 for (; s < send; s++)
2418 s = (U8*)SvPV(sv,len);
2420 SvUTF8_off(TARG); /* decontaminate */
2421 sv_setpvn(TARG, "", 0);
2425 (void)SvUPGRADE(TARG, SVt_PV);
2426 SvGROW(TARG, (len * 2) + 1);
2427 (void)SvPOK_only(TARG);
2428 d = (U8*)SvPVX(TARG);
2430 if (PL_op->op_private & OPpLOCALE) {
2434 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2440 d = uv_to_utf8(d, toLOWER_utf8(s));
2446 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2451 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2453 SvUTF8_off(TARG); /* decontaminate */
2459 s = (U8*)SvPV_force(sv, len);
2461 register U8 *send = s + len;
2463 if (PL_op->op_private & OPpLOCALE) {
2466 for (; s < send; s++)
2467 *s = toLOWER_LC(*s);
2470 for (; s < send; s++)
2485 register char *s = SvPV(sv,len);
2488 SvUTF8_off(TARG); /* decontaminate */
2490 (void)SvUPGRADE(TARG, SVt_PV);
2491 SvGROW(TARG, (len * 2) + 1);
2496 STRLEN ulen = UTF8SKIP(s);
2520 SvCUR_set(TARG, d - SvPVX(TARG));
2521 (void)SvPOK_only(TARG);
2524 sv_setpvn(TARG, s, len);
2526 if (SvSMAGICAL(TARG))
2535 djSP; dMARK; dORIGMARK;
2537 register AV* av = (AV*)POPs;
2538 register I32 lval = PL_op->op_flags & OPf_MOD;
2539 I32 arybase = PL_curcop->cop_arybase;
2542 if (SvTYPE(av) == SVt_PVAV) {
2543 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2545 for (svp = MARK + 1; svp <= SP; svp++) {
2550 if (max > AvMAX(av))
2553 while (++MARK <= SP) {
2554 elem = SvIVx(*MARK);
2558 svp = av_fetch(av, elem, lval);
2560 if (!svp || *svp == &PL_sv_undef)
2561 DIE(aTHX_ PL_no_aelem, elem);
2562 if (PL_op->op_private & OPpLVAL_INTRO)
2563 save_aelem(av, elem, svp);
2565 *MARK = svp ? *svp : &PL_sv_undef;
2568 if (GIMME != G_ARRAY) {
2576 /* Associative arrays. */
2581 HV *hash = (HV*)POPs;
2583 I32 gimme = GIMME_V;
2584 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2587 /* might clobber stack_sp */
2588 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2593 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2594 if (gimme == G_ARRAY) {
2597 /* might clobber stack_sp */
2599 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
2604 else if (gimme == G_SCALAR)
2623 I32 gimme = GIMME_V;
2624 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2628 if (PL_op->op_private & OPpSLICE) {
2632 hvtype = SvTYPE(hv);
2633 if (hvtype == SVt_PVHV) { /* hash element */
2634 while (++MARK <= SP) {
2635 sv = hv_delete_ent(hv, *MARK, discard, 0);
2636 *MARK = sv ? sv : &PL_sv_undef;
2639 else if (hvtype == SVt_PVAV) {
2640 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
2641 while (++MARK <= SP) {
2642 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
2643 *MARK = sv ? sv : &PL_sv_undef;
2646 else { /* pseudo-hash element */
2647 while (++MARK <= SP) {
2648 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2649 *MARK = sv ? sv : &PL_sv_undef;
2654 DIE(aTHX_ "Not a HASH reference");
2657 else if (gimme == G_SCALAR) {
2666 if (SvTYPE(hv) == SVt_PVHV)
2667 sv = hv_delete_ent(hv, keysv, discard, 0);
2668 else if (SvTYPE(hv) == SVt_PVAV) {
2669 if (PL_op->op_flags & OPf_SPECIAL)
2670 sv = av_delete((AV*)hv, SvIV(keysv), discard);
2672 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2675 DIE(aTHX_ "Not a HASH reference");
2690 if (PL_op->op_private & OPpEXISTS_SUB) {
2694 cv = sv_2cv(sv, &hv, &gv, FALSE);
2697 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
2703 if (SvTYPE(hv) == SVt_PVHV) {
2704 if (hv_exists_ent(hv, tmpsv, 0))
2707 else if (SvTYPE(hv) == SVt_PVAV) {
2708 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
2709 if (av_exists((AV*)hv, SvIV(tmpsv)))
2712 else if (avhv_exists_ent((AV*)hv, tmpsv, 0)) /* pseudo-hash element */
2716 DIE(aTHX_ "Not a HASH reference");
2723 djSP; dMARK; dORIGMARK;
2724 register HV *hv = (HV*)POPs;
2725 register I32 lval = PL_op->op_flags & OPf_MOD;
2726 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2728 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2729 DIE(aTHX_ "Can't localize pseudo-hash element");
2731 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2732 while (++MARK <= SP) {
2736 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2737 svp = he ? &HeVAL(he) : 0;
2740 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2743 if (!svp || *svp == &PL_sv_undef) {
2745 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2747 if (PL_op->op_private & OPpLVAL_INTRO)
2748 save_helem(hv, keysv, svp);
2750 *MARK = svp ? *svp : &PL_sv_undef;
2753 if (GIMME != G_ARRAY) {
2761 /* List operators. */
2766 if (GIMME != G_ARRAY) {
2768 *MARK = *SP; /* unwanted list, return last item */
2770 *MARK = &PL_sv_undef;
2779 SV **lastrelem = PL_stack_sp;
2780 SV **lastlelem = PL_stack_base + POPMARK;
2781 SV **firstlelem = PL_stack_base + POPMARK + 1;
2782 register SV **firstrelem = lastlelem + 1;
2783 I32 arybase = PL_curcop->cop_arybase;
2784 I32 lval = PL_op->op_flags & OPf_MOD;
2785 I32 is_something_there = lval;
2787 register I32 max = lastrelem - lastlelem;
2788 register SV **lelem;
2791 if (GIMME != G_ARRAY) {
2792 ix = SvIVx(*lastlelem);
2797 if (ix < 0 || ix >= max)
2798 *firstlelem = &PL_sv_undef;
2800 *firstlelem = firstrelem[ix];
2806 SP = firstlelem - 1;
2810 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2816 if (ix < 0 || ix >= max)
2817 *lelem = &PL_sv_undef;
2819 is_something_there = TRUE;
2820 if (!(*lelem = firstrelem[ix]))
2821 *lelem = &PL_sv_undef;
2824 if (is_something_there)
2827 SP = firstlelem - 1;
2833 djSP; dMARK; dORIGMARK;
2834 I32 items = SP - MARK;
2835 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2836 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2843 djSP; dMARK; dORIGMARK;
2844 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2848 SV *val = NEWSV(46, 0);
2850 sv_setsv(val, *++MARK);
2851 else if (ckWARN(WARN_MISC))
2852 Perl_warner(aTHX_ WARN_MISC, "Odd number of elements in hash assignment");
2853 (void)hv_store_ent(hv,key,val,0);
2862 djSP; dMARK; dORIGMARK;
2863 register AV *ary = (AV*)*++MARK;
2867 register I32 offset;
2868 register I32 length;
2875 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2876 *MARK-- = SvTIED_obj((SV*)ary, mg);
2880 call_method("SPLICE",GIMME_V);
2889 offset = i = SvIVx(*MARK);
2891 offset += AvFILLp(ary) + 1;
2893 offset -= PL_curcop->cop_arybase;
2895 DIE(aTHX_ PL_no_aelem, i);
2897 length = SvIVx(*MARK++);
2899 length += AvFILLp(ary) - offset + 1;
2905 length = AvMAX(ary) + 1; /* close enough to infinity */
2909 length = AvMAX(ary) + 1;
2911 if (offset > AvFILLp(ary) + 1)
2912 offset = AvFILLp(ary) + 1;
2913 after = AvFILLp(ary) + 1 - (offset + length);
2914 if (after < 0) { /* not that much array */
2915 length += after; /* offset+length now in array */
2921 /* At this point, MARK .. SP-1 is our new LIST */
2924 diff = newlen - length;
2925 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2928 if (diff < 0) { /* shrinking the area */
2930 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2931 Copy(MARK, tmparyval, newlen, SV*);
2934 MARK = ORIGMARK + 1;
2935 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2936 MEXTEND(MARK, length);
2937 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2939 EXTEND_MORTAL(length);
2940 for (i = length, dst = MARK; i; i--) {
2941 sv_2mortal(*dst); /* free them eventualy */
2948 *MARK = AvARRAY(ary)[offset+length-1];
2951 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2952 SvREFCNT_dec(*dst++); /* free them now */
2955 AvFILLp(ary) += diff;
2957 /* pull up or down? */
2959 if (offset < after) { /* easier to pull up */
2960 if (offset) { /* esp. if nothing to pull */
2961 src = &AvARRAY(ary)[offset-1];
2962 dst = src - diff; /* diff is negative */
2963 for (i = offset; i > 0; i--) /* can't trust Copy */
2967 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2971 if (after) { /* anything to pull down? */
2972 src = AvARRAY(ary) + offset + length;
2973 dst = src + diff; /* diff is negative */
2974 Move(src, dst, after, SV*);
2976 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2977 /* avoid later double free */
2981 dst[--i] = &PL_sv_undef;
2984 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2986 *dst = NEWSV(46, 0);
2987 sv_setsv(*dst++, *src++);
2989 Safefree(tmparyval);
2992 else { /* no, expanding (or same) */
2994 New(452, tmparyval, length, SV*); /* so remember deletion */
2995 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2998 if (diff > 0) { /* expanding */
3000 /* push up or down? */
3002 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
3006 Move(src, dst, offset, SV*);
3008 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
3010 AvFILLp(ary) += diff;
3013 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
3014 av_extend(ary, AvFILLp(ary) + diff);
3015 AvFILLp(ary) += diff;
3018 dst = AvARRAY(ary) + AvFILLp(ary);
3020 for (i = after; i; i--) {
3027 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
3028 *dst = NEWSV(46, 0);
3029 sv_setsv(*dst++, *src++);
3031 MARK = ORIGMARK + 1;
3032 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3034 Copy(tmparyval, MARK, length, SV*);
3036 EXTEND_MORTAL(length);
3037 for (i = length, dst = MARK; i; i--) {
3038 sv_2mortal(*dst); /* free them eventualy */
3042 Safefree(tmparyval);
3046 else if (length--) {
3047 *MARK = tmparyval[length];
3050 while (length-- > 0)
3051 SvREFCNT_dec(tmparyval[length]);
3053 Safefree(tmparyval);
3056 *MARK = &PL_sv_undef;
3064 djSP; dMARK; dORIGMARK; dTARGET;
3065 register AV *ary = (AV*)*++MARK;
3066 register SV *sv = &PL_sv_undef;
3069 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3070 *MARK-- = SvTIED_obj((SV*)ary, mg);
3074 call_method("PUSH",G_SCALAR|G_DISCARD);
3079 /* Why no pre-extend of ary here ? */
3080 for (++MARK; MARK <= SP; MARK++) {
3083 sv_setsv(sv, *MARK);
3088 PUSHi( AvFILL(ary) + 1 );
3096 SV *sv = av_pop(av);
3098 (void)sv_2mortal(sv);
3107 SV *sv = av_shift(av);
3112 (void)sv_2mortal(sv);
3119 djSP; dMARK; dORIGMARK; dTARGET;
3120 register AV *ary = (AV*)*++MARK;
3125 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3126 *MARK-- = SvTIED_obj((SV*)ary, mg);
3130 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3135 av_unshift(ary, SP - MARK);
3138 sv_setsv(sv, *++MARK);
3139 (void)av_store(ary, i++, sv);
3143 PUSHi( AvFILL(ary) + 1 );
3153 if (GIMME == G_ARRAY) {
3160 /* safe as long as stack cannot get extended in the above */
3165 register char *down;
3170 SvUTF8_off(TARG); /* decontaminate */
3172 do_join(TARG, &PL_sv_no, MARK, SP);
3174 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3175 up = SvPV_force(TARG, len);
3177 if (DO_UTF8(TARG)) { /* first reverse each character */
3178 U8* s = (U8*)SvPVX(TARG);
3179 U8* send = (U8*)(s + len);
3188 down = (char*)(s - 1);
3189 if (s > send || !((*down & 0xc0) == 0x80)) {
3190 if (ckWARN_d(WARN_UTF8))
3191 Perl_warner(aTHX_ WARN_UTF8,
3192 "Malformed UTF-8 character");
3204 down = SvPVX(TARG) + len - 1;
3210 (void)SvPOK_only(TARG);
3219 S_mul128(pTHX_ SV *sv, U8 m)
3222 char *s = SvPV(sv, len);
3226 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3227 SV *tmpNew = newSVpvn("0000000000", 10);
3229 sv_catsv(tmpNew, sv);
3230 SvREFCNT_dec(sv); /* free old sv */
3235 while (!*t) /* trailing '\0'? */
3238 i = ((*t - '0') << 7) + m;
3239 *(t--) = '0' + (i % 10);
3245 /* Explosives and implosives. */
3247 #if 'I' == 73 && 'J' == 74
3248 /* On an ASCII/ISO kind of system */
3249 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3252 Some other sort of character set - use memchr() so we don't match
3255 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3262 I32 start_sp_offset = SP - PL_stack_base;
3263 I32 gimme = GIMME_V;
3267 register char *pat = SvPV(left, llen);
3268 register char *s = SvPV(right, rlen);
3269 char *strend = s + rlen;
3271 register char *patend = pat + llen;
3277 /* These must not be in registers: */
3294 register U32 culong;
3298 #ifdef PERL_NATINT_PACK
3299 int natint; /* native integer */
3300 int unatint; /* unsigned native integer */
3303 if (gimme != G_ARRAY) { /* arrange to do first one only */
3305 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3306 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3308 while (isDIGIT(*patend) || *patend == '*')
3314 while (pat < patend) {
3316 datumtype = *pat++ & 0xFF;
3317 #ifdef PERL_NATINT_PACK
3320 if (isSPACE(datumtype))
3322 if (datumtype == '#') {
3323 while (pat < patend && *pat != '\n')
3328 char *natstr = "sSiIlL";
3330 if (strchr(natstr, datumtype)) {
3331 #ifdef PERL_NATINT_PACK
3337 DIE(aTHX_ "'!' allowed only after types %s", natstr);
3342 else if (*pat == '*') {
3343 len = strend - strbeg; /* long enough */
3347 else if (isDIGIT(*pat)) {
3349 while (isDIGIT(*pat)) {
3350 len = (len * 10) + (*pat++ - '0');
3352 DIE(aTHX_ "Repeat count in unpack overflows");
3356 len = (datumtype != '@');
3360 DIE(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
3361 case ',': /* grandfather in commas but with a warning */
3362 if (commas++ == 0 && ckWARN(WARN_UNPACK))
3363 Perl_warner(aTHX_ WARN_UNPACK,
3364 "Invalid type in unpack: '%c'", (int)datumtype);
3367 if (len == 1 && pat[-1] != '1')
3376 if (len > strend - strbeg)
3377 DIE(aTHX_ "@ outside of string");
3381 if (len > s - strbeg)
3382 DIE(aTHX_ "X outside of string");
3386 if (len > strend - s)
3387 DIE(aTHX_ "x outside of string");
3391 if (start_sp_offset >= SP - PL_stack_base)
3392 DIE(aTHX_ "/ must follow a numeric type");
3395 pat++; /* ignore '*' for compatibility with pack */
3397 DIE(aTHX_ "/ cannot take a count" );
3404 if (len > strend - s)
3407 goto uchar_checksum;
3408 sv = NEWSV(35, len);
3409 sv_setpvn(sv, s, len);
3411 if (datumtype == 'A' || datumtype == 'Z') {
3412 aptr = s; /* borrow register */
3413 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3418 else { /* 'A' strips both nulls and spaces */
3419 s = SvPVX(sv) + len - 1;
3420 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3424 SvCUR_set(sv, s - SvPVX(sv));
3425 s = aptr; /* unborrow register */
3427 XPUSHs(sv_2mortal(sv));
3431 if (star || len > (strend - s) * 8)
3432 len = (strend - s) * 8;
3435 Newz(601, PL_bitcount, 256, char);
3436 for (bits = 1; bits < 256; bits++) {
3437 if (bits & 1) PL_bitcount[bits]++;
3438 if (bits & 2) PL_bitcount[bits]++;
3439 if (bits & 4) PL_bitcount[bits]++;
3440 if (bits & 8) PL_bitcount[bits]++;
3441 if (bits & 16) PL_bitcount[bits]++;
3442 if (bits & 32) PL_bitcount[bits]++;
3443 if (bits & 64) PL_bitcount[bits]++;
3444 if (bits & 128) PL_bitcount[bits]++;
3448 culong += PL_bitcount[*(unsigned char*)s++];
3453 if (datumtype == 'b') {
3455 if (bits & 1) culong++;
3461 if (bits & 128) culong++;
3468 sv = NEWSV(35, len + 1);
3472 if (datumtype == 'b') {
3474 for (len = 0; len < aint; len++) {
3475 if (len & 7) /*SUPPRESS 595*/
3479 *str++ = '0' + (bits & 1);
3484 for (len = 0; len < aint; len++) {
3489 *str++ = '0' + ((bits & 128) != 0);
3493 XPUSHs(sv_2mortal(sv));
3497 if (star || len > (strend - s) * 2)
3498 len = (strend - s) * 2;
3499 sv = NEWSV(35, len + 1);
3503 if (datumtype == 'h') {
3505 for (len = 0; len < aint; len++) {
3510 *str++ = PL_hexdigit[bits & 15];
3515 for (len = 0; len < aint; len++) {
3520 *str++ = PL_hexdigit[(bits >> 4) & 15];
3524 XPUSHs(sv_2mortal(sv));
3527 if (len > strend - s)
3532 if (aint >= 128) /* fake up signed chars */
3542 if (aint >= 128) /* fake up signed chars */
3545 sv_setiv(sv, (IV)aint);
3546 PUSHs(sv_2mortal(sv));
3551 if (len > strend - s)
3566 sv_setiv(sv, (IV)auint);
3567 PUSHs(sv_2mortal(sv));
3572 if (len > strend - s)
3575 while (len-- > 0 && s < strend) {
3576 auint = utf8_to_uv((U8*)s, &along);
3579 cdouble += (NV)auint;
3587 while (len-- > 0 && s < strend) {
3588 auint = utf8_to_uv((U8*)s, &along);
3591 sv_setuv(sv, (UV)auint);
3592 PUSHs(sv_2mortal(sv));
3597 #if SHORTSIZE == SIZE16
3598 along = (strend - s) / SIZE16;
3600 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3605 #if SHORTSIZE != SIZE16
3609 COPYNN(s, &ashort, sizeof(short));
3620 #if SHORTSIZE > SIZE16
3632 #if SHORTSIZE != SIZE16
3636 COPYNN(s, &ashort, sizeof(short));
3639 sv_setiv(sv, (IV)ashort);
3640 PUSHs(sv_2mortal(sv));
3648 #if SHORTSIZE > SIZE16
3654 sv_setiv(sv, (IV)ashort);
3655 PUSHs(sv_2mortal(sv));
3663 #if SHORTSIZE == SIZE16
3664 along = (strend - s) / SIZE16;
3666 unatint = natint && datumtype == 'S';
3667 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3672 #if SHORTSIZE != SIZE16
3674 unsigned short aushort;
3676 COPYNN(s, &aushort, sizeof(unsigned short));
3677 s += sizeof(unsigned short);
3685 COPY16(s, &aushort);
3688 if (datumtype == 'n')
3689 aushort = PerlSock_ntohs(aushort);
3692 if (datumtype == 'v')
3693 aushort = vtohs(aushort);
3702 #if SHORTSIZE != SIZE16
3704 unsigned short aushort;
3706 COPYNN(s, &aushort, sizeof(unsigned short));
3707 s += sizeof(unsigned short);
3709 sv_setiv(sv, (UV)aushort);
3710 PUSHs(sv_2mortal(sv));
3717 COPY16(s, &aushort);
3721 if (datumtype == 'n')
3722 aushort = PerlSock_ntohs(aushort);
3725 if (datumtype == 'v')
3726 aushort = vtohs(aushort);
3728 sv_setiv(sv, (UV)aushort);
3729 PUSHs(sv_2mortal(sv));
3735 along = (strend - s) / sizeof(int);
3740 Copy(s, &aint, 1, int);
3743 cdouble += (NV)aint;
3752 Copy(s, &aint, 1, int);
3756 /* Without the dummy below unpack("i", pack("i",-1))
3757 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3758 * cc with optimization turned on.
3760 * The bug was detected in
3761 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3762 * with optimization (-O4) turned on.
3763 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3764 * does not have this problem even with -O4.
3766 * This bug was reported as DECC_BUGS 1431
3767 * and tracked internally as GEM_BUGS 7775.
3769 * The bug is fixed in
3770 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3771 * UNIX V4.0F support: DEC C V5.9-006 or later
3772 * UNIX V4.0E support: DEC C V5.8-011 or later
3775 * See also few lines later for the same bug.
3778 sv_setiv(sv, (IV)aint) :
3780 sv_setiv(sv, (IV)aint);
3781 PUSHs(sv_2mortal(sv));
3786 along = (strend - s) / sizeof(unsigned int);
3791 Copy(s, &auint, 1, unsigned int);
3792 s += sizeof(unsigned int);
3794 cdouble += (NV)auint;
3803 Copy(s, &auint, 1, unsigned int);
3804 s += sizeof(unsigned int);
3807 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3808 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3809 * See details few lines earlier. */
3811 sv_setuv(sv, (UV)auint) :
3813 sv_setuv(sv, (UV)auint);
3814 PUSHs(sv_2mortal(sv));
3819 #if LONGSIZE == SIZE32
3820 along = (strend - s) / SIZE32;
3822 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3827 #if LONGSIZE != SIZE32
3831 COPYNN(s, &along, sizeof(long));
3834 cdouble += (NV)along;
3844 #if LONGSIZE > SIZE32
3845 if (along > 2147483647)
3846 along -= 4294967296;
3850 cdouble += (NV)along;
3859 #if LONGSIZE != SIZE32
3863 COPYNN(s, &along, sizeof(long));
3866 sv_setiv(sv, (IV)along);
3867 PUSHs(sv_2mortal(sv));
3875 #if LONGSIZE > SIZE32
3876 if (along > 2147483647)
3877 along -= 4294967296;
3881 sv_setiv(sv, (IV)along);
3882 PUSHs(sv_2mortal(sv));
3890 #if LONGSIZE == SIZE32
3891 along = (strend - s) / SIZE32;
3893 unatint = natint && datumtype == 'L';
3894 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3899 #if LONGSIZE != SIZE32
3901 unsigned long aulong;
3903 COPYNN(s, &aulong, sizeof(unsigned long));
3904 s += sizeof(unsigned long);
3906 cdouble += (NV)aulong;
3918 if (datumtype == 'N')
3919 aulong = PerlSock_ntohl(aulong);
3922 if (datumtype == 'V')
3923 aulong = vtohl(aulong);
3926 cdouble += (NV)aulong;
3935 #if LONGSIZE != SIZE32
3937 unsigned long aulong;
3939 COPYNN(s, &aulong, sizeof(unsigned long));
3940 s += sizeof(unsigned long);
3942 sv_setuv(sv, (UV)aulong);
3943 PUSHs(sv_2mortal(sv));
3953 if (datumtype == 'N')
3954 aulong = PerlSock_ntohl(aulong);
3957 if (datumtype == 'V')
3958 aulong = vtohl(aulong);
3961 sv_setuv(sv, (UV)aulong);
3962 PUSHs(sv_2mortal(sv));
3968 along = (strend - s) / sizeof(char*);
3974 if (sizeof(char*) > strend - s)
3977 Copy(s, &aptr, 1, char*);
3983 PUSHs(sv_2mortal(sv));
3993 while ((len > 0) && (s < strend)) {
3994 auv = (auv << 7) | (*s & 0x7f);
3995 if (!(*s++ & 0x80)) {
3999 PUSHs(sv_2mortal(sv));
4003 else if (++bytes >= sizeof(UV)) { /* promote to string */
4007 sv = Perl_newSVpvf(aTHX_ "%.*Vu", (int)TYPE_DIGITS(UV), auv);
4008 while (s < strend) {
4009 sv = mul128(sv, *s & 0x7f);
4010 if (!(*s++ & 0x80)) {
4019 PUSHs(sv_2mortal(sv));
4024 if ((s >= strend) && bytes)
4025 DIE(aTHX_ "Unterminated compressed integer");
4030 if (sizeof(char*) > strend - s)
4033 Copy(s, &aptr, 1, char*);
4038 sv_setpvn(sv, aptr, len);
4039 PUSHs(sv_2mortal(sv));
4043 along = (strend - s) / sizeof(Quad_t);
4049 if (s + sizeof(Quad_t) > strend)
4052 Copy(s, &aquad, 1, Quad_t);
4053 s += sizeof(Quad_t);
4056 if (aquad >= IV_MIN && aquad <= IV_MAX)
4057 sv_setiv(sv, (IV)aquad);
4059 sv_setnv(sv, (NV)aquad);
4060 PUSHs(sv_2mortal(sv));
4064 along = (strend - s) / sizeof(Quad_t);
4070 if (s + sizeof(Uquad_t) > strend)
4073 Copy(s, &auquad, 1, Uquad_t);
4074 s += sizeof(Uquad_t);
4077 if (auquad <= UV_MAX)
4078 sv_setuv(sv, (UV)auquad);
4080 sv_setnv(sv, (NV)auquad);
4081 PUSHs(sv_2mortal(sv));
4085 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4088 along = (strend - s) / sizeof(float);
4093 Copy(s, &afloat, 1, float);
4102 Copy(s, &afloat, 1, float);
4105 sv_setnv(sv, (NV)afloat);
4106 PUSHs(sv_2mortal(sv));
4112 along = (strend - s) / sizeof(double);
4117 Copy(s, &adouble, 1, double);
4118 s += sizeof(double);
4126 Copy(s, &adouble, 1, double);
4127 s += sizeof(double);
4129 sv_setnv(sv, (NV)adouble);
4130 PUSHs(sv_2mortal(sv));
4136 * Initialise the decode mapping. By using a table driven
4137 * algorithm, the code will be character-set independent
4138 * (and just as fast as doing character arithmetic)
4140 if (PL_uudmap['M'] == 0) {
4143 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4144 PL_uudmap[PL_uuemap[i]] = i;
4146 * Because ' ' and '`' map to the same value,
4147 * we need to decode them both the same.
4152 along = (strend - s) * 3 / 4;
4153 sv = NEWSV(42, along);
4156 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4161 len = PL_uudmap[*s++] & 077;
4163 if (s < strend && ISUUCHAR(*s))
4164 a = PL_uudmap[*s++] & 077;
4167 if (s < strend && ISUUCHAR(*s))
4168 b = PL_uudmap[*s++] & 077;
4171 if (s < strend && ISUUCHAR(*s))
4172 c = PL_uudmap[*s++] & 077;
4175 if (s < strend && ISUUCHAR(*s))
4176 d = PL_uudmap[*s++] & 077;
4179 hunk[0] = (a << 2) | (b >> 4);
4180 hunk[1] = (b << 4) | (c >> 2);
4181 hunk[2] = (c << 6) | d;
4182 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4187 else if (s[1] == '\n') /* possible checksum byte */
4190 XPUSHs(sv_2mortal(sv));
4195 if (strchr("fFdD", datumtype) ||
4196 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4200 while (checksum >= 16) {
4204 while (checksum >= 4) {
4210 along = (1 << checksum) - 1;
4211 while (cdouble < 0.0)
4213 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
4214 sv_setnv(sv, cdouble);
4217 if (checksum < 32) {
4218 aulong = (1 << checksum) - 1;
4221 sv_setuv(sv, (UV)culong);
4223 XPUSHs(sv_2mortal(sv));
4227 if (SP - PL_stack_base == start_sp_offset && gimme == G_SCALAR)
4228 PUSHs(&PL_sv_undef);
4233 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
4237 *hunk = PL_uuemap[len];
4238 sv_catpvn(sv, hunk, 1);
4241 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4242 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4243 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4244 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4245 sv_catpvn(sv, hunk, 4);
4250 char r = (len > 1 ? s[1] : '\0');
4251 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4252 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4253 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4254 hunk[3] = PL_uuemap[0];
4255 sv_catpvn(sv, hunk, 4);
4257 sv_catpvn(sv, "\n", 1);
4261 S_is_an_int(pTHX_ char *s, STRLEN l)
4264 SV *result = newSVpvn(s, l);
4265 char *result_c = SvPV(result, n_a); /* convenience */
4266 char *out = result_c;
4276 SvREFCNT_dec(result);
4299 SvREFCNT_dec(result);
4305 SvCUR_set(result, out - result_c);
4309 /* pnum must be '\0' terminated */
4311 S_div128(pTHX_ SV *pnum, bool *done)
4314 char *s = SvPV(pnum, len);
4323 i = m * 10 + (*t - '0');
4325 r = (i >> 7); /* r < 10 */
4332 SvCUR_set(pnum, (STRLEN) (t - s));
4339 djSP; dMARK; dORIGMARK; dTARGET;
4340 register SV *cat = TARG;
4343 register char *pat = SvPVx(*++MARK, fromlen);
4344 register char *patend = pat + fromlen;
4349 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4350 static char *space10 = " ";
4352 /* These must not be in registers: */
4367 #ifdef PERL_NATINT_PACK
4368 int natint; /* native integer */
4373 sv_setpvn(cat, "", 0);
4374 while (pat < patend) {
4375 SV *lengthcode = Nullsv;
4376 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *MARK++ : &PL_sv_no)
4377 datumtype = *pat++ & 0xFF;
4378 #ifdef PERL_NATINT_PACK
4381 if (isSPACE(datumtype))
4383 if (datumtype == '#') {
4384 while (pat < patend && *pat != '\n')
4389 char *natstr = "sSiIlL";
4391 if (strchr(natstr, datumtype)) {
4392 #ifdef PERL_NATINT_PACK
4398 DIE(aTHX_ "'!' allowed only after types %s", natstr);
4401 len = strchr("@Xxu", datumtype) ? 0 : items;
4404 else if (isDIGIT(*pat)) {
4406 while (isDIGIT(*pat)) {
4407 len = (len * 10) + (*pat++ - '0');
4409 DIE(aTHX_ "Repeat count in pack overflows");
4416 if (*pat != 'a' && *pat != 'A' && *pat != 'Z' || pat[1] != '*')
4417 DIE(aTHX_ "/ must be followed by a*, A* or Z*");
4418 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
4419 ? *MARK : &PL_sv_no)));
4423 DIE(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
4424 case ',': /* grandfather in commas but with a warning */
4425 if (commas++ == 0 && ckWARN(WARN_PACK))
4426 Perl_warner(aTHX_ WARN_PACK,
4427 "Invalid type in pack: '%c'", (int)datumtype);
4430 DIE(aTHX_ "%% may only be used in unpack");
4441 if (SvCUR(cat) < len)
4442 DIE(aTHX_ "X outside of string");
4449 sv_catpvn(cat, null10, 10);
4452 sv_catpvn(cat, null10, len);
4458 aptr = SvPV(fromstr, fromlen);
4459 if (pat[-1] == '*') {
4461 if (datumtype == 'Z')
4464 if (fromlen >= len) {
4465 sv_catpvn(cat, aptr, len);
4466 if (datumtype == 'Z')
4467 *(SvEND(cat)-1) = '\0';
4470 sv_catpvn(cat, aptr, fromlen);
4472 if (datumtype == 'A') {
4474 sv_catpvn(cat, space10, 10);
4477 sv_catpvn(cat, space10, len);
4481 sv_catpvn(cat, null10, 10);
4484 sv_catpvn(cat, null10, len);
4496 str = SvPV(fromstr, fromlen);
4500 SvCUR(cat) += (len+7)/8;
4501 SvGROW(cat, SvCUR(cat) + 1);
4502 aptr = SvPVX(cat) + aint;
4507 if (datumtype == 'B') {
4508 for (len = 0; len++ < aint;) {
4509 items |= *str++ & 1;
4513 *aptr++ = items & 0xff;
4519 for (len = 0; len++ < aint;) {
4525 *aptr++ = items & 0xff;
4531 if (datumtype == 'B')
4532 items <<= 7 - (aint & 7);
4534 items >>= 7 - (aint & 7);
4535 *aptr++ = items & 0xff;
4537 str = SvPVX(cat) + SvCUR(cat);
4552 str = SvPV(fromstr, fromlen);
4556 SvCUR(cat) += (len+1)/2;
4557 SvGROW(cat, SvCUR(cat) + 1);
4558 aptr = SvPVX(cat) + aint;
4563 if (datumtype == 'H') {
4564 for (len = 0; len++ < aint;) {
4566 items |= ((*str++ & 15) + 9) & 15;
4568 items |= *str++ & 15;
4572 *aptr++ = items & 0xff;
4578 for (len = 0; len++ < aint;) {
4580 items |= (((*str++ & 15) + 9) & 15) << 4;
4582 items |= (*str++ & 15) << 4;
4586 *aptr++ = items & 0xff;
4592 *aptr++ = items & 0xff;
4593 str = SvPVX(cat) + SvCUR(cat);
4604 aint = SvIV(fromstr);
4606 sv_catpvn(cat, &achar, sizeof(char));
4612 auint = SvUV(fromstr);
4613 SvGROW(cat, SvCUR(cat) + UTF8_MAXLEN);
4614 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4619 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4624 afloat = (float)SvNV(fromstr);
4625 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4632 adouble = (double)SvNV(fromstr);
4633 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4639 ashort = (I16)SvIV(fromstr);
4641 ashort = PerlSock_htons(ashort);
4643 CAT16(cat, &ashort);
4649 ashort = (I16)SvIV(fromstr);
4651 ashort = htovs(ashort);
4653 CAT16(cat, &ashort);
4657 #if SHORTSIZE != SIZE16
4659 unsigned short aushort;
4663 aushort = SvUV(fromstr);
4664 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4674 aushort = (U16)SvUV(fromstr);
4675 CAT16(cat, &aushort);
4681 #if SHORTSIZE != SIZE16
4687 ashort = SvIV(fromstr);
4688 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4696 ashort = (I16)SvIV(fromstr);
4697 CAT16(cat, &ashort);
4704 auint = SvUV(fromstr);
4705 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4711 adouble = Perl_floor(SvNV(fromstr));
4714 DIE(aTHX_ "Cannot compress negative numbers");
4717 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4718 adouble <= UV_MAX_cxux
4724 char buf[1 + sizeof(UV)];
4725 char *in = buf + sizeof(buf);
4726 UV auv = U_V(adouble);
4729 *--in = (auv & 0x7f) | 0x80;
4732 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4733 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4735 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4736 char *from, *result, *in;
4741 /* Copy string and check for compliance */
4742 from = SvPV(fromstr, len);
4743 if ((norm = is_an_int(from, len)) == NULL)
4744 DIE(aTHX_ "can compress only unsigned integer");
4746 New('w', result, len, char);
4750 *--in = div128(norm, &done) | 0x80;
4751 result[len - 1] &= 0x7F; /* clear continue bit */
4752 sv_catpvn(cat, in, (result + len) - in);
4754 SvREFCNT_dec(norm); /* free norm */
4756 else if (SvNOKp(fromstr)) {
4757 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4758 char *in = buf + sizeof(buf);
4761 double next = floor(adouble / 128);
4762 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4763 if (--in < buf) /* this cannot happen ;-) */
4764 DIE(aTHX_ "Cannot compress integer");
4766 } while (adouble > 0);
4767 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4768 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4771 DIE(aTHX_ "Cannot compress non integer");
4777 aint = SvIV(fromstr);
4778 sv_catpvn(cat, (char*)&aint, sizeof(int));
4784 aulong = SvUV(fromstr);
4786 aulong = PerlSock_htonl(aulong);
4788 CAT32(cat, &aulong);
4794 aulong = SvUV(fromstr);
4796 aulong = htovl(aulong);
4798 CAT32(cat, &aulong);
4802 #if LONGSIZE != SIZE32
4804 unsigned long aulong;
4808 aulong = SvUV(fromstr);
4809 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4817 aulong = SvUV(fromstr);
4818 CAT32(cat, &aulong);
4823 #if LONGSIZE != SIZE32
4829 along = SvIV(fromstr);
4830 sv_catpvn(cat, (char *)&along, sizeof(long));
4838 along = SvIV(fromstr);
4847 auquad = (Uquad_t)SvUV(fromstr);
4848 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4854 aquad = (Quad_t)SvIV(fromstr);
4855 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4860 len = 1; /* assume SV is correct length */
4865 if (fromstr == &PL_sv_undef)
4869 /* XXX better yet, could spirit away the string to
4870 * a safe spot and hang on to it until the result
4871 * of pack() (and all copies of the result) are
4874 if (ckWARN(WARN_PACK) && (SvTEMP(fromstr)
4875 || (SvPADTMP(fromstr)
4876 && !SvREADONLY(fromstr))))
4878 Perl_warner(aTHX_ WARN_PACK,
4879 "Attempt to pack pointer to temporary value");
4881 if (SvPOK(fromstr) || SvNIOK(fromstr))
4882 aptr = SvPV(fromstr,n_a);
4884 aptr = SvPV_force(fromstr,n_a);
4886 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4891 aptr = SvPV(fromstr, fromlen);
4892 SvGROW(cat, fromlen * 4 / 3);
4897 while (fromlen > 0) {
4904 doencodes(cat, aptr, todo);
4923 register I32 limit = POPi; /* note, negative is forever */
4926 register char *s = SvPV(sv, len);
4927 char *strend = s + len;
4929 register REGEXP *rx;
4933 I32 maxiters = (strend - s) + 10;
4936 I32 origlimit = limit;
4939 AV *oldstack = PL_curstack;
4940 I32 gimme = GIMME_V;
4941 I32 oldsave = PL_savestack_ix;
4942 I32 make_mortal = 1;
4943 MAGIC *mg = (MAGIC *) NULL;
4946 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4951 DIE(aTHX_ "panic: do_split");
4952 rx = pm->op_pmregexp;
4954 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4955 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4957 if (pm->op_pmreplroot) {
4959 ary = GvAVn((GV*)PL_curpad[(PADOFFSET)pm->op_pmreplroot]);
4961 ary = GvAVn((GV*)pm->op_pmreplroot);
4964 else if (gimme != G_ARRAY)
4966 ary = (AV*)PL_curpad[0];
4968 ary = GvAVn(PL_defgv);
4969 #endif /* USE_THREADS */
4972 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4978 if (mg = SvTIED_mg((SV*)ary, 'P')) {
4980 XPUSHs(SvTIED_obj((SV*)ary, mg));
4986 for (i = AvFILLp(ary); i >= 0; i--)
4987 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4989 /* temporarily switch stacks */
4990 SWITCHSTACK(PL_curstack, ary);
4994 base = SP - PL_stack_base;
4996 if (pm->op_pmflags & PMf_SKIPWHITE) {
4997 if (pm->op_pmflags & PMf_LOCALE) {
4998 while (isSPACE_LC(*s))
5006 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
5007 SAVEINT(PL_multiline);
5008 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
5012 limit = maxiters + 2;
5013 if (pm->op_pmflags & PMf_WHITE) {
5016 while (m < strend &&
5017 !((pm->op_pmflags & PMf_LOCALE)
5018 ? isSPACE_LC(*m) : isSPACE(*m)))
5023 dstr = NEWSV(30, m-s);
5024 sv_setpvn(dstr, s, m-s);
5030 while (s < strend &&
5031 ((pm->op_pmflags & PMf_LOCALE)
5032 ? isSPACE_LC(*s) : isSPACE(*s)))
5036 else if (strEQ("^", rx->precomp)) {
5039 for (m = s; m < strend && *m != '\n'; m++) ;
5043 dstr = NEWSV(30, m-s);
5044 sv_setpvn(dstr, s, m-s);
5051 else if ((rx->reganch & RE_USE_INTUIT) && !rx->nparens
5052 && (rx->reganch & ROPT_CHECK_ALL)
5053 && !(rx->reganch & ROPT_ANCH)) {
5054 int tail = (rx->reganch & RE_INTUIT_TAIL);
5055 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
5059 if (len == 1 && !tail) {
5063 for (m = s; m < strend && *m != c; m++) ;
5066 dstr = NEWSV(30, m-s);
5067 sv_setpvn(dstr, s, m-s);
5076 while (s < strend && --limit &&
5077 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5078 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
5081 dstr = NEWSV(31, m-s);
5082 sv_setpvn(dstr, s, m-s);
5086 s = m + len; /* Fake \n at the end */
5091 maxiters += (strend - s) * rx->nparens;
5092 while (s < strend && --limit
5093 /* && (!rx->check_substr
5094 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
5096 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
5097 1 /* minend */, sv, NULL, 0))
5099 TAINT_IF(RX_MATCH_TAINTED(rx));
5100 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5105 strend = s + (strend - m);
5107 m = rx->startp[0] + orig;
5108 dstr = NEWSV(32, m-s);
5109 sv_setpvn(dstr, s, m-s);
5114 for (i = 1; i <= rx->nparens; i++) {
5115 s = rx->startp[i] + orig;
5116 m = rx->endp[i] + orig;
5118 dstr = NEWSV(33, m-s);
5119 sv_setpvn(dstr, s, m-s);
5122 dstr = NEWSV(33, 0);
5128 s = rx->endp[0] + orig;
5132 LEAVE_SCOPE(oldsave);
5133 iters = (SP - PL_stack_base) - base;
5134 if (iters > maxiters)
5135 DIE(aTHX_ "Split loop");
5137 /* keep field after final delim? */
5138 if (s < strend || (iters && origlimit)) {
5139 dstr = NEWSV(34, strend-s);
5140 sv_setpvn(dstr, s, strend-s);
5146 else if (!origlimit) {
5147 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5153 SWITCHSTACK(ary, oldstack);
5154 if (SvSMAGICAL(ary)) {
5159 if (gimme == G_ARRAY) {
5161 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5169 call_method("PUSH",G_SCALAR|G_DISCARD);
5172 if (gimme == G_ARRAY) {
5173 /* EXTEND should not be needed - we just popped them */
5175 for (i=0; i < iters; i++) {
5176 SV **svp = av_fetch(ary, i, FALSE);
5177 PUSHs((svp) ? *svp : &PL_sv_undef);
5184 if (gimme == G_ARRAY)
5187 if (iters || !pm->op_pmreplroot) {
5197 Perl_unlock_condpair(pTHX_ void *svv)
5200 MAGIC *mg = mg_find((SV*)svv, 'm');
5203 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
5204 MUTEX_LOCK(MgMUTEXP(mg));
5205 if (MgOWNER(mg) != thr)
5206 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
5208 COND_SIGNAL(MgOWNERCONDP(mg));
5209 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": unlock 0x%"UVxf"\n",
5210 PTR2UV(thr), PTR2UV(svv));)
5211 MUTEX_UNLOCK(MgMUTEXP(mg));
5213 #endif /* USE_THREADS */
5226 mg = condpair_magic(sv);
5227 MUTEX_LOCK(MgMUTEXP(mg));
5228 if (MgOWNER(mg) == thr)
5229 MUTEX_UNLOCK(MgMUTEXP(mg));
5232 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
5234 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": pp_lock lock 0x%"UVxf"\n",
5235 PTR2UV(thr), PTR2UV(sv));)
5236 MUTEX_UNLOCK(MgMUTEXP(mg));
5237 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
5239 #endif /* USE_THREADS */
5240 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5241 || SvTYPE(retsv) == SVt_PVCV) {
5242 retsv = refto(retsv);
5253 if (PL_op->op_private & OPpLVAL_INTRO)
5254 PUSHs(*save_threadsv(PL_op->op_targ));
5256 PUSHs(THREADSV(PL_op->op_targ));
5259 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
5260 #endif /* USE_THREADS */