in reply to Perl process to constantly detect the time change of a file?

You could cut out a lot of code by using stat - specifically "mtime", which is the "last modify time in seconds since the epoch". I would suggest putting a small sleep time in the loop as well, just so you're not constantly hitting your machine - unless it's absolutely crucial that you know the second when this file has changed.

my $file = "somefile.txt"; # Establish a base "last accessed time" my $lastaccessed = (stat($file))[9]; while(1) { if ((stat($file))[9] == $lastaccessed) { # The file is the same } else { # File is different } sleep 5; }

Check the docs for stat - it provides a lot of useful info about files.


Rich36
There's more than one way to screw it up...

Replies are listed 'Best First'.
Re: Re: Perl process to constantly detect the time change of a file?
by graff (Chancellor) on May 03, 2002 at 21:33 UTC
    Right -- of course one would usually want to make sure it goes like this (Rich36 assumes this goes without saying):
    while(1) { my $modtime = (stat($file))[9]; if ($modtime == $lastaccessed) { # The file is the same } else { # File is different; do whatever, and also: $lastaccessed = $modtime; } sleep 5; }