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 $nam
sub bar {
my ($coderef) = @_;
⁝
$coderef->($f, @arguments);
⁝
}
bar(sub { my ($f) = @_; while … }, @other_arguments);
or perhaps a bit less tangled with a named coderef:
my $while_sub = sub {
my ($f) = @_;
while …
⁝
};
bar($while_sub, @other_arguments);
Edit: The Higher-Order Perl book is full of this sort of programming.