Hi,
sub gen() {
state $svar = 42;
# Only initialized once, as it is (per S04) equivalent to
# state $svar will first{ 42 };
return { $svar++ };
}
my $a = gen(); # $svar == 42
$a(); $a(); # $svar == 44
my $b = gen(); # $svar == 44
say $b(); # 44
# $svar == 45
I presume that's correct. Is the following correct, too?
sub gen() {
(state $svar) = 42;
# Initialized on each invocation of &gen, as it is equivalent to
# (state $svar will first{ undef }) = 42;
# I.e. $svar is only set to undef once (as $svar is a state
# variable), but it is set to 42 each time &gen is called. Correct?
return { $svar++ };
}
my $a = gen(); # $svar == 42
$a(); $a(); # $svar == 44
my $b = gen(); # $svar == 42
say $b(); # 42
# $svar == 43
--Ingo
--
Linux, the choice of a GNU | Mathematicians practice absolute freedom.
generation on a dual AMD | -- Henry Adams
Athlon! |