8 use vars
qw( $VERSION @ISA @EXPORT );
10 @ISA = qw( DynaLoader Exporter );
11 @EXPORT = qw( DDumper DPeek DDump DDual );
12 $] >= 5.007003 and push @EXPORT, "DDump_IO";
14 bootstrap Data
::Peek
$VERSION;
16 ### ############# DDumper () ##################################################
22 local $Data::Dumper
::Sortkeys
= 1;
23 local $Data::Dumper
::Indent
= 1;
25 my $s = Data
::Dumper
::Dumper
@_;
26 $s =~ s!^(\s*)'([^']*)'\s*=>!sprintf "%s%-16s =>", $1, $2!gme; # Align => '
27 $s =~ s!^(?= *[]}](?:[;,]|$))! !gm;
28 $s =~ s!^(\s+)!$1$1!gm;
30 defined wantarray or print STDERR
$s;
34 ### ############# DDump () ####################################################
40 $has_perlio = ($Config{useperlio
} || "undef") eq "define";
45 my ($var, $down) = (@_, 0);
48 if ($ref eq "SCALAR" || $ref eq "REF") {
49 my %hash = DDump
($$var, $down);
52 if ($ref eq "ARRAY") {
54 foreach my $list (@
$var) {
55 my %hash = DDump
($list, $down);
56 push @list, { %hash };
62 foreach my $key (sort keys %$var) {
63 $hash{DPeek
($key)} = { DDump
($var->{$key}, $down) };
72 my ($var, $down, $dump, $fh) = (@_, "");
74 if ($has_perlio and open $fh, ">", \
$dump) {
75 #print STDERR "Using DDump_IO\n";
76 DDump_IO
($fh, $var, $down);
80 #print STDERR "Using DDump_XS\n";
81 $dump = DDump_XS
($var);
89 my ($var, $down) = (@_, 0);
90 my @dump = split m/[\r\n]+/, _DDump
($var, wantarray || $down) or return;
94 ($hash{sv
} = $dump[0]) =~ s/^SV\s*=\s*//;
95 m/^\s+(\w+)\s*=\s*(.*)/ and $hash{$1} = $2 for @dump;
97 if (exists $hash{FLAGS
}) {
98 $hash{FLAGS
} =~ tr/()//d;
99 $hash{FLAGS
} = { map { $_ => 1 } split m/,/ => $hash{FLAGS
} };
102 $down && ref $var and
103 $hash{RV
} = _DDump_ref
($var, $down - 1) || $var;
107 my $dump = join "\n", @dump, "";
109 defined wantarray and return $dump;
120 Data::Peek - A collection of low-level debug facilities
126 print DDumper \%hash; # Same syntax as Data::Dumper
129 my ($pv, $iv, $nv, $rv, $magic) = DDual ($var [, 1]);
130 print DPeek for DDual ($!, 1);
132 my $dump = DDump $var;
133 my %hash = DDump \@list;
136 my %hash = DDump (\%hash, 5); # dig 5 levels deep
139 open my $fh, ">", \$dump;
140 DDump_IO ($fh, \%hash, 6);
146 Data::Peek started off as C<DDumper> being a wrapper module over
147 L<Data::Dumper>, but grew out to be a set of low-level data
148 introspection utilities that no other module provided yet, using the
149 lowest level of the perl internals API as possible.
151 =head2 DDumper ($var, ...)
153 Not liking the default output of Data::Dumper, and always feeling the need
154 to set C<$Data::Dumper::Sortkeys = 1;>, and not liking any of the default
155 layouts, this function is just a wrapper around Data::Dumper::Dumper with
156 everything set as I like it.
158 $Data::Dumper::Sortkeys = 1;
159 $Data::Dumper::Indent = 1;
161 And the result is further beautified to meet my needs:
163 * quotation of hash keys has been removed (with the disadvantage
164 that the output might not be parseable again).
165 * arrows for hashes are aligned at 16 (longer keys don't align)
166 * closing braces and brackets are now correctly aligned
168 In void context, C<DDumper ()> prints to STDERR.
172 print DDumper { ape => 1, foo => "egg", bar => [ 2, "baz", undef ]};
188 Playing with C<sv_dump ()>, I found C<Perl_sv_peek ()>, and it might be
189 very useful for simple checks. If C<$var> is omitted, uses $_.
193 print DPeek "abc\x{0a}de\x{20ac}fg";
195 PV("abc\nde\342\202\254fg"\0) [UTF8 "abc\nde\x{20ac}fg"]
197 =head2 DDual ($var [, $getmagic])
199 DDual will return the basic elements in a variable, guaranteeing that no
200 conversion takes place. This is very useful for dual-var variables, or
201 when checking is a variable has defined entries for a certain type of
202 scalar. For each Integer (IV), Double (NV), String (PV), and Reference (RV),
203 the current value of C<$var> is returned or undef if it is not set (yet).
204 The 5th element is an indicator if C<$var> has magic, which is B<not> invoked
205 in the returned values, unless explicitly asked for with a true optional
210 print DPeek for DDual ($!, 1);
212 =head3 DDump ($var [, $dig_level])
214 A very useful module when debugging is C<Devel::Peek>, but is has one big
215 disadvantage: it only prints to STDERR, which is not very handy when your
216 code wants to inspect variables al a low level.
218 Perl itself has C<sv_dump ()>, which does something similar, but still
219 prints to STDERR, and only one level deep.
221 C<DDump ()> is an attempt to make the innards available to the script level
222 with a reasonable level of compatibility. C<DDump ()> is context sensitive.
224 In void context, it behaves exactly like C<Perl_sv_dump ()>.
226 In scalar context, it returns what C<Perl_sv_dump ()> would have printed.
228 In list context, it returns a hash of the variable's properties. In this mode
229 you can pass an optional second argument that determines the depth of digging.
233 print scalar DDump "abc\x{0a}de\x{20ac}fg"
235 SV = PV(0x723250) at 0x8432b0
237 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8)
238 PV = 0x731ac0 "abc\nde\342\202\254fg"\0 [UTF8 "abc\nde\x{20ac}fg"]
242 my %h = DDump "abc\x{0a}de\x{20ac}fg";
255 PV => '0x731ac0 "abc\\nde\\342\\202\\254fg"\\0 [UTF8 "abc\\nde\\x{20ac}fg"]',
257 sv => 'PV(0x723250) at 0x8432c0'
263 bar => [ 2, "baz", undef ],
284 sv => 'IV(0x747020) at 0x843a10'
298 sv => 'PVIV(0x7223e0) at 0x843a10'
310 PV => '0x7496c0 "egg"\\0',
312 sv => 'PVIV(0x7223e0) at 0x843a10'
315 sv => 'RV(0x79d058) at 0x843310'
318 =head2 DDump_IO ($io, $var [, $dig_level])
320 A wrapper function around perl's internal C<Perl_do_sv_dump ()>, which
321 makes C<Devel::Peek> completely superfluous. As PerlIO is only available
322 perl version 5.7.3 and up, this function is not available in older perls.
327 open my $eh, ">", \$dump;
328 DDump_IO ($eh, { 3 => 4, ape => [5..8]}, 6);
332 SV = RV(0x79d9e0) at 0x843f00
336 SV = PVHV(0x79c948) at 0x741090
341 ARRAY = 0x748ff0 (0:7, 2:1)
348 Elt "ape" HASH = 0x97623e03
349 SV = RV(0x79d9d8) at 0x8440e0
353 SV = PVAV(0x7264b0) at 0x741470
364 SV = IV(0x7467c8) at 0x7c1aa0
369 SV = IV(0x7467b0) at 0x8440f0
374 SV = IV(0x746810) at 0x75be00
379 SV = IV(0x746d38) at 0x7799d0
383 Elt "3" HASH = 0xa400c7f3
384 SV = IV(0x746fd0) at 0x7200e0
391 C<DDump ()> uses an XS wrapper around C<Perl_sv_dump ()> where the
392 STDERR is temporarily caught to a pipe. The internal XS helper functions
393 are not meant for user space
395 =head2 DDump_XS (SV *sv)
397 Base interface to internals for C<DDump ()>.
401 Not all types of references are supported.
405 No idea how far back this goes in perl support.
409 L<Devel::Peek(3)>, L<Data::Dumper(3)>, L<Data::Dump(3)>,
410 L<Data::Dump::Streamer(3)>
414 H.Merijn Brand <h.m.brand@xs4all.nl>
416 =head1 COPYRIGHT AND LICENSE
418 Copyright (C) 2008-2008 H.Merijn Brand
420 This library is free software; you can redistribute it and/or modify
421 it under the same terms as Perl itself.