title: Watching multiple files with inotify in perl
body: <p>I need to watch multiple files in Perl, and I am using <a href="http://search.cpan.org/perldoc?Linux%3a%3aInotify2" rel="nofollow">Linux::Inotify2</a>. However I am encountering an issue in that the first file being watched needs to be modified and hit, then the second, then the first etc etc </p>
  
  <p>For example if the second file is changed before the first, it will not trigger out, or if the first is triggered twice in a row without the second being triggered in between.</p>
  
  <p>This is the section of code I am using which is having this issue.</p>
  
  <pre><code>my $inotify = new Linux::Inotify2;
  my $inotify2 = new Linux::Inotify2;
  $inotify-&gt;watch ("/tmp/rules.txt", IN_MODIFY);
  $inotify2-&gt;watch ("/tmp/csvrules.out", IN_MODIFY);
  
  while () {
    my @events = $inotify-&gt;read;
    unless (@events &gt; 0){
      print "read error: $!";
      last ;
    }
  
    foreach $mask (@events) {
      printf "mask\t%d\n", $mask;
  
      open (WWWRULES, "/tmp/rules.txt");
  
      my @lines = &lt;WWWRULES&gt;;
      foreach $line (@lines) {
        @things = split(/,/, $line);
        addrule(@things[0], @things[1], @things[2], @things[3], trim(@things[4]));
        print "PRINTING: @things[0], @things[1], @things[2], @things[3], @things[4]";
        close (WWWRULES);
        open (WWWRULES, "&gt;/tmp/rules.txt");
        close (WWWRULES);
      }
    }
  
    my @events2 = $inotify2-&gt;read;
    unless (@events2 &gt; 0){
      print "read error: $!";
      last ;
    }
    foreach $mask (@events) {
      printf "mask\t%d\n", $mask;
      open (SNORTRULES, "/tmp/csvrules.out");
  
      my @lines2 = &lt;SNORTRULES&gt;;
      foreach $line2 (@lines2) {
        @things2 = split(/,/, $line2);
        addrule("INPUT", @things2[0], @things2[1], @things2[2], trim(@things2[3]));
        print "PRINTING: INPUT, @things2[0], @things2[1], @things2[2], @things2[3]";
  
        close (SNORTRULES);
        open (SNORTRULES, "&gt;/tmp/csvrules.out");
        close (SNORTRULES);
      }
    }
  }
  </code></pre>
  
  <p>Ideally I would like to be watching 3 files but as I cannot get 2 working it seems a little pointless at this stage.</p>
  
  <p>Thanks for any help!</p>