Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Print variable name/value for all program variables

by kennethk (Abbot)
on Nov 08, 2012 at 15:43 UTC ( #1002930=note: print w/replies, xml ) Need Help??

in reply to [SOLVED] - Print variable name/value for all program variables

So I'll first point out what I'm about to give will not help with lexical variables, since they have no entry in the symbol table. However, and thankfully, lexical variables can't escape the scope of your eval, so they won't persist and thus shouldn't be an issue.

I'll also assume that you won't be dealing with people changing packages. Otherwise, you'll have to navigate around all possible packages and keep track of an initial state.

Oh, and string eval, security risk, blah, blah.

Lastly, this solution will fail if a scalar actually holds an undef. This is because *{$glob}{SCALAR} will always return a reference to a scalar regardless of whether you ever actually accessed that scalar. The docs say "[t]his might change in a future release."

I give you my modified version of your code:

#use strict; use warnings; use Term::ReadLine; my $prompt = "prompt> "; my $prog = Term::ReadLine->new('Script'); $prog->ornaments(0); while (defined (my $cmd = $prog->readline($prompt))) { chomp $cmd; # nothing if ($cmd =~ /^\s*$/) { next } # exit if ($cmd =~ /^\s*exit\s*$/) { last } # execute eval "package Sandbox; $cmd"; warn $@ if $@; print "\n" } package Sandbox; sub command { print "Special command"; } sub names { my @keys = sort keys %Sandbox::; for my $key (@keys) { my $glob = \*{"Sandbox::$key"}; print "\$$key\n" if defined ${*{$glob}{SCALAR}}; print "\@$key\n" if defined *{$glob}{ARRAY}; print "\%$key\n" if defined *{$glob}{HASH}; } }

Outside of the addition of the names routine, you'll note I've added package statements to your eval and the subroutine declarations. This avoids the problem of your dump being polluted by the very large number of special variables in the main package.

This approach also has the advantage of only outputting names associated with actual variables, as well as telling the user what variable type they were. Just dumping the names from the table will also yield all subroutines in the package, which is probably not what you mean. You could even add a subroutine to print out all available subroutines:

sub commands { my @keys = sort keys %Sandbox::; for my $key (@keys) { my $glob = \*{"Sandbox::$key"}; print "\&$key\n" if defined *{$glob}{CODE}; } }

Reference materials to be found in Symbol Tables from perlmod and in perlref.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1002930]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2017-10-19 09:38 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (252 votes). Check out past polls.