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

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

Thread Previous | Thread Next
From:
Darin McBride
Date:
January 18, 2012 11:17
Subject:
Re: [perl #108470] Term::ReadLine should use AE instead of Tk for event looping
Message ID:
2124425.RXyo5BHBmQ@naboo
On Wednesday January 18 2012 10:13:48 AM you wrote:
> > I'm curious as to how you would receive events registered wtih Event while
> > T::RL is waiting for text, since T::RL is currently going to be spinning
> > the Tk event loop, not the Event event loop, unless you have an idle
> > callback registered with Tk that simply spins the Event loop once.
> 
> I use Event in a situation that the app runs without loaded
> the Tk based GUI, to provide timers, etc. In that case,
> tkRunning is not set.

Yeah, I'm very curious.  Are you running multi-threaded?  That should do it, 
but otherwise, I can't figure it out.

> > In other words, as far as I can tell, today, T::RL will only spin a Tk
> > loop. If Tk isn't loaded, it won't spin any loop, and all events will be
> > on hold (with a possible exception of signal handlers) until the user
> > hits enter.
> I'm glad no one told *me* that! :-)
> 
> A T::RL based command prompt seems to run fine alongside
> an Event event loop. I may be able to dig up some
> test code to verify this FYI.

You've piqued my curiosity. :-)  Here's the test I've been running, it's as 
simple as I can make it.  If you uncomment out the two tk lines and comment 
out the Event line, it works.  But if you try to remove Tk, the timer doesn't 
print out the elapsed time (or something approximating such) at the top of the 
console, assuming ANSI escape sequences are being interpreted (I was too lazy 
to do any other method of controlling the cursor as this one dates back to my 
DOS 5.0 days).

I assume that if I moved the Event loop to one thread and the Tk loop (or 
basically, T::RL without tk) to a separate thread from the Event loop, it'll 
work.  But that now comes with all the joys and tribulations of 
multithreading.  As opposed to the joys and tribulations of event programming.

#!/usr/bin/perl

use strict;
use warnings;

use Event;
#use Tk;
use AnyEvent;
use Term::ReadLine;

my $esc;
BEGIN { $esc = "\x1b[";
    print "${esc}2J${esc}3H";
}

my $t    = 0;
my $w    = AE::timer (0,1,sub {print "${esc}s${esc}1H$t s   ${esc}u";++$t});
my $term = Term::ReadLine->new('...');
#$term->tkRunning(1);

my $x = $term->readline('> ');


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About