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

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.
  • Comment on Re^8: Finding file level lexical variables

Replies are listed 'Best First'.
Re^9: Finding file level lexical variables
by LanX (Saint) on May 26, 2016 at 14:13 UTC
    > . 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!

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