Re: [rt.cpan.org #59322] SetConsoleCtrlHandler function not supported

[email protected] ("Bill Luebkert via RT")
Newsgroups perl.libwin32
Message-ID <[email protected]>
Thu Jul 15 05:30:45 2010: Request 59322 was acted upon.
Transaction: Correspondence added by [email protected]
       Queue: Win32-Console
     Subject: Re: [rt.cpan.org #59322] SetConsoleCtrlHandler function not supported
   Broken in: 0.09
    Severity: Important
       Owner: Nobody
  Requestors: [email protected]
      Status: open
 Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=59322 >


Chris Marshall via RT wrote:
> 
> So the problem appears that the 1st Ctrl-C is caught
> but something in the readline part prevents reloading
> of the signal handler so an immediate Ctrl-C will
> send another "SIGINT" which will not be caught and
> then the console control handler will be called which
> offers to exit the CMD console.  (Presumably, the
> default handler was also called from the 2nd Ctrl-C
> in perl which is why that application exited....
> 
> I think I've convinced myself it is tied in with the
> Term::ReadLine::Perl stuff but don't know how it
> could be fixed.  If that is the case, having the
> missing SetConsoleCtrlHandler() routine might not
> help either.

Looks like 1) you have to stop reading after you get an EOF
from readline; 2) readline seems to catch the first ^C and
quits working after that - then you can catch ^C OK; 3) you
can't create a new Readline obj after it fails.

New test case:

use strict;
use warnings;
use Term::ReadLine;

$| = 1;
my $cnt = 0;
sub handler {
	print "Caught a SIG '$_[0]' - continuing\n";
	die "Got three" if ++$cnt > 2;
}
$SIG{INT}  = \&handler;

my $term = new Term::ReadLine 'ProgramName';
while (1) {
	my $input = $term->readline('prompt> ');
	if (not defined $input) {
		print "EOF on input\n";
	} elsif ($input eq 'q') {
		print "quitting\n"; exit;
	} else {
		printf "Got: '%s'\n", $input;
	}
	Win32::Sleep(1000);	# stop runaway console
}

__END__

There's definitely some work that could be done in readline
to make this a little more friendly and some docs to go with.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.