Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Event based programming: Callbacks or "Interfaces"?

by etcshadow (Priest)
on Jun 01, 2004 at 14:43 UTC ( #358104=note: print w/replies, xml ) Need Help??


in reply to Event based programming: Callbacks or "Interfaces"?

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

Replies are listed 'Best First'.
Re: Re: Event based programming: Callbacks or "Interfaces"?
by BUU (Prior) on Jun 01, 2004 at 21:04 UTC
    I understand completely where you are coming from, I read the original discussion about hating on File::Find, but to be honest, I have no freaking idea how I'm supposed to make an iterator interface for this thing.

    The code in question is almost certainly going to respond to only one event at a time. The code will probably be eval'd once for each event needful, so it'll never have any access to the "next event". I suppose in rare cases it might handle 2 or 3 events, but I doubt it. This is primarily for security purposes as the code handling the event is going to be untrusted, so I don't want it hanging around for very long.
      Fair enough... which is why I qualified my statement as "unless there is a really good reason not to".

      But just to throw in one parting jibe: untrusted code is another reason to avoid callbacks. :-)

      Also, for the record: I don't hate File::Find, I just wish that the writers had made a different interface for it. One of the reasons why I was probably so negative about File::Find is that I'm still stuck in 5.005, and the File::Find that came with 5.005 was really hardly any better than rolling your own. It's gotten better since then, though (in terms of performance, features, and cross-platform portability... but not in terms of code sanity or interface design).

      ------------ :Wq Not an editor command: Wq

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2019-10-17 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?