I am looking for best, easiest way to do something like:
$var1="value";
bunch of code.....
**print allVariablesAndTheirValuesCurrentlyDefined;**
I am looking for best, easiest way to do something like:
$var1="value";
bunch of code.....
**print allVariablesAndTheirValuesCurrentlyDefined;**
The global symbol table is
%main::
, so you can get global variables from there. However, each entry is a typeglob which can hold multiple values, e.g., $x, @x, %x, etc, so you need to check for each data type. You can find code that does this here. The comments on that page might help you find other solutions for non-global variables (like lexical variables declared with "my").Nathan's answer is part of the story -- unfortunately, the rest of the story is that lexical variables aren't listed in
%main::
or anywhere else (at least anywhere accessible from Perl -- it's probably possible to write some hairy XS code that digs this information out of Perl's C-level internals).Lexical variables are what you would normally use for "ordinary local" variables. They are declared like:
The PadWalker module gives you
peek_my
andpeek_our
which take a LEVEL argument that determines which scope to look for variables in:Here is an example:
Would this be for anything other than debugging purposes? If not, you may want to familiarize yourself with perl's debugger. Once inside the debugger you can inspect all variables by issuing 'V'.
Package variables? Lexical variables?
Package variables can be looked up via the symbol table. Try Devel::Symdump:
Lexical variables are a little tricker, you won't find them in the symbol table. They can be looked up via the 'scratchpad' that belongs to the block of code they're defined in. Try PadWalker: