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

I'm trying to do something rediculously easy. I've tried to configure it, and can't get the output I'm looking for. I am trying to monitor a directory for when new file is created. When a new file is created in the directory, the file name(extension included) is saved into a variable. I would like to print this variable to screen for testing purposes as well.

I run the program from my command prompt, and all it does is return nothing to the screen. The code is below. Thanks for any help!

use File::Monitor; my $monitor = File::Monitor->new(); $monitor->watch( { name => 'C:/Path', recurse => 1, callback => { files_created => sub { my ($name, $event, $change) = @_; print "@_"; } } } );
I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: Please HELP! FILE::MONITOR Configuration
by hbm (Hermit) on Jan 21, 2009 at 19:30 UTC

    I've never used File::Monitor, but the documentation indicates you need to do $monitor->scan, once to get a snapshot, and subsequently to detect changes. From that, I assume you would do the second and subsequent calls (and sleep) in an infinite loop.

Re: Please HELP! FILE::MONITOR Configuration
by Not_a_Number (Prior) on Jan 21, 2009 at 22:19 UTC

    This seems to work:

    use strict; use warnings; use File::Monitor; my $monitor = File::Monitor->new(); $monitor->watch ( { name => 'C:/whatever', # or whatever callback => \&print_new, files => 1, } ); while ( 1 ) { $monitor->scan; sleep 10; # or however long you need } sub print_new { print join "\n", pop->files_created; print "\n"; }
      Outstanding! I tried it, and in 2 minutes, the program was up and running after fiddling with it for hours! Thanks so much, you get the vote! :) :)
      I love it when a program comes together - jdhannibal
Re: Please HELP! FILE::MONITOR Configuration
by zentara (Cardinal) on Jan 21, 2009 at 18:44 UTC
    You might want to print newlines to force printouts....you may be suffering from buffering.
    $monitor->watch( { name => 'C:/Path', recurse => 1, callback => { files_created => sub { my ($name, $event, $change) = @_; print "@_\n callback fired \n"; #will print right a +way, (on linux anyways) } } } );

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      It doesn't work, maybe because I'm using windows?
Re: Please HELP! FILE::MONITOR Configuration
by Not_a_Number (Prior) on Jan 21, 2009 at 22:12 UTC
    Please remove: Duplicate (see below). Sorry!