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

Rewritten to be slightly more Perl 6:

        C<state> declares a lexically scoped variable, just like C<my>.
        However, those variables will never be reinitialized, contrary to
        lexical variables that are reinitialized each time their enclosing
        block is entered. 

        If more than one variable is listed, the list must be placed in
        parentheses. Within a parenthesised list, C<$> can be used as a
        dummy placeholder. 

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:

        C<state> declares lexically scoped variables, just like 
        C<my>. However, initialization happens exactly once the
        first time the initialization is encountered in the normal
        flow of execution. Thus, state variables will retain
        their value across multiple executions of the enclosing
        block or routine.

        Also, just like C<my>, declaring multiple variables must be
        placed in parentheses and for declaring a single variable,
        parentheses may be omitted. Within a parenthesized list,
        C<$> can be used as a dummy placeholder.

Mostly trying to nail down the exact time of initialization.

@masak
Copy link

masak commented Sep 8, 2015

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

@raiph
Copy link

raiph commented Sep 9, 2015

Aiui state variables are initialized on first entry of each closure clone.

http://irclog.perlgeek.de/perl6/search/?nick=jnthn&q=closure-clone

@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