Old(er) perls (e.g. 5.8.0) do not like \my $x, more
[Data-Peek.git] / Peek.pm
blob0692ddcc20d037f8380248c74f488adf3e775451
1 package Data::Peek;
3 use strict;
4 use warnings;
6 use DynaLoader ();
8 use vars qw( $VERSION @ISA @EXPORT @EXPORT_OK );
9 $VERSION = "0.40";
10 @ISA = qw( DynaLoader Exporter );
11 @EXPORT = qw( DDumper DTidy DDsort DPeek DDisplay DDump DHexDump
12 DDual DGrow );
13 @EXPORT_OK = qw( triplevar :tidy );
14 push @EXPORT, "DDump_IO";
16 bootstrap Data::Peek $VERSION;
18 our $has_perlio;
19 our $has_perltidy;
21 BEGIN {
22 use Config;
23 $has_perlio = ($Config{useperlio} || "undef") eq "define";
24 $has_perltidy = eval q{use Perl::Tidy; $Perl::Tidy::VERSION};
27 ### ############# DDumper () ##################################################
29 use Data::Dumper;
31 my %sk = (
32 undef => 0,
33 "" => 0,
34 0 => 0,
35 1 => 1,
37 V => sub { # Sort by value
38 my $r = shift;
39 [ sort { $r->{$a} cmp $r->{$b} } keys %$r ];
41 VN => sub { # Sort by value numeric
42 my $r = shift;
43 [ sort { $r->{$a} <=> $r->{$b} } keys %$r ];
45 VNR => sub { # Sort by value numeric reverse
46 my $r = shift;
47 [ sort { $r->{$b} <=> $r->{$a} } keys %$r ];
49 VR => sub { # Sort by value reverse
50 my $r = shift;
51 [ sort { $r->{$b} cmp $r->{$a} } keys %$r ];
53 R => sub { # Sort reverse
54 my $r = shift;
55 [ reverse sort keys %$r ];
58 my $_sortkeys = 1;
59 our $_perltidy = 0;
61 sub DDsort
63 @_ or return;
65 $_sortkeys = exists $sk{$_[0]} ? $sk{$_[0]} : $_[0];
66 } # DDsort
68 sub import
70 my @exp = @_;
71 my @etl;
72 foreach my $p (@exp) {
73 exists $sk{$p} and DDsort ($p), next;
75 if ($p eq ":tidy") {
76 $_perltidy = $has_perltidy;
77 next;
80 push @etl, $p;
82 __PACKAGE__->export_to_level (1, @etl);
83 } # import
85 sub DDumper
87 $_perltidy and goto \&DTidy;
89 local $Data::Dumper::Sortkeys = $_sortkeys;
90 local $Data::Dumper::Indent = 1;
91 local $Data::Dumper::Quotekeys = 0;
92 local $Data::Dumper::Deparse = 1;
93 local $Data::Dumper::Terse = 1;
94 local $Data::Dumper::Useqq = 0; # I want unicode visible
96 my $s = Data::Dumper::Dumper @_;
97 $s =~ s/^(\s*)(.*?)\s*=>/sprintf "%s%-16s =>", $1, $2/gme; # Align =>
98 $s =~ s/\bbless\s*\(\s*/bless (/gm and $s =~ s/\s+\)([;,])$/)$1/gm;
99 $s =~ s/^(?=\s*[]}](?:[;,]|$))/ /gm;
100 $s =~ s/^(\s*[{[]) *\n *(?=\S)(?![{[])/$1 /gm;
101 $s =~ s/^(\s+)/$1$1/gm;
103 defined wantarray or warn $s;
104 return $s;
105 } # DDumper
107 sub DTidy
109 $has_perltidy or goto \&DDumper;
111 local $Data::Dumper::Sortkeys = $_sortkeys;
112 local $Data::Dumper::Indent = 1;
113 local $Data::Dumper::Quotekeys = 1;
114 local $Data::Dumper::Deparse = 1;
115 local $Data::Dumper::Terse = 1;
116 local $Data::Dumper::Useqq = 0;
118 my $s = Data::Dumper::Dumper @_;
119 my $t;
120 Perl::Tidy::perltidy (source => \$s, destination => \$t, argv => [
121 # Disable stupid options in ~/.perltidyrc
122 # people do so, even for root
123 "--no-backup-and-modify-in-place",
124 "--no-check-syntax",
125 "--no-standard-output",
126 "--no-warning-output",
128 $s = $t;
130 defined wantarray or warn $s;
131 return $s;
132 } # DTidy
134 ### ############# DDump () ####################################################
136 sub _DDump_ref
138 my (undef, $down) = (@_, 0);
140 my $ref = ref $_[0];
141 if ($ref eq "SCALAR" || $ref eq "REF") {
142 my %hash = DDump (${$_[0]}, $down);
143 return { %hash };
145 if ($ref eq "ARRAY") {
146 my @list;
147 foreach my $list (@{$_[0]}) {
148 my %hash = DDump ($list, $down);
149 push @list, { %hash };
151 return [ @list ];
153 if ($ref eq "HASH") {
154 my %hash;
155 foreach my $key (sort keys %{$_[0]}) {
156 $hash{DPeek ($key)} = { DDump ($_[0]->{$key}, $down) };
158 return { %hash };
160 undef;
161 } # _DDump_ref
163 sub _DDump
165 my (undef, $down, $dump, $fh) = (@_, "");
167 if ($has_perlio and open $fh, ">", \$dump) {
168 #print STDERR "Using DDump_IO\n";
169 DDump_IO ($fh, $_[0], $down);
170 close $fh;
172 else {
173 #print STDERR "Using DDump_XS\n";
174 $dump = DDump_XS ($_[0]);
177 return $dump;
178 } # _DDump
180 sub DDump ($;$)
182 my (undef, $down) = (@_, 0);
183 my @dump = split m/[\r\n]+/, _DDump ($_[0], wantarray || $down) or return;
185 if (wantarray) {
186 my %hash;
187 ($hash{sv} = $dump[0]) =~ s/^SV\s*=\s*//;
188 m/^\s+(\w+)\s*=\s*(.*)/ and $hash{$1} = $2 for @dump;
190 if (exists $hash{FLAGS}) {
191 $hash{FLAGS} =~ tr/()//d;
192 $hash{FLAGS} = { map { $_ => 1 } split m/,/ => $hash{FLAGS} };
195 $down && ref $_[0] and
196 $hash{RV} = _DDump_ref ($_[0], $down - 1) || $_[0];
197 return %hash;
200 my $dump = join "\n", @dump, "";
202 defined wantarray and return $dump;
204 warn $dump;
205 } # DDump
207 sub DHexDump
209 use bytes;
210 my $off = 0;
211 my @out;
212 my $var = @_ ? $_[0] : $_;
213 defined $var or return;
214 my $fmt = @_ > 1 && $_[1] < length ($var) ? "A$_[1]" : "A*";
215 my $str = pack $fmt, $var; # force stringification
216 for (unpack "(A32)*", unpack "H*", $str) {
217 my @b = unpack "(A2)*", $_;
218 my $out = sprintf "%04x ", $off;
219 $out .= " ".($b[$_]||" ") for 0 .. 7;
220 $out .= " ";
221 $out .= " ".($b[$_]||" ") for 8 .. 15;
222 $out .= " ";
223 $out .= ($_ < 0x20 || $_ >= 0x7f ? "." : chr $_) for map { hex $_ } @b;
224 push @out, $out."\n";
225 $off += 16;
228 wantarray and return @out;
230 defined wantarray and return join "", @out;
232 warn join "", @out;
233 } # DHexDump
235 "Indent";
237 __END__
239 =head1 NAME
241 Data::Peek - A collection of low-level debug facilities
243 =head1 SYNOPSIS
245 use Data::Peek;
247 print DDumper \%hash; # Same syntax as Data::Dumper
248 DTidy { ref => $ref };
250 print DPeek \$var;
251 my ($pv, $iv, $nv, $rv, $magic) = DDual ($var [, 1]);
252 print DPeek for DDual ($!, 1);
253 print DDisplay ("ab\nc\x{20ac}\rdef\n");
254 print DHexDump ("ab\nc\x{20ac}\rdef\n");
256 my $dump = DDump $var;
257 my %hash = DDump \@list;
258 DDump \%hash;
260 my %hash = DDump (\%hash, 5); # dig 5 levels deep
262 my $dump;
263 open my $fh, ">", \$dump;
264 DDump_IO ($fh, \%hash, 6);
265 close $fh;
266 print $dump;
268 # Imports
269 use Data::Peek qw( :tidy VNR DGrow triplevar );
270 my $x = ""; DGrow ($x, 10000);
271 my $tv = triplevar ("\N{GREEK SMALL LETTER PI}", 3, "3.1415");
272 DDsort ("R");
273 DDumper [ $x ]; # use of :tidy make DDumper behave as DTidy
275 =head1 DESCRIPTION
277 Data::Peek started off as C<DDumper> being a wrapper module over
278 L<Data::Dumper>, but grew out to be a set of low-level data
279 introspection utilities that no other module provided yet, using the
280 lowest level of the perl internals API as possible.
282 =head2 DDumper ($var, ...)
284 Not liking the default output of Data::Dumper, and always feeling the need
285 to set C<$Data::Dumper::Sortkeys = 1;>, and not liking any of the default
286 layouts, this function is just a wrapper around Data::Dumper::Dumper with
287 everything set as I like it.
289 $Data::Dumper::Sortkeys = 1;
290 $Data::Dumper::Indent = 1;
292 If C<Data::Peek> is C<use>d with import argument C<:tidy>, the result is
293 formatted according to L<Perl::Tidy>, see L<DTidy> below, otherwise the
294 result is further beautified to meet my needs:
296 * quotation of hash keys has been removed (with the disadvantage
297 that the output might not be parseable again).
298 * arrows for hashes are aligned at 16 (longer keys don't align)
299 * closing braces and brackets are now correctly aligned
301 In void context, C<DDumper ()> warn ()'s.
303 Example
305 $ perl -MDP \
306 -e'DDumper { ape => 1, foo => "egg", bar => [ 2, "baz", undef ]};'
308 { ape => 1,
309 bar => [
311 'baz',
312 undef
314 foo => 'egg'
317 =head2 DTidy ($var, ...)
319 C<DTidy> is an alternative to C<DDumper>, where the output of C<DDumper>
320 is formatted using C<Perl::Tidy> (if available) according to your
321 C<.perltidyrc> instead of the default behavior, maybe somewhat like (YMMV):
323 $ perl -MDP=:tidy \
324 -we'DDumper { ape => 1, foo => "egg", bar => [ 2, "baz", undef ]};'
325 { 'ape' => 1,
326 'bar' => [2, 'baz', undef],
327 'foo' => 'egg'
330 If C<Data::Peek> is C<use>d with import argument C<:tidy>, this is the
331 default output method for C<DDumper>.
333 If L<Perl::Tidy> is not available, C<DTidy> will fallback to C<DDumper>.
335 This idea was shamelessly copied from John McNamara's L<Data::Dumper::Perltidy>.
337 =head2 DDsort ( 0 | 1 | R | V | VR | VN | VNR )
339 Set the hash sort algorithm for DDumper. The default is to sort by key value.
341 0 - Do not sort
342 1 - Sort by key
343 R - Reverse sort by key
344 V - Sort by value
345 VR - Reverse sort by value
346 VN - Sort by value numerical
347 VNR - Reverse sort by value numerical
349 These can also be passed to import:
351 $ perl -MDP=VNR \
352 -we'DDumper { foo => 1, bar => 2, zap => 3, gum => 13 }'
353 { gum => 13,
354 zap => 3,
355 bar => 2,
356 foo => 1
358 $ perl -MDP=V \
359 -we'DDumper { foo => 1, bar => 2, zap => 3, gum => 13 }'
360 { foo => 1,
361 gum => 13,
362 bar => 2,
363 zap => 3
366 =head2 DPeek
368 =head2 DPeek ($var)
370 Playing with C<sv_dump ()>, I found C<Perl_sv_peek ()>, and it might be
371 very useful for simple checks. If C<$var> is omitted, uses $_.
373 Example
375 print DPeek "abc\x{0a}de\x{20ac}fg";
377 PV("abc\nde\342\202\254fg"\0) [UTF8 "abc\nde\x{20ac}fg"]
379 In void context, C<DPeek ()> prints to C<STDERR> plus a newline.
381 =head2 DDisplay
383 =head2 DDisplay ($var)
385 Show the PV content of a scalar the way perl debugging would have done.
386 UTF-8 detection is on, so this is effectively the same as returning the
387 first part the C<DPeek ()> returns for non-UTF8 PV's or the second part
388 for UTF-8 PV's. C<DDisplay ()> returns the empty string for scalars that
389 no have a valid PV.
391 Example
393 print DDisplay "abc\x{0a}de\x{20ac}fg";
395 "abc\nde\x{20ac}fg"
397 =head2 DHexDump
399 =head2 DHexDump ($var)
401 =head2 DHexDump ($var, $length)
403 Show the (stringified) content of a scalar as a hex-dump. If C<$var>
404 is omitted, C<$_> is dumped. Returns C<undef> or an empty list if
405 C<$var> (or C<$_>) is undefined. If C<$length> is given and is lower than
406 the length of the stringified C<$var>, only <$length> bytes are dumped.
408 In void context, the dump is done to STDERR. In scalar context, the
409 complete dump is returned as a single string. In list context, the dump
410 is returned as lines.
412 Example
414 print DHexDump "abc\x{0a}de\x{20ac}fg";
416 0000 61 62 63 0a 64 65 e2 82 ac 66 67 abc.de...fg
418 =head2 my ($pv, $iv, $nv, $rv, $hm) = DDual ($var [, $getmagic])
420 DDual will return the basic elements in a variable, guaranteeing that no
421 conversion takes place. This is very useful for dual-var variables, or
422 when checking is a variable has defined entries for a certain type of
423 scalar. For each String (PV), Integer (IV), Double (NV), and Reference (RV),
424 the current value of C<$var> is returned or undef if it is not set (yet).
425 The 5th element is an indicator if C<$var> has magic, which is B<not> invoked
426 in the returned values, unless explicitly asked for with a true optional
427 second argument.
429 Example
431 print DPeek for DDual ($!, 1);
433 In void context, DDual does the equivalent of
435 { my @d = DDual ($!, 1);
436 print STDERR
437 DPeek ($!), "\n",
438 " PV: ", DPeek ($d[0]), "\n",
439 " IV: ", DPeek ($d[1]), "\n",
440 " NV: ", DPeek ($d[2]), "\n",
441 " RV: ", DPeek ($d[3]), "\n";
444 =head2 my $len = DGrow ($pv, $size)
446 Fastest way to preallocate space for a PV scalar. Returns the allocated
447 length. If $size is smaller than the already allocated space, it will
448 not shrink.
450 cmpthese (-2, {
451 pack => q{my $x = ""; $x = pack "x20000"; $x = "";},
452 op_x => q{my $x = ""; $x = "x" x 20000; $x = "";},
453 grow => q{my $x = ""; DGrow ($x, 20000); $x = "";},
456 Rate op_x pack grow 5.8.9 5.10.1 5.12.4 5.14.2
457 op_x 62127/s -- -59% -96% 118606/s 119730/s 352255/s 362605/s
458 pack 152046/s 145% -- -91% 380075/s 355666/s 347247/s 387349/s
459 grow 1622943/s 2512% 967% -- 2818380/s 2918783/s 2672340/s 2886787/s
461 =head2 my $tp = triplevar ($pv, $iv, $nv)
463 When making C<DDual ()> I wondered if it were possible to create triple-val
464 scalar variables. L<Scalar::Util> already gives us C<dualvar ()>, that creates
465 you a scalar with different numeric and string values that return different
466 values in different context. Not that C<triplevar ()> would be very useful,
467 compared to C<dualvar ()>, but at least this shows that it is possible.
469 C<triplevar ()> is not exported by default.
471 Example:
473 print DPeek for DDual
474 Data::Peek::triplevar ("\N{GREEK SMALL LETTER PI}", 3, 3.1415);
476 PV("\317\200"\0) [UTF8 "\x{3c0}"]
477 IV(3)
478 NV(3.1415)
479 SV_UNDEF
480 IV(0)
482 =head2 DDump ($var [, $dig_level])
484 A very useful module when debugging is C<Devel::Peek>, but is has one big
485 disadvantage: it only prints to STDERR, which is not very handy when your
486 code wants to inspect variables at a low level.
488 Perl itself has C<sv_dump ()>, which does something similar, but still
489 prints to STDERR, and only one level deep.
491 C<DDump ()> is an attempt to make the innards available to the script level
492 with a reasonable level of compatibility. C<DDump ()> is context sensitive.
494 In void context, it behaves exactly like C<Perl_sv_dump ()>.
496 In scalar context, it returns what C<Perl_sv_dump ()> would have printed.
498 In list context, it returns a hash of the variable's properties. In this mode
499 you can pass an optional second argument that determines the depth of digging.
501 Example
503 print scalar DDump "abc\x{0a}de\x{20ac}fg"
505 SV = PV(0x723250) at 0x8432b0
506 REFCNT = 1
507 FLAGS = (PADBUSY,PADMY,POK,pPOK,UTF8)
508 PV = 0x731ac0 "abc\nde\342\202\254fg"\0 [UTF8 "abc\nde\x{20ac}fg"]
509 CUR = 11
510 LEN = 16
512 my %h = DDump "abc\x{0a}de\x{20ac}fg";
513 print DDumper \%h;
515 { CUR => '11',
516 FLAGS => {
517 PADBUSY => 1,
518 PADMY => 1,
519 POK => 1,
520 UTF8 => 1,
521 pPOK => 1
523 LEN => '16',
524 PV => '0x731ac0 "abc\\nde\\342\\202\\254fg"\\0 [UTF8 "abc\\nde\\x{20ac}fg"]',
525 REFCNT => '1',
526 sv => 'PV(0x723250) at 0x8432c0'
529 my %h = DDump {
530 ape => 1,
531 foo => "egg",
532 bar => [ 2, "baz", undef ],
533 }, 1;
534 print DDumper \%h;
536 { FLAGS => {
537 PADBUSY => 1,
538 PADMY => 1,
539 ROK => 1
541 REFCNT => '1',
542 RV => {
543 PVIV("ape") => {
544 FLAGS => {
545 IOK => 1,
546 PADBUSY => 1,
547 PADMY => 1,
548 pIOK => 1
550 IV => '1',
551 REFCNT => '1',
552 sv => 'IV(0x747020) at 0x843a10'
554 PVIV("bar") => {
555 CUR => '0',
556 FLAGS => {
557 PADBUSY => 1,
558 PADMY => 1,
559 ROK => 1
561 IV => '1',
562 LEN => '0',
563 PV => '0x720210 ""',
564 REFCNT => '1',
565 RV => '0x720210',
566 sv => 'PVIV(0x7223e0) at 0x843a10'
568 PVIV("foo") => {
569 CUR => '3',
570 FLAGS => {
571 PADBUSY => 1,
572 PADMY => 1,
573 POK => 1,
574 pPOK => 1
576 IV => '1',
577 LEN => '8',
578 PV => '0x7496c0 "egg"\\0',
579 REFCNT => '1',
580 sv => 'PVIV(0x7223e0) at 0x843a10'
583 sv => 'RV(0x79d058) at 0x843310'
586 =head2 DDump_IO ($io, $var [, $dig_level])
588 A wrapper function around perl's internal C<Perl_do_sv_dump ()>, which
589 makes C<Devel::Peek> completely superfluous.
591 Example
593 my $dump;
594 open my $eh, ">", \$dump;
595 DDump_IO ($eh, { 3 => 4, ape => [5..8]}, 6);
596 close $eh;
597 print $dump;
599 SV = RV(0x79d9e0) at 0x843f00
600 REFCNT = 1
601 FLAGS = (TEMP,ROK)
602 RV = 0x741090
603 SV = PVHV(0x79c948) at 0x741090
604 REFCNT = 1
605 FLAGS = (SHAREKEYS)
606 IV = 2
607 NV = 0
608 ARRAY = 0x748ff0 (0:7, 2:1)
609 hash quality = 62.5%
610 KEYS = 2
611 FILL = 1
612 MAX = 7
613 RITER = -1
614 EITER = 0x0
615 Elt "ape" HASH = 0x97623e03
616 SV = RV(0x79d9d8) at 0x8440e0
617 REFCNT = 1
618 FLAGS = (ROK)
619 RV = 0x741470
620 SV = PVAV(0x7264b0) at 0x741470
621 REFCNT = 2
622 FLAGS = ()
623 IV = 0
624 NV = 0
625 ARRAY = 0x822f70
626 FILL = 3
627 MAX = 3
628 ARYLEN = 0x0
629 FLAGS = (REAL)
630 Elt No. 0
631 SV = IV(0x7467c8) at 0x7c1aa0
632 REFCNT = 1
633 FLAGS = (IOK,pIOK)
634 IV = 5
635 Elt No. 1
636 SV = IV(0x7467b0) at 0x8440f0
637 REFCNT = 1
638 FLAGS = (IOK,pIOK)
639 IV = 6
640 Elt No. 2
641 SV = IV(0x746810) at 0x75be00
642 REFCNT = 1
643 FLAGS = (IOK,pIOK)
644 IV = 7
645 Elt No. 3
646 SV = IV(0x746d38) at 0x7799d0
647 REFCNT = 1
648 FLAGS = (IOK,pIOK)
649 IV = 8
650 Elt "3" HASH = 0xa400c7f3
651 SV = IV(0x746fd0) at 0x7200e0
652 REFCNT = 1
653 FLAGS = (IOK,pIOK)
654 IV = 4
656 =head1 INTERNALS
658 C<DDump ()> uses an XS wrapper around C<Perl_sv_dump ()> where the
659 STDERR is temporarily caught to a pipe. The internal XS helper functions
660 are not meant for user space
662 =head2 DDump_XS (SV *sv)
664 Base interface to internals for C<DDump ()>.
666 =head1 BUGS
668 Windows and AIX might be using a build where not all symbols that were
669 supposed to be exported in the public API are not. Perl_pv_peek () is
670 one of them.
672 Not all types of references are supported.
674 No idea how far back this goes in perl support, but Devel::PPPort has
675 proven to be a big help.
677 =head1 SEE ALSO
679 L<Devel::Peek>, L<Data::Dumper>, L<Data::Dump>, L<Devel::Dumpvar>,
680 L<Data::Dump::Streamer>, L<Data::Dumper::Perltidy>, L<Perl::Tidy>.
682 =head1 AUTHOR
684 H.Merijn Brand <h.m.brand@xs4all.nl>
686 =head1 COPYRIGHT AND LICENSE
688 Copyright (C) 2008-2014 H.Merijn Brand
690 This library is free software; you can redistribute it and/or modify
691 it under the same terms as Perl itself.
693 =cut