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)/* SPC - OK */
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)/* SPC - NOT OK */
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)/* SPC - OK */
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);
389 /* Downgrading just 'cos it will is suspect - NI-S */
390 if (!isutf8 && !(PL_hints & HINT_UTF8))
391 sv_utf8_downgrade(sv, TRUE);
397 S_do_trans_count_utf8(pTHX_ SV *sv)/* SPC - OK */
400 U8 *start = 0, *send;
404 SV* rv = (SV*)cSVOP->op_sv;
405 HV* hv = (HV*)SvRV(rv);
406 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
407 UV none = svp ? SvUV(*svp) : 0x7fffffff;
412 s = (U8*)SvPV(sv, len);
414 U8 *t = s, *e = s + len;
417 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
421 start = s = bytes_to_utf8(s, &len);
426 if ((uv = swash_fetch(rv, s, TRUE)) < none || uv == extra)
437 S_do_trans_complex_utf8(pTHX_ SV *sv) /* SPC - NOT OK */
443 I32 squash = PL_op->op_private & OPpTRANS_SQUASH;
444 I32 del = PL_op->op_private & OPpTRANS_DELETE;
445 I32 grows = PL_op->op_private & OPpTRANS_GROWS;
446 SV* rv = (SV*)cSVOP->op_sv;
447 HV* hv = (HV*)SvRV(rv);
448 SV** svp = hv_fetch(hv, "NONE", 4, FALSE);
449 UV none = svp ? SvUV(*svp) : 0x7fffffff;
452 bool havefinal = FALSE;
459 s = (U8*)SvPV(sv, len);
462 U8 *t = s, *e = s + len;
465 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
469 s = bytes_to_utf8(s, &len);
474 svp = hv_fetch(hv, "FINAL", 5, FALSE);
481 /* d needs to be bigger than s, in case e.g. upgrading is required */
482 New(0, d, len*3+UTF8_MAXLEN, U8);
494 uv = swash_fetch(rv, s, TRUE);
497 STRLEN clen = d - dstart;
498 STRLEN nlen = dend - dstart + len + UTF8_MAXLEN;
500 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
501 Renew(dstart, nlen+UTF8_MAXLEN, U8);
503 dend = dstart + nlen;
509 d = uvuni_to_utf8(d, uv);
514 else if (uv == none) { /* "none" is unmapped character */
522 else if (uv == extra && !del) {
527 d = uvuni_to_utf8(d, final);
533 uv = utf8_to_uvuni(s, &len);
543 matches++; /* "none+1" is delete character */
549 uv = swash_fetch(rv, s, TRUE);
551 STRLEN clen = d - dstart;
552 STRLEN nlen = dend - dstart + len + UTF8_MAXLEN;
554 Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__);
555 Renew(dstart, nlen+UTF8_MAXLEN, U8);
557 dend = dstart + nlen;
562 d = uvuni_to_utf8(d, uv);
565 else if (uv == none) { /* "none" is unmapped character */
572 else if (uv == extra && !del) {
575 d = uvuni_to_utf8(d, final);
578 matches++; /* "none+1" is delete character */
582 if (grows || hibit) {
583 sv_setpvn(sv, (char*)dstart, d - dstart);
590 SvCUR_set(sv, d - dstart);
593 if (!isutf8 && !(PL_hints & HINT_UTF8))
594 sv_utf8_downgrade(sv, TRUE);
601 Perl_do_trans(pTHX_ SV *sv)
604 I32 hasutf = (PL_op->op_private &
605 (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF));
607 if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL))
608 Perl_croak(aTHX_ PL_no_modify);
614 (void)SvPV_force(sv, len);
615 if (!(PL_op->op_private & OPpTRANS_IDENTICAL))
616 (void)SvPOK_only_UTF8(sv);
618 DEBUG_t( Perl_deb(aTHX_ "2.TBL\n"));
620 switch (PL_op->op_private & ~hasutf & 63) {
623 return do_trans_simple_utf8(sv);
625 return do_trans_simple(sv);
627 case OPpTRANS_IDENTICAL:
628 case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT:
630 return do_trans_count_utf8(sv);
632 return do_trans_count(sv);
636 return do_trans_complex_utf8(sv);
638 return do_trans_complex(sv);
643 Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp)
646 register I32 items = sp - mark;
651 (void) SvPV(del, delimlen); /* stringify and get the delimlen */
652 /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */
655 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
656 (void)SvUPGRADE(sv, SVt_PV);
657 if (SvLEN(sv) < len + items) { /* current length is way too short */
658 while (items-- > 0) {
659 if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) {
665 SvGROW(sv, len + 1); /* so try to pre-extend */
673 if (PL_tainting && SvMAGICAL(sv))
683 for (; items > 0; items--,mark++) {
689 for (; items > 0; items--,mark++)
696 Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg)
699 char *pat = SvPV(*sarg, patlen);
700 bool do_taint = FALSE;
702 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
708 /* currently converts input to bytes if possible, but doesn't sweat failure */
710 Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size)
713 unsigned char *s = (unsigned char *) SvPV(sv, srclen);
718 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
719 Perl_croak(aTHX_ "Illegal number of bits in vec");
722 (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE);
724 offset *= size; /* turn into bit offset */
725 len = (offset + size + 7) / 8; /* required number of bytes */
730 offset >>= 3; /* turn into byte offset */
732 if (offset >= srclen)
735 retnum = (UV) s[offset] << 8;
737 else if (size == 32) {
738 if (offset >= srclen)
740 else if (offset + 1 >= srclen)
742 ((UV) s[offset ] << 24);
743 else if (offset + 2 >= srclen)
745 ((UV) s[offset ] << 24) +
746 ((UV) s[offset + 1] << 16);
749 ((UV) s[offset ] << 24) +
750 ((UV) s[offset + 1] << 16) +
751 ( s[offset + 2] << 8);
754 else if (size == 64) {
755 if (ckWARN(WARN_PORTABLE))
756 Perl_warner(aTHX_ WARN_PORTABLE,
757 "Bit vector size > 32 non-portable");
758 if (offset >= srclen)
760 else if (offset + 1 >= srclen)
762 (UV) s[offset ] << 56;
763 else if (offset + 2 >= srclen)
765 ((UV) s[offset ] << 56) +
766 ((UV) s[offset + 1] << 48);
767 else if (offset + 3 >= srclen)
769 ((UV) s[offset ] << 56) +
770 ((UV) s[offset + 1] << 48) +
771 ((UV) s[offset + 2] << 40);
772 else if (offset + 4 >= srclen)
774 ((UV) s[offset ] << 56) +
775 ((UV) s[offset + 1] << 48) +
776 ((UV) s[offset + 2] << 40) +
777 ((UV) s[offset + 3] << 32);
778 else if (offset + 5 >= srclen)
780 ((UV) s[offset ] << 56) +
781 ((UV) s[offset + 1] << 48) +
782 ((UV) s[offset + 2] << 40) +
783 ((UV) s[offset + 3] << 32) +
784 ( s[offset + 4] << 24);
785 else if (offset + 6 >= srclen)
787 ((UV) s[offset ] << 56) +
788 ((UV) s[offset + 1] << 48) +
789 ((UV) s[offset + 2] << 40) +
790 ((UV) s[offset + 3] << 32) +
791 ((UV) s[offset + 4] << 24) +
792 ((UV) s[offset + 5] << 16);
795 ((UV) s[offset ] << 56) +
796 ((UV) s[offset + 1] << 48) +
797 ((UV) s[offset + 2] << 40) +
798 ((UV) s[offset + 3] << 32) +
799 ((UV) s[offset + 4] << 24) +
800 ((UV) s[offset + 5] << 16) +
801 ( s[offset + 6] << 8);
807 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
809 offset >>= 3; /* turn into byte offset */
814 ((UV) s[offset] << 8) +
818 ((UV) s[offset ] << 24) +
819 ((UV) s[offset + 1] << 16) +
820 ( s[offset + 2] << 8) +
823 else if (size == 64) {
824 if (ckWARN(WARN_PORTABLE))
825 Perl_warner(aTHX_ WARN_PORTABLE,
826 "Bit vector size > 32 non-portable");
828 ((UV) s[offset ] << 56) +
829 ((UV) s[offset + 1] << 48) +
830 ((UV) s[offset + 2] << 40) +
831 ((UV) s[offset + 3] << 32) +
832 ((UV) s[offset + 4] << 24) +
833 ((UV) s[offset + 5] << 16) +
834 ( s[offset + 6] << 8) +
843 /* currently converts input to bytes if possible but doesn't sweat failures,
844 * although it does ensure that the string it clobbers is not marked as
845 * utf8-valid any more
848 Perl_do_vecset(pTHX_ SV *sv)
850 SV *targ = LvTARG(sv);
853 register unsigned char *s;
861 s = (unsigned char*)SvPV_force(targ, targlen);
863 /* This is handled by the SvPOK_only below...
864 if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE))
867 (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE);
870 (void)SvPOK_only(targ);
872 offset = LvTARGOFF(sv);
874 Perl_croak(aTHX_ "Assigning to negative offset in vec");
875 size = LvTARGLEN(sv);
876 if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */
877 Perl_croak(aTHX_ "Illegal number of bits in vec");
879 offset *= size; /* turn into bit offset */
880 len = (offset + size + 7) / 8; /* required number of bytes */
882 s = (unsigned char*)SvGROW(targ, len + 1);
883 (void)memzero((char *)(s + targlen), len - targlen + 1);
884 SvCUR_set(targ, len);
888 mask = (1 << size) - 1;
891 offset >>= 3; /* turn into byte offset */
892 s[offset] &= ~(mask << size);
893 s[offset] |= lval << size;
896 offset >>= 3; /* turn into byte offset */
898 s[offset ] = lval & 0xff;
899 else if (size == 16) {
900 s[offset ] = (lval >> 8) & 0xff;
901 s[offset+1] = lval & 0xff;
903 else if (size == 32) {
904 s[offset ] = (lval >> 24) & 0xff;
905 s[offset+1] = (lval >> 16) & 0xff;
906 s[offset+2] = (lval >> 8) & 0xff;
907 s[offset+3] = lval & 0xff;
910 else if (size == 64) {
911 if (ckWARN(WARN_PORTABLE))
912 Perl_warner(aTHX_ WARN_PORTABLE,
913 "Bit vector size > 32 non-portable");
914 s[offset ] = (lval >> 56) & 0xff;
915 s[offset+1] = (lval >> 48) & 0xff;
916 s[offset+2] = (lval >> 40) & 0xff;
917 s[offset+3] = (lval >> 32) & 0xff;
918 s[offset+4] = (lval >> 24) & 0xff;
919 s[offset+5] = (lval >> 16) & 0xff;
920 s[offset+6] = (lval >> 8) & 0xff;
921 s[offset+7] = lval & 0xff;
929 Perl_do_chop(pTHX_ register SV *astr, register SV *sv)
934 if (SvTYPE(sv) == SVt_PVAV) {
939 for (i = 0; i <= max; i++) {
940 sv = (SV*)av_fetch(av, i, FALSE);
941 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
946 else if (SvTYPE(sv) == SVt_PVHV) {
949 (void)hv_iterinit(hv);
951 while ((entry = hv_iternext(hv)))
952 do_chop(astr,hv_iterval(hv,entry));
955 else if (SvREADONLY(sv))
956 Perl_croak(aTHX_ PL_no_modify);
958 if (len && !SvPOK(sv))
959 s = SvPV_force(sv, len);
962 char *send = s + len;
965 while (s > start && UTF8_IS_CONTINUATION(*s))
967 if (utf8_to_uvchr((U8*)s, 0)) {
968 sv_setpvn(astr, s, send - s);
970 SvCUR_set(sv, s - start);
976 sv_setpvn(astr, "", 0);
980 sv_setpvn(astr, s, 1);
987 sv_setpvn(astr, "", 0);
992 Perl_do_chomp(pTHX_ register SV *sv)
1001 if (RsRECORD(PL_rs))
1004 if (SvTYPE(sv) == SVt_PVAV) {
1009 for (i = 0; i <= max; i++) {
1010 sv = (SV*)av_fetch(av, i, FALSE);
1011 if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef))
1012 count += do_chomp(sv);
1016 else if (SvTYPE(sv) == SVt_PVHV) {
1019 (void)hv_iterinit(hv);
1021 while ((entry = hv_iternext(hv)))
1022 count += do_chomp(hv_iterval(hv,entry));
1025 else if (SvREADONLY(sv))
1026 Perl_croak(aTHX_ PL_no_modify);
1030 if (RsPARA(PL_rs)) {
1034 while (len && s[-1] == '\n') {
1042 char *rsptr = SvPV(PL_rs, rslen);
1049 if (len < rslen - 1)
1053 if (memNE(s, rsptr, rslen))
1058 s = SvPV_force(sv, n_a);
1069 Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right)
1085 bool left_utf = DO_UTF8(left);
1086 bool right_utf = DO_UTF8(right);
1089 if (left_utf && !right_utf)
1090 sv_utf8_upgrade(right);
1091 else if (!left_utf && right_utf)
1092 sv_utf8_upgrade(left);
1094 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
1095 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
1096 lsave = lc = SvPV(left, leftlen);
1097 rsave = rc = SvPV(right, rightlen);
1098 len = leftlen < rightlen ? leftlen : rightlen;
1100 if ((left_utf || right_utf) && (sv == left || sv == right)) {
1101 needlen = optype == OP_BIT_AND ? len : leftlen + rightlen;
1102 Newz(801, dc, needlen + 1, char);
1104 else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
1106 dc = SvPV_force(sv, n_a);
1107 if (SvCUR(sv) < len) {
1108 dc = SvGROW(sv, len + 1);
1109 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
1111 if (optype != OP_BIT_AND && (left_utf || right_utf))
1112 dc = SvGROW(sv, leftlen + rightlen + 1);
1115 needlen = ((optype == OP_BIT_AND)
1116 ? len : (leftlen > rightlen ? leftlen : rightlen));
1117 Newz(801, dc, needlen + 1, char);
1118 (void)sv_usepvn(sv, dc, needlen);
1119 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
1122 (void)SvPOK_only(sv);
1123 if (left_utf || right_utf) {
1126 STRLEN lulen = leftlen;
1127 STRLEN rulen = rightlen;
1132 while (lulen && rulen) {
1133 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1136 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1140 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1142 if (sv == left || sv == right)
1143 (void)sv_usepvn(sv, dcsave, needlen);
1144 SvCUR_set(sv, dc - dcsave);
1147 while (lulen && rulen) {
1148 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1151 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1155 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1159 while (lulen && rulen) {
1160 luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV);
1163 ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV);
1167 dc = (char*)uvchr_to_utf8((U8*)dc, duc);
1170 if (sv == left || sv == right)
1171 (void)sv_usepvn(sv, dcsave, needlen);
1172 SvCUR_set(sv, dc - dcsave);
1174 sv_catpvn(sv, rc, rulen);
1176 sv_catpvn(sv, lc, lulen);
1186 if (len >= sizeof(long)*4 &&
1187 !((long)dc % sizeof(long)) &&
1188 !((long)lc % sizeof(long)) &&
1189 !((long)rc % sizeof(long))) /* It's almost always aligned... */
1191 I32 remainder = len % (sizeof(long)*4);
1192 len /= (sizeof(long)*4);
1201 *dl++ = *ll++ & *rl++;
1202 *dl++ = *ll++ & *rl++;
1203 *dl++ = *ll++ & *rl++;
1204 *dl++ = *ll++ & *rl++;
1209 *dl++ = *ll++ ^ *rl++;
1210 *dl++ = *ll++ ^ *rl++;
1211 *dl++ = *ll++ ^ *rl++;
1212 *dl++ = *ll++ ^ *rl++;
1217 *dl++ = *ll++ | *rl++;
1218 *dl++ = *ll++ | *rl++;
1219 *dl++ = *ll++ | *rl++;
1220 *dl++ = *ll++ | *rl++;
1235 *dc++ = *lc++ & *rc++;
1239 *dc++ = *lc++ ^ *rc++;
1243 *dc++ = *lc++ | *rc++;
1247 sv_catpvn(sv, rsave + len, rightlen - len);
1248 else if (leftlen > len)
1249 sv_catpvn(sv, lsave + len, leftlen - len);
1267 I32 gimme = GIMME_V;
1268 I32 dokeys = (PL_op->op_type == OP_KEYS);
1269 I32 dovalues = (PL_op->op_type == OP_VALUES);
1270 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
1272 if (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV)
1273 dokeys = dovalues = TRUE;
1276 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1277 dTARGET; /* make sure to clear its target here */
1278 if (SvTYPE(TARG) == SVt_PVLV)
1279 LvTARG(TARG) = Nullsv;
1285 keys = realhv ? hv : avhv_keys((AV*)hv);
1286 (void)hv_iterinit(keys); /* always reset iterator regardless */
1288 if (gimme == G_VOID)
1291 if (gimme == G_SCALAR) {
1295 if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */
1296 if (SvTYPE(TARG) < SVt_PVLV) {
1297 sv_upgrade(TARG, SVt_PVLV);
1298 sv_magic(TARG, Nullsv, PERL_MAGIC_nkeys, Nullch, 0);
1301 if (LvTARG(TARG) != (SV*)keys) {
1303 SvREFCNT_dec(LvTARG(TARG));
1304 LvTARG(TARG) = SvREFCNT_inc(keys);
1310 if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied))
1315 while (hv_iternext(keys)) i++;
1321 EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues));
1323 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
1324 while ((entry = hv_iternext(keys))) {
1327 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
1331 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry);
1332 DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu",
1333 (unsigned long)HeHASH(entry),
1335 (unsigned long)(HeHASH(entry) & HvMAX(keys))));