我正在使用下面的代码连续读取文件。我遇到的问题是,每当我运行代码时,它都会从文件开头开始,返回整个文件,而不是从最后 50 行开始。
我查阅了文档并尝试了 tail=>n 参数的不同值 - 甚至 n= 文件中的总行数减去 50 - 但是我的代码每次都从文件的开头而不是结尾处开始我需要。我做错了什么?
use strict;
use warnings;
use File::Tail;
my $name; #file name
my $name="/home/richard/double-take/messages.log";
my $line;
my $file=File::Tail->new(name=>$name, interval=>3, maxinterval=>3, tail=>50);
while (defined($line=$file->read)) {
print "$line";
}
您没有准确描述发生的情况。实际发生的情况是,它根据需要显示最后 50 行。但 30 秒后,它显示整个文件。
无论您是否设置
maxinterval
,都会发生这种情况。如果不这样做的话,只是需要更长的时间。具体来说,它需要的秒数等于 resetafter
属性。默认为 adjustafter * maxinterval
,adjustafter
默认为 10
。
这似乎是一个“已知问题”。票证上有一个补丁可以解决这个问题(尽管我不知道它是否引入了新的补丁)。
--- Tail.pm 2018-05-22 10:43:03.149802345 +0200
+++ ../perl5/lib/perl5/File/Tail.pm 2018-05-22 10:43:36.676959638 +0200
@@ -404,6 +404,7 @@
} else { # This is the first time we are opening this file
$st=stat($newhandle);
$object->{handle}=$newhandle;
+ $object->{inode}=$st->ino;
$object->position;
$object->{lastread}=$st->mtime; # for better estimate on initial read
}