How to create a Perl subroutine that accepts a block of code
问题 I have a set of subroutines that look like this: sub foo_1($) { my $name = shift; my $f; run_something(); open($f, $name) or die ("Couldn't open $name"); while (<$f>) { //Something for foo_1() } close($f); do_something_else(); } And I have like four or more that look the same, the only thing that changes is the body of the while block. I'd like to abstract this and stop copy-pasting the code around. Is there a way to code a subroutine that accepts a block of code and executes it? To give more