open( my $handle, '<', 'file.dat' ) or die $!;
my @data = map { do_things($_) } <$handle>;
close $handle;
这个命令式的 open
和 close
在整个代码中显得很突兀。有没有更简洁的写法?我可以自己编写 read_file
的子程序,但应该已经有类似的东西了。
sub read_file {
open( my $handle, '<', $_[0] ) or croak $!;
return <$handle>;
}
my @data = map { do_things($_) } read_file('file.dat');
效率并不重要,但解决方案应该是跨平台的。
close
的返回值了。 - tchristclose
可能会失败。 - Tim