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();
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.