Example code using Perl’s Linux::Inotify2 module with POE

The examples in Linux::Inotify2 are for Event, Glib::IO and a manual loop. With the help of rcaputo, tye and Animator over on Perlmonks.org, I was able to get Linux::Inotify2 to work with POE. 🙂


use strict;
use warnings;

use Linux::Inotify2;
use POE;


  ( inline_states =>
      { _start => sub {
            $_[HEAP]{inotify} = new Linux::Inotify2
              or die "Unable to create new inotify object: $!";

            $_[HEAP]{inotify}->watch("/tmp/j", IN_CLOSE_WRITE, $_[SESSION]->postback("watch_hdlr"))
              or die "Unable to watch dir: $!";

            my $inotify_FH;
            open $inotify_FH, "< &=" . $_&#91;HEAP&#93;{inotify}->fileno or die "Can't fdopen: $!\n";
            $_[KERNEL]->select_read( $inotify_FH, "inotify_poll" );
        inotify_poll => sub {
        watch_hdlr => \&watch_hdlr,

sub watch_hdlr {
  my $event = $_[ARG1][0];

  my $name = $event->fullname;

  print "$name was accessed\n" if $event->IN_ACCESS;
  print "$name is no longer mounted\n" if $event->IN_UNMOUNT;
  print "$name is gone\n" if $event->IN_IGNORED;
  print "$name is new\n" if $event->IN_CLOSE_WRITE;
  print "events for $name have been lost\n" if $event->IN_Q_OVERFLOW;


exit 0;

Create files for testing:

dd if=/dev/zero of=/tmp/j/p bs=1M count=2
2+0 records in
2+0 records out
2097152 bytes (2.1 MB) copied, 0.112971 s, 18.6 MB/s


/tmp/j/p is new
/tmp/j/p is new
Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.