3 * Copyright (c) 1991-2000, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "It's a big house this, and very peculiar. Always a bit more to discover,
12 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
20 * The compiler on Concurrent CX/UX systems has a subtle bug which only
21 * seems to show up when compiling pp.c - it generates the wrong double
22 * precision constant value for (double)UV_MAX when used inline in the body
23 * of the code below, so this makes a static variable up front (which the
24 * compiler seems to get correct) and uses it in place of UV_MAX below.
26 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
27 static double UV_MAX_cxux = ((double)UV_MAX);
31 * 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)
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 LONGSIZE > 4 && defined(_CRAY)
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) {
246 if (cUNOP->op_targ) {
248 SV *namesv = PL_curpad[cUNOP->op_targ];
249 name = SvPV(namesv, len);
250 gv = (GV*)NEWSV(0,0);
251 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
254 name = CopSTASHPV(PL_curcop);
257 sv_upgrade(sv, SVt_RV);
263 if (PL_op->op_flags & OPf_REF ||
264 PL_op->op_private & HINT_STRICT_REFS)
265 DIE(aTHX_ PL_no_usym, "a symbol");
266 if (ckWARN(WARN_UNINITIALIZED))
271 if ((PL_op->op_flags & OPf_SPECIAL) &&
272 !(PL_op->op_flags & OPf_MOD))
274 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
279 if (PL_op->op_private & HINT_STRICT_REFS)
280 DIE(aTHX_ PL_no_symref, sym, "a symbol");
281 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
285 if (PL_op->op_private & OPpLVAL_INTRO)
286 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
297 tryAMAGICunDEREF(to_sv);
300 switch (SvTYPE(sv)) {
304 DIE(aTHX_ "Not a SCALAR reference");
312 if (SvTYPE(gv) != SVt_PVGV) {
313 if (SvGMAGICAL(sv)) {
319 if (PL_op->op_flags & OPf_REF ||
320 PL_op->op_private & HINT_STRICT_REFS)
321 DIE(aTHX_ PL_no_usym, "a SCALAR");
322 if (ckWARN(WARN_UNINITIALIZED))
327 if ((PL_op->op_flags & OPf_SPECIAL) &&
328 !(PL_op->op_flags & OPf_MOD))
330 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
335 if (PL_op->op_private & HINT_STRICT_REFS)
336 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
337 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
342 if (PL_op->op_flags & OPf_MOD) {
343 if (PL_op->op_private & OPpLVAL_INTRO)
344 sv = save_scalar((GV*)TOPs);
345 else if (PL_op->op_private & OPpDEREF)
346 vivify_ref(sv, PL_op->op_private & OPpDEREF);
356 SV *sv = AvARYLEN(av);
358 AvARYLEN(av) = sv = NEWSV(0,0);
359 sv_upgrade(sv, SVt_IV);
360 sv_magic(sv, (SV*)av, '#', Nullch, 0);
368 djSP; dTARGET; dPOPss;
370 if (PL_op->op_flags & OPf_MOD) {
371 if (SvTYPE(TARG) < SVt_PVLV) {
372 sv_upgrade(TARG, SVt_PVLV);
373 sv_magic(TARG, Nullsv, '.', Nullch, 0);
377 if (LvTARG(TARG) != sv) {
379 SvREFCNT_dec(LvTARG(TARG));
380 LvTARG(TARG) = SvREFCNT_inc(sv);
382 PUSHs(TARG); /* no SvSETMAGIC */
388 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
389 mg = mg_find(sv, 'g');
390 if (mg && mg->mg_len >= 0) {
394 PUSHi(i + PL_curcop->cop_arybase);
408 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
409 /* (But not in defined().) */
410 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
413 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
414 if ((PL_op->op_private & OPpLVAL_INTRO) && !CvLVALUE(cv))
415 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
418 cv = (CV*)&PL_sv_undef;
432 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
433 char *s = SvPVX(TOPs);
434 if (strnEQ(s, "CORE::", 6)) {
437 code = keyword(s + 6, SvCUR(TOPs) - 6);
438 if (code < 0) { /* Overridable. */
439 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
440 int i = 0, n = 0, seen_question = 0;
442 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
444 while (i < MAXO) { /* The slow way. */
445 if (strEQ(s + 6, PL_op_name[i])
446 || strEQ(s + 6, PL_op_desc[i]))
452 goto nonesuch; /* Should not happen... */
454 oa = PL_opargs[i] >> OASHIFT;
456 if (oa & OA_OPTIONAL) {
460 else if (seen_question)
461 goto set; /* XXXX system, exec */
462 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
463 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
466 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
467 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
471 ret = sv_2mortal(newSVpvn(str, n - 1));
473 else if (code) /* Non-Overridable */
475 else { /* None such */
477 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
481 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
483 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
492 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
494 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
510 if (GIMME != G_ARRAY) {
514 *MARK = &PL_sv_undef;
515 *MARK = refto(*MARK);
519 EXTEND_MORTAL(SP - MARK);
521 *MARK = refto(*MARK);
526 S_refto(pTHX_ SV *sv)
530 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
533 if (!(sv = LvTARG(sv)))
536 (void)SvREFCNT_inc(sv);
538 else if (SvTYPE(sv) == SVt_PVAV) {
539 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
542 (void)SvREFCNT_inc(sv);
544 else if (SvPADTMP(sv))
548 (void)SvREFCNT_inc(sv);
551 sv_upgrade(rv, SVt_RV);
565 if (sv && SvGMAGICAL(sv))
568 if (!sv || !SvROK(sv))
572 pv = sv_reftype(sv,TRUE);
573 PUSHp(pv, strlen(pv));
583 stash = CopSTASH(PL_curcop);
587 char *ptr = SvPV(ssv,len);
588 if (ckWARN(WARN_MISC) && len == 0)
589 Perl_warner(aTHX_ WARN_MISC,
590 "Explicit blessing to '' (assuming package main)");
591 stash = gv_stashpvn(ptr, len, TRUE);
594 (void)sv_bless(TOPs, stash);
608 elem = SvPV(sv, n_a);
612 switch (elem ? *elem : '\0')
615 if (strEQ(elem, "ARRAY"))
616 tmpRef = (SV*)GvAV(gv);
619 if (strEQ(elem, "CODE"))
620 tmpRef = (SV*)GvCVu(gv);
623 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
624 tmpRef = (SV*)GvIOp(gv);
627 if (strEQ(elem, "GLOB"))
631 if (strEQ(elem, "HASH"))
632 tmpRef = (SV*)GvHV(gv);
635 if (strEQ(elem, "IO"))
636 tmpRef = (SV*)GvIOp(gv);
639 if (strEQ(elem, "NAME"))
640 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
643 if (strEQ(elem, "PACKAGE"))
644 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
647 if (strEQ(elem, "SCALAR"))
661 /* Pattern matching */
666 register unsigned char *s;
669 register I32 *sfirst;
673 if (sv == PL_lastscream) {
679 SvSCREAM_off(PL_lastscream);
680 SvREFCNT_dec(PL_lastscream);
682 PL_lastscream = SvREFCNT_inc(sv);
685 s = (unsigned char*)(SvPV(sv, len));
689 if (pos > PL_maxscream) {
690 if (PL_maxscream < 0) {
691 PL_maxscream = pos + 80;
692 New(301, PL_screamfirst, 256, I32);
693 New(302, PL_screamnext, PL_maxscream, I32);
696 PL_maxscream = pos + pos / 4;
697 Renew(PL_screamnext, PL_maxscream, I32);
701 sfirst = PL_screamfirst;
702 snext = PL_screamnext;
704 if (!sfirst || !snext)
705 DIE(aTHX_ "do_study: out of memory");
707 for (ch = 256; ch; --ch)
714 snext[pos] = sfirst[ch] - pos;
721 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
730 if (PL_op->op_flags & OPf_STACKED)
736 TARG = sv_newmortal();
741 /* Lvalue operators. */
753 djSP; dMARK; dTARGET;
763 SETi(do_chomp(TOPs));
769 djSP; dMARK; dTARGET;
770 register I32 count = 0;
773 count += do_chomp(POPs);
784 if (!sv || !SvANY(sv))
786 switch (SvTYPE(sv)) {
788 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
792 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
796 if (CvROOT(sv) || CvXSUB(sv))
813 if (!PL_op->op_private) {
822 if (SvTHINKFIRST(sv))
825 switch (SvTYPE(sv)) {
835 if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv))
836 Perl_warner(aTHX_ WARN_MISC, "Constant subroutine %s undefined",
837 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
841 /* let user-undef'd sub keep its identity */
842 GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
849 SvSetMagicSV(sv, &PL_sv_undef);
853 Newz(602, gp, 1, GP);
854 GvGP(sv) = gp_ref(gp);
855 GvSV(sv) = NEWSV(72,0);
856 GvLINE(sv) = CopLINE(PL_curcop);
862 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
865 SvPV_set(sv, Nullch);
878 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
879 DIE(aTHX_ PL_no_modify);
880 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
881 SvIVX(TOPs) != IV_MIN)
884 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
895 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
896 DIE(aTHX_ PL_no_modify);
897 sv_setsv(TARG, TOPs);
898 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
899 SvIVX(TOPs) != IV_MAX)
902 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
916 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
917 DIE(aTHX_ PL_no_modify);
918 sv_setsv(TARG, TOPs);
919 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
920 SvIVX(TOPs) != IV_MIN)
923 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
932 /* Ordinary operators. */
936 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
939 SETn( pow( left, right) );
946 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
949 SETn( left * right );
956 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
961 DIE(aTHX_ "Illegal division by zero");
963 /* insure that 20./5. == 4. */
966 if ((NV)I_V(left) == left &&
967 (NV)I_V(right) == right &&
968 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
972 value = left / right;
976 value = left / right;
985 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
995 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
997 right = (right_neg = (i < 0)) ? -i : i;
1002 right_neg = dright < 0;
1007 if (!use_double && SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
1009 left = (left_neg = (i < 0)) ? -i : i;
1017 left_neg = dleft < 0;
1026 /* Somehow U_V is pessimized even if CASTFLAGS is 0 */
1028 # define CAST_D2UV(d) U_V(d)
1030 # define CAST_D2UV(d) ((UV)(d))
1032 /* Tried to do this only in the case DOUBLESIZE <= UV_SIZE,
1033 * or, in other words, precision of UV more than of NV.
1034 * But in fact the approach below turned out to be an
1035 * optimization - floor() may be slow */
1036 if (dright <= UV_MAX && dleft <= UV_MAX) {
1037 right = CAST_D2UV(dright);
1038 left = CAST_D2UV(dleft);
1043 /* Backward-compatibility clause: */
1044 dright = floor(dright + 0.5);
1045 dleft = floor(dleft + 0.5);
1048 DIE(aTHX_ "Illegal modulus zero");
1050 dans = Perl_fmod(dleft, dright);
1051 if ((left_neg != right_neg) && dans)
1052 dans = dright - dans;
1055 sv_setnv(TARG, dans);
1062 DIE(aTHX_ "Illegal modulus zero");
1065 if ((left_neg != right_neg) && ans)
1068 /* XXX may warn: unary minus operator applied to unsigned type */
1069 /* could change -foo to be (~foo)+1 instead */
1070 if (ans <= ~((UV)IV_MAX)+1)
1071 sv_setiv(TARG, ~ans+1);
1073 sv_setnv(TARG, -(NV)ans);
1076 sv_setuv(TARG, ans);
1085 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1087 register I32 count = POPi;
1088 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1090 I32 items = SP - MARK;
1093 max = items * count;
1102 repeatcpy((char*)(MARK + items), (char*)MARK,
1103 items * sizeof(SV*), count - 1);
1106 else if (count <= 0)
1109 else { /* Note: mark already snarfed by pp_list */
1114 SvSetSV(TARG, tmpstr);
1115 SvPV_force(TARG, len);
1120 SvGROW(TARG, (count * len) + 1);
1121 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1122 SvCUR(TARG) *= count;
1124 *SvEND(TARG) = '\0';
1126 (void)SvPOK_only(TARG);
1135 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1138 SETn( left - right );
1145 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1148 if (PL_op->op_private & HINT_INTEGER) {
1150 i = BWi(i) << shift;
1164 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1167 if (PL_op->op_private & HINT_INTEGER) {
1169 i = BWi(i) >> shift;
1183 djSP; tryAMAGICbinSET(lt,0);
1186 SETs(boolSV(TOPn < value));
1193 djSP; tryAMAGICbinSET(gt,0);
1196 SETs(boolSV(TOPn > value));
1203 djSP; tryAMAGICbinSET(le,0);
1206 SETs(boolSV(TOPn <= value));
1213 djSP; tryAMAGICbinSET(ge,0);
1216 SETs(boolSV(TOPn >= value));
1223 djSP; tryAMAGICbinSET(ne,0);
1226 SETs(boolSV(TOPn != value));
1233 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1240 else if (left < right)
1242 else if (left > right)
1255 djSP; tryAMAGICbinSET(slt,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(sgt,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(sle,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(sge,0);
1297 int cmp = ((PL_op->op_private & OPpLOCALE)
1298 ? sv_cmp_locale(left, right)
1299 : sv_cmp(left, right));
1300 SETs(boolSV(cmp >= 0));
1307 djSP; tryAMAGICbinSET(seq,0);
1310 SETs(boolSV(sv_eq(left, right)));
1317 djSP; tryAMAGICbinSET(sne,0);
1320 SETs(boolSV(!sv_eq(left, right)));
1327 djSP; dTARGET; tryAMAGICbin(scmp,0);
1330 int cmp = ((PL_op->op_private & OPpLOCALE)
1331 ? sv_cmp_locale(left, right)
1332 : sv_cmp(left, right));
1340 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1343 if (SvNIOKp(left) || SvNIOKp(right)) {
1344 if (PL_op->op_private & HINT_INTEGER) {
1345 IBW value = SvIV(left) & SvIV(right);
1349 UBW value = SvUV(left) & SvUV(right);
1354 do_vop(PL_op->op_type, TARG, left, right);
1363 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1366 if (SvNIOKp(left) || SvNIOKp(right)) {
1367 if (PL_op->op_private & HINT_INTEGER) {
1368 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1372 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1377 do_vop(PL_op->op_type, TARG, left, right);
1386 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1389 if (SvNIOKp(left) || SvNIOKp(right)) {
1390 if (PL_op->op_private & HINT_INTEGER) {
1391 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1395 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1400 do_vop(PL_op->op_type, TARG, left, right);
1409 djSP; dTARGET; tryAMAGICun(neg);
1414 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1416 else if (SvNIOKp(sv))
1418 else if (SvPOKp(sv)) {
1420 char *s = SvPV(sv, len);
1421 if (isIDFIRST(*s)) {
1422 sv_setpvn(TARG, "-", 1);
1425 else if (*s == '+' || *s == '-') {
1427 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1429 else if (DO_UTF8(sv) && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1430 sv_setpvn(TARG, "-", 1);
1434 sv_setnv(TARG, -SvNV(sv));
1445 djSP; tryAMAGICunSET(not);
1446 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1452 djSP; dTARGET; tryAMAGICun(compl);
1456 if (PL_op->op_private & HINT_INTEGER) {
1457 IBW value = ~SvIV(sv);
1461 UBW value = ~SvUV(sv);
1466 register char *tmps;
1467 register long *tmpl;
1472 tmps = SvPV_force(TARG, len);
1475 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1478 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1482 for ( ; anum > 0; anum--, tmps++)
1491 /* integer versions of some of the above */
1495 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1498 SETi( left * right );
1505 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1509 DIE(aTHX_ "Illegal division by zero");
1510 value = POPi / value;
1518 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1522 DIE(aTHX_ "Illegal modulus zero");
1523 SETi( left % right );
1530 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1533 SETi( left + right );
1540 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1543 SETi( left - right );
1550 djSP; tryAMAGICbinSET(lt,0);
1553 SETs(boolSV(left < right));
1560 djSP; tryAMAGICbinSET(gt,0);
1563 SETs(boolSV(left > right));
1570 djSP; tryAMAGICbinSET(le,0);
1573 SETs(boolSV(left <= right));
1580 djSP; tryAMAGICbinSET(ge,0);
1583 SETs(boolSV(left >= right));
1590 djSP; tryAMAGICbinSET(eq,0);
1593 SETs(boolSV(left == right));
1600 djSP; tryAMAGICbinSET(ne,0);
1603 SETs(boolSV(left != right));
1610 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1617 else if (left < right)
1628 djSP; dTARGET; tryAMAGICun(neg);
1633 /* High falutin' math. */
1637 djSP; dTARGET; tryAMAGICbin(atan2,0);
1640 SETn(Perl_atan2(left, right));
1647 djSP; dTARGET; tryAMAGICun(sin);
1651 value = Perl_sin(value);
1659 djSP; dTARGET; tryAMAGICun(cos);
1663 value = Perl_cos(value);
1669 /* Support Configure command-line overrides for rand() functions.
1670 After 5.005, perhaps we should replace this by Configure support
1671 for drand48(), random(), or rand(). For 5.005, though, maintain
1672 compatibility by calling rand() but allow the user to override it.
1673 See INSTALL for details. --Andy Dougherty 15 July 1998
1675 /* Now it's after 5.005, and Configure supports drand48() and random(),
1676 in addition to rand(). So the overrides should not be needed any more.
1677 --Jarkko Hietaniemi 27 September 1998
1680 #ifndef HAS_DRAND48_PROTO
1681 extern double drand48 (void);
1694 if (!PL_srand_called) {
1695 (void)seedDrand01((Rand_seed_t)seed());
1696 PL_srand_called = TRUE;
1711 (void)seedDrand01((Rand_seed_t)anum);
1712 PL_srand_called = TRUE;
1721 * This is really just a quick hack which grabs various garbage
1722 * values. It really should be a real hash algorithm which
1723 * spreads the effect of every input bit onto every output bit,
1724 * if someone who knows about such things would bother to write it.
1725 * Might be a good idea to add that function to CORE as well.
1726 * No numbers below come from careful analysis or anything here,
1727 * except they are primes and SEED_C1 > 1E6 to get a full-width
1728 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1729 * probably be bigger too.
1732 # define SEED_C1 1000003
1733 #define SEED_C4 73819
1735 # define SEED_C1 25747
1736 #define SEED_C4 20639
1740 #define SEED_C5 26107
1743 #ifndef PERL_NO_DEV_RANDOM
1748 # include <starlet.h>
1749 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1750 * in 100-ns units, typically incremented ever 10 ms. */
1751 unsigned int when[2];
1753 # ifdef HAS_GETTIMEOFDAY
1754 struct timeval when;
1760 /* This test is an escape hatch, this symbol isn't set by Configure. */
1761 #ifndef PERL_NO_DEV_RANDOM
1762 #ifndef PERL_RANDOM_DEVICE
1763 /* /dev/random isn't used by default because reads from it will block
1764 * if there isn't enough entropy available. You can compile with
1765 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1766 * is enough real entropy to fill the seed. */
1767 # define PERL_RANDOM_DEVICE "/dev/urandom"
1769 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1771 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1780 _ckvmssts(sys$gettim(when));
1781 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1783 # ifdef HAS_GETTIMEOFDAY
1784 gettimeofday(&when,(struct timezone *) 0);
1785 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1788 u = (U32)SEED_C1 * when;
1791 u += SEED_C3 * (U32)PerlProc_getpid();
1792 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
1793 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1794 u += SEED_C5 * (U32)PTR2UV(&when);
1801 djSP; dTARGET; tryAMAGICun(exp);
1805 value = Perl_exp(value);
1813 djSP; dTARGET; tryAMAGICun(log);
1818 RESTORE_NUMERIC_STANDARD();
1819 DIE(aTHX_ "Can't take log of %g", value);
1821 value = Perl_log(value);
1829 djSP; dTARGET; tryAMAGICun(sqrt);
1834 RESTORE_NUMERIC_STANDARD();
1835 DIE(aTHX_ "Can't take sqrt of %g", value);
1837 value = Perl_sqrt(value);
1850 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1856 (void)Perl_modf(value, &value);
1858 (void)Perl_modf(-value, &value);
1873 djSP; dTARGET; tryAMAGICun(abs);
1878 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1879 (iv = SvIVX(TOPs)) != IV_MIN) {
1901 XPUSHn(scan_hex(tmps, 99, &argtype));
1914 while (*tmps && isSPACE(*tmps))
1919 value = scan_hex(++tmps, 99, &argtype);
1920 else if (*tmps == 'b')
1921 value = scan_bin(++tmps, 99, &argtype);
1923 value = scan_oct(tmps, 99, &argtype);
1936 SETi(sv_len_utf8(sv));
1952 I32 lvalue = PL_op->op_flags & OPf_MOD;
1954 I32 arybase = PL_curcop->cop_arybase;
1958 SvTAINTED_off(TARG); /* decontaminate */
1959 SvUTF8_off(TARG); /* decontaminate */
1963 repl = SvPV(sv, repl_len);
1970 tmps = SvPV(sv, curlen);
1972 utfcurlen = sv_len_utf8(sv);
1973 if (utfcurlen == curlen)
1981 if (pos >= arybase) {
1999 else if (len >= 0) {
2001 if (rem > (I32)curlen)
2016 Perl_croak(aTHX_ "substr outside of string");
2017 if (ckWARN(WARN_SUBSTR))
2018 Perl_warner(aTHX_ WARN_SUBSTR, "substr outside of string");
2023 sv_pos_u2b(sv, &pos, &rem);
2027 sv_setpvn(TARG, tmps, rem);
2029 sv_insert(sv, pos, rem, repl, repl_len);
2030 else if (lvalue) { /* it's an lvalue! */
2031 if (!SvGMAGICAL(sv)) {
2035 if (ckWARN(WARN_SUBSTR))
2036 Perl_warner(aTHX_ WARN_SUBSTR,
2037 "Attempt to use reference as lvalue in substr");
2039 if (SvOK(sv)) /* is it defined ? */
2040 (void)SvPOK_only(sv);
2042 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2045 if (SvTYPE(TARG) < SVt_PVLV) {
2046 sv_upgrade(TARG, SVt_PVLV);
2047 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
2051 if (LvTARG(TARG) != sv) {
2053 SvREFCNT_dec(LvTARG(TARG));
2054 LvTARG(TARG) = SvREFCNT_inc(sv);
2056 LvTARGOFF(TARG) = pos;
2057 LvTARGLEN(TARG) = rem;
2061 PUSHs(TARG); /* avoid SvSETMAGIC here */
2068 register I32 size = POPi;
2069 register I32 offset = POPi;
2070 register SV *src = POPs;
2071 I32 lvalue = PL_op->op_flags & OPf_MOD;
2073 SvTAINTED_off(TARG); /* decontaminate */
2074 if (lvalue) { /* it's an lvalue! */
2075 if (SvTYPE(TARG) < SVt_PVLV) {
2076 sv_upgrade(TARG, SVt_PVLV);
2077 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2080 if (LvTARG(TARG) != src) {
2082 SvREFCNT_dec(LvTARG(TARG));
2083 LvTARG(TARG) = SvREFCNT_inc(src);
2085 LvTARGOFF(TARG) = offset;
2086 LvTARGLEN(TARG) = size;
2089 sv_setuv(TARG, do_vecget(src, offset, size));
2104 I32 arybase = PL_curcop->cop_arybase;
2109 offset = POPi - arybase;
2112 tmps = SvPV(big, biglen);
2113 if (offset > 0 && DO_UTF8(big))
2114 sv_pos_u2b(big, &offset, 0);
2117 else if (offset > biglen)
2119 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2120 (unsigned char*)tmps + biglen, little, 0)))
2123 retval = tmps2 - tmps;
2124 if (retval > 0 && DO_UTF8(big))
2125 sv_pos_b2u(big, &retval);
2126 PUSHi(retval + arybase);
2141 I32 arybase = PL_curcop->cop_arybase;
2147 tmps2 = SvPV(little, llen);
2148 tmps = SvPV(big, blen);
2152 if (offset > 0 && DO_UTF8(big))
2153 sv_pos_u2b(big, &offset, 0);
2154 offset = offset - arybase + llen;
2158 else if (offset > blen)
2160 if (!(tmps2 = rninstr(tmps, tmps + offset,
2161 tmps2, tmps2 + llen)))
2164 retval = tmps2 - tmps;
2165 if (retval > 0 && DO_UTF8(big))
2166 sv_pos_b2u(big, &retval);
2167 PUSHi(retval + arybase);
2173 djSP; dMARK; dORIGMARK; dTARGET;
2174 do_sprintf(TARG, SP-MARK, MARK+1);
2175 TAINT_IF(SvTAINTED(TARG));
2187 U8 *tmps = (U8*)SvPVx(tmpsv,n_a);
2190 if ((*tmps & 0x80) && DO_UTF8(tmpsv))
2191 value = utf8_to_uv(tmps, &retlen);
2193 value = (UV)(*tmps & 255);
2204 (void)SvUPGRADE(TARG,SVt_PV);
2206 if (value > 255 && !IN_BYTE) {
2207 SvGROW(TARG, UTF8_MAXLEN+1);
2209 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2210 SvCUR_set(TARG, tmps - SvPVX(TARG));
2212 (void)SvPOK_only(TARG);
2223 SvUTF8_off(TARG); /* decontaminate */
2224 (void)SvPOK_only(TARG);
2231 djSP; dTARGET; dPOPTOPssrl;
2234 char *tmps = SvPV(left, n_a);
2236 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2238 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2242 "The crypt() function is unimplemented due to excessive paranoia.");
2255 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2257 U8 tmpbuf[UTF8_MAXLEN];
2259 UV uv = utf8_to_uv(s, &ulen);
2261 if (PL_op->op_private & OPpLOCALE) {
2264 uv = toTITLE_LC_uni(uv);
2267 uv = toTITLE_utf8(s);
2269 tend = uv_to_utf8(tmpbuf, uv);
2271 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
2273 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2274 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2279 s = (U8*)SvPV_force(sv, slen);
2280 Copy(tmpbuf, s, ulen, U8);
2284 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2286 SvUTF8_off(TARG); /* decontaminate */
2291 s = (U8*)SvPV_force(sv, slen);
2293 if (PL_op->op_private & OPpLOCALE) {
2296 *s = toUPPER_LC(*s);
2314 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2316 U8 tmpbuf[UTF8_MAXLEN];
2318 UV uv = utf8_to_uv(s, &ulen);
2320 if (PL_op->op_private & OPpLOCALE) {
2323 uv = toLOWER_LC_uni(uv);
2326 uv = toLOWER_utf8(s);
2328 tend = uv_to_utf8(tmpbuf, uv);
2330 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
2332 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2333 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2338 s = (U8*)SvPV_force(sv, slen);
2339 Copy(tmpbuf, s, ulen, U8);
2343 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2345 SvUTF8_off(TARG); /* decontaminate */
2350 s = (U8*)SvPV_force(sv, slen);
2352 if (PL_op->op_private & OPpLOCALE) {
2355 *s = toLOWER_LC(*s);
2379 s = (U8*)SvPV(sv,len);
2381 SvUTF8_off(TARG); /* decontaminate */
2382 sv_setpvn(TARG, "", 0);
2386 (void)SvUPGRADE(TARG, SVt_PV);
2387 SvGROW(TARG, (len * 2) + 1);
2388 (void)SvPOK_only(TARG);
2389 d = (U8*)SvPVX(TARG);
2391 if (PL_op->op_private & OPpLOCALE) {
2395 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2401 d = uv_to_utf8(d, toUPPER_utf8( s ));
2407 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2412 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2414 SvUTF8_off(TARG); /* decontaminate */
2419 s = (U8*)SvPV_force(sv, len);
2421 register U8 *send = s + len;
2423 if (PL_op->op_private & OPpLOCALE) {
2426 for (; s < send; s++)
2427 *s = toUPPER_LC(*s);
2430 for (; s < send; s++)
2453 s = (U8*)SvPV(sv,len);
2455 SvUTF8_off(TARG); /* decontaminate */
2456 sv_setpvn(TARG, "", 0);
2460 (void)SvUPGRADE(TARG, SVt_PV);
2461 SvGROW(TARG, (len * 2) + 1);
2462 (void)SvPOK_only(TARG);
2463 d = (U8*)SvPVX(TARG);
2465 if (PL_op->op_private & OPpLOCALE) {
2469 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2475 d = uv_to_utf8(d, toLOWER_utf8(s));
2481 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2486 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2488 SvUTF8_off(TARG); /* decontaminate */
2494 s = (U8*)SvPV_force(sv, len);
2496 register U8 *send = s + len;
2498 if (PL_op->op_private & OPpLOCALE) {
2501 for (; s < send; s++)
2502 *s = toLOWER_LC(*s);
2505 for (; s < send; s++)
2520 register char *s = SvPV(sv,len);
2523 SvUTF8_off(TARG); /* decontaminate */
2525 (void)SvUPGRADE(TARG, SVt_PV);
2526 SvGROW(TARG, (len * 2) + 1);
2531 STRLEN ulen = UTF8SKIP(s);
2555 SvCUR_set(TARG, d - SvPVX(TARG));
2556 (void)SvPOK_only(TARG);
2559 sv_setpvn(TARG, s, len);
2561 if (SvSMAGICAL(TARG))
2570 djSP; dMARK; dORIGMARK;
2572 register AV* av = (AV*)POPs;
2573 register I32 lval = PL_op->op_flags & OPf_MOD;
2574 I32 arybase = PL_curcop->cop_arybase;
2577 if (SvTYPE(av) == SVt_PVAV) {
2578 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2580 for (svp = MARK + 1; svp <= SP; svp++) {
2585 if (max > AvMAX(av))
2588 while (++MARK <= SP) {
2589 elem = SvIVx(*MARK);
2593 svp = av_fetch(av, elem, lval);
2595 if (!svp || *svp == &PL_sv_undef)
2596 DIE(aTHX_ PL_no_aelem, elem);
2597 if (PL_op->op_private & OPpLVAL_INTRO)
2598 save_aelem(av, elem, svp);
2600 *MARK = svp ? *svp : &PL_sv_undef;
2603 if (GIMME != G_ARRAY) {
2611 /* Associative arrays. */
2616 HV *hash = (HV*)POPs;
2618 I32 gimme = GIMME_V;
2619 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2622 /* might clobber stack_sp */
2623 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2628 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2629 if (gimme == G_ARRAY) {
2632 /* might clobber stack_sp */
2634 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
2639 else if (gimme == G_SCALAR)
2658 I32 gimme = GIMME_V;
2659 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2663 if (PL_op->op_private & OPpSLICE) {
2667 hvtype = SvTYPE(hv);
2668 if (hvtype == SVt_PVHV) { /* hash element */
2669 while (++MARK <= SP) {
2670 sv = hv_delete_ent(hv, *MARK, discard, 0);
2671 *MARK = sv ? sv : &PL_sv_undef;
2674 else if (hvtype == SVt_PVAV) {
2675 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
2676 while (++MARK <= SP) {
2677 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
2678 *MARK = sv ? sv : &PL_sv_undef;
2681 else { /* pseudo-hash element */
2682 while (++MARK <= SP) {
2683 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2684 *MARK = sv ? sv : &PL_sv_undef;
2689 DIE(aTHX_ "Not a HASH reference");
2692 else if (gimme == G_SCALAR) {
2701 if (SvTYPE(hv) == SVt_PVHV)
2702 sv = hv_delete_ent(hv, keysv, discard, 0);
2703 else if (SvTYPE(hv) == SVt_PVAV) {
2704 if (PL_op->op_flags & OPf_SPECIAL)
2705 sv = av_delete((AV*)hv, SvIV(keysv), discard);
2707 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2710 DIE(aTHX_ "Not a HASH reference");
2725 if (PL_op->op_private & OPpEXISTS_SUB) {
2729 cv = sv_2cv(sv, &hv, &gv, FALSE);
2732 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
2738 if (SvTYPE(hv) == SVt_PVHV) {
2739 if (hv_exists_ent(hv, tmpsv, 0))
2742 else if (SvTYPE(hv) == SVt_PVAV) {
2743 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
2744 if (av_exists((AV*)hv, SvIV(tmpsv)))
2747 else if (avhv_exists_ent((AV*)hv, tmpsv, 0)) /* pseudo-hash element */
2751 DIE(aTHX_ "Not a HASH reference");
2758 djSP; dMARK; dORIGMARK;
2759 register HV *hv = (HV*)POPs;
2760 register I32 lval = PL_op->op_flags & OPf_MOD;
2761 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2763 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2764 DIE(aTHX_ "Can't localize pseudo-hash element");
2766 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2767 while (++MARK <= SP) {
2771 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2772 svp = he ? &HeVAL(he) : 0;
2775 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2778 if (!svp || *svp == &PL_sv_undef) {
2780 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2782 if (PL_op->op_private & OPpLVAL_INTRO)
2783 save_helem(hv, keysv, svp);
2785 *MARK = svp ? *svp : &PL_sv_undef;
2788 if (GIMME != G_ARRAY) {
2796 /* List operators. */
2801 if (GIMME != G_ARRAY) {
2803 *MARK = *SP; /* unwanted list, return last item */
2805 *MARK = &PL_sv_undef;
2814 SV **lastrelem = PL_stack_sp;
2815 SV **lastlelem = PL_stack_base + POPMARK;
2816 SV **firstlelem = PL_stack_base + POPMARK + 1;
2817 register SV **firstrelem = lastlelem + 1;
2818 I32 arybase = PL_curcop->cop_arybase;
2819 I32 lval = PL_op->op_flags & OPf_MOD;
2820 I32 is_something_there = lval;
2822 register I32 max = lastrelem - lastlelem;
2823 register SV **lelem;
2826 if (GIMME != G_ARRAY) {
2827 ix = SvIVx(*lastlelem);
2832 if (ix < 0 || ix >= max)
2833 *firstlelem = &PL_sv_undef;
2835 *firstlelem = firstrelem[ix];
2841 SP = firstlelem - 1;
2845 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2851 if (ix < 0 || ix >= max)
2852 *lelem = &PL_sv_undef;
2854 is_something_there = TRUE;
2855 if (!(*lelem = firstrelem[ix]))
2856 *lelem = &PL_sv_undef;
2859 if (is_something_there)
2862 SP = firstlelem - 1;
2868 djSP; dMARK; dORIGMARK;
2869 I32 items = SP - MARK;
2870 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2871 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2878 djSP; dMARK; dORIGMARK;
2879 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2883 SV *val = NEWSV(46, 0);
2885 sv_setsv(val, *++MARK);
2886 else if (ckWARN(WARN_MISC))
2887 Perl_warner(aTHX_ WARN_MISC, "Odd number of elements in hash assignment");
2888 (void)hv_store_ent(hv,key,val,0);
2897 djSP; dMARK; dORIGMARK;
2898 register AV *ary = (AV*)*++MARK;
2902 register I32 offset;
2903 register I32 length;
2910 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2911 *MARK-- = SvTIED_obj((SV*)ary, mg);
2915 call_method("SPLICE",GIMME_V);
2924 offset = i = SvIVx(*MARK);
2926 offset += AvFILLp(ary) + 1;
2928 offset -= PL_curcop->cop_arybase;
2930 DIE(aTHX_ PL_no_aelem, i);
2932 length = SvIVx(*MARK++);
2934 length += AvFILLp(ary) - offset + 1;
2940 length = AvMAX(ary) + 1; /* close enough to infinity */
2944 length = AvMAX(ary) + 1;
2946 if (offset > AvFILLp(ary) + 1)
2947 offset = AvFILLp(ary) + 1;
2948 after = AvFILLp(ary) + 1 - (offset + length);
2949 if (after < 0) { /* not that much array */
2950 length += after; /* offset+length now in array */
2956 /* At this point, MARK .. SP-1 is our new LIST */
2959 diff = newlen - length;
2960 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2963 if (diff < 0) { /* shrinking the area */
2965 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2966 Copy(MARK, tmparyval, newlen, SV*);
2969 MARK = ORIGMARK + 1;
2970 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2971 MEXTEND(MARK, length);
2972 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2974 EXTEND_MORTAL(length);
2975 for (i = length, dst = MARK; i; i--) {
2976 sv_2mortal(*dst); /* free them eventualy */
2983 *MARK = AvARRAY(ary)[offset+length-1];
2986 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2987 SvREFCNT_dec(*dst++); /* free them now */
2990 AvFILLp(ary) += diff;
2992 /* pull up or down? */
2994 if (offset < after) { /* easier to pull up */
2995 if (offset) { /* esp. if nothing to pull */
2996 src = &AvARRAY(ary)[offset-1];
2997 dst = src - diff; /* diff is negative */
2998 for (i = offset; i > 0; i--) /* can't trust Copy */
3002 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
3006 if (after) { /* anything to pull down? */
3007 src = AvARRAY(ary) + offset + length;
3008 dst = src + diff; /* diff is negative */
3009 Move(src, dst, after, SV*);
3011 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
3012 /* avoid later double free */
3016 dst[--i] = &PL_sv_undef;
3019 for (src = tmparyval, dst = AvARRAY(ary) + offset;
3021 *dst = NEWSV(46, 0);
3022 sv_setsv(*dst++, *src++);
3024 Safefree(tmparyval);
3027 else { /* no, expanding (or same) */
3029 New(452, tmparyval, length, SV*); /* so remember deletion */
3030 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
3033 if (diff > 0) { /* expanding */
3035 /* push up or down? */
3037 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
3041 Move(src, dst, offset, SV*);
3043 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
3045 AvFILLp(ary) += diff;
3048 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
3049 av_extend(ary, AvFILLp(ary) + diff);
3050 AvFILLp(ary) += diff;
3053 dst = AvARRAY(ary) + AvFILLp(ary);
3055 for (i = after; i; i--) {
3062 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
3063 *dst = NEWSV(46, 0);
3064 sv_setsv(*dst++, *src++);
3066 MARK = ORIGMARK + 1;
3067 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3069 Copy(tmparyval, MARK, length, SV*);
3071 EXTEND_MORTAL(length);
3072 for (i = length, dst = MARK; i; i--) {
3073 sv_2mortal(*dst); /* free them eventualy */
3077 Safefree(tmparyval);
3081 else if (length--) {
3082 *MARK = tmparyval[length];
3085 while (length-- > 0)
3086 SvREFCNT_dec(tmparyval[length]);
3088 Safefree(tmparyval);
3091 *MARK = &PL_sv_undef;
3099 djSP; dMARK; dORIGMARK; dTARGET;
3100 register AV *ary = (AV*)*++MARK;
3101 register SV *sv = &PL_sv_undef;
3104 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3105 *MARK-- = SvTIED_obj((SV*)ary, mg);
3109 call_method("PUSH",G_SCALAR|G_DISCARD);
3114 /* Why no pre-extend of ary here ? */
3115 for (++MARK; MARK <= SP; MARK++) {
3118 sv_setsv(sv, *MARK);
3123 PUSHi( AvFILL(ary) + 1 );
3131 SV *sv = av_pop(av);
3133 (void)sv_2mortal(sv);
3142 SV *sv = av_shift(av);
3147 (void)sv_2mortal(sv);
3154 djSP; dMARK; dORIGMARK; dTARGET;
3155 register AV *ary = (AV*)*++MARK;
3160 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3161 *MARK-- = SvTIED_obj((SV*)ary, mg);
3165 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3170 av_unshift(ary, SP - MARK);
3173 sv_setsv(sv, *++MARK);
3174 (void)av_store(ary, i++, sv);
3178 PUSHi( AvFILL(ary) + 1 );
3188 if (GIMME == G_ARRAY) {
3195 /* safe as long as stack cannot get extended in the above */
3200 register char *down;
3205 SvUTF8_off(TARG); /* decontaminate */
3207 do_join(TARG, &PL_sv_no, MARK, SP);
3209 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3210 up = SvPV_force(TARG, len);
3212 if (DO_UTF8(TARG)) { /* first reverse each character */
3213 U8* s = (U8*)SvPVX(TARG);
3214 U8* send = (U8*)(s + len);
3223 down = (char*)(s - 1);
3224 if (s > send || !((*down & 0xc0) == 0x80)) {
3225 if (ckWARN_d(WARN_UTF8))
3226 Perl_warner(aTHX_ WARN_UTF8,
3227 "Malformed UTF-8 character");
3239 down = SvPVX(TARG) + len - 1;
3245 (void)SvPOK_only(TARG);
3254 S_mul128(pTHX_ SV *sv, U8 m)
3257 char *s = SvPV(sv, len);
3261 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3262 SV *tmpNew = newSVpvn("0000000000", 10);
3264 sv_catsv(tmpNew, sv);
3265 SvREFCNT_dec(sv); /* free old sv */
3270 while (!*t) /* trailing '\0'? */
3273 i = ((*t - '0') << 7) + m;
3274 *(t--) = '0' + (i % 10);
3280 /* Explosives and implosives. */
3282 #if 'I' == 73 && 'J' == 74
3283 /* On an ASCII/ISO kind of system */
3284 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3287 Some other sort of character set - use memchr() so we don't match
3290 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3297 I32 start_sp_offset = SP - PL_stack_base;
3298 I32 gimme = GIMME_V;
3302 register char *pat = SvPV(left, llen);
3303 register char *s = SvPV(right, rlen);
3304 char *strend = s + rlen;
3306 register char *patend = pat + llen;
3312 /* These must not be in registers: */
3329 register U32 culong;
3333 #ifdef PERL_NATINT_PACK
3334 int natint; /* native integer */
3335 int unatint; /* unsigned native integer */
3338 if (gimme != G_ARRAY) { /* arrange to do first one only */
3340 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3341 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3343 while (isDIGIT(*patend) || *patend == '*')
3349 while (pat < patend) {
3351 datumtype = *pat++ & 0xFF;
3352 #ifdef PERL_NATINT_PACK
3355 if (isSPACE(datumtype))
3357 if (datumtype == '#') {
3358 while (pat < patend && *pat != '\n')
3363 char *natstr = "sSiIlL";
3365 if (strchr(natstr, datumtype)) {
3366 #ifdef PERL_NATINT_PACK
3372 DIE(aTHX_ "'!' allowed only after types %s", natstr);
3377 else if (*pat == '*') {
3378 len = strend - strbeg; /* long enough */
3382 else if (isDIGIT(*pat)) {
3384 while (isDIGIT(*pat)) {
3385 len = (len * 10) + (*pat++ - '0');
3387 DIE(aTHX_ "Repeat count in unpack overflows");
3391 len = (datumtype != '@');
3395 DIE(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
3396 case ',': /* grandfather in commas but with a warning */
3397 if (commas++ == 0 && ckWARN(WARN_UNPACK))
3398 Perl_warner(aTHX_ WARN_UNPACK,
3399 "Invalid type in unpack: '%c'", (int)datumtype);
3402 if (len == 1 && pat[-1] != '1')
3411 if (len > strend - strbeg)
3412 DIE(aTHX_ "@ outside of string");
3416 if (len > s - strbeg)
3417 DIE(aTHX_ "X outside of string");
3421 if (len > strend - s)
3422 DIE(aTHX_ "x outside of string");
3426 if (start_sp_offset >= SP - PL_stack_base)
3427 DIE(aTHX_ "/ must follow a numeric type");
3430 pat++; /* ignore '*' for compatibility with pack */
3432 DIE(aTHX_ "/ cannot take a count" );
3439 if (len > strend - s)
3442 goto uchar_checksum;
3443 sv = NEWSV(35, len);
3444 sv_setpvn(sv, s, len);
3446 if (datumtype == 'A' || datumtype == 'Z') {
3447 aptr = s; /* borrow register */
3448 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3453 else { /* 'A' strips both nulls and spaces */
3454 s = SvPVX(sv) + len - 1;
3455 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3459 SvCUR_set(sv, s - SvPVX(sv));
3460 s = aptr; /* unborrow register */
3462 XPUSHs(sv_2mortal(sv));
3466 if (star || len > (strend - s) * 8)
3467 len = (strend - s) * 8;
3470 Newz(601, PL_bitcount, 256, char);
3471 for (bits = 1; bits < 256; bits++) {
3472 if (bits & 1) PL_bitcount[bits]++;
3473 if (bits & 2) PL_bitcount[bits]++;
3474 if (bits & 4) PL_bitcount[bits]++;
3475 if (bits & 8) PL_bitcount[bits]++;
3476 if (bits & 16) PL_bitcount[bits]++;
3477 if (bits & 32) PL_bitcount[bits]++;
3478 if (bits & 64) PL_bitcount[bits]++;
3479 if (bits & 128) PL_bitcount[bits]++;
3483 culong += PL_bitcount[*(unsigned char*)s++];
3488 if (datumtype == 'b') {
3490 if (bits & 1) culong++;
3496 if (bits & 128) culong++;
3503 sv = NEWSV(35, len + 1);
3507 if (datumtype == 'b') {
3509 for (len = 0; len < aint; len++) {
3510 if (len & 7) /*SUPPRESS 595*/
3514 *str++ = '0' + (bits & 1);
3519 for (len = 0; len < aint; len++) {
3524 *str++ = '0' + ((bits & 128) != 0);
3528 XPUSHs(sv_2mortal(sv));
3532 if (star || len > (strend - s) * 2)
3533 len = (strend - s) * 2;
3534 sv = NEWSV(35, len + 1);
3538 if (datumtype == 'h') {
3540 for (len = 0; len < aint; len++) {
3545 *str++ = PL_hexdigit[bits & 15];
3550 for (len = 0; len < aint; len++) {
3555 *str++ = PL_hexdigit[(bits >> 4) & 15];
3559 XPUSHs(sv_2mortal(sv));
3562 if (len > strend - s)
3567 if (aint >= 128) /* fake up signed chars */
3577 if (aint >= 128) /* fake up signed chars */
3580 sv_setiv(sv, (IV)aint);
3581 PUSHs(sv_2mortal(sv));
3586 if (len > strend - s)
3601 sv_setiv(sv, (IV)auint);
3602 PUSHs(sv_2mortal(sv));
3607 if (len > strend - s)
3610 while (len-- > 0 && s < strend) {
3611 auint = utf8_to_uv((U8*)s, &along);
3614 cdouble += (NV)auint;
3622 while (len-- > 0 && s < strend) {
3623 auint = utf8_to_uv((U8*)s, &along);
3626 sv_setuv(sv, (UV)auint);
3627 PUSHs(sv_2mortal(sv));
3632 #if SHORTSIZE == SIZE16
3633 along = (strend - s) / SIZE16;
3635 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3640 #if SHORTSIZE != SIZE16
3644 COPYNN(s, &ashort, sizeof(short));
3655 #if SHORTSIZE > SIZE16
3667 #if SHORTSIZE != SIZE16
3671 COPYNN(s, &ashort, sizeof(short));
3674 sv_setiv(sv, (IV)ashort);
3675 PUSHs(sv_2mortal(sv));
3683 #if SHORTSIZE > SIZE16
3689 sv_setiv(sv, (IV)ashort);
3690 PUSHs(sv_2mortal(sv));
3698 #if SHORTSIZE == SIZE16
3699 along = (strend - s) / SIZE16;
3701 unatint = natint && datumtype == 'S';
3702 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3707 #if SHORTSIZE != SIZE16
3709 unsigned short aushort;
3711 COPYNN(s, &aushort, sizeof(unsigned short));
3712 s += sizeof(unsigned short);
3720 COPY16(s, &aushort);
3723 if (datumtype == 'n')
3724 aushort = PerlSock_ntohs(aushort);
3727 if (datumtype == 'v')
3728 aushort = vtohs(aushort);
3737 #if SHORTSIZE != SIZE16
3739 unsigned short aushort;
3741 COPYNN(s, &aushort, sizeof(unsigned short));
3742 s += sizeof(unsigned short);
3744 sv_setiv(sv, (UV)aushort);
3745 PUSHs(sv_2mortal(sv));
3752 COPY16(s, &aushort);
3756 if (datumtype == 'n')
3757 aushort = PerlSock_ntohs(aushort);
3760 if (datumtype == 'v')
3761 aushort = vtohs(aushort);
3763 sv_setiv(sv, (UV)aushort);
3764 PUSHs(sv_2mortal(sv));
3770 along = (strend - s) / sizeof(int);
3775 Copy(s, &aint, 1, int);
3778 cdouble += (NV)aint;
3787 Copy(s, &aint, 1, int);
3791 /* Without the dummy below unpack("i", pack("i",-1))
3792 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3793 * cc with optimization turned on.
3795 * The bug was detected in
3796 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3797 * with optimization (-O4) turned on.
3798 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3799 * does not have this problem even with -O4.
3801 * This bug was reported as DECC_BUGS 1431
3802 * and tracked internally as GEM_BUGS 7775.
3804 * The bug is fixed in
3805 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3806 * UNIX V4.0F support: DEC C V5.9-006 or later
3807 * UNIX V4.0E support: DEC C V5.8-011 or later
3810 * See also few lines later for the same bug.
3813 sv_setiv(sv, (IV)aint) :
3815 sv_setiv(sv, (IV)aint);
3816 PUSHs(sv_2mortal(sv));
3821 along = (strend - s) / sizeof(unsigned int);
3826 Copy(s, &auint, 1, unsigned int);
3827 s += sizeof(unsigned int);
3829 cdouble += (NV)auint;
3838 Copy(s, &auint, 1, unsigned int);
3839 s += sizeof(unsigned int);
3842 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3843 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3844 * See details few lines earlier. */
3846 sv_setuv(sv, (UV)auint) :
3848 sv_setuv(sv, (UV)auint);
3849 PUSHs(sv_2mortal(sv));
3854 #if LONGSIZE == SIZE32
3855 along = (strend - s) / SIZE32;
3857 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3862 #if LONGSIZE != SIZE32
3866 COPYNN(s, &along, sizeof(long));
3869 cdouble += (NV)along;
3879 #if LONGSIZE > SIZE32
3880 if (along > 2147483647)
3881 along -= 4294967296;
3885 cdouble += (NV)along;
3894 #if LONGSIZE != SIZE32
3898 COPYNN(s, &along, sizeof(long));
3901 sv_setiv(sv, (IV)along);
3902 PUSHs(sv_2mortal(sv));
3910 #if LONGSIZE > SIZE32
3911 if (along > 2147483647)
3912 along -= 4294967296;
3916 sv_setiv(sv, (IV)along);
3917 PUSHs(sv_2mortal(sv));
3925 #if LONGSIZE == SIZE32
3926 along = (strend - s) / SIZE32;
3928 unatint = natint && datumtype == 'L';
3929 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3934 #if LONGSIZE != SIZE32
3936 unsigned long aulong;
3938 COPYNN(s, &aulong, sizeof(unsigned long));
3939 s += sizeof(unsigned long);
3941 cdouble += (NV)aulong;
3953 if (datumtype == 'N')
3954 aulong = PerlSock_ntohl(aulong);
3957 if (datumtype == 'V')
3958 aulong = vtohl(aulong);
3961 cdouble += (NV)aulong;
3970 #if LONGSIZE != SIZE32
3972 unsigned long aulong;
3974 COPYNN(s, &aulong, sizeof(unsigned long));
3975 s += sizeof(unsigned long);
3977 sv_setuv(sv, (UV)aulong);
3978 PUSHs(sv_2mortal(sv));
3988 if (datumtype == 'N')
3989 aulong = PerlSock_ntohl(aulong);
3992 if (datumtype == 'V')
3993 aulong = vtohl(aulong);
3996 sv_setuv(sv, (UV)aulong);
3997 PUSHs(sv_2mortal(sv));
4003 along = (strend - s) / sizeof(char*);
4009 if (sizeof(char*) > strend - s)
4012 Copy(s, &aptr, 1, char*);
4018 PUSHs(sv_2mortal(sv));
4028 while ((len > 0) && (s < strend)) {
4029 auv = (auv << 7) | (*s & 0x7f);
4030 if (!(*s++ & 0x80)) {
4034 PUSHs(sv_2mortal(sv));
4038 else if (++bytes >= sizeof(UV)) { /* promote to string */
4042 sv = Perl_newSVpvf(aTHX_ "%.*Vu", (int)TYPE_DIGITS(UV), auv);
4043 while (s < strend) {
4044 sv = mul128(sv, *s & 0x7f);
4045 if (!(*s++ & 0x80)) {
4054 PUSHs(sv_2mortal(sv));
4059 if ((s >= strend) && bytes)
4060 DIE(aTHX_ "Unterminated compressed integer");
4065 if (sizeof(char*) > strend - s)
4068 Copy(s, &aptr, 1, char*);
4073 sv_setpvn(sv, aptr, len);
4074 PUSHs(sv_2mortal(sv));
4078 along = (strend - s) / sizeof(Quad_t);
4084 if (s + sizeof(Quad_t) > strend)
4087 Copy(s, &aquad, 1, Quad_t);
4088 s += sizeof(Quad_t);
4091 if (aquad >= IV_MIN && aquad <= IV_MAX)
4092 sv_setiv(sv, (IV)aquad);
4094 sv_setnv(sv, (NV)aquad);
4095 PUSHs(sv_2mortal(sv));
4099 along = (strend - s) / sizeof(Quad_t);
4105 if (s + sizeof(Uquad_t) > strend)
4108 Copy(s, &auquad, 1, Uquad_t);
4109 s += sizeof(Uquad_t);
4112 if (auquad <= UV_MAX)
4113 sv_setuv(sv, (UV)auquad);
4115 sv_setnv(sv, (NV)auquad);
4116 PUSHs(sv_2mortal(sv));
4120 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4123 along = (strend - s) / sizeof(float);
4128 Copy(s, &afloat, 1, float);
4137 Copy(s, &afloat, 1, float);
4140 sv_setnv(sv, (NV)afloat);
4141 PUSHs(sv_2mortal(sv));
4147 along = (strend - s) / sizeof(double);
4152 Copy(s, &adouble, 1, double);
4153 s += sizeof(double);
4161 Copy(s, &adouble, 1, double);
4162 s += sizeof(double);
4164 sv_setnv(sv, (NV)adouble);
4165 PUSHs(sv_2mortal(sv));
4171 * Initialise the decode mapping. By using a table driven
4172 * algorithm, the code will be character-set independent
4173 * (and just as fast as doing character arithmetic)
4175 if (PL_uudmap['M'] == 0) {
4178 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4179 PL_uudmap[PL_uuemap[i]] = i;
4181 * Because ' ' and '`' map to the same value,
4182 * we need to decode them both the same.
4187 along = (strend - s) * 3 / 4;
4188 sv = NEWSV(42, along);
4191 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4196 len = PL_uudmap[*s++] & 077;
4198 if (s < strend && ISUUCHAR(*s))
4199 a = PL_uudmap[*s++] & 077;
4202 if (s < strend && ISUUCHAR(*s))
4203 b = PL_uudmap[*s++] & 077;
4206 if (s < strend && ISUUCHAR(*s))
4207 c = PL_uudmap[*s++] & 077;
4210 if (s < strend && ISUUCHAR(*s))
4211 d = PL_uudmap[*s++] & 077;
4214 hunk[0] = (a << 2) | (b >> 4);
4215 hunk[1] = (b << 4) | (c >> 2);
4216 hunk[2] = (c << 6) | d;
4217 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4222 else if (s[1] == '\n') /* possible checksum byte */
4225 XPUSHs(sv_2mortal(sv));
4230 if (strchr("fFdD", datumtype) ||
4231 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4235 while (checksum >= 16) {
4239 while (checksum >= 4) {
4245 along = (1 << checksum) - 1;
4246 while (cdouble < 0.0)
4248 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
4249 sv_setnv(sv, cdouble);
4252 if (checksum < 32) {
4253 aulong = (1 << checksum) - 1;
4256 sv_setuv(sv, (UV)culong);
4258 XPUSHs(sv_2mortal(sv));
4262 if (SP - PL_stack_base == start_sp_offset && gimme == G_SCALAR)
4263 PUSHs(&PL_sv_undef);
4268 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
4272 *hunk = PL_uuemap[len];
4273 sv_catpvn(sv, hunk, 1);
4276 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4277 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4278 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4279 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4280 sv_catpvn(sv, hunk, 4);
4285 char r = (len > 1 ? s[1] : '\0');
4286 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4287 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4288 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4289 hunk[3] = PL_uuemap[0];
4290 sv_catpvn(sv, hunk, 4);
4292 sv_catpvn(sv, "\n", 1);
4296 S_is_an_int(pTHX_ char *s, STRLEN l)
4299 SV *result = newSVpvn(s, l);
4300 char *result_c = SvPV(result, n_a); /* convenience */
4301 char *out = result_c;
4311 SvREFCNT_dec(result);
4334 SvREFCNT_dec(result);
4340 SvCUR_set(result, out - result_c);
4344 /* pnum must be '\0' terminated */
4346 S_div128(pTHX_ SV *pnum, bool *done)
4349 char *s = SvPV(pnum, len);
4358 i = m * 10 + (*t - '0');
4360 r = (i >> 7); /* r < 10 */
4367 SvCUR_set(pnum, (STRLEN) (t - s));
4374 djSP; dMARK; dORIGMARK; dTARGET;
4375 register SV *cat = TARG;
4378 register char *pat = SvPVx(*++MARK, fromlen);
4379 register char *patend = pat + fromlen;
4384 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4385 static char *space10 = " ";
4387 /* These must not be in registers: */
4402 #ifdef PERL_NATINT_PACK
4403 int natint; /* native integer */
4408 sv_setpvn(cat, "", 0);
4409 while (pat < patend) {
4410 SV *lengthcode = Nullsv;
4411 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *MARK++ : &PL_sv_no)
4412 datumtype = *pat++ & 0xFF;
4413 #ifdef PERL_NATINT_PACK
4416 if (isSPACE(datumtype))
4418 if (datumtype == '#') {
4419 while (pat < patend && *pat != '\n')
4424 char *natstr = "sSiIlL";
4426 if (strchr(natstr, datumtype)) {
4427 #ifdef PERL_NATINT_PACK
4433 DIE(aTHX_ "'!' allowed only after types %s", natstr);
4436 len = strchr("@Xxu", datumtype) ? 0 : items;
4439 else if (isDIGIT(*pat)) {
4441 while (isDIGIT(*pat)) {
4442 len = (len * 10) + (*pat++ - '0');
4444 DIE(aTHX_ "Repeat count in pack overflows");
4451 if (*pat != 'a' && *pat != 'A' && *pat != 'Z' || pat[1] != '*')
4452 DIE(aTHX_ "/ must be followed by a*, A* or Z*");
4453 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
4454 ? *MARK : &PL_sv_no)));
4458 DIE(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
4459 case ',': /* grandfather in commas but with a warning */
4460 if (commas++ == 0 && ckWARN(WARN_PACK))
4461 Perl_warner(aTHX_ WARN_PACK,
4462 "Invalid type in pack: '%c'", (int)datumtype);
4465 DIE(aTHX_ "%% may only be used in unpack");
4476 if (SvCUR(cat) < len)
4477 DIE(aTHX_ "X outside of string");
4484 sv_catpvn(cat, null10, 10);
4487 sv_catpvn(cat, null10, len);
4493 aptr = SvPV(fromstr, fromlen);
4494 if (pat[-1] == '*') {
4496 if (datumtype == 'Z')
4499 if (fromlen >= len) {
4500 sv_catpvn(cat, aptr, len);
4501 if (datumtype == 'Z')
4502 *(SvEND(cat)-1) = '\0';
4505 sv_catpvn(cat, aptr, fromlen);
4507 if (datumtype == 'A') {
4509 sv_catpvn(cat, space10, 10);
4512 sv_catpvn(cat, space10, len);
4516 sv_catpvn(cat, null10, 10);
4519 sv_catpvn(cat, null10, len);
4531 str = SvPV(fromstr, fromlen);
4535 SvCUR(cat) += (len+7)/8;
4536 SvGROW(cat, SvCUR(cat) + 1);
4537 aptr = SvPVX(cat) + aint;
4542 if (datumtype == 'B') {
4543 for (len = 0; len++ < aint;) {
4544 items |= *str++ & 1;
4548 *aptr++ = items & 0xff;
4554 for (len = 0; len++ < aint;) {
4560 *aptr++ = items & 0xff;
4566 if (datumtype == 'B')
4567 items <<= 7 - (aint & 7);
4569 items >>= 7 - (aint & 7);
4570 *aptr++ = items & 0xff;
4572 str = SvPVX(cat) + SvCUR(cat);
4587 str = SvPV(fromstr, fromlen);
4591 SvCUR(cat) += (len+1)/2;
4592 SvGROW(cat, SvCUR(cat) + 1);
4593 aptr = SvPVX(cat) + aint;
4598 if (datumtype == 'H') {
4599 for (len = 0; len++ < aint;) {
4601 items |= ((*str++ & 15) + 9) & 15;
4603 items |= *str++ & 15;
4607 *aptr++ = items & 0xff;
4613 for (len = 0; len++ < aint;) {
4615 items |= (((*str++ & 15) + 9) & 15) << 4;
4617 items |= (*str++ & 15) << 4;
4621 *aptr++ = items & 0xff;
4627 *aptr++ = items & 0xff;
4628 str = SvPVX(cat) + SvCUR(cat);
4639 aint = SvIV(fromstr);
4641 sv_catpvn(cat, &achar, sizeof(char));
4647 auint = SvUV(fromstr);
4648 SvGROW(cat, SvCUR(cat) + UTF8_MAXLEN);
4649 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4654 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4659 afloat = (float)SvNV(fromstr);
4660 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4667 adouble = (double)SvNV(fromstr);
4668 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4674 ashort = (I16)SvIV(fromstr);
4676 ashort = PerlSock_htons(ashort);
4678 CAT16(cat, &ashort);
4684 ashort = (I16)SvIV(fromstr);
4686 ashort = htovs(ashort);
4688 CAT16(cat, &ashort);
4692 #if SHORTSIZE != SIZE16
4694 unsigned short aushort;
4698 aushort = SvUV(fromstr);
4699 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4709 aushort = (U16)SvUV(fromstr);
4710 CAT16(cat, &aushort);
4716 #if SHORTSIZE != SIZE16
4722 ashort = SvIV(fromstr);
4723 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4731 ashort = (I16)SvIV(fromstr);
4732 CAT16(cat, &ashort);
4739 auint = SvUV(fromstr);
4740 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4746 adouble = Perl_floor(SvNV(fromstr));
4749 DIE(aTHX_ "Cannot compress negative numbers");
4755 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4756 adouble <= UV_MAX_cxux
4763 char buf[1 + sizeof(UV)];
4764 char *in = buf + sizeof(buf);
4765 UV auv = U_V(adouble);
4768 *--in = (auv & 0x7f) | 0x80;
4771 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4772 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4774 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4775 char *from, *result, *in;
4780 /* Copy string and check for compliance */
4781 from = SvPV(fromstr, len);
4782 if ((norm = is_an_int(from, len)) == NULL)
4783 DIE(aTHX_ "can compress only unsigned integer");
4785 New('w', result, len, char);
4789 *--in = div128(norm, &done) | 0x80;
4790 result[len - 1] &= 0x7F; /* clear continue bit */
4791 sv_catpvn(cat, in, (result + len) - in);
4793 SvREFCNT_dec(norm); /* free norm */
4795 else if (SvNOKp(fromstr)) {
4796 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4797 char *in = buf + sizeof(buf);
4800 double next = floor(adouble / 128);
4801 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4802 if (--in < buf) /* this cannot happen ;-) */
4803 DIE(aTHX_ "Cannot compress integer");
4805 } while (adouble > 0);
4806 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4807 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4810 DIE(aTHX_ "Cannot compress non integer");
4816 aint = SvIV(fromstr);
4817 sv_catpvn(cat, (char*)&aint, sizeof(int));
4823 aulong = SvUV(fromstr);
4825 aulong = PerlSock_htonl(aulong);
4827 CAT32(cat, &aulong);
4833 aulong = SvUV(fromstr);
4835 aulong = htovl(aulong);
4837 CAT32(cat, &aulong);
4841 #if LONGSIZE != SIZE32
4843 unsigned long aulong;
4847 aulong = SvUV(fromstr);
4848 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4856 aulong = SvUV(fromstr);
4857 CAT32(cat, &aulong);
4862 #if LONGSIZE != SIZE32
4868 along = SvIV(fromstr);
4869 sv_catpvn(cat, (char *)&along, sizeof(long));
4877 along = SvIV(fromstr);
4886 auquad = (Uquad_t)SvUV(fromstr);
4887 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4893 aquad = (Quad_t)SvIV(fromstr);
4894 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4899 len = 1; /* assume SV is correct length */
4904 if (fromstr == &PL_sv_undef)
4908 /* XXX better yet, could spirit away the string to
4909 * a safe spot and hang on to it until the result
4910 * of pack() (and all copies of the result) are
4913 if (ckWARN(WARN_PACK) && (SvTEMP(fromstr)
4914 || (SvPADTMP(fromstr)
4915 && !SvREADONLY(fromstr))))
4917 Perl_warner(aTHX_ WARN_PACK,
4918 "Attempt to pack pointer to temporary value");
4920 if (SvPOK(fromstr) || SvNIOK(fromstr))
4921 aptr = SvPV(fromstr,n_a);
4923 aptr = SvPV_force(fromstr,n_a);
4925 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4930 aptr = SvPV(fromstr, fromlen);
4931 SvGROW(cat, fromlen * 4 / 3);
4936 while (fromlen > 0) {
4943 doencodes(cat, aptr, todo);
4962 register I32 limit = POPi; /* note, negative is forever */
4965 register char *s = SvPV(sv, len);
4966 char *strend = s + len;
4968 register REGEXP *rx;
4972 I32 maxiters = (strend - s) + 10;
4975 I32 origlimit = limit;
4978 AV *oldstack = PL_curstack;
4979 I32 gimme = GIMME_V;
4980 I32 oldsave = PL_savestack_ix;
4981 I32 make_mortal = 1;
4982 MAGIC *mg = (MAGIC *) NULL;
4985 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4990 DIE(aTHX_ "panic: do_split");
4991 rx = pm->op_pmregexp;
4993 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4994 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4996 if (pm->op_pmreplroot) {
4998 ary = GvAVn((GV*)PL_curpad[(PADOFFSET)pm->op_pmreplroot]);
5000 ary = GvAVn((GV*)pm->op_pmreplroot);
5003 else if (gimme != G_ARRAY)
5005 ary = (AV*)PL_curpad[0];
5007 ary = GvAVn(PL_defgv);
5008 #endif /* USE_THREADS */
5011 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
5017 if (mg = SvTIED_mg((SV*)ary, 'P')) {
5019 XPUSHs(SvTIED_obj((SV*)ary, mg));
5025 for (i = AvFILLp(ary); i >= 0; i--)
5026 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5028 /* temporarily switch stacks */
5029 SWITCHSTACK(PL_curstack, ary);
5033 base = SP - PL_stack_base;
5035 if (pm->op_pmflags & PMf_SKIPWHITE) {
5036 if (pm->op_pmflags & PMf_LOCALE) {
5037 while (isSPACE_LC(*s))
5045 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
5046 SAVEINT(PL_multiline);
5047 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
5051 limit = maxiters + 2;
5052 if (pm->op_pmflags & PMf_WHITE) {
5055 while (m < strend &&
5056 !((pm->op_pmflags & PMf_LOCALE)
5057 ? isSPACE_LC(*m) : isSPACE(*m)))
5062 dstr = NEWSV(30, m-s);
5063 sv_setpvn(dstr, s, m-s);
5069 while (s < strend &&
5070 ((pm->op_pmflags & PMf_LOCALE)
5071 ? isSPACE_LC(*s) : isSPACE(*s)))
5075 else if (strEQ("^", rx->precomp)) {
5078 for (m = s; m < strend && *m != '\n'; m++) ;
5082 dstr = NEWSV(30, m-s);
5083 sv_setpvn(dstr, s, m-s);
5090 else if ((rx->reganch & RE_USE_INTUIT) && !rx->nparens
5091 && (rx->reganch & ROPT_CHECK_ALL)
5092 && !(rx->reganch & ROPT_ANCH)) {
5093 int tail = (rx->reganch & RE_INTUIT_TAIL);
5094 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
5098 if (len == 1 && !tail) {
5102 for (m = s; m < strend && *m != c; m++) ;
5105 dstr = NEWSV(30, m-s);
5106 sv_setpvn(dstr, s, m-s);
5115 while (s < strend && --limit &&
5116 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5117 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
5120 dstr = NEWSV(31, m-s);
5121 sv_setpvn(dstr, s, m-s);
5125 s = m + len; /* Fake \n at the end */
5130 maxiters += (strend - s) * rx->nparens;
5131 while (s < strend && --limit
5132 /* && (!rx->check_substr
5133 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
5135 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
5136 1 /* minend */, sv, NULL, 0))
5138 TAINT_IF(RX_MATCH_TAINTED(rx));
5139 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5144 strend = s + (strend - m);
5146 m = rx->startp[0] + orig;
5147 dstr = NEWSV(32, m-s);
5148 sv_setpvn(dstr, s, m-s);
5153 for (i = 1; i <= rx->nparens; i++) {
5154 s = rx->startp[i] + orig;
5155 m = rx->endp[i] + orig;
5157 dstr = NEWSV(33, m-s);
5158 sv_setpvn(dstr, s, m-s);
5161 dstr = NEWSV(33, 0);
5167 s = rx->endp[0] + orig;
5171 LEAVE_SCOPE(oldsave);
5172 iters = (SP - PL_stack_base) - base;
5173 if (iters > maxiters)
5174 DIE(aTHX_ "Split loop");
5176 /* keep field after final delim? */
5177 if (s < strend || (iters && origlimit)) {
5178 dstr = NEWSV(34, strend-s);
5179 sv_setpvn(dstr, s, strend-s);
5185 else if (!origlimit) {
5186 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5192 SWITCHSTACK(ary, oldstack);
5193 if (SvSMAGICAL(ary)) {
5198 if (gimme == G_ARRAY) {
5200 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5208 call_method("PUSH",G_SCALAR|G_DISCARD);
5211 if (gimme == G_ARRAY) {
5212 /* EXTEND should not be needed - we just popped them */
5214 for (i=0; i < iters; i++) {
5215 SV **svp = av_fetch(ary, i, FALSE);
5216 PUSHs((svp) ? *svp : &PL_sv_undef);
5223 if (gimme == G_ARRAY)
5226 if (iters || !pm->op_pmreplroot) {
5236 Perl_unlock_condpair(pTHX_ void *svv)
5239 MAGIC *mg = mg_find((SV*)svv, 'm');
5242 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
5243 MUTEX_LOCK(MgMUTEXP(mg));
5244 if (MgOWNER(mg) != thr)
5245 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
5247 COND_SIGNAL(MgOWNERCONDP(mg));
5248 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": unlock 0x%"UVxf"\n",
5249 PTR2UV(thr), PTR2UV(svv));)
5250 MUTEX_UNLOCK(MgMUTEXP(mg));
5252 #endif /* USE_THREADS */
5265 mg = condpair_magic(sv);
5266 MUTEX_LOCK(MgMUTEXP(mg));
5267 if (MgOWNER(mg) == thr)
5268 MUTEX_UNLOCK(MgMUTEXP(mg));
5271 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
5273 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": pp_lock lock 0x%"UVxf"\n",
5274 PTR2UV(thr), PTR2UV(sv));)
5275 MUTEX_UNLOCK(MgMUTEXP(mg));
5276 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
5278 #endif /* USE_THREADS */
5279 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5280 || SvTYPE(retsv) == SVt_PVCV) {
5281 retsv = refto(retsv);
5292 if (PL_op->op_private & OPpLVAL_INTRO)
5293 PUSHs(*save_threadsv(PL_op->op_targ));
5295 PUSHs(THREADSV(PL_op->op_targ));
5298 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
5299 #endif /* USE_THREADS */