Skip to content

Instantly share code, notes, and snippets.

@dha
Created September 8, 2015 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dha/02f1f41b5f8937c0271e to your computer and use it in GitHub Desktop.
Save dha/02f1f41b5f8937c0271e to your computer and use it in GitHub Desktop.
documenatation for C<state>
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.
@perlpilot
Copy link

@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