Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Cutting repetition in POE coding

by BUU (Prior)
on Nov 26, 2005 at 11:48 UTC ( #511869=perlquestion: print w/ replies, xml ) Need Help??
BUU has asked for the wisdom of the Perl Monks concerning the following question:

Here's the scenario. I have an event handle that, based on certain conditions, should send an event to another session with specific arguments, and then exit the current subroutine. The perl implementation is simple:
if( $condition ) { $kernel->post( $s, event => @args ); return; }
The problem is, I want to do this multiple times, with different conditions and arguments. Obviously I could type this out multiple times, but surely we all agree cut and pasting code is a bad idea. So my question to you is, how can I best achieve this in the smallest amount of code?

My first attempt involved definining a subroutine:
sub EVENT { $kernel->post( shift, event => @_ ) }; #basically ... EVENT $session, "argh" if $condition;
This is nice and short, as it should be, but it lacks the temrinating 'return' and I can not devise a good method to make the sub EVENT do it.

Any bright ideas?

Comment on Cutting repetition in POE coding
Select or Download Code
Re: Cutting repetition in POE coding
by BrowserUk (Pope) on Nov 26, 2005 at 12:06 UTC

    Why not

    return EVENT $session, "argh" if $condition;

    If the return value will be acted upon, then arrange the appropriate return value from the sub

    sub EVENT { $kernel->post( shift, event => @_ ); 0; }; # whatever valu +e is appropriate

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Cutting repetition in POE coding
by Anonymous Monk on Nov 26, 2005 at 12:14 UTC
    sub check_cond { my $callback = shift; my $kernel = shift; my $session = shift; my $result = $callback->(); if ($result) { $kernel->post( $session, event => @_ ); } return $result; } # then check_cond(sub { 2 > 1 }, $kernel, $session, @args) and return;
      Your sub name, check_condition(), does not imply to the reader that there is a side-effect, namely, posting an event to the kernel.

      --
      [ e d @ h a l l e y . c c ]

Re: Cutting repetition in POE coding
by johnnywang (Priest) on Nov 26, 2005 at 20:16 UTC
    I guess your question is how to force the return of the caller from the callee? I had similar problems before and the only one I came up was the equivalent of (not really a solution):
    EVENT($session, "argh"),return if $condition;
Re: Cutting repetition in POE coding
by robin (Chaplain) on Nov 26, 2005 at 23:26 UTC
    It does sound rather as though you're looking for a way to make a sub force its caller to return. You may be interested, amused, and/or horrified to know that Want provides an (undocumented) way to do just this. If you call Want::double_return(), then the next ordinary return will return from the calling sub.

    For example, the code:

    use Want; sub foo { print "Entering foo\n"; bar(); print "Leaving foo\n"; return "foo did this"; } sub bar { print "Entering bar\n"; print "Leaving bar\n"; Want::double_return(); return "bar did this"; } print "About to call foo()\n"; my $r = foo(); print "foo() returned '$r'\n";
    prints the following:
    About to call foo() Entering foo Entering bar Leaving bar foo() returned 'bar did this'

    Update: so in your case, you could write

    sub EVENT { $kernel->post( shift, event => @_ ); Want::double_return(); return; }

Re: Cutting repetition in POE coding
by nicholasrperez (Monk) on Nov 26, 2005 at 23:29 UTC
    Rigorously checking that condition is probably unavoidable. It sounds like this core event is tied to a wheel or perhaps a component that is feeding you information and then you are delegating the processing of that data to other specialized sessions. Inherently, there is nothing wrong with an if/else statement based on that condition even if it is a bit repetitive. The problem with trying to do overly clever based event dispatching is that unless you specifically validate the incoming information, someone maybe able to send you malformed data that causes your program to fire the wrong event. And then if you are going through all the trouble of validating the information you might as well go back to the if/else statement to do the dispatching.

    As an aside, if you really have that many events and sessions to which you are dispatching, then perhaps you need to conglomerate a bit more and build a second tier for processing and have your events trickle down to keep things a bit more simple. Of course without an overall picture it is difficult to know if this really is the case with your program or not.

    -- Nick
Re: Cutting repetition in POE coding
by rcaputo (Chaplain) on Nov 27, 2005 at 17:52 UTC

    It sounds like you're reinventing POE::NFA, which dispatches events to different handlers depending on a session-scoped state.

    It's hard to pinpoint a proper solution since you're asking about a specific technology rather than describing the problem to be solved.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://511869]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2014-07-28 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (196 votes), past polls