my $subcode = sub { for (1 .. $n) { local \$_; package $pack; $c;} } my $subref = eval $subcode; &$subref #### package R; # Just to demonstrate a variable in another package my $R = 10; package main; # Just to prove we are "executing" in a different package my $subcode = "sub { local \$_; package R; print $R}"; my $subref = eval $subcode; &$subref; #### Benchmark: timing 1000 iterations of Grep, Max, Ternary... Grep: 20 wallclock secs (19.99 usr + 0.03 sys = 20.02 CPU) Max: 22 wallclock secs (21.69 usr + 0.03 sys = 21.72 CPU) Ternary: 22 wallclock secs (22.58 usr + 0.00 sys = 22.58 CPU)