develooper Front page | perl.perl5.porters | Postings from January 2012

Re: [perl #108470] Term::ReadLine should use AE instead of Tk for event looping

Matt S Trout
January 30, 2012 12:45
Re: [perl #108470] Term::ReadLine should use AE instead of Tk for event looping
Message ID:
On Mon, Jan 30, 2012 at 03:23:43PM -0500, Rocco Caputo wrote:
> On Jan 30, 2012, at 13:27, Matt S Trout wrote:
> >> Tk is not a hard dependency at the moment, unless you want an event loop.  AE 
> >> will not be a hard dependency (with my proposed patch), either.
> > 
> > So far as I can tell, ReadLine simply wants to know when a filehandle is
> > readable.
> > 
> > So rather than adding yet another hard-coded piece of loop support (and one
> > that as noted in my other message is unusable in some environments), perhaps
> > we could simply add a callback interface 
> That's a very sane idea.  It nicely sidesteps some maintenance pitfalls, like having to explicitly list supported libraries and optional dependencies.
> >  my $ae_cb = sub {
> >    my $cv = AE::cv;
> >    AE::io($_[0]->in, 0, sub { $cv->send });
> >    $cv->recv;
> >  };
> >  Term::ReadLine->new(..., read_callback => $ae_cb);
> That's kind of a weird and AE-specific way to do that callback.  A more portable API would have two methods: One to access the library's file handle, and another to be called when that handle is ready to be read.

Bah, $_[0]->IN is the file handle, which is held by Term::ReadLine

The existing code uses that approach for Tk - so it's not so much AE

Given IO::Async I'd write something like

  my $ioasync_cb = sub {
    my $done;
    my $loop = IO::Async::Loop->new;
    $loop->watch_io($_[0]->IN, on_read_ready => sub { $done = 1 });
    $loop->loop_once until $done;
    $loop->unwatch_io($_[0]->IN, on_read_ready => 1);

Matt S Trout - Shadowcat Systems - Perl consulting with a commit bit and a clue

Email me now on mst (at) and let's chat about how our Catalyst
commercial support, training and consultancy packages could help your team. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About