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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.