Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^3: Finding file level lexical variables

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

in reply to Re^2: Finding file level lexical variables
in thread Finding file level lexical variables

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!

Replies are listed 'Best First'.
Re^4: Finding file level lexical variables
by johndeighan (Novice) on May 26, 2016 at 12:19 UTC
    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;
      What exactly is your point? Not sure what you expect. ...

      You are using peek_my(0) , the file scope variables are from the outer scope but never used, so they are not really integrated in the pad and should be already destroyed at the time of your printing. (By ref counting, but digging in memory might still show the old content)

      PadWalker is reading internals in unofficial ways and can't be more intelligent as their structures. Always be prudent about side effects.

      Anyway, the case of the OP is easily solved with peek_my(1)


      If I were you I'd do the introspection and analysis in the sub before any destruction can have happened! Do not return references.

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

        I'm totally confused by your statement that the file level lexicals are "never used". They are set and read - how else can you use a variable? In any case, file level lexicals are not destroyed, except perhaps when the script ends. After my original code, I can still access and print their values. For example, in my original package TestModule, I can add these functions:
        sub get_str { return $str; } sub get_h { return $h; } sub get_l { return $l; }
        Then, in the script, after my original code (where you claim that the variables will be destroyed before my original code printed their values), I can add this, and the correct values of the variables are output:
        print(Dumper(TestModule::get_str())); print(Dumper(TestModule::get_h())); print(Dumper(TestModule::get_l()));

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2023-12-10 10:06 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (39 votes). Check out past polls.