sub foo { my $bar = sub { my ($i) = @_; push @arrb, $arra[$i]; }; $bar->($#arra); ... }