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();
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|