Skip Menu |

This queue is for tickets about the Term-ReadLine-Event CPAN distribution.

Report information
The Basics
Id: 80057
Status: stalled
Priority: 0/
Queue: Term-ReadLine-Event

People
Owner: Nobody in particular
Requestors: james2vegas [...] aim.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.03
Fixed in: (no value)



Subject: How to integrate this with an app already handling its own event loop?
I already have an app using AnyEvent, but it does its own ->recv, I'd like to kick off a Term::ReadLine loop in a callback, for example:

use AnyEvent;
use Term::ReadLine::Event;
my $cv = AE::cv;

my $term = Term::ReadLine::Event->with_AnyEvent('test');
my $input;
my $tm = AE::timer 1, 0, sub {
    $input = $term->readline('Prompt >');                                      
};
AE::cv->recv;
print "$input \n";

After 1 second I get 'Prompt >' and then
recursive blocking wait attempted at /home/jamesw/perl5/lib/perl5/Term/ReadLine/Event.pm line 34.  Reading AnyEvent POD we see:
           Note that doing a blocking wait in a callback is not supported by
           any event loop, that is, recursive invocation of a blocking
           "->recv" is not allowed, and the "recv" call will "croak" if such a
           condition is detected. This condition can be slightly loosened by
           using Coro::AnyEvent, which allows you to do a blocking "->recv"
           from any thread that doesn't run the event loop itself.

Looking at the code you seem to be using $cv->recv as a way around not having a one_event mechanism, but it prevents the use
of $rl->readline in any callbacks (reducing its usefulness greatly)

Not sure how your original AnyEvent-only code worked, but right now AnyEvent and this module do not work together.  A non-blocking,
event driven Term-ReadLine shouldn't be blocking and returning values, it should be accepting callbacks and returning a guard so it
can be cancelled, something like this perhaps:

my $rl_guard = $rl->readline_cb( sub { $input = $_[1] } );

On Mon Oct 08 07:16:45 2012, JWRIGHT wrote: Show quoted text
> I already have an app using AnyEvent, but it does its own ->recv, I'd > like to > kick off a Term::ReadLine loop in a callback, for example:
Unfortunately, due to the way that Term::ReadLine is implemented, the limitation here is that this must become your loop. The idea here is that you can still handle events while using readline, not that you could use readline while using events. Maybe that wasn't clear in the documentation. There are ways around this, but mostly only with Term::ReadLine::Gnu (this module isn't specific to a type of T::RL, so doesn't use them). For example, you can see that with AnyEvent::ReadLine::Gnu. Also, this module was largely intended as a set of examples of how you could do this (making T::RL your main loop). The grand purpose was to have the examples directory that you could use as a starting point. However, if you're using T::RL::Perl, I don't think you're going to be able to do what you want. If you're using T::RL::Gnu, we may be able to tweak the callbacks somehow to do what you want, though it would be a drastic change in interface. Show quoted text
> Looking at the code you seem to be using $cv->recv as a way around
not Show quoted text
> having a > one_event mechanism, but it prevents the use > of $rl->readline in any callbacks (reducing its usefulness greatly)
Yes, unfortunately it does. Again, the T::RL interface itself doesn't provide a way to hook into these things, so this is just providing what we can. Show quoted text
> Not sure how your original AnyEvent-only code worked, but right now > AnyEvent > and this module do not work together. A non-blocking,
The original AE-only code worked just as well as this does, which is to say, within these limitations they worked fine. Outside of these limitations, it wouldn't have worked any better. Show quoted text
> event driven Term-ReadLine shouldn't be blocking and returning
values, Show quoted text
> it > should be accepting callbacks and returning a guard so it > can be cancelled, something like this perhaps: > > my $rl_guard = $rl->readline_cb( sub { $input = $_[1] } );
Yes, I agree. But that would require significant changes to T::RL. I've thought about creating an event-based readline interface to do exactly what you are saying, but then I'd have to re-write T::RL::Perl and understand T::RL::Gnu better, and that seemed like a huge headache. And, for my purposes, this was close enough. It got T::RL to be integratable to event-based programs, just not quite as integratable as it should be, but better than it was (which was Tk-only).
On Mon Oct 08 09:11:40 2012, DMCBRIDE wrote:

Show quoted text
> And, for my purposes, this was close enough. It got T::RL to be
> integratable to event-based programs, just not quite as integratable
> as it
> should be, but better than it was (which was Tk-only).

Unfortunately AnyEvent::ReadLine::Gnu seems to be a bit more broken than I
expected, so this is it for now.

How about adding an option to pass an already existing condvar to with_AnyEvent, similar
to the IO::Async option.


On Sat Nov 03 04:59:48 2012, JWRIGHT wrote: Show quoted text
> How about adding an option to pass an already existing condvar to > with_AnyEvent, similar > to the IO::Async option.
That's easy enough (patches accepted!), but since this module was intended as a *teaching* tool rather than necessarily something to be used directly, what I'm not sure about is how that improves things - how does this help the caller with using T::RL with AE events? If I understood that, I could put it into the perldoc for the module to help more people than just you :-) Thanks!
On Sat Nov 03 04:59:48 2012, JWRIGHT wrote: Show quoted text
> How about adding an option to pass an already existing condvar to > with_AnyEvent, similar to the IO::Async option.
James, If you could elaborate on how this helps integrate T::RL with existing AE- based apps, I would be happy to add that to the docs. Thus far, it hasn't been obvious to me, but that doesn't mean it isn't helpful, but does indicate to me that it's even more important to have such documentation. Any insight would be appreciated. Thanks,