【发布时间】:2012-07-21 16:29:49
【问题描述】:
我必须使用 perl 逐行读取内存中的一个大 (BIG) 文件。 如果出现错误,函数 open() 会返回 false 和 $!设置为系统错误。 但是,如果我在读取文件时遇到一些错误? 我使用此代码:
open(STATISTICS, "<" . $statisticsFile) or die "Can't open statistics file $statisticsFile ($!)";
while (<STATISTICS>) {
my $line = $_;
...
}
close($STATISTICS);
有什么提示吗?
【问题讨论】:
-
任何读取错误:说磁盘在完成读取文件之前着火了吗?
-
另见perldoc -f readline:
while ( ! eof($fh) ) { defined( $_ = <$fh> ) or die "readline failed: $!"; ... } -
你也可以在循环体中测试
$filehandle->error。见 IO::句柄