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 ((NV)I_V(left) == left &&
954 (NV)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 = Perl_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, -(NV)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(Perl_atan2(left, right));
1634 djSP; dTARGET; tryAMAGICun(sin);
1638 value = Perl_sin(value);
1646 djSP; dTARGET; tryAMAGICun(cos);
1650 value = Perl_cos(value);
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);
1792 value = Perl_exp(value);
1800 djSP; dTARGET; tryAMAGICun(log);
1805 RESTORE_NUMERIC_STANDARD();
1806 DIE(aTHX_ "Can't take log of %g", value);
1808 value = Perl_log(value);
1816 djSP; dTARGET; tryAMAGICun(sqrt);
1821 RESTORE_NUMERIC_STANDARD();
1822 DIE(aTHX_ "Can't take sqrt of %g", value);
1824 value = Perl_sqrt(value);
1837 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1843 (void)Perl_modf(value, &value);
1845 (void)Perl_modf(-value, &value);
1860 djSP; dTARGET; tryAMAGICun(abs);
1865 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1866 (iv = SvIVX(TOPs)) != IV_MIN) {
1888 XPUSHn(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 SvTAINTED_off(TARG); /* decontaminate */
2057 if (lvalue) { /* it's an lvalue! */
2058 if (SvTYPE(TARG) < SVt_PVLV) {
2059 sv_upgrade(TARG, SVt_PVLV);
2060 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2063 if (LvTARG(TARG) != src) {
2065 SvREFCNT_dec(LvTARG(TARG));
2066 LvTARG(TARG) = SvREFCNT_inc(src);
2068 LvTARGOFF(TARG) = offset;
2069 LvTARGLEN(TARG) = size;
2072 sv_setuv(TARG, do_vecget(src, offset, size));
2087 I32 arybase = PL_curcop->cop_arybase;
2092 offset = POPi - arybase;
2095 tmps = SvPV(big, biglen);
2096 if (IN_UTF8 && offset > 0)
2097 sv_pos_u2b(big, &offset, 0);
2100 else if (offset > biglen)
2102 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2103 (unsigned char*)tmps + biglen, little, 0)))
2106 retval = tmps2 - tmps;
2107 if (IN_UTF8 && retval > 0)
2108 sv_pos_b2u(big, &retval);
2109 PUSHi(retval + arybase);
2124 I32 arybase = PL_curcop->cop_arybase;
2130 tmps2 = SvPV(little, llen);
2131 tmps = SvPV(big, blen);
2135 if (IN_UTF8 && offset > 0)
2136 sv_pos_u2b(big, &offset, 0);
2137 offset = offset - arybase + llen;
2141 else if (offset > blen)
2143 if (!(tmps2 = rninstr(tmps, tmps + offset,
2144 tmps2, tmps2 + llen)))
2147 retval = tmps2 - tmps;
2148 if (IN_UTF8 && retval > 0)
2149 sv_pos_b2u(big, &retval);
2150 PUSHi(retval + arybase);
2156 djSP; dMARK; dORIGMARK; dTARGET;
2157 do_sprintf(TARG, SP-MARK, MARK+1);
2158 TAINT_IF(SvTAINTED(TARG));
2169 U8 *tmps = (U8*)POPpx;
2172 if (IN_UTF8 && (*tmps & 0x80))
2173 value = utf8_to_uv(tmps, &retlen);
2175 value = (UV)(*tmps & 255);
2186 (void)SvUPGRADE(TARG,SVt_PV);
2188 if (IN_UTF8 && value >= 128) {
2191 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2192 SvCUR_set(TARG, tmps - SvPVX(TARG));
2194 (void)SvPOK_only(TARG);
2204 (void)SvPOK_only(TARG);
2211 djSP; dTARGET; dPOPTOPssrl;
2214 char *tmps = SvPV(left, n_a);
2216 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2218 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2222 "The crypt() function is unimplemented due to excessive paranoia.");
2235 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2239 UV uv = utf8_to_uv(s, &ulen);
2241 if (PL_op->op_private & OPpLOCALE) {
2244 uv = toTITLE_LC_uni(uv);
2247 uv = toTITLE_utf8(s);
2249 tend = uv_to_utf8(tmpbuf, uv);
2251 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2253 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2254 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2258 s = (U8*)SvPV_force(sv, slen);
2259 Copy(tmpbuf, s, ulen, U8);
2263 if (!SvPADTMP(sv)) {
2269 s = (U8*)SvPV_force(sv, slen);
2271 if (PL_op->op_private & OPpLOCALE) {
2274 *s = toUPPER_LC(*s);
2292 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2296 UV uv = utf8_to_uv(s, &ulen);
2298 if (PL_op->op_private & OPpLOCALE) {
2301 uv = toLOWER_LC_uni(uv);
2304 uv = toLOWER_utf8(s);
2306 tend = uv_to_utf8(tmpbuf, uv);
2308 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2310 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2311 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2315 s = (U8*)SvPV_force(sv, slen);
2316 Copy(tmpbuf, s, ulen, U8);
2320 if (!SvPADTMP(sv)) {
2326 s = (U8*)SvPV_force(sv, slen);
2328 if (PL_op->op_private & OPpLOCALE) {
2331 *s = toLOWER_LC(*s);
2356 s = (U8*)SvPV(sv,len);
2358 sv_setpvn(TARG, "", 0);
2362 (void)SvUPGRADE(TARG, SVt_PV);
2363 SvGROW(TARG, (len * 2) + 1);
2364 (void)SvPOK_only(TARG);
2365 d = (U8*)SvPVX(TARG);
2367 if (PL_op->op_private & OPpLOCALE) {
2371 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2377 d = uv_to_utf8(d, toUPPER_utf8( s ));
2382 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2387 if (!SvPADTMP(sv)) {
2393 s = (U8*)SvPV_force(sv, len);
2395 register U8 *send = s + len;
2397 if (PL_op->op_private & OPpLOCALE) {
2400 for (; s < send; s++)
2401 *s = toUPPER_LC(*s);
2404 for (; s < send; s++)
2427 s = (U8*)SvPV(sv,len);
2429 sv_setpvn(TARG, "", 0);
2433 (void)SvUPGRADE(TARG, SVt_PV);
2434 SvGROW(TARG, (len * 2) + 1);
2435 (void)SvPOK_only(TARG);
2436 d = (U8*)SvPVX(TARG);
2438 if (PL_op->op_private & OPpLOCALE) {
2442 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2448 d = uv_to_utf8(d, toLOWER_utf8(s));
2453 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2458 if (!SvPADTMP(sv)) {
2465 s = (U8*)SvPV_force(sv, len);
2467 register U8 *send = s + len;
2469 if (PL_op->op_private & OPpLOCALE) {
2472 for (; s < send; s++)
2473 *s = toLOWER_LC(*s);
2476 for (; s < send; s++)
2491 register char *s = SvPV(sv,len);
2495 (void)SvUPGRADE(TARG, SVt_PV);
2496 SvGROW(TARG, (len * 2) + 1);
2501 STRLEN ulen = UTF8SKIP(s);
2524 SvCUR_set(TARG, d - SvPVX(TARG));
2525 (void)SvPOK_only(TARG);
2528 sv_setpvn(TARG, s, len);
2530 if (SvSMAGICAL(TARG))
2539 djSP; dMARK; dORIGMARK;
2541 register AV* av = (AV*)POPs;
2542 register I32 lval = PL_op->op_flags & OPf_MOD;
2543 I32 arybase = PL_curcop->cop_arybase;
2546 if (SvTYPE(av) == SVt_PVAV) {
2547 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2549 for (svp = MARK + 1; svp <= SP; svp++) {
2554 if (max > AvMAX(av))
2557 while (++MARK <= SP) {
2558 elem = SvIVx(*MARK);
2562 svp = av_fetch(av, elem, lval);
2564 if (!svp || *svp == &PL_sv_undef)
2565 DIE(aTHX_ PL_no_aelem, elem);
2566 if (PL_op->op_private & OPpLVAL_INTRO)
2567 save_aelem(av, elem, svp);
2569 *MARK = svp ? *svp : &PL_sv_undef;
2572 if (GIMME != G_ARRAY) {
2580 /* Associative arrays. */
2585 HV *hash = (HV*)POPs;
2587 I32 gimme = GIMME_V;
2588 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2591 /* might clobber stack_sp */
2592 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2597 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2598 if (gimme == G_ARRAY) {
2601 /* might clobber stack_sp */
2603 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
2608 else if (gimme == G_SCALAR)
2627 I32 gimme = GIMME_V;
2628 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2632 if (PL_op->op_private & OPpSLICE) {
2636 hvtype = SvTYPE(hv);
2637 while (++MARK <= SP) {
2638 if (hvtype == SVt_PVHV)
2639 sv = hv_delete_ent(hv, *MARK, discard, 0);
2641 DIE(aTHX_ "Not a HASH reference");
2642 *MARK = sv ? sv : &PL_sv_undef;
2646 else if (gimme == G_SCALAR) {
2655 if (SvTYPE(hv) == SVt_PVHV)
2656 sv = hv_delete_ent(hv, keysv, discard, 0);
2658 DIE(aTHX_ "Not a HASH reference");
2672 if (SvTYPE(hv) == SVt_PVHV) {
2673 if (hv_exists_ent(hv, tmpsv, 0))
2676 else if (SvTYPE(hv) == SVt_PVAV) {
2677 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2681 DIE(aTHX_ "Not a HASH reference");
2688 djSP; dMARK; dORIGMARK;
2689 register HV *hv = (HV*)POPs;
2690 register I32 lval = PL_op->op_flags & OPf_MOD;
2691 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2693 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2694 DIE(aTHX_ "Can't localize pseudo-hash element");
2696 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2697 while (++MARK <= SP) {
2701 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2702 svp = he ? &HeVAL(he) : 0;
2705 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2708 if (!svp || *svp == &PL_sv_undef) {
2710 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2712 if (PL_op->op_private & OPpLVAL_INTRO)
2713 save_helem(hv, keysv, svp);
2715 *MARK = svp ? *svp : &PL_sv_undef;
2718 if (GIMME != G_ARRAY) {
2726 /* List operators. */
2731 if (GIMME != G_ARRAY) {
2733 *MARK = *SP; /* unwanted list, return last item */
2735 *MARK = &PL_sv_undef;
2744 SV **lastrelem = PL_stack_sp;
2745 SV **lastlelem = PL_stack_base + POPMARK;
2746 SV **firstlelem = PL_stack_base + POPMARK + 1;
2747 register SV **firstrelem = lastlelem + 1;
2748 I32 arybase = PL_curcop->cop_arybase;
2749 I32 lval = PL_op->op_flags & OPf_MOD;
2750 I32 is_something_there = lval;
2752 register I32 max = lastrelem - lastlelem;
2753 register SV **lelem;
2756 if (GIMME != G_ARRAY) {
2757 ix = SvIVx(*lastlelem);
2762 if (ix < 0 || ix >= max)
2763 *firstlelem = &PL_sv_undef;
2765 *firstlelem = firstrelem[ix];
2771 SP = firstlelem - 1;
2775 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2781 if (ix < 0 || ix >= max)
2782 *lelem = &PL_sv_undef;
2784 is_something_there = TRUE;
2785 if (!(*lelem = firstrelem[ix]))
2786 *lelem = &PL_sv_undef;
2789 if (is_something_there)
2792 SP = firstlelem - 1;
2798 djSP; dMARK; dORIGMARK;
2799 I32 items = SP - MARK;
2800 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2801 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2808 djSP; dMARK; dORIGMARK;
2809 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2813 SV *val = NEWSV(46, 0);
2815 sv_setsv(val, *++MARK);
2816 else if (ckWARN(WARN_UNSAFE))
2817 Perl_warner(aTHX_ WARN_UNSAFE, "Odd number of elements in hash assignment");
2818 (void)hv_store_ent(hv,key,val,0);
2827 djSP; dMARK; dORIGMARK;
2828 register AV *ary = (AV*)*++MARK;
2832 register I32 offset;
2833 register I32 length;
2840 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2841 *MARK-- = SvTIED_obj((SV*)ary, mg);
2845 call_method("SPLICE",GIMME_V);
2854 offset = i = SvIVx(*MARK);
2856 offset += AvFILLp(ary) + 1;
2858 offset -= PL_curcop->cop_arybase;
2860 DIE(aTHX_ PL_no_aelem, i);
2862 length = SvIVx(*MARK++);
2864 length += AvFILLp(ary) - offset + 1;
2870 length = AvMAX(ary) + 1; /* close enough to infinity */
2874 length = AvMAX(ary) + 1;
2876 if (offset > AvFILLp(ary) + 1)
2877 offset = AvFILLp(ary) + 1;
2878 after = AvFILLp(ary) + 1 - (offset + length);
2879 if (after < 0) { /* not that much array */
2880 length += after; /* offset+length now in array */
2886 /* At this point, MARK .. SP-1 is our new LIST */
2889 diff = newlen - length;
2890 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2893 if (diff < 0) { /* shrinking the area */
2895 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2896 Copy(MARK, tmparyval, newlen, SV*);
2899 MARK = ORIGMARK + 1;
2900 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2901 MEXTEND(MARK, length);
2902 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2904 EXTEND_MORTAL(length);
2905 for (i = length, dst = MARK; i; i--) {
2906 sv_2mortal(*dst); /* free them eventualy */
2913 *MARK = AvARRAY(ary)[offset+length-1];
2916 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2917 SvREFCNT_dec(*dst++); /* free them now */
2920 AvFILLp(ary) += diff;
2922 /* pull up or down? */
2924 if (offset < after) { /* easier to pull up */
2925 if (offset) { /* esp. if nothing to pull */
2926 src = &AvARRAY(ary)[offset-1];
2927 dst = src - diff; /* diff is negative */
2928 for (i = offset; i > 0; i--) /* can't trust Copy */
2932 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2936 if (after) { /* anything to pull down? */
2937 src = AvARRAY(ary) + offset + length;
2938 dst = src + diff; /* diff is negative */
2939 Move(src, dst, after, SV*);
2941 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2942 /* avoid later double free */
2946 dst[--i] = &PL_sv_undef;
2949 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2951 *dst = NEWSV(46, 0);
2952 sv_setsv(*dst++, *src++);
2954 Safefree(tmparyval);
2957 else { /* no, expanding (or same) */
2959 New(452, tmparyval, length, SV*); /* so remember deletion */
2960 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2963 if (diff > 0) { /* expanding */
2965 /* push up or down? */
2967 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2971 Move(src, dst, offset, SV*);
2973 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2975 AvFILLp(ary) += diff;
2978 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2979 av_extend(ary, AvFILLp(ary) + diff);
2980 AvFILLp(ary) += diff;
2983 dst = AvARRAY(ary) + AvFILLp(ary);
2985 for (i = after; i; i--) {
2992 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2993 *dst = NEWSV(46, 0);
2994 sv_setsv(*dst++, *src++);
2996 MARK = ORIGMARK + 1;
2997 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2999 Copy(tmparyval, MARK, length, SV*);
3001 EXTEND_MORTAL(length);
3002 for (i = length, dst = MARK; i; i--) {
3003 sv_2mortal(*dst); /* free them eventualy */
3007 Safefree(tmparyval);
3011 else if (length--) {
3012 *MARK = tmparyval[length];
3015 while (length-- > 0)
3016 SvREFCNT_dec(tmparyval[length]);
3018 Safefree(tmparyval);
3021 *MARK = &PL_sv_undef;
3029 djSP; dMARK; dORIGMARK; dTARGET;
3030 register AV *ary = (AV*)*++MARK;
3031 register SV *sv = &PL_sv_undef;
3034 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3035 *MARK-- = SvTIED_obj((SV*)ary, mg);
3039 call_method("PUSH",G_SCALAR|G_DISCARD);
3044 /* Why no pre-extend of ary here ? */
3045 for (++MARK; MARK <= SP; MARK++) {
3048 sv_setsv(sv, *MARK);
3053 PUSHi( AvFILL(ary) + 1 );
3061 SV *sv = av_pop(av);
3063 (void)sv_2mortal(sv);
3072 SV *sv = av_shift(av);
3077 (void)sv_2mortal(sv);
3084 djSP; dMARK; dORIGMARK; dTARGET;
3085 register AV *ary = (AV*)*++MARK;
3090 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3091 *MARK-- = SvTIED_obj((SV*)ary, mg);
3095 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3100 av_unshift(ary, SP - MARK);
3103 sv_setsv(sv, *++MARK);
3104 (void)av_store(ary, i++, sv);
3108 PUSHi( AvFILL(ary) + 1 );
3118 if (GIMME == G_ARRAY) {
3129 register char *down;
3135 do_join(TARG, &PL_sv_no, MARK, SP);
3137 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3138 up = SvPV_force(TARG, len);
3140 if (IN_UTF8) { /* first reverse each character */
3141 U8* s = (U8*)SvPVX(TARG);
3142 U8* send = (U8*)(s + len);
3151 down = (char*)(s - 1);
3152 if (s > send || !((*down & 0xc0) == 0x80)) {
3153 if (ckWARN_d(WARN_UTF8))
3154 Perl_warner(aTHX_ WARN_UTF8,
3155 "Malformed UTF-8 character");
3167 down = SvPVX(TARG) + len - 1;
3173 (void)SvPOK_only(TARG);
3182 S_mul128(pTHX_ SV *sv, U8 m)
3185 char *s = SvPV(sv, len);
3189 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3190 SV *tmpNew = newSVpvn("0000000000", 10);
3192 sv_catsv(tmpNew, sv);
3193 SvREFCNT_dec(sv); /* free old sv */
3198 while (!*t) /* trailing '\0'? */
3201 i = ((*t - '0') << 7) + m;
3202 *(t--) = '0' + (i % 10);
3208 /* Explosives and implosives. */
3210 #if 'I' == 73 && 'J' == 74
3211 /* On an ASCII/ISO kind of system */
3212 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3215 Some other sort of character set - use memchr() so we don't match
3218 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3226 I32 gimme = GIMME_V;
3230 register char *pat = SvPV(left, llen);
3231 register char *s = SvPV(right, rlen);
3232 char *strend = s + rlen;
3234 register char *patend = pat + llen;
3239 /* These must not be in registers: */
3256 register U32 culong;
3259 #ifdef PERL_NATINT_PACK
3260 int natint; /* native integer */
3261 int unatint; /* unsigned native integer */
3264 if (gimme != G_ARRAY) { /* arrange to do first one only */
3266 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3267 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3269 while (isDIGIT(*patend) || *patend == '*')
3275 while (pat < patend) {
3277 datumtype = *pat++ & 0xFF;
3278 #ifdef PERL_NATINT_PACK
3281 if (isSPACE(datumtype))
3284 char *natstr = "sSiIlL";
3286 if (strchr(natstr, datumtype)) {
3287 #ifdef PERL_NATINT_PACK
3293 Perl_croak(aTHX_ "'!' allowed only after types %s", natstr);
3297 else if (*pat == '*') {
3298 len = strend - strbeg; /* long enough */
3301 else if (isDIGIT(*pat)) {
3303 while (isDIGIT(*pat)) {
3304 len = (len * 10) + (*pat++ - '0');
3306 Perl_croak(aTHX_ "Repeat count in unpack overflows");
3310 len = (datumtype != '@');
3313 Perl_croak(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
3314 case ',': /* grandfather in commas but with a warning */
3315 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
3316 Perl_warner(aTHX_ WARN_UNSAFE, "Invalid type in unpack: '%c'", (int)datumtype);
3319 if (len == 1 && pat[-1] != '1')
3328 if (len > strend - strbeg)
3329 DIE(aTHX_ "@ outside of string");
3333 if (len > s - strbeg)
3334 DIE(aTHX_ "X outside of string");
3338 if (len > strend - s)
3339 DIE(aTHX_ "x outside of string");
3344 DIE(aTHX_ "# must follow a numeric type");
3345 if (*pat != 'a' && *pat != 'A' && *pat != 'Z')
3346 DIE(aTHX_ "# must be followed by a, A or Z");
3349 pat++; /* ignore '*' for compatibility with pack */
3351 DIE(aTHX_ "# cannot take a count" );
3357 if (len > strend - s)
3360 goto uchar_checksum;
3361 sv = NEWSV(35, len);
3362 sv_setpvn(sv, s, len);
3364 if (datumtype == 'A' || datumtype == 'Z') {
3365 aptr = s; /* borrow register */
3366 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3371 else { /* 'A' strips both nulls and spaces */
3372 s = SvPVX(sv) + len - 1;
3373 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3377 SvCUR_set(sv, s - SvPVX(sv));
3378 s = aptr; /* unborrow register */
3380 XPUSHs(sv_2mortal(sv));
3384 if (pat[-1] == '*' || len > (strend - s) * 8)
3385 len = (strend - s) * 8;
3388 Newz(601, PL_bitcount, 256, char);
3389 for (bits = 1; bits < 256; bits++) {
3390 if (bits & 1) PL_bitcount[bits]++;
3391 if (bits & 2) PL_bitcount[bits]++;
3392 if (bits & 4) PL_bitcount[bits]++;
3393 if (bits & 8) PL_bitcount[bits]++;
3394 if (bits & 16) PL_bitcount[bits]++;
3395 if (bits & 32) PL_bitcount[bits]++;
3396 if (bits & 64) PL_bitcount[bits]++;
3397 if (bits & 128) PL_bitcount[bits]++;
3401 culong += PL_bitcount[*(unsigned char*)s++];
3406 if (datumtype == 'b') {
3408 if (bits & 1) culong++;
3414 if (bits & 128) culong++;
3421 sv = NEWSV(35, len + 1);
3424 aptr = pat; /* borrow register */
3426 if (datumtype == 'b') {
3428 for (len = 0; len < aint; len++) {
3429 if (len & 7) /*SUPPRESS 595*/
3433 *pat++ = '0' + (bits & 1);
3438 for (len = 0; len < aint; len++) {
3443 *pat++ = '0' + ((bits & 128) != 0);
3447 pat = aptr; /* unborrow register */
3448 XPUSHs(sv_2mortal(sv));
3452 if (pat[-1] == '*' || len > (strend - s) * 2)
3453 len = (strend - s) * 2;
3454 sv = NEWSV(35, len + 1);
3457 aptr = pat; /* borrow register */
3459 if (datumtype == 'h') {
3461 for (len = 0; len < aint; len++) {
3466 *pat++ = PL_hexdigit[bits & 15];
3471 for (len = 0; len < aint; len++) {
3476 *pat++ = PL_hexdigit[(bits >> 4) & 15];
3480 pat = aptr; /* unborrow register */
3481 XPUSHs(sv_2mortal(sv));
3484 if (len > strend - s)
3489 if (aint >= 128) /* fake up signed chars */
3499 if (aint >= 128) /* fake up signed chars */
3502 sv_setiv(sv, (IV)aint);
3503 PUSHs(sv_2mortal(sv));
3508 if (len > strend - s)
3523 sv_setiv(sv, (IV)auint);
3524 PUSHs(sv_2mortal(sv));
3529 if (len > strend - s)
3532 while (len-- > 0 && s < strend) {
3533 auint = utf8_to_uv((U8*)s, &along);
3536 cdouble += (NV)auint;
3544 while (len-- > 0 && s < strend) {
3545 auint = utf8_to_uv((U8*)s, &along);
3548 sv_setuv(sv, (UV)auint);
3549 PUSHs(sv_2mortal(sv));
3554 #if SHORTSIZE == SIZE16
3555 along = (strend - s) / SIZE16;
3557 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3562 #if SHORTSIZE != SIZE16
3566 COPYNN(s, &ashort, sizeof(short));
3577 #if SHORTSIZE > SIZE16
3589 #if SHORTSIZE != SIZE16
3593 COPYNN(s, &ashort, sizeof(short));
3596 sv_setiv(sv, (IV)ashort);
3597 PUSHs(sv_2mortal(sv));
3605 #if SHORTSIZE > SIZE16
3611 sv_setiv(sv, (IV)ashort);
3612 PUSHs(sv_2mortal(sv));
3620 #if SHORTSIZE == SIZE16
3621 along = (strend - s) / SIZE16;
3623 unatint = natint && datumtype == 'S';
3624 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3629 #if SHORTSIZE != SIZE16
3631 unsigned short aushort;
3633 COPYNN(s, &aushort, sizeof(unsigned short));
3634 s += sizeof(unsigned short);
3642 COPY16(s, &aushort);
3645 if (datumtype == 'n')
3646 aushort = PerlSock_ntohs(aushort);
3649 if (datumtype == 'v')
3650 aushort = vtohs(aushort);
3659 #if SHORTSIZE != SIZE16
3661 unsigned short aushort;
3663 COPYNN(s, &aushort, sizeof(unsigned short));
3664 s += sizeof(unsigned short);
3666 sv_setiv(sv, (UV)aushort);
3667 PUSHs(sv_2mortal(sv));
3674 COPY16(s, &aushort);
3678 if (datumtype == 'n')
3679 aushort = PerlSock_ntohs(aushort);
3682 if (datumtype == 'v')
3683 aushort = vtohs(aushort);
3685 sv_setiv(sv, (UV)aushort);
3686 PUSHs(sv_2mortal(sv));
3692 along = (strend - s) / sizeof(int);
3697 Copy(s, &aint, 1, int);
3700 cdouble += (NV)aint;
3709 Copy(s, &aint, 1, int);
3713 /* Without the dummy below unpack("i", pack("i",-1))
3714 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3715 * cc with optimization turned on.
3717 * The bug was detected in
3718 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3719 * with optimization (-O4) turned on.
3720 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3721 * does not have this problem even with -O4.
3723 * This bug was reported as DECC_BUGS 1431
3724 * and tracked internally as GEM_BUGS 7775.
3726 * The bug is fixed in
3727 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3728 * UNIX V4.0F support: DEC C V5.9-006 or later
3729 * UNIX V4.0E support: DEC C V5.8-011 or later
3732 * See also few lines later for the same bug.
3735 sv_setiv(sv, (IV)aint) :
3737 sv_setiv(sv, (IV)aint);
3738 PUSHs(sv_2mortal(sv));
3743 along = (strend - s) / sizeof(unsigned int);
3748 Copy(s, &auint, 1, unsigned int);
3749 s += sizeof(unsigned int);
3751 cdouble += (NV)auint;
3760 Copy(s, &auint, 1, unsigned int);
3761 s += sizeof(unsigned int);
3764 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3765 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3766 * See details few lines earlier. */
3768 sv_setuv(sv, (UV)auint) :
3770 sv_setuv(sv, (UV)auint);
3771 PUSHs(sv_2mortal(sv));
3776 #if LONGSIZE == SIZE32
3777 along = (strend - s) / SIZE32;
3779 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3784 #if LONGSIZE != SIZE32
3788 COPYNN(s, &along, sizeof(long));
3791 cdouble += (NV)along;
3801 #if LONGSIZE > SIZE32
3802 if (along > 2147483647)
3803 along -= 4294967296;
3807 cdouble += (NV)along;
3816 #if LONGSIZE != SIZE32
3820 COPYNN(s, &along, sizeof(long));
3823 sv_setiv(sv, (IV)along);
3824 PUSHs(sv_2mortal(sv));
3832 #if LONGSIZE > SIZE32
3833 if (along > 2147483647)
3834 along -= 4294967296;
3838 sv_setiv(sv, (IV)along);
3839 PUSHs(sv_2mortal(sv));
3847 #if LONGSIZE == SIZE32
3848 along = (strend - s) / SIZE32;
3850 unatint = natint && datumtype == 'L';
3851 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3856 #if LONGSIZE != SIZE32
3858 unsigned long aulong;
3860 COPYNN(s, &aulong, sizeof(unsigned long));
3861 s += sizeof(unsigned long);
3863 cdouble += (NV)aulong;
3875 if (datumtype == 'N')
3876 aulong = PerlSock_ntohl(aulong);
3879 if (datumtype == 'V')
3880 aulong = vtohl(aulong);
3883 cdouble += (NV)aulong;
3892 #if LONGSIZE != SIZE32
3894 unsigned long aulong;
3896 COPYNN(s, &aulong, sizeof(unsigned long));
3897 s += sizeof(unsigned long);
3899 sv_setuv(sv, (UV)aulong);
3900 PUSHs(sv_2mortal(sv));
3910 if (datumtype == 'N')
3911 aulong = PerlSock_ntohl(aulong);
3914 if (datumtype == 'V')
3915 aulong = vtohl(aulong);
3918 sv_setuv(sv, (UV)aulong);
3919 PUSHs(sv_2mortal(sv));
3925 along = (strend - s) / sizeof(char*);
3931 if (sizeof(char*) > strend - s)
3934 Copy(s, &aptr, 1, char*);
3940 PUSHs(sv_2mortal(sv));
3950 while ((len > 0) && (s < strend)) {
3951 auv = (auv << 7) | (*s & 0x7f);
3952 if (!(*s++ & 0x80)) {
3956 PUSHs(sv_2mortal(sv));
3960 else if (++bytes >= sizeof(UV)) { /* promote to string */
3964 sv = Perl_newSVpvf(aTHX_ "%.*Vu", (int)TYPE_DIGITS(UV), auv);
3965 while (s < strend) {
3966 sv = mul128(sv, *s & 0x7f);
3967 if (!(*s++ & 0x80)) {
3976 PUSHs(sv_2mortal(sv));
3981 if ((s >= strend) && bytes)
3982 Perl_croak(aTHX_ "Unterminated compressed integer");
3987 if (sizeof(char*) > strend - s)
3990 Copy(s, &aptr, 1, char*);
3995 sv_setpvn(sv, aptr, len);
3996 PUSHs(sv_2mortal(sv));
4000 along = (strend - s) / sizeof(Quad_t);
4006 if (s + sizeof(Quad_t) > strend)
4009 Copy(s, &aquad, 1, Quad_t);
4010 s += sizeof(Quad_t);
4013 if (aquad >= IV_MIN && aquad <= IV_MAX)
4014 sv_setiv(sv, (IV)aquad);
4016 sv_setnv(sv, (NV)aquad);
4017 PUSHs(sv_2mortal(sv));
4021 along = (strend - s) / sizeof(Quad_t);
4027 if (s + sizeof(Uquad_t) > strend)
4030 Copy(s, &auquad, 1, Uquad_t);
4031 s += sizeof(Uquad_t);
4034 if (auquad <= UV_MAX)
4035 sv_setuv(sv, (UV)auquad);
4037 sv_setnv(sv, (NV)auquad);
4038 PUSHs(sv_2mortal(sv));
4042 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4045 along = (strend - s) / sizeof(float);
4050 Copy(s, &afloat, 1, float);
4059 Copy(s, &afloat, 1, float);
4062 sv_setnv(sv, (NV)afloat);
4063 PUSHs(sv_2mortal(sv));
4069 along = (strend - s) / sizeof(double);
4074 Copy(s, &adouble, 1, double);
4075 s += sizeof(double);
4083 Copy(s, &adouble, 1, double);
4084 s += sizeof(double);
4086 sv_setnv(sv, (NV)adouble);
4087 PUSHs(sv_2mortal(sv));
4093 * Initialise the decode mapping. By using a table driven
4094 * algorithm, the code will be character-set independent
4095 * (and just as fast as doing character arithmetic)
4097 if (PL_uudmap['M'] == 0) {
4100 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4101 PL_uudmap[PL_uuemap[i]] = i;
4103 * Because ' ' and '`' map to the same value,
4104 * we need to decode them both the same.
4109 along = (strend - s) * 3 / 4;
4110 sv = NEWSV(42, along);
4113 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4118 len = PL_uudmap[*s++] & 077;
4120 if (s < strend && ISUUCHAR(*s))
4121 a = PL_uudmap[*s++] & 077;
4124 if (s < strend && ISUUCHAR(*s))
4125 b = PL_uudmap[*s++] & 077;
4128 if (s < strend && ISUUCHAR(*s))
4129 c = PL_uudmap[*s++] & 077;
4132 if (s < strend && ISUUCHAR(*s))
4133 d = PL_uudmap[*s++] & 077;
4136 hunk[0] = (a << 2) | (b >> 4);
4137 hunk[1] = (b << 4) | (c >> 2);
4138 hunk[2] = (c << 6) | d;
4139 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4144 else if (s[1] == '\n') /* possible checksum byte */
4147 XPUSHs(sv_2mortal(sv));
4152 if (strchr("fFdD", datumtype) ||
4153 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4157 while (checksum >= 16) {
4161 while (checksum >= 4) {
4167 along = (1 << checksum) - 1;
4168 while (cdouble < 0.0)
4170 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
4171 sv_setnv(sv, cdouble);
4174 if (checksum < 32) {
4175 aulong = (1 << checksum) - 1;
4178 sv_setuv(sv, (UV)culong);
4180 XPUSHs(sv_2mortal(sv));
4184 if (SP == oldsp && gimme == G_SCALAR)
4185 PUSHs(&PL_sv_undef);
4190 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
4194 *hunk = PL_uuemap[len];
4195 sv_catpvn(sv, hunk, 1);
4198 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4199 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4200 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4201 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4202 sv_catpvn(sv, hunk, 4);
4207 char r = (len > 1 ? s[1] : '\0');
4208 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4209 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4210 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4211 hunk[3] = PL_uuemap[0];
4212 sv_catpvn(sv, hunk, 4);
4214 sv_catpvn(sv, "\n", 1);
4218 S_is_an_int(pTHX_ char *s, STRLEN l)
4221 SV *result = newSVpvn(s, l);
4222 char *result_c = SvPV(result, n_a); /* convenience */
4223 char *out = result_c;
4233 SvREFCNT_dec(result);
4256 SvREFCNT_dec(result);
4262 SvCUR_set(result, out - result_c);
4266 /* pnum must be '\0' terminated */
4268 S_div128(pTHX_ SV *pnum, bool *done)
4271 char *s = SvPV(pnum, len);
4280 i = m * 10 + (*t - '0');
4282 r = (i >> 7); /* r < 10 */
4289 SvCUR_set(pnum, (STRLEN) (t - s));
4296 djSP; dMARK; dORIGMARK; dTARGET;
4297 register SV *cat = TARG;
4300 register char *pat = SvPVx(*++MARK, fromlen);
4301 register char *patend = pat + fromlen;
4306 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4307 static char *space10 = " ";
4309 /* These must not be in registers: */
4324 #ifdef PERL_NATINT_PACK
4325 int natint; /* native integer */
4330 sv_setpvn(cat, "", 0);
4331 while (pat < patend) {
4332 SV *lengthcode = Nullsv;
4333 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *MARK++ : &PL_sv_no)
4334 datumtype = *pat++ & 0xFF;
4335 #ifdef PERL_NATINT_PACK
4338 if (isSPACE(datumtype))
4341 char *natstr = "sSiIlL";
4343 if (strchr(natstr, datumtype)) {
4344 #ifdef PERL_NATINT_PACK
4350 Perl_croak(aTHX_ "'!' allowed only after types %s", natstr);
4353 len = strchr("@Xxu", datumtype) ? 0 : items;
4356 else if (isDIGIT(*pat)) {
4358 while (isDIGIT(*pat)) {
4359 len = (len * 10) + (*pat++ - '0');
4361 Perl_croak(aTHX_ "Repeat count in pack overflows");
4368 if (*pat != 'a' && *pat != 'A' && *pat != 'Z' || pat[1] != '*')
4369 DIE(aTHX_ "# must be followed by a*, A* or Z*");
4370 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
4371 ? *MARK : &PL_sv_no)));
4375 Perl_croak(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
4376 case ',': /* grandfather in commas but with a warning */
4377 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
4378 Perl_warner(aTHX_ WARN_UNSAFE,
4379 "Invalid type in pack: '%c'", (int)datumtype);
4382 DIE(aTHX_ "%% may only be used in unpack");
4393 if (SvCUR(cat) < len)
4394 DIE(aTHX_ "X outside of string");
4401 sv_catpvn(cat, null10, 10);
4404 sv_catpvn(cat, null10, len);
4410 aptr = SvPV(fromstr, fromlen);
4414 sv_catpvn(cat, aptr, len);
4416 sv_catpvn(cat, aptr, fromlen);
4418 if (datumtype == 'A') {
4420 sv_catpvn(cat, space10, 10);
4423 sv_catpvn(cat, space10, len);
4427 sv_catpvn(cat, null10, 10);
4430 sv_catpvn(cat, null10, len);
4437 char *savepat = pat;
4442 aptr = SvPV(fromstr, fromlen);
4447 SvCUR(cat) += (len+7)/8;
4448 SvGROW(cat, SvCUR(cat) + 1);
4449 aptr = SvPVX(cat) + aint;
4454 if (datumtype == 'B') {
4455 for (len = 0; len++ < aint;) {
4456 items |= *pat++ & 1;
4460 *aptr++ = items & 0xff;
4466 for (len = 0; len++ < aint;) {
4472 *aptr++ = items & 0xff;
4478 if (datumtype == 'B')
4479 items <<= 7 - (aint & 7);
4481 items >>= 7 - (aint & 7);
4482 *aptr++ = items & 0xff;
4484 pat = SvPVX(cat) + SvCUR(cat);
4495 char *savepat = pat;
4500 aptr = SvPV(fromstr, fromlen);
4505 SvCUR(cat) += (len+1)/2;
4506 SvGROW(cat, SvCUR(cat) + 1);
4507 aptr = SvPVX(cat) + aint;
4512 if (datumtype == 'H') {
4513 for (len = 0; len++ < aint;) {
4515 items |= ((*pat++ & 15) + 9) & 15;
4517 items |= *pat++ & 15;
4521 *aptr++ = items & 0xff;
4527 for (len = 0; len++ < aint;) {
4529 items |= (((*pat++ & 15) + 9) & 15) << 4;
4531 items |= (*pat++ & 15) << 4;
4535 *aptr++ = items & 0xff;
4541 *aptr++ = items & 0xff;
4542 pat = SvPVX(cat) + SvCUR(cat);
4554 aint = SvIV(fromstr);
4556 sv_catpvn(cat, &achar, sizeof(char));
4562 auint = SvUV(fromstr);
4563 SvGROW(cat, SvCUR(cat) + 10);
4564 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4569 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4574 afloat = (float)SvNV(fromstr);
4575 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4582 adouble = (double)SvNV(fromstr);
4583 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4589 ashort = (I16)SvIV(fromstr);
4591 ashort = PerlSock_htons(ashort);
4593 CAT16(cat, &ashort);
4599 ashort = (I16)SvIV(fromstr);
4601 ashort = htovs(ashort);
4603 CAT16(cat, &ashort);
4607 #if SHORTSIZE != SIZE16
4609 unsigned short aushort;
4613 aushort = SvUV(fromstr);
4614 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4624 aushort = (U16)SvUV(fromstr);
4625 CAT16(cat, &aushort);
4631 #if SHORTSIZE != SIZE16
4637 ashort = SvIV(fromstr);
4638 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4646 ashort = (I16)SvIV(fromstr);
4647 CAT16(cat, &ashort);
4654 auint = SvUV(fromstr);
4655 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4661 adouble = Perl_floor(SvNV(fromstr));
4664 Perl_croak(aTHX_ "Cannot compress negative numbers");
4670 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4671 adouble <= UV_MAX_cxux
4678 char buf[1 + sizeof(UV)];
4679 char *in = buf + sizeof(buf);
4680 UV auv = U_V(adouble);
4683 *--in = (auv & 0x7f) | 0x80;
4686 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4687 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4689 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4690 char *from, *result, *in;
4695 /* Copy string and check for compliance */
4696 from = SvPV(fromstr, len);
4697 if ((norm = is_an_int(from, len)) == NULL)
4698 Perl_croak(aTHX_ "can compress only unsigned integer");
4700 New('w', result, len, char);
4704 *--in = div128(norm, &done) | 0x80;
4705 result[len - 1] &= 0x7F; /* clear continue bit */
4706 sv_catpvn(cat, in, (result + len) - in);
4708 SvREFCNT_dec(norm); /* free norm */
4710 else if (SvNOKp(fromstr)) {
4711 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4712 char *in = buf + sizeof(buf);
4715 double next = floor(adouble / 128);
4716 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4717 if (--in < buf) /* this cannot happen ;-) */
4718 Perl_croak(aTHX_ "Cannot compress integer");
4720 } while (adouble > 0);
4721 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4722 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4725 Perl_croak(aTHX_ "Cannot compress non integer");
4731 aint = SvIV(fromstr);
4732 sv_catpvn(cat, (char*)&aint, sizeof(int));
4738 aulong = SvUV(fromstr);
4740 aulong = PerlSock_htonl(aulong);
4742 CAT32(cat, &aulong);
4748 aulong = SvUV(fromstr);
4750 aulong = htovl(aulong);
4752 CAT32(cat, &aulong);
4756 #if LONGSIZE != SIZE32
4758 unsigned long aulong;
4762 aulong = SvUV(fromstr);
4763 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4771 aulong = SvUV(fromstr);
4772 CAT32(cat, &aulong);
4777 #if LONGSIZE != SIZE32
4783 along = SvIV(fromstr);
4784 sv_catpvn(cat, (char *)&along, sizeof(long));
4792 along = SvIV(fromstr);
4801 auquad = (Uquad_t)SvUV(fromstr);
4802 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4808 aquad = (Quad_t)SvIV(fromstr);
4809 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4812 #endif /* HAS_QUAD */
4814 len = 1; /* assume SV is correct length */
4819 if (fromstr == &PL_sv_undef)
4823 /* XXX better yet, could spirit away the string to
4824 * a safe spot and hang on to it until the result
4825 * of pack() (and all copies of the result) are
4828 if (ckWARN(WARN_UNSAFE) && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4829 Perl_warner(aTHX_ WARN_UNSAFE,
4830 "Attempt to pack pointer to temporary value");
4831 if (SvPOK(fromstr) || SvNIOK(fromstr))
4832 aptr = SvPV(fromstr,n_a);
4834 aptr = SvPV_force(fromstr,n_a);
4836 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4841 aptr = SvPV(fromstr, fromlen);
4842 SvGROW(cat, fromlen * 4 / 3);
4847 while (fromlen > 0) {
4854 doencodes(cat, aptr, todo);
4873 register I32 limit = POPi; /* note, negative is forever */
4876 register char *s = SvPV(sv, len);
4877 char *strend = s + len;
4879 register REGEXP *rx;
4883 I32 maxiters = (strend - s) + 10;
4886 I32 origlimit = limit;
4889 AV *oldstack = PL_curstack;
4890 I32 gimme = GIMME_V;
4891 I32 oldsave = PL_savestack_ix;
4892 I32 make_mortal = 1;
4893 MAGIC *mg = (MAGIC *) NULL;
4896 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4901 DIE(aTHX_ "panic: do_split");
4902 rx = pm->op_pmregexp;
4904 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4905 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4907 if (pm->op_pmreplroot)
4908 ary = GvAVn((GV*)pm->op_pmreplroot);
4909 else if (gimme != G_ARRAY)
4911 ary = (AV*)PL_curpad[0];
4913 ary = GvAVn(PL_defgv);
4914 #endif /* USE_THREADS */
4917 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4923 if (mg = SvTIED_mg((SV*)ary, 'P')) {
4925 XPUSHs(SvTIED_obj((SV*)ary, mg));
4930 for (i = AvFILLp(ary); i >= 0; i--)
4931 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4933 /* temporarily switch stacks */
4934 SWITCHSTACK(PL_curstack, ary);
4938 base = SP - PL_stack_base;
4940 if (pm->op_pmflags & PMf_SKIPWHITE) {
4941 if (pm->op_pmflags & PMf_LOCALE) {
4942 while (isSPACE_LC(*s))
4950 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4951 SAVEINT(PL_multiline);
4952 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4956 limit = maxiters + 2;
4957 if (pm->op_pmflags & PMf_WHITE) {
4960 while (m < strend &&
4961 !((pm->op_pmflags & PMf_LOCALE)
4962 ? isSPACE_LC(*m) : isSPACE(*m)))
4967 dstr = NEWSV(30, m-s);
4968 sv_setpvn(dstr, s, m-s);
4974 while (s < strend &&
4975 ((pm->op_pmflags & PMf_LOCALE)
4976 ? isSPACE_LC(*s) : isSPACE(*s)))
4980 else if (rx->prelen == 1 && *rx->precomp == '^') {
4981 if (!(pm->op_pmflags & PMf_MULTILINE)
4982 && !(pm->op_pmregexp->reganch & ROPT_WARNED)) {
4983 if (ckWARN(WARN_DEPRECATED))
4984 Perl_warner(aTHX_ WARN_DEPRECATED,
4985 "split /^/ better written as split /^/m");
4986 pm->op_pmregexp->reganch |= ROPT_WARNED;
4990 for (m = s; m < strend && *m != '\n'; m++) ;
4994 dstr = NEWSV(30, m-s);
4995 sv_setpvn(dstr, s, m-s);
5002 else if ((rx->reganch & RE_USE_INTUIT) && !rx->nparens
5003 && (rx->reganch & ROPT_CHECK_ALL)
5004 && !(rx->reganch & ROPT_ANCH)) {
5005 int tail = (rx->reganch & RE_INTUIT_TAIL);
5006 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
5010 if (len == 1 && !tail) {
5014 for (m = s; m < strend && *m != c; m++) ;
5017 dstr = NEWSV(30, m-s);
5018 sv_setpvn(dstr, s, m-s);
5027 while (s < strend && --limit &&
5028 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5029 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
5032 dstr = NEWSV(31, m-s);
5033 sv_setpvn(dstr, s, m-s);
5037 s = m + len; /* Fake \n at the end */
5042 maxiters += (strend - s) * rx->nparens;
5043 while (s < strend && --limit
5044 /* && (!rx->check_substr
5045 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
5047 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
5048 1 /* minend */, sv, NULL, 0))
5050 TAINT_IF(RX_MATCH_TAINTED(rx));
5051 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5056 strend = s + (strend - m);
5058 m = rx->startp[0] + orig;
5059 dstr = NEWSV(32, m-s);
5060 sv_setpvn(dstr, s, m-s);
5065 for (i = 1; i <= rx->nparens; i++) {
5066 s = rx->startp[i] + orig;
5067 m = rx->endp[i] + orig;
5069 dstr = NEWSV(33, m-s);
5070 sv_setpvn(dstr, s, m-s);
5073 dstr = NEWSV(33, 0);
5079 s = rx->endp[0] + orig;
5083 LEAVE_SCOPE(oldsave);
5084 iters = (SP - PL_stack_base) - base;
5085 if (iters > maxiters)
5086 DIE(aTHX_ "Split loop");
5088 /* keep field after final delim? */
5089 if (s < strend || (iters && origlimit)) {
5090 dstr = NEWSV(34, strend-s);
5091 sv_setpvn(dstr, s, strend-s);
5097 else if (!origlimit) {
5098 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5104 SWITCHSTACK(ary, oldstack);
5105 if (SvSMAGICAL(ary)) {
5110 if (gimme == G_ARRAY) {
5112 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5120 call_method("PUSH",G_SCALAR|G_DISCARD);
5123 if (gimme == G_ARRAY) {
5124 /* EXTEND should not be needed - we just popped them */
5126 for (i=0; i < iters; i++) {
5127 SV **svp = av_fetch(ary, i, FALSE);
5128 PUSHs((svp) ? *svp : &PL_sv_undef);
5135 if (gimme == G_ARRAY)
5138 if (iters || !pm->op_pmreplroot) {
5148 Perl_unlock_condpair(pTHX_ void *svv)
5151 MAGIC *mg = mg_find((SV*)svv, 'm');
5154 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
5155 MUTEX_LOCK(MgMUTEXP(mg));
5156 if (MgOWNER(mg) != thr)
5157 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
5159 COND_SIGNAL(MgOWNERCONDP(mg));
5160 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
5161 (unsigned long)thr, (unsigned long)svv);)
5162 MUTEX_UNLOCK(MgMUTEXP(mg));
5164 #endif /* USE_THREADS */
5177 mg = condpair_magic(sv);
5178 MUTEX_LOCK(MgMUTEXP(mg));
5179 if (MgOWNER(mg) == thr)
5180 MUTEX_UNLOCK(MgMUTEXP(mg));
5183 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
5185 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
5186 (unsigned long)thr, (unsigned long)sv);)
5187 MUTEX_UNLOCK(MgMUTEXP(mg));
5188 SAVEDESTRUCTOR(Perl_unlock_condpair, sv);
5190 #endif /* USE_THREADS */
5191 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5192 || SvTYPE(retsv) == SVt_PVCV) {
5193 retsv = refto(retsv);
5204 if (PL_op->op_private & OPpLVAL_INTRO)
5205 PUSHs(*save_threadsv(PL_op->op_targ));
5207 PUSHs(THREADSV(PL_op->op_targ));
5210 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
5211 #endif /* USE_THREADS */