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

Re: pmchat-2

by jakobi (Pilgrim)
on Sep 29, 2009 at 13:10 UTC ( #798116=note: print w/replies, xml ) Need Help??


in reply to pmchat-2

Here is the POSIX::sigaction variant of the short patch updating the script to work with UNIX again. It no longer requires the extra CR, instead using unsafe signals locally for interrupting readline.

Thanx to anonymous for the pointer.

Please use the earlier Patch for the time being.

(partial line lost, as noted below; so it's patch is merely a valid example for interrupting linked code with POSIX::sigaction, and annoying the user, but not for sane interrupted ReadLine use

@@ -408,15 +408,18 @@ ##################################################################### +# my $old; my $term = new Term::ReadLine 'pmchat'; +use POSIX qw/SIGALRM/; + +my $posixsigh=sub { + $old = $readline::line; + die +}; sub getlineUnix { my $message; eval { - local $SIG{ALRM}=sub { - $old = $readline::line; - die - }; + POSIX::sigaction(SIGALRM, POSIX::SigAction->new($posixsigh)); ## I don't use the version of readline from ReadKey (that include +s ## a timeout) because this version stores the interrupted (what @@ -427,8 +430,11 @@ $message = $term->readline("\rTalk: ", $old); $old = $readline::line = ''; alarm(0) unless $win32; + POSIX::sigaction(SIGALRM, POSIX::SigAction->new(undef)); }; + $message="" if not defined $message; + #warn "seen: $message\n"; $message; }

It might be another improvement to also comment-out the undef of the handler and move the setting of the handler to just below the use Term::Readline; as SIGALRM shouldn't happen without us using alarm().

Update 1: with the change of the Alarm, I cannot obtain the partial input line (noticed when reducing the timeout). While there's a tantalizing rl_set_keyboard_input_timeout(int usec) for Gnu ReadLine, it's probably not worth the effort, as the version of the patch in the first post works well enough. KISS wins, I guess.

Please drop me a line if you've a trick to try and get the partial line buffer. Thanx.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://798116]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2020-04-02 01:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (19 votes). Check out past polls.

    Notices?