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.


In reply to Re: Print variable name/value for all program variables by kennethk
in thread [SOLVED] - Print variable name/value for all program variables by VinsWorldcom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.