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


in reply to 'better mousetrap': how to perform timed event

It's time for everyone's favorite "i love my hammer, everything looks like a nail" post :) I have sung the praises of POE numerous times, and I am here once again to do so. POE is an event driven framework, you my friend, have events... so, I think that this sample code might be the kind of thing that you are looking for.
#!/usr/bin/perl -w use strict; use POE; use POE::Session; use POE::Wheel::FollowTail; sub usage { "test.pl file_to_watch timeout [debug]\n"; } my $file_name = shift(@ARGV) or die usage(); my $time_out = shift(@ARGV) or die usage(); my $debug = shift(@ARGV) || 0; my $to_match = "I AM LOOKING FOR THIS"; my $session = POE::Session->create( inline_states => { #Called upon POE Kernel initialization, this method #is what we use to set up our environment. _start => sub { my ($heap, $session) = @_[ HEAP, SESSION ]; print "Starting parent session ".$session->ID."\n" if ($debug >= 1); #POE::Wheel::FollowTail is one of those great POE #modules. It basically will sit there and emit an #event every time that a "record" has been added to the #file. Since I did not give it a Filter, it is just #going to read line by line, but you could do some crazy #things with it :) $heap->{tail} = POE::Wheel::FollowTail->new( Filename => $file_name, PollInterval => 1, InputEvent => "input_event", ); }, #This is the event that will be called every time the wheel #emits an InputEvent. It's just a cheesy skeleton, but it #should give you a good idea as to what *you* need to do. input_event => sub { my ($kernel, $heap, $input) = @_[ KERNEL, HEAP, ARG0 ]; print "Input: $input" if ($debug >= 2); if ($input =~ /$to_match/o) { #IMPORTANT: this is where I'm setting the delay event $kernel->delay(do_action => $time_out); print "input matched in P::W::F::InputEvent\n" if $debug; } else { print "input did not match in P::W::F::InputEvent\n" if $debug; } }, #After every line that matches, I will be called after #$time_out time :) do_action => sub { print "I am doing some action!\n" if $debug; }, }, ); $poe_kernel->run();