in reply to %main:: and my'd vars

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*

Replies are listed 'Best First'.
Re: Re: %main:: and my'd vars
by broquaint (Abbot) on Jul 15, 2002 at 15:02 UTC
    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, };
        you didn't use my for your variables.

        update

        use v5.12.0; use warnings; use PadWalker qw/peek_my peek_our/; my $a1 = {}; our $a2 = {}; for my $space ( peek_my(0),peek_our(0) ){ for my $item (keys %{$space}) { say $item if (ref ${$space->{$item}} eq "HASH"); } }

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery