Matthijs van Duin writes: > > >does it mean that this is legitimate > > > > sub a { > > state $x; > > my $y; > > state sub b { state $z ; return $x++ + $y++ + $z++ ; } > > return &b; # is a \ before &b needed? > > } > > No, since you can't refer to $y in that sub (perl 5 actually allows you to > do that but gives a warning 'Variable "%s" will not stay shared' - but I > hope perl 6 will simply give a compile-time error)
so, the only problem here is that the closure of "sub b" will be evaluated only once and the following exemple will work ? sub a { state $x; state sub b { ... } ; my $y; &b = sub { state $z ; return $x++ + $y++ + $z++ ; } return &b; # is a \ before &b needed? } also , is that correct ? sub new_counter($start=0) { return sub { state $cnt = $start; return ++$cnt; } } our &counter = new_counter ; our &another_counter = new_counter ; print counter, counter, another_counter, another_counter ; #prints: 1, 2, 3, 4 thanks, arcadi