【问题标题】:Exit from tailing a file - In perl退出拖尾文件 - 在 perl 中
【发布时间】:2011-10-18 20:44:48
【问题描述】:

我有这段代码,它基本上是一个文件的尾部。 该文件每秒填充近 100 个条目。

open (MYFILE, 'output.txt');
for (;;)
{
    while (<MYFILE>)
    {
        chomp;
        my $test=$_;
        if  ($test =~ m/^ok/)
        {
            $passed++;
            print "Number of passed :$passed\n";
            print "Number of failed :$failed\n";
        }
        elsif ($test =~ m/^not/)
        {
            $failed++;
            print "Number of passed :$passed\n";
            print "Number of failed :$failed\n";
        }
        elsif ($test =~ m/^The time taken is: (.*)/)
        { push (@array, "$1") ; }
        $row++;

    }
sleep (5);
print "It has been ".(time - $time)."seconds\n";
seek(MYFILE, 0, 1);

}

所有这些都可以正常工作,但是我希望这个 perl 脚本在文件 output.txt 不再被填充时自动退出。

除了使用标志技术之外还有其他方法吗? perl做出的规定?

【问题讨论】:

    标签: perl tail-recursion filehandle file-handling


    【解决方案1】:

    据我所知,Perl 中没有内置任何内容。您可以检查文件大小(使用tell),记录文件大小变化的时间,如果距离上次更改时间过长,则退出。

    【讨论】:

    • 我不知道tell 函数,有人可以给我看一个示例吗?
    • $current_position_in_bytes = tell FILEHANDLE
    • @perlnewbie - 只是为了补充暴民所说的 - 如果您在搜索到文件末尾后使用tell,它会告诉您文件大小(至少在搜索时)完成)。
    • 谢谢大家,我知道了。
    猜你喜欢
    • 2015-05-29
    • 2012-09-03
    • 2020-06-21
    • 2017-01-26
    • 2012-01-28
    • 2021-02-08
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多