3 * Copyright (c) 1991-2001, 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) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
603 Perl_croak(aTHX_ PL_no_modify);
609 (void)SvPV_force(sv, len);
610 if (!(PL_op->op_private & OPpTRANS_IDENTICAL))
611 (void)SvPOK_only_UTF8(sv);
613 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
615 switch (PL_op->op_private & ~hasutf & 63) {
618 return do_trans_simple_utf8(sv);
620 return do_trans_simple(sv);
622 case OPpTRANS_IDENTICAL:
623 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
625 return do_trans_count_utf8(sv);
627 return do_trans_count(sv);
631 return do_trans_complex_utf8(sv);
633 return do_trans_complex(sv);
638 Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
641 register I32 items = sp - mark;
646 (void) SvPV(del, delimlen); /* stringify and get the delimlen */
647 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
650 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
651 (void)SvUPGRADE(sv, SVt_PV);
652 if (SvLEN(sv) < len + items) { /* current length is way too short */
653 while (items-- > 0) {
654 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
660 SvGROW(sv, len + 1); /* so try to pre-extend */
668 if (PL_tainting && SvMAGICAL(sv))
678 for (; items > 0; items--,mark++) {
684 for (; items > 0; items--,mark++)
691 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
694 char *pat = SvPV(*sarg, patlen);
695 bool do_taint = FALSE;
697 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
703 /* currently converts input to bytes if possible, but doesn't sweat failure */
705 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
708 unsigned char *s = (unsigned char *) SvPV(sv, srclen);
713 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
714 Perl_croak(aTHX_ "Illegal number of bits in vec");
717 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
719 offset *= size; /* turn into bit offset */
720 len = (offset + size + 7) / 8; /* required number of bytes */
725 offset >>= 3; /* turn into byte offset */
727 if (offset >= srclen)
730 retnum = (UV) s[offset] << 8;
732 else if (size == 32) {
733 if (offset >= srclen)
735 else if (offset + 1 >= srclen)
737 ((UV) s[offset ] << 24);
738 else if (offset + 2 >= srclen)
740 ((UV) s[offset ] << 24) +
741 ((UV) s[offset + 1] << 16);
744 ((UV) s[offset ] << 24) +
745 ((UV) s[offset + 1] << 16) +
746 ( s[offset + 2] << 8);
749 else if (size == 64) {
750 if (ckWARN(WARN_PORTABLE))
751 Perl_warner(aTHX_ WARN_PORTABLE,
752 "Bit vector size > 32 non-portable");
753 if (offset >= srclen)
755 else if (offset + 1 >= srclen)
757 (UV) s[offset ] << 56;
758 else if (offset + 2 >= srclen)
760 ((UV) s[offset ] << 56) +
761 ((UV) s[offset + 1] << 48);
762 else if (offset + 3 >= srclen)
764 ((UV) s[offset ] << 56) +
765 ((UV) s[offset + 1] << 48) +
766 ((UV) s[offset + 2] << 40);
767 else if (offset + 4 >= srclen)
769 ((UV) s[offset ] << 56) +
770 ((UV) s[offset + 1] << 48) +
771 ((UV) s[offset + 2] << 40) +
772 ((UV) s[offset + 3] << 32);
773 else if (offset + 5 >= 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 ( s[offset + 4] << 24);
780 else if (offset + 6 >= srclen)
782 ((UV) s[offset ] << 56) +
783 ((UV) s[offset + 1] << 48) +
784 ((UV) s[offset + 2] << 40) +
785 ((UV) s[offset + 3] << 32) +
786 ((UV) s[offset + 4] << 24) +
787 ((UV) s[offset + 5] << 16);
790 ((UV) s[offset ] << 56) +
791 ((UV) s[offset + 1] << 48) +
792 ((UV) s[offset + 2] << 40) +
793 ((UV) s[offset + 3] << 32) +
794 ((UV) s[offset + 4] << 24) +
795 ((UV) s[offset + 5] << 16) +
796 ( s[offset + 6] << 8);
802 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
804 offset >>= 3; /* turn into byte offset */
809 ((UV) s[offset] << 8) +
813 ((UV) s[offset ] << 24) +
814 ((UV) s[offset + 1] << 16) +
815 ( s[offset + 2] << 8) +
818 else if (size == 64) {
819 if (ckWARN(WARN_PORTABLE))
820 Perl_warner(aTHX_ WARN_PORTABLE,
821 "Bit vector size > 32 non-portable");
823 ((UV) s[offset ] << 56) +
824 ((UV) s[offset + 1] << 48) +
825 ((UV) s[offset + 2] << 40) +
826 ((UV) s[offset + 3] << 32) +
827 ((UV) s[offset + 4] << 24) +
828 ((UV) s[offset + 5] << 16) +
829 ( s[offset + 6] << 8) +
838 /* currently converts input to bytes if possible but doesn't sweat failures,
839 * although it does ensure that the string it clobbers is not marked as
840 * utf8-valid any more
843 Perl_do_vecset(pTHX_ SV *sv)
845 SV *targ = LvTARG(sv);
848 register unsigned char *s;
856 s = (unsigned char*)SvPV_force(targ, targlen);
858 /* This is handled by the SvPOK_only below...
859 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
862 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
865 (void)SvPOK_only(targ);
867 offset = LvTARGOFF(sv);
869 Perl_croak(aTHX_ "Assigning to negative offset in vec");
870 size = LvTARGLEN(sv);
871 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
872 Perl_croak(aTHX_ "Illegal number of bits in vec");
874 offset *= size; /* turn into bit offset */
875 len = (offset + size + 7) / 8; /* required number of bytes */
877 s = (unsigned char*)SvGROW(targ, len + 1);
878 (void)memzero((char *)(s + targlen), len - targlen + 1);
879 SvCUR_set(targ, len);
883 mask = (1 << size) - 1;
886 offset >>= 3; /* turn into byte offset */
887 s[offset] &= ~(mask << size);
888 s[offset] |= lval << size;
891 offset >>= 3; /* turn into byte offset */
893 s[offset ] = lval & 0xff;
894 else if (size == 16) {
895 s[offset ] = (lval >> 8) & 0xff;
896 s[offset+1] = lval & 0xff;
898 else if (size == 32) {
899 s[offset ] = (lval >> 24) & 0xff;
900 s[offset+1] = (lval >> 16) & 0xff;
901 s[offset+2] = (lval >> 8) & 0xff;
902 s[offset+3] = lval & 0xff;
905 else if (size == 64) {
906 if (ckWARN(WARN_PORTABLE))
907 Perl_warner(aTHX_ WARN_PORTABLE,
908 "Bit vector size > 32 non-portable");
909 s[offset ] = (lval >> 56) & 0xff;
910 s[offset+1] = (lval >> 48) & 0xff;
911 s[offset+2] = (lval >> 40) & 0xff;
912 s[offset+3] = (lval >> 32) & 0xff;
913 s[offset+4] = (lval >> 24) & 0xff;
914 s[offset+5] = (lval >> 16) & 0xff;
915 s[offset+6] = (lval >> 8) & 0xff;
916 s[offset+7] = lval & 0xff;
924 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
929 if (SvTYPE(sv) == SVt_PVAV) {
934 for (i = 0; i <= max; i++) {
935 sv = (SV*)av_fetch(av, i, FALSE);
936 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
941 else if (SvTYPE(sv) == SVt_PVHV) {
944 (void)hv_iterinit(hv);
946 while ((entry = hv_iternext(hv)))
947 do_chop(astr,hv_iterval(hv,entry));
950 else if (SvREADONLY(sv))
951 Perl_croak(aTHX_ PL_no_modify);
953 if (len && !SvPOK(sv))
954 s = SvPV_force(sv, len);
957 char *send = s + len;
960 while (s > start && UTF8_IS_CONTINUATION(*s))
962 if (utf8_to_uvchr((U8*)s, 0)) {
963 sv_setpvn(astr, s, send - s);
965 SvCUR_set(sv, s - start);
971 sv_setpvn(astr, "", 0);
975 sv_setpvn(astr, s, 1);
982 sv_setpvn(astr, "", 0);
987 Perl_do_chomp(pTHX_ register SV *sv)
999 if (SvTYPE(sv) == SVt_PVAV) {
1004 for (i = 0; i <= max; i++) {
1005 sv = (SV*)av_fetch(av, i, FALSE);
1006 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1007 count += do_chomp(sv);
1011 else if (SvTYPE(sv) == SVt_PVHV) {
1014 (void)hv_iterinit(hv);
1016 while ((entry = hv_iternext(hv)))
1017 count += do_chomp(hv_iterval(hv,entry));
1020 else if (SvREADONLY(sv))
1021 Perl_croak(aTHX_ PL_no_modify);
1025 if (RsPARA(PL_rs)) {
1029 while (len && s[-1] == '\n') {
1037 char *rsptr = SvPV(PL_rs, rslen);
1044 if (len < rslen - 1)
1048 if (memNE(s, rsptr, rslen))
1053 s = SvPV_force(sv, n_a);
1064 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1080 bool left_utf = DO_UTF8(left);
1081 bool right_utf = DO_UTF8(right);
1084 if (left_utf && !right_utf)
1085 sv_utf8_upgrade(right);
1086 else if (!left_utf && right_utf)
1087 sv_utf8_upgrade(left);
1089 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1090 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1091 lsave = lc = SvPV(left, leftlen);
1092 rsave = rc = SvPV(right, rightlen);
1093 len = leftlen < rightlen ? leftlen : rightlen;
1095 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1096 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1097 Newz(801, dc, needlen + 1, char);
1099 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1101 dc = SvPV_force(sv, n_a);
1102 if (SvCUR(sv) < len) {
1103 dc = SvGROW(sv, len + 1);
1104 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1106 if (optype != OP_BIT_AND && (left_utf || right_utf))
1107 dc = SvGROW(sv, leftlen + rightlen + 1);
1110 needlen = ((optype == OP_BIT_AND)
1111 ? len : (leftlen > rightlen ? leftlen : rightlen));
1112 Newz(801, dc, needlen + 1, char);
1113 (void)sv_usepvn(sv, dc, needlen);
1114 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1117 (void)SvPOK_only(sv);
1118 if (left_utf || right_utf) {
1121 STRLEN lulen = leftlen;
1122 STRLEN rulen = rightlen;
1127 while (lulen && rulen) {
1128 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1131 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1135 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1137 if (sv == left || sv == right)
1138 (void)sv_usepvn(sv, dcsave, needlen);
1139 SvCUR_set(sv, dc - dcsave);
1142 while (lulen && rulen) {
1143 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1146 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1150 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1154 while (lulen && rulen) {
1155 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1158 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1162 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1165 if (sv == left || sv == right)
1166 (void)sv_usepvn(sv, dcsave, needlen);
1167 SvCUR_set(sv, dc - dcsave);
1169 sv_catpvn(sv, rc, rulen);
1171 sv_catpvn(sv, lc, lulen);
1181 if (len >= sizeof(long)*4 &&
1182 !((long)dc % sizeof(long)) &&
1183 !((long)lc % sizeof(long)) &&
1184 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1186 I32 remainder = len % (sizeof(long)*4);
1187 len /= (sizeof(long)*4);
1196 *dl++ = *ll++ & *rl++;
1197 *dl++ = *ll++ & *rl++;
1198 *dl++ = *ll++ & *rl++;
1199 *dl++ = *ll++ & *rl++;
1204 *dl++ = *ll++ ^ *rl++;
1205 *dl++ = *ll++ ^ *rl++;
1206 *dl++ = *ll++ ^ *rl++;
1207 *dl++ = *ll++ ^ *rl++;
1212 *dl++ = *ll++ | *rl++;
1213 *dl++ = *ll++ | *rl++;
1214 *dl++ = *ll++ | *rl++;
1215 *dl++ = *ll++ | *rl++;
1230 *dc++ = *lc++ & *rc++;
1234 *dc++ = *lc++ ^ *rc++;
1238 *dc++ = *lc++ | *rc++;
1242 sv_catpvn(sv, rsave + len, rightlen - len);
1243 else if (leftlen > len)
1244 sv_catpvn(sv, lsave + len, leftlen - len);
1262 I32 gimme = GIMME_V;
1263 I32 dokeys = (PL_op->op_type == OP_KEYS);
1264 I32 dovalues = (PL_op->op_type == OP_VALUES);
1265 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1267 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
1268 dokeys = dovalues = TRUE;
1271 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1272 dTARGET; /* make sure to clear its target here */
1273 if (SvTYPE(TARG) == SVt_PVLV)
1274 LvTARG(TARG) = Nullsv;
1280 keys = realhv ? hv : avhv_keys((AV*)hv);
1281 (void)hv_iterinit(keys); /* always reset iterator regardless */
1283 if (gimme == G_VOID)
1286 if (gimme == G_SCALAR) {
1290 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1291 if (SvTYPE(TARG) < SVt_PVLV) {
1292 sv_upgrade(TARG, SVt_PVLV);
1293 sv_magic(TARG, Nullsv, PERL_MAGIC_nkeys, Nullch, 0);
1296 if (LvTARG(TARG) != (SV*)keys) {
1298 SvREFCNT_dec(LvTARG(TARG));
1299 LvTARG(TARG) = SvREFCNT_inc(keys);
1305 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied))
1310 while (hv_iternext(keys)) i++;
1316 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1318 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1319 while ((entry = hv_iternext(keys))) {
1322 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
1326 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
1327 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1328 (unsigned long)HeHASH(entry),
1330 (unsigned long)(HeHASH(entry) & HvMAX(keys))));