Today I spent some minutes in seeing what could be done with Linux::Inotify. Actually not much because all you can do with events is print them or know the name of the file affected. I googled a bit and learnt that with C, one can do a switch and test for every possible event in order to do this or that. AFAICT, with Linux::Inotify there's no way of knowing which event occurred, what makes the module pretty useless. Except that I'm missing something, of course. The doc says "An Linux::Inotify::Event object has some interesting data members: mask, cookie and name" but I shouldn't be accessing them directly but through method calls, should I? (I'm pretty newbie to this OO thingy, precisely these days I'm reading on the subject).
So I decided to add this lines to the Event.pm module, where I think they belong:
sub type(%) { my $self = shift; return $reverse{$self->{mask}}; }
Now, one can do something more useful than just printing what happened:
## This code has no checking at all. use strict; use warnings; use Linux::Inotify; my $notifier = Linux::Inotify->new(); my $watch = $notifier->add_watch('.', Linux::Inotify::CREATE); sub do_create { print "File $_[0] created\n"; unlink $_[0]; ## Just for fun } my %dt = ( create => \&do_create, ); for (;;) { my @events = $notifier->read(); for (@events) { $dt{$_->type()}->($_->fullname()); } }
Now, is there a way of knowing what happened without my sub type?
--
David Serrano
In reply to Linux::Inotify usage. by Hue-Bond
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |