Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: OT: Design questionby SpanishInquisition (Pilgrim) |
on Sep 30, 2004 at 20:06 UTC ( [id://395465]=note: print w/replies, xml ) | Need Help?? |
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 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.
In Section
Seekers of Perl Wisdom
|
|