Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Perl Term::ReadLine::Gnu Signal Handling Difficulties

by Mr. Muskrat (Canon)
on Nov 13, 2012 at 06:00 UTC ( #1003557=note: print w/replies, xml ) Need Help??

in reply to Perl Term::ReadLine::Gnu Signal Handling Difficulties

You access GNU Readline variables through the Attribs method (with the rl_ stripped). Term::ReadLine::Gnu_Variables

#!/usr/bin/perl use strict; use warnings; use Term::ReadLine; $SIG{TERM} = sub { print "I got a TERM\n"; exit; }; $SIG{INT} = sub { print "I got a INT\n"; exit; }; # add any additional signal handlers you want my $term = Term::ReadLine->new('Term1'); $term->ornaments(0); my $attribs = $term->Attribs; $attribs->{catch_signals} = 0; # the default is 1 my $prompt = 'cmd> '; while ( defined (my $cmd = $term->readline($prompt)) ) { chomp($cmd); if ($cmd =~ /^help$/) { print "Help Menu\n"; } else { print "Nothing\n"; } }

Update: Added $SIG{INT}

Replies are listed 'Best First'.
Re^2: Perl Term::ReadLine::Gnu Signal Handling Difficulties
by sgt_b2002 (Initiate) on Nov 13, 2012 at 13:50 UTC

    Hi Mr. Muskrat

    Thanks for the help. I tried the script you provided and can see how to set the Gnu variables now, but my script still doesn't act how I think it should.

    Given the script you provided, I'll hit control-c to send an INT to the script while at the prompt. I expect to see "I got a INT" right after I hit control-c. Instead, the script stays at the prompt until I hit enter. After hitting enter I see "I got a INT".

    Is this the designed behavior? Ideally, I'd like to have the signal handlers trigger as soon as the signal is received.

    Thanks again for helping me out. :)

      It works for me; that is to say that when I run it and press ctrl-c, it exits with "I got a INT".

      We are going to need more information about the environment where you are testing this. Let's start with operating system, version of Perl, version of Term::ReadLine::Gnu and version of GNU Readline library version should be sufficient for now.

      By the way, it's impolite to cross-post without saying you've cross-posted.

        Thanks again for the reply. I'll be sure to mention cross positing if it happens again as well.

        So I've tested this in two environments and in each, the ctrl-c isn't handled until I press enter. Here are the two environments.

        OS: Gentoo Linux 2.6.35 x86_64 Perl: 5.12.4 Term::ReadLine::Gnu: 1.20 readline version: 6.2_p1 OS: Gentoo Linux 3.3.8 x86_64 Perl: 5.16.1 Term::ReadLine::Gnu: 1.20 readline version: 6.2_p1

        As an aside, I added the following line as a quick check to make sure I'm using Term::ReadLine::Gnu when testing this on different machines.

        print $term->ReadLine."\n";

        Again, thanks for your help with this. :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003557]
[choroba]: Good morning!
[Corion]: I hope you had a good weekend!
[marto]: jetlag has really done a number on the kids, it's been a tough week
choroba played with the band on Saturday, so Sunday was very sleepy
[choroba]: managed to release Syntax::Construct with 5.28 support in the night, though
[Corion]: choroba: Whee ;)
[Corion]: marto: Ouch - I would've thought that kids adapt much better, but that's obviously not the case...
[marto]: well, their mother let them sleep till 15:00 & 12:00 last week, which didn't help them adjust :P
[Corion]: I was "productive" over the weekend in the sense that I revived my old "Perlmonks on SQLite" code, which likely means I can get a test instance back up running on my webhost. Small steps :)
[Corion]: marto: Ow, no, that doesn't help at all :)

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2018-06-25 08:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.