Variable dump

B

Bill H

Is there a way in perl to dump all the variables used and their
current values to a file without printing everyone of them
individually?

Bill H
 
A

anno4000

Bill H said:
Is there a way in perl to dump all the variables used and their
current values to a file without printing everyone of them
individually?

Not easily. You'd have a chance with package variables. For
lexicals you'd need something like PadWalker, which is a bit
tricky to use.

Most people settle for printing selected values at selected
places for debugging. The module Data::Dumper helps with
viewing complex data structures.

I have a module Report.pm in my private Perl library which
applies Data::Dumper to a list of Perl expressions. I find
that I never use it, but I'm appending it anyway. It uses
a little-known feature to evaluate expressions in the caller's
context.

Anno

--------------------------------------------------------------------
package Report;
use strict; use warnings; # @^~`
use base 'Exporter';
our @EXPORT = qw( report);

{
# evaluate in caller context
package DB;
sub _reporter_report {
for my $expr ( @_ ) {
print Report::answer( $expr, eval $expr);
}
}
}
*Report::report = \ &DB::_reporter_report;

use Data::Dumper ();
use Scalar::Util ();

sub answer {
my ( $expr, @val) = @_;
my $ans;
if ( $@ ) {
$ans = "$expr: $@" if $@;
$ans =~ s/ at \(eval .*$//;
} else {
if ( @val == 1 ) {
$ans = answer_scalar( $expr, @val);
} else {
$ans =join ', ' => map answer_scalar( $expr, $_), @val;
$ans = "($ans)";
}
}
$ans;
}

sub answer_scalar {
my ( $expr, $val) = @_;
my $ans;
if ( !defined $val ) {
$ans = "$expr = undef;\n";
} elsif ( ref $val ) {
( $ans = Data::Dumper::Dumper $val) =~ s/\$VAR1\b/$expr/g;
my $indent = ' ' x ( 8 + length( $expr) - length( '$VAR'));
$ans =~ s/^ {8}/$indent/mg;
} elsif (
Scalar::Util::looks_like_number( $val) or
ref( \ $val) eq 'GLOB'
) {
$ans = "$expr = $val;\n";
} else {
$ans = "$expr = '$val';\n";
}
$ans;
}

'@^~`'; # it's true
__END__

Use like this:

#!/usr/local/bin/perl
use strict; use warnings; $| = 1;

use Report;

my $x = 123;
my $y = [ qw( one two)];
my %z = (
hihi => [ 456, 789],
haha => { hoho => 'huhu'},
);

report qw( $x $y $z{haha});
 
S

Sisyphus

Bill H said:
Is there a way in perl to dump all the variables used and their
current values to a file without printing everyone of them
individually?

Closest I could find is PadWalker (
http://search.cpan.org/~robin/PadWalker-1.5/PadWalker.pm ).

It handles lexical variables, but not globals (afaict) :

--------------------------------
use PadWalker;
use warnings;

my $s = 'hello world';
my @array = (1, 3, 5);
our $t = 'hello again';
our @array2 = (5, 7, 9);

$h1 = PadWalker::peek_my(0);
%deref = %{$h1};

print "\n\"my\" variables\n";

for(keys(%deref)) {
print "$_ : $deref{$_} \n";
print "@{$deref{$_}} \n" if ref($deref{$_}) eq "ARRAY";
print ${$deref{$_}}, "\n" if ref($deref{$_}) eq "SCALAR";
}


print "\n\"our\" variables\n";

$h1 = PadWalker::peek_our(0);
%deref = %{$h1};

for(keys(%deref)) {
print "$_ : $deref{$_} \n";
print "@{$deref{$_}} \n" if ref($deref{$_}) eq "ARRAY";
print ${$deref{$_}}, "\n" if ref($deref{$_}) eq "SCALAR";
}
--------------------------------

Which outputs:

--------------------------------
"my" variables
@array : ARRAY(0xb54914)
1 3 5
$s : SCALAR(0xb54944)
hello world

"our" variables
$t : SCALAR(0x26aa184)
hello again
@array2 : ARRAY(0x2716884)
5 7 9
--------------------------------

Faik, there may well be something better.

Hope this helps.

Cheers,
Rob
 
B

Bill H

Not easily. You'd have a chance with package variables. For
lexicals you'd need something like PadWalker, which is a bit
tricky to use.

Most people settle for printing selected values at selected
places for debugging. The module Data::Dumper helps with
viewing complex data structures.

That was what I thought I would need to do, but since perl has so much
in it I thought maybe it would do this too.

Bill H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top