#!/opt/perl_5.18.2/linux50/bin/perl
use strict;
#use warnings;
use File::ChangeNotify;
$| = 1;
my $watcher = File::ChangeNotify->instantiate_watcher(
directories => [ '/var/icc_shantesh/logs' ],
filter => qr/\.log/,
);
while ( my @events = $watcher->wait_for_events ) {
print "loop got executed\n";
print "File name: $_\n";
}
I am using File::ChangeNotify
in the above code in my R&D environment. The script monitors a folder and informs whenever there is a change. I want to know the name of the file that has changed, and its updated content
The @events
array has the information you want:
while (my @events = $watcher->wait_for_events){
print "loop got executed\n";
foreach my $event (@events) {
print "File name: " . $event->path . "\n";
}
}
The array contains File::ChangeNotify::Event objects, and they have a path
accessor for filename, and a type
for what kind of change has been done.
You could look at the example on the man page and rewrite your code to be like that:
for my $event ( $watcher->new_events() ) {
print "File name: ", $event->path(), ' - ', $event->type(), "\n";
}
$_
is the default variable of perl, but when you explicit set the variable like you do in the loop (my @events = ...
) $_
is not being used.