3 * Copyright (c) 1991-1999, 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 * Types used in bitwise operations.
33 * Normally we'd just use IV and UV. However, some hardware and
34 * software combinations (e.g. Alpha and current OSF/1) don't have a
35 * floating-point type to use for NV that has adequate bits to fully
36 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
38 * It just so happens that "int" is the right size almost everywhere.
44 * Mask used after bitwise operations.
46 * There is at least one realm (Cray word machines) that doesn't
47 * have an integral type (except char) small enough to be represented
48 * in a double without loss; that is, it has no 32-bit type.
50 #if LONGSIZE > 4 && defined(_CRAY) && !defined(_CRAYMPP)
52 # define BW_MASK ((1 << BW_BITS) - 1)
53 # define BW_SIGN (1 << (BW_BITS - 1))
54 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
55 # define BWu(u) ((u) & BW_MASK)
62 * Offset for integer pack/unpack.
64 * On architectures where I16 and I32 aren't really 16 and 32 bits,
65 * which for now are all Crays, pack and unpack have to play games.
69 * These values are required for portability of pack() output.
70 * If they're not right on your machine, then pack() and unpack()
71 * wouldn't work right anyway; you'll need to apply the Cray hack.
72 * (I'd like to check them with #if, but you can't use sizeof() in
73 * the preprocessor.) --???
76 The appropriate SHORTSIZE, INTSIZE, LONGSIZE, and LONGLONGSIZE
77 defines are now in config.h. --Andy Dougherty April 1998
82 /* CROSSCOMPILE and MULTIARCH are going to affect pp_pack() and pp_unpack().
85 #if SHORTSIZE != SIZE16 || LONGSIZE != SIZE32
86 # define PERL_NATINT_PACK
89 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
90 # if BYTEORDER == 0x12345678
91 # define OFF16(p) (char*)(p)
92 # define OFF32(p) (char*)(p)
94 # if BYTEORDER == 0x87654321
95 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
96 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
98 }}}} bad cray byte order
101 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
102 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
103 # define COPYNN(s,p,n) (*(p) = 0, Copy(s, (char *)(p), n, char))
104 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
105 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
107 # define COPY16(s,p) Copy(s, p, SIZE16, char)
108 # define COPY32(s,p) Copy(s, p, SIZE32, char)
109 # define COPYNN(s,p,n) Copy(s, (char *)(p), n, char)
110 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
111 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
114 /* variations on pp_null */
120 /* XXX I can't imagine anyone who doesn't have this actually _needs_
121 it, since pid_t is an integral type.
124 #ifdef NEED_GETPID_PROTO
125 extern Pid_t getpid (void);
131 if (GIMME_V == G_SCALAR)
132 XPUSHs(&PL_sv_undef);
146 if (PL_op->op_private & OPpLVAL_INTRO)
147 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
149 if (PL_op->op_flags & OPf_REF) {
153 if (GIMME == G_ARRAY) {
154 I32 maxarg = AvFILL((AV*)TARG) + 1;
156 if (SvMAGICAL(TARG)) {
158 for (i=0; i < maxarg; i++) {
159 SV **svp = av_fetch((AV*)TARG, i, FALSE);
160 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
164 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
169 SV* sv = sv_newmortal();
170 I32 maxarg = AvFILL((AV*)TARG) + 1;
171 sv_setiv(sv, maxarg);
183 if (PL_op->op_private & OPpLVAL_INTRO)
184 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
185 if (PL_op->op_flags & OPf_REF)
188 if (gimme == G_ARRAY) {
191 else if (gimme == G_SCALAR) {
192 SV* sv = sv_newmortal();
193 if (HvFILL((HV*)TARG))
194 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
195 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
205 DIE(aTHX_ "NOT IMPL LINE %d",__LINE__);
216 tryAMAGICunDEREF(to_gv);
219 if (SvTYPE(sv) == SVt_PVIO) {
220 GV *gv = (GV*) sv_newmortal();
221 gv_init(gv, 0, "", 0, 0);
222 GvIOp(gv) = (IO *)sv;
223 (void)SvREFCNT_inc(sv);
226 else if (SvTYPE(sv) != SVt_PVGV)
227 DIE(aTHX_ "Not a GLOB reference");
230 if (SvTYPE(sv) != SVt_PVGV) {
234 if (SvGMAGICAL(sv)) {
240 /* If this is a 'my' scalar and flag is set then vivify
243 if (PL_op->op_private & OPpDEREF) {
244 GV *gv = (GV *) newSV(0);
247 if (cUNOP->op_first->op_type == OP_PADSV) {
248 SV *padname = *av_fetch(PL_comppad_name, cUNOP->op_first->op_targ, 4);
249 name = SvPV(padname,len);
251 gv_init(gv, PL_curcop->cop_stash, name, len, 0);
252 sv_upgrade(sv, SVt_RV);
253 SvRV(sv) = (SV *) gv;
258 if (PL_op->op_flags & OPf_REF ||
259 PL_op->op_private & HINT_STRICT_REFS)
260 DIE(aTHX_ PL_no_usym, "a symbol");
261 if (ckWARN(WARN_UNINITIALIZED))
262 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
266 if ((PL_op->op_flags & OPf_SPECIAL) &&
267 !(PL_op->op_flags & OPf_MOD))
269 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
274 if (PL_op->op_private & HINT_STRICT_REFS)
275 DIE(aTHX_ PL_no_symref, sym, "a symbol");
276 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
280 if (PL_op->op_private & OPpLVAL_INTRO)
281 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
292 tryAMAGICunDEREF(to_sv);
295 switch (SvTYPE(sv)) {
299 DIE(aTHX_ "Not a SCALAR reference");
307 if (SvTYPE(gv) != SVt_PVGV) {
308 if (SvGMAGICAL(sv)) {
314 if (PL_op->op_flags & OPf_REF ||
315 PL_op->op_private & HINT_STRICT_REFS)
316 DIE(aTHX_ PL_no_usym, "a SCALAR");
317 if (ckWARN(WARN_UNINITIALIZED))
318 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit);
322 if ((PL_op->op_flags & OPf_SPECIAL) &&
323 !(PL_op->op_flags & OPf_MOD))
325 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
330 if (PL_op->op_private & HINT_STRICT_REFS)
331 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
332 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
337 if (PL_op->op_flags & OPf_MOD) {
338 if (PL_op->op_private & OPpLVAL_INTRO)
339 sv = save_scalar((GV*)TOPs);
340 else if (PL_op->op_private & OPpDEREF)
341 vivify_ref(sv, PL_op->op_private & OPpDEREF);
351 SV *sv = AvARYLEN(av);
353 AvARYLEN(av) = sv = NEWSV(0,0);
354 sv_upgrade(sv, SVt_IV);
355 sv_magic(sv, (SV*)av, '#', Nullch, 0);
363 djSP; dTARGET; dPOPss;
365 if (PL_op->op_flags & OPf_MOD) {
366 if (SvTYPE(TARG) < SVt_PVLV) {
367 sv_upgrade(TARG, SVt_PVLV);
368 sv_magic(TARG, Nullsv, '.', Nullch, 0);
372 if (LvTARG(TARG) != sv) {
374 SvREFCNT_dec(LvTARG(TARG));
375 LvTARG(TARG) = SvREFCNT_inc(sv);
377 PUSHs(TARG); /* no SvSETMAGIC */
383 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
384 mg = mg_find(sv, 'g');
385 if (mg && mg->mg_len >= 0) {
389 PUSHi(i + PL_curcop->cop_arybase);
403 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
404 /* (But not in defined().) */
405 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
408 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
411 cv = (CV*)&PL_sv_undef;
425 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
426 char *s = SvPVX(TOPs);
427 if (strnEQ(s, "CORE::", 6)) {
430 code = keyword(s + 6, SvCUR(TOPs) - 6);
431 if (code < 0) { /* Overridable. */
432 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
433 int i = 0, n = 0, seen_question = 0;
435 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
437 while (i < MAXO) { /* The slow way. */
438 if (strEQ(s + 6, PL_op_name[i])
439 || strEQ(s + 6, PL_op_desc[i]))
445 goto nonesuch; /* Should not happen... */
447 oa = PL_opargs[i] >> OASHIFT;
449 if (oa & OA_OPTIONAL) {
453 else if (seen_question)
454 goto set; /* XXXX system, exec */
455 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
456 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
459 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
460 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
464 ret = sv_2mortal(newSVpvn(str, n - 1));
466 else if (code) /* Non-Overridable */
468 else { /* None such */
470 Perl_croak(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
474 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
476 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
485 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
487 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
503 if (GIMME != G_ARRAY) {
507 *MARK = &PL_sv_undef;
508 *MARK = refto(*MARK);
512 EXTEND_MORTAL(SP - MARK);
514 *MARK = refto(*MARK);
519 S_refto(pTHX_ SV *sv)
523 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
526 if (!(sv = LvTARG(sv)))
529 (void)SvREFCNT_inc(sv);
531 else if (SvPADTMP(sv))
535 (void)SvREFCNT_inc(sv);
538 sv_upgrade(rv, SVt_RV);
552 if (sv && SvGMAGICAL(sv))
555 if (!sv || !SvROK(sv))
559 pv = sv_reftype(sv,TRUE);
560 PUSHp(pv, strlen(pv));
570 stash = PL_curcop->cop_stash;
574 char *ptr = SvPV(ssv,len);
575 if (ckWARN(WARN_UNSAFE) && len == 0)
576 Perl_warner(aTHX_ WARN_UNSAFE,
577 "Explicit blessing to '' (assuming package main)");
578 stash = gv_stashpvn(ptr, len, TRUE);
581 (void)sv_bless(TOPs, stash);
595 elem = SvPV(sv, n_a);
599 switch (elem ? *elem : '\0')
602 if (strEQ(elem, "ARRAY"))
603 tmpRef = (SV*)GvAV(gv);
606 if (strEQ(elem, "CODE"))
607 tmpRef = (SV*)GvCVu(gv);
610 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
611 tmpRef = (SV*)GvIOp(gv);
614 if (strEQ(elem, "GLOB"))
618 if (strEQ(elem, "HASH"))
619 tmpRef = (SV*)GvHV(gv);
622 if (strEQ(elem, "IO"))
623 tmpRef = (SV*)GvIOp(gv);
626 if (strEQ(elem, "NAME"))
627 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
630 if (strEQ(elem, "PACKAGE"))
631 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
634 if (strEQ(elem, "SCALAR"))
648 /* Pattern matching */
653 register unsigned char *s;
656 register I32 *sfirst;
660 if (sv == PL_lastscream) {
666 SvSCREAM_off(PL_lastscream);
667 SvREFCNT_dec(PL_lastscream);
669 PL_lastscream = SvREFCNT_inc(sv);
672 s = (unsigned char*)(SvPV(sv, len));
676 if (pos > PL_maxscream) {
677 if (PL_maxscream < 0) {
678 PL_maxscream = pos + 80;
679 New(301, PL_screamfirst, 256, I32);
680 New(302, PL_screamnext, PL_maxscream, I32);
683 PL_maxscream = pos + pos / 4;
684 Renew(PL_screamnext, PL_maxscream, I32);
688 sfirst = PL_screamfirst;
689 snext = PL_screamnext;
691 if (!sfirst || !snext)
692 DIE(aTHX_ "do_study: out of memory");
694 for (ch = 256; ch; --ch)
701 snext[pos] = sfirst[ch] - pos;
708 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
717 if (PL_op->op_flags & OPf_STACKED)
723 TARG = sv_newmortal();
728 /* Lvalue operators. */
740 djSP; dMARK; dTARGET;
750 SETi(do_chomp(TOPs));
756 djSP; dMARK; dTARGET;
757 register I32 count = 0;
760 count += do_chomp(POPs);
771 if (!sv || !SvANY(sv))
773 switch (SvTYPE(sv)) {
775 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
779 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
783 if (CvROOT(sv) || CvXSUB(sv))
800 if (!PL_op->op_private) {
809 if (SvTHINKFIRST(sv))
812 switch (SvTYPE(sv)) {
822 if (ckWARN(WARN_UNSAFE) && cv_const_sv((CV*)sv))
823 Perl_warner(aTHX_ WARN_UNSAFE, "Constant subroutine %s undefined",
824 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
828 /* let user-undef'd sub keep its identity */
829 GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
836 SvSetMagicSV(sv, &PL_sv_undef);
840 Newz(602, gp, 1, GP);
841 GvGP(sv) = gp_ref(gp);
842 GvSV(sv) = NEWSV(72,0);
843 GvLINE(sv) = PL_curcop->cop_line;
849 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
852 SvPV_set(sv, Nullch);
865 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
866 Perl_croak(aTHX_ PL_no_modify);
867 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
868 SvIVX(TOPs) != IV_MIN)
871 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
882 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
883 Perl_croak(aTHX_ PL_no_modify);
884 sv_setsv(TARG, TOPs);
885 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
886 SvIVX(TOPs) != IV_MAX)
889 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
903 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
904 Perl_croak(aTHX_ PL_no_modify);
905 sv_setsv(TARG, TOPs);
906 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
907 SvIVX(TOPs) != IV_MIN)
910 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
919 /* Ordinary operators. */
923 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
926 SETn( pow( left, right) );
933 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
936 SETn( left * right );
943 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
948 DIE(aTHX_ "Illegal division by zero");
950 /* insure that 20./5. == 4. */
953 if ((double)I_V(left) == left &&
954 (double)I_V(right) == right &&
955 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
959 value = left / right;
963 value = left / right;
972 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
982 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
984 right = (right_neg = (i < 0)) ? -i : i;
989 right_neg = dright < 0;
994 if (!use_double && SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
996 left = (left_neg = (i < 0)) ? -i : i;
1004 left_neg = dleft < 0;
1013 /* Somehow U_V is pessimized even if CASTFLAGS is 0 */
1015 # define CAST_D2UV(d) U_V(d)
1017 # define CAST_D2UV(d) ((UV)(d))
1019 /* Tried to do this only in the case DOUBLESIZE <= UV_SIZE,
1020 * or, in other words, precision of UV more than of NV.
1021 * But in fact the approach below turned out to be an
1022 * optimization - floor() may be slow */
1023 if (dright <= UV_MAX && dleft <= UV_MAX) {
1024 right = CAST_D2UV(dright);
1025 left = CAST_D2UV(dleft);
1030 /* Backward-compatibility clause: */
1031 dright = floor(dright + 0.5);
1032 dleft = floor(dleft + 0.5);
1035 DIE(aTHX_ "Illegal modulus zero");
1037 dans = fmod(dleft, dright);
1038 if ((left_neg != right_neg) && dans)
1039 dans = dright - dans;
1042 sv_setnv(TARG, dans);
1049 DIE(aTHX_ "Illegal modulus zero");
1052 if ((left_neg != right_neg) && ans)
1055 /* XXX may warn: unary minus operator applied to unsigned type */
1056 /* could change -foo to be (~foo)+1 instead */
1057 if (ans <= ~((UV)IV_MAX)+1)
1058 sv_setiv(TARG, ~ans+1);
1060 sv_setnv(TARG, -(double)ans);
1063 sv_setuv(TARG, ans);
1072 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1074 register I32 count = POPi;
1075 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1077 I32 items = SP - MARK;
1080 max = items * count;
1089 repeatcpy((char*)(MARK + items), (char*)MARK,
1090 items * sizeof(SV*), count - 1);
1093 else if (count <= 0)
1096 else { /* Note: mark already snarfed by pp_list */
1101 SvSetSV(TARG, tmpstr);
1102 SvPV_force(TARG, len);
1107 SvGROW(TARG, (count * len) + 1);
1108 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1109 SvCUR(TARG) *= count;
1111 *SvEND(TARG) = '\0';
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) {
1137 i = BWi(i) << shift;
1151 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1154 if (PL_op->op_private & HINT_INTEGER) {
1156 i = BWi(i) >> shift;
1170 djSP; tryAMAGICbinSET(lt,0);
1173 SETs(boolSV(TOPn < value));
1180 djSP; tryAMAGICbinSET(gt,0);
1183 SETs(boolSV(TOPn > value));
1190 djSP; tryAMAGICbinSET(le,0);
1193 SETs(boolSV(TOPn <= value));
1200 djSP; tryAMAGICbinSET(ge,0);
1203 SETs(boolSV(TOPn >= value));
1210 djSP; tryAMAGICbinSET(ne,0);
1213 SETs(boolSV(TOPn != value));
1220 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1227 else if (left < right)
1229 else if (left > right)
1242 djSP; tryAMAGICbinSET(slt,0);
1245 int cmp = ((PL_op->op_private & OPpLOCALE)
1246 ? sv_cmp_locale(left, right)
1247 : sv_cmp(left, right));
1248 SETs(boolSV(cmp < 0));
1255 djSP; tryAMAGICbinSET(sgt,0);
1258 int cmp = ((PL_op->op_private & OPpLOCALE)
1259 ? sv_cmp_locale(left, right)
1260 : sv_cmp(left, right));
1261 SETs(boolSV(cmp > 0));
1268 djSP; tryAMAGICbinSET(sle,0);
1271 int cmp = ((PL_op->op_private & OPpLOCALE)
1272 ? sv_cmp_locale(left, right)
1273 : sv_cmp(left, right));
1274 SETs(boolSV(cmp <= 0));
1281 djSP; tryAMAGICbinSET(sge,0);
1284 int cmp = ((PL_op->op_private & OPpLOCALE)
1285 ? sv_cmp_locale(left, right)
1286 : sv_cmp(left, right));
1287 SETs(boolSV(cmp >= 0));
1294 djSP; tryAMAGICbinSET(seq,0);
1297 SETs(boolSV(sv_eq(left, right)));
1304 djSP; tryAMAGICbinSET(sne,0);
1307 SETs(boolSV(!sv_eq(left, right)));
1314 djSP; dTARGET; tryAMAGICbin(scmp,0);
1317 int cmp = ((PL_op->op_private & OPpLOCALE)
1318 ? sv_cmp_locale(left, right)
1319 : sv_cmp(left, right));
1327 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1330 if (SvNIOKp(left) || SvNIOKp(right)) {
1331 if (PL_op->op_private & HINT_INTEGER) {
1332 IBW value = SvIV(left) & SvIV(right);
1336 UBW value = SvUV(left) & SvUV(right);
1341 do_vop(PL_op->op_type, TARG, left, right);
1350 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1353 if (SvNIOKp(left) || SvNIOKp(right)) {
1354 if (PL_op->op_private & HINT_INTEGER) {
1355 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1359 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1364 do_vop(PL_op->op_type, TARG, left, right);
1373 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1376 if (SvNIOKp(left) || SvNIOKp(right)) {
1377 if (PL_op->op_private & HINT_INTEGER) {
1378 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1382 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1387 do_vop(PL_op->op_type, TARG, left, right);
1396 djSP; dTARGET; tryAMAGICun(neg);
1401 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1403 else if (SvNIOKp(sv))
1405 else if (SvPOKp(sv)) {
1407 char *s = SvPV(sv, len);
1408 if (isIDFIRST(*s)) {
1409 sv_setpvn(TARG, "-", 1);
1412 else if (*s == '+' || *s == '-') {
1414 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1416 else if (IN_UTF8 && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1417 sv_setpvn(TARG, "-", 1);
1421 sv_setnv(TARG, -SvNV(sv));
1432 djSP; tryAMAGICunSET(not);
1433 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1439 djSP; dTARGET; tryAMAGICun(compl);
1443 if (PL_op->op_private & HINT_INTEGER) {
1444 IBW value = ~SvIV(sv);
1448 UBW value = ~SvUV(sv);
1453 register char *tmps;
1454 register long *tmpl;
1459 tmps = SvPV_force(TARG, len);
1462 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1465 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1469 for ( ; anum > 0; anum--, tmps++)
1478 /* integer versions of some of the above */
1482 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1485 SETi( left * right );
1492 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1496 DIE(aTHX_ "Illegal division by zero");
1497 value = POPi / value;
1505 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1509 DIE(aTHX_ "Illegal modulus zero");
1510 SETi( left % right );
1517 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1520 SETi( left + right );
1527 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1530 SETi( left - right );
1537 djSP; tryAMAGICbinSET(lt,0);
1540 SETs(boolSV(left < right));
1547 djSP; tryAMAGICbinSET(gt,0);
1550 SETs(boolSV(left > right));
1557 djSP; tryAMAGICbinSET(le,0);
1560 SETs(boolSV(left <= right));
1567 djSP; tryAMAGICbinSET(ge,0);
1570 SETs(boolSV(left >= right));
1577 djSP; tryAMAGICbinSET(eq,0);
1580 SETs(boolSV(left == right));
1587 djSP; tryAMAGICbinSET(ne,0);
1590 SETs(boolSV(left != right));
1597 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1604 else if (left < right)
1615 djSP; dTARGET; tryAMAGICun(neg);
1620 /* High falutin' math. */
1624 djSP; dTARGET; tryAMAGICbin(atan2,0);
1627 SETn(atan2(left, right));
1634 djSP; dTARGET; tryAMAGICun(sin);
1646 djSP; dTARGET; tryAMAGICun(cos);
1656 /* Support Configure command-line overrides for rand() functions.
1657 After 5.005, perhaps we should replace this by Configure support
1658 for drand48(), random(), or rand(). For 5.005, though, maintain
1659 compatibility by calling rand() but allow the user to override it.
1660 See INSTALL for details. --Andy Dougherty 15 July 1998
1662 /* Now it's after 5.005, and Configure supports drand48() and random(),
1663 in addition to rand(). So the overrides should not be needed any more.
1664 --Jarkko Hietaniemi 27 September 1998
1667 #ifndef HAS_DRAND48_PROTO
1668 extern double drand48 (void);
1681 if (!PL_srand_called) {
1682 (void)seedDrand01((Rand_seed_t)seed());
1683 PL_srand_called = TRUE;
1698 (void)seedDrand01((Rand_seed_t)anum);
1699 PL_srand_called = TRUE;
1708 * This is really just a quick hack which grabs various garbage
1709 * values. It really should be a real hash algorithm which
1710 * spreads the effect of every input bit onto every output bit,
1711 * if someone who knows about such things would bother to write it.
1712 * Might be a good idea to add that function to CORE as well.
1713 * No numbers below come from careful analysis or anything here,
1714 * except they are primes and SEED_C1 > 1E6 to get a full-width
1715 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1716 * probably be bigger too.
1719 # define SEED_C1 1000003
1720 #define SEED_C4 73819
1722 # define SEED_C1 25747
1723 #define SEED_C4 20639
1727 #define SEED_C5 26107
1730 #ifndef PERL_NO_DEV_RANDOM
1735 # include <starlet.h>
1736 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1737 * in 100-ns units, typically incremented ever 10 ms. */
1738 unsigned int when[2];
1740 # ifdef HAS_GETTIMEOFDAY
1741 struct timeval when;
1747 /* This test is an escape hatch, this symbol isn't set by Configure. */
1748 #ifndef PERL_NO_DEV_RANDOM
1749 #ifndef PERL_RANDOM_DEVICE
1750 /* /dev/random isn't used by default because reads from it will block
1751 * if there isn't enough entropy available. You can compile with
1752 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1753 * is enough real entropy to fill the seed. */
1754 # define PERL_RANDOM_DEVICE "/dev/urandom"
1756 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1758 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1767 _ckvmssts(sys$gettim(when));
1768 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1770 # ifdef HAS_GETTIMEOFDAY
1771 gettimeofday(&when,(struct timezone *) 0);
1772 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1775 u = (U32)SEED_C1 * when;
1778 u += SEED_C3 * (U32)getpid();
1779 u += SEED_C4 * (U32)(UV)PL_stack_sp;
1780 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1781 u += SEED_C5 * (U32)(UV)&when;
1788 djSP; dTARGET; tryAMAGICun(exp);
1800 djSP; dTARGET; tryAMAGICun(log);
1805 RESTORE_NUMERIC_STANDARD();
1806 DIE(aTHX_ "Can't take log of %g", value);
1816 djSP; dTARGET; tryAMAGICun(sqrt);
1821 RESTORE_NUMERIC_STANDARD();
1822 DIE(aTHX_ "Can't take sqrt of %g", value);
1824 value = sqrt(value);
1834 double value = TOPn;
1837 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1843 (void)modf(value, &value);
1845 (void)modf(-value, &value);
1860 djSP; dTARGET; tryAMAGICun(abs);
1862 double value = TOPn;
1865 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1866 (iv = SvIVX(TOPs)) != IV_MIN) {
1888 XPUSHu(scan_hex(tmps, 99, &argtype));
1901 while (*tmps && isSPACE(*tmps))
1906 value = scan_hex(++tmps, 99, &argtype);
1907 else if (*tmps == 'b')
1908 value = scan_bin(++tmps, 99, &argtype);
1910 value = scan_oct(tmps, 99, &argtype);
1922 SETi( sv_len_utf8(TOPs) );
1926 SETi( sv_len(TOPs) );
1940 I32 lvalue = PL_op->op_flags & OPf_MOD;
1942 I32 arybase = PL_curcop->cop_arybase;
1946 SvTAINTED_off(TARG); /* decontaminate */
1950 repl = SvPV(sv, repl_len);
1957 tmps = SvPV(sv, curlen);
1959 utfcurlen = sv_len_utf8(sv);
1960 if (utfcurlen == curlen)
1968 if (pos >= arybase) {
1986 else if (len >= 0) {
1988 if (rem > (I32)curlen)
2002 if (ckWARN(WARN_SUBSTR) || lvalue || repl)
2003 Perl_warner(aTHX_ WARN_SUBSTR, "substr outside of string");
2008 sv_pos_u2b(sv, &pos, &rem);
2010 sv_setpvn(TARG, tmps, rem);
2011 if (lvalue) { /* it's an lvalue! */
2012 if (!SvGMAGICAL(sv)) {
2016 if (ckWARN(WARN_SUBSTR))
2017 Perl_warner(aTHX_ WARN_SUBSTR,
2018 "Attempt to use reference as lvalue in substr");
2020 if (SvOK(sv)) /* is it defined ? */
2021 (void)SvPOK_only(sv);
2023 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2026 if (SvTYPE(TARG) < SVt_PVLV) {
2027 sv_upgrade(TARG, SVt_PVLV);
2028 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
2032 if (LvTARG(TARG) != sv) {
2034 SvREFCNT_dec(LvTARG(TARG));
2035 LvTARG(TARG) = SvREFCNT_inc(sv);
2037 LvTARGOFF(TARG) = pos;
2038 LvTARGLEN(TARG) = rem;
2041 sv_insert(sv, pos, rem, repl, repl_len);
2044 PUSHs(TARG); /* avoid SvSETMAGIC here */
2051 register I32 size = POPi;
2052 register I32 offset = POPi;
2053 register SV *src = POPs;
2054 I32 lvalue = PL_op->op_flags & OPf_MOD;
2056 unsigned char *s = (unsigned char*)SvPV(src, srclen);
2057 unsigned long retnum;
2060 SvTAINTED_off(TARG); /* decontaminate */
2061 offset *= size; /* turn into bit offset */
2062 len = (offset + size + 7) / 8;
2063 if (offset < 0 || size < 1)
2066 if (lvalue) { /* it's an lvalue! */
2067 if (SvTYPE(TARG) < SVt_PVLV) {
2068 sv_upgrade(TARG, SVt_PVLV);
2069 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2073 if (LvTARG(TARG) != src) {
2075 SvREFCNT_dec(LvTARG(TARG));
2076 LvTARG(TARG) = SvREFCNT_inc(src);
2078 LvTARGOFF(TARG) = offset;
2079 LvTARGLEN(TARG) = size;
2087 if (offset >= srclen)
2090 retnum = (unsigned long) s[offset] << 8;
2092 else if (size == 32) {
2093 if (offset >= srclen)
2095 else if (offset + 1 >= srclen)
2096 retnum = (unsigned long) s[offset] << 24;
2097 else if (offset + 2 >= srclen)
2098 retnum = ((unsigned long) s[offset] << 24) +
2099 ((unsigned long) s[offset + 1] << 16);
2101 retnum = ((unsigned long) s[offset] << 24) +
2102 ((unsigned long) s[offset + 1] << 16) +
2103 (s[offset + 2] << 8);
2108 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
2113 else if (size == 16)
2114 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
2115 else if (size == 32)
2116 retnum = ((unsigned long) s[offset] << 24) +
2117 ((unsigned long) s[offset + 1] << 16) +
2118 (s[offset + 2] << 8) + s[offset+3];
2122 sv_setuv(TARG, (UV)retnum);
2137 I32 arybase = PL_curcop->cop_arybase;
2142 offset = POPi - arybase;
2145 tmps = SvPV(big, biglen);
2146 if (IN_UTF8 && offset > 0)
2147 sv_pos_u2b(big, &offset, 0);
2150 else if (offset > biglen)
2152 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2153 (unsigned char*)tmps + biglen, little, 0)))
2156 retval = tmps2 - tmps;
2157 if (IN_UTF8 && retval > 0)
2158 sv_pos_b2u(big, &retval);
2159 PUSHi(retval + arybase);
2174 I32 arybase = PL_curcop->cop_arybase;
2180 tmps2 = SvPV(little, llen);
2181 tmps = SvPV(big, blen);
2185 if (IN_UTF8 && offset > 0)
2186 sv_pos_u2b(big, &offset, 0);
2187 offset = offset - arybase + llen;
2191 else if (offset > blen)
2193 if (!(tmps2 = rninstr(tmps, tmps + offset,
2194 tmps2, tmps2 + llen)))
2197 retval = tmps2 - tmps;
2198 if (IN_UTF8 && retval > 0)
2199 sv_pos_b2u(big, &retval);
2200 PUSHi(retval + arybase);
2206 djSP; dMARK; dORIGMARK; dTARGET;
2207 do_sprintf(TARG, SP-MARK, MARK+1);
2208 TAINT_IF(SvTAINTED(TARG));
2219 U8 *tmps = (U8*)POPpx;
2222 if (IN_UTF8 && (*tmps & 0x80))
2223 value = utf8_to_uv(tmps, &retlen);
2225 value = (UV)(*tmps & 255);
2236 (void)SvUPGRADE(TARG,SVt_PV);
2238 if (IN_UTF8 && value >= 128) {
2241 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2242 SvCUR_set(TARG, tmps - SvPVX(TARG));
2244 (void)SvPOK_only(TARG);
2254 (void)SvPOK_only(TARG);
2261 djSP; dTARGET; dPOPTOPssrl;
2264 char *tmps = SvPV(left, n_a);
2266 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2268 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2272 "The crypt() function is unimplemented due to excessive paranoia.");
2285 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2289 UV uv = utf8_to_uv(s, &ulen);
2291 if (PL_op->op_private & OPpLOCALE) {
2294 uv = toTITLE_LC_uni(uv);
2297 uv = toTITLE_utf8(s);
2299 tend = uv_to_utf8(tmpbuf, uv);
2301 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2303 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2304 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2308 s = (U8*)SvPV_force(sv, slen);
2309 Copy(tmpbuf, s, ulen, U8);
2313 if (!SvPADTMP(sv)) {
2319 s = (U8*)SvPV_force(sv, slen);
2321 if (PL_op->op_private & OPpLOCALE) {
2324 *s = toUPPER_LC(*s);
2342 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2346 UV uv = utf8_to_uv(s, &ulen);
2348 if (PL_op->op_private & OPpLOCALE) {
2351 uv = toLOWER_LC_uni(uv);
2354 uv = toLOWER_utf8(s);
2356 tend = uv_to_utf8(tmpbuf, uv);
2358 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2360 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2361 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2365 s = (U8*)SvPV_force(sv, slen);
2366 Copy(tmpbuf, s, ulen, U8);
2370 if (!SvPADTMP(sv)) {
2376 s = (U8*)SvPV_force(sv, slen);
2378 if (PL_op->op_private & OPpLOCALE) {
2381 *s = toLOWER_LC(*s);
2406 s = (U8*)SvPV(sv,len);
2408 sv_setpvn(TARG, "", 0);
2412 (void)SvUPGRADE(TARG, SVt_PV);
2413 SvGROW(TARG, (len * 2) + 1);
2414 (void)SvPOK_only(TARG);
2415 d = (U8*)SvPVX(TARG);
2417 if (PL_op->op_private & OPpLOCALE) {
2421 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2427 d = uv_to_utf8(d, toUPPER_utf8( s ));
2432 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2437 if (!SvPADTMP(sv)) {
2443 s = (U8*)SvPV_force(sv, len);
2445 register U8 *send = s + len;
2447 if (PL_op->op_private & OPpLOCALE) {
2450 for (; s < send; s++)
2451 *s = toUPPER_LC(*s);
2454 for (; s < send; s++)
2477 s = (U8*)SvPV(sv,len);
2479 sv_setpvn(TARG, "", 0);
2483 (void)SvUPGRADE(TARG, SVt_PV);
2484 SvGROW(TARG, (len * 2) + 1);
2485 (void)SvPOK_only(TARG);
2486 d = (U8*)SvPVX(TARG);
2488 if (PL_op->op_private & OPpLOCALE) {
2492 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2498 d = uv_to_utf8(d, toLOWER_utf8(s));
2503 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2508 if (!SvPADTMP(sv)) {
2515 s = (U8*)SvPV_force(sv, len);
2517 register U8 *send = s + len;
2519 if (PL_op->op_private & OPpLOCALE) {
2522 for (; s < send; s++)
2523 *s = toLOWER_LC(*s);
2526 for (; s < send; s++)
2541 register char *s = SvPV(sv,len);
2545 (void)SvUPGRADE(TARG, SVt_PV);
2546 SvGROW(TARG, (len * 2) + 1);
2551 STRLEN ulen = UTF8SKIP(s);
2574 SvCUR_set(TARG, d - SvPVX(TARG));
2575 (void)SvPOK_only(TARG);
2578 sv_setpvn(TARG, s, len);
2580 if (SvSMAGICAL(TARG))
2589 djSP; dMARK; dORIGMARK;
2591 register AV* av = (AV*)POPs;
2592 register I32 lval = PL_op->op_flags & OPf_MOD;
2593 I32 arybase = PL_curcop->cop_arybase;
2596 if (SvTYPE(av) == SVt_PVAV) {
2597 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2599 for (svp = MARK + 1; svp <= SP; svp++) {
2604 if (max > AvMAX(av))
2607 while (++MARK <= SP) {
2608 elem = SvIVx(*MARK);
2612 svp = av_fetch(av, elem, lval);
2614 if (!svp || *svp == &PL_sv_undef)
2615 DIE(aTHX_ PL_no_aelem, elem);
2616 if (PL_op->op_private & OPpLVAL_INTRO)
2617 save_aelem(av, elem, svp);
2619 *MARK = svp ? *svp : &PL_sv_undef;
2622 if (GIMME != G_ARRAY) {
2630 /* Associative arrays. */
2635 HV *hash = (HV*)POPs;
2637 I32 gimme = GIMME_V;
2638 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2641 /* might clobber stack_sp */
2642 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2647 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2648 if (gimme == G_ARRAY) {
2650 /* might clobber stack_sp */
2651 sv_setsv(TARG, realhv ?
2652 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2657 else if (gimme == G_SCALAR)
2676 I32 gimme = GIMME_V;
2677 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2681 if (PL_op->op_private & OPpSLICE) {
2685 hvtype = SvTYPE(hv);
2686 while (++MARK <= SP) {
2687 if (hvtype == SVt_PVHV)
2688 sv = hv_delete_ent(hv, *MARK, discard, 0);
2690 DIE(aTHX_ "Not a HASH reference");
2691 *MARK = sv ? sv : &PL_sv_undef;
2695 else if (gimme == G_SCALAR) {
2704 if (SvTYPE(hv) == SVt_PVHV)
2705 sv = hv_delete_ent(hv, keysv, discard, 0);
2707 DIE(aTHX_ "Not a HASH reference");
2721 if (SvTYPE(hv) == SVt_PVHV) {
2722 if (hv_exists_ent(hv, tmpsv, 0))
2725 else if (SvTYPE(hv) == SVt_PVAV) {
2726 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2730 DIE(aTHX_ "Not a HASH reference");
2737 djSP; dMARK; dORIGMARK;
2738 register HV *hv = (HV*)POPs;
2739 register I32 lval = PL_op->op_flags & OPf_MOD;
2740 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2742 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2743 DIE(aTHX_ "Can't localize pseudo-hash element");
2745 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2746 while (++MARK <= SP) {
2750 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2751 svp = he ? &HeVAL(he) : 0;
2754 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2757 if (!svp || *svp == &PL_sv_undef) {
2759 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2761 if (PL_op->op_private & OPpLVAL_INTRO)
2762 save_helem(hv, keysv, svp);
2764 *MARK = svp ? *svp : &PL_sv_undef;
2767 if (GIMME != G_ARRAY) {
2775 /* List operators. */
2780 if (GIMME != G_ARRAY) {
2782 *MARK = *SP; /* unwanted list, return last item */
2784 *MARK = &PL_sv_undef;
2793 SV **lastrelem = PL_stack_sp;
2794 SV **lastlelem = PL_stack_base + POPMARK;
2795 SV **firstlelem = PL_stack_base + POPMARK + 1;
2796 register SV **firstrelem = lastlelem + 1;
2797 I32 arybase = PL_curcop->cop_arybase;
2798 I32 lval = PL_op->op_flags & OPf_MOD;
2799 I32 is_something_there = lval;
2801 register I32 max = lastrelem - lastlelem;
2802 register SV **lelem;
2805 if (GIMME != G_ARRAY) {
2806 ix = SvIVx(*lastlelem);
2811 if (ix < 0 || ix >= max)
2812 *firstlelem = &PL_sv_undef;
2814 *firstlelem = firstrelem[ix];
2820 SP = firstlelem - 1;
2824 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2830 if (ix < 0 || ix >= max)
2831 *lelem = &PL_sv_undef;
2833 is_something_there = TRUE;
2834 if (!(*lelem = firstrelem[ix]))
2835 *lelem = &PL_sv_undef;
2838 if (is_something_there)
2841 SP = firstlelem - 1;
2847 djSP; dMARK; dORIGMARK;
2848 I32 items = SP - MARK;
2849 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2850 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2857 djSP; dMARK; dORIGMARK;
2858 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2862 SV *val = NEWSV(46, 0);
2864 sv_setsv(val, *++MARK);
2865 else if (ckWARN(WARN_UNSAFE))
2866 Perl_warner(aTHX_ WARN_UNSAFE, "Odd number of elements in hash assignment");
2867 (void)hv_store_ent(hv,key,val,0);
2876 djSP; dMARK; dORIGMARK;
2877 register AV *ary = (AV*)*++MARK;
2881 register I32 offset;
2882 register I32 length;
2889 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2890 *MARK-- = SvTIED_obj((SV*)ary, mg);
2894 call_method("SPLICE",GIMME_V);
2903 offset = i = SvIVx(*MARK);
2905 offset += AvFILLp(ary) + 1;
2907 offset -= PL_curcop->cop_arybase;
2909 DIE(aTHX_ PL_no_aelem, i);
2911 length = SvIVx(*MARK++);
2913 length += AvFILLp(ary) - offset + 1;
2919 length = AvMAX(ary) + 1; /* close enough to infinity */
2923 length = AvMAX(ary) + 1;
2925 if (offset > AvFILLp(ary) + 1)
2926 offset = AvFILLp(ary) + 1;
2927 after = AvFILLp(ary) + 1 - (offset + length);
2928 if (after < 0) { /* not that much array */
2929 length += after; /* offset+length now in array */
2935 /* At this point, MARK .. SP-1 is our new LIST */
2938 diff = newlen - length;
2939 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2942 if (diff < 0) { /* shrinking the area */
2944 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2945 Copy(MARK, tmparyval, newlen, SV*);
2948 MARK = ORIGMARK + 1;
2949 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2950 MEXTEND(MARK, length);
2951 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2953 EXTEND_MORTAL(length);
2954 for (i = length, dst = MARK; i; i--) {
2955 sv_2mortal(*dst); /* free them eventualy */
2962 *MARK = AvARRAY(ary)[offset+length-1];
2965 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2966 SvREFCNT_dec(*dst++); /* free them now */
2969 AvFILLp(ary) += diff;
2971 /* pull up or down? */
2973 if (offset < after) { /* easier to pull up */
2974 if (offset) { /* esp. if nothing to pull */
2975 src = &AvARRAY(ary)[offset-1];
2976 dst = src - diff; /* diff is negative */
2977 for (i = offset; i > 0; i--) /* can't trust Copy */
2981 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2985 if (after) { /* anything to pull down? */
2986 src = AvARRAY(ary) + offset + length;
2987 dst = src + diff; /* diff is negative */
2988 Move(src, dst, after, SV*);
2990 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2991 /* avoid later double free */
2995 dst[--i] = &PL_sv_undef;
2998 for (src = tmparyval, dst = AvARRAY(ary) + offset;
3000 *dst = NEWSV(46, 0);
3001 sv_setsv(*dst++, *src++);
3003 Safefree(tmparyval);
3006 else { /* no, expanding (or same) */
3008 New(452, tmparyval, length, SV*); /* so remember deletion */
3009 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
3012 if (diff > 0) { /* expanding */
3014 /* push up or down? */
3016 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
3020 Move(src, dst, offset, SV*);
3022 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
3024 AvFILLp(ary) += diff;
3027 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
3028 av_extend(ary, AvFILLp(ary) + diff);
3029 AvFILLp(ary) += diff;
3032 dst = AvARRAY(ary) + AvFILLp(ary);
3034 for (i = after; i; i--) {
3041 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
3042 *dst = NEWSV(46, 0);
3043 sv_setsv(*dst++, *src++);
3045 MARK = ORIGMARK + 1;
3046 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3048 Copy(tmparyval, MARK, length, SV*);
3050 EXTEND_MORTAL(length);
3051 for (i = length, dst = MARK; i; i--) {
3052 sv_2mortal(*dst); /* free them eventualy */
3056 Safefree(tmparyval);
3060 else if (length--) {
3061 *MARK = tmparyval[length];
3064 while (length-- > 0)
3065 SvREFCNT_dec(tmparyval[length]);
3067 Safefree(tmparyval);
3070 *MARK = &PL_sv_undef;
3078 djSP; dMARK; dORIGMARK; dTARGET;
3079 register AV *ary = (AV*)*++MARK;
3080 register SV *sv = &PL_sv_undef;
3083 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3084 *MARK-- = SvTIED_obj((SV*)ary, mg);
3088 call_method("PUSH",G_SCALAR|G_DISCARD);
3093 /* Why no pre-extend of ary here ? */
3094 for (++MARK; MARK <= SP; MARK++) {
3097 sv_setsv(sv, *MARK);
3102 PUSHi( AvFILL(ary) + 1 );
3110 SV *sv = av_pop(av);
3112 (void)sv_2mortal(sv);
3121 SV *sv = av_shift(av);
3126 (void)sv_2mortal(sv);
3133 djSP; dMARK; dORIGMARK; dTARGET;
3134 register AV *ary = (AV*)*++MARK;
3139 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3140 *MARK-- = SvTIED_obj((SV*)ary, mg);
3144 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3149 av_unshift(ary, SP - MARK);
3152 sv_setsv(sv, *++MARK);
3153 (void)av_store(ary, i++, sv);
3157 PUSHi( AvFILL(ary) + 1 );
3167 if (GIMME == G_ARRAY) {
3178 register char *down;
3184 do_join(TARG, &PL_sv_no, MARK, SP);
3186 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3187 up = SvPV_force(TARG, len);
3189 if (IN_UTF8) { /* first reverse each character */
3190 U8* s = (U8*)SvPVX(TARG);
3191 U8* send = (U8*)(s + len);
3200 down = (char*)(s - 1);
3201 if (s > send || !((*down & 0xc0) == 0x80)) {
3202 Perl_warn(aTHX_ "Malformed UTF-8 character");
3214 down = SvPVX(TARG) + len - 1;
3220 (void)SvPOK_only(TARG);
3229 S_mul128(pTHX_ SV *sv, U8 m)
3232 char *s = SvPV(sv, len);
3236 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3237 SV *tmpNew = newSVpvn("0000000000", 10);
3239 sv_catsv(tmpNew, sv);
3240 SvREFCNT_dec(sv); /* free old sv */
3245 while (!*t) /* trailing '\0'? */
3248 i = ((*t - '0') << 7) + m;
3249 *(t--) = '0' + (i % 10);
3255 /* Explosives and implosives. */
3257 #if 'I' == 73 && 'J' == 74
3258 /* On an ASCII/ISO kind of system */
3259 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3262 Some other sort of character set - use memchr() so we don't match
3265 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3273 I32 gimme = GIMME_V;
3277 register char *pat = SvPV(left, llen);
3278 register char *s = SvPV(right, rlen);
3279 char *strend = s + rlen;
3281 register char *patend = pat + llen;
3286 /* These must not be in registers: */
3303 register U32 culong;
3306 #ifdef PERL_NATINT_PACK
3307 int natint; /* native integer */
3308 int unatint; /* unsigned native integer */
3311 if (gimme != G_ARRAY) { /* arrange to do first one only */
3313 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3314 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3316 while (isDIGIT(*patend) || *patend == '*')
3322 while (pat < patend) {
3324 datumtype = *pat++ & 0xFF;
3325 #ifdef PERL_NATINT_PACK
3328 if (isSPACE(datumtype))
3331 char *natstr = "sSiIlL";
3333 if (strchr(natstr, datumtype)) {
3334 #ifdef PERL_NATINT_PACK
3340 Perl_croak(aTHX_ "'!' allowed only after types %s", natstr);
3344 else if (*pat == '*') {
3345 len = strend - strbeg; /* long enough */
3348 else if (isDIGIT(*pat)) {
3350 while (isDIGIT(*pat))
3351 len = (len * 10) + (*pat++ - '0');
3354 len = (datumtype != '@');
3357 Perl_croak(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
3358 case ',': /* grandfather in commas but with a warning */
3359 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
3360 Perl_warner(aTHX_ WARN_UNSAFE, "Invalid type in unpack: '%c'", (int)datumtype);
3363 if (len == 1 && pat[-1] != '1')
3372 if (len > strend - strbeg)
3373 DIE(aTHX_ "@ outside of string");
3377 if (len > s - strbeg)
3378 DIE(aTHX_ "X outside of string");
3382 if (len > strend - s)
3383 DIE(aTHX_ "x outside of string");
3389 if (len > strend - s)
3392 goto uchar_checksum;
3393 sv = NEWSV(35, len);
3394 sv_setpvn(sv, s, len);
3396 if (datumtype == 'A' || datumtype == 'Z') {
3397 aptr = s; /* borrow register */
3398 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3403 else { /* 'A' strips both nulls and spaces */
3404 s = SvPVX(sv) + len - 1;
3405 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3409 SvCUR_set(sv, s - SvPVX(sv));
3410 s = aptr; /* unborrow register */
3412 XPUSHs(sv_2mortal(sv));
3416 if (pat[-1] == '*' || len > (strend - s) * 8)
3417 len = (strend - s) * 8;
3420 Newz(601, PL_bitcount, 256, char);
3421 for (bits = 1; bits < 256; bits++) {
3422 if (bits & 1) PL_bitcount[bits]++;
3423 if (bits & 2) PL_bitcount[bits]++;
3424 if (bits & 4) PL_bitcount[bits]++;
3425 if (bits & 8) PL_bitcount[bits]++;
3426 if (bits & 16) PL_bitcount[bits]++;
3427 if (bits & 32) PL_bitcount[bits]++;
3428 if (bits & 64) PL_bitcount[bits]++;
3429 if (bits & 128) PL_bitcount[bits]++;
3433 culong += PL_bitcount[*(unsigned char*)s++];
3438 if (datumtype == 'b') {
3440 if (bits & 1) culong++;
3446 if (bits & 128) culong++;
3453 sv = NEWSV(35, len + 1);
3456 aptr = pat; /* borrow register */
3458 if (datumtype == 'b') {
3460 for (len = 0; len < aint; len++) {
3461 if (len & 7) /*SUPPRESS 595*/
3465 *pat++ = '0' + (bits & 1);
3470 for (len = 0; len < aint; len++) {
3475 *pat++ = '0' + ((bits & 128) != 0);
3479 pat = aptr; /* unborrow register */
3480 XPUSHs(sv_2mortal(sv));
3484 if (pat[-1] == '*' || len > (strend - s) * 2)
3485 len = (strend - s) * 2;
3486 sv = NEWSV(35, len + 1);
3489 aptr = pat; /* borrow register */
3491 if (datumtype == 'h') {
3493 for (len = 0; len < aint; len++) {
3498 *pat++ = PL_hexdigit[bits & 15];
3503 for (len = 0; len < aint; len++) {
3508 *pat++ = PL_hexdigit[(bits >> 4) & 15];
3512 pat = aptr; /* unborrow register */
3513 XPUSHs(sv_2mortal(sv));
3516 if (len > strend - s)
3521 if (aint >= 128) /* fake up signed chars */
3531 if (aint >= 128) /* fake up signed chars */
3534 sv_setiv(sv, (IV)aint);
3535 PUSHs(sv_2mortal(sv));
3540 if (len > strend - s)
3555 sv_setiv(sv, (IV)auint);
3556 PUSHs(sv_2mortal(sv));
3561 if (len > strend - s)
3564 while (len-- > 0 && s < strend) {
3565 auint = utf8_to_uv((U8*)s, &along);
3568 cdouble += (double)auint;
3576 while (len-- > 0 && s < strend) {
3577 auint = utf8_to_uv((U8*)s, &along);
3580 sv_setuv(sv, (UV)auint);
3581 PUSHs(sv_2mortal(sv));
3586 #if SHORTSIZE == SIZE16
3587 along = (strend - s) / SIZE16;
3589 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3594 #if SHORTSIZE != SIZE16
3597 COPYNN(s, &ashort, sizeof(short));
3608 #if SHORTSIZE > SIZE16
3620 #if SHORTSIZE != SIZE16
3623 COPYNN(s, &ashort, sizeof(short));
3626 sv_setiv(sv, (IV)ashort);
3627 PUSHs(sv_2mortal(sv));
3635 #if SHORTSIZE > SIZE16
3641 sv_setiv(sv, (IV)ashort);
3642 PUSHs(sv_2mortal(sv));
3650 #if SHORTSIZE == SIZE16
3651 along = (strend - s) / SIZE16;
3653 unatint = natint && datumtype == 'S';
3654 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3659 #if SHORTSIZE != SIZE16
3662 COPYNN(s, &aushort, sizeof(unsigned short));
3663 s += sizeof(unsigned short);
3671 COPY16(s, &aushort);
3674 if (datumtype == 'n')
3675 aushort = PerlSock_ntohs(aushort);
3678 if (datumtype == 'v')
3679 aushort = vtohs(aushort);
3688 #if SHORTSIZE != SIZE16
3691 COPYNN(s, &aushort, sizeof(unsigned short));
3692 s += sizeof(unsigned short);
3694 sv_setiv(sv, (UV)aushort);
3695 PUSHs(sv_2mortal(sv));
3702 COPY16(s, &aushort);
3706 if (datumtype == 'n')
3707 aushort = PerlSock_ntohs(aushort);
3710 if (datumtype == 'v')
3711 aushort = vtohs(aushort);
3713 sv_setiv(sv, (UV)aushort);
3714 PUSHs(sv_2mortal(sv));
3720 along = (strend - s) / sizeof(int);
3725 Copy(s, &aint, 1, int);
3728 cdouble += (double)aint;
3737 Copy(s, &aint, 1, int);
3741 /* Without the dummy below unpack("i", pack("i",-1))
3742 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3743 * cc with optimization turned on.
3745 * The bug was detected in
3746 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3747 * with optimization (-O4) turned on.
3748 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3749 * does not have this problem even with -O4.
3751 * This bug was reported as DECC_BUGS 1431
3752 * and tracked internally as GEM_BUGS 7775.
3754 * The bug is fixed in
3755 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3756 * UNIX V4.0F support: DEC C V5.9-006 or later
3757 * UNIX V4.0E support: DEC C V5.8-011 or later
3760 * See also few lines later for the same bug.
3763 sv_setiv(sv, (IV)aint) :
3765 sv_setiv(sv, (IV)aint);
3766 PUSHs(sv_2mortal(sv));
3771 along = (strend - s) / sizeof(unsigned int);
3776 Copy(s, &auint, 1, unsigned int);
3777 s += sizeof(unsigned int);
3779 cdouble += (double)auint;
3788 Copy(s, &auint, 1, unsigned int);
3789 s += sizeof(unsigned int);
3792 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3793 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3794 * See details few lines earlier. */
3796 sv_setuv(sv, (UV)auint) :
3798 sv_setuv(sv, (UV)auint);
3799 PUSHs(sv_2mortal(sv));
3804 #if LONGSIZE == SIZE32
3805 along = (strend - s) / SIZE32;
3807 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3812 #if LONGSIZE != SIZE32
3815 COPYNN(s, &along, sizeof(long));
3818 cdouble += (double)along;
3828 #if LONGSIZE > SIZE32
3829 if (along > 2147483647)
3830 along -= 4294967296;
3834 cdouble += (double)along;
3843 #if LONGSIZE != SIZE32
3846 COPYNN(s, &along, sizeof(long));
3849 sv_setiv(sv, (IV)along);
3850 PUSHs(sv_2mortal(sv));
3858 #if LONGSIZE > SIZE32
3859 if (along > 2147483647)
3860 along -= 4294967296;
3864 sv_setiv(sv, (IV)along);
3865 PUSHs(sv_2mortal(sv));
3873 #if LONGSIZE == SIZE32
3874 along = (strend - s) / SIZE32;
3876 unatint = natint && datumtype == 'L';
3877 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3882 #if LONGSIZE != SIZE32
3885 COPYNN(s, &aulong, sizeof(unsigned long));
3886 s += sizeof(unsigned long);
3888 cdouble += (double)aulong;
3900 if (datumtype == 'N')
3901 aulong = PerlSock_ntohl(aulong);
3904 if (datumtype == 'V')
3905 aulong = vtohl(aulong);
3908 cdouble += (double)aulong;
3917 #if LONGSIZE != SIZE32
3920 COPYNN(s, &aulong, sizeof(unsigned long));
3921 s += sizeof(unsigned long);
3923 sv_setuv(sv, (UV)aulong);
3924 PUSHs(sv_2mortal(sv));
3934 if (datumtype == 'N')
3935 aulong = PerlSock_ntohl(aulong);
3938 if (datumtype == 'V')
3939 aulong = vtohl(aulong);
3942 sv_setuv(sv, (UV)aulong);
3943 PUSHs(sv_2mortal(sv));
3949 along = (strend - s) / sizeof(char*);
3955 if (sizeof(char*) > strend - s)
3958 Copy(s, &aptr, 1, char*);
3964 PUSHs(sv_2mortal(sv));
3974 while ((len > 0) && (s < strend)) {
3975 auv = (auv << 7) | (*s & 0x7f);
3976 if (!(*s++ & 0x80)) {
3980 PUSHs(sv_2mortal(sv));
3984 else if (++bytes >= sizeof(UV)) { /* promote to string */
3988 sv = Perl_newSVpvf(aTHX_ "%.*Vu", (int)TYPE_DIGITS(UV), auv);
3989 while (s < strend) {
3990 sv = mul128(sv, *s & 0x7f);
3991 if (!(*s++ & 0x80)) {
4000 PUSHs(sv_2mortal(sv));
4005 if ((s >= strend) && bytes)
4006 Perl_croak(aTHX_ "Unterminated compressed integer");
4011 if (sizeof(char*) > strend - s)
4014 Copy(s, &aptr, 1, char*);
4019 sv_setpvn(sv, aptr, len);
4020 PUSHs(sv_2mortal(sv));
4024 along = (strend - s) / sizeof(Quad_t);
4030 if (s + sizeof(Quad_t) > strend)
4033 Copy(s, &aquad, 1, Quad_t);
4034 s += sizeof(Quad_t);
4037 if (aquad >= IV_MIN && aquad <= IV_MAX)
4038 sv_setiv(sv, (IV)aquad);
4040 sv_setnv(sv, (double)aquad);
4041 PUSHs(sv_2mortal(sv));
4045 along = (strend - s) / sizeof(Quad_t);
4051 if (s + sizeof(Uquad_t) > strend)
4054 Copy(s, &auquad, 1, Uquad_t);
4055 s += sizeof(Uquad_t);
4058 if (auquad <= UV_MAX)
4059 sv_setuv(sv, (UV)auquad);
4061 sv_setnv(sv, (double)auquad);
4062 PUSHs(sv_2mortal(sv));
4066 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4069 along = (strend - s) / sizeof(float);
4074 Copy(s, &afloat, 1, float);
4083 Copy(s, &afloat, 1, float);
4086 sv_setnv(sv, (double)afloat);
4087 PUSHs(sv_2mortal(sv));
4093 along = (strend - s) / sizeof(double);
4098 Copy(s, &adouble, 1, double);
4099 s += sizeof(double);
4107 Copy(s, &adouble, 1, double);
4108 s += sizeof(double);
4110 sv_setnv(sv, (double)adouble);
4111 PUSHs(sv_2mortal(sv));
4117 * Initialise the decode mapping. By using a table driven
4118 * algorithm, the code will be character-set independent
4119 * (and just as fast as doing character arithmetic)
4121 if (PL_uudmap['M'] == 0) {
4124 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4125 PL_uudmap[PL_uuemap[i]] = i;
4127 * Because ' ' and '`' map to the same value,
4128 * we need to decode them both the same.
4133 along = (strend - s) * 3 / 4;
4134 sv = NEWSV(42, along);
4137 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4142 len = PL_uudmap[*s++] & 077;
4144 if (s < strend && ISUUCHAR(*s))
4145 a = PL_uudmap[*s++] & 077;
4148 if (s < strend && ISUUCHAR(*s))
4149 b = PL_uudmap[*s++] & 077;
4152 if (s < strend && ISUUCHAR(*s))
4153 c = PL_uudmap[*s++] & 077;
4156 if (s < strend && ISUUCHAR(*s))
4157 d = PL_uudmap[*s++] & 077;
4160 hunk[0] = (a << 2) | (b >> 4);
4161 hunk[1] = (b << 4) | (c >> 2);
4162 hunk[2] = (c << 6) | d;
4163 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4168 else if (s[1] == '\n') /* possible checksum byte */
4171 XPUSHs(sv_2mortal(sv));
4176 if (strchr("fFdD", datumtype) ||
4177 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4181 while (checksum >= 16) {
4185 while (checksum >= 4) {
4191 along = (1 << checksum) - 1;
4192 while (cdouble < 0.0)
4194 cdouble = modf(cdouble / adouble, &trouble) * adouble;
4195 sv_setnv(sv, cdouble);
4198 if (checksum < 32) {
4199 aulong = (1 << checksum) - 1;
4202 sv_setuv(sv, (UV)culong);
4204 XPUSHs(sv_2mortal(sv));
4208 if (SP == oldsp && gimme == G_SCALAR)
4209 PUSHs(&PL_sv_undef);
4214 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
4218 *hunk = PL_uuemap[len];
4219 sv_catpvn(sv, hunk, 1);
4222 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4223 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4224 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4225 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4226 sv_catpvn(sv, hunk, 4);
4231 char r = (len > 1 ? s[1] : '\0');
4232 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4233 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4234 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4235 hunk[3] = PL_uuemap[0];
4236 sv_catpvn(sv, hunk, 4);
4238 sv_catpvn(sv, "\n", 1);
4242 S_is_an_int(pTHX_ char *s, STRLEN l)
4245 SV *result = newSVpvn(s, l);
4246 char *result_c = SvPV(result, n_a); /* convenience */
4247 char *out = result_c;
4257 SvREFCNT_dec(result);
4280 SvREFCNT_dec(result);
4286 SvCUR_set(result, out - result_c);
4290 /* pnum must be '\0' terminated */
4292 S_div128(pTHX_ SV *pnum, bool *done)
4295 char *s = SvPV(pnum, len);
4304 i = m * 10 + (*t - '0');
4306 r = (i >> 7); /* r < 10 */
4313 SvCUR_set(pnum, (STRLEN) (t - s));
4320 djSP; dMARK; dORIGMARK; dTARGET;
4321 register SV *cat = TARG;
4324 register char *pat = SvPVx(*++MARK, fromlen);
4325 register char *patend = pat + fromlen;
4330 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4331 static char *space10 = " ";
4333 /* These must not be in registers: */
4348 #ifdef PERL_NATINT_PACK
4349 int natint; /* native integer */
4354 sv_setpvn(cat, "", 0);
4355 while (pat < patend) {
4356 #define NEXTFROM (items-- > 0 ? *MARK++ : &PL_sv_no)
4357 datumtype = *pat++ & 0xFF;
4358 #ifdef PERL_NATINT_PACK
4361 if (isSPACE(datumtype))
4364 char *natstr = "sSiIlL";
4366 if (strchr(natstr, datumtype)) {
4367 #ifdef PERL_NATINT_PACK
4373 Perl_croak(aTHX_ "'!' allowed only after types %s", natstr);
4376 len = strchr("@Xxu", datumtype) ? 0 : items;
4379 else if (isDIGIT(*pat)) {
4381 while (isDIGIT(*pat))
4382 len = (len * 10) + (*pat++ - '0');
4388 Perl_croak(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
4389 case ',': /* grandfather in commas but with a warning */
4390 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
4391 Perl_warner(aTHX_ WARN_UNSAFE, "Invalid type in pack: '%c'", (int)datumtype);
4394 DIE(aTHX_ "%% may only be used in unpack");
4405 if (SvCUR(cat) < len)
4406 DIE(aTHX_ "X outside of string");
4413 sv_catpvn(cat, null10, 10);
4416 sv_catpvn(cat, null10, len);
4422 aptr = SvPV(fromstr, fromlen);
4426 sv_catpvn(cat, aptr, len);
4428 sv_catpvn(cat, aptr, fromlen);
4430 if (datumtype == 'A') {
4432 sv_catpvn(cat, space10, 10);
4435 sv_catpvn(cat, space10, len);
4439 sv_catpvn(cat, null10, 10);
4442 sv_catpvn(cat, null10, len);
4449 char *savepat = pat;
4454 aptr = SvPV(fromstr, fromlen);
4459 SvCUR(cat) += (len+7)/8;
4460 SvGROW(cat, SvCUR(cat) + 1);
4461 aptr = SvPVX(cat) + aint;
4466 if (datumtype == 'B') {
4467 for (len = 0; len++ < aint;) {
4468 items |= *pat++ & 1;
4472 *aptr++ = items & 0xff;
4478 for (len = 0; len++ < aint;) {
4484 *aptr++ = items & 0xff;
4490 if (datumtype == 'B')
4491 items <<= 7 - (aint & 7);
4493 items >>= 7 - (aint & 7);
4494 *aptr++ = items & 0xff;
4496 pat = SvPVX(cat) + SvCUR(cat);
4507 char *savepat = pat;
4512 aptr = SvPV(fromstr, fromlen);
4517 SvCUR(cat) += (len+1)/2;
4518 SvGROW(cat, SvCUR(cat) + 1);
4519 aptr = SvPVX(cat) + aint;
4524 if (datumtype == 'H') {
4525 for (len = 0; len++ < aint;) {
4527 items |= ((*pat++ & 15) + 9) & 15;
4529 items |= *pat++ & 15;
4533 *aptr++ = items & 0xff;
4539 for (len = 0; len++ < aint;) {
4541 items |= (((*pat++ & 15) + 9) & 15) << 4;
4543 items |= (*pat++ & 15) << 4;
4547 *aptr++ = items & 0xff;
4553 *aptr++ = items & 0xff;
4554 pat = SvPVX(cat) + SvCUR(cat);
4566 aint = SvIV(fromstr);
4568 sv_catpvn(cat, &achar, sizeof(char));
4574 auint = SvUV(fromstr);
4575 SvGROW(cat, SvCUR(cat) + 10);
4576 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4581 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4586 afloat = (float)SvNV(fromstr);
4587 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4594 adouble = (double)SvNV(fromstr);
4595 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4601 ashort = (I16)SvIV(fromstr);
4603 ashort = PerlSock_htons(ashort);
4605 CAT16(cat, &ashort);
4611 ashort = (I16)SvIV(fromstr);
4613 ashort = htovs(ashort);
4615 CAT16(cat, &ashort);
4619 #if SHORTSIZE != SIZE16
4621 unsigned short aushort;
4625 aushort = SvUV(fromstr);
4626 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4636 aushort = (U16)SvUV(fromstr);
4637 CAT16(cat, &aushort);
4643 #if SHORTSIZE != SIZE16
4647 ashort = SvIV(fromstr);
4648 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4656 ashort = (I16)SvIV(fromstr);
4657 CAT16(cat, &ashort);
4664 auint = SvUV(fromstr);
4665 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4671 adouble = floor(SvNV(fromstr));
4674 Perl_croak(aTHX_ "Cannot compress negative numbers");
4680 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4681 adouble <= UV_MAX_cxux
4688 char buf[1 + sizeof(UV)];
4689 char *in = buf + sizeof(buf);
4690 UV auv = U_V(adouble);
4693 *--in = (auv & 0x7f) | 0x80;
4696 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4697 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4699 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4700 char *from, *result, *in;
4705 /* Copy string and check for compliance */
4706 from = SvPV(fromstr, len);
4707 if ((norm = is_an_int(from, len)) == NULL)
4708 Perl_croak(aTHX_ "can compress only unsigned integer");
4710 New('w', result, len, char);
4714 *--in = div128(norm, &done) | 0x80;
4715 result[len - 1] &= 0x7F; /* clear continue bit */
4716 sv_catpvn(cat, in, (result + len) - in);
4718 SvREFCNT_dec(norm); /* free norm */
4720 else if (SvNOKp(fromstr)) {
4721 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4722 char *in = buf + sizeof(buf);
4725 double next = floor(adouble / 128);
4726 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4727 if (--in < buf) /* this cannot happen ;-) */
4728 Perl_croak(aTHX_ "Cannot compress integer");
4730 } while (adouble > 0);
4731 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4732 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4735 Perl_croak(aTHX_ "Cannot compress non integer");
4741 aint = SvIV(fromstr);
4742 sv_catpvn(cat, (char*)&aint, sizeof(int));
4748 aulong = SvUV(fromstr);
4750 aulong = PerlSock_htonl(aulong);
4752 CAT32(cat, &aulong);
4758 aulong = SvUV(fromstr);
4760 aulong = htovl(aulong);
4762 CAT32(cat, &aulong);
4766 #if LONGSIZE != SIZE32
4770 aulong = SvUV(fromstr);
4771 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4779 aulong = SvUV(fromstr);
4780 CAT32(cat, &aulong);
4785 #if LONGSIZE != SIZE32
4789 along = SvIV(fromstr);
4790 sv_catpvn(cat, (char *)&along, sizeof(long));
4798 along = SvIV(fromstr);
4807 auquad = (Uquad_t)SvIV(fromstr);
4808 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4814 aquad = (Quad_t)SvIV(fromstr);
4815 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4818 #endif /* HAS_QUAD */
4820 len = 1; /* assume SV is correct length */
4825 if (fromstr == &PL_sv_undef)
4829 /* XXX better yet, could spirit away the string to
4830 * a safe spot and hang on to it until the result
4831 * of pack() (and all copies of the result) are
4834 if (ckWARN(WARN_UNSAFE) && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4835 Perl_warner(aTHX_ WARN_UNSAFE,
4836 "Attempt to pack pointer to temporary value");
4837 if (SvPOK(fromstr) || SvNIOK(fromstr))
4838 aptr = SvPV(fromstr,n_a);
4840 aptr = SvPV_force(fromstr,n_a);
4842 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4847 aptr = SvPV(fromstr, fromlen);
4848 SvGROW(cat, fromlen * 4 / 3);
4853 while (fromlen > 0) {
4860 doencodes(cat, aptr, todo);
4879 register I32 limit = POPi; /* note, negative is forever */
4882 register char *s = SvPV(sv, len);
4883 char *strend = s + len;
4885 register REGEXP *rx;
4889 I32 maxiters = (strend - s) + 10;
4892 I32 origlimit = limit;
4895 AV *oldstack = PL_curstack;
4896 I32 gimme = GIMME_V;
4897 I32 oldsave = PL_savestack_ix;
4898 I32 make_mortal = 1;
4899 MAGIC *mg = (MAGIC *) NULL;
4902 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4907 DIE(aTHX_ "panic: do_split");
4908 rx = pm->op_pmregexp;
4910 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4911 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4913 if (pm->op_pmreplroot)
4914 ary = GvAVn((GV*)pm->op_pmreplroot);
4915 else if (gimme != G_ARRAY)
4917 ary = (AV*)PL_curpad[0];
4919 ary = GvAVn(PL_defgv);
4920 #endif /* USE_THREADS */
4923 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4929 if (mg = SvTIED_mg((SV*)ary, 'P')) {
4931 XPUSHs(SvTIED_obj((SV*)ary, mg));
4936 for (i = AvFILLp(ary); i >= 0; i--)
4937 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4939 /* temporarily switch stacks */
4940 SWITCHSTACK(PL_curstack, ary);
4944 base = SP - PL_stack_base;
4946 if (pm->op_pmflags & PMf_SKIPWHITE) {
4947 if (pm->op_pmflags & PMf_LOCALE) {
4948 while (isSPACE_LC(*s))
4956 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4957 SAVEINT(PL_multiline);
4958 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4962 limit = maxiters + 2;
4963 if (pm->op_pmflags & PMf_WHITE) {
4966 while (m < strend &&
4967 !((pm->op_pmflags & PMf_LOCALE)
4968 ? isSPACE_LC(*m) : isSPACE(*m)))
4973 dstr = NEWSV(30, m-s);
4974 sv_setpvn(dstr, s, m-s);
4980 while (s < strend &&
4981 ((pm->op_pmflags & PMf_LOCALE)
4982 ? isSPACE_LC(*s) : isSPACE(*s)))
4986 else if (strEQ("^", rx->precomp)) {
4989 for (m = s; m < strend && *m != '\n'; m++) ;
4993 dstr = NEWSV(30, m-s);
4994 sv_setpvn(dstr, s, m-s);
5001 else if (rx->check_substr && !rx->nparens
5002 && (rx->reganch & ROPT_CHECK_ALL)
5003 && !(rx->reganch & ROPT_ANCH)) {
5004 int tail = SvTAIL(rx->check_substr) != 0;
5006 i = SvCUR(rx->check_substr);
5007 if (i == 1 && !tail) {
5008 i = *SvPVX(rx->check_substr);
5011 for (m = s; m < strend && *m != i; m++) ;
5014 dstr = NEWSV(30, m-s);
5015 sv_setpvn(dstr, s, m-s);
5024 while (s < strend && --limit &&
5025 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
5026 rx->check_substr, PL_multiline ? FBMrf_MULTILINE : 0)) )
5029 dstr = NEWSV(31, m-s);
5030 sv_setpvn(dstr, s, m-s);
5034 s = m + i - tail; /* Fake \n at the end */
5039 maxiters += (strend - s) * rx->nparens;
5040 while (s < strend && --limit &&
5041 CALLREGEXEC(aTHX_ rx, s, strend, orig, 1, sv, NULL, 0))
5043 TAINT_IF(RX_MATCH_TAINTED(rx));
5044 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5049 strend = s + (strend - m);
5051 m = rx->startp[0] + orig;
5052 dstr = NEWSV(32, m-s);
5053 sv_setpvn(dstr, s, m-s);
5058 for (i = 1; i <= rx->nparens; i++) {
5059 s = rx->startp[i] + orig;
5060 m = rx->endp[i] + orig;
5062 dstr = NEWSV(33, m-s);
5063 sv_setpvn(dstr, s, m-s);
5066 dstr = NEWSV(33, 0);
5072 s = rx->endp[0] + orig;
5076 LEAVE_SCOPE(oldsave);
5077 iters = (SP - PL_stack_base) - base;
5078 if (iters > maxiters)
5079 DIE(aTHX_ "Split loop");
5081 /* keep field after final delim? */
5082 if (s < strend || (iters && origlimit)) {
5083 dstr = NEWSV(34, strend-s);
5084 sv_setpvn(dstr, s, strend-s);
5090 else if (!origlimit) {
5091 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5097 SWITCHSTACK(ary, oldstack);
5098 if (SvSMAGICAL(ary)) {
5103 if (gimme == G_ARRAY) {
5105 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5113 call_method("PUSH",G_SCALAR|G_DISCARD);
5116 if (gimme == G_ARRAY) {
5117 /* EXTEND should not be needed - we just popped them */
5119 for (i=0; i < iters; i++) {
5120 SV **svp = av_fetch(ary, i, FALSE);
5121 PUSHs((svp) ? *svp : &PL_sv_undef);
5128 if (gimme == G_ARRAY)
5131 if (iters || !pm->op_pmreplroot) {
5141 Perl_unlock_condpair(pTHX_ void *svv)
5144 MAGIC *mg = mg_find((SV*)svv, 'm');
5147 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
5148 MUTEX_LOCK(MgMUTEXP(mg));
5149 if (MgOWNER(mg) != thr)
5150 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
5152 COND_SIGNAL(MgOWNERCONDP(mg));
5153 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
5154 (unsigned long)thr, (unsigned long)svv);)
5155 MUTEX_UNLOCK(MgMUTEXP(mg));
5157 #endif /* USE_THREADS */
5170 mg = condpair_magic(sv);
5171 MUTEX_LOCK(MgMUTEXP(mg));
5172 if (MgOWNER(mg) == thr)
5173 MUTEX_UNLOCK(MgMUTEXP(mg));
5176 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
5178 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
5179 (unsigned long)thr, (unsigned long)sv);)
5180 MUTEX_UNLOCK(MgMUTEXP(mg));
5181 save_destructor(Perl_unlock_condpair, sv);
5183 #endif /* USE_THREADS */
5184 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5185 || SvTYPE(retsv) == SVt_PVCV) {
5186 retsv = refto(retsv);
5197 if (PL_op->op_private & OPpLVAL_INTRO)
5198 PUSHs(*save_threadsv(PL_op->op_targ));
5200 PUSHs(THREADSV(PL_op->op_targ));
5203 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
5204 #endif /* USE_THREADS */