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

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!

Replies are listed 'Best First'.
Re^8: Finding file level lexical variables
by johndeighan (Novice) on May 26, 2016 at 14:06 UTC
    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.
      > . Do you know the difference between going up 1 level in the call stack as opposed to going up one level lexically?

      Yes I know the difference and I was very surprised that you don't call the sub from the filescope.

      That was your decision, nobody suggested this. ..

      Otherwise you may wanna try my original guess closed_over() °.

      Since I don't have the possibility to run tests at the moment I'll leave this thread now. :)

      Good luck!

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

      update

      °) and probably explicitly need to use them in the sub! (In a way which isn't deleted by the compiler optimization.)

      update

      In hindsight if you know all variable names, you don't need padwalker anymore, just inspect the closure vars directly!

        Since I want to develop a script to find all of these variables automatically, I'll need PadWalker just to find them. However, once I've gotten their names, and given that the reference to them seems to be incorrect, I need some other way to determine how much memory they're using. So, it comes down to this question - If I know the name of a Perl module, and I know the name of a "my" variable at file level in that module, how do I determine how much memory it's using?

        The test code I presented was mainly to show a problem that I saw in PadWalker. However, to create a truely automated script, I can't count on having a function like get_mem() in each module. I've thought about writing a generic get_mem()-like function and adding it to packages automatically, but in that case, it would not know about specific variables in each package. All suggestions gratefully accepted!