Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use File::ChangeNotify on Windows?

Tags:

perl

winapi

I installed File::ChangeNotify on Windows System and try to run the following code :

my $watcher =
     File::ChangeNotify->instantiate_watcher
         ( directories => [ 'C:\files' ],
             filter  => qr/\.txt$/
         );


 # # blocking
 while ( my @events = $watcher->wait_for_events() ) { print "new event"}

When I ran the script and try to create a new .txt file or modify a .txt file under c:\files the script didn't print anything.

like image 811
user3019319 Avatar asked Jan 15 '14 19:01

user3019319


1 Answers

It works for me (on linux) if I add this line:

$| = 1;

Then I see new event.

Refer to perldoc perlvar: $| or $OUTPUT_AUTOFLUSH

Here is the complete code:

use warnings;
use strict;
use File::ChangeNotify;

$| = 1;

my $watcher =
     File::ChangeNotify->instantiate_watcher
         ( directories => [ 'C:\files' ],
             filter  => qr/\.txt$/
         );


 # # blocking
 while ( my @events = $watcher->wait_for_events() ) { print "new event"}

UPDATE: As cjm astutely points out, adding a newline works as an alternative to $|:

 while ( my @events = $watcher->wait_for_events() ) { print "new event\n"}
like image 157
toolic Avatar answered Sep 20 '22 20:09

toolic