|go ahead... be a heretic|
[SOLVED] - Print variable name/value for all program variablesby VinsWorldcom (Priest)
|on Nov 08, 2012 at 14:16 UTC||Need Help??|
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:
And here is basic operation:
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:
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.