How to run an anonymous function in Perl?

前端 未结 7 2013
长发绾君心
长发绾君心 2021-01-31 08:57
(sub {
print 1;
})();



sub {
print 1;
}();

I tried various ways, all are wrong...

7条回答
  •  梦如初夏
    2021-01-31 09:26

    # ------------------------------------------------------
    # perl: filter array using given function
    # ------------------------------------------------------
    sub filter {
       my ($arr1, $func) = @_;
       my @arr2=();
       foreach ( @{$arr1} ) {
            push ( @arr2, $_ ) if $func->( $_ );
       };
       return @arr2;
    }
    
    # ------------------------------------------------------
    # get files from dir
    # ------------------------------------------------------
    sub getFiles{
     my ($p) = @_;
     opendir my $dir, $p  or die "Cannot open directory: $!";
     my @files=readdir $dir;
     closedir $dir;
     #return files and directories that not ignored but not links
     return filter \@files, (sub { my $f = $p.(shift);return ((-f $f) || (-d $f)) && (! -l $f) } ); 
    }
    

提交回复
热议问题