I'm trying to write a some code which prints out every defined
variable in my program. (I'm debugging a giant program which
uses hundreds of global variables and I think this would be
useful.) I can iterate over %main and see everything in it, but
I don't see my'd variables even when they're in my current scope.
Are my'd variables kept in some other place?
Here's my code. I thought it would at least print out $symname
and $shouldPrint. Ideally I'd like to also see @colors2 and
$symbols, but I suspect that shouldn't be possible.
use Data::Dumper;
$joe = 1;
$matt = 2;
@colors = ('tan', 'silver', 'silver', 'green', 'black');
my (@colors2) = ('red', 'green', 'blue');
my ($symbols);
for my $symname (sort keys %main::)
{
my ($shouldPrint) = 1;
# ignore some data that we don't care about: %main::, %SIG, %Carp:
+:, etc.
next if $symname =~ /::$/ || $symname eq 'SIG';
if (defined @$symname)
{
$symbols .= "\@$symname:\n";
$symbols .= Dumper @$symname;
}
elsif (defined %$symname)
{
$symbols .= "\%$symname:\n";
$symbols .= Dumper %$symname;
}
elsif (defined $$symname)
{
$symbols .= "\$$symname: \"$$symname\"\n";
}
elsif (defined &$symname)
{
$symbols .= "sub $symname\n";
}
else
{
$symbols .= "unrecognized symbol: $symname\n";
}
}
print "\n==================\n$symbols\n==================\n";
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.