for my $i (1 .. 3) { my $s = sub { state $x = $_; say "$i $x" }; $s->($_) for qw( a b ); }