more useful options | |
PerlMonks |
Re^4: Experimental features: autoderef vs postfix derefby kcott (Archbishop) |
on Jul 12, 2015 at 20:49 UTC ( [id://1134420]=note: print w/replies, xml ) | Need Help?? |
"... and a new mechanism specifically designed to replace them that doesn't." Firstly, I'm assuming that statement is based on the opening paragraph from "perlsub: Persistent Private Variables": "There are two ways to build persistent private variables in Perl 5.10. First, you can simply use the state feature. Or, you can use closures, if you want to stay compatible with releases older than 5.10." My interpretation of that, is that you can replace
with the more succinct
[And similarly for: local our $closure;] state variables are lexically scoped. If you declare two state variables (with the same name) in different scopes, e.g.
they will remain different variables: changing one has no effect on the other. The form you presented with "sub x1{ our $closure = ..." loses the benefits of lexical scoping. $closure is now just aliasing a package variable accessible globally. A quick and dirty example:
In closing, my assumption (stated initially) may be wrong; in which case, I'd be interested in the "specifically designed" part you mentioned. I do use state for purposes other than closures: typically, once-off initialization of variables used in a single subroutine (but that's, perhaps, getting a bit off-topic). — Ken
In Section
Seekers of Perl Wisdom
|
|