develooper Front page | perl.perl5.porters | Postings from March 2007

Differences between state variables and smart matches in 5.10 and 6

Thread Next
From:
Ævar Arnfjörð Bjarmason
Date:
March 25, 2007 15:14
Subject:
Differences between state variables and smart matches in 5.10 and 6
Message ID:
51dd1af80703251514t7b3a50bcya1150223553e1028@mail.gmail.com
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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About