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

I have the code below that I am attempting to use to show me what file in a particular directory has been added. Instead of showing me the added file, it just prints the dir name when a file is added. How do I go about printing the name of the actual file that was changed? Thank you.
use File::Monitor; my $monitor = File::Monitor->new(); my $path="c:\\temp"; # Watch a directory $monitor->watch( { name => $path, recurse => 1, callback => { files_created => sub { my ($name, $event, $change) = @_; # Do stuff } } } ); # First scan just finds out about the monitored files. No changes # will be reported. $monitor->scan; # Later perform a scan and gather any changes while ($path) { @new_files = $monitor->scan; for my $file ( @new_files ) { print $file->name, " created\n"; } sleep 1; }

Replies are listed 'Best First'.
Re: File::Monitor Question
by Anonymous Monk on Jan 10, 2012 at 19:02 UTC
    Reading the documentation for File::Monitor, it appears that you need to call $file->files_created to get a list of the files created in the directory given by $file->name.
Re: File::Monitor Question
by i5513 (Pilgrim) on Jan 10, 2012 at 20:01 UTC
      OK, I'm getting closer. I changed the line above of
      print $file->name, " created\n";
      to
      print $file->files_created, " created\n";
      and now it is printing the newly created file. Now I am having a problem getting the name of that file saved as a variable. I'm not experienced enough to understand how this thing works. I thought I could just do this:
      $new_file = $file->files_created
      But that doesn't work. How can I save that to a variable that I can work with?
        I used: my $file = @{$change->{delta}->{files_created}}[0]; and it worked. BR