Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^4: Finding file level lexical variables

by johndeighan (Novice)
on May 26, 2016 at 12:19 UTC ( #1164187=note: print w/replies, xml ) Need Help??

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

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;

Replies are listed 'Best First'.
Re^5: Finding file level lexical variables
by LanX (Saint) on May 26, 2016 at 12:37 UTC
    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()));
        They are not closed over in the sub, ie never used there.

        Did you try peek_my(1) like I suggested 3 times already ?

        Maybe you want to have a look into perlguts


        > sub get_str { return $str; }

        This is a closure .... do you know what closures are?


        Do you know how Perl's refcount and "garbage collection" works?

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

Log In?

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

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

    No recent polls found