How do I read a file which is constantly updating?

前端 未结 6 519
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 15:41

I am getting a stream of data (text format) from an external server and like to pass it on to a script line-by-line. The file is getting appended in a continuous manner. Whi

6条回答
  •  爱一瞬间的悲伤
    2020-12-13 16:28

    In perl there are a couple of modules that make tailing a file easier. IO::Tail and File::Tail one uses a callback the other uses a blocking read so it just depends on which suits your needs better. There are likely other tailing modules as well but these are the two that came to mind.

    IO::Tail - follow the tail of files/stream

     use IO::Tail;
     my $tail = IO::Tail->new();
     $tail->add('test.log', \&callback);
     $tail->check();
     $tail->loop();
    

    File::Tail - Perl extension for reading from continously updated files

    use File::Tail;
    my $file = File::Tail->new("/some/log/file");
    while (defined(my $line= $file->read)) {
        print $line;
    }
    

提交回复
热议问题