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));
409 if ((PL_op->op_private & OPpLVAL_INTRO) && !CvLVALUE(cv))
410 Perl_croak(aTHX_ "Can't modify non-lvalue subroutine call");
413 cv = (CV*)&PL_sv_undef;
427 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
428 char *s = SvPVX(TOPs);
429 if (strnEQ(s, "CORE::", 6)) {
432 code = keyword(s + 6, SvCUR(TOPs) - 6);
433 if (code < 0) { /* Overridable. */
434 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
435 int i = 0, n = 0, seen_question = 0;
437 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
439 while (i < MAXO) { /* The slow way. */
440 if (strEQ(s + 6, PL_op_name[i])
441 || strEQ(s + 6, PL_op_desc[i]))
447 goto nonesuch; /* Should not happen... */
449 oa = PL_opargs[i] >> OASHIFT;
451 if (oa & OA_OPTIONAL) {
455 else if (seen_question)
456 goto set; /* XXXX system, exec */
457 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
458 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
461 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
462 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
466 ret = sv_2mortal(newSVpvn(str, n - 1));
468 else if (code) /* Non-Overridable */
470 else { /* None such */
472 Perl_croak(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
476 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
478 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
487 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
489 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
505 if (GIMME != G_ARRAY) {
509 *MARK = &PL_sv_undef;
510 *MARK = refto(*MARK);
514 EXTEND_MORTAL(SP - MARK);
516 *MARK = refto(*MARK);
521 S_refto(pTHX_ SV *sv)
525 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
528 if (!(sv = LvTARG(sv)))
531 (void)SvREFCNT_inc(sv);
533 else if (SvPADTMP(sv))
537 (void)SvREFCNT_inc(sv);
540 sv_upgrade(rv, SVt_RV);
554 if (sv && SvGMAGICAL(sv))
557 if (!sv || !SvROK(sv))
561 pv = sv_reftype(sv,TRUE);
562 PUSHp(pv, strlen(pv));
572 stash = PL_curcop->cop_stash;
576 char *ptr = SvPV(ssv,len);
577 if (ckWARN(WARN_UNSAFE) && len == 0)
578 Perl_warner(aTHX_ WARN_UNSAFE,
579 "Explicit blessing to '' (assuming package main)");
580 stash = gv_stashpvn(ptr, len, TRUE);
583 (void)sv_bless(TOPs, stash);
597 elem = SvPV(sv, n_a);
601 switch (elem ? *elem : '\0')
604 if (strEQ(elem, "ARRAY"))
605 tmpRef = (SV*)GvAV(gv);
608 if (strEQ(elem, "CODE"))
609 tmpRef = (SV*)GvCVu(gv);
612 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
613 tmpRef = (SV*)GvIOp(gv);
616 if (strEQ(elem, "GLOB"))
620 if (strEQ(elem, "HASH"))
621 tmpRef = (SV*)GvHV(gv);
624 if (strEQ(elem, "IO"))
625 tmpRef = (SV*)GvIOp(gv);
628 if (strEQ(elem, "NAME"))
629 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
632 if (strEQ(elem, "PACKAGE"))
633 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
636 if (strEQ(elem, "SCALAR"))
650 /* Pattern matching */
655 register unsigned char *s;
658 register I32 *sfirst;
662 if (sv == PL_lastscream) {
668 SvSCREAM_off(PL_lastscream);
669 SvREFCNT_dec(PL_lastscream);
671 PL_lastscream = SvREFCNT_inc(sv);
674 s = (unsigned char*)(SvPV(sv, len));
678 if (pos > PL_maxscream) {
679 if (PL_maxscream < 0) {
680 PL_maxscream = pos + 80;
681 New(301, PL_screamfirst, 256, I32);
682 New(302, PL_screamnext, PL_maxscream, I32);
685 PL_maxscream = pos + pos / 4;
686 Renew(PL_screamnext, PL_maxscream, I32);
690 sfirst = PL_screamfirst;
691 snext = PL_screamnext;
693 if (!sfirst || !snext)
694 DIE(aTHX_ "do_study: out of memory");
696 for (ch = 256; ch; --ch)
703 snext[pos] = sfirst[ch] - pos;
710 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
719 if (PL_op->op_flags & OPf_STACKED)
725 TARG = sv_newmortal();
730 /* Lvalue operators. */
742 djSP; dMARK; dTARGET;
752 SETi(do_chomp(TOPs));
758 djSP; dMARK; dTARGET;
759 register I32 count = 0;
762 count += do_chomp(POPs);
773 if (!sv || !SvANY(sv))
775 switch (SvTYPE(sv)) {
777 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
781 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
785 if (CvROOT(sv) || CvXSUB(sv))
802 if (!PL_op->op_private) {
811 if (SvTHINKFIRST(sv))
814 switch (SvTYPE(sv)) {
824 if (ckWARN(WARN_UNSAFE) && cv_const_sv((CV*)sv))
825 Perl_warner(aTHX_ WARN_UNSAFE, "Constant subroutine %s undefined",
826 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
830 /* let user-undef'd sub keep its identity */
831 GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
838 SvSetMagicSV(sv, &PL_sv_undef);
842 Newz(602, gp, 1, GP);
843 GvGP(sv) = gp_ref(gp);
844 GvSV(sv) = NEWSV(72,0);
845 GvLINE(sv) = PL_curcop->cop_line;
851 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
854 SvPV_set(sv, Nullch);
867 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
868 Perl_croak(aTHX_ PL_no_modify);
869 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
870 SvIVX(TOPs) != IV_MIN)
873 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
884 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
885 Perl_croak(aTHX_ PL_no_modify);
886 sv_setsv(TARG, TOPs);
887 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
888 SvIVX(TOPs) != IV_MAX)
891 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
905 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
906 Perl_croak(aTHX_ PL_no_modify);
907 sv_setsv(TARG, TOPs);
908 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
909 SvIVX(TOPs) != IV_MIN)
912 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
921 /* Ordinary operators. */
925 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
928 SETn( pow( left, right) );
935 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
938 SETn( left * right );
945 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
950 DIE(aTHX_ "Illegal division by zero");
952 /* insure that 20./5. == 4. */
955 if ((NV)I_V(left) == left &&
956 (NV)I_V(right) == right &&
957 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
961 value = left / right;
965 value = left / right;
974 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
984 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
986 right = (right_neg = (i < 0)) ? -i : i;
991 right_neg = dright < 0;
996 if (!use_double && SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
998 left = (left_neg = (i < 0)) ? -i : i;
1006 left_neg = dleft < 0;
1015 /* Somehow U_V is pessimized even if CASTFLAGS is 0 */
1017 # define CAST_D2UV(d) U_V(d)
1019 # define CAST_D2UV(d) ((UV)(d))
1021 /* Tried to do this only in the case DOUBLESIZE <= UV_SIZE,
1022 * or, in other words, precision of UV more than of NV.
1023 * But in fact the approach below turned out to be an
1024 * optimization - floor() may be slow */
1025 if (dright <= UV_MAX && dleft <= UV_MAX) {
1026 right = CAST_D2UV(dright);
1027 left = CAST_D2UV(dleft);
1032 /* Backward-compatibility clause: */
1033 dright = floor(dright + 0.5);
1034 dleft = floor(dleft + 0.5);
1037 DIE(aTHX_ "Illegal modulus zero");
1039 dans = Perl_fmod(dleft, dright);
1040 if ((left_neg != right_neg) && dans)
1041 dans = dright - dans;
1044 sv_setnv(TARG, dans);
1051 DIE(aTHX_ "Illegal modulus zero");
1054 if ((left_neg != right_neg) && ans)
1057 /* XXX may warn: unary minus operator applied to unsigned type */
1058 /* could change -foo to be (~foo)+1 instead */
1059 if (ans <= ~((UV)IV_MAX)+1)
1060 sv_setiv(TARG, ~ans+1);
1062 sv_setnv(TARG, -(NV)ans);
1065 sv_setuv(TARG, ans);
1074 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1076 register I32 count = POPi;
1077 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1079 I32 items = SP - MARK;
1082 max = items * count;
1091 repeatcpy((char*)(MARK + items), (char*)MARK,
1092 items * sizeof(SV*), count - 1);
1095 else if (count <= 0)
1098 else { /* Note: mark already snarfed by pp_list */
1103 SvSetSV(TARG, tmpstr);
1104 SvPV_force(TARG, len);
1109 SvGROW(TARG, (count * len) + 1);
1110 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1111 SvCUR(TARG) *= count;
1113 *SvEND(TARG) = '\0';
1115 (void)SvPOK_only(TARG);
1124 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1127 SETn( left - right );
1134 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1137 if (PL_op->op_private & HINT_INTEGER) {
1139 i = BWi(i) << shift;
1153 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1156 if (PL_op->op_private & HINT_INTEGER) {
1158 i = BWi(i) >> shift;
1172 djSP; tryAMAGICbinSET(lt,0);
1175 SETs(boolSV(TOPn < value));
1182 djSP; tryAMAGICbinSET(gt,0);
1185 SETs(boolSV(TOPn > value));
1192 djSP; tryAMAGICbinSET(le,0);
1195 SETs(boolSV(TOPn <= value));
1202 djSP; tryAMAGICbinSET(ge,0);
1205 SETs(boolSV(TOPn >= value));
1212 djSP; tryAMAGICbinSET(ne,0);
1215 SETs(boolSV(TOPn != value));
1222 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1229 else if (left < right)
1231 else if (left > right)
1244 djSP; tryAMAGICbinSET(slt,0);
1247 int cmp = ((PL_op->op_private & OPpLOCALE)
1248 ? sv_cmp_locale(left, right)
1249 : sv_cmp(left, right));
1250 SETs(boolSV(cmp < 0));
1257 djSP; tryAMAGICbinSET(sgt,0);
1260 int cmp = ((PL_op->op_private & OPpLOCALE)
1261 ? sv_cmp_locale(left, right)
1262 : sv_cmp(left, right));
1263 SETs(boolSV(cmp > 0));
1270 djSP; tryAMAGICbinSET(sle,0);
1273 int cmp = ((PL_op->op_private & OPpLOCALE)
1274 ? sv_cmp_locale(left, right)
1275 : sv_cmp(left, right));
1276 SETs(boolSV(cmp <= 0));
1283 djSP; tryAMAGICbinSET(sge,0);
1286 int cmp = ((PL_op->op_private & OPpLOCALE)
1287 ? sv_cmp_locale(left, right)
1288 : sv_cmp(left, right));
1289 SETs(boolSV(cmp >= 0));
1296 djSP; tryAMAGICbinSET(seq,0);
1299 SETs(boolSV(sv_eq(left, right)));
1306 djSP; tryAMAGICbinSET(sne,0);
1309 SETs(boolSV(!sv_eq(left, right)));
1316 djSP; dTARGET; tryAMAGICbin(scmp,0);
1319 int cmp = ((PL_op->op_private & OPpLOCALE)
1320 ? sv_cmp_locale(left, right)
1321 : sv_cmp(left, right));
1329 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1332 if (SvNIOKp(left) || SvNIOKp(right)) {
1333 if (PL_op->op_private & HINT_INTEGER) {
1334 IBW value = SvIV(left) & SvIV(right);
1338 UBW value = SvUV(left) & SvUV(right);
1343 do_vop(PL_op->op_type, TARG, left, right);
1352 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1355 if (SvNIOKp(left) || SvNIOKp(right)) {
1356 if (PL_op->op_private & HINT_INTEGER) {
1357 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1361 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1366 do_vop(PL_op->op_type, TARG, left, right);
1375 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1378 if (SvNIOKp(left) || SvNIOKp(right)) {
1379 if (PL_op->op_private & HINT_INTEGER) {
1380 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1384 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1389 do_vop(PL_op->op_type, TARG, left, right);
1398 djSP; dTARGET; tryAMAGICun(neg);
1403 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1405 else if (SvNIOKp(sv))
1407 else if (SvPOKp(sv)) {
1409 char *s = SvPV(sv, len);
1410 if (isIDFIRST(*s)) {
1411 sv_setpvn(TARG, "-", 1);
1414 else if (*s == '+' || *s == '-') {
1416 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1418 else if (IN_UTF8 && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1419 sv_setpvn(TARG, "-", 1);
1423 sv_setnv(TARG, -SvNV(sv));
1434 djSP; tryAMAGICunSET(not);
1435 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1441 djSP; dTARGET; tryAMAGICun(compl);
1445 if (PL_op->op_private & HINT_INTEGER) {
1446 IBW value = ~SvIV(sv);
1450 UBW value = ~SvUV(sv);
1455 register char *tmps;
1456 register long *tmpl;
1461 tmps = SvPV_force(TARG, len);
1464 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1467 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1471 for ( ; anum > 0; anum--, tmps++)
1480 /* integer versions of some of the above */
1484 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1487 SETi( left * right );
1494 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1498 DIE(aTHX_ "Illegal division by zero");
1499 value = POPi / value;
1507 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1511 DIE(aTHX_ "Illegal modulus zero");
1512 SETi( left % right );
1519 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1522 SETi( left + right );
1529 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1532 SETi( left - right );
1539 djSP; tryAMAGICbinSET(lt,0);
1542 SETs(boolSV(left < right));
1549 djSP; tryAMAGICbinSET(gt,0);
1552 SETs(boolSV(left > right));
1559 djSP; tryAMAGICbinSET(le,0);
1562 SETs(boolSV(left <= right));
1569 djSP; tryAMAGICbinSET(ge,0);
1572 SETs(boolSV(left >= right));
1579 djSP; tryAMAGICbinSET(eq,0);
1582 SETs(boolSV(left == right));
1589 djSP; tryAMAGICbinSET(ne,0);
1592 SETs(boolSV(left != right));
1599 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1606 else if (left < right)
1617 djSP; dTARGET; tryAMAGICun(neg);
1622 /* High falutin' math. */
1626 djSP; dTARGET; tryAMAGICbin(atan2,0);
1629 SETn(Perl_atan2(left, right));
1636 djSP; dTARGET; tryAMAGICun(sin);
1640 value = Perl_sin(value);
1648 djSP; dTARGET; tryAMAGICun(cos);
1652 value = Perl_cos(value);
1658 /* Support Configure command-line overrides for rand() functions.
1659 After 5.005, perhaps we should replace this by Configure support
1660 for drand48(), random(), or rand(). For 5.005, though, maintain
1661 compatibility by calling rand() but allow the user to override it.
1662 See INSTALL for details. --Andy Dougherty 15 July 1998
1664 /* Now it's after 5.005, and Configure supports drand48() and random(),
1665 in addition to rand(). So the overrides should not be needed any more.
1666 --Jarkko Hietaniemi 27 September 1998
1669 #ifndef HAS_DRAND48_PROTO
1670 extern double drand48 (void);
1683 if (!PL_srand_called) {
1684 (void)seedDrand01((Rand_seed_t)seed());
1685 PL_srand_called = TRUE;
1700 (void)seedDrand01((Rand_seed_t)anum);
1701 PL_srand_called = TRUE;
1710 * This is really just a quick hack which grabs various garbage
1711 * values. It really should be a real hash algorithm which
1712 * spreads the effect of every input bit onto every output bit,
1713 * if someone who knows about such things would bother to write it.
1714 * Might be a good idea to add that function to CORE as well.
1715 * No numbers below come from careful analysis or anything here,
1716 * except they are primes and SEED_C1 > 1E6 to get a full-width
1717 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1718 * probably be bigger too.
1721 # define SEED_C1 1000003
1722 #define SEED_C4 73819
1724 # define SEED_C1 25747
1725 #define SEED_C4 20639
1729 #define SEED_C5 26107
1732 #ifndef PERL_NO_DEV_RANDOM
1737 # include <starlet.h>
1738 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1739 * in 100-ns units, typically incremented ever 10 ms. */
1740 unsigned int when[2];
1742 # ifdef HAS_GETTIMEOFDAY
1743 struct timeval when;
1749 /* This test is an escape hatch, this symbol isn't set by Configure. */
1750 #ifndef PERL_NO_DEV_RANDOM
1751 #ifndef PERL_RANDOM_DEVICE
1752 /* /dev/random isn't used by default because reads from it will block
1753 * if there isn't enough entropy available. You can compile with
1754 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1755 * is enough real entropy to fill the seed. */
1756 # define PERL_RANDOM_DEVICE "/dev/urandom"
1758 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1760 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1769 _ckvmssts(sys$gettim(when));
1770 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1772 # ifdef HAS_GETTIMEOFDAY
1773 gettimeofday(&when,(struct timezone *) 0);
1774 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1777 u = (U32)SEED_C1 * when;
1780 u += SEED_C3 * (U32)getpid();
1781 u += SEED_C4 * (U32)(UV)PTR_CAST PL_stack_sp;
1782 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1783 u += SEED_C5 * (U32)(UV)PTR_CAST &when;
1790 djSP; dTARGET; tryAMAGICun(exp);
1794 value = Perl_exp(value);
1802 djSP; dTARGET; tryAMAGICun(log);
1807 RESTORE_NUMERIC_STANDARD();
1808 DIE(aTHX_ "Can't take log of %g", value);
1810 value = Perl_log(value);
1818 djSP; dTARGET; tryAMAGICun(sqrt);
1823 RESTORE_NUMERIC_STANDARD();
1824 DIE(aTHX_ "Can't take sqrt of %g", value);
1826 value = Perl_sqrt(value);
1839 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1845 (void)Perl_modf(value, &value);
1847 (void)Perl_modf(-value, &value);
1862 djSP; dTARGET; tryAMAGICun(abs);
1867 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1868 (iv = SvIVX(TOPs)) != IV_MIN) {
1890 XPUSHn(scan_hex(tmps, 99, &argtype));
1903 while (*tmps && isSPACE(*tmps))
1908 value = scan_hex(++tmps, 99, &argtype);
1909 else if (*tmps == 'b')
1910 value = scan_bin(++tmps, 99, &argtype);
1912 value = scan_oct(tmps, 99, &argtype);
1924 SETi( sv_len_utf8(TOPs) );
1928 SETi( sv_len(TOPs) );
1942 I32 lvalue = PL_op->op_flags & OPf_MOD;
1944 I32 arybase = PL_curcop->cop_arybase;
1948 SvTAINTED_off(TARG); /* decontaminate */
1952 repl = SvPV(sv, repl_len);
1959 tmps = SvPV(sv, curlen);
1961 utfcurlen = sv_len_utf8(sv);
1962 if (utfcurlen == curlen)
1970 if (pos >= arybase) {
1988 else if (len >= 0) {
1990 if (rem > (I32)curlen)
2004 if (ckWARN(WARN_SUBSTR) || lvalue || repl)
2005 Perl_warner(aTHX_ WARN_SUBSTR, "substr outside of string");
2010 sv_pos_u2b(sv, &pos, &rem);
2012 sv_setpvn(TARG, tmps, rem);
2013 if (lvalue) { /* it's an lvalue! */
2014 if (!SvGMAGICAL(sv)) {
2018 if (ckWARN(WARN_SUBSTR))
2019 Perl_warner(aTHX_ WARN_SUBSTR,
2020 "Attempt to use reference as lvalue in substr");
2022 if (SvOK(sv)) /* is it defined ? */
2023 (void)SvPOK_only(sv);
2025 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2028 if (SvTYPE(TARG) < SVt_PVLV) {
2029 sv_upgrade(TARG, SVt_PVLV);
2030 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
2034 if (LvTARG(TARG) != sv) {
2036 SvREFCNT_dec(LvTARG(TARG));
2037 LvTARG(TARG) = SvREFCNT_inc(sv);
2039 LvTARGOFF(TARG) = pos;
2040 LvTARGLEN(TARG) = rem;
2043 sv_insert(sv, pos, rem, repl, repl_len);
2046 PUSHs(TARG); /* avoid SvSETMAGIC here */
2053 register I32 size = POPi;
2054 register I32 offset = POPi;
2055 register SV *src = POPs;
2056 I32 lvalue = PL_op->op_flags & OPf_MOD;
2058 SvTAINTED_off(TARG); /* decontaminate */
2059 if (lvalue) { /* it's an lvalue! */
2060 if (SvTYPE(TARG) < SVt_PVLV) {
2061 sv_upgrade(TARG, SVt_PVLV);
2062 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2065 if (LvTARG(TARG) != src) {
2067 SvREFCNT_dec(LvTARG(TARG));
2068 LvTARG(TARG) = SvREFCNT_inc(src);
2070 LvTARGOFF(TARG) = offset;
2071 LvTARGLEN(TARG) = size;
2074 sv_setuv(TARG, do_vecget(src, offset, size));
2089 I32 arybase = PL_curcop->cop_arybase;
2094 offset = POPi - arybase;
2097 tmps = SvPV(big, biglen);
2098 if (IN_UTF8 && offset > 0)
2099 sv_pos_u2b(big, &offset, 0);
2102 else if (offset > biglen)
2104 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2105 (unsigned char*)tmps + biglen, little, 0)))
2108 retval = tmps2 - tmps;
2109 if (IN_UTF8 && retval > 0)
2110 sv_pos_b2u(big, &retval);
2111 PUSHi(retval + arybase);
2126 I32 arybase = PL_curcop->cop_arybase;
2132 tmps2 = SvPV(little, llen);
2133 tmps = SvPV(big, blen);
2137 if (IN_UTF8 && offset > 0)
2138 sv_pos_u2b(big, &offset, 0);
2139 offset = offset - arybase + llen;
2143 else if (offset > blen)
2145 if (!(tmps2 = rninstr(tmps, tmps + offset,
2146 tmps2, tmps2 + llen)))
2149 retval = tmps2 - tmps;
2150 if (IN_UTF8 && retval > 0)
2151 sv_pos_b2u(big, &retval);
2152 PUSHi(retval + arybase);
2158 djSP; dMARK; dORIGMARK; dTARGET;
2159 do_sprintf(TARG, SP-MARK, MARK+1);
2160 TAINT_IF(SvTAINTED(TARG));
2171 U8 *tmps = (U8*)POPpx;
2174 if (IN_UTF8 && (*tmps & 0x80))
2175 value = utf8_to_uv(tmps, &retlen);
2177 value = (UV)(*tmps & 255);
2188 (void)SvUPGRADE(TARG,SVt_PV);
2190 if (IN_UTF8 && value >= 128) {
2193 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2194 SvCUR_set(TARG, tmps - SvPVX(TARG));
2196 (void)SvPOK_only(TARG);
2206 (void)SvPOK_only(TARG);
2213 djSP; dTARGET; dPOPTOPssrl;
2216 char *tmps = SvPV(left, n_a);
2218 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2220 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2224 "The crypt() function is unimplemented due to excessive paranoia.");
2237 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2241 UV uv = utf8_to_uv(s, &ulen);
2243 if (PL_op->op_private & OPpLOCALE) {
2246 uv = toTITLE_LC_uni(uv);
2249 uv = toTITLE_utf8(s);
2251 tend = uv_to_utf8(tmpbuf, uv);
2253 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2255 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2256 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2260 s = (U8*)SvPV_force(sv, slen);
2261 Copy(tmpbuf, s, ulen, U8);
2265 if (!SvPADTMP(sv)) {
2271 s = (U8*)SvPV_force(sv, slen);
2273 if (PL_op->op_private & OPpLOCALE) {
2276 *s = toUPPER_LC(*s);
2294 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2298 UV uv = utf8_to_uv(s, &ulen);
2300 if (PL_op->op_private & OPpLOCALE) {
2303 uv = toLOWER_LC_uni(uv);
2306 uv = toLOWER_utf8(s);
2308 tend = uv_to_utf8(tmpbuf, uv);
2310 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2312 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2313 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2317 s = (U8*)SvPV_force(sv, slen);
2318 Copy(tmpbuf, s, ulen, U8);
2322 if (!SvPADTMP(sv)) {
2328 s = (U8*)SvPV_force(sv, slen);
2330 if (PL_op->op_private & OPpLOCALE) {
2333 *s = toLOWER_LC(*s);
2358 s = (U8*)SvPV(sv,len);
2360 sv_setpvn(TARG, "", 0);
2364 (void)SvUPGRADE(TARG, SVt_PV);
2365 SvGROW(TARG, (len * 2) + 1);
2366 (void)SvPOK_only(TARG);
2367 d = (U8*)SvPVX(TARG);
2369 if (PL_op->op_private & OPpLOCALE) {
2373 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2379 d = uv_to_utf8(d, toUPPER_utf8( s ));
2384 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2389 if (!SvPADTMP(sv)) {
2395 s = (U8*)SvPV_force(sv, len);
2397 register U8 *send = s + len;
2399 if (PL_op->op_private & OPpLOCALE) {
2402 for (; s < send; s++)
2403 *s = toUPPER_LC(*s);
2406 for (; s < send; s++)
2429 s = (U8*)SvPV(sv,len);
2431 sv_setpvn(TARG, "", 0);
2435 (void)SvUPGRADE(TARG, SVt_PV);
2436 SvGROW(TARG, (len * 2) + 1);
2437 (void)SvPOK_only(TARG);
2438 d = (U8*)SvPVX(TARG);
2440 if (PL_op->op_private & OPpLOCALE) {
2444 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2450 d = uv_to_utf8(d, toLOWER_utf8(s));
2455 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2460 if (!SvPADTMP(sv)) {
2467 s = (U8*)SvPV_force(sv, len);
2469 register U8 *send = s + len;
2471 if (PL_op->op_private & OPpLOCALE) {
2474 for (; s < send; s++)
2475 *s = toLOWER_LC(*s);
2478 for (; s < send; s++)
2493 register char *s = SvPV(sv,len);
2497 (void)SvUPGRADE(TARG, SVt_PV);
2498 SvGROW(TARG, (len * 2) + 1);
2503 STRLEN ulen = UTF8SKIP(s);
2526 SvCUR_set(TARG, d - SvPVX(TARG));
2527 (void)SvPOK_only(TARG);
2530 sv_setpvn(TARG, s, len);
2532 if (SvSMAGICAL(TARG))
2541 djSP; dMARK; dORIGMARK;
2543 register AV* av = (AV*)POPs;
2544 register I32 lval = PL_op->op_flags & OPf_MOD;
2545 I32 arybase = PL_curcop->cop_arybase;
2548 if (SvTYPE(av) == SVt_PVAV) {
2549 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2551 for (svp = MARK + 1; svp <= SP; svp++) {
2556 if (max > AvMAX(av))
2559 while (++MARK <= SP) {
2560 elem = SvIVx(*MARK);
2564 svp = av_fetch(av, elem, lval);
2566 if (!svp || *svp == &PL_sv_undef)
2567 DIE(aTHX_ PL_no_aelem, elem);
2568 if (PL_op->op_private & OPpLVAL_INTRO)
2569 save_aelem(av, elem, svp);
2571 *MARK = svp ? *svp : &PL_sv_undef;
2574 if (GIMME != G_ARRAY) {
2582 /* Associative arrays. */
2587 HV *hash = (HV*)POPs;
2589 I32 gimme = GIMME_V;
2590 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2593 /* might clobber stack_sp */
2594 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2599 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2600 if (gimme == G_ARRAY) {
2603 /* might clobber stack_sp */
2605 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
2610 else if (gimme == G_SCALAR)
2629 I32 gimme = GIMME_V;
2630 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2634 if (PL_op->op_private & OPpSLICE) {
2638 hvtype = SvTYPE(hv);
2639 while (++MARK <= SP) {
2640 if (hvtype == SVt_PVHV)
2641 sv = hv_delete_ent(hv, *MARK, discard, 0);
2643 DIE(aTHX_ "Not a HASH reference");
2644 *MARK = sv ? sv : &PL_sv_undef;
2648 else if (gimme == G_SCALAR) {
2657 if (SvTYPE(hv) == SVt_PVHV)
2658 sv = hv_delete_ent(hv, keysv, discard, 0);
2660 DIE(aTHX_ "Not a HASH reference");
2674 if (SvTYPE(hv) == SVt_PVHV) {
2675 if (hv_exists_ent(hv, tmpsv, 0))
2678 else if (SvTYPE(hv) == SVt_PVAV) {
2679 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2683 DIE(aTHX_ "Not a HASH reference");
2690 djSP; dMARK; dORIGMARK;
2691 register HV *hv = (HV*)POPs;
2692 register I32 lval = PL_op->op_flags & OPf_MOD;
2693 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2695 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2696 DIE(aTHX_ "Can't localize pseudo-hash element");
2698 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2699 while (++MARK <= SP) {
2703 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2704 svp = he ? &HeVAL(he) : 0;
2707 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2710 if (!svp || *svp == &PL_sv_undef) {
2712 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2714 if (PL_op->op_private & OPpLVAL_INTRO)
2715 save_helem(hv, keysv, svp);
2717 *MARK = svp ? *svp : &PL_sv_undef;
2720 if (GIMME != G_ARRAY) {
2728 /* List operators. */
2733 if (GIMME != G_ARRAY) {
2735 *MARK = *SP; /* unwanted list, return last item */
2737 *MARK = &PL_sv_undef;
2746 SV **lastrelem = PL_stack_sp;
2747 SV **lastlelem = PL_stack_base + POPMARK;
2748 SV **firstlelem = PL_stack_base + POPMARK + 1;
2749 register SV **firstrelem = lastlelem + 1;
2750 I32 arybase = PL_curcop->cop_arybase;
2751 I32 lval = PL_op->op_flags & OPf_MOD;
2752 I32 is_something_there = lval;
2754 register I32 max = lastrelem - lastlelem;
2755 register SV **lelem;
2758 if (GIMME != G_ARRAY) {
2759 ix = SvIVx(*lastlelem);
2764 if (ix < 0 || ix >= max)
2765 *firstlelem = &PL_sv_undef;
2767 *firstlelem = firstrelem[ix];
2773 SP = firstlelem - 1;
2777 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2783 if (ix < 0 || ix >= max)
2784 *lelem = &PL_sv_undef;
2786 is_something_there = TRUE;
2787 if (!(*lelem = firstrelem[ix]))
2788 *lelem = &PL_sv_undef;
2791 if (is_something_there)
2794 SP = firstlelem - 1;
2800 djSP; dMARK; dORIGMARK;
2801 I32 items = SP - MARK;
2802 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2803 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2810 djSP; dMARK; dORIGMARK;
2811 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2815 SV *val = NEWSV(46, 0);
2817 sv_setsv(val, *++MARK);
2818 else if (ckWARN(WARN_UNSAFE))
2819 Perl_warner(aTHX_ WARN_UNSAFE, "Odd number of elements in hash assignment");
2820 (void)hv_store_ent(hv,key,val,0);
2829 djSP; dMARK; dORIGMARK;
2830 register AV *ary = (AV*)*++MARK;
2834 register I32 offset;
2835 register I32 length;
2842 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2843 *MARK-- = SvTIED_obj((SV*)ary, mg);
2847 call_method("SPLICE",GIMME_V);
2856 offset = i = SvIVx(*MARK);
2858 offset += AvFILLp(ary) + 1;
2860 offset -= PL_curcop->cop_arybase;
2862 DIE(aTHX_ PL_no_aelem, i);
2864 length = SvIVx(*MARK++);
2866 length += AvFILLp(ary) - offset + 1;
2872 length = AvMAX(ary) + 1; /* close enough to infinity */
2876 length = AvMAX(ary) + 1;
2878 if (offset > AvFILLp(ary) + 1)
2879 offset = AvFILLp(ary) + 1;
2880 after = AvFILLp(ary) + 1 - (offset + length);
2881 if (after < 0) { /* not that much array */
2882 length += after; /* offset+length now in array */
2888 /* At this point, MARK .. SP-1 is our new LIST */
2891 diff = newlen - length;
2892 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2895 if (diff < 0) { /* shrinking the area */
2897 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2898 Copy(MARK, tmparyval, newlen, SV*);
2901 MARK = ORIGMARK + 1;
2902 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2903 MEXTEND(MARK, length);
2904 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2906 EXTEND_MORTAL(length);
2907 for (i = length, dst = MARK; i; i--) {
2908 sv_2mortal(*dst); /* free them eventualy */
2915 *MARK = AvARRAY(ary)[offset+length-1];
2918 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2919 SvREFCNT_dec(*dst++); /* free them now */
2922 AvFILLp(ary) += diff;
2924 /* pull up or down? */
2926 if (offset < after) { /* easier to pull up */
2927 if (offset) { /* esp. if nothing to pull */
2928 src = &AvARRAY(ary)[offset-1];
2929 dst = src - diff; /* diff is negative */
2930 for (i = offset; i > 0; i--) /* can't trust Copy */
2934 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2938 if (after) { /* anything to pull down? */
2939 src = AvARRAY(ary) + offset + length;
2940 dst = src + diff; /* diff is negative */
2941 Move(src, dst, after, SV*);
2943 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2944 /* avoid later double free */
2948 dst[--i] = &PL_sv_undef;
2951 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2953 *dst = NEWSV(46, 0);
2954 sv_setsv(*dst++, *src++);
2956 Safefree(tmparyval);
2959 else { /* no, expanding (or same) */
2961 New(452, tmparyval, length, SV*); /* so remember deletion */
2962 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2965 if (diff > 0) { /* expanding */
2967 /* push up or down? */
2969 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2973 Move(src, dst, offset, SV*);
2975 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2977 AvFILLp(ary) += diff;
2980 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2981 av_extend(ary, AvFILLp(ary) + diff);
2982 AvFILLp(ary) += diff;
2985 dst = AvARRAY(ary) + AvFILLp(ary);
2987 for (i = after; i; i--) {
2994 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2995 *dst = NEWSV(46, 0);
2996 sv_setsv(*dst++, *src++);
2998 MARK = ORIGMARK + 1;
2999 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3001 Copy(tmparyval, MARK, length, SV*);
3003 EXTEND_MORTAL(length);
3004 for (i = length, dst = MARK; i; i--) {
3005 sv_2mortal(*dst); /* free them eventualy */
3009 Safefree(tmparyval);
3013 else if (length--) {
3014 *MARK = tmparyval[length];
3017 while (length-- > 0)
3018 SvREFCNT_dec(tmparyval[length]);
3020 Safefree(tmparyval);
3023 *MARK = &PL_sv_undef;
3031 djSP; dMARK; dORIGMARK; dTARGET;
3032 register AV *ary = (AV*)*++MARK;
3033 register SV *sv = &PL_sv_undef;
3036 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3037 *MARK-- = SvTIED_obj((SV*)ary, mg);
3041 call_method("PUSH",G_SCALAR|G_DISCARD);
3046 /* Why no pre-extend of ary here ? */
3047 for (++MARK; MARK <= SP; MARK++) {
3050 sv_setsv(sv, *MARK);
3055 PUSHi( AvFILL(ary) + 1 );
3063 SV *sv = av_pop(av);
3065 (void)sv_2mortal(sv);
3074 SV *sv = av_shift(av);
3079 (void)sv_2mortal(sv);
3086 djSP; dMARK; dORIGMARK; dTARGET;
3087 register AV *ary = (AV*)*++MARK;
3092 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3093 *MARK-- = SvTIED_obj((SV*)ary, mg);
3097 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3102 av_unshift(ary, SP - MARK);
3105 sv_setsv(sv, *++MARK);
3106 (void)av_store(ary, i++, sv);
3110 PUSHi( AvFILL(ary) + 1 );
3120 if (GIMME == G_ARRAY) {
3131 register char *down;
3137 do_join(TARG, &PL_sv_no, MARK, SP);
3139 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3140 up = SvPV_force(TARG, len);
3142 if (IN_UTF8) { /* first reverse each character */
3143 U8* s = (U8*)SvPVX(TARG);
3144 U8* send = (U8*)(s + len);
3153 down = (char*)(s - 1);
3154 if (s > send || !((*down & 0xc0) == 0x80)) {
3155 if (ckWARN_d(WARN_UTF8))
3156 Perl_warner(aTHX_ WARN_UTF8,
3157 "Malformed UTF-8 character");
3169 down = SvPVX(TARG) + len - 1;
3175 (void)SvPOK_only(TARG);
3184 S_mul128(pTHX_ SV *sv, U8 m)
3187 char *s = SvPV(sv, len);
3191 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3192 SV *tmpNew = newSVpvn("0000000000", 10);
3194 sv_catsv(tmpNew, sv);
3195 SvREFCNT_dec(sv); /* free old sv */
3200 while (!*t) /* trailing '\0'? */
3203 i = ((*t - '0') << 7) + m;
3204 *(t--) = '0' + (i % 10);
3210 /* Explosives and implosives. */
3212 #if 'I' == 73 && 'J' == 74
3213 /* On an ASCII/ISO kind of system */
3214 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3217 Some other sort of character set - use memchr() so we don't match
3220 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3228 I32 gimme = GIMME_V;
3232 register char *pat = SvPV(left, llen);
3233 register char *s = SvPV(right, rlen);
3234 char *strend = s + rlen;
3236 register char *patend = pat + llen;
3241 /* These must not be in registers: */
3258 register U32 culong;
3261 #ifdef PERL_NATINT_PACK
3262 int natint; /* native integer */
3263 int unatint; /* unsigned native integer */
3266 if (gimme != G_ARRAY) { /* arrange to do first one only */
3268 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3269 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3271 while (isDIGIT(*patend) || *patend == '*')
3277 while (pat < patend) {
3279 datumtype = *pat++ & 0xFF;
3280 #ifdef PERL_NATINT_PACK
3283 if (isSPACE(datumtype))
3286 char *natstr = "sSiIlL";
3288 if (strchr(natstr, datumtype)) {
3289 #ifdef PERL_NATINT_PACK
3295 Perl_croak(aTHX_ "'!' allowed only after types %s", natstr);
3299 else if (*pat == '*') {
3300 len = strend - strbeg; /* long enough */
3303 else if (isDIGIT(*pat)) {
3305 while (isDIGIT(*pat)) {
3306 len = (len * 10) + (*pat++ - '0');
3308 Perl_croak(aTHX_ "Repeat count in unpack overflows");
3312 len = (datumtype != '@');
3315 Perl_croak(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
3316 case ',': /* grandfather in commas but with a warning */
3317 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
3318 Perl_warner(aTHX_ WARN_UNSAFE, "Invalid type in unpack: '%c'", (int)datumtype);
3321 if (len == 1 && pat[-1] != '1')
3330 if (len > strend - strbeg)
3331 DIE(aTHX_ "@ outside of string");
3335 if (len > s - strbeg)
3336 DIE(aTHX_ "X outside of string");
3340 if (len > strend - s)
3341 DIE(aTHX_ "x outside of string");
3346 DIE(aTHX_ "# must follow a numeric type");
3347 if (*pat != 'a' && *pat != 'A' && *pat != 'Z')
3348 DIE(aTHX_ "# must be followed by a, A or Z");
3351 pat++; /* ignore '*' for compatibility with pack */
3353 DIE(aTHX_ "# cannot take a count" );
3359 if (len > strend - s)
3362 goto uchar_checksum;
3363 sv = NEWSV(35, len);
3364 sv_setpvn(sv, s, len);
3366 if (datumtype == 'A' || datumtype == 'Z') {
3367 aptr = s; /* borrow register */
3368 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3373 else { /* 'A' strips both nulls and spaces */
3374 s = SvPVX(sv) + len - 1;
3375 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3379 SvCUR_set(sv, s - SvPVX(sv));
3380 s = aptr; /* unborrow register */
3382 XPUSHs(sv_2mortal(sv));
3386 if (pat[-1] == '*' || len > (strend - s) * 8)
3387 len = (strend - s) * 8;
3390 Newz(601, PL_bitcount, 256, char);
3391 for (bits = 1; bits < 256; bits++) {
3392 if (bits & 1) PL_bitcount[bits]++;
3393 if (bits & 2) PL_bitcount[bits]++;
3394 if (bits & 4) PL_bitcount[bits]++;
3395 if (bits & 8) PL_bitcount[bits]++;
3396 if (bits & 16) PL_bitcount[bits]++;
3397 if (bits & 32) PL_bitcount[bits]++;
3398 if (bits & 64) PL_bitcount[bits]++;
3399 if (bits & 128) PL_bitcount[bits]++;
3403 culong += PL_bitcount[*(unsigned char*)s++];
3408 if (datumtype == 'b') {
3410 if (bits & 1) culong++;
3416 if (bits & 128) culong++;
3423 sv = NEWSV(35, len + 1);
3426 aptr = pat; /* borrow register */
3428 if (datumtype == 'b') {
3430 for (len = 0; len < aint; len++) {
3431 if (len & 7) /*SUPPRESS 595*/
3435 *pat++ = '0' + (bits & 1);
3440 for (len = 0; len < aint; len++) {
3445 *pat++ = '0' + ((bits & 128) != 0);
3449 pat = aptr; /* unborrow register */
3450 XPUSHs(sv_2mortal(sv));
3454 if (pat[-1] == '*' || len > (strend - s) * 2)
3455 len = (strend - s) * 2;
3456 sv = NEWSV(35, len + 1);
3459 aptr = pat; /* borrow register */
3461 if (datumtype == 'h') {
3463 for (len = 0; len < aint; len++) {
3468 *pat++ = PL_hexdigit[bits & 15];
3473 for (len = 0; len < aint; len++) {
3478 *pat++ = PL_hexdigit[(bits >> 4) & 15];
3482 pat = aptr; /* unborrow register */
3483 XPUSHs(sv_2mortal(sv));
3486 if (len > strend - s)
3491 if (aint >= 128) /* fake up signed chars */
3501 if (aint >= 128) /* fake up signed chars */
3504 sv_setiv(sv, (IV)aint);
3505 PUSHs(sv_2mortal(sv));
3510 if (len > strend - s)
3525 sv_setiv(sv, (IV)auint);
3526 PUSHs(sv_2mortal(sv));
3531 if (len > strend - s)
3534 while (len-- > 0 && s < strend) {
3535 auint = utf8_to_uv((U8*)s, &along);
3538 cdouble += (NV)auint;
3546 while (len-- > 0 && s < strend) {
3547 auint = utf8_to_uv((U8*)s, &along);
3550 sv_setuv(sv, (UV)auint);
3551 PUSHs(sv_2mortal(sv));
3556 #if SHORTSIZE == SIZE16
3557 along = (strend - s) / SIZE16;
3559 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3564 #if SHORTSIZE != SIZE16
3568 COPYNN(s, &ashort, sizeof(short));
3579 #if SHORTSIZE > SIZE16
3591 #if SHORTSIZE != SIZE16
3595 COPYNN(s, &ashort, sizeof(short));
3598 sv_setiv(sv, (IV)ashort);
3599 PUSHs(sv_2mortal(sv));
3607 #if SHORTSIZE > SIZE16
3613 sv_setiv(sv, (IV)ashort);
3614 PUSHs(sv_2mortal(sv));
3622 #if SHORTSIZE == SIZE16
3623 along = (strend - s) / SIZE16;
3625 unatint = natint && datumtype == 'S';
3626 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3631 #if SHORTSIZE != SIZE16
3633 unsigned short aushort;
3635 COPYNN(s, &aushort, sizeof(unsigned short));
3636 s += sizeof(unsigned short);
3644 COPY16(s, &aushort);
3647 if (datumtype == 'n')
3648 aushort = PerlSock_ntohs(aushort);
3651 if (datumtype == 'v')
3652 aushort = vtohs(aushort);
3661 #if SHORTSIZE != SIZE16
3663 unsigned short aushort;
3665 COPYNN(s, &aushort, sizeof(unsigned short));
3666 s += sizeof(unsigned short);
3668 sv_setiv(sv, (UV)aushort);
3669 PUSHs(sv_2mortal(sv));
3676 COPY16(s, &aushort);
3680 if (datumtype == 'n')
3681 aushort = PerlSock_ntohs(aushort);
3684 if (datumtype == 'v')
3685 aushort = vtohs(aushort);
3687 sv_setiv(sv, (UV)aushort);
3688 PUSHs(sv_2mortal(sv));
3694 along = (strend - s) / sizeof(int);
3699 Copy(s, &aint, 1, int);
3702 cdouble += (NV)aint;
3711 Copy(s, &aint, 1, int);
3715 /* Without the dummy below unpack("i", pack("i",-1))
3716 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3717 * cc with optimization turned on.
3719 * The bug was detected in
3720 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3721 * with optimization (-O4) turned on.
3722 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3723 * does not have this problem even with -O4.
3725 * This bug was reported as DECC_BUGS 1431
3726 * and tracked internally as GEM_BUGS 7775.
3728 * The bug is fixed in
3729 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3730 * UNIX V4.0F support: DEC C V5.9-006 or later
3731 * UNIX V4.0E support: DEC C V5.8-011 or later
3734 * See also few lines later for the same bug.
3737 sv_setiv(sv, (IV)aint) :
3739 sv_setiv(sv, (IV)aint);
3740 PUSHs(sv_2mortal(sv));
3745 along = (strend - s) / sizeof(unsigned int);
3750 Copy(s, &auint, 1, unsigned int);
3751 s += sizeof(unsigned int);
3753 cdouble += (NV)auint;
3762 Copy(s, &auint, 1, unsigned int);
3763 s += sizeof(unsigned int);
3766 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3767 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3768 * See details few lines earlier. */
3770 sv_setuv(sv, (UV)auint) :
3772 sv_setuv(sv, (UV)auint);
3773 PUSHs(sv_2mortal(sv));
3778 #if LONGSIZE == SIZE32
3779 along = (strend - s) / SIZE32;
3781 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3786 #if LONGSIZE != SIZE32
3790 COPYNN(s, &along, sizeof(long));
3793 cdouble += (NV)along;
3803 #if LONGSIZE > SIZE32
3804 if (along > 2147483647)
3805 along -= 4294967296;
3809 cdouble += (NV)along;
3818 #if LONGSIZE != SIZE32
3822 COPYNN(s, &along, sizeof(long));
3825 sv_setiv(sv, (IV)along);
3826 PUSHs(sv_2mortal(sv));
3834 #if LONGSIZE > SIZE32
3835 if (along > 2147483647)
3836 along -= 4294967296;
3840 sv_setiv(sv, (IV)along);
3841 PUSHs(sv_2mortal(sv));
3849 #if LONGSIZE == SIZE32
3850 along = (strend - s) / SIZE32;
3852 unatint = natint && datumtype == 'L';
3853 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3858 #if LONGSIZE != SIZE32
3860 unsigned long aulong;
3862 COPYNN(s, &aulong, sizeof(unsigned long));
3863 s += sizeof(unsigned long);
3865 cdouble += (NV)aulong;
3877 if (datumtype == 'N')
3878 aulong = PerlSock_ntohl(aulong);
3881 if (datumtype == 'V')
3882 aulong = vtohl(aulong);
3885 cdouble += (NV)aulong;
3894 #if LONGSIZE != SIZE32
3896 unsigned long aulong;
3898 COPYNN(s, &aulong, sizeof(unsigned long));
3899 s += sizeof(unsigned long);
3901 sv_setuv(sv, (UV)aulong);
3902 PUSHs(sv_2mortal(sv));
3912 if (datumtype == 'N')
3913 aulong = PerlSock_ntohl(aulong);
3916 if (datumtype == 'V')
3917 aulong = vtohl(aulong);
3920 sv_setuv(sv, (UV)aulong);
3921 PUSHs(sv_2mortal(sv));
3927 along = (strend - s) / sizeof(char*);
3933 if (sizeof(char*) > strend - s)
3936 Copy(s, &aptr, 1, char*);
3942 PUSHs(sv_2mortal(sv));
3952 while ((len > 0) && (s < strend)) {
3953 auv = (auv << 7) | (*s & 0x7f);
3954 if (!(*s++ & 0x80)) {
3958 PUSHs(sv_2mortal(sv));
3962 else if (++bytes >= sizeof(UV)) { /* promote to string */
3966 sv = Perl_newSVpvf(aTHX_ "%.*Vu", (int)TYPE_DIGITS(UV), auv);
3967 while (s < strend) {
3968 sv = mul128(sv, *s & 0x7f);
3969 if (!(*s++ & 0x80)) {
3978 PUSHs(sv_2mortal(sv));
3983 if ((s >= strend) && bytes)
3984 Perl_croak(aTHX_ "Unterminated compressed integer");
3989 if (sizeof(char*) > strend - s)
3992 Copy(s, &aptr, 1, char*);
3997 sv_setpvn(sv, aptr, len);
3998 PUSHs(sv_2mortal(sv));
4002 along = (strend - s) / sizeof(Quad_t);
4008 if (s + sizeof(Quad_t) > strend)
4011 Copy(s, &aquad, 1, Quad_t);
4012 s += sizeof(Quad_t);
4015 if (aquad >= IV_MIN && aquad <= IV_MAX)
4016 sv_setiv(sv, (IV)aquad);
4018 sv_setnv(sv, (NV)aquad);
4019 PUSHs(sv_2mortal(sv));
4023 along = (strend - s) / sizeof(Quad_t);
4029 if (s + sizeof(Uquad_t) > strend)
4032 Copy(s, &auquad, 1, Uquad_t);
4033 s += sizeof(Uquad_t);
4036 if (auquad <= UV_MAX)
4037 sv_setuv(sv, (UV)auquad);
4039 sv_setnv(sv, (NV)auquad);
4040 PUSHs(sv_2mortal(sv));
4044 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4047 along = (strend - s) / sizeof(float);
4052 Copy(s, &afloat, 1, float);
4061 Copy(s, &afloat, 1, float);
4064 sv_setnv(sv, (NV)afloat);
4065 PUSHs(sv_2mortal(sv));
4071 along = (strend - s) / sizeof(double);
4076 Copy(s, &adouble, 1, double);
4077 s += sizeof(double);
4085 Copy(s, &adouble, 1, double);
4086 s += sizeof(double);
4088 sv_setnv(sv, (NV)adouble);
4089 PUSHs(sv_2mortal(sv));
4095 * Initialise the decode mapping. By using a table driven
4096 * algorithm, the code will be character-set independent
4097 * (and just as fast as doing character arithmetic)
4099 if (PL_uudmap['M'] == 0) {
4102 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4103 PL_uudmap[PL_uuemap[i]] = i;
4105 * Because ' ' and '`' map to the same value,
4106 * we need to decode them both the same.
4111 along = (strend - s) * 3 / 4;
4112 sv = NEWSV(42, along);
4115 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4120 len = PL_uudmap[*s++] & 077;
4122 if (s < strend && ISUUCHAR(*s))
4123 a = PL_uudmap[*s++] & 077;
4126 if (s < strend && ISUUCHAR(*s))
4127 b = PL_uudmap[*s++] & 077;
4130 if (s < strend && ISUUCHAR(*s))
4131 c = PL_uudmap[*s++] & 077;
4134 if (s < strend && ISUUCHAR(*s))
4135 d = PL_uudmap[*s++] & 077;
4138 hunk[0] = (a << 2) | (b >> 4);
4139 hunk[1] = (b << 4) | (c >> 2);
4140 hunk[2] = (c << 6) | d;
4141 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4146 else if (s[1] == '\n') /* possible checksum byte */
4149 XPUSHs(sv_2mortal(sv));
4154 if (strchr("fFdD", datumtype) ||
4155 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4159 while (checksum >= 16) {
4163 while (checksum >= 4) {
4169 along = (1 << checksum) - 1;
4170 while (cdouble < 0.0)
4172 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
4173 sv_setnv(sv, cdouble);
4176 if (checksum < 32) {
4177 aulong = (1 << checksum) - 1;
4180 sv_setuv(sv, (UV)culong);
4182 XPUSHs(sv_2mortal(sv));
4186 if (SP == oldsp && gimme == G_SCALAR)
4187 PUSHs(&PL_sv_undef);
4192 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
4196 *hunk = PL_uuemap[len];
4197 sv_catpvn(sv, hunk, 1);
4200 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4201 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4202 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4203 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4204 sv_catpvn(sv, hunk, 4);
4209 char r = (len > 1 ? s[1] : '\0');
4210 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4211 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4212 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4213 hunk[3] = PL_uuemap[0];
4214 sv_catpvn(sv, hunk, 4);
4216 sv_catpvn(sv, "\n", 1);
4220 S_is_an_int(pTHX_ char *s, STRLEN l)
4223 SV *result = newSVpvn(s, l);
4224 char *result_c = SvPV(result, n_a); /* convenience */
4225 char *out = result_c;
4235 SvREFCNT_dec(result);
4258 SvREFCNT_dec(result);
4264 SvCUR_set(result, out - result_c);
4268 /* pnum must be '\0' terminated */
4270 S_div128(pTHX_ SV *pnum, bool *done)
4273 char *s = SvPV(pnum, len);
4282 i = m * 10 + (*t - '0');
4284 r = (i >> 7); /* r < 10 */
4291 SvCUR_set(pnum, (STRLEN) (t - s));
4298 djSP; dMARK; dORIGMARK; dTARGET;
4299 register SV *cat = TARG;
4302 register char *pat = SvPVx(*++MARK, fromlen);
4303 register char *patend = pat + fromlen;
4308 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4309 static char *space10 = " ";
4311 /* These must not be in registers: */
4326 #ifdef PERL_NATINT_PACK
4327 int natint; /* native integer */
4332 sv_setpvn(cat, "", 0);
4333 while (pat < patend) {
4334 SV *lengthcode = Nullsv;
4335 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *MARK++ : &PL_sv_no)
4336 datumtype = *pat++ & 0xFF;
4337 #ifdef PERL_NATINT_PACK
4340 if (isSPACE(datumtype))
4343 char *natstr = "sSiIlL";
4345 if (strchr(natstr, datumtype)) {
4346 #ifdef PERL_NATINT_PACK
4352 Perl_croak(aTHX_ "'!' allowed only after types %s", natstr);
4355 len = strchr("@Xxu", datumtype) ? 0 : items;
4358 else if (isDIGIT(*pat)) {
4360 while (isDIGIT(*pat)) {
4361 len = (len * 10) + (*pat++ - '0');
4363 Perl_croak(aTHX_ "Repeat count in pack overflows");
4370 if (*pat != 'a' && *pat != 'A' && *pat != 'Z' || pat[1] != '*')
4371 DIE(aTHX_ "# must be followed by a*, A* or Z*");
4372 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
4373 ? *MARK : &PL_sv_no)));
4377 Perl_croak(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
4378 case ',': /* grandfather in commas but with a warning */
4379 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
4380 Perl_warner(aTHX_ WARN_UNSAFE,
4381 "Invalid type in pack: '%c'", (int)datumtype);
4384 DIE(aTHX_ "%% may only be used in unpack");
4395 if (SvCUR(cat) < len)
4396 DIE(aTHX_ "X outside of string");
4403 sv_catpvn(cat, null10, 10);
4406 sv_catpvn(cat, null10, len);
4412 aptr = SvPV(fromstr, fromlen);
4416 sv_catpvn(cat, aptr, len);
4418 sv_catpvn(cat, aptr, fromlen);
4420 if (datumtype == 'A') {
4422 sv_catpvn(cat, space10, 10);
4425 sv_catpvn(cat, space10, len);
4429 sv_catpvn(cat, null10, 10);
4432 sv_catpvn(cat, null10, len);
4439 char *savepat = pat;
4444 aptr = SvPV(fromstr, fromlen);
4449 SvCUR(cat) += (len+7)/8;
4450 SvGROW(cat, SvCUR(cat) + 1);
4451 aptr = SvPVX(cat) + aint;
4456 if (datumtype == 'B') {
4457 for (len = 0; len++ < aint;) {
4458 items |= *pat++ & 1;
4462 *aptr++ = items & 0xff;
4468 for (len = 0; len++ < aint;) {
4474 *aptr++ = items & 0xff;
4480 if (datumtype == 'B')
4481 items <<= 7 - (aint & 7);
4483 items >>= 7 - (aint & 7);
4484 *aptr++ = items & 0xff;
4486 pat = SvPVX(cat) + SvCUR(cat);
4497 char *savepat = pat;
4502 aptr = SvPV(fromstr, fromlen);
4507 SvCUR(cat) += (len+1)/2;
4508 SvGROW(cat, SvCUR(cat) + 1);
4509 aptr = SvPVX(cat) + aint;
4514 if (datumtype == 'H') {
4515 for (len = 0; len++ < aint;) {
4517 items |= ((*pat++ & 15) + 9) & 15;
4519 items |= *pat++ & 15;
4523 *aptr++ = items & 0xff;
4529 for (len = 0; len++ < aint;) {
4531 items |= (((*pat++ & 15) + 9) & 15) << 4;
4533 items |= (*pat++ & 15) << 4;
4537 *aptr++ = items & 0xff;
4543 *aptr++ = items & 0xff;
4544 pat = SvPVX(cat) + SvCUR(cat);
4556 aint = SvIV(fromstr);
4558 sv_catpvn(cat, &achar, sizeof(char));
4564 auint = SvUV(fromstr);
4565 SvGROW(cat, SvCUR(cat) + 10);
4566 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4571 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4576 afloat = (float)SvNV(fromstr);
4577 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4584 adouble = (double)SvNV(fromstr);
4585 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4591 ashort = (I16)SvIV(fromstr);
4593 ashort = PerlSock_htons(ashort);
4595 CAT16(cat, &ashort);
4601 ashort = (I16)SvIV(fromstr);
4603 ashort = htovs(ashort);
4605 CAT16(cat, &ashort);
4609 #if SHORTSIZE != SIZE16
4611 unsigned short aushort;
4615 aushort = SvUV(fromstr);
4616 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4626 aushort = (U16)SvUV(fromstr);
4627 CAT16(cat, &aushort);
4633 #if SHORTSIZE != SIZE16
4639 ashort = SvIV(fromstr);
4640 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4648 ashort = (I16)SvIV(fromstr);
4649 CAT16(cat, &ashort);
4656 auint = SvUV(fromstr);
4657 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4663 adouble = Perl_floor(SvNV(fromstr));
4666 Perl_croak(aTHX_ "Cannot compress negative numbers");
4672 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4673 adouble <= UV_MAX_cxux
4680 char buf[1 + sizeof(UV)];
4681 char *in = buf + sizeof(buf);
4682 UV auv = U_V(adouble);
4685 *--in = (auv & 0x7f) | 0x80;
4688 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4689 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4691 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4692 char *from, *result, *in;
4697 /* Copy string and check for compliance */
4698 from = SvPV(fromstr, len);
4699 if ((norm = is_an_int(from, len)) == NULL)
4700 Perl_croak(aTHX_ "can compress only unsigned integer");
4702 New('w', result, len, char);
4706 *--in = div128(norm, &done) | 0x80;
4707 result[len - 1] &= 0x7F; /* clear continue bit */
4708 sv_catpvn(cat, in, (result + len) - in);
4710 SvREFCNT_dec(norm); /* free norm */
4712 else if (SvNOKp(fromstr)) {
4713 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4714 char *in = buf + sizeof(buf);
4717 double next = floor(adouble / 128);
4718 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4719 if (--in < buf) /* this cannot happen ;-) */
4720 Perl_croak(aTHX_ "Cannot compress integer");
4722 } while (adouble > 0);
4723 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4724 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4727 Perl_croak(aTHX_ "Cannot compress non integer");
4733 aint = SvIV(fromstr);
4734 sv_catpvn(cat, (char*)&aint, sizeof(int));
4740 aulong = SvUV(fromstr);
4742 aulong = PerlSock_htonl(aulong);
4744 CAT32(cat, &aulong);
4750 aulong = SvUV(fromstr);
4752 aulong = htovl(aulong);
4754 CAT32(cat, &aulong);
4758 #if LONGSIZE != SIZE32
4760 unsigned long aulong;
4764 aulong = SvUV(fromstr);
4765 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4773 aulong = SvUV(fromstr);
4774 CAT32(cat, &aulong);
4779 #if LONGSIZE != SIZE32
4785 along = SvIV(fromstr);
4786 sv_catpvn(cat, (char *)&along, sizeof(long));
4794 along = SvIV(fromstr);
4803 auquad = (Uquad_t)SvUV(fromstr);
4804 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4810 aquad = (Quad_t)SvIV(fromstr);
4811 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4814 #endif /* HAS_QUAD */
4816 len = 1; /* assume SV is correct length */
4821 if (fromstr == &PL_sv_undef)
4825 /* XXX better yet, could spirit away the string to
4826 * a safe spot and hang on to it until the result
4827 * of pack() (and all copies of the result) are
4830 if (ckWARN(WARN_UNSAFE) && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4831 Perl_warner(aTHX_ WARN_UNSAFE,
4832 "Attempt to pack pointer to temporary value");
4833 if (SvPOK(fromstr) || SvNIOK(fromstr))
4834 aptr = SvPV(fromstr,n_a);
4836 aptr = SvPV_force(fromstr,n_a);
4838 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4843 aptr = SvPV(fromstr, fromlen);
4844 SvGROW(cat, fromlen * 4 / 3);
4849 while (fromlen > 0) {
4856 doencodes(cat, aptr, todo);
4875 register I32 limit = POPi; /* note, negative is forever */
4878 register char *s = SvPV(sv, len);
4879 char *strend = s + len;
4881 register REGEXP *rx;
4885 I32 maxiters = (strend - s) + 10;
4888 I32 origlimit = limit;
4891 AV *oldstack = PL_curstack;
4892 I32 gimme = GIMME_V;
4893 I32 oldsave = PL_savestack_ix;
4894 I32 make_mortal = 1;
4895 MAGIC *mg = (MAGIC *) NULL;
4898 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4903 DIE(aTHX_ "panic: do_split");
4904 rx = pm->op_pmregexp;
4906 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4907 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4909 if (pm->op_pmreplroot)
4910 ary = GvAVn((GV*)pm->op_pmreplroot);
4911 else if (gimme != G_ARRAY)
4913 ary = (AV*)PL_curpad[0];
4915 ary = GvAVn(PL_defgv);
4916 #endif /* USE_THREADS */
4919 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4925 if (mg = SvTIED_mg((SV*)ary, 'P')) {
4927 XPUSHs(SvTIED_obj((SV*)ary, mg));
4933 for (i = AvFILLp(ary); i >= 0; i--)
4934 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4936 /* temporarily switch stacks */
4937 SWITCHSTACK(PL_curstack, ary);
4941 base = SP - PL_stack_base;
4943 if (pm->op_pmflags & PMf_SKIPWHITE) {
4944 if (pm->op_pmflags & PMf_LOCALE) {
4945 while (isSPACE_LC(*s))
4953 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4954 SAVEINT(PL_multiline);
4955 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4959 limit = maxiters + 2;
4960 if (pm->op_pmflags & PMf_WHITE) {
4963 while (m < strend &&
4964 !((pm->op_pmflags & PMf_LOCALE)
4965 ? isSPACE_LC(*m) : isSPACE(*m)))
4970 dstr = NEWSV(30, m-s);
4971 sv_setpvn(dstr, s, m-s);
4977 while (s < strend &&
4978 ((pm->op_pmflags & PMf_LOCALE)
4979 ? isSPACE_LC(*s) : isSPACE(*s)))
4983 else if (strEQ("^", rx->precomp)) {
4986 for (m = s; m < strend && *m != '\n'; m++) ;
4990 dstr = NEWSV(30, m-s);
4991 sv_setpvn(dstr, s, m-s);
4998 else if ((rx->reganch & RE_USE_INTUIT) && !rx->nparens
4999 && (rx->reganch & ROPT_CHECK_ALL)
5000 && !(rx->reganch & ROPT_ANCH)) {
5001 int tail = (rx->reganch & RE_INTUIT_TAIL);
5002 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
5006 if (len == 1 && !tail) {
5010 for (m = s; m < strend && *m != c; m++) ;
5013 dstr = NEWSV(30, m-s);
5014 sv_setpvn(dstr, s, m-s);
5023 while (s < strend && --limit &&
5024 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5025 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
5028 dstr = NEWSV(31, m-s);
5029 sv_setpvn(dstr, s, m-s);
5033 s = m + len; /* Fake \n at the end */
5038 maxiters += (strend - s) * rx->nparens;
5039 while (s < strend && --limit
5040 /* && (!rx->check_substr
5041 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
5043 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
5044 1 /* minend */, sv, NULL, 0))
5046 TAINT_IF(RX_MATCH_TAINTED(rx));
5047 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5052 strend = s + (strend - m);
5054 m = rx->startp[0] + orig;
5055 dstr = NEWSV(32, m-s);
5056 sv_setpvn(dstr, s, m-s);
5061 for (i = 1; i <= rx->nparens; i++) {
5062 s = rx->startp[i] + orig;
5063 m = rx->endp[i] + orig;
5065 dstr = NEWSV(33, m-s);
5066 sv_setpvn(dstr, s, m-s);
5069 dstr = NEWSV(33, 0);
5075 s = rx->endp[0] + orig;
5079 LEAVE_SCOPE(oldsave);
5080 iters = (SP - PL_stack_base) - base;
5081 if (iters > maxiters)
5082 DIE(aTHX_ "Split loop");
5084 /* keep field after final delim? */
5085 if (s < strend || (iters && origlimit)) {
5086 dstr = NEWSV(34, strend-s);
5087 sv_setpvn(dstr, s, strend-s);
5093 else if (!origlimit) {
5094 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5100 SWITCHSTACK(ary, oldstack);
5101 if (SvSMAGICAL(ary)) {
5106 if (gimme == G_ARRAY) {
5108 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5116 call_method("PUSH",G_SCALAR|G_DISCARD);
5119 if (gimme == G_ARRAY) {
5120 /* EXTEND should not be needed - we just popped them */
5122 for (i=0; i < iters; i++) {
5123 SV **svp = av_fetch(ary, i, FALSE);
5124 PUSHs((svp) ? *svp : &PL_sv_undef);
5131 if (gimme == G_ARRAY)
5134 if (iters || !pm->op_pmreplroot) {
5144 Perl_unlock_condpair(pTHX_ void *svv)
5147 MAGIC *mg = mg_find((SV*)svv, 'm');
5150 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
5151 MUTEX_LOCK(MgMUTEXP(mg));
5152 if (MgOWNER(mg) != thr)
5153 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
5155 COND_SIGNAL(MgOWNERCONDP(mg));
5156 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
5157 (unsigned long)thr, (unsigned long)svv);)
5158 MUTEX_UNLOCK(MgMUTEXP(mg));
5160 #endif /* USE_THREADS */
5173 mg = condpair_magic(sv);
5174 MUTEX_LOCK(MgMUTEXP(mg));
5175 if (MgOWNER(mg) == thr)
5176 MUTEX_UNLOCK(MgMUTEXP(mg));
5179 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
5181 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
5182 (unsigned long)thr, (unsigned long)sv);)
5183 MUTEX_UNLOCK(MgMUTEXP(mg));
5184 SAVEDESTRUCTOR(Perl_unlock_condpair, sv);
5186 #endif /* USE_THREADS */
5187 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5188 || SvTYPE(retsv) == SVt_PVCV) {
5189 retsv = refto(retsv);
5200 if (PL_op->op_private & OPpLVAL_INTRO)
5201 PUSHs(*save_threadsv(PL_op->op_targ));
5203 PUSHs(THREADSV(PL_op->op_targ));
5206 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
5207 #endif /* USE_THREADS */