Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Catching INT in Term::ReadLine::Gnu->readline()

by mbrouillet (Initiate)
on Jul 03, 2019 at 10:37 UTC ( [id://11102343]=perlquestion: print w/replies, xml ) Need Help??

mbrouillet has asked for the wisdom of the Perl Monks concerning the following question:

Hello, When I run this script,
#!/usr/bin/perl use Term::ReadLine; use sigtrap 'handler' => \&myhand, 'INT'; sub myhand { print "\n caught $SIG{INT}",@_,"\n"; } my $term = Term::ReadLine->new('test'); print $term->ReadLine()."\n"; $term->readline("try CTRL-C in some text here :");
I get a «^C» character displayed when I hit CTRL-C, but the signal is only processed once I type the Enter key. Here is the output :
marcel@machine:/tmp$ perl test.pl Term::ReadLine::Gnu try CTRL-C in some text here :auie ^C caught CODE(0x558c9e99acb0)INT marcel@machine:/tmp$
How can I catch it sooner ? Thanks.

Note : I use $term->AddDefun() and $term->bind_keyseq() to catch CTRL-UP/DOWN successfully, but this appears to be limited to some keys. I can't bind to CTRL-Enter for instance as I don't get a key-sequence as I do for CTRL-UP. Is this all related ?

Replies are listed 'Best First'.
Re: Catching INT in Term::ReadLine::Gnu->readline()
by hippo (Bishop) on Jul 03, 2019 at 11:00 UTC

    Your code as supplied works for me in that it produces the output which I believe you were expecting:

    $ perl cctrap.pl Term::ReadLine::Stub try CTRL-C in some text here :abcd^C caught CODE(0x194e9a0)INT xyz $

    The input sequence is: abcd^Cxyz<RETURN>

    This runs with perl v5.20.3 and Term::ReadLine 1.14 from bash on Linux. Maybe you just need to avoid Term::ReadLine::Gnu?

Re: Catching INT in Term::ReadLine::Gnu->readline()
by gnosti (Chaplain) on Jul 03, 2019 at 18:45 UTC
    Hi,

    I use Terminal::ReadLine::Gnu with the AnyEvent asynchronous framework in an application and manage to immediately catch Ctrl-C.

    Edit

    I am replacing previous code with a working example

    use Term::ReadLine; use AnyEvent; use Event; use strict; use warnings; my $events = {}; my $text = {}; my $term = Term::ReadLine->new("MyApp"); ($text->{screen_lines}, $text->{screen_columns}) = $term->get_screen_s +ize(); # create a STDIN watcher $events->{stdin} = AE::io(*STDIN, 0, sub { &{$term->Attribs->{'callback_read_char'}}(); }); $term->callback_handler_install(prompt(), \&process_line); # handle Control-C from terminal $events->{sigint} = AE::signal('INT', \&cleanup_exit); $term->stuff_char(10); # necessary to respond to Ctrl-C at first promp +t &{$term->Attribs->{'callback_read_char'}}(); print prompt(); $Event::DIED = sub { my ($event, $errmsg) = @_; print($errmsg); $term->Attribs->{line_buffer} = q(); if($term){ $term->clear_message(); $term->rl_reset_line_state(); } }; Event::loop(); sub cleanup_exit { print "\n caught $SIG{INT}",@_,"\n"; $term->rl_deprep_terminal(); exit; } sub prompt {"Enter command : "} sub process_line { my ($input) = @_; print "got some text: $input\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11102343]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found