I've done some searching and found printing the variable name and value is trivial if you know at least the variable names you want to operate on. I have a script that creates a user shell where the user can create variables and then use them later. How can I print a list of all the variable names/values the user has defined?

Here's the script, I purposely don't 'use strict' so the user doesn't have to type 'my' to create variables and so they are accessible for the duration of the script:

#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 $cmd; warn $@ if $@; print "\n" } sub command { print "Special command"; }

And here is basic operation:

VinsWorldcom@C:\Users\VinsWorldcom\tmp> perl script.pl prompt> command Special command prompt> $var1 = "variable" prompt> print $var1 variable prompt> $var1 = "variable1" prompt> print $var1 variable1 prompt> exit VinsWorldcom@C:\Users\VinsWorldcom\tmp>

The above simple example shows calling an "internal command" and defining a variable ($var1), printing it, redefining it and printing again. Not too complicated.

Suppose the user creates 100 variables and changes the values of them over and over again and then gets confused. It would be nice to supply an "internal command" (like the 'sub commmand()' in the example script) that would print all the variable names/values the user has created. This is complicated (for me at least) since all the solutions I've found presuppose the variable names are known - in this, case they are not known; created by the user during script run-time.

Of course, they must be 'stored' somewhere since subsequent "prompt>" allows the user to print previously defined variables (if the user can remember the name of the variable), but where are they stored? And how to access them?


SOLVED: Thanks to Athanasius recommendation below, I used that advice to create the following:

package MyPackage; ... sub variables { my %SKIP = ( # list var names that you want to ignore ); for my $var (sort(keys(%MyPackage::))) { if (defined(${$var}) && !defined($SKIP{$var})) { print "\$$var = ${$var}\n" } } }

That was MUCH easier than I thought and honestly thought I had tried something similar which didn't work which lead to my searches and finally this question. I feel better with a Monk-certified solution - THANKS Athanasius ++ and to all other respondents.


In reply to [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.