4 use vars
qw(@EXPORT @EXPORT_OK $VERSION $DEBUG);
8 *import = \&Exporter::import;
10 @EXPORT_OK = qw(dump pp dumpf quote);
16 use vars
qw(%seen %refcnt @dump @fixup %require $TRY_BASE64 @FILTERS $INDENT);
18 $TRY_BASE64 = 50 unless defined $TRY_BASE64;
19 $INDENT = " " unless defined $INDENT;
28 require Data::Dump::FilterContext if @FILTERS;
34 my $val = _dump($v, $name, [], tied($v));
35 push(@dump, [$name, $val]);
42 for (sort keys %require) {
43 $out .= "require $_;\n";
47 # output all those with refcounts first
51 $out .= "my \$$name = $_->[1];\n";
60 my $paren = (@dump != 1);
61 $out .= "(" if $paren;
62 $out .= format_list($paren, undef,
63 map {defined($_->[1]) ? $_->[1] : "\$".$_->[0]}
66 $out .= ")" if $paren;
68 if (%refcnt || %require) {
70 $out =~ s/^/$INDENT/gm;
74 print STDERR "$out\n" unless defined wantarray;
85 my(undef, $file, $line) = caller;
87 my $out = "$file:$line: " . dump(@_) . "\n";
93 require Data::Dump::Filtered;
94 goto &Data::Dump::Filtered::dump_filtered;
100 my $rval = $ref ? $_[0] : \$_[0];
103 my($name, $idx, $dont_remember, $pclass, $pidx) = @_;
105 my($class, $type, $id);
106 my $strval = overload::StrVal($rval);
107 # Parse $strval without using regexps, in order not to clobber $1, $2,...
108 if ((my $i = rindex($strval, "=")) >= 0) {
109 $class = substr($strval, 0, $i);
110 $strval = substr($strval, $i+1);
112 if ((my $i = index($strval, "(0x")) >= 0) {
113 $type = substr($strval, 0, $i);
114 $id = substr($strval, $i + 2, -1);
117 die "Can't parse " . overload::StrVal($rval);
119 if ($] < 5.008 && $type eq "SCALAR") {
120 $type = "REF" if $ref eq "REF";
122 warn "\$$name(@$idx) $class $type $id ($ref)" if $DEBUG;
129 $pself = fullname("self", [@$idx[$pidx..(@$idx - 1)]]) if $pclass;
130 my $ctx = Data::Dump::FilterContext->new($rval, $class, $type, $ref, $pclass, $pidx, $idx);
132 for my $filter (@FILTERS) {
133 if (my $f = $filter->($ctx, $rval)) {
134 if (my $v = $f->{object}) {
136 $out = _dump($v, $name, $idx, 1);
139 if (defined(my $c = $f->{bless})) {
142 if (my $c = $f->{comment}) {
145 if (defined(my $c = $f->{dump})) {
149 if (my $h = $f->{hide_keys}) {
150 if (ref($h) eq "ARRAY") {
153 return 1 if $k eq $_[0];
161 push(@bless, "") if defined($out) && !@bless;
163 $class = shift(@bless);
164 warn "More than one filter callback tried to bless object" if @bless;
168 unless ($dont_remember) {
169 if (my $s = $seen{$id}) {
170 my($sname, $sidx) = @$s;
172 my $sref = fullname($sname, $sidx,
173 ($ref && $type eq "SCALAR"));
174 warn "SEEN: [\$$name(@$idx)] => [\$$sname(@$sidx)] ($ref,$sref)" if $DEBUG;
175 return $sref unless $sname eq $name;
177 push(@fixup, fullname($name,$idx)." = $sref");
178 return "do{my \$fix}" if @$idx && $idx->[-1] eq '$';
181 $seen{$id} = [$name, $idx];
192 elsif ($type eq "SCALAR" || $type eq "REF" || $type eq "REGEXP") {
194 if ($class && $class eq "Regexp") {
198 if ($v =~ /^\(\?\^?([msix-]*):([\x00-\xFF]*)\)\z/) {
205 my $sep_count = ($v =~ tr/\///);
207 # see if we can find a better one
208 for ('|', ',', ':', '#') {
209 my $c = eval "\$v =~ tr/\Q$_\E//";
210 #print "SEP $_ $c $sep_count\n";
211 if ($c < $sep_count) {
214 last if $sep_count == 0;
218 $v =~ s/\Q$sep\E/\\$sep/g;
220 $out = "qr$sep$v$sep$mod";
224 delete $seen{$id} if $type eq "SCALAR"; # will be seen again shortly
225 my $val = _dump($$rval, $name, [@$idx, "\$"], 0, $pclass, $pidx);
226 $out = $class ? "do{\\(my \$o = $val)}" : "\\$val";
229 if (!defined $$rval) {
232 elsif (do {no warnings 'numeric'; $$rval + 0 eq $$rval}) {
238 if ($class && !@$idx) {
239 # Top is an object, not a reference to one as perl needs
241 my $obj = fullname($name, $idx);
242 my $cl = quote($class);
243 push(@fixup, "bless \\$obj, $cl");
247 elsif ($type eq "GLOB") {
250 my $val = _dump($$rval, $name, [@$idx, "*"], 0, $pclass, $pidx);
252 if ($out =~ /^\\\*Symbol::/) {
254 $out = "Symbol::gensym()";
260 for my $k (qw(SCALAR ARRAY HASH)) {
261 my $gval = *$$rval{$k};
262 next unless defined $gval;
263 next if $k eq "SCALAR" && ! defined $$gval; # always there
264 my $f = scalar @fixup;
265 push(@fixup, "RESERVED"); # overwritten after _dump() below
266 $gval = _dump
($gval, $name, [@
$idx, "*{$k}"], 0, $pclass, $pidx);
268 my $gname = fullname
($name, $idx);
269 $fixup[$f] = "$gname = $gval"; #XXX indent $gval
273 elsif ($type eq "ARRAY") {
275 my $tied = tied_str
(tied(@
$rval));
278 push(@vals, _dump
($v, $name, [@
$idx, "[$i]"], $tied, $pclass, $pidx));
281 $out = "[" . format_list
(1, $tied, @vals) . "]";
283 elsif ($type eq "HASH") {
285 my $tied = tied_str
(tied(%$rval));
287 # statistics to determine variation in key lengths
292 my @orig_keys = keys %$rval;
294 @orig_keys = grep !$hide_keys->($_), @orig_keys;
298 $text_keys++, last unless /^[-+]?(?:0|[1-9]\d*)(?:\.\d+)?\z/;
302 @orig_keys = sort { lc($a) cmp lc($b) } @orig_keys;
305 @orig_keys = sort { $a <=> $b } @orig_keys;
309 for my $key (@orig_keys) {
310 next if $key =~ /^-?[a-zA-Z_]\w*\z/;
311 next if $key =~ /^-?[1-9]\d{0,8}\z/;
316 for my $key (@orig_keys) {
317 my $val = \
$rval->{$key}; # capture value before we modify $key
318 $key = quote
($key) if $quote;
319 $kstat_max = length($key) if length($key) > $kstat_max;
320 $kstat_sum += length($key);
321 $kstat_sum2 += length($key)*length($key);
324 push(@vals, _dump
($$val, $name, [@
$idx, "{$key}"], $tied, $pclass, $pidx));
328 my $tmp = "@keys @vals";
329 if (length($tmp) > 60 || $tmp =~ /\n/ || $tied) {
332 # Determine what padding to add
333 if ($kstat_max < 4) {
334 $klen_pad = $kstat_max;
338 my $avg = $kstat_sum/$n;
339 my $stddev = sqrt(($kstat_sum2 - $n * $avg * $avg) / ($n - 1));
341 # I am not actually very happy with this heuristics
342 if ($stddev / $kstat_max < 0.25) {
343 $klen_pad = $kstat_max;
347 push(@vals, sprintf("%.2f (%d/%.1f/%.1f)",
348 $stddev / $kstat_max,
349 $kstat_max, $avg, $stddev));
354 $out .= "$INDENT# $tied$nl" if $tied;
356 my $key = shift @keys;
357 my $val = shift @vals;
358 my $vpad = $INDENT . (" " x
($klen_pad ?
$klen_pad + 4 : 0));
359 $val =~ s/\n/\n$vpad/gm;
360 my $kpad = $nl ?
$INDENT : " ";
361 $key .= " " x
($klen_pad - length($key)) if $nl;
362 $out .= "$kpad$key => $val,$nl";
364 $out =~ s/,$/ / unless $nl;
367 elsif ($type eq "CODE") {
368 $out = 'sub { ... }';
370 elsif ($type eq "VSTRING") {
371 $out = sprintf +($ref ?
'\v%vd' : 'v%vd'), $$rval;
374 warn "Can't handle $type data";
378 if ($class && $ref) {
379 $out = "bless($out, " . quote
($class) . ")";
382 $comment =~ s/^/# /gm;
383 $comment .= "\n" unless $comment =~ /\n\z/;
384 $comment =~ s/^#[ \t]+\n/\n/;
385 $out = "$comment$out";
393 if (my $tied_ref = ref($tied)) {
394 $tied = "tied $tied_ref";
405 my($name, $idx, $ref) = @_;
406 substr($name, 0, 0) = "\$";
408 my @i = @
$idx; # need copy in order to not modify @$idx
409 if ($ref && @i && $i[0] eq "\$") {
410 shift(@i); # remove one deref
413 while (@i && $i[0] eq "\$") {
420 if ($i eq "*" || $i eq "\$") {
422 $name = "$i\{$name}";
423 } elsif ($i =~ s/^\*//) {
427 $name .= "->" unless $last_was_index++;
431 $name = "\\$name" if $ref;
439 my $indent_lim = $paren ?
0 : 1;
441 # can we use range operator to shorten the list?
447 # XXX allow string increment too?
448 if ($v eq "0" || $v =~ /^-?[1-9]\d{0,9}\z/) {
451 elsif ($v =~ /^"([A-Za-z]{1,3}\d*)"\z/) {
459 last if $_[$j] ne $v;
463 splice(@_, $i, $j - $i, "$_[$i] .. $_[$j-1]");
469 if ($comment || (@_ > $indent_lim && (length($tmp) > 60 || $tmp =~ /\n/))) {
471 for (@elem) { s/^/$INDENT/gm; }
472 return "\n" . ($comment ?
"$INDENT# $comment\n" : "") .
473 join(",\n", @elem, "");
475 return join(", ", @_);
480 if (length($_[0]) > 20) {
482 # Check for repeated string
484 # seems to be a repating sequence, let's check if it really is
485 # without backtracking
486 unless (/[^\Q$1\E]/) {
487 my $base = quote
($1);
489 return "($base x $repeat)"
492 # Length protection because the RE engine will blow the stack [RT#33520]
493 if (length($_) < 16 * 1024 && /^(.{2,5}?)\1*\z/s) {
494 my $base = quote
($1);
495 my $repeat = length($_)/length($1);
496 return "($base x $repeat)";
503 if (length($_) > 40 && !/\\x\{/ && length($_) > (length($_[0]) * 2)) {
504 # too much binary data, better to represent as a hex/base64 string
506 # Base64 is more compact than hex when string is longer than
507 # 17 bytes (not counting any require statement needed).
508 # But on the other hand, hex is much more readable.
509 if ($TRY_BASE64 && length($_[0]) > $TRY_BASE64 &&
510 (defined &utf8
::is_utf8
&& !utf8
::is_utf8
($_[0])) &&
511 eval { require MIME
::Base64
})
513 $require{"MIME::Base64"}++;
514 return "MIME::Base64::decode(\"" .
515 MIME
::Base64
::encode
($_[0],"") .
518 return "pack(\"H*\",\"" . unpack("H*", $_[0]) . "\")";
534 # put a string value in double quotes
537 # If there are many '"' we might want to use qq() instead
538 s/([\\\"\@\$])/\\$1/g;
539 return qq("$_") unless /[^\040-\176]/; # fast exit
541 s/([\a\b\t\n\f\r\e])/$esc{$1}/g;
543 # no need for 3 digits in escape for these
544 s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
546 s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
547 s/([^\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
558 Data::Dump - Pretty printing of data structures
562 use Data::Dump qw(dump);
565 @copy_of_list = eval $str;
567 # or use it for easy debug printout
568 use Data::Dump; dd localtime;
572 This module provide a few functions that traverse their
573 argument and produces a string as its result. The string contains
574 Perl code that, when C<eval>ed, produces a deep copy of the original
577 The main feature of the module is that it strives to produce output
578 that is easy to read. Example:
580 @a = (1, [2, 3], {4 => 5});
585 "(1, [2, 3], { 4 => 5 })"
587 If you dump just a little data, it is output on a single line. If
588 you dump data that is more complex or there is a lot of it, line breaks
589 are automatically added to keep it easy to read.
591 The following functions are provided (only the dd* functions are exported by default):
599 Returns a string containing a Perl expression. If you pass this
600 string to Perl's built-in eval() function it should return a copy of
601 the arguments you passed to dump().
603 If you call the function with multiple arguments then the output will
604 be wrapped in parenthesis "( ..., ... )". If you call the function with a
605 single argument the output will not have the wrapping. If you call the function with
606 a single scalar (non-reference) argument it will just return the
607 scalar quoted if needed, but never break it into multiple lines. If you
608 pass multiple arguments or references to arrays of hashes then the
609 return value might contain line breaks to format it for easier
610 reading. The returned string will never be "\n" terminated, even if
611 contains multiple lines. This allows code like this to place the
612 semicolon in the expected place:
614 print '$obj = ', dump($obj), ";\n";
616 If dump() is called in void context, then the dump is printed on
617 STDERR and then "\n" terminated. You might find this useful for quick
618 debug printouts, but the dd*() functions might be better alternatives
621 There is no difference between dump() and pp(), except that dump()
622 shares its name with a not-so-useful perl builtin. Because of this
623 some might want to avoid using that name.
625 =item quote( $string )
627 Returns a quoted version of the provided string.
629 It differs from C<dump($string)> in that it will quote even numbers and
630 not try to come up with clever expressions that might shorten the
631 output. If a non-scalar argument is provided then it's just stringified
632 instead of traversed.
638 These functions will call dump() on their argument and print the
639 result to STDOUT (actually, it's the currently selected output handle, but
640 STDOUT is the default for that).
642 The difference between them is only that ddx() will prefix the lines
643 it prints with "# " and mark the first line with the file and line
644 number where it was called. This is meant to be useful for debug
645 printouts of state within programs.
647 =item dumpf( ..., \&filter )
649 Short hand for calling the dump_filtered() function of L<Data::Dump::Filtered>.
650 This works like dump(), but the last argument should be a filter callback
651 function. As objects are visited the filter callback is invoked and it
652 can modify how the objects are dumped.
658 There are a few global variables that can be set to modify the output
659 generated by the dump functions. It's wise to localize the setting of
664 =item $Data::Dump::INDENT
666 This holds the string that's used for indenting multiline data structures.
667 It's default value is " " (two spaces). Set it to "" to suppress indentation.
668 Setting it to "| " makes for nice visuals even if the dump output then fails to
671 =item $Data::Dump::TRY_BASE64
673 How long must a binary string be before we try to use the base64 encoding
674 for the dump output. The default is 50. Set it to 0 to disable base64 dumps.
681 Code references will be dumped as C<< sub { ... } >>. Thus, C<eval>ing them will
682 not reproduce the original routine. The C<...>-operator used will also require
683 perl-5.12 or better to be evaled.
685 If you forget to explicitly import the C<dump> function, your code will
686 core dump. That's because you just called the builtin C<dump> function
687 by accident, which intentionally dumps core. Because of this you can
688 also import the same function as C<pp>, mnemonic for "pretty-print".
692 The C<Data::Dump> module grew out of frustration with Sarathy's
693 in-most-cases-excellent C<Data::Dumper>. Basic ideas and some code
694 are shared with Sarathy's module.
696 The C<Data::Dump> module provides a much simpler interface than
697 C<Data::Dumper>. No OO interface is available and there are fewer
698 configuration options to worry about. The other benefit is
699 that the dump produced does not try to set any variables. It only
700 returns what is needed to produce a copy of the arguments. This means
701 that C<dump("foo")> simply returns C<'"foo"'>, and C<dump(1..3)> simply
702 returns C<'(1, 2, 3)'>.
706 L<Data::Dump::Filtered>, L<Data::Dump::Trace>, L<Data::Dumper>, L<JSON>,
711 The C<Data::Dump> module is written by Gisle Aas <gisle@aas.no>, based
712 on C<Data::Dumper> by Gurusamy Sarathy <gsar@umich.edu>.
714 Copyright 1998-2010 Gisle Aas.
715 Copyright 1996-1998 Gurusamy Sarathy.
717 This library is free software; you can redistribute it and/or
718 modify it under the same terms as Perl itself.