To get the "won't stay shared" warning, you have to have something like:
Your my $foo isn't inside a subroutine so you just have one lexical variable scoped to that bare block. If the my was inside your up subroutine, then each call to up would create a new instance of $foo and that would mean that your get subroutine wouldn't know which instances of $foo that you wanted it to use.sub a { my $x; sub b { $x } }
Since the bare block is only ever executed once, your my only ever creates one instance of the variable so it "stays shared". When a my is compiled, it declares the variable (which leaves it set to undef). When the my is executed, it initializes the variables. Executing the my a second or subsequent time creates a new instance of the variable and then initializes that. Even if you require or use a module more than once, the code for the module is only compiled and executed once (but the code for subroutines in that module could be executed more times or zero times).
The code you used would be a fine way to store module-specific results. If your block were in a different module, then it would be the module and not the caller storing them.
Updated.
- tye (but my friends call me "Tye")In reply to (tye)Re: Why no stay shared warning?
by tye
in thread Why no stay shared warning?
by tenya
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |