laziness, impatience, and hubris | |
PerlMonks |
Most efficient File Modified check (w/o warnings)by Cefu (Beadle) |
on Nov 14, 2007 at 15:33 UTC ( [id://650771]=perlquestion: print w/replies, xml ) | Need Help?? |
Cefu has asked for the wisdom of the Perl Monks concerning the following question:
Monks, I'm looking for the most efficient way to repeatedly check a file for a new last-modified date. I currently have a script which continuously monitors a fast-growing log file for new lines and checks them using some parameters loaded from a config file. As such it uses (stat(FILE))[7] on the log file to notice new lines and (stat(FILE))[1) to check for a new inode number when the log gets rotated. I plan to 'daemonize' this when I get it working and I want to support updating the config file on the fly. To do this I note the modified date in $lastModConfig when I open it then check as follows: if( (stat($configFileName))[9] != $lastModConfig ){ ...re-process config... } However, this can produce a warning about using an undefined value with a not equal comparison if the config file is ever missing (renamed etc.) Of course I could fix this by checking for definedness first with: if( defined (stat($configFileName))[9] && (stat($configFileName))[9] != $lastModConfig ){ ...re-process config... } but then I'd be calling stat twice. Since this check happens every couple of seconds (remember the log file grows fast) I don't want to eat up any cycles I don't have to. Is there a better (less CPU time) way to pre-check the file for existance than calling stat($configFileName) or is the an altogether better way to check for modifications to the config file? Perhaps I should just accept an extra second or two of CPU time every hour to do the second check. Or am I being entirely too neurotic about the warnings? They are just warnings after all and the script functions as intended. I could, of course, just throw in a no warnings 'uninitialized'; or even let the warnings drop into the void since STDERR will go to null when this script is a daemon. I'd like to know what course those with more experience would take. Thanks, Cefu
Back to
Seekers of Perl Wisdom
|
|