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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.