IO::Lambda::Inotify - bridge between IO::Lambda and Linux::Inotify2
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.
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 ;
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 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.
inotify
If $timeout is specified, and expired, $error is set to 'timeout'
'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.
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.
IO::Lambda, Linux::Inotify2
Idea: Peter Gordon
Implementation: Dmitry Karasik, <dmitry@karasik.eu.org>.
To install IO::Lambda::Inotify, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO::Lambda::Inotify
CPAN shell
perl -MCPAN -e shell install IO::Lambda::Inotify
For more information on module installation, please visit the detailed CPAN module installation guide.