in reply to Losing first entry of Apache log

I would guess that in the for loop when you call <LOGFILE>; it grabs a line, and then grabs a new line in the while loop.
for ($logpos = tell(LOGFILE); <LOGFILE>; . . .#here while (<LOGFILE>) { #and here
So everytime it hits the inside for loop it grabs two lines before it prints one (and only prints the latter). There is File::Tail for this sort of thing though.

-enlil

Replies are listed 'Best First'.
Re: Re: Losing first entry of Apache log
by Grygonos (Chaplain) on Feb 25, 2004 at 18:11 UTC

    I was testing that theory and it is in fact what is happening. The modified code below works and does not drop the first entry... also a bit easier to read. I would reccomend using while(1) for continous loops.

    #!/usr/bin/perl -w use strict; my $LogFile="C:/test.txt"; my $naptime=3; my $logpos; #Reads LOGFILE continuously open(LOGFILE,"<$LogFile") || die "Couldn't open file Status - $!\n"; while (1) { sleep $naptime; $logpos = tell(LOGFILE); while(<LOGFILE>) { print; } seek(LOGFILE, $logpos, 0); # seek to last known }

    <edit>the more I thought about it you don't even need the $logpos variable. Just use seek(LOGFILE,0,0) to seek to the beginning,since it seemed like what you wanted to do. Although if you have a specific point in the file you want to start reading from then you will need to predefine it and even then you still wouldn't need the call to tell(LOGFILE) I believe</edit>

    Hope this helps.

    Grygonos