8 use vars
qw( $VERSION @ISA @EXPORT );
10 @ISA = qw( DynaLoader Exporter );
11 @EXPORT = qw( DDumper DPeek DDump DDump_IO );
13 bootstrap DDumper
$VERSION;
19 local $Data::Dumper
::Sortkeys
= 1;
20 local $Data::Dumper
::Indent
= 1;
22 my $s = Data
::Dumper
::Dumper
@_;
23 $s =~ s!^(\s*)'([^']*)'\s*=>!sprintf "%s%-16s =>", $1, $2!gme; # Align => '
24 $s =~ s!^(?= *[]}](?:[;,]|$))! !gm;
25 $s =~ s!^(\s+)!$1$1!gm;
31 my ($var, $down) = (@_, 0);
34 if ($ref eq "SCALAR" || $ref eq "REF") {
35 my %hash = DDump
($$var, $down);
38 if ($ref eq "ARRAY") {
40 foreach my $list (@
$var) {
41 my %hash = DDump
($list, $down);
42 push @list, { %hash };
48 foreach my $key (sort keys %$var) {
49 $hash{DPeek
($key)} = { DDump
($var->{$key}, $down) };
58 my ($var, $down) = (@_, 0);
59 my @dump = split "\n", DDump_XS
($var) or return;
63 ($hash{sv
} = $dump[0]) =~ s/^SV\s*=\s*//;
64 m/^\s+(\w+)\s*=\s*(.*)/ and $hash{$1} = $2 for @dump;
66 if (exists $hash{FLAGS
}) {
67 $hash{FLAGS
} =~ tr/()//d;
68 $hash{FLAGS
} = { map { $_ => 1 } split m/,/ => $hash{FLAGS
} };
72 $hash{RV
} = _DDump_ref
($var, $down - 1);
76 my $dump = join "\n", @dump, "";
78 defined wantarray and return $dump;
89 DDumper - Modified and extended debugging facilities
95 print DDumper \%hash; # Same syntax as Data::Dumper
99 my $dump = DDump $var;
100 my %hash = DDump \@list;
103 my %hash = DDump (\%hash, 5); # dig 5 levels deep
106 open my $fh, ">", \$dump;
107 DDump_IO ($fh, \%hash, 6);
113 =head2 DDumper ($var, ...)
115 Not liking the default output of Data::Dumper, and always feeling the need
116 to set C<$Data::Dumper::Sortkeys = 1;>, and not liking any of the default
117 layouts, this function is just a wrapper around Data::Dumper::Dumper with
118 everything set as I like it.
120 $Data::Dumper::Sortkeys = 1;
121 $Data::Dumper::Indent = 1;
123 And the result is further beautified to meet my needs:
125 * quotation of hash keys has been removed
126 * arrows for hashes are aligned at 20 (longer keys don't align)
127 * closing braces and brackets are now correctly aligned
131 print DDumper { ape => 1, foo => "egg", bar => [ 2, "baz", undef ]};
145 Playing with C<sv_dump ()>, I found C<Perl_sv_peek ()>, and it might be
146 very useful for simple checks.
150 print DPeek "abc\x{0a}de\x{20ac}fg";
152 PV("abc\nde\342\202\254fg"\0) [UTF8 "abc\nde\x{20ac}fg"]
154 =head3 DDump ($var [, $dig_level])
156 A very useful module when debugging is C<Devel::Peek>, but is has one big
157 disadvantage: it only prints to STDERR, which is not very handy when your
158 code wants to inspect variables al a low level.
160 Perl itself has C<sv_dump ()>, which does something similar, but still
161 prints to STDERR, and only one level deep.
163 C<DDump ()> is an attempt to make the innards available to the script level
164 with a reasonable level of compatibility. C<DDump ()> is context sensitive.
166 In void context, it behaves exactly like C<Perl_sv_dump ()>.
168 In scalar context, it returns what C<Perl_sv_dump ()> would have printed.
170 In list context, it returns a hash of the variable's properties. In this mode
171 you can pass an optional second argument that detemines the depth of digging.
175 print scalar DDump "abc\x{0a}de\x{20ac}fg"
177 SV = PV(0x723250) at 0x8432b0
179 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8)
180 PV = 0x731ac0 "abc\nde\342\202\254fg"\0 [UTF8 "abc\nde\x{20ac}fg"]
184 my %h = DDump "abc\x{0a}de\x{20ac}fg";
197 PV => '0x731ac0 "abc\\nde\\342\\202\\254fg"\\0 [UTF8 "abc\\nde\\x{20ac}fg"]',
199 sv => 'PV(0x723250) at 0x8432c0'
205 bar => [ 2, "baz", undef ],
226 sv => 'IV(0x747020) at 0x843a10'
240 sv => 'PVIV(0x7223e0) at 0x843a10'
252 PV => '0x7496c0 "egg"\\0',
254 sv => 'PVIV(0x7223e0) at 0x843a10'
257 sv => 'RV(0x79d058) at 0x843310'
260 =head2 DDump_IO ($io, $var [, $dig_level])
262 A wrapper function around perl's internal C<Perl_do_sv_dump ()>, which
263 makes C<Devel::Peek> completely superfluous.
268 open my $eh, ">", \$dump;
269 DDump_IO ($eh, { 3 => 4, ape => [5..8]}, 6);
273 SV = RV(0x79d9e0) at 0x843f00
277 SV = PVHV(0x79c948) at 0x741090
282 ARRAY = 0x748ff0 (0:7, 2:1)
289 Elt "ape" HASH = 0x97623e03
290 SV = RV(0x79d9d8) at 0x8440e0
294 SV = PVAV(0x7264b0) at 0x741470
305 SV = IV(0x7467c8) at 0x7c1aa0
310 SV = IV(0x7467b0) at 0x8440f0
315 SV = IV(0x746810) at 0x75be00
320 SV = IV(0x746d38) at 0x7799d0
324 Elt "3" HASH = 0xa400c7f3
325 SV = IV(0x746fd0) at 0x7200e0
332 C<DDump ()> uses an XS wrapper around C<Perl_sv_dump ()> where the
333 STDERR is temporarily caught to a pipe. The internal XS helper functions
334 are not meant for user space
336 =head2 DDump_XS (SV *sv)
338 Base interface to internals for C<DDump ()>.
340 =head2 DDump_rf (SV *sv)
342 Dump the content of a reference variable like C<DDump_XS ()>.
346 Not all types of references are supported.
350 No idea how far back this goes in perl support.
360 H.Merijn Brand <h.m.brand@xs4all.nl>
362 =head1 COPYRIGHT AND LICENSE
364 Copyright (C) 2008-2008 H.Merijn Brand
366 This library is free software; you can redistribute it and/or modify
367 it under the same terms as Perl itself.