defmer has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to set a directory monitor using file::monitor, but it does not seem to work.... the implementation is simple.....kindly let me know if u have any suggestions of what could be going wrong.
#!/usr/bin/perl use strict; use warnings; use File::Monitor; use File::Monitor::Object; my $monitor = File::Monitor->new(); $monitor->watch('/home/test/work/obsrv'); my $flag=1; while(1) { $monitor->scan; $flag=1; while ($flag){ for my $change ($monitor->scan){ print ("\n new file was created!!!!"); $flag=0; } } }
thank you

Replies are listed 'Best First'.
Re: File::Monitor does not work ...
by toolic (Bishop) on Mar 29, 2009 at 01:10 UTC
    it does not seem to work
    That's not very descriptive, as Corion already alluded to. Are you not getting a print out when you expect it? If so, you may be suffering from buffering. When I add $| = 1; to your code, it starts printing out when I expect it to.

    Do you really need those nested while loops?

Re: File::Monitor does not work ...
by Bloodnok (Vicar) on Mar 29, 2009 at 01:13 UTC
    10/10 for presentation, 0/10 for problem statement.

    It's only a guess, but your code appears to be in a tight loop - are you absolutely sure it's not working - try either printing to STDERR or auto-flushing the STDOUT buffer (by putting $| = 1; before you go into the loop).

    A user level that continues to overstate my experience :-))
Re: File::Monitor does not work ...
by Corion (Patriarch) on Mar 29, 2009 at 00:36 UTC

    Maybe we can get off to a better start if you tell us what you experience?