How to create a Perl subroutine that accepts a block of code

后端 未结 4 827
猫巷女王i
猫巷女王i 2021-02-01 06:12

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         


        
4条回答
  •  梦谈多话
    2021-02-01 06:53

    You want the & prototype.

    sub foo(&@) {
        my ($callback) = shift;
        ...
        $callback->(...);
        ...
    }
    

    makes

    foo { ... } ...;
    

    equivalent to

    foo(sub { ... }, ...);
    

提交回复
热议问题