8 use vars
qw( $VERSION @ISA @EXPORT );
10 @ISA = qw( DynaLoader Exporter );
11 @EXPORT = qw( DDumper DPeek DDump );
12 $] >= 5.007003 and push @EXPORT, "DDump_IO";
14 bootstrap DDumper
$VERSION;
20 local $Data::Dumper
::Sortkeys
= 1;
21 local $Data::Dumper
::Indent
= 1;
23 my $s = Data
::Dumper
::Dumper
@_;
24 $s =~ s!^(\s*)'([^']*)'\s*=>!sprintf "%s%-16s =>", $1, $2!gme; # Align => '
25 $s =~ s!^(?= *[]}](?:[;,]|$))! !gm;
26 $s =~ s!^(\s+)!$1$1!gm;
32 my ($var, $down) = (@_, 0);
35 if ($ref eq "SCALAR" || $ref eq "REF") {
36 my %hash = DDump
($$var, $down);
39 if ($ref eq "ARRAY") {
41 foreach my $list (@
$var) {
42 my %hash = DDump
($list, $down);
43 push @list, { %hash };
49 foreach my $key (sort keys %$var) {
50 $hash{DPeek
($key)} = { DDump
($var->{$key}, $down) };
59 my ($var, $down) = (@_, 0);
60 my @dump = split "\n", DDump_XS
($var) or return;
64 ($hash{sv
} = $dump[0]) =~ s/^SV\s*=\s*//;
65 m/^\s+(\w+)\s*=\s*(.*)/ and $hash{$1} = $2 for @dump;
67 if (exists $hash{FLAGS
}) {
68 $hash{FLAGS
} =~ tr/()//d;
69 $hash{FLAGS
} = { map { $_ => 1 } split m/,/ => $hash{FLAGS
} };
73 $hash{RV
} = _DDump_ref
($var, $down - 1);
77 my $dump = join "\n", @dump, "";
79 defined wantarray and return $dump;
90 DDumper - Modified and extended debugging facilities
96 print DDumper \%hash; # Same syntax as Data::Dumper
100 my $dump = DDump $var;
101 my %hash = DDump \@list;
104 my %hash = DDump (\%hash, 5); # dig 5 levels deep
107 open my $fh, ">", \$dump;
108 DDump_IO ($fh, \%hash, 6);
114 =head2 DDumper ($var, ...)
116 Not liking the default output of Data::Dumper, and always feeling the need
117 to set C<$Data::Dumper::Sortkeys = 1;>, and not liking any of the default
118 layouts, this function is just a wrapper around Data::Dumper::Dumper with
119 everything set as I like it.
121 $Data::Dumper::Sortkeys = 1;
122 $Data::Dumper::Indent = 1;
124 And the result is further beautified to meet my needs:
126 * quotation of hash keys has been removed
127 * arrows for hashes are aligned at 20 (longer keys don't align)
128 * closing braces and brackets are now correctly aligned
132 print DDumper { ape => 1, foo => "egg", bar => [ 2, "baz", undef ]};
146 Playing with C<sv_dump ()>, I found C<Perl_sv_peek ()>, and it might be
147 very useful for simple checks.
151 print DPeek "abc\x{0a}de\x{20ac}fg";
153 PV("abc\nde\342\202\254fg"\0) [UTF8 "abc\nde\x{20ac}fg"]
155 =head3 DDump ($var [, $dig_level])
157 A very useful module when debugging is C<Devel::Peek>, but is has one big
158 disadvantage: it only prints to STDERR, which is not very handy when your
159 code wants to inspect variables al a low level.
161 Perl itself has C<sv_dump ()>, which does something similar, but still
162 prints to STDERR, and only one level deep.
164 C<DDump ()> is an attempt to make the innards available to the script level
165 with a reasonable level of compatibility. C<DDump ()> is context sensitive.
167 In void context, it behaves exactly like C<Perl_sv_dump ()>.
169 In scalar context, it returns what C<Perl_sv_dump ()> would have printed.
171 In list context, it returns a hash of the variable's properties. In this mode
172 you can pass an optional second argument that detemines the depth of digging.
176 print scalar DDump "abc\x{0a}de\x{20ac}fg"
178 SV = PV(0x723250) at 0x8432b0
180 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8)
181 PV = 0x731ac0 "abc\nde\342\202\254fg"\0 [UTF8 "abc\nde\x{20ac}fg"]
185 my %h = DDump "abc\x{0a}de\x{20ac}fg";
198 PV => '0x731ac0 "abc\\nde\\342\\202\\254fg"\\0 [UTF8 "abc\\nde\\x{20ac}fg"]',
200 sv => 'PV(0x723250) at 0x8432c0'
206 bar => [ 2, "baz", undef ],
227 sv => 'IV(0x747020) at 0x843a10'
241 sv => 'PVIV(0x7223e0) at 0x843a10'
253 PV => '0x7496c0 "egg"\\0',
255 sv => 'PVIV(0x7223e0) at 0x843a10'
258 sv => 'RV(0x79d058) at 0x843310'
261 =head2 DDump_IO ($io, $var [, $dig_level])
263 A wrapper function around perl's internal C<Perl_do_sv_dump ()>, which
264 makes C<Devel::Peek> completely superfluous. As PerlIO is only available
265 perl version 5.7.3 and up, this function is not available in older perls.
270 open my $eh, ">", \$dump;
271 DDump_IO ($eh, { 3 => 4, ape => [5..8]}, 6);
275 SV = RV(0x79d9e0) at 0x843f00
279 SV = PVHV(0x79c948) at 0x741090
284 ARRAY = 0x748ff0 (0:7, 2:1)
291 Elt "ape" HASH = 0x97623e03
292 SV = RV(0x79d9d8) at 0x8440e0
296 SV = PVAV(0x7264b0) at 0x741470
307 SV = IV(0x7467c8) at 0x7c1aa0
312 SV = IV(0x7467b0) at 0x8440f0
317 SV = IV(0x746810) at 0x75be00
322 SV = IV(0x746d38) at 0x7799d0
326 Elt "3" HASH = 0xa400c7f3
327 SV = IV(0x746fd0) at 0x7200e0
334 C<DDump ()> uses an XS wrapper around C<Perl_sv_dump ()> where the
335 STDERR is temporarily caught to a pipe. The internal XS helper functions
336 are not meant for user space
338 =head2 DDump_XS (SV *sv)
340 Base interface to internals for C<DDump ()>.
342 =head2 DDump_rf (SV *sv)
344 Dump the content of a reference variable like C<DDump_XS ()>.
348 Not all types of references are supported.
352 No idea how far back this goes in perl support.
362 H.Merijn Brand <h.m.brand@xs4all.nl>
364 =head1 COPYRIGHT AND LICENSE
366 Copyright (C) 2008-2008 H.Merijn Brand
368 This library is free software; you can redistribute it and/or modify
369 it under the same terms as Perl itself.