Perl Subroutine References

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);
Feedback is always welcome! If you'd like to get in touch with me concerning the contents of this article, please use Twitter.