in reply to Re: File::Tail on win32
in thread File::Tail on win32

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 !

Replies are listed 'Best First'.
Re: some more help
by GrandFather (Saint) on Nov 30, 2005 at 03:26 UTC

    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