blahblahblah has asked for the wisdom of the Perl Monks concerning the following question:

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";

Replies are listed 'Best First'.
Re: %main:: and my'd vars
by broquaint (Abbot) on Jul 15, 2002 at 14:27 UTC
    Are my'd variables kept in some other place?
    Indeed they are. While package variables are stored in their respective packages and can be accessed through the symbol table, lexical (or my()ed) variables are only stored in the 'scratch pad' of the current lexical scope which cannot be natively accessed in perl.
    HTH

    _________
    broquaint

Re: %main:: and my'd vars
by particle (Vicar) on Jul 15, 2002 at 14:43 UTC
    nobody's mentioned PadWalker, so i thought i'd throw that in.

    from the docs:

    PadWalker is a module which allows you to inspect (and even change!) lexical variables in any subroutine which called you. It will only show those variables which are in scope at the point of the call.

    ~Particle *accelerates*

      Here's a small example of how you might use PadWalker to look at the variables in the current lexical scope
      use PadWalker qw(peek_my); use Data::Dumper; my $s = "a string"; { my @a = qw(an array); print Dumper( peek_my(0) ); } my %h = qw(a hash); print Dumper( peek_my(0) ); __output__ $VAR1 = { '@a' => [ 'an', 'array' ], '$s' => \'a string' }; $VAR1 = { '%h' => { 'a' => 'hash' }, '$s' => \'a string' };
      Funky huh?
      HTH

      _________
      broquaint

        use PadWalker qw(peek_my); do "xyz"; #this file has many hashrefs defined foreach $item (keys %{peek_my(0)}) { print $item,"\n"; print $item if (ref $item eq ref {}); }
        I dont get any o/p . why is this so? Code in "xyz" file:
        $a1 = { 'p' => 1, 'q' => 2, }; $a2 = { 'p' => 1, 'q' => 2, };
Re: %main:: and my'd vars
by PodMaster (Abbot) on Jul 15, 2002 at 14:40 UTC
•Re: %main:: and my'd vars
by merlyn (Sage) on Jul 15, 2002 at 14:25 UTC