http://qs321.pair.com?node_id=748094

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

Dear Monks,

We are making some changes to our system and one of the objectives was to use POE::Component::DirWatch.
I tried the following code from the internet, modified to my needs.
Main objective of the script is to watch a directory and print the filenames that gets added to that directory

The script is able to watch a specified directory and when a file gets added to the directory the script responds.
One issue I'm facing is that it is not able to print the file names which came into the directory. Instead it prints "" and empty session id.

use warnings; use strict; use POE; use POE::Component::DirWatch; POE::Session->create( inline_states => { _start => \&init_watcher, watch_dir => \&watch_dir, }, ); sub init_watcher { my ($kernel, $heap, $session, $sender) = @_[KERNEL, HEAP, SESSION, +SENDER]; warn "Queue 1 starting (session id " . $session->ID . ")"; # watch a directory for this queue $kernel->yield('watch_dir', '/tmp/queue1'); } sub watch_dir { my ($kernel, $heap, $session, $queue) = @_[KERNEL, HEAP, SESSION, + ARG0]; # start watching the directory POE::Component::DirWatch->new( alias => 'dirwatch', directory => $queue, file_callback => \&found_file, interval => 1, ); } sub found_file { my ($kernel, $heap, $session, $sender, $file) = @_[KERNEL, HEAP, S +ESSION, SENDER, ARG1]; # warn "Found '$file' for " . $session->ID . " (from " . $sender-ID + . ")\n"; warn "Found '$file' for $session->ID\n"; } $poe_kernel->run(); exit(0);
Below is the output of the script without the filename(s) and the session ID.
-bash-3.00$ ./dirwatch_test1.pl Queue 1 starting (session id 2) at ./dirwatch_test1.pl line 19. Found '' for ->ID Found '' for ->ID
Please let me know whats the error. Also let me know if there is a shorter way of doing this.
I'm using verion 0.20000 of POE::Component::DirWatch
Thanks for your help.

Replies are listed 'Best First'.
Re: Watching a directory with POE::Component::DirWatch
by bingos (Vicar) on Mar 04, 2009 at 17:09 UTC

    POE::Component::DirWatch's file_callback parameter requires a plain coderef. The coderef you are supplying is a POE type handler

    Your found_file sub should probably be:

    sub found_file { my $file = shift; warn "Found '$file'\n"; }

    To do it a POE way would be to supply a postback or callback to POE::Component::DirWatch so it sends an event back to your session:

    use warnings; use strict; use POE; use POE::Component::DirWatch; POE::Session->create( inline_states => { _start => \&init_watcher, watch_dir => \&watch_dir, found_file => \&found_file, }, ); sub init_watcher { my ($kernel, $heap, $session, $sender) = @_[KERNEL, HEAP, SESSION, +SENDER]; warn "Queue 1 starting (session id " . $session->ID . ")"; # watch a directory for this queue $kernel->yield('watch_dir', '/tmp/queue1'); } sub watch_dir { my ($kernel, $heap, $session, $queue) = @_[KERNEL, HEAP, SESSION, + ARG0]; # start watching the directory POE::Component::DirWatch->new( alias => 'dirwatch', directory => $queue, file_callback => $session->postback('foun +d_file'), interval => 1, ); } sub found_file { my ($kernel, $heap, $session, $sender, $args) = @_[KERNEL, HEAP, S +ESSION, SENDER, ARG1]; my $file = shift @{ $args }; # warn "Found '$file' for " . $session->ID . " (from " . $sender->I +D . ")\n"; warn "Found '$file' for $session->ID\n"; } $poe_kernel->run(); exit(0);
      Thankyou bingos and others for your explanation.

Re: Watching a directory with POE::Component::DirWatch
by Anonymous Monk on Mar 04, 2009 at 12:43 UTC
    sub found_file { use Data::Dumper; die Data::Dumper->new([@_])->Indent(0)->Dump; }
      POE::Component::DirWatch#file_callback
      Optional read-write code reference to call when a file is found. The code reference will passed a single argument, a Path::Class::File object representing the file found. It usually makes most sense to process the file and remove it from the directory to avoid duplicate processing