Raku Books / Perl 6 at a Glance / Code Organization / Variables and signatures
state variables
State variables (declared with the keyword state)
appeared in Perl 5.10 and work in Perl 6. Such variables are initialized
during the first call and keep their values in subsequent sub calls.
It is important to keep in mind that a single instance of the
variable is created. Let us return to the example with a counter and
replace the my declaration with the state one.
The closure will now contain a reference to the same variable.
sub seq($init) {
state $c = $init;
return {$c++};
}CodeBlockPlaceholder2raku-static my $a = seq(1); my $b = seq(42); CodeBlockPlaceholder3raku-static say $a(); # 1 say $a(); # 2 say $b(); # 3 say $a(); # 4 say $b(); # 5 CodeBlockPlaceholder4