declarator state
1 | language documentation Variables |
1.1 | (Variables) declarator state |
1.1.1 | The $ variable |
1.1.2 | The @ variable |
1.1.3 | The % variable |
Documentation for declarator state
assembled from the following types:
language documentation Variables
From Variables
(Variables) declarator state
state
declares lexically scoped variables, just like 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.
Therefore, the subroutine
sub a;say a for 1..6;
will continue to increment $l
and append it to @x
each time it is called. So it will output:
[A][A B][A B C][A B C D][A B C D E][A B C D E F]
Since they have a lexical scope, they are tied to the block in which they are declared.
sub foo ();foo; # OUTPUT: «12»foo; # OUTPUT: «12»
In this case, a new state variable is created every time the block that runs the for loop is entered, which is why the state variable is reset in every call to foo
.
This works per "clone" of the containing code object, as in this example:
( xx 3).map: ; # says 1 then 2 thrice
Note that this is not a thread-safe construct when the same clone of the same block is run by multiple threads. Also remember that methods only have one clone per class, not per object.
As with my
, a declaration of multiple state
variables must be placed in parentheses which can be omitted for a single variable.
Many operators come with implicit binding which can lead to actions at a distance.
Use .clone
or coercion to create a new container that can be bound to.
my ;my ;sub f();f for 1..3;say ; # OUTPUT: «[k1 => 3 k2 => 3 k3 => 3]»say ; # OUTPUT: «[k1 => 1 k2 => 2 k3 => 3]»
State variables are shared between all threads. The result can be unexpected.
sub code();awaitstart ,start ;# OUTPUT: «1234435»# OUTPUT: «21345»# many other more or less odd variations can be produced
The $
variable
In addition to explicitly declared named state variables, $
can be used as an anonymous state variable without an explicit state
declaration.
say "1-a 2-b 3-c".subst(:g, /\d/, );# OUTPUT: «one-a two-b three-c»
Furthermore, state variables can be used outside of subroutines. You could, for example, use $
in a one-liner to number the lines in a file.
perl6 -ne 'say ++$ ~ " $_"' example.txt
Each reference to $
within a lexical scope is in effect a separate variable.
perl6 -e '{ say ++$; say $++ } for ^5'# OUTPUT: «1021324354»
That is why, if you need to reference the same $ variable (or, for that matter, any of the other anon state variables @
and %
) more than once, a possible solution is to bind another variable to it, although in this example it would be more straightforward to just declare state $x and not use the magical/anonymous $
variable:
sub foo ()foo() for ^3; # OUTPUT: «135»
In general, it is better style to declare a named state variable in case you have to refer to it several times.
Note that the implicit state declarator is only applied to the variable itself, not the expression that may contain an initializer. If the initializer has to be called exactly once, the state
declarator has to be provided.
for ^3 # OUTPUT: «012»for ^3 # OUTPUT: «0»
The @
variable
Similar to the $
variable, there is also a Positional anonymous state variable @
.
sub foo()foo() for ^3;# OUTPUT: «[0]# [0 1]# [0 1 2]»
The @
here is parenthesized in order to disambiguate the expression from a class member variable named @.push
. Indexed access doesn't require this disambiguation but you will need to copy the value in order to do anything useful with it.
sub foo()foo() for ^3;# OUTPUT: «[0]# [0 1]# [0 1 2]»
As with $
, each mention of @
in a scope introduces a new anonymous array.
The %
variable
In addition, there's an Associative anonymous state variable %
.
sub foo()foo() for ^3;# OUTPUT: «{0 => 0}# {0 => 0, 1 => 1}# {0 => 0, 1 => 1, 2 => 2}»
The same caveat about disambiguation applies. As you may expect, indexed access is also possible (with copying to make it useful).
sub foo()foo() for ^3;# OUTPUT: «{0 => 0}# {0 => 0, 1 => 1}# {0 => 0, 1 => 1, 2 => 2}»
As with the other anonymous state variables, each mention of %
within a given scope will effectively introduce a separate variable.