Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Why callbacks?

by ysth (Canon)
on Mar 30, 2007 at 17:29 UTC ( [id://607537]=note: print w/replies, xml ) Need Help??


in reply to Why callbacks?

It's part of a different way of thinking. Consider reading Higher-Order Perl, and, as you go through it, think about how things that use callbacks might work differently or not work at all without callbacks.

Replies are listed 'Best First'.
Re^2: Why callbacks?
by Fletch (Bishop) on Mar 30, 2007 at 17:35 UTC

    Yup. Whereas the normal calling sequence for an API is "Don't call us, we'll call you", using callbacks is more "If something interesting happens, call me at this number (coderef)". One good way to get your head into the callback-driven mindset is to use one of the GUI toolkits which are pretty much all event driven. When something interesting happens (the user hits the mouse button; the environment needs part of a window redrawn) the framework needs a chunk of code (the callback) which it calls into in order to handle the event.

      ++!

      Okay, that makes sense! Events. Ah...

      And it suddenly reminds me of where I've seen callbacks that didn't annoy me: signal handlers.

      Would I be going out on a limb if I said the use of callbacks in Net::IMAP is a little .. er.. funny?

      For those of you who haven't had the (dis)pleasure of working with that particular module, if you want to know the size of an email message, you send a query to the IMAP server and get the results via callback.

        It does. Maybe I'm missing something but just now it seems that the author is using callbacks for no good reason.

        Whats even worse is that the module was released 03 Oct 1999, its tests (or instalation) fails in 8 of 10 CPAN Testers' attempts and the request tracker contains unapplied patches as old as Mar 12 2002. There are actually four bugs in RT, all in state "new". Looks like an abandoned module to me.

        Maybe you'd have more luck with Net::IMAP::Simple or IMAP::Client. I've never used either of those, but they seem to be in a better shape. They pass their tests, have less reported bugs and the bugs are not as old. (The Net::IMAP::Simple is NOT based on Net::IMAP!)

        I think you're right -- using callbacks for that sounds like a very strange API choice. I assume it does this because the set of data to be fetched could be very large and take a while. Most perl programmers are more accustomed to an OO iterator API in that situation, like what DBI provides.

        Since IMAP commands are asynchronous it would make sense for them to be handled with callbacks.

        I can request the headers for one message, and then request the body for the other, without getting the answer for the first one back, and it can give me the answer to any pending IMAP query in any order it likes.

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re^2: Why callbacks?
by pileofrogs (Priest) on Mar 30, 2007 at 17:36 UTC

    Can you elaborate a little? I'll try and check that book out. Can you give a tiny example or something?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-24 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found