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

> All suggestions gratefully accepted!

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

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!

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

    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)