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

What is the right way to remove a single watch. This doesn't work. After "canceling" only one watch, the the poll loop stops blocking.
use strict; use warnings; use Linux::Inotify2; my $inotify=new Linux::Inotify2 or die "Unable to create new inotify object: $!"; my $watch_one= $inotify->watch("/var/tmp/one", IN_MODIFY , sub {print "one modified\n"} ) or die "one $!\n"; my $watch_two= $inotify->watch("/var/tmp/two", IN_MODIFY, sub {print "two modified\n"} ) or die "two $!\n"; $watch_one->cancel; # Canceling a single watch stops all activity 1 while $inotify->poll; print STDERR "loop stoped blocking\n";

Replies are listed 'Best First'.
Re: Linux::Inotify2 remove single watch
by Anonymous Monk on Jul 23, 2016 at 18:45 UTC

    Linux::Inotify2 is a very thin wrapper for the inotify system calls. The poll returns with zero events because inotify_rm_watch causes an event on the watch descriptor (but this event is discarded).

    Perhaps a modification to loop logic can fix your problem:

    $inotify->poll while !$done;

      Many thanks! That worked. So if I want the loop forever (it is in a daemon), it would be :
      $inotify->poll while 1
      Right?
        This is correct - the Linux::Inotify2 documentation was consistently wrong, read and poll both croak on errors. This has been fixed in release 2.0.