File Coverage

blib/lib/Data/Dumper/Names.pm
Criterion Covered Total %
statement 34 34 100.0
branch 2 2 100.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 45 45 100.0


line stmt bran cond sub pod time code
1             package Data::Dumper::Names;
2              
3 3     3   96579 use warnings;
  3         7  
  3         98  
4 3     3   15 use strict;
  3         7  
  3         101  
5              
6 3     3   5112 use Data::Dumper ();
  3         33763  
  3         85  
7 3     3   28 use Scalar::Util 'refaddr';
  3         5  
  3         3142  
8 3     3   5355 use PadWalker 'peek_my';
  3         4610  
  3         226  
9 3     3   22 use base 'Exporter';
  3         6  
  3         10708  
10             our @EXPORT = qw/Dumper/;
11             our $UpLevel = 1;
12              
13             =head1 NAME
14              
15             Data::Dumper::Names - Dump variables with names (no source filter)
16              
17             =head1 VERSION
18              
19             Version 0.03
20              
21             =cut
22              
23             our $VERSION = '0.03';
24              
25             =head1 SYNOPSIS
26              
27             use Data::Dumper::Names;
28              
29             my $foo = 3;
30             my @bar = qw/this that/;
31             warn Dumper($foo, \@bar);
32             __END__
33             output:
34            
35             $foo = 3;
36             @bar = (
37             'this',
38             'that'
39             );
40              
41             =head1 EXPORT
42              
43             Like L, this module automatically exports C
44             unless a null import list is explicitly stated.
45              
46             This module should be considered ALPHA.
47              
48             =head1 FUNCTIONS
49              
50             =head2 Dumper
51              
52             warn Dumper($foo, \@bar);
53              
54             C returns a string like C but the variable names are prefixed
55             for you. Unlike L, arrays and hashes must be passed by
56             reference.
57              
58             =cut
59              
60             sub Dumper {
61 10     10 1 11488 my $pad = peek_my($UpLevel);
62 10         20 my %pad_vars;
63 10         43 while ( my ( $var, $ref ) = each %$pad ) {
64              
65             # we no longer remove the '$' sigil because we don't want
66             # "$foo = \@array" reported as "@foo".
67 61         113 $var =~ s/^[\@\%]/*/;
68 61         300 $pad_vars{ refaddr $ref } = $var;
69             }
70 10         11 my @names;
71 10         13 my $varcount = 1;
72 10         20 foreach (@_) {
73 18         19 my $name;
74 18         25 INNER: foreach ( \$_, $_ ) {
75 3     3   27 no warnings 'uninitialized';
  3         5  
  3         525  
76 29 100       103 $name = $pad_vars{ refaddr $_} and last INNER;
77             }
78 18         42 push @names, $name;
79             }
80              
81 10         48 return Data::Dumper->Dump( \@_, \@names );
82             }
83              
84             =head1 CAVEATS
85              
86             =head2 PadWalker
87              
88             This module is an alternative to L. Many people like
89             the aforementioned module but do not like the fact that it uses a source
90             filter. In order to pull off the trick with this module, we use L.
91             This introduces its own set of problems, not the least of which is that
92             L uses undocumented features of the Perl internals and has an
93             annoying tendency to break. Thus, if this module doesn't work on your
94             machine you may have to go back to L.
95              
96             =head2 References
97              
98             Arrays and hashes, unlike in L, must be passed by
99             reference. Unfortunately, this causes a problem:
100              
101             my $foo = \@array;
102             warn Dumper( $foo, \@array );
103              
104             Because of how pads work, there is no easy way to disambiguate between these
105             two variables. Thus, C may identify them as C<$foo> or it may
106             identify them as C<@array>. If it misidentifies them, it should at least do
107             so consistently for the individual call to C. (For Perl 5.8 and
108             after, subsequent calls to C may have different results in the above
109             case. This is because of how Perl handles hash ordering).
110              
111             =head2 Call stack level
112              
113             You generally will call things with this:
114              
115             warn Dumper($foo, $bar, \@baz);
116              
117             However, you might be refactoring code and want to shove that into a
118             subroutine somewhere. In that case, you'll need to set (via C!), the
119             $Data::Dumper::Names::UpLevel variable. It defaults to one, but you might set
120             it to a higher level, depending on how high up the call stack those variables
121             are really located:
122              
123             sub show {
124             return unless $ENV{VERBOSE};
125             local $Data::Dumper::Names::UpLevel = 2;
126             warn Dumper(@_);
127             }
128              
129             Note that if you fail to use C, subsequent calls to C may be
130             looking at the wrong call stack level.
131              
132             =head2 Unknown Variables
133              
134             The easiest way to have things "just work" is to make sure that you can
135             see the name of the variable in the C call:
136              
137             warn Dumper($foo, \@bar); # good
138             warn Dumper($_); # probably will get output like $VAR1 = ...
139             warn Dumper($bar[2]); # probably will get output like $VAR1 = ...
140              
141             Usually the output from L will be something like this:
142              
143             $foo = 3;
144             @bar = (
145             'this',
146             'that'
147             );
148              
149             However, sometimes a C<$VAR1> or C<$VAR2> will creep in there. This happens
150             if pass in anything I a named variable. For example:
151              
152             warn Dumper( $bar[2] ); # $VAR1 = ... can't figure out the name
153              
154             We probably won't be able to figure out the name of the variable directly
155             unless we took the time to walk all data structures in scope at the time
156             C is called. This is an expensive proposition, so we don't do that.
157             It's possible we I be able to figure out that name, but only if the
158             variable was assigned its value from a reference to a named variable.
159              
160             $bar[2] = \%foo;
161             warn Dumper( $bar[2] );
162              
163             C, in the above example, will identify that variable as being C<%foo>.
164             That could be confusing if those lines are far apart.
165              
166             foreach ( @customer ) {
167             print Dumper( $_ );
168             }
169              
170             It should go without saying that the above will also probably not be able to
171             name the variables.
172              
173             =head1 AUTHOR
174              
175             Curtis "Ovid" Poe, C<< >>
176              
177             =head1 BUGS
178              
179             Please report any bugs or feature requests to
180             C, or through the web interface at
181             L.
182             I will be notified, and then you'll automatically be notified of progress on
183             your bug as I make changes.
184              
185             =head1 ACKNOWLEDGEMENTS
186              
187             See L and L.
188              
189             Thanks to demerphq (Yves Orton) for finding a bug in how some variable names
190             are reported. See Changes for details.
191              
192             =head1 COPYRIGHT & LICENSE
193              
194             Copyright 2005 Curtis "Ovid" Poe, all rights reserved.
195              
196             This program is free software; you can redistribute it and/or modify it
197             under the same terms as Perl itself.
198              
199             =cut
200              
201             1; # End of Data::Dumper::Names