3 * Copyright (c) 1991-2002, 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 * "'So that was the job I felt I had to do when I started,' thought Sam."
15 #define PERL_IN_DOOP_C
19 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
25 S_do_trans_simple(pTHX_ SV *sv)
32 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
37 tbl = (short*)cPVOP->op_pv;
39 Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__);
41 s = (U8*)SvPV(sv, len);
44 /* First, take care of non-UTF8 input strings, because they're easy */
47 if ((ch = tbl[*s]) >= 0) {
58 /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */
60 New(0, d, len*2+1, U8);
68 /* Need to check this, otherwise 128..255 won't match */
69 c = utf8n_to_uvchr(s, send - s, &ulen, 0);
70 if (c < 0x100 && (ch = tbl[c]) >= 0) {
72 d = uvchr_to_utf8(d, ch);
75 else { /* No match -> copy */
82 sv_setpvn(sv, (char*)dstart, d - dstart);
87 SvCUR_set(sv, d - dstart);
95 S_do_trans_count(pTHX_ SV *sv)
102 I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
104 tbl = (short*)cPVOP->op_pv;
106 Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__);
108 s = (U8*)SvPV(sv, len);
120 c = utf8n_to_uvchr(s, send - s, &ulen, 0);
124 } else if (complement)
133 S_do_trans_complex(pTHX_ SV *sv)
141 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
142 I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT;
143 I32 del = PL_op->op_private & OPpTRANS_DELETE;
144 STRLEN len, rlen = 0;
148 tbl = (short*)cPVOP->op_pv;
150 Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__);
152 s = (U8*)SvPV(sv, len);
158 if (PL_op->op_private & OPpTRANS_SQUASH) {
161 if ((ch = tbl[*s]) >= 0) {
164 if (p != d - 1 || *p != *d)
167 else if (ch == -1) /* -1 is unmapped character */
169 else if (ch == -2) /* -2 is delete character */
176 if ((ch = tbl[*s]) >= 0) {
180 else if (ch == -1) /* -1 is unmapped character */
182 else if (ch == -2) /* -2 is delete character */
188 SvCUR_set(sv, d - dstart);
192 New(0, d, len*2+1, U8);
196 if (complement && !del)
199 #ifdef MACOS_TRADITIONAL
200 #define comp CoMP /* "comp" is a keyword in some compilers ... */
203 if (PL_op->op_private & OPpTRANS_SQUASH) {
207 UV comp = utf8_to_uvchr(s, &len);
217 ch = (rlen == 0) ? comp :
218 (comp - 0x100 < rlen) ?
219 tbl[comp+1] : tbl[0x100+rlen];
221 d = uvchr_to_utf8(d, ch);
229 else if ((ch = tbl[comp]) >= 0) {
232 d = uvchr_to_utf8(d, ch);
238 else if (ch == -1) { /* -1 is unmapped character */
242 else if (ch == -2) /* -2 is delete character */
251 UV comp = utf8_to_uvchr(s, &len);
260 if (comp - 0x100 < rlen)
261 d = uvchr_to_utf8(d, tbl[comp+1]);
263 d = uvchr_to_utf8(d, tbl[0x100+rlen]);
267 else if ((ch = tbl[comp]) >= 0) {
268 d = uvchr_to_utf8(d, ch);
271 else if (ch == -1) { /* -1 is unmapped character */
275 else if (ch == -2) /* -2 is delete character */
281 sv_setpvn(sv, (char*)dstart, d - dstart);
286 SvCUR_set(sv, d - dstart);
295 S_do_trans_simple_utf8(pTHX_ SV *sv)
303 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
306 SV* rv = (SV*)cSVOP->op_sv;
307 HV* hv = (HV*)SvRV(rv);
308 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
309 UV none = svp ? SvUV(*svp) : 0x7fffffff;
316 s = (U8*)SvPV(sv, len);
319 U8 *t = s, *e = s + len;
322 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
326 s = bytes_to_utf8(s, &len);
331 svp = hv_fetch(hv, "FINAL", 5, FALSE);
336 /* d needs to be bigger than s, in case e.g. upgrading is required */
337 New(0, d, len*3+UTF8_MAXLEN, U8);
347 if ((uv = swash_fetch(rv, s, TRUE)) < none) {
350 d = uvuni_to_utf8(d, uv);
352 else if (uv == none) {
358 else if (uv == extra) {
362 d = uvuni_to_utf8(d, final);
368 STRLEN clen = d - dstart;
369 STRLEN nlen = dend - dstart + len + UTF8_MAXLEN;
371 Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__);
372 Renew(dstart, nlen+UTF8_MAXLEN, U8);
374 dend = dstart + nlen;
377 if (grows || hibit) {
378 sv_setpvn(sv, (char*)dstart, d - dstart);
385 SvCUR_set(sv, d - dstart);
394 S_do_trans_count_utf8(pTHX_ SV *sv)
397 U8 *start = 0, *send;
401 SV* rv = (SV*)cSVOP->op_sv;
402 HV* hv = (HV*)SvRV(rv);
403 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
404 UV none = svp ? SvUV(*svp) : 0x7fffffff;
409 s = (U8*)SvPV(sv, len);
411 U8 *t = s, *e = s + len;
414 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
418 start = s = bytes_to_utf8(s, &len);
423 if ((uv = swash_fetch(rv, s, TRUE)) < none || uv == extra)
434 S_do_trans_complex_utf8(pTHX_ SV *sv)
440 I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
441 I32 del = PL_op->op_private & OPpTRANS_DELETE;
442 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
443 SV* rv = (SV*)cSVOP->op_sv;
444 HV* hv = (HV*)SvRV(rv);
445 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
446 UV none = svp ? SvUV(*svp) : 0x7fffffff;
449 bool havefinal = FALSE;
456 s = (U8*)SvPV(sv, len);
459 U8 *t = s, *e = s + len;
462 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
466 s = bytes_to_utf8(s, &len);
471 svp = hv_fetch(hv, "FINAL", 5, FALSE);
478 /* d needs to be bigger than s, in case e.g. upgrading is required */
479 New(0, d, len*3+UTF8_MAXLEN, U8);
491 uv = swash_fetch(rv, s, TRUE);
494 STRLEN clen = d - dstart;
495 STRLEN nlen = dend - dstart + len + UTF8_MAXLEN;
497 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
498 Renew(dstart, nlen+UTF8_MAXLEN, U8);
500 dend = dstart + nlen;
506 d = uvuni_to_utf8(d, uv);
511 else if (uv == none) { /* "none" is unmapped character */
519 else if (uv == extra && !del) {
524 d = uvuni_to_utf8(d, final);
530 uv = utf8_to_uvuni(s, &len);
540 matches++; /* "none+1" is delete character */
546 uv = swash_fetch(rv, s, TRUE);
548 STRLEN clen = d - dstart;
549 STRLEN nlen = dend - dstart + len + UTF8_MAXLEN;
551 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
552 Renew(dstart, nlen+UTF8_MAXLEN, U8);
554 dend = dstart + nlen;
559 d = uvuni_to_utf8(d, uv);
562 else if (uv == none) { /* "none" is unmapped character */
569 else if (uv == extra && !del) {
572 d = uvuni_to_utf8(d, final);
575 matches++; /* "none+1" is delete character */
579 if (grows || hibit) {
580 sv_setpvn(sv, (char*)dstart, d - dstart);
587 SvCUR_set(sv, d - dstart);
596 Perl_do_trans(pTHX_ SV *sv)
599 I32 hasutf = (PL_op->op_private &
600 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
602 if (SvREADONLY(sv)) {
605 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
606 Perl_croak(aTHX_ PL_no_modify);
612 (void)SvPV_force(sv, len);
613 if (!(PL_op->op_private & OPpTRANS_IDENTICAL))
614 (void)SvPOK_only_UTF8(sv);
616 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
618 switch (PL_op->op_private & ~hasutf & 63) {
621 return do_trans_simple_utf8(sv);
623 return do_trans_simple(sv);
625 case OPpTRANS_IDENTICAL:
626 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
628 return do_trans_count_utf8(sv);
630 return do_trans_count(sv);
634 return do_trans_complex_utf8(sv);
636 return do_trans_complex(sv);
641 Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
644 register I32 items = sp - mark;
649 (void) SvPV(del, delimlen); /* stringify and get the delimlen */
650 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
653 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
654 (void)SvUPGRADE(sv, SVt_PV);
655 if (SvLEN(sv) < len + items) { /* current length is way too short */
656 while (items-- > 0) {
657 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
663 SvGROW(sv, len + 1); /* so try to pre-extend */
671 if (PL_tainting && SvMAGICAL(sv))
681 for (; items > 0; items--,mark++) {
687 for (; items > 0; items--,mark++)
694 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
697 char *pat = SvPV(*sarg, patlen);
698 bool do_taint = FALSE;
703 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
709 /* currently converts input to bytes if possible, but doesn't sweat failure */
711 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
714 unsigned char *s = (unsigned char *) SvPV(sv, srclen);
719 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
720 Perl_croak(aTHX_ "Illegal number of bits in vec");
723 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
725 offset *= size; /* turn into bit offset */
726 len = (offset + size + 7) / 8; /* required number of bytes */
731 offset >>= 3; /* turn into byte offset */
733 if (offset >= srclen)
736 retnum = (UV) s[offset] << 8;
738 else if (size == 32) {
739 if (offset >= srclen)
741 else if (offset + 1 >= srclen)
743 ((UV) s[offset ] << 24);
744 else if (offset + 2 >= srclen)
746 ((UV) s[offset ] << 24) +
747 ((UV) s[offset + 1] << 16);
750 ((UV) s[offset ] << 24) +
751 ((UV) s[offset + 1] << 16) +
752 ( s[offset + 2] << 8);
755 else if (size == 64) {
756 if (ckWARN(WARN_PORTABLE))
757 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
758 "Bit vector size > 32 non-portable");
759 if (offset >= srclen)
761 else if (offset + 1 >= srclen)
763 (UV) s[offset ] << 56;
764 else if (offset + 2 >= srclen)
766 ((UV) s[offset ] << 56) +
767 ((UV) s[offset + 1] << 48);
768 else if (offset + 3 >= srclen)
770 ((UV) s[offset ] << 56) +
771 ((UV) s[offset + 1] << 48) +
772 ((UV) s[offset + 2] << 40);
773 else if (offset + 4 >= srclen)
775 ((UV) s[offset ] << 56) +
776 ((UV) s[offset + 1] << 48) +
777 ((UV) s[offset + 2] << 40) +
778 ((UV) s[offset + 3] << 32);
779 else if (offset + 5 >= srclen)
781 ((UV) s[offset ] << 56) +
782 ((UV) s[offset + 1] << 48) +
783 ((UV) s[offset + 2] << 40) +
784 ((UV) s[offset + 3] << 32) +
785 ( s[offset + 4] << 24);
786 else if (offset + 6 >= srclen)
788 ((UV) s[offset ] << 56) +
789 ((UV) s[offset + 1] << 48) +
790 ((UV) s[offset + 2] << 40) +
791 ((UV) s[offset + 3] << 32) +
792 ((UV) s[offset + 4] << 24) +
793 ((UV) s[offset + 5] << 16);
796 ((UV) s[offset ] << 56) +
797 ((UV) s[offset + 1] << 48) +
798 ((UV) s[offset + 2] << 40) +
799 ((UV) s[offset + 3] << 32) +
800 ((UV) s[offset + 4] << 24) +
801 ((UV) s[offset + 5] << 16) +
802 ( s[offset + 6] << 8);
808 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
810 offset >>= 3; /* turn into byte offset */
815 ((UV) s[offset] << 8) +
819 ((UV) s[offset ] << 24) +
820 ((UV) s[offset + 1] << 16) +
821 ( s[offset + 2] << 8) +
824 else if (size == 64) {
825 if (ckWARN(WARN_PORTABLE))
826 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
827 "Bit vector size > 32 non-portable");
829 ((UV) s[offset ] << 56) +
830 ((UV) s[offset + 1] << 48) +
831 ((UV) s[offset + 2] << 40) +
832 ((UV) s[offset + 3] << 32) +
833 ((UV) s[offset + 4] << 24) +
834 ((UV) s[offset + 5] << 16) +
835 ( s[offset + 6] << 8) +
844 /* currently converts input to bytes if possible but doesn't sweat failures,
845 * although it does ensure that the string it clobbers is not marked as
846 * utf8-valid any more
849 Perl_do_vecset(pTHX_ SV *sv)
851 SV *targ = LvTARG(sv);
854 register unsigned char *s;
862 s = (unsigned char*)SvPV_force(targ, targlen);
864 /* This is handled by the SvPOK_only below...
865 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
868 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
871 (void)SvPOK_only(targ);
873 offset = LvTARGOFF(sv);
875 Perl_croak(aTHX_ "Negative offset to vec in lvalue context");
876 size = LvTARGLEN(sv);
877 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
878 Perl_croak(aTHX_ "Illegal number of bits in vec");
880 offset *= size; /* turn into bit offset */
881 len = (offset + size + 7) / 8; /* required number of bytes */
883 s = (unsigned char*)SvGROW(targ, len + 1);
884 (void)memzero((char *)(s + targlen), len - targlen + 1);
885 SvCUR_set(targ, len);
889 mask = (1 << size) - 1;
892 offset >>= 3; /* turn into byte offset */
893 s[offset] &= ~(mask << size);
894 s[offset] |= lval << size;
897 offset >>= 3; /* turn into byte offset */
899 s[offset ] = lval & 0xff;
900 else if (size == 16) {
901 s[offset ] = (lval >> 8) & 0xff;
902 s[offset+1] = lval & 0xff;
904 else if (size == 32) {
905 s[offset ] = (lval >> 24) & 0xff;
906 s[offset+1] = (lval >> 16) & 0xff;
907 s[offset+2] = (lval >> 8) & 0xff;
908 s[offset+3] = lval & 0xff;
911 else if (size == 64) {
912 if (ckWARN(WARN_PORTABLE))
913 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
914 "Bit vector size > 32 non-portable");
915 s[offset ] = (lval >> 56) & 0xff;
916 s[offset+1] = (lval >> 48) & 0xff;
917 s[offset+2] = (lval >> 40) & 0xff;
918 s[offset+3] = (lval >> 32) & 0xff;
919 s[offset+4] = (lval >> 24) & 0xff;
920 s[offset+5] = (lval >> 16) & 0xff;
921 s[offset+6] = (lval >> 8) & 0xff;
922 s[offset+7] = lval & 0xff;
930 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
935 if (SvTYPE(sv) == SVt_PVAV) {
940 for (i = 0; i <= max; i++) {
941 sv = (SV*)av_fetch(av, i, FALSE);
942 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
947 else if (SvTYPE(sv) == SVt_PVHV) {
950 (void)hv_iterinit(hv);
952 while ((entry = hv_iternext(hv)))
953 do_chop(astr,hv_iterval(hv,entry));
956 else if (SvREADONLY(sv)) {
958 /* SV is copy-on-write */
959 sv_force_normal_flags(sv, 0);
962 Perl_croak(aTHX_ PL_no_modify);
965 if (len && !SvPOK(sv))
966 s = SvPV_force(sv, len);
969 char *send = s + len;
972 while (s > start && UTF8_IS_CONTINUATION(*s))
974 if (utf8_to_uvchr((U8*)s, 0)) {
975 sv_setpvn(astr, s, send - s);
977 SvCUR_set(sv, s - start);
983 sv_setpvn(astr, "", 0);
987 sv_setpvn(astr, s, 1);
994 sv_setpvn(astr, "", 0);
999 Perl_do_chomp(pTHX_ register SV *sv)
1008 if (RsRECORD(PL_rs))
1011 if (SvTYPE(sv) == SVt_PVAV) {
1016 for (i = 0; i <= max; i++) {
1017 sv = (SV*)av_fetch(av, i, FALSE);
1018 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1019 count += do_chomp(sv);
1023 else if (SvTYPE(sv) == SVt_PVHV) {
1026 (void)hv_iterinit(hv);
1028 while ((entry = hv_iternext(hv)))
1029 count += do_chomp(hv_iterval(hv,entry));
1032 else if (SvREADONLY(sv)) {
1034 /* SV is copy-on-write */
1035 sv_force_normal_flags(sv, 0);
1038 Perl_croak(aTHX_ PL_no_modify);
1043 if (RsPARA(PL_rs)) {
1047 while (len && s[-1] == '\n') {
1055 char *rsptr = SvPV(PL_rs, rslen);
1062 if (len < rslen - 1)
1066 if (memNE(s, rsptr, rslen))
1071 s = SvPV_force(sv, n_a);
1082 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1098 bool left_utf = DO_UTF8(left);
1099 bool right_utf = DO_UTF8(right);
1102 if (left_utf && !right_utf)
1103 sv_utf8_upgrade(right);
1104 else if (!left_utf && right_utf)
1105 sv_utf8_upgrade(left);
1107 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1108 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1109 lsave = lc = SvPV(left, leftlen);
1110 rsave = rc = SvPV(right, rightlen);
1111 len = leftlen < rightlen ? leftlen : rightlen;
1113 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1114 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1115 Newz(801, dc, needlen + 1, char);
1117 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1119 dc = SvPV_force(sv, n_a);
1120 if (SvCUR(sv) < len) {
1121 dc = SvGROW(sv, len + 1);
1122 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1124 if (optype != OP_BIT_AND && (left_utf || right_utf))
1125 dc = SvGROW(sv, leftlen + rightlen + 1);
1128 needlen = ((optype == OP_BIT_AND)
1129 ? len : (leftlen > rightlen ? leftlen : rightlen));
1130 Newz(801, dc, needlen + 1, char);
1131 (void)sv_usepvn(sv, dc, needlen);
1132 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1135 (void)SvPOK_only(sv);
1136 if (left_utf || right_utf) {
1139 STRLEN lulen = leftlen;
1140 STRLEN rulen = rightlen;
1145 while (lulen && rulen) {
1146 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1149 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1153 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1155 if (sv == left || sv == right)
1156 (void)sv_usepvn(sv, dcsave, needlen);
1157 SvCUR_set(sv, dc - dcsave);
1160 while (lulen && rulen) {
1161 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1164 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1168 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1172 while (lulen && rulen) {
1173 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1176 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1180 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1183 if (sv == left || sv == right)
1184 (void)sv_usepvn(sv, dcsave, needlen);
1185 SvCUR_set(sv, dc - dcsave);
1187 sv_catpvn(sv, rc, rulen);
1189 sv_catpvn(sv, lc, lulen);
1199 if (len >= sizeof(long)*4 &&
1200 !((long)dc % sizeof(long)) &&
1201 !((long)lc % sizeof(long)) &&
1202 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1204 I32 remainder = len % (sizeof(long)*4);
1205 len /= (sizeof(long)*4);
1214 *dl++ = *ll++ & *rl++;
1215 *dl++ = *ll++ & *rl++;
1216 *dl++ = *ll++ & *rl++;
1217 *dl++ = *ll++ & *rl++;
1222 *dl++ = *ll++ ^ *rl++;
1223 *dl++ = *ll++ ^ *rl++;
1224 *dl++ = *ll++ ^ *rl++;
1225 *dl++ = *ll++ ^ *rl++;
1230 *dl++ = *ll++ | *rl++;
1231 *dl++ = *ll++ | *rl++;
1232 *dl++ = *ll++ | *rl++;
1233 *dl++ = *ll++ | *rl++;
1248 *dc++ = *lc++ & *rc++;
1252 *dc++ = *lc++ ^ *rc++;
1256 *dc++ = *lc++ | *rc++;
1260 sv_catpvn(sv, rsave + len, rightlen - len);
1261 else if (leftlen > len)
1262 sv_catpvn(sv, lsave + len, leftlen - len);
1280 I32 gimme = GIMME_V;
1281 I32 dokeys = (PL_op->op_type == OP_KEYS);
1282 I32 dovalues = (PL_op->op_type == OP_VALUES);
1283 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1285 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
1286 dokeys = dovalues = TRUE;
1289 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1290 dTARGET; /* make sure to clear its target here */
1291 if (SvTYPE(TARG) == SVt_PVLV)
1292 LvTARG(TARG) = Nullsv;
1298 keys = realhv ? hv : avhv_keys((AV*)hv);
1299 (void)hv_iterinit(keys); /* always reset iterator regardless */
1301 if (gimme == G_VOID)
1304 if (gimme == G_SCALAR) {
1308 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1309 if (SvTYPE(TARG) < SVt_PVLV) {
1310 sv_upgrade(TARG, SVt_PVLV);
1311 sv_magic(TARG, Nullsv, PERL_MAGIC_nkeys, Nullch, 0);
1314 if (LvTARG(TARG) != (SV*)keys) {
1316 SvREFCNT_dec(LvTARG(TARG));
1317 LvTARG(TARG) = SvREFCNT_inc(keys);
1323 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied))
1328 while (hv_iternext(keys)) i++;
1334 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1336 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1337 while ((entry = hv_iternext(keys))) {
1340 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
1344 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
1345 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1346 (unsigned long)HeHASH(entry),
1348 (unsigned long)(HeHASH(entry) & HvMAX(keys))));