Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Is it possible to get all variable names which are declared?

by rsFalse (Pilgrim)
on Nov 09, 2017 at 17:11 UTC ( #1203070=perlquestion: print w/replies, xml ) Need Help??
rsFalse has asked for the wisdom of the Perl Monks concerning the following question:


Is it possible to access the names of all declared variables? E.g.:
my $A; my $B = 0; print keys %VARIABLES; # <- here I'd like to see: $A$B for my $i ( $B .. 3 ){ my $C = 3; print keys %VARIABLES; # <- here I'd like to see: $A$B$i$C }

Replies are listed 'Best First'.
Re: Is it possible to get all variable names which are declared? (updated)
by haukex (Abbot) on Nov 09, 2017 at 17:20 UTC

    The fact that you want to do this means that you are either writing a debugger, or you are looking at the problem from the wrong angle (e.g. "it's stupid to use a variable as a variable name", and you should use a hash instead). If you could tell us why you want to do this, we can almost certainly provide better solutions.

    PadWalker will provide you with enough rope to shoot yourself in the foot:

    use warnings; use strict; use PadWalker qw/peek_my/; use Data::Dump; my $A; my $B = 0; dd peek_my(0); for my $i ( $B .. 3 ){ my $C = 3; dd peek_my(0); } __END__ { "\$A" => \undef, "\$B" => \0 } { "\$A" => \undef, "\$B" => \0, "\$C" => \3, "\$i" => \0 } { "\$A" => \undef, "\$B" => \0, "\$C" => \3, "\$i" => \1 } { "\$A" => \undef, "\$B" => \0, "\$C" => \3, "\$i" => \2 } { "\$A" => \undef, "\$B" => \0, "\$C" => \3, "\$i" => \3 }

    Update: That only dumps my variables. See also the module's peek_our function, or for inspecting the Symbol Tables of packages, Devel::Symdump or my simplistic replacement here (only does scalars, arrays and hashes). When dumping symbol tables, note the caveat described in the Devel::Symdump documentation: "scalar symbol table entries are a special case. If a symbol table entry exists at all, presence of a scalar is currently unknowable, due to a feature of Perl described in "Making References" in perlref point 7. For example, this package will mark a scalar value $foo as present if any of @foo, %foo, &foo etc. have been declared or used."

Re: Is it possible to get all variable names which are declared?
by Chuma (Beadle) on Nov 09, 2017 at 18:03 UTC

    Try something like this:
    for(keys %{'main::'}){if(defined $$_){print "\$$_\n\t$$_\n"}}

      Looking into the symbol table like that would attempt to access the "global" (package) variables (those defined with our, for example), not to lexical variables (defined with my), which is apparently what the OP was looking for.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1203070]
Approved by stevieb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-04-20 01:17 GMT
Find Nodes?
    Voting Booth?