Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: OT: Design question

by SpanishInquisition (Pilgrim)
on Sep 30, 2004 at 20:06 UTC ( [id://395465]=note: print w/replies, xml ) Need Help??


in reply to OT: Design question

Event loops also lend themselves to hashes of anonymous subroutine references (in this case, used as callbacks). Hashes of anonymous subroutines are one of my favorite idioms.

$callbacks = { 567 => sub { }, 568 => sub { }, 569 => sub { } }; # as an exercise for the reader # if defined event id callback, invoke callback # else do what you will to indicate no handler is # registered, log whatever.
callbacks can be keyed off of event ID's, names, or some stringification of a class -- it doesn't matter. Anyhow, this is efficient and very fast, much better than lots of if's, and much cleaner than 100 subclasses of a base event class! It's almost like a bucket sort filled with function pointers. Wheee! (You could do this too, use an array instead of a hash, your choice...)

You may also be interested in POE... but I'm not really a big fan of it.

If the differences between events are subtle and you don't want to repeat code, it may be issue to have a multi-level set of callbacks, whose purpose it is to route events to different types of more specialized event handlers. (Basically seperate your DeathOfWorld events from your ItsAllGood and PleaseHelpMe and IWantACookie events, and then have the handlers for these commands implement a more simplistic callback mechanism.

Thinking in terms of patterns alone will get you roped into spending 80% of your time in the design process. Think in C, Think in C, Think in C, Think in C, there will be an answer, Think in C.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-19 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found