| 1 | package Data::Dumper::Names; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | use Data::Dumper (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use Scalar::Util 'refaddr'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use PadWalker 'peek_my'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | our @EXPORT = qw/Dumper/; | |||||||||
| 11 | 4 | 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 | 4 | 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<Data::Dumper>, this module automatically exports C<Dumper()> | ||||||||||
| 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<Dumper> returns a string like C<Dumper> but the variable names are prefixed | ||||||||||
| 55 | for you. Unlike L<Data::Dumper::Simple>, arrays and hashes must be passed by | ||||||||||
| 56 | reference. | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | 10 | ▶ | sub Dumper { | ||||||||
|
Subroutine
Dumper: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 61 | 10 | my $pad = peek_my($UpLevel); | |||||||||
| 62 | 10 | my %pad_vars; | |||||||||
| 63 | 10 | ▶ | while ( my ( $var, $ref ) = each %$pad ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | # we no longer remove the '$' sigil because we don't want | ||||||||||
| 66 | # "$foo = \@array" reported as "@foo". | ||||||||||
| 67 | 61 | $var =~ s/^[\@\%]/*/; | |||||||||
| 68 | 61 | $pad_vars{ refaddr $ref } = $var; | |||||||||
| 69 | } | ||||||||||
| 70 | 10 | my @names; | |||||||||
| 71 | 10 | my $varcount = 1; | |||||||||
| 72 | 10 | foreach (@_) { | |||||||||
| 73 | 18 | my $name; | |||||||||
| 74 | 18 | INNER: foreach ( \$_, $_ ) { | |||||||||
| 75 | 4 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 76 | 29 | ▶ | $name = $pad_vars{ refaddr $_} and last INNER; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | } | ||||||||||
| 78 | 18 | push @names, $name; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 10 | return Data::Dumper->Dump( \@_, \@names ); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | =head1 CAVEATS | ||||||||||
| 85 | |||||||||||
| 86 | =head2 PadWalker | ||||||||||
| 87 | |||||||||||
| 88 | This module is an alternative to L<Data::Dumper::Simple>. 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<PadWalker>. | ||||||||||
| 91 | This introduces its own set of problems, not the least of which is that | ||||||||||
| 92 | L<PadWalker> 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<Data::Dumper::Simple>. | ||||||||||
| 95 | |||||||||||
| 96 | =head2 References | ||||||||||
| 97 | |||||||||||
| 98 | Arrays and hashes, unlike in L<Data::Dumper::Simple>, 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<Dumper> 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<Dumper>. (For Perl 5.8 and | ||||||||||
| 108 | after, subsequent calls to C<Dumper> 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<local>!), 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<local>, subsequent calls to C<Dumper> 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<Dumper> 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<Dumper> 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<but> 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<Dumper> is called. This is an expensive proposition, so we don't do that. | ||||||||||
| 157 | It's possible we I<will> 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<Dumper>, 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<< <ovid@cpan.org> >> | ||||||||||
| 176 | |||||||||||
| 177 | =head1 BUGS | ||||||||||
| 178 | |||||||||||
| 179 | Please report any bugs or feature requests to | ||||||||||
| 180 | C<bug-data-dumper-names@rt.cpan.org>, or through the web interface at | ||||||||||
| 181 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Dumper-Names>. | ||||||||||
| 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<Data::Dumper> and L<Data::Dumper::Simple>. | ||||||||||
| 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 | 4 | 1; # End of Data::Dumper::Names | |||||||||