NAME
IO::Lambda::Inotify - bridge between IO::Lambda and Linux::Inotify2
SYNOPSIS
Easy
use strict ;
use IO::Lambda qw(:all);
use IO::Lambda::Inotify qw(inotify);
lambda {
context inotify( "/tmp/xxx", IN_ACCESS, 3600);
tail {
my ( $e, $error ) = @_;
if ($error) {
print "timed out\n" if $error eq 'timeout';
print "error:$error\n";
return;
}
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;
}
}-> wait;
Explicit inotify object, share with other users
use strict;
use IO::Lambda qw(:all);
use Linux::Inotify2;
use IO::Lambda::Inotify qw(inotify);
my $inotify = new Linux::Inotify2 or die "unable to create new inotify object: $!";
lambda {
context inotify($inotify, "/tmp/xxx", IN_ACCESS, 3600);
tail {
my ( $e, $error ) = @_;
if ($error) {
print "timed out\n" if $error eq 'timeout';
print "error:$error\n";
return;
}
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;
}
}-> wait;
inotify native watcher style - needs extra step with 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 ;
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 inotify_server
and let the resulting lambda running forever, or stop it when the $inotify object is not needed anymore.
inotify ([ $inotify ], $path, $flags [, $timeout ]) :: () -> ( $event, $error )
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 '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.
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.
SEE ALSO
AUTHORS
Idea: Peter Gordon
Implementation: Dmitry Karasik, <dmitry@karasik.eu.org>.