in reply to File::Tail on win32

THe following sample code should do it for you. You should omit the updateFile related code - that's just there to get the test file updated.

use strict; use warnings; my $logFileName = 'test.txt'; my $lastPos = 0; my $endTime = time + 60; while (time < $endTime) { next if ! -e $logFileName; next if $lastPos >= -s $logFileName; open inFile, '<', $logFileName; seek inFile, $lastPos, 0; print while <inFile>; $lastPos = tell inFile; close inFile; } continue { sleep (5); updateFile (); } sub updateFile { open outFile, '>>', $logFileName; print outFile 'Line added to file at ' . (join ', ', localtime) . "\n" +; close outFile; }

Printed:

Line added to file at 30, 16, 12, 30, 10, 105, 3, 333, 1 Line added to file at 35, 16, 12, 30, 10, 105, 3, 333, 1 Line added to file at 40, 16, 12, 30, 10, 105, 3, 333, 1 Line added to file at 45, 16, 12, 30, 10, 105, 3, 333, 1 Line added to file at 50, 16, 12, 30, 10, 105, 3, 333, 1 Line added to file at 55, 16, 12, 30, 10, 105, 3, 333, 1 Line added to file at 0, 17, 12, 30, 10, 105, 3, 333, 1 Line added to file at 5, 17, 12, 30, 10, 105, 3, 333, 1 Line added to file at 10, 17, 12, 30, 10, 105, 3, 333, 1 Line added to file at 15, 17, 12, 30, 10, 105, 3, 333, 1 Line added to file at 20, 17, 12, 30, 10, 105, 3, 333, 1

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: File::Tail on win32
by perlAffen (Sexton) on Nov 30, 2005 at 00:47 UTC
    thanks, I'll hook it up to large file and try it.
some more help
by perlAffen (Sexton) on Nov 30, 2005 at 01:44 UTC
    The script behaves strangely when the source file is removed and recreated.
    like so, test.txt is created by doing echo "" > test.txt
    run -> perl this.pl
    other window (ow) echo one >> test.txt
    one
    (ow) echo two >> test.txt
    two
    (ow) del /F test.txt
    (ow) echo uno > test.txt
    (ow) echo due >> test.txt
    (ow) echo tre >> test.txt
    re
    (ow) echo quattro >> test.txt
    quattro

    So it seems to hold on to the last cursor position and won't yield any output until that last postion is surpassed ?
    Also when you first run it, it outputs the entire file, is it possible to set $lastpos to its current EOF line ? ie Can it go to the bottom and wait for the next newline ?
    Also if I wanted to hand the output to a program, rather than print, how would I change
    print while <inFile>; ??
    Thanks !

      This modified version may be closer to what you want:

      use strict; use warnings; my $logFileName = 'test.txt'; my $highWater = -1; # Signal first time through while (1) { next if ! -e $logFileName; if ($highWater >= -s $logFileName) { $highWater = -s $logFileName; # Reset high water mark next; } open inFile, '<', $logFileName; seek inFile, $highWater, 0; #my @lines; # Uncomment to collate lines while (<inFile>) { print $_; # Replace with alternate code to handle output. #push @lines, $_; #Uncomment to collate lines } $highWater = tell inFile; close inFile; # Use @lines as required here to deal with added lines in one hit. e +g.: #print @lines; #Uncomment to print collated lines } continue { sleep (5); }

      DWIM is Perl's answer to Gödel