1Data::Dumper::Names(3)User Contributed Perl DocumentationData::Dumper::Names(3)
2
3
4
6 Data::Dumper::Names - Dump variables with names (no source filter)
7
9 Version 0.03
10
12 use Data::Dumper::Names;
13
14 my $foo = 3;
15 my @bar = qw/this that/;
16 warn Dumper($foo, \@bar);
17 __END__
18 output:
19
20 $foo = 3;
21 @bar = (
22 'this',
23 'that'
24 );
25
27 Like Data::Dumper, this module automatically exports Dumper() unless a
28 null import list is explicitly stated.
29
30 This module should be considered ALPHA.
31
33 Dumper
34 warn Dumper($foo, \@bar);
35
36 "Dumper" returns a string like "Dumper" but the variable names are
37 prefixed for you. Unlike Data::Dumper::Simple, arrays and hashes must
38 be passed by reference.
39
41 PadWalker
42 This module is an alternative to Data::Dumper::Simple. Many people
43 like the aforementioned module but do not like the fact that it uses a
44 source filter. In order to pull off the trick with this module, we use
45 PadWalker. This introduces its own set of problems, not the least of
46 which is that PadWalker uses undocumented features of the Perl
47 internals and has an annoying tendency to break. Thus, if this module
48 doesn't work on your machine you may have to go back to
49 Data::Dumper::Simple.
50
51 References
52 Arrays and hashes, unlike in Data::Dumper::Simple, must be passed by
53 reference. Unfortunately, this causes a problem:
54
55 my $foo = \@array;
56 warn Dumper( $foo, \@array );
57
58 Because of how pads work, there is no easy way to disambiguate between
59 these two variables. Thus, "Dumper" may identify them as $foo or it
60 may identify them as @array. If it misidentifies them, it should at
61 least do so consistently for the individual call to "Dumper". (For
62 Perl 5.8 and after, subsequent calls to "Dumper" may have different
63 results in the above case. This is because of how Perl handles hash
64 ordering).
65
66 Call stack level
67 You generally will call things with this:
68
69 warn Dumper($foo, $bar, \@baz);
70
71 However, you might be refactoring code and want to shove that into a
72 subroutine somewhere. In that case, you'll need to set (via "local"!),
73 the $Data::Dumper::Names::UpLevel variable. It defaults to one, but
74 you might set it to a higher level, depending on how high up the call
75 stack those variables are really located:
76
77 sub show {
78 return unless $ENV{VERBOSE};
79 local $Data::Dumper::Names::UpLevel = 2;
80 warn Dumper(@_);
81 }
82
83 Note that if you fail to use "local", subsequent calls to "Dumper" may
84 be looking at the wrong call stack level.
85
86 Unknown Variables
87 The easiest way to have things "just work" is to make sure that you can
88 see the name of the variable in the "Dumper" call:
89
90 warn Dumper($foo, \@bar); # good
91 warn Dumper($_); # probably will get output like $VAR1 = ...
92 warn Dumper($bar[2]); # probably will get output like $VAR1 = ...
93
94 Usually the output from Dumper will be something like this:
95
96 $foo = 3;
97 @bar = (
98 'this',
99 'that'
100 );
101
102 However, sometimes a $VAR1 or $VAR2 will creep in there. This happens
103 if pass in anything but a named variable. For example:
104
105 warn Dumper( $bar[2] ); # $VAR1 = ... can't figure out the name
106
107 We probably won't be able to figure out the name of the variable
108 directly unless we took the time to walk all data structures in scope
109 at the time "Dumper" is called. This is an expensive proposition, so
110 we don't do that. It's possible we will be able to figure out that
111 name, but only if the variable was assigned its value from a reference
112 to a named variable.
113
114 $bar[2] = \%foo;
115 warn Dumper( $bar[2] );
116
117 "Dumper", in the above example, will identify that variable as being
118 %foo. That could be confusing if those lines are far apart.
119
120 foreach ( @customer ) {
121 print Dumper( $_ );
122 }
123
124 It should go without saying that the above will also probably not be
125 able to name the variables.
126
128 Curtis "Ovid" Poe, "<ovid@cpan.org>"
129
131 Please report any bugs or feature requests to
132 "bug-data-dumper-names@rt.cpan.org", or through the web interface at
133 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Dumper-Names>. I
134 will be notified, and then you'll automatically be notified of progress
135 on your bug as I make changes.
136
138 See Data::Dumper and Data::Dumper::Simple.
139
140 Thanks to demerphq (Yves Orton) for finding a bug in how some variable
141 names are reported. See Changes for details.
142
144 Copyright 2005 Curtis "Ovid" Poe, all rights reserved.
145
146 This program is free software; you can redistribute it and/or modify it
147 under the same terms as Perl itself.
148
149
150
151perl v5.36.0 2023-01-20 Data::Dumper::Names(3)