views:

525

answers:

5

I am looking for best, easiest way to do something like:

$var1="value";
bunch of code.....
**print allVariablesAndTheirValuesCurrentlyDefined;**
+5  A: 

The global symbol table is %main::, so you can get global variables from there. However, each entry is a typeglob which can hold multiple values, e.g., $x, @x, %x, etc, so you need to check for each data type. You can find code that does this here. The comments on that page might help you find other solutions for non-global variables (like lexical variables declared with "my").

Nathan Kitchen
And don't forget about all the other packages which have variables in scope too!
brian d foy
+1, but please also mention that lexical variables (those declared like "my $x;") aren't listed in %main:: or anywhere else. :(
j_random_hacker
+1  A: 

Nathan's answer is part of the story -- unfortunately, the rest of the story is that lexical variables aren't listed in %main:: or anywhere else (at least anywhere accessible from Perl -- it's probably possible to write some hairy XS code that digs this information out of Perl's C-level internals).

Lexical variables are what you would normally use for "ordinary local" variables. They are declared like:

my $x;
j_random_hacker
Luckily, someone has already written the hairy XS code for us. See PadWalker on CPAN.
Chas. Owens
@Chas: Interesting. Although I would feel dirty ever using PadWalker ;)
j_random_hacker
In production code? never. In dev code, no problem. Think of it as a programmable debugger, in fact, I believe it is used by Devel::ebug to provide the watch variables.
Chas. Owens
@Chas: I totally agree. Definitely a useful tool.
j_random_hacker
+5  A: 

The PadWalker module gives you peek_my and peek_our which take a LEVEL argument that determines which scope to look for variables in:

The LEVEL argument is interpreted just like the argument to caller.
So peek_my(0) returns a reference to a hash of all the my variables
that are currently in scope; peek_my(1) returns a reference to a hash
of all the my variables that are in scope at the point where the 
current sub was called, and so on.

Here is an example:

#!/usr/bin/perl

use strict;
use warnings;

use PadWalker qw/peek_my/;

my $baz = "hi";

foo();

sub foo {
    my $foo = 5;
    my $bar = 10;

    print "I have access to these variables\n";
    my $pad = peek_my(0);
    for my $var (keys %$pad) {
     print "\t$var\n";
    }

    print "and the caller has these variables\n";
    $pad = peek_my(1);
    for my $var (keys %$pad) {
     print "\t$var\n";
    }
}
Chas. Owens
+4  A: 

Package variables? Lexical variables?

Package variables can be looked up via the symbol table. Try Devel::Symdump:

#!/path/to/perl

use Devel::Symdump;

package example;

$var = "value";
@var = ("value1", "value2");
%var = ("key1" => "value1", "key2" => "value2");

my $obj = Devel::Symdump->new('example');

print $obj->as_string();

Lexical variables are a little tricker, you won't find them in the symbol table. They can be looked up via the 'scratchpad' that belongs to the block of code they're defined in. Try PadWalker:

#!/path/to/perl

use strict;
use warnings;

use Data::Dumper;
use PadWalker qw(peek_my);

my $var = "value";
my @var = ("value1", "value2");
my %var = ("key1" =>  "value1", "key2" => "value2");

my $hash_ref = peek_my(0);

print Dumper($hash_ref);
Mark Johnson
A: 

Would this be for anything other than debugging purposes? If not, you may want to familiarize yourself with perl's debugger. Once inside the debugger you can inspect all variables by issuing 'V'.

mnology
no, not so much for debugging, it would be part of "dynamic" runtime loading and then figuring what got loaded
Ville M