devslashneil has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I was just wondering what the best way to read from a file which is constantly being written to.

The first thought that i had with this matter is simply to
open(FILE,"< $file"); @ary = <FILE>; close(FILE);
repeatedly, however i assume there is a much more efficiant way of doing this, maybe similar to Tie::File?.

Cheers

- Neil

Replies are listed 'Best First'.
Re: Monitoring a file which is constantly being written to.
by chip (Curate) on Jun 20, 2003 at 03:30 UTC
    If I were writing this I'd probably use:

    open T, '-|', 'tail', '-f', $file; while (<T>) {}

    Note that, barring serious system failure or ^C, this loop will never end.

    If you can't stand that, then you should just leave the file open, then once in a while (between sleeps, perhaps) call:

    seek FILE, 0, 1; # clear EOF condition while (<FILE>) {}

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      I've run into a situation where data was coming in real time
      and couldn't be ignored, but also needed to be displayed
      and stored in database.

      Writing and reading a file constantly was too "messy" in
      terms of possible conflicts from two different programs -
      even with lockfiles.

      Perls Interprocess Communication (IPC) solved the problem.

      See:

      http://www.perldoc.com/perl5.6/pod/perlipc.html

      CPAN also many variations in IPC (222 hits on "IPC" alone).

      Thanks for your reply.

      Almost as soon as i clicked "submit" on this post i thought of the tail approach, however i would be suprised if there is not a module which can handle this more efficiantly than i can write?

      - Neil

        You could take a look at File::Tail.

        I doubt that it is much more efficient than chips seek approach as it uses that same method, but a quick browse of the source shows that it does a lot more besides.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


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

        This should do nicely ;)

        - Neil