I was wondering if there's a way to wait for a file to be updated, and then read from it once it's updated. So if I have file.txt
, I want to wait until something new is written to it, and then read it/process it/etc. Currently I am polling using Time::HiRes::sleep(.01)
, but I'm wondering if there's a better way. Thanks.
views:
104answers:
2http://search.cpan.org/perldoc?SGI::FAM and http://search.cpan.org/perldoc?Sys::Gamin may work on filesystems and UNIXes where inotify doesn't.
ephemient
2010-03-02 22:50:22
+2
A:
File::Tail will poll the file, but has a few advantages over your approach:
- The poll time is recomputed dynamically based on the number of lines written since the last poll
- If the file remains unchanged, polling will slow to avoid using up CPU
- File::Tail will detect if the file has been truncated, moved and/or recreated, and silently re-open the file for you
- It can tie a regular file handle which you can use like normal without any special API or syntax.
Example from the perldoc:
use File::Tail;
my $ref=tie *FH,"File::Tail",(name=>$name);
while (<FH>) {
print "$_";
}
rjh
2010-03-03 01:24:10