Perl File::Monitor module notifying multiple notifications for the new file created whereas i expect one notification -


below code snippet! using file creatiing notification using perl module file::monitor.

#!/usr/bin/perl  use strict; use warnings; use file::monitor; use file::basename; use time::hires qw {usleep};  $pid,@pids;   sub textfile_notifier { ($watch_name, $event, $change) = @_;   @new_file_paths = $change->files_created; #the change object has property called files_created,                                               #which contains names of new files $path (@new_file_paths) {     ($base, $fname, $ext) = fileparse($path, '.log'); # $ext "" if '.txt' extension                                                          # not found, otherwise it's '.txt'.     if ($ext eq '.log') {         print "$path created\n";     #-----------------------------------------forking part #-----------------------------------------     defined ($pid = fork()) or die "couldn't fork: $!";     if ($pid == 0) { #then in child process         print "loop got executed\n";     }     else {  #then in parent process, $pid pid of child         push @pids, $pid;     }     #-----------------------------------------forking part ends here #-----------------------------------------     } } }  $monitor = file::monitor->new();  $monitor->watch( { name        => '/home/goudarsh/desktop/logs/', recurse     => 1, callback    => {files_created => \&textfile_notifier},  #event => handler 1  } );  $monitor->scan; while (1) { $monitor->scan; #scanning directory 1 #sleep(2); #usleep(10_000); #$microseconds = 750_000; $pid (@pids) {     waitpid($pid, 0)  #0 => block } } 

file creation

=============================

touch 1.log   touch 2.log   touch 3.log   touch 4.log   

output of script

=========================

/home/goudarsh/desktop/logs/1.log created   /home/goudarsh/desktop/logs/2.log created   /home/goudarsh/desktop/logs/2.log created   /home/goudarsh/desktop/logs/3.log created   /home/goudarsh/desktop/logs/3.log created   /home/goudarsh/desktop/logs/3.log created   /home/goudarsh/desktop/logs/4.log created   /home/goudarsh/desktop/logs/3.log created   /home/goudarsh/desktop/logs/4.log created   /home/goudarsh/desktop/logs/4.log created   /home/goudarsh/desktop/logs/4.log created   /home/goudarsh/desktop/logs/4.log created   /home/goudarsh/desktop/logs/4.log created   /home/goudarsh/desktop/logs/4.log created   /home/goudarsh/desktop/logs/4.log created       

where expecting 1 notification/alert printed 1 file on terminal. suspect there bug in "forking part" section!

any idea where's doing wrong ?

here's solved problem

use strict; use warnings; use file::monitor; use file::basename; use time::hires qw {usleep}; $script = '/homw/goudarsh/desktop/script.pl'; sub textfile_notifier { ($watch_name, $event, $change) = @_;   @new_file_paths = $change->files_created; #the change object has property called files_created,                                           #which contains names of new files $path (@new_file_paths) { ($base, $fname, $ext) = fileparse($path, '.log'); # $ext "" if '.txt' extension                                                      # not found, otherwise it's '.txt'. if ($ext eq '.log') {     print "$path created\n";     system("perl $script $path \&"); } }} $monitor = file::monitor->new(); $monitor->watch( { name        => '/home/goudarsh/desktop/logs/', recurse     => 1, callback    => {files_created => \&textfile_notifier},  #event => handler 1  } );  $monitor->scan; while (1) { $monitor->scan; #scanning directory 1 sleep(2); #usleep(10_000); #$microseconds = 750_000;  } 

all wanted execute $script $path argument , used forking creating child process wrong concept of using fork not needed! without forking can whatever required , happy learning new things


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -