有人写文件时读取文件

时间:2018-09-10 19:35:39

标签: perl

脚本从文件中读取行,这是代码:

#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;

open my $fh, '<', 'input.txt' or die "Can't open file: $!";

my $cv = AE::cv;

my $timer = AE::timer 0, 10, sub {
    printf "TELL: %s EOF: %s\n", tell $fh, eof $fh;
    print while <$fh>;
};

$cv->recv;

如果我通过cat添加行,那么脚本将读取新行。但是,如果我通过vim编辑器编辑文件,则脚本不会在文件中看到任何新行并发出EOF。为什么脚本会以这种方式运行而看不到新行?

脚本的输出:

$ perl test.pl
TELL: 0 EOF:
row_1
row_2
row_3
TELL: 18 EOF: 1
TELL: 18 EOF: 1
TELL: 18 EOF:
row_4
TELL: 24 EOF:
row_5
TELL: 30 EOF: 1
TELL: 30 EOF: 1
TELL: 30 EOF: 1
TELL: 30 EOF: 1

input.txt的初始内容:

row_1
row_2
row_3

通过cat添加:

$ cat >> input.txt
row_4
row_5

通过vim添加:

$ vim input.txt
row_6
row_7

1 个答案:

答案 0 :(得分:8)

shell的>>指令打开现有文件,而vim创建一个与旧文件同名的新文件,使程序读取一个现在匿名的文件。

$ touch a

$ perl -MFile::stat -e'my $qfn = $ARGV[0]; my $s = stat($qfn) or die $!; printf "%s:%s\n", $s->dev, $s->ino' a
2065:1084527165

$ cat >>a
foo

$ perl -MFile::stat -e'my $qfn = $ARGV[0]; my $s = stat($qfn) or die $!; printf "%s:%s\n", $s->dev, $s->ino' a
2065:1084527165

$ vim a

$ perl -MFile::stat -e'my $qfn = $ARGV[0]; my $s = stat($qfn) or die $!; printf "%s:%s\n", $s->dev, $s->ino' a
2065:1084520254

您需要重新打开文件。

#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use Fcntl qw( SEEK_SET );

my $cv = AE::cv;

my $qfn = $ARGV[0];
my $last_pos = 0;

my $timer = AE::timer 0, 10, sub {
    open(my $fh, '<', $qfn)
        or die("Can't open file: $!\n");
    seek($fh, $last_pos, SEEK_SET)
        or die("Can't seek: $!\n");

    printf "TELL: %s EOF: %s\n", tell $fh, eof $fh;

    print while <$fh>;

    ( $last_pos = tell($fh) ) >= 0
        or die("Can't tell: $!\n");
};

$cv->recv;

输出:

$ echo foo >a

$ perl a.pl a
TELL: 0 EOF:
foo
TELL: 4 EOF: 1
TELL: 4 EOF: 1        echo bar >>foo
TELL: 4 EOF:
bar
TELL: 8 EOF: 1
TELL: 8 EOF: 1        Used vim to change the file to "The quick brown fox"
TELL: 8 EOF:
k brown fox
TELL: 20 EOF: 1
^C