Signal Handling/Term::ReadLine problem in Perl

J

John C. Worsley

I've got an extremely inscrutable problem here using Perl's
Term::ReadLine::Gnu module. I'm using Perl 5.8.0, readline 4.3 and
Term::ReadLine::Gnu 1.14.

The problem is specific to catching INT signals while in the
readline() function. In C, when I've set a SIGINT handler using
signal(), it is immediately called (even in readline()) when I hit
CTRL-C. Likewise in Perl, if I am reading in a while() loop from
STDIN, and I have set the $SIG{INT} handler, it is immediately called
on CTRL-C being struck.

However, the maddening thing here is that when calling readline() from
within Perl, when I hit CTRL-C, it doesn't call the signal handler
until AFTER I hit ENTER on the keyboard. I can even use
tab-completion, reverse-i-search and other readline features after I
have hit CTRL-C, but only after I hit enter, the CTRL-C is processed.

How on earth do I solve this? :) I've googled for over an hour trying
to find a solution and haven't come up with anything. I tried messing
around with some of the Term Attribs to make sure it wasn't using its
own signal handlers but never got different results. Simple script to
duplicate the problem follows.

------------------------------------------------------------------------------
#!/usr/bin/perl
use IO;
use Term::ReadLine;

STDOUT->autoflush();
$SIG{INT} = sub { die("CTRL-C"); };

$term = new Term::ReadLine 'ProgramName';
while (1) {
$input = $term->readline('prompt> ');
print "Got: $input\n";
}

------------------------------------------------------------------------------

Any help would be appreciated!

Best wishes,
John C. Worsley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top