Not that this is directly in answer to your question, but...

I'd try to avoid any kind of callback interface (unless there is a really good reason to use it). And by that I mean either an explicitly named callback (passing a coderef), or an implicitly named callback (what you are calling "Interfaces"). In pretty much any case where you are writing a callback-style interface to your code, you could be (and, imho, should be) writing an "iterator"-style interface.

That is: instead of calling

DoStuff({@parameters, on_event=>\&handle_event});
Try constructing your code so that it can be used like
my $doer = new ThingThatDoesStuff ({@parameters}); while ($doer->GetNextEvent()) { # handle event }
Or, perhaps even better, support both kinds of interfaces (because, honestly, callbacks can be a little easier to work with if you are doing something simple, and you understand callbacks really well).

Why does this matter? Well, imagine someone else who wants to implement something that uses your code, and, for some reason, they aren't able to implement their module with a callback-style interface. (This comes up a lot, you know... you've got to make something with an interface that was already defined by someone else.) If you wrote your code such that it could only be used with callbacks (and without any sort of ->GetNext() type of interface available to it), then it is actually impossible (I know... not a word often used in the context of perl) to build an iterator-like interface around it. (If you want to prove me wrong: show me some code that actually uses File::Find, but presents an iterator interface.)

However, it is trivial to implement a callback-style interface around an iterator (based loosely on the previous, silly example):

package ThingWithCallbacks; use ThingAsIterator; sub DoThing { my ($params) = @_; my $callback = $params->{callback}; my $doer = new ThingAsIterator ({@parameters}); while (my $event = $doer->GetNextEvent()) { &$callback($event); } }
And that's all there is to it. So, you see... make an iterator interface, so that you don't make the decision of what interface everyone else has to use.
------------ :Wq Not an editor command: Wq

In reply to Re: Event based programming: Callbacks or "Interfaces"? by etcshadow
in thread Event based programming: Callbacks or "Interfaces"? by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.