Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Finding file level lexical variables

by LanX (Saint)
on May 25, 2016 at 15:50 UTC ( #1164094=note: print w/replies, xml ) Need Help??

in reply to Finding file level lexical variables

My first guess its to write a sub calling PadWalker's closed_over() to get a hash of all vars and their refs in the calling scope.

Untested, bc I'm on mobile now.

HTH :)


The docs are a bit unclear, probably you need to use peek_my(1) instead

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Finding file level lexical variables
by Athanasius (Archbishop) on May 25, 2016 at 16:01 UTC

    I think that should be peek_my(0):

    #! perl use strict; use warnings; use feature qw( state ); use PadWalker qw( peek_my ); our $fred = 'Flintstone'; my $foo = 17; my $quux = bar(); printf "Sum is %d\n", $foo + $$quux; sub bar { state $c = 5; my $baz = 42; return \$baz; } my $h_ref = peek_my(0); print "$_\n" for keys %$h_ref;


    2:00 >perl Sum is 59 $quux $foo 2:00 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      well I meant peek_my(1) to be called from a sub to avoid cluttering the top scope with introspection code. :)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        PadWalker has some serious problems when it comes to file-level lexical variables. Consider the following Perl script and Perl module:
        use strict; use Devel::Size qw(total_size); use Data::Dumper; use TestModule; my $h = TestModule::get_mem(); foreach my $name (keys(%$h)) { my $ref = $h->{$name}; my $mem = total_size($ref); print("$name - $mem bytes\n"); print(Dumper($ref)); print("\n"); }
        package TestModule; use strict; use PadWalker qw(peek_my); my $str = 'abcxyzabcxyzabcxyzabcxyz'; my $h = { x => 'abcabcabcabcabc', y => 'xyzxyzxyzxyzxyz'}; my $l = ['abcdefghi', 'stuvwxyz']; sub get_mem { my $str1 = 'abcxyzabcxyzabcxyzabcxyz'; my $h1 = { x => 'abcabcabcabcabc', y => 'xyzxyzxyzxyzxyz'}; my $l1 = ['abcdefghi', 'stuvwxyz']; return peek_my(0); } 1;
        It produces the following output (I'm doing this on Windows using ActivePerl, PadWalker version 2.2). I'm particularly intrigued by the fact that it reports the variable $str of having size 88 bytes (which is correct), while Data::Dumper says that it's a reference to undef!
        $str1 - 52 bytes $VAR1 = \'abcxyzabcxyzabcxyzabcxyz'; $h - 16 bytes $VAR1 = \undef; $h1 - 230 bytes $VAR1 = \{ 'y' => 'xyzxyzxyzxyzxyz', 'x' => 'abcabcabcabcabc' }; $l1 - 132 bytes $VAR1 = \[ 'abcdefghi', 'stuvwxyz' ]; $str - 52 bytes $VAR1 = \undef; $l - 16 bytes $VAR1 = \undef;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164094]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2023-11-30 03:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found