Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

(USING POE!) Re: 'better mousetrap': how to perform timed event

by eduardo (Curate)
on Apr 23, 2003 at 16:05 UTC ( #252613=note: print w/ replies, xml ) Need Help??


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();


Comment on (USING POE!) Re: 'better mousetrap': how to perform timed event
Download Code
(jeffa) Re: (USING POE!) Re: 'better mousetrap': how to perform timed event
by jeffa (Chancellor) on Apr 23, 2003 at 23:59 UTC
    Awesome ... i only have one nit-pick. ;)

    Change the input match regex from:

    $input =~ /$to_match/o
    to:
    $input =~ /\Q$to_match\E/
    This will allow you to match metachars as regular characters. I also (rather cargo-cultish-ly) avoid using the o modifier. ask tye :D

    eduardo++

    maybe this should be added to POE Central?

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Ah yes... \Q and \E. I'll be honest, I'm not nearly crafty enough with regular expressions to have thought of that. As far as my perling goes, it has been rather surprisingly devoid of regular expressions. I've probably written 20-30k lines of perl code in my life, and alltogether, I've probably written 50 regular expressions in my code. Ah well, that's what we have maverick for :)

      I am however curious about the /o modifier on the re. I mean, if we know the "event" that the user is looking for, what is the reason for *not* using it? After all, as the code has been specified, it's a non-mutable trigger... is it simply an issue of pre-emptive maintenance coding headache prevention? I don't think I know tye nearly well enough to just badger him needlessly when I have you to thwack about :)

        Heh heh ... thwack away! ;)

        "... is it simply an issue of pre-emptive maintenance coding headache prevention?"

        I think so. What if you suddenly decide that the string you need to match needs to be dynamic? You forget to remove the o modifer and possibly spend a lot of time trying to find the bug when your regex stops matching. Here is some code to demonstrate:
        @data = qw(foo bar baz qux); { print "search: "; chomp ($in = <STDIN>); last unless $in; print $_,$/ for grep /\Q$in\E/o, @data; redo; }
        I figure why bother with such premature optimizations if they have the potential to bite.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: (USING POE!) Re: 'better mousetrap': how to perform timed event
by mugwumpjism (Hermit) on Apr 24, 2003 at 08:09 UTC

    Nice. POE is indeed a very good way to implement user-space threading.

    $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";

      but total overkill for this application.

        :)

        You are entitled to your opinion. I personally don't agree. Maybe it's a limitation caused by my own incompetence, but being able to think of this task as "events" and transitions between those events, in the manner which POE development prescribes, has removed a great deal of complexity and overhead from a great many tasks I've personally had to tackle. POE doesn't add considerable processing overhead (we're tailing a file, the Von Neumann bottleneck is going to make sure we're IO bound), it makes the structure painfully obvious to the future reader: "there is a file that is being tailed, and upon hitting some regular expression, we delay some named event by $timeout time," and it's ridiculously portable! It worked on my WinXP box where I happen to code for a living (*you* try to get alarms on perl 5.6.1 on Win32!) And I can pretty much guarantee, it would work on *most* OSs pretty cleanly, due to the use of a comprehensive and rather brilliant 3rd party framework.

        I'll be honest, I'm relatively certain that I've been trolled. But, you did raise a good point, why use POE when a few while loops, if statements, and whatnot would probably do? Because it simplifies the task for *me*... and that's what this is all about, what makes eduardo happy. :)

Re: (USING POE!) Re: 'better mousetrap': how to perform timed event
by snafu (Chaplain) on Apr 26, 2003 at 11:18 UTC
    Well Eduardo, I took your code, tweaked it a bit, implemented it and when I got it to work I giggled like a little girl! I appreciate your help and pseudocode. I normally wouldn't waste thread space with this thank you but I thought it would be beneficial to everyone to know that POE is very nice!

    Thanks again!

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://252613]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-09-21 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (166 votes), past polls