In thinking about things to ship with AxKit2 I thought it would be cool to have a webmail that doesn't suck written in Perl. You know - drag and drop with AJAX, HTML editor, decent speed, etc.
Of course all the perl IMAP libraries are blocking - they don't drop in well to a non-blocking framework. So I set out to write a non-blocking library for IMAP.
Turns out it was easier than I hoped. Net::IMAP::Simple (which is probably "Simple" because it doesn't support much of IMAP, but I'll probably figure that out in a few weeks) is mostly really well written, and was fairly easy to extend. I had to re-write quite a few of the methods, though some of the changes can probably be incorporated into the base class as they make for a nice improvement to the design.
Result is I can now do this:
use Net::IMAP::Simple::NB;
Danga::Socket->AddTimer(0, sub {
# Create the object
my $imap = Net::IMAP::Simple::NB->new('felony:143') ||
die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
print "IMAP: $imap\n";
$imap->login('matt','xxxxxxx', sub {
my $login_ok = shift;
if ($login_ok) {
print "Login OK\n";
$imap->select("INBOX", sub {
my $nm = shift;
print "Got $nm Messages\n";
my $i = 1;
my $sub;
$sub = sub {
my $headers = shift;
print grep {/^Subject:/ } @$headers;
$i++;
$imap->top($i, $sub) unless $i == $nm;
};
$imap->top($i, $sub);
});
}
});
});
Danga::Socket->EventLoop;
Where the "sub" callbacks are called when the Net::IMAP::Simple methods return with their responses, and in the meantime the event loop is not blocked.
One of the problems with building IMAP based webmail is you don't want to be re-establishing connections to the IMAP server on every hit. AxKit2 can get around that problem by just keeping the connection open and hooking the request up to the right connection using a cookie/session-id.
Lots of work still to do, but now it's just a small matter of programming.
closure-callback model rocks (Score:1)
I'd hate to have to go back to the old model of event-driven programming, with objects to hold all state between events, etc.
Re: (Score:2)
What's really cool is that closures in perl just do what you expect them to do. The hard thing to do is explain closures and make them look like they might not be what's expected - because when you look at the syntax you just think "yep, that's normal".
Leaky? (Score:1)
Re: (Score:2)
This design is used a lot by IO::AIO (which is used in some people's production apps) and thus if it leaked we would find out about it darn fast.
Re: (Score:1)
my $sub; $sub = sub { ...$sub... };construct, and that whenever that code got called, the reference count for $sub would never go to zero. Is what you're doing any different than this (leaks in 5.8), or did this sort of thing recently get fixed?:Re: (Score:2)
Re: (Score:1)
Re: (Score:2)
Re: (Score:1)
I agree that the Y() is cryptic (and you can't blame perl too much), but the U() is not that hard to understand, and if they're tucked away in a library (as they are in Moose::Autobox albeit in an OO form), then you don't have to look at them, and they're pretty simple to use. In other words, "don't look behind the curtain, just use the Y-combinator" :-)
If you have freedom in what args your recursive sub accepts (which I assume you don't in the above instance), then you can let the first arg be the sub its
Y combinator (Score:1)
It’s the construct that’s cryptic, not the Perl code. It’s no easier to understand in Lisp, although the core concept is actually very simple. The basic idea is that you make an anonymous function recursive by making it take a reference to itself as a parameter, so that it can call that to recurse on itself.
The actual implementation requires doing a series of code sit-ups that makes the fully built combinator very hard to follow. I think I’ll have to rewrite it for myself in Pe
Re: (Score:2)
Re: (Score:1)
I would usually agree but in this case I don’t see how. Using $func or $closure in place of $f wouldn’t do anything to clarify any of what’s going on, and I can’t think of any good name of $h. The functions are no easier to name more sensibly. It would help the legibility of the Y combinator to break it down a bit and assign subexpressions to suitably named variables, but the difference wouldn’t be anything to write home about.
These are simply near-unreadable constructs.
Re: (Score:2)
Re: (Score:1)
That sums up my frustrations with higher order mathematics pretty effectively. I strongly suspect that the Haskell compiler rejects identifiers with more than one vowel or over eight characters, but I can't prove it, for example.
Re: (Score:1)
I suppose. I can’t read the Y combinator either, y’know. I can derive it when I start from scratch, though, and for as long as that model is in my head, I can reason about it.
I just can’t build it inductively from reading the source/expression. I have to deduce it every time from scratch.
It may be a matter of practice.
Re: (Score:2)
Re: (Score:1)
Well yes, you get away without the Y combinator here. wrap_func is a rather nondescript name, though, don’t you agree? A better one might be something like curry_self or apply_self_maker or some such, although it’s probably not obvious to someone without basic knowledge of functional programming why these names are in fact better. All of them sound similarly meaningless, though the better ones might even be a little scary. And to someone with functional programming knowledge, U would be just a
Re: (Score:2)
wrap_func() took a function, and returned a new function that took the given $func as the first argument. Therefore, in my callback $sub is not "wrapped", so needs to be re-wrapped.
Re: (Score:1)
Oh d’uh. I got confused between properties of the Y and U combinators.
Re: (Score:2)
Re: (Score:1)
Heh. I’d read about the Y combinator before but I only started studying it in anger yesterday because I realized I wouldn’t actually understand it on any deeper level if I didn’t sit down to derive it from first principles for myself.
My grasp is strengthening by the hour, but you can see I’m not quite there yet. :-)