#!/usr/bin/perl use Cwd; use Data::Dumper; use Linux::Inotify2; use threads; use Thread::Queue; use Time::HiRes; use strict; use warnings; $|++; my($ToBeDone_q); my(@Worker_a); { $ToBeDone_q=Thread::Queue->new(); push(@Worker_a,threads->new(\&Watchdog,$ToBeDone_q)); }; # Process the queued up signals while (my($Request_s)=$ToBeDone_q->dequeue()) { warn ''.Data::Dumper->Dump([\$Request_s],[qw(*Request_s)]).' '; if ($Request_s eq 'All done!') { # Took one "All Done!" off last; } else { # Perform the work if possible my(%_h); eval $Request_s; # Do what you want to do with the results from the watchdog ToBeDone(); }; }; grep {$_->join;} @Worker_a; # Just In Case! while ( my(@list)=threads->list()) { print "$#list\n"; grep { $_->join } @list; }; exit; sub Watchdog { my($ToBeDone_q)=@_; print "Starting thread.\n"; my $Inotify_o=Linux::Inotify2->new(); $Inotify_o->watch(Cwd::abs_path('.'),IN_ALL_EVENTS); my $Done_f:shared; # Ctrl/c signal handler 1 - doesn't seem to work #$SIG{INT}=$SIG{TERM}=$SIG{HUP}=sub { # ? # # print "SignalHandler 1 tripped!\n"; # lock $Done_f; # $Done_f=1; # print "Attempting to unblock Inotify\n"; # $Inotify_o->blocking(0); # print "Inotify unblocked!\n" # }; # SignalHandler: Done # Ctrl/c signal handler 2 - doesn't seem to work ... sub _Sighandler { print "SignalHandler 2 tripped!\n"; lock $Done_f; $Done_f=1; print "Attempting to unblock Inotify\n"; $Inotify_o->blocking(0); print "Inotify unblocked!\n" }; # SignalHandler: Done #$SIG{INT}=$SIG{TERM}=$SIG{HUP}=\&_Sighandler; print "Starting while.\n"; while (!$Done_f) { print "Waiting on read.\n"; my @events_ao = $Inotify_o->read; print "read has been unblocked.\n"; unless (@events_ao > 0){ print "read error: $!"; } else { # To Do! foreach my $event_o (@events_ao) { print $event_o->fullname . " was modified\n" if $event_o->IN_MODIFY; if ($event_o->fullname =~ m{\.pm$} && $event_o->IN_CLOSE_WRITE) { # Have a module } elsif ($event_o->fullname =~ m{\.(?:cgi|pl)$} && $event_o->IN_CLOSE_WRITE) { # Have a script }; }; }; print "Continuing while.\n"; }; print "Exited while.\n"; print "Exiting thread.\n"; return; }; __END__