in reply to Reading only new lines from a file

What about File::Tail? I have no experience of the module, but perhaps looking at the source may give an indication of what it (may) do, that you don't.

Replies are listed 'Best First'.
Re^2: Reading only new lines from a file
by dsheroh (Monsignor) on Aug 31, 2007 at 21:58 UTC
    Looking over the File::Tail docs, it appears that it just opens the file once, then keeps it open while watching for new additions. I'm processing the file to the end, exiting, and then re-opening it to get the new stuff later, so not quite the same situation.
Re^2: Reading only new lines from a file
by shmem (Chancellor) on Sep 01, 2007 at 09:31 UTC
    Given the limitations of File::Tail, for such tasks I just
    open my $tailfh, "tail -F $logfile |" or die "blunze: $!\n";

    tail -F handles re-opening the logfile on inode change and file truncating. To process the gathered lines in chunks, I write them to another file:

    my @lines; while (<$tailfh>) { push @lines, $_; unless (-f $lockfile) { open my $chunkfh, '>>', $chunkfile or die "more blunze: $!\n"; $| = 1; # or use IO::File and autoflush print $chunkfh @lines; close $chunkfh; @lines = (); } }

    Then another process can touch the lockfile, process the lines in the chunkfile, truncate it and remove the lockfile. The line gathering process then flushes its @lines.

    update: if you're wondering what blunze is - that's black pudding (or blood sausage)

    update 2: moved the open/close inside the loop, changed the open mode to append.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}