Created
September 8, 2015 17:16
-
-
Save dha/02f1f41b5f8937c0271e to your computer and use it in GitHub Desktop.
documenatation for C<state>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is the documentation for C<state> from Perl 5. | |
Is it inaccurate or incomplete for C<state> in Perl 6? If not, can we slot it into variables.pod? | |
"state" declares a lexically scoped variable, just like "my". | |
However, those variables will never be reinitialized, contrary to | |
lexical variables that are reinitialized each time their enclosing | |
block is entered. See "Persistent Private Variables" in perlsub | |
for details. | |
If more than one variable is listed, the list must be placed in | |
parentheses. With a parenthesised list, "undef" can be used as a | |
dummy placeholder. However, since initialization of state | |
variables in list context is currently not possible this would | |
serve no purpose. |
Speaking of dummy placeholder, maybe worth pointing out that if what you want is an anonymous state
variable, then $
(outside of a signature) does that, like so:
$ perl6 -e 'sub foo() { say ++$ }; foo() for ^3'
1
2
3
Aiui state variables are initialized on first entry of each closure clone.
http://irclog.perlgeek.de/perl6/search/?nick=jnthn&q=closure-clone
@raiph, If that were the case, then
sub foo {
say "enter";
state $x = say "state";
say "leave";
}
foo();
would output:
state
enter
leave
But it does not.
Each cloned closure gets its own state var, but they are initialized "inline".
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rewritten to be slightly more Perl 6:
Though, in my experiments multiple uses of the dummy placeholder cause weird errors even though it should work.
Here's an attempt to say the same thing plus a little more:
Mostly trying to nail down the exact time of initialization.