Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^5: Finding file level lexical variables

by LanX (Saint)
on May 26, 2016 at 12:37 UTC ( #1164188=note: print w/replies, xml ) Need Help??


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

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)

update

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!

Replies are listed 'Best First'.
Re^6: Finding file level lexical variables
by johndeighan (Novice) on May 26, 2016 at 13:04 UTC
    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

      update

      > sub get_str { return $str; }

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

      update

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

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

        Yes, I know what closures are. Do you know the difference between going up 1 level in the call stack as opposed to going up one level lexically? peek_my(1) will go up one level in the call stack which just finds variables in the script, not the package TestModule. I tried it and it found nothing, which is what I expected.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2023-12-08 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (36 votes). Check out past polls.

    Notices?