First, as pointed out below, the module is called Win32::ChangeNotify.
Second, ChangeNotify doesn't give you the time of LAST_WRITE. It will inform you when a file or directory or it's attributes, within a subdirectory or subtree changes. You can filter the changes that it notifies you of to only include LAST_WRITE. The notification itself does not tell you which file changed, only that a file changed. It then becomes up to you to discover which one did and act upon it.
This require you to record the state of that part of the file system you are interested in, within your program, and creating one or more ChangeNotify objects to monitor it, and the calling $ChangeNotifyObj->wait( timeout ); periodically to be informed if someting has changed. You then need to re-scan the directory or subtree in question and detect what changed by comparing it with your stored record.
Effectively, all the ChangeNotify object does is save you from having to scan the directory until you know that there is a reason to do so.
A little sample code may help.
#! perl -slw
use strict;
use Win32::ChangeNotify;
our $PATH ||= '.';
our $S = defined $S ? 1 : 0;
my $notify = Win32::ChangeNotify->new( $PATH, $S, 'FILE_NAME' );
my %last; @last{ glob $PATH . '/*' } = ();
while( 1 ) {
print('Nothing changed'), next
unless $notify->wait( 10_000 ); # Check every 10 seconds
$notify->reset;
print 'Something changed';
my @files = glob $PATH . '/*';
if( @files < scalar keys %last ) {
delete @last{ @files };
print 'These files where deleted: ';
print for keys %last;
}
elsif( @files > scalar keys %last ) {
my %temp;
@temp{ @files } = ();
delete @temp{ keys %last };
print 'These files where created: ';
print for keys %temp;
}
else {
print "A non-deletion or creation change occured";
}
undef %last;
@last{ @files } = ();
}
__END__
P:\test>changeNotify
Nothing changed
Something changed
These files where created:
./junk
Nothing changed
Nothing changed
Terminating on signal SIGINT(2)
Note: The above sample session, was monitoring the current directory, and I created a new file called junk.
Note also that as coded, this is looking for 'FILE_NAME' changes... this will inform you of creation, deletion and renamed files in the watched directory. You'll need to read the pod and change the filter argument to 'LAST_WRITE. to detect those. You will also need to record the last write times (see stat and file test operators specifically -M -A & -C for further information on how to obtain this).
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!
|