It was discussed on #p5p that state variables in 5.10 will always
evaluate the expression on the RHS and discard the value in later
calls, example:
$ perl5.9.5 -E 'sub res { print "eek"; int rand 1337 } sub { state $x
= res(); print "$x\n" }->() for 1 .. 2'
eek392
eek392
Pugs does not do this currently doing the right thing (IMO):
$ pugs -e 'my sub res { say "eek"; int rand 1337 } my sub st { state
$x = res(); say $x } st() for 1 .. 2'
eek
1141
1141
Similarly s/// doesn't work with smart matching as it does in Perl 6:
$ pugs -e 'my $str = "baab"; $str ~~ s:g/a//; say $str'
bb
$ perl5.9.5 -E 'my $str = "baaab"; $_ = $str; $str ~~ s/a//g; say $str; say $_'
baaab
bb
$str ~~ s/a//g; means $str ~~ ($_ =~ s/a//g) in 5.10;
The state issue can sort of be worked around by doing:
state $x //= func();
and the s/// issue by using =~ instead, but these constructs should
mirror their v6 functionality, should they not?
Thread Next