1 #define PERL_NO_GET_CONTEXT
6 # define NEED_my_snprintf
7 # define NEED_sv_2pv_flags
12 # define DD_USE_OLD_ID_FORMAT
17 # define strlcpy(d,s,l) my_strlcpy(d,s,l)
19 # define strlcpy(d,s,l) strcpy(d,s)
23 /* These definitions are ASCII only. But the pure-perl .pm avoids
24 * calling this .xs file for releases where they aren't defined */
27 # define isASCII(c) (((UV) (c)) < 128)
30 #ifndef ESC_NATIVE /* \e */
31 # define ESC_NATIVE 27
35 # define isPRINT(c) (((UV) (c)) >= ' ' && ((UV) (c)) < 127)
39 # define isALPHA(c) ( (((UV) (c)) >= 'a' && ((UV) (c)) <= 'z') \
40 || (((UV) (c)) <= 'Z' && ((UV) (c)) >= 'A'))
44 # define isIDFIRST(c) (isALPHA(c) || (c) == '_')
48 # define isWORDCHAR(c) (isIDFIRST(c) \
49 || (((UV) (c)) >= '0' && ((UV) (c)) <= '9'))
52 /* SvPVCLEAR only from perl 5.25.6 */
54 # define SvPVCLEAR(sv) sv_setpvs((sv), "")
58 # define memBEGINs(s1, l, s2) \
59 ( (l) >= sizeof(s2) - 1 \
60 && memEQ(s1, "" s2 "", sizeof(s2)-1))
63 /* This struct contains almost all the user's desired configuration, and it
64 * is treated as constant by the recursive function. This arrangement has
65 * the advantage of needing less memory than passing all of them on the
66 * stack all the time (as was the case in an earlier implementation). */
83 int use_sparse_seen_hash;
88 static STRLEN num_q (const char *s, STRLEN slen);
89 static STRLEN esc_q (char *dest, const char *src, STRLEN slen);
90 static STRLEN esc_q_utf8 (pTHX_ SV *sv, const char *src, STRLEN slen, I32 do_utf8, I32 useqq);
91 static bool globname_needs_quote(const char *s, STRLEN len);
92 static bool key_needs_quote(const char *s, STRLEN len);
93 static bool safe_decimal_number(const char *p, STRLEN len);
94 static SV *sv_x (pTHX_ SV *sv, const char *str, STRLEN len, I32 n);
95 static I32 DD_dump (pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval,
96 HV *seenhv, AV *postav, const I32 level, SV *apad,
100 #define HvNAME_get HvNAME
103 /* Perls 7 through portions of 15 used utf8_to_uvchr() which didn't have a
104 * length parameter. This wrongly allowed reading beyond the end of buffer
105 * given malformed input */
107 #if PERL_VERSION <= 6 /* Perl 5.6 and earlier */
110 Perl_utf8_to_uvchr_buf(pTHX_ U8 *s, U8 *send, STRLEN *retlen)
112 const UV uv = utf8_to_uv(s, send - s, retlen,
113 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
114 return UNI_TO_NATIVE(uv);
117 # if !defined(PERL_IMPLICIT_CONTEXT)
118 # define utf8_to_uvchr_buf Perl_utf8_to_uvchr_buf
120 # define utf8_to_uvchr_buf(a,b,c) Perl_utf8_to_uvchr_buf(aTHX_ a,b,c)
123 #endif /* PERL_VERSION <= 6 */
125 /* Perl 5.7 through part of 5.15 */
126 #if PERL_VERSION > 6 && PERL_VERSION <= 15 && ! defined(utf8_to_uvchr_buf)
129 Perl_utf8_to_uvchr_buf(pTHX_ U8 *s, U8 *send, STRLEN *retlen)
131 /* We have to discard <send> for these versions; hence can read off the
132 * end of the buffer if there is a malformation that indicates the
133 * character is longer than the space available */
135 return utf8_to_uvchr(s, retlen);
138 # if !defined(PERL_IMPLICIT_CONTEXT)
139 # define utf8_to_uvchr_buf Perl_utf8_to_uvchr_buf
141 # define utf8_to_uvchr_buf(a,b,c) Perl_utf8_to_uvchr_buf(aTHX_ a,b,c)
144 #endif /* PERL_VERSION > 6 && <= 15 */
146 /* Changes in 5.7 series mean that now IOK is only set if scalar is
147 precisely integer but in 5.6 and earlier we need to do a more
149 #if PERL_VERSION <= 6
150 #define DD_is_integer(sv) (SvIOK(sv) && (SvIsUV(val) ? SvUV(sv) == SvNV(sv) : SvIV(sv) == SvNV(sv)))
152 #define DD_is_integer(sv) SvIOK(sv)
155 /* does a glob name need to be protected? */
157 globname_needs_quote(const char *s, STRLEN len)
159 const char *send = s+len;
171 if (!isWORDCHAR(*s)) {
184 /* does a hash key need to be quoted (to the left of => ).
185 Previously this used (globname_)needs_quote() which accepted strings
186 like '::foo', but these aren't safe as unquoted keys under strict.
189 key_needs_quote(const char *s, STRLEN len) {
190 const char *send = s+len;
192 if (safe_decimal_number(s, len)) {
195 else if (isIDFIRST(*s)) {
206 /* Check that the SV can be represented as a simple decimal integer.
208 * The perl code does this by matching against /^(?:0|-?[1-9]\d{0,8})\z/
211 safe_decimal_number(const char *p, STRLEN len) {
212 if (len == 1 && *p == '0')
215 if (len && *p == '-') {
220 if (len == 0 || *p < '1' || *p > '9')
230 /* the perl code checks /\d/ but we don't want unicode digits here */
231 if (*p < '0' || *p > '9')
239 /* count the number of "'"s and "\"s in string */
241 num_q(const char *s, STRLEN slen)
246 if (*s == '\'' || *s == '\\')
255 /* returns number of chars added to escape "'"s and "\"s in s */
256 /* slen number of characters in s will be escaped */
257 /* destination must be long enough for additional chars */
259 esc_q(char *d, const char *s, STRLEN slen)
279 /* this function is also misused for implementing $Useqq */
281 esc_q_utf8(pTHX_ SV* sv, const char *src, STRLEN slen, I32 do_utf8, I32 useqq)
285 const char * const send = src + slen;
286 STRLEN j, cur = SvCUR(sv);
287 /* Could count 128-255 and 256+ in two variables, if we want to
288 be like &qquote and make a distinction. */
289 STRLEN grow = 0; /* bytes needed to represent chars 128+ */
290 /* STRLEN topbit_grow = 0; bytes needed to represent chars 128-255 */
291 STRLEN backslashes = 0;
292 STRLEN single_quotes = 0;
293 STRLEN qq_escapables = 0; /* " $ @ will need a \ in "" strings. */
297 for (s = src; s < send; s += increment) { /* Sizing pass */
300 increment = 1; /* Will override if necessary for utf-8 */
305 } else if (k == '\'') {
307 } else if (k == '"' || k == '$' || k == '@') {
313 else if (! isASCII(k) && k > ' ') {
314 /* High ordinal non-printable code point. (The test that k is
315 * above SPACE should be optimized out by the compiler on
316 * non-EBCDIC platforms; otherwise we could put an #ifdef around
317 * it, but it's better to have just a single code path when
318 * possible. All but one of the non-ASCII EBCDIC controls are low
319 * ordinal; that one is the only one above SPACE.)
321 * If UTF-8, output as hex, regardless of useqq. This means there
322 * is an overhead of 4 chars '\x{}'. Then count the number of hex
325 k = utf8_to_uvchr_buf((U8*)s, (U8*) send, NULL);
327 /* treat invalid utf8 byte by byte. This loop iteration gets the
329 increment = (k == 0 && *s != '\0') ? 1 : UTF8SKIP(s);
331 grow += 4 + (k <= 0xFF ? 2 : k <= 0xFFF ? 3 : k <= 0xFFFF ? 4 :
333 8 /* We may allocate a bit more than the minimum here. */
335 k <= 0xFFFFFFFF ? 8 : UVSIZE * 4
339 else if (useqq) { /* Not utf8, must be <= 0xFF, hence 2 hex
343 else { /* Non-qq generates 3 octal digits plus backslash */
346 } /* End of high-ordinal non-printable */
347 else if (! useqq) { /* Low ordinal, non-printable, non-qq just
348 * outputs the raw char */
351 else { /* Is qq, low ordinal, non-printable. Output escape
353 if ( k == '\a' || k == '\b' || k == '\t' || k == '\n' || k == '\r'
354 || k == '\f' || k == ESC_NATIVE)
356 grow += 2; /* 1 char plus backslash */
358 else /* The other low ordinals are output as an octal escape
360 if (s + 1 >= send || ( *(U8*)(s+1) >= '0'
361 && *(U8*)(s+1) <= '9'))
363 /* When the following character is a digit, use 3 octal digits
364 * plus backslash, as using fewer digits would concatenate the
365 * following char into this one */
369 grow += 2; /* 1 octal digit, plus backslash */
372 grow += 3; /* 2 octal digits plus backslash */
375 grow += 4; /* 3 octal digits plus backslash */
378 } /* End of size-calculating loop */
381 /* We have something needing hex. 3 is ""\0 */
382 sv_grow(sv, cur + 3 + grow + 2*backslashes + single_quotes
383 + 2*qq_escapables + normal);
384 rstart = r = SvPVX(sv) + cur;
388 for (s = src; s < send; s += increment) {
393 /* Exclude non-ASCII low ordinal controls. This should be
394 * optimized out by the compiler on ASCII platforms; if not
395 * could wrap it in a #ifdef EBCDIC, but better to avoid
396 * #if's if possible */
400 /* When in UTF-8, we output all non-ascii chars as \x{}
401 * reqardless of useqq, except for the low ordinal controls on
402 * EBCDIC platforms */
403 k = utf8_to_uvchr_buf((U8*)s, (U8*) send, NULL);
405 /* treat invalid utf8 byte by byte. This loop iteration gets the
407 increment = (k == 0 && *s != '\0') ? 1 : UTF8SKIP(s);
409 #if PERL_VERSION < 10
410 sprintf(r, "\\x{%" UVxf "}", k);
412 /* my_sprintf is not supported by ppport.h */
414 r = r + my_sprintf(r, "\\x{%" UVxf "}", k);
419 /* Here 1) isn't UTF-8; or
420 * 2) the current character is ASCII; or
421 * 3) it is an EBCDIC platform and is a low ordinal
423 * In each case the character occupies just one byte */
428 /* These need a backslash escape */
429 if (k == '"' || k == '\\' || k == '$' || k == '@') {
435 else if (! useqq) { /* non-qq, non-printable, low-ordinal is
439 else { /* Is qq means use escape sequences */
444 case '\a': *r++ = 'a'; break;
445 case '\b': *r++ = 'b'; break;
446 case '\t': *r++ = 't'; break;
447 case '\n': *r++ = 'n'; break;
448 case '\f': *r++ = 'f'; break;
449 case '\r': *r++ = 'r'; break;
450 case ESC_NATIVE: *r++ = 'e'; break;
453 /* only ASCII digits matter here, which are invariant,
454 * since we only encode characters \377 and under, or
455 * \x177 and under for a unicode string
457 next_is_digit = (s + 1 >= send )
459 : (*(U8*)(s+1) >= '0' && *(U8*)(s+1) <= '9');
462 * r = r + my_sprintf(r, "%o", k);
464 if (k <= 7 && !next_is_digit) {
465 *r++ = (char)k + '0';
466 } else if (k <= 63 && !next_is_digit) {
467 *r++ = (char)(k>>3) + '0';
468 *r++ = (char)(k&7) + '0';
470 *r++ = (char)(k>>6) + '0';
471 *r++ = (char)((k&63)>>3) + '0';
472 *r++ = (char)(k&7) + '0';
480 sv_grow(sv, cur + 3 + 2*backslashes + 2*single_quotes
481 + qq_escapables + normal);
482 rstart = r = SvPVX(sv) + cur;
484 for (s = src; s < send; s ++) {
486 if (k == '\'' || k == '\\')
494 SvCUR_set(sv, cur + j);
499 /* append a repeated string to an SV */
501 sv_x(pTHX_ SV *sv, const char *str, STRLEN len, I32 n)
507 assert(SvTYPE(sv) >= SVt_PV);
511 SvGROW(sv, len*n + SvCUR(sv) + 1);
513 char * const start = SvPVX(sv) + SvCUR(sv);
514 SvCUR_set(sv, SvCUR(sv) + n);
521 sv_catpvn(sv, str, len);
529 deparsed_output(pTHX_ SV *val)
535 /* This is passed to load_module(), which decrements its ref count and
536 * modifies it (so we also can't reuse it below) */
537 SV *pkg = newSVpvs("B::Deparse");
539 load_module(PERL_LOADMOD_NOIMPORT, pkg, 0);
544 mXPUSHs(newSVpvs("B::Deparse"));
547 n = call_method("new", G_SCALAR);
551 croak("B::Deparse->new returned %d items, but expected exactly 1", n);
558 n = call_method("coderef2text", G_SCALAR);
562 croak("$b_deparse->coderef2text returned %d items, but expected exactly 1", n);
566 SvREFCNT_inc(text); /* the caller will mortalise this */
576 * This ought to be split into smaller functions. (it is one long function since
577 * it exactly parallels the perl version, which was one long thing for
578 * efficiency raisins.) Ugggh!
581 DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
582 AV *postav, const I32 level, SV *apad, const Style *style)
586 char *c, *r, *realpack;
587 #ifdef DD_USE_OLD_ID_FORMAT
591 char *const id = (char *)&id_buffer;
594 SV *sv, *ipad, *ival;
595 SV *blesspad = Nullsv;
596 AV *seenentry = NULL;
598 STRLEN inamelen, idlen = 0;
600 bool no_bless = 0; /* when a qr// is blessed into Regexp we dont want to bless it.
601 in later perls we should actually check the classname of the
602 engine. this gets tricky as it involves lexical issues that arent so
604 bool is_regex = 0; /* we are dumping a regex, we need to know this before we bless */
609 /* If the output buffer has less than some arbitrary amount of space
610 remaining, then enlarge it. For the test case (25M of output),
611 *1.1 was slower, *2.0 was the same, so the first guess of 1.5 is
612 deemed to be good enough. */
613 if (SvTYPE(retval) >= SVt_PV && (SvLEN(retval) - SvCUR(retval)) < 42) {
614 sv_grow(retval, SvCUR(retval) * 3 / 2);
617 realtype = SvTYPE(val);
623 /* If a freeze method is provided and the object has it, call
624 it. Warn on errors. */
625 if (SvOBJECT(SvRV(val)) && style->freezer &&
626 SvPOK(style->freezer) && SvCUR(style->freezer) &&
627 gv_fetchmeth(SvSTASH(SvRV(val)), SvPVX_const(style->freezer),
628 SvCUR(style->freezer), -1) != NULL)
630 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
631 XPUSHs(val); PUTBACK;
632 i = perl_call_method(SvPVX_const(style->freezer), G_EVAL|G_VOID|G_DISCARD);
635 warn("WARNING(Freezer method call failed): %" SVf, ERRSV);
636 PUTBACK; FREETMPS; LEAVE;
640 realtype = SvTYPE(ival);
641 #ifdef DD_USE_OLD_ID_FORMAT
642 idlen = my_snprintf(id, sizeof(id), "0x%" UVxf, PTR2UV(ival));
644 id_buffer = PTR2UV(ival);
645 idlen = sizeof(id_buffer);
648 realpack = HvNAME_get(SvSTASH(ival));
652 /* if it has a name, we need to either look it up, or keep a tab
653 * on it so we know when we hit it later
656 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
657 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
660 if ((svp = av_fetch(seenentry, 0, FALSE))
661 && (othername = *svp))
663 if (style->purity && level > 0) {
666 if (realtype == SVt_PVHV)
667 sv_catpvs(retval, "{}");
668 else if (realtype == SVt_PVAV)
669 sv_catpvs(retval, "[]");
671 sv_catpvs(retval, "do{my $o}");
672 postentry = newSVpvn(name, namelen);
673 sv_catpvs(postentry, " = ");
674 sv_catsv(postentry, othername);
675 av_push(postav, postentry);
678 if (name[0] == '@' || name[0] == '%') {
679 if ((SvPVX_const(othername))[0] == '\\' &&
680 (SvPVX_const(othername))[1] == name[0]) {
681 sv_catpvn(retval, SvPVX_const(othername)+1,
685 sv_catpvn(retval, name, 1);
686 sv_catpvs(retval, "{");
687 sv_catsv(retval, othername);
688 sv_catpvs(retval, "}");
692 sv_catsv(retval, othername);
697 #ifdef DD_USE_OLD_ID_FORMAT
698 warn("ref name not found for %s", id);
700 warn("ref name not found for 0x%" UVxf, PTR2UV(ival));
705 else { /* store our name and continue */
707 if (name[0] == '@' || name[0] == '%') {
708 namesv = newSVpvs("\\");
709 sv_catpvn(namesv, name, namelen);
711 else if (realtype == SVt_PVCV && name[0] == '*') {
712 namesv = newSVpvs("\\");
713 sv_catpvn(namesv, name, namelen);
714 (SvPVX(namesv))[1] = '&';
717 namesv = newSVpvn(name, namelen);
719 av_push(seenentry, namesv);
720 (void)SvREFCNT_inc(val);
721 av_push(seenentry, val);
722 (void)hv_store(seenhv, id, idlen,
723 newRV_inc((SV*)seenentry), 0);
724 SvREFCNT_dec(seenentry);
727 /* regexps dont have to be blessed into package "Regexp"
728 * they can be blessed into any package.
731 if (realpack && *realpack == 'R' && strEQ(realpack, "Regexp"))
732 #elif PERL_VERSION < 11
733 if (realpack && realtype == SVt_PVMG && mg_find(ival, PERL_MAGIC_qr))
735 if (realpack && realtype == SVt_REGEXP)
739 if (strEQ(realpack, "Regexp"))
745 /* If purity is not set and maxdepth is set, then check depth:
746 * if we have reached maximum depth, return the string
747 * representation of the thing we are currently examining
748 * at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)').
750 if (!style->purity && style->maxdepth > 0 && level >= style->maxdepth) {
752 const char * const valstr = SvPV(val,vallen);
753 sv_catpvs(retval, "'");
754 sv_catpvn(retval, valstr, vallen);
755 sv_catpvs(retval, "'");
759 if (style->maxrecurse > 0 && level >= style->maxrecurse) {
760 croak("Recursion limit of %" IVdf " exceeded", style->maxrecurse);
763 if (realpack && !no_bless) { /* we have a blessed ref */
765 const char * const blessstr = SvPV(style->bless, blesslen);
766 sv_catpvn(retval, blessstr, blesslen);
767 sv_catpvs(retval, "( ");
768 if (style->indent >= 2) {
770 apad = newSVsv(apad);
771 sv_x(aTHX_ apad, " ", 1, blesslen+2);
775 ipad = sv_x(aTHX_ Nullsv, SvPVX_const(style->xpad), SvCUR(style->xpad), level+1);
780 SV *sv_pattern = NULL;
787 if ((re_pattern_cv = get_cv("re::regexp_pattern", 0))) {
795 count = call_sv((SV*)re_pattern_cv, G_ARRAY);
800 SvREFCNT_inc(sv_flags);
801 SvREFCNT_inc(sv_pattern);
807 sv_2mortal(sv_pattern);
808 sv_2mortal(sv_flags);
815 rval = SvPV(sv_pattern, rlen);
818 sv_catpvs(retval, "qr/");
819 for (;slash < rend; slash++) {
820 if (*slash == '\\') { ++slash; continue; }
822 sv_catpvn(retval, rval, slash-rval);
823 sv_catpvs(retval, "\\/");
824 rlen -= slash-rval+1;
828 sv_catpvn(retval, rval, rlen);
829 sv_catpvs(retval, "/");
831 sv_catsv(retval, sv_flags);
840 SV * const namesv = newSVpvs("${");
841 sv_catpvn(namesv, name, namelen);
842 sv_catpvs(namesv, "}");
843 if (realpack) { /* blessed */
844 sv_catpvs(retval, "do{\\(my $o = ");
845 DD_dump(aTHX_ ival, SvPVX_const(namesv), SvCUR(namesv), retval, seenhv,
846 postav, level+1, apad, style);
847 sv_catpvs(retval, ")}");
850 sv_catpvs(retval, "\\");
851 DD_dump(aTHX_ ival, SvPVX_const(namesv), SvCUR(namesv), retval, seenhv,
852 postav, level+1, apad, style);
854 SvREFCNT_dec(namesv);
856 else if (realtype == SVt_PVGV) { /* glob ref */
857 SV * const namesv = newSVpvs("*{");
858 sv_catpvn(namesv, name, namelen);
859 sv_catpvs(namesv, "}");
860 sv_catpvs(retval, "\\");
861 DD_dump(aTHX_ ival, SvPVX_const(namesv), SvCUR(namesv), retval, seenhv,
862 postav, level+1, apad, style);
863 SvREFCNT_dec(namesv);
865 else if (realtype == SVt_PVAV) {
868 const SSize_t ixmax = av_len((AV *)ival);
870 SV * const ixsv = newSViv(0);
871 /* allowing for a 24 char wide array index */
872 New(0, iname, namelen+28, char);
873 (void) strlcpy(iname, name, namelen+28);
875 if (name[0] == '@') {
876 sv_catpvs(retval, "(");
880 sv_catpvs(retval, "[");
881 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
883 && name[namelen-1] != ']' && name[namelen-1] != '}'
884 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
886 && name[namelen-1] != ']' && name[namelen-1] != '}')
889 || (name[0] == '\\' && name[2] == '{'))))
891 iname[inamelen++] = '-'; iname[inamelen++] = '>';
892 iname[inamelen] = '\0';
895 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
896 (instr(iname+inamelen-8, "{SCALAR}") ||
897 instr(iname+inamelen-7, "{ARRAY}") ||
898 instr(iname+inamelen-6, "{HASH}"))) {
899 iname[inamelen++] = '-'; iname[inamelen++] = '>';
901 iname[inamelen++] = '['; iname[inamelen] = '\0';
902 totpad = newSVsv(style->sep);
903 sv_catsv(totpad, style->pad);
904 sv_catsv(totpad, apad);
906 for (ix = 0; ix <= ixmax; ++ix) {
909 svp = av_fetch((AV*)ival, ix, FALSE);
917 #if PERL_VERSION < 10
918 (void) sprintf(iname+ilen, "%" IVdf, (IV)ix);
919 ilen = strlen(iname);
921 ilen = ilen + my_sprintf(iname+ilen, "%" IVdf, (IV)ix);
923 iname[ilen++] = ']'; iname[ilen] = '\0';
924 if (style->indent >= 3) {
925 sv_catsv(retval, totpad);
926 sv_catsv(retval, ipad);
927 sv_catpvs(retval, "#");
928 sv_catsv(retval, ixsv);
930 sv_catsv(retval, totpad);
931 sv_catsv(retval, ipad);
932 DD_dump(aTHX_ elem, iname, ilen, retval, seenhv, postav,
933 level+1, apad, style);
934 if (ix < ixmax || (style->trailingcomma && style->indent >= 1))
935 sv_catpvs(retval, ",");
938 SV * const opad = sv_x(aTHX_ Nullsv, SvPVX_const(style->xpad), SvCUR(style->xpad), level);
939 sv_catsv(retval, totpad);
940 sv_catsv(retval, opad);
944 sv_catpvs(retval, ")");
946 sv_catpvs(retval, "]");
948 SvREFCNT_dec(totpad);
951 else if (realtype == SVt_PVHV) {
952 SV *totpad, *newapad;
959 SV * const iname = newSVpvn(name, namelen);
960 if (name[0] == '%') {
961 sv_catpvs(retval, "(");
962 (SvPVX(iname))[0] = '$';
965 sv_catpvs(retval, "{");
966 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
968 && name[namelen-1] != ']' && name[namelen-1] != '}')
971 || (name[0] == '\\' && name[2] == '{'))))
973 sv_catpvs(iname, "->");
976 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
977 (instr(name+namelen-8, "{SCALAR}") ||
978 instr(name+namelen-7, "{ARRAY}") ||
979 instr(name+namelen-6, "{HASH}"))) {
980 sv_catpvs(iname, "->");
982 sv_catpvs(iname, "{");
983 totpad = newSVsv(style->sep);
984 sv_catsv(totpad, style->pad);
985 sv_catsv(totpad, apad);
987 /* If requested, get a sorted/filtered array of hash keys */
988 if (style->sortkeys) {
989 #if PERL_VERSION >= 8
990 if (style->sortkeys == &PL_sv_yes) {
992 (void)hv_iterinit((HV*)ival);
993 while ((entry = hv_iternext((HV*)ival))) {
994 sv = hv_iterkeysv(entry);
995 (void)SvREFCNT_inc(sv);
998 # ifdef USE_LOCALE_COLLATE
999 # ifdef IN_LC /* Use this if available */
1000 if (IN_LC(LC_COLLATE))
1005 sortsv(AvARRAY(keys),
1007 Perl_sv_cmp_locale);
1012 sortsv(AvARRAY(keys),
1020 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
1021 XPUSHs(sv_2mortal(newRV_inc(ival))); PUTBACK;
1022 i = perl_call_sv(style->sortkeys, G_SCALAR | G_EVAL);
1026 if (SvROK(sv) && (SvTYPE(SvRV(sv)) == SVt_PVAV))
1027 keys = (AV*)SvREFCNT_inc(SvRV(sv));
1030 warn("Sortkeys subroutine did not return ARRAYREF\n");
1031 PUTBACK; FREETMPS; LEAVE;
1034 sv_2mortal((SV*)keys);
1037 (void)hv_iterinit((HV*)ival);
1039 /* foreach (keys %hash) */
1040 for (i = 0; 1; i++) {
1042 char *nkey_buffer = NULL;
1048 bool do_utf8 = FALSE;
1050 if (style->sortkeys) {
1051 if (!(keys && (SSize_t)i <= av_len(keys))) break;
1053 if (!(entry = hv_iternext((HV *)ival))) break;
1057 sv_catpvs(retval, ",");
1059 if (style->sortkeys) {
1061 svp = av_fetch(keys, i, FALSE);
1062 keysv = svp ? *svp : sv_newmortal();
1063 key = SvPV(keysv, keylen);
1064 svp = hv_fetch((HV*)ival, key,
1065 SvUTF8(keysv) ? -(I32)keylen : (I32)keylen, 0);
1066 hval = svp ? *svp : sv_newmortal();
1069 keysv = hv_iterkeysv(entry);
1070 hval = hv_iterval((HV*)ival, entry);
1073 key = SvPV(keysv, keylen);
1074 do_utf8 = DO_UTF8(keysv);
1077 sv_catsv(retval, totpad);
1078 sv_catsv(retval, ipad);
1080 old logic was first to check utf8 flag, and if utf8 always
1081 call esc_q_utf8. This caused test to break under -Mutf8,
1082 because there even strings like 'c' have utf8 flag on.
1083 Hence with quotekeys == 0 the XS code would still '' quote
1084 them based on flags, whereas the perl code would not,
1087 The old logic checked that the string was a valid
1088 perl glob name (foo::bar), which isn't safe under
1089 strict, and differs from the perl code which only
1090 accepts simple identifiers.
1092 With the fix for [perl #120384] I chose to make
1093 their handling of key quoting compatible between XS
1096 if (style->quotekeys || key_needs_quote(key,keylen)) {
1097 if (do_utf8 || style->useqq) {
1098 STRLEN ocur = SvCUR(retval);
1099 klen = nlen = esc_q_utf8(aTHX_ retval, key, klen, do_utf8, style->useqq);
1100 nkey = SvPVX(retval) + ocur;
1103 nticks = num_q(key, klen);
1104 New(0, nkey_buffer, klen+nticks+3, char);
1108 klen += esc_q(nkey+1, key, klen);
1110 (void)Copy(key, nkey+1, klen, char);
1111 nkey[++klen] = '\'';
1112 nkey[++klen] = '\0';
1114 sv_catpvn(retval, nkey, klen);
1120 sv_catpvn(retval, nkey, klen);
1122 sname = newSVsv(iname);
1123 sv_catpvn(sname, nkey, nlen);
1124 sv_catpvs(sname, "}");
1126 sv_catsv(retval, style->pair);
1127 if (style->indent >= 2) {
1130 newapad = newSVsv(apad);
1131 New(0, extra, klen+4+1, char);
1132 while (elen < (klen+4))
1133 extra[elen++] = ' ';
1135 sv_catpvn(newapad, extra, elen);
1141 DD_dump(aTHX_ hval, SvPVX_const(sname), SvCUR(sname), retval, seenhv,
1142 postav, level+1, newapad, style);
1143 SvREFCNT_dec(sname);
1144 Safefree(nkey_buffer);
1145 if (style->indent >= 2)
1146 SvREFCNT_dec(newapad);
1149 SV *opad = sv_x(aTHX_ Nullsv, SvPVX_const(style->xpad),
1150 SvCUR(style->xpad), level);
1151 if (style->trailingcomma && style->indent >= 1)
1152 sv_catpvs(retval, ",");
1153 sv_catsv(retval, totpad);
1154 sv_catsv(retval, opad);
1158 sv_catpvs(retval, ")");
1160 sv_catpvs(retval, "}");
1161 SvREFCNT_dec(iname);
1162 SvREFCNT_dec(totpad);
1164 else if (realtype == SVt_PVCV) {
1165 if (style->deparse) {
1166 SV *deparsed = sv_2mortal(deparsed_output(aTHX_ val));
1167 SV *fullpad = sv_2mortal(newSVsv(style->sep));
1172 sv_catsv(fullpad, style->pad);
1173 sv_catsv(fullpad, apad);
1174 for (i = 0; i < level; i++) {
1175 sv_catsv(fullpad, style->xpad);
1178 sv_catpvs(retval, "sub ");
1179 p = SvPV(deparsed, plen);
1181 const char *nl = (const char *) memchr(p, '\n', plen);
1183 sv_catpvn(retval, p, plen);
1188 sv_catpvn(retval, p, n);
1189 sv_catsv(retval, fullpad);
1196 sv_catpvs(retval, "sub { \"DUMMY\" }");
1198 warn("Encountered CODE ref, using dummy placeholder");
1202 warn("cannot handle ref type %d", (int)realtype);
1205 if (realpack && !no_bless) { /* free blessed allocs */
1206 STRLEN plen, pticks;
1208 if (style->indent >= 2) {
1212 sv_catpvs(retval, ", '");
1214 plen = strlen(realpack);
1215 pticks = num_q(realpack, plen);
1216 if (pticks) { /* needs escaping */
1218 char *npack_buffer = NULL;
1220 New(0, npack_buffer, plen+pticks+1, char);
1221 npack = npack_buffer;
1222 plen += esc_q(npack, realpack, plen);
1225 sv_catpvn(retval, npack, plen);
1226 Safefree(npack_buffer);
1229 sv_catpvn(retval, realpack, strlen(realpack));
1231 sv_catpvs(retval, "' )");
1232 if (style->toaster && SvPOK(style->toaster) && SvCUR(style->toaster)) {
1233 sv_catpvs(retval, "->");
1234 sv_catsv(retval, style->toaster);
1235 sv_catpvs(retval, "()");
1245 #ifdef DD_USE_OLD_ID_FORMAT
1246 idlen = my_snprintf(id, sizeof(id), "0x%" UVxf, PTR2UV(val));
1248 id_buffer = PTR2UV(val);
1249 idlen = sizeof(id_buffer);
1251 if ((svp = hv_fetch(seenhv, id, idlen, FALSE)) &&
1252 (sv = *svp) && SvROK(sv) &&
1253 (seenentry = (AV*)SvRV(sv)))
1256 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
1257 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
1259 sv_catpvs(retval, "${");
1260 sv_catsv(retval, othername);
1261 sv_catpvs(retval, "}");
1265 /* If we're allowed to keep only a sparse "seen" hash
1266 * (IOW, the user does not expect it to contain everything
1267 * after the dump, then only store in seen hash if the SV
1268 * ref count is larger than 1. If it's 1, then we know that
1269 * there is no other reference, duh. This is an optimization.
1270 * Note that we'd have to check for weak-refs, too, but this is
1271 * already the branch for non-refs only. */
1272 else if (val != &PL_sv_undef && (!style->use_sparse_seen_hash || SvREFCNT(val) > 1)) {
1273 SV * const namesv = newSVpvs("\\");
1274 sv_catpvn(namesv, name, namelen);
1275 seenentry = newAV();
1276 av_push(seenentry, namesv);
1277 av_push(seenentry, newRV_inc(val));
1278 (void)hv_store(seenhv, id, idlen, newRV_inc((SV*)seenentry), 0);
1279 SvREFCNT_dec(seenentry);
1283 if (DD_is_integer(val)) {
1286 len = my_snprintf(tmpbuf, sizeof(tmpbuf), "%" UVuf, SvUV(val));
1288 len = my_snprintf(tmpbuf, sizeof(tmpbuf), "%" IVdf, SvIV(val));
1290 /* Need to check to see if this is a string such as " 0".
1291 I'm assuming from sprintf isn't going to clash with utf8. */
1293 const char * const pv = SvPV(val, pvlen);
1294 if (pvlen != len || memNE(pv, tmpbuf, len))
1295 goto integer_came_from_string;
1298 /* Looks like we're on a 64 bit system. Make it a string so that
1299 if a 32 bit system reads the number it will cope better. */
1300 sv_catpvf(retval, "'%s'", tmpbuf);
1302 sv_catpvn(retval, tmpbuf, len);
1304 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
1306 if(i) ++c, --i; /* just get the name */
1307 if (memBEGINs(c, i, "main::")) {
1309 #if PERL_VERSION < 7
1310 if (i == 6 || (i == 7 && c[6] == '\0'))
1316 if (globname_needs_quote(c,i)) {
1317 sv_grow(retval, SvCUR(retval)+3);
1318 r = SvPVX(retval)+SvCUR(retval);
1319 r[0] = '*'; r[1] = '{'; r[2] = 0;
1320 SvCUR_set(retval, SvCUR(retval)+2);
1321 i = 3 + esc_q_utf8(aTHX_ retval, c, i,
1328 sv_grow(retval, SvCUR(retval)+2);
1329 r = SvPVX(retval)+SvCUR(retval);
1330 r[0] = '}'; r[1] = '\0';
1331 SvCUR_set(retval, SvCUR(retval)+1);
1335 sv_grow(retval, SvCUR(retval)+i+2);
1336 r = SvPVX(retval)+SvCUR(retval);
1337 r[0] = '*'; strlcpy(r+1, c, SvLEN(retval));
1339 SvCUR_set(retval, SvCUR(retval)+i);
1342 if (style->purity) {
1343 static const char* const entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
1344 static const STRLEN sizes[] = { 8, 7, 6 };
1346 SV * const nname = newSVpvs("");
1347 SV * const newapad = newSVpvs("");
1348 GV * const gv = (GV*)val;
1351 for (j=0; j<3; j++) {
1352 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
1355 if (j == 0 && !SvOK(e))
1359 SV *postentry = newSVpvn(r,i);
1361 sv_setsv(nname, postentry);
1362 sv_catpvn(nname, entries[j], sizes[j]);
1363 sv_catpvs(postentry, " = ");
1364 av_push(postav, postentry);
1367 SvCUR_set(newapad, 0);
1368 if (style->indent >= 2)
1369 (void)sv_x(aTHX_ newapad, " ", 1, SvCUR(postentry));
1371 DD_dump(aTHX_ e, SvPVX_const(nname), SvCUR(nname), postentry,
1372 seenhv, postav, 0, newapad, style);
1377 SvREFCNT_dec(newapad);
1378 SvREFCNT_dec(nname);
1381 else if (val == &PL_sv_undef || !SvOK(val)) {
1382 sv_catpvs(retval, "undef");
1385 else if (SvMAGICAL(val) && (mg = mg_find(val, 'V'))) {
1386 # if !defined(PL_vtbl_vstring) && PERL_VERSION < 17
1387 SV * const vecsv = sv_newmortal();
1388 # if PERL_VERSION < 10
1389 scan_vstring(mg->mg_ptr, vecsv);
1391 scan_vstring(mg->mg_ptr, mg->mg_ptr + mg->mg_len, vecsv);
1393 if (!sv_eq(vecsv, val)) goto integer_came_from_string;
1395 sv_catpvn(retval, (const char *)mg->mg_ptr, mg->mg_len);
1400 integer_came_from_string:
1402 /* the pure perl and XS non-qq outputs have historically been
1403 * different in this case, but for useqq, let's try to match
1404 * the pure perl code.
1407 if (style->useqq && safe_decimal_number(c, i)) {
1408 sv_catsv(retval, val);
1410 else if (DO_UTF8(val) || style->useqq)
1411 i += esc_q_utf8(aTHX_ retval, c, i, DO_UTF8(val), style->useqq);
1413 sv_grow(retval, SvCUR(retval)+3+2*i); /* 3: ""\0 */
1414 r = SvPVX(retval) + SvCUR(retval);
1416 i += esc_q(r+1, c, i);
1420 SvCUR_set(retval, SvCUR(retval)+i);
1426 if (style->deepcopy)
1427 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
1428 else if (namelen && seenentry) {
1429 SV *mark = *av_fetch(seenentry, 2, TRUE);
1437 MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
1440 # This is the exact equivalent of Dump. Well, almost. The things that are
1441 # different as of now (due to Laziness):
1442 # * doesn't do deparse yet.'
1446 Data_Dumper_Dumpxs(href, ...)
1452 SV *retval, *valstr;
1454 AV *postav, *todumpav, *namesav;
1456 SSize_t i, imax, postlen;
1458 SV *apad = &PL_sv_undef;
1461 SV *name, *val = &PL_sv_undef, *varname = &PL_sv_undef;
1463 I32 gimme = GIMME_V;
1465 if (!SvROK(href)) { /* call new to get an object first */
1467 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, [NAME_ARY_REF])");
1473 EXTEND(SP, 3); /* 3 == max of all branches below */
1475 PUSHs(sv_2mortal(newSVsv(ST(1))));
1477 PUSHs(sv_2mortal(newSVsv(ST(2))));
1479 i = perl_call_method("new", G_SCALAR);
1482 href = newSVsv(POPs);
1488 (void)sv_2mortal(href);
1491 todumpav = namesav = NULL;
1493 style.quotekeys = 1;
1494 style.maxrecurse = 1000;
1495 style.purity = style.deepcopy = style.useqq = style.maxdepth
1496 = style.use_sparse_seen_hash = style.trailingcomma = 0;
1497 style.pad = style.xpad = style.sep = style.pair = style.sortkeys
1498 = style.freezer = style.toaster = style.bless = &PL_sv_undef;
1500 name = sv_newmortal();
1502 retval = newSVpvs("");
1504 && (hv = (HV*)SvRV((SV*)href))
1505 && SvTYPE(hv) == SVt_PVHV) {
1507 if ((svp = hv_fetchs(hv, "seen", FALSE)) && SvROK(*svp))
1508 seenhv = (HV*)SvRV(*svp);
1510 style.use_sparse_seen_hash = 1;
1511 if ((svp = hv_fetchs(hv, "noseen", FALSE)))
1512 style.use_sparse_seen_hash = (SvOK(*svp) && SvIV(*svp) != 0);
1513 if ((svp = hv_fetchs(hv, "todump", FALSE)) && SvROK(*svp))
1514 todumpav = (AV*)SvRV(*svp);
1515 if ((svp = hv_fetchs(hv, "names", FALSE)) && SvROK(*svp))
1516 namesav = (AV*)SvRV(*svp);
1517 if ((svp = hv_fetchs(hv, "indent", FALSE)))
1518 style.indent = SvIV(*svp);
1519 if ((svp = hv_fetchs(hv, "purity", FALSE)))
1520 style.purity = SvIV(*svp);
1521 if ((svp = hv_fetchs(hv, "terse", FALSE)))
1522 terse = SvTRUE(*svp);
1523 if ((svp = hv_fetchs(hv, "useqq", FALSE)))
1524 style.useqq = SvTRUE(*svp);
1525 if ((svp = hv_fetchs(hv, "pad", FALSE)))
1527 if ((svp = hv_fetchs(hv, "xpad", FALSE)))
1529 if ((svp = hv_fetchs(hv, "apad", FALSE)))
1531 if ((svp = hv_fetchs(hv, "sep", FALSE)))
1533 if ((svp = hv_fetchs(hv, "pair", FALSE)))
1535 if ((svp = hv_fetchs(hv, "varname", FALSE)))
1537 if ((svp = hv_fetchs(hv, "freezer", FALSE)))
1538 style.freezer = *svp;
1539 if ((svp = hv_fetchs(hv, "toaster", FALSE)))
1540 style.toaster = *svp;
1541 if ((svp = hv_fetchs(hv, "deepcopy", FALSE)))
1542 style.deepcopy = SvTRUE(*svp);
1543 if ((svp = hv_fetchs(hv, "quotekeys", FALSE)))
1544 style.quotekeys = SvTRUE(*svp);
1545 if ((svp = hv_fetchs(hv, "trailingcomma", FALSE)))
1546 style.trailingcomma = SvTRUE(*svp);
1547 if ((svp = hv_fetchs(hv, "deparse", FALSE)))
1548 style.deparse = SvTRUE(*svp);
1549 if ((svp = hv_fetchs(hv, "bless", FALSE)))
1551 if ((svp = hv_fetchs(hv, "maxdepth", FALSE)))
1552 style.maxdepth = SvIV(*svp);
1553 if ((svp = hv_fetchs(hv, "maxrecurse", FALSE)))
1554 style.maxrecurse = SvIV(*svp);
1555 if ((svp = hv_fetchs(hv, "sortkeys", FALSE))) {
1558 style.sortkeys = NULL;
1559 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV)
1560 style.sortkeys = sv;
1561 else if (PERL_VERSION < 8)
1562 /* 5.6 doesn't make sortsv() available to XS code,
1563 * so we must use this helper instead. Note that we
1564 * always allocate this mortal SV, but it will be
1565 * used only if at least one hash is encountered
1566 * while dumping recursively; an older version
1567 * allocated it lazily as needed. */
1568 style.sortkeys = sv_2mortal(newSVpvs("Data::Dumper::_sortkeys"));
1570 /* flag to use sortsv() for sorting hash keys */
1571 style.sortkeys = &PL_sv_yes;
1576 imax = av_len(todumpav);
1579 valstr = newSVpvs("");
1580 for (i = 0; i <= imax; ++i) {
1584 if ((svp = av_fetch(todumpav, i, FALSE)))
1588 if ((svp = av_fetch(namesav, i, TRUE))) {
1589 sv_setsv(name, *svp);
1590 if (SvOK(*svp) && !SvPOK(*svp))
1591 (void)SvPV_nolen_const(name);
1594 (void)SvOK_off(name);
1597 if ((SvPVX_const(name))[0] == '*') {
1599 switch (SvTYPE(SvRV(val))) {
1601 (SvPVX(name))[0] = '@';
1604 (SvPVX(name))[0] = '%';
1607 (SvPVX(name))[0] = '*';
1610 (SvPVX(name))[0] = '$';
1615 (SvPVX(name))[0] = '$';
1617 else if ((SvPVX_const(name))[0] != '$')
1618 sv_insert(name, 0, 0, "$", 1);
1622 sv_setpvs(name, "$");
1623 sv_catsv(name, varname);
1624 nchars = my_snprintf(tmpbuf, sizeof(tmpbuf), "%" IVdf,
1626 sv_catpvn(name, tmpbuf, nchars);
1629 if (style.indent >= 2 && !terse) {
1630 SV * const tmpsv = sv_x(aTHX_ NULL, " ", 1, SvCUR(name)+3);
1631 newapad = newSVsv(apad);
1632 sv_catsv(newapad, tmpsv);
1633 SvREFCNT_dec(tmpsv);
1639 DD_dump(aTHX_ val, SvPVX_const(name), SvCUR(name), valstr, seenhv,
1640 postav, 0, newapad, &style);
1643 if (style.indent >= 2 && !terse)
1644 SvREFCNT_dec(newapad);
1646 postlen = av_len(postav);
1647 if (postlen >= 0 || !terse) {
1648 sv_insert(valstr, 0, 0, " = ", 3);
1649 sv_insert(valstr, 0, 0, SvPVX_const(name), SvCUR(name));
1650 sv_catpvs(valstr, ";");
1652 sv_catsv(retval, style.pad);
1653 sv_catsv(retval, valstr);
1654 sv_catsv(retval, style.sep);
1657 sv_catsv(retval, style.pad);
1658 for (i = 0; i <= postlen; ++i) {
1660 svp = av_fetch(postav, i, FALSE);
1661 if (svp && (elem = *svp)) {
1662 sv_catsv(retval, elem);
1664 sv_catpvs(retval, ";");
1665 sv_catsv(retval, style.sep);
1666 sv_catsv(retval, style.pad);
1670 sv_catpvs(retval, ";");
1671 sv_catsv(retval, style.sep);
1674 if (gimme == G_ARRAY) {
1675 XPUSHs(sv_2mortal(retval));
1676 if (i < imax) /* not the last time thro ? */
1677 retval = newSVpvs("");
1680 SvREFCNT_dec(postav);
1681 SvREFCNT_dec(valstr);
1684 croak("Call to new() method failed to return HASH ref");
1685 if (gimme != G_ARRAY)
1686 XPUSHs(sv_2mortal(retval));
1690 Data_Dumper__vstring(sv)
1698 SvMAGICAL(sv) && (mg = mg_find(sv, 'V'))
1699 ? newSVpvn((const char *)mg->mg_ptr, mg->mg_len)
1702 RETVAL = &PL_sv_undef;