Perl Subroutine References
Published on 30 Nov 2007Tags #Perl
If you are planning to use dynamically added code in your program, you should not rely on eval because it is several times slower than adding a subroutine:
# $code contains your eval block
my $code = q{print 'param: ' . $_[0] . "n";};
# wrap a subroutine around the code
my $func = 'sub {' . $code . '};';
# build the subroutine reference
my $ref = eval $func;
# call the subroutine
&$ref(1);
&$ref(2);