IO-Lambda-Inotify

 view release on metacpan or  search on metacpan

lib/IO/Lambda/Inotify.pm  view on Meta::CPAN

inotify native watcher style - needs extra step with C<inotify_server>.

   use strict ;
   use IO::Lambda qw(:all);
   use Linux::Inotify2;
   use IO::Lambda::Inotify qw(:all);
   
   sub timer {
       my $timeout = shift ;
       lambda {
           context $timeout ;
           timeout {
               print "RECEIVED A TIMEOUT\n" ;
           }
       }
   }
   
   # create a new object
   my $inotify = new Linux::Inotify2
      or die "unable to create new inotify object: $!";
   
   # add watchers
   $inotify->watch ("/tmp/xxx", IN_ACCESS, sub {
       my $e = shift;
       my $name = $e->fullname;
       print "$name was accessed\n" if $e->IN_ACCESS;
       print "$name is no longer mounted\n" if $e->IN_UNMOUNT;
       print "$name is gone\n" if $e->IN_IGNORED;
       print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
       
       # cancel this watcher: remove no further events
       $e->w->cancel;
   });
   
   my $server = inotify_server($inotify);
   $server->start;
   timer(10)->wait ;

=head1 DESCRIPTION

The module is a bridge between Linux::Inotify2 and IO::Lambda. It uses
lambda-style wrappers for subscribe and listen to inotify events, in the more
or less the same interface as Linux::Inotify2 does, but with extra timeout
capability for free.

The module can also be absolutely non-invasive, and one can just use the
non-blocking programming style advertized by Linux::Inotify2 . The only
requirements for the programmer is to register $inotify objects with
C<inotify_server> and let the resulting lambda running forever, or stop it when
the $inotify object is not needed anymore.

=head2 inotify ([ $inotify ], $path, $flags [, $timeout ]) :: () -> ( $event, $error )

C<inotify> creates and returns a lambda, that registers a watcher on $path using
$flags ( see Linux manpage for inotify ). On success, the lambda returns $event objects
of type Linux::Inotify2::Event (exactly as Linux::Inotify2 does), on failure, $event is undefined,
and $error is set.

If $timeout is specified, and expired, $error is set to C<'timeout'>

If no $inotify object is passed, then it is created automatically, and stays alive until
the end of the program. It is also reused for other such calls.

=head2 inotify_server( $inotify, ... ) :: () -> ()

Accepts one or more $inotify objects, creates a lambda that serves as a proxy for Linux::Inotify2
event loop. Use only when programming style compatible with Linux::Inotify2 is needed.

=head1 SEE ALSO

L<IO::Lambda>, L<Linux::Inotify2>

=head1 AUTHORS

Idea: Peter Gordon

Implementation: Dmitry Karasik, E<lt>dmitry@karasik.eu.orgE<gt>.

=cut



( run in 0.734 second using v1.01-cache-2.11-cpan-39bf76dae61 )