VinsWorldcom has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Print variable name/value for all program variables
by greengaroo (Hermit) on Nov 08, 2012 at 14:35 UTC | |
by Athanasius (Archbishop) on Nov 08, 2012 at 14:56 UTC | |
Re: Print variable name/value for all program variables
by kennethk (Abbot) on Nov 08, 2012 at 15:43 UTC | |
Re: Print variable name/value for all program variables
by clueless newbie (Curate) on Nov 08, 2012 at 14:39 UTC |