my $icopy = $i; # ... my $psub = sub { $chosen_idx = $icopy };