Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

%main:: and my'd vars

by blahblahblah (Priest)
on Jul 15, 2002 at 14:14 UTC ( #181783=perlquestion: print w/replies, xml ) Need Help??
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.


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?


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://181783]
Approved by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2017-11-23 10:32 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (333 votes). Check out past polls.