I spent most of my Rakudo day today adding state variables to Rakudo. Perl 5.10 supports them, so it's nice that they can now be used in Perl 6 - which first specified the feature - under Rakudo. As often happens, it took a first not-quite-right implementation to do one that I'm now reasonably happy with. I'm sure it can be improved in various ways, but for now it seems to get the right answers for everything we've thrown at it. So, a quick example.
sub counter {
state $count = 0;
$count++;
say $count;
}
counter(); # 1
counter(); # 2
counter(); # 3
Here, $count is set the first time we run the sub to 0. But then in future invocations it remembers its value. The state is kept "per closure", such that you can do things like:
sub make_counter($start = 1) {
return {
state $count = $start;
$count++;
}
}
my $c1 = make_counter();
my $c2 = make_counter(10);
$c1(); $c1();
$c2(); $c2();
say $c1(); # 3
say $c2(); # 12
Thanks to masak, moritz and nicholas for all contributing extra test cases to exercise the state implementation throughout the day - the already decent state.t has gained an extra ten tests thanks to their input. We now pass all tests in there apart from six which test the interaction of state with other not yet implemented features. So, 24 new tests passing. Nice.
I spent the rest of the day on some more minor things.
So, enjoy the new features, and thanks to Vienna.pm for funding this work.
http://use.perl.org/user/nicholas/journal/36270
Re:Now who is the best?
nicholas on 2009-03-25T23:04:05
Rakudo is now the undisputed best, because
state @a =
works in Rakudo, whereas it doesn't (yet) in Perl 5...;