我必须使用perl逐行读取内存中的大(BIG)文件.如果出现一些错误,函数open()会返回false和$!设置为系统错误.但是,如果我在阅读文件时遇到一些错误?我用这个代码:
open(STATISTICS, "<" . $statisticsFile) or die "Can't open statistics file $statisticsFile ($!)";
while (<STATISTICS>) {
my $line = $_;
...
}
close($STATISTICS);
Run Code Online (Sandbox Code Playgroud)
任何提示?
您可以将代码更改为如下所示.
您似乎正在使用它们STATISTICS和$STATISTICS文件句柄.由于词汇句柄是优选的,我在$stat这里使用.
open my $stat, "<" . $statisticsFile
or die "Can't open statistics file $statisticsFile: $!";
until (eof $stat) {
my $line = <$stat>;
defined $line or die "Read failure on statistics file $statisticsFile: $!";
...
}
close($stat);
Run Code Online (Sandbox Code Playgroud)