dsheroh has asked for the wisdom of the Perl Monks concerning the following question:
This seems to work perfectly on my test system, where apache is mostly idle.open my $fh, '<', $filename or die "Can't open $filename: $!\n"; _restore_offset($filename, $fh); while (my $line = <$fh>) { # do stuff here } _record_offset($filename, $fh); close $fh; sub _restore_offset { my ($filename, $fh) = @_; # Get $offset and $last_inode from database my $current_inode = (stat $fh)[1]; return unless $current_inode == $last_inode; seek $fh, $offset, 0; } sub _record_offset { my ($filename, $fh) = @_; my $offset = tell $fh; my $inode = (stat $fh)[1]; # Stuff $offset and $inode back into database }
Moving to a more heavily-trafficked server, however, there are issues with the first line read in a new run being incomplete, with the first part of the line missing, presumably because the seek landed in the middle of the line. (I would blame this on log rotation if I weren't already explicitly checking for an inode change to catch that.)
What's the best/most straightforward way to deal with this (without defeating its purpose by always reading the file from the beginning)?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reading only new lines from a file
by snopal (Pilgrim) on Aug 31, 2007 at 21:14 UTC | |
by dsheroh (Monsignor) on Aug 31, 2007 at 22:04 UTC | |
|
Re: Reading only new lines from a file
by bruceb3 (Pilgrim) on Sep 01, 2007 at 00:53 UTC | |
by dsheroh (Monsignor) on Sep 01, 2007 at 01:32 UTC | |
by bruceb3 (Pilgrim) on Sep 01, 2007 at 01:47 UTC | |
|
Re: Reading only new lines from a file
by jdporter (Paladin) on Aug 31, 2007 at 22:37 UTC | |
by dsheroh (Monsignor) on Sep 01, 2007 at 01:12 UTC | |
|
Re: Reading only new lines from a file
by FunkyMonk (Bishop) on Aug 31, 2007 at 21:02 UTC | |
by shmem (Chancellor) on Sep 01, 2007 at 09:31 UTC | |
by dsheroh (Monsignor) on Aug 31, 2007 at 21:58 UTC |