in reply to Re^4: How will you use state declared variables in Perl6?
in thread How will you use state declared variables in Perl6?
I'm still not seeing a difference in safety.
If Limbic~Region's analogy with C statics is accurate, state defines an single, absolute, static piece of ram. So, this:
sub genclosure { my $bar; return sub { $bar++; } }
And this:
sub genclosure { return sub { state $bar; $bar++; } }
are semantically quite different.
In the former case, each time you generate a coderef by calling genclosure(), that coderef will close over a different instance of the lexical $bar.
In the latter case, each coderef generated will reference the same, single, immovable, static piece of ram</code> labelled $bar.
And further, references to state $bar; in other unrelated generator functions--in the same package, or a different packages, or a different modules--will all refer to that same static piece of ram.
It is possible that state will be cleverer than that, and somehow be scoped by file or package or namespace, but then a) the analogy with C static falls through; b) the would seem to be considerable overlap with local $bar;.
|
|---|