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

> . 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!

Replies are listed 'Best First'.
Re^10: Finding file level lexical variables
by johndeighan (Novice) on May 26, 2016 at 15:02 UTC

    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!

      > All suggestions gratefully accepted!

      the only possibility I see for a generic get_mem() is to

      • define it in another module and use it
      • use peek_my(1) inside to inspect the callers level
      • analyze the vars right away
      • get_mem() must be called explicitly within the inspected file
      • this even at different locations depending on block scopes

      Lexical variables are destroyed at the end of their scope.

      And totally different lexicals in a file can have the same name.

      The documentation of PadWalker talks about this.

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

        So, I don't want to be thick here, but here's my confusion. Take my baby example of a test.pl script and a TestModule.pm Perl module. What I'm interested in is in test.pl, finding the variables, declared using "my", that are at file level (i.e. outside of any function) in TestModule.pm. Now, being outside of any function in TestModule.pm, they are created when TestModule.pm is parsed. I've read somewhere that it's kind of like TestModule.pm is enclosed in '{' and '}'. I know from experience that these variables exist for the life of the script. So, it seems to me that the statement "Lexical variables are destroyed at the end of their scope." isn't relevant here, i.e. the "scope" of TestModule.pm doesn't end until the script test.pl ends.

        I have gotten what I need by creating a function like this one in TestModule.pm and calling it from test.pl:

        # --------------------------------------------------------- sub GetMyMemoryMap { # --- returns hash: # { <nameWSigil> => { # Shared => <is_shared>, # Size => <nBytes>, # }} require Devel::Size; require threads::shared; return { '$hUserInfo' => { Shared => threads::shared::is_shared($hUserInfo), Size => Devel::Size::total_size($hUserInfo), } }; } # GetMyMemoryMap() # ----------------------------------------------------------

        and it works. But it isn't automatic. I.e. it would require me to create such a function, which has to explicitly know what file level lexical variables exist in each of my Perl modules (the actual application has many, many Perl modules)