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

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()));

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