perlmad has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!!!

I have been using File::Monitor perl module to monitor a directory and its working well when create or delete a file to a given directory ,if i update a file it shows nothing and i have referred all the links but i don't know what i did wrong

Here is my code

#! usr/bin/perl use strict; use warnings; use File::Monitor; use File::Monitor::Delta; use File::Monitor::Object; use Getopt::Long; use Config::IniFiles; use MIME::Lite; $file_list[$#file_list]=''; $temp=join('/',@file_list); $email_body=$email_body."\n File Name : $fil +e_name , Path : ".$temp."\n"; print "\n\n File Name : $file_name , Path : +".$temp."\n"; } $email_subject="Monitor Path : $path , Status : Added" +; &email; $email_subject=''; $email_body=''; undef @file_changes; undef @file_list; } if($file->files_deleted){ #print $file->files_deleted, " deleted\n"; print "Files Removed \n"; @file_changes=$file->files_deleted; $email_body="\nFiles Removed,\n"; for my $temp(@file_changes){ @file_list=split('/',$temp); my $file_name=$file_list[$#file_list]; $file_list[$#file_list]=''; $temp=join('/',@file_list); $email_body=$email_body."\n File Name : $fil +e_name , Path : ".$temp."\n"; print "\n\n File Name : $file_name , Path : +".$temp."\n"; } $email_subject="Monitor Path : $path , Status : Remove +d"; &email; $email_subject=''; $email_body=''; undef @changes; undef @file_list; } if($file->is_event('size')){ print "size change\n\n"; } if ($file->is_size) { my $name = $file->name; my $old_size = $file->old_size; my $new_size = $file->new_size; print "$name has changed size from $old_size to $new_size\ +n"; } } }

Actual Output

[abc@linux0059 monitor]$ perl code.pl --c config.ini Files Removed File Name : same1 , Path : /tmp/abc/ Email send : abc@google.com Files Created File Name : same1 , Path : /tmp/abc/ Email send : abc@google.com [abc@linux0059 monitor]$

Monitor Directory

[abc@linux0059 abc]$ ls folder log same1 same.txt sample2.txt simple [abc@linux0059 abc]$ rm same1 [abc@linux0059 abc]$ touch same1 [abc@linux0059 abc]$ echo "hai hello" > same1 [abc@linux0059 abc]$

Response Much appreciated!!!

Replies are listed 'Best First'.
Re: Directory Monitor
by Corion (Patriarch) on Jul 25, 2016 at 10:36 UTC

    The code you show cannot be the code you run. $file is never defined.

    Do you really need to include MIME::Lite in the program? Is the problem with sending an email or is the problem with getting the events?

    Please provide a short, self-contained program that we can run to reproduce your problem. It should be shorter than 20 lines.

      Corion I have added short code , problem with event not email

      #! usr/bin/perl use strict; use warnings; use File::Monitor; use File::Monitor::Delta; use File::Monitor::Object; my $path="/tmp/abc"; my $monitor = File::Monitor->new(); # Watch a directory $monitor->watch( { name => $path, recurse => 1 } ); # First scan just finds out about the monitored files. No changes # will be reported. $monitor->scan; # Later perform a scan and gather any changes while ($path) { my @changes = $monitor->scan; for my $file ( @changes ) { chomp($file); my @file_changes; my @file_list; if($file->is_event('size')){ print "size change\n\n"; } if ($file->is_size) { my $name = $file->name; my $old_size = $file->old_size; my $new_size = $file->new_size; print "$name has changed size from $old_size to $new_size\ +n"; } } }

        Personally, before checking for changes in file size, I would check and print what kinds of events the monitor sends you. Unfortunately, there is no easy way to ask the Delta about what it contains, so you will have to look at the raw contents using Data::Dumper and hope that they show what kinds of events get triggered by a change:

        for my $change (@changes) { use Data::Dumper; warn "Got a change:"; warn Dumper $change;

        Also, why are you calling chomp on an element of @changes? What do you expect @changes to contain?