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)?
In reply to Reading only new lines from a file by dsheroh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |