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

I wrote a Windows keyboard handler by the aid of module Win32::Console. I capture $SIG{INT} because ctrl-C shall be received as a normal character, which works fine.

However, when I call my perl program from a Windows batch, the latter at termination of the perl subtask asks whether the batch shall be terminated. The interrupt is ignored by the perl program but it's still there.

How can I flush the interrupt, not just ignore it?

Sample code illustrating the problem:

use Win32::Console; $keyboard = Win32::Console->new(STD_INPUT_HANDLE); $SIG{'INT'} = sub { print "Gotcha!\n"; exit 0 }; $| = 1; # autoflush input print("Press ctrl-C to exit\n"); while (1) { @in = $keyboard->Input(); print(chr($in[5])) if $in[1]; }
Now call the perl program, e.g. named "sigint.pl", from a batch program "sigint.bat" set up as follows:
@echo off perl sigint.pl
Thanks for any hint and best regards
herqles

Replies are listed 'Best First'.
Re: How to Flush SIGINT on Win32?
by VinsWorldcom (Prior) on Jun 09, 2009 at 12:57 UTC
    Another "work around" to avoid using the .pl suffix is to add that suffix to the PATHEXT environment variable. This would allow you to call the Perl script directly from the command line as you would a batch script.

    In your case, you would just use:

    C:\> sigint
    Here's the Windows magic:

    1) Control Panel --> System --> "Advanced" tab --> "Environment Variables" button.

    2) In the "System variables" section, locate the "PATHEXT".

    3) Press "Edit" button.

    4) In the "Variable value:" text box that pops up, go to the end and add the text ";.PL" without the double quotes. (That's semicolon period capital P capital L.)

    5) Press the "OK" button.

    6) Press the "OK" button ("Environment Variables" window).

    7) Press the "OK" button ("System Properties" window).

    8) Close any open cmd.exe windows and open a new one.

    Before:

    {C} > set | find "PATHEXT" PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH; {C} > dir /b \usr\bin\crapps.pl crapps.pl {C} > crapps 'crapps' is not recognized as an internal or external command, operabl +e program or batch file. {C} >
    AFTER:
    {C} > set | find "PATHEXT" PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PL; {C} > dir /b \usr\bin\crapps.pl crapps.pl {C} > crapps C:\usr\bin\crapps.pl: host required Usage: crapps [options] [SNMP options] | [Telnet options] host ... crapps -P password [options] crapps -S svc [options] {C} >
Re: How to Flush SIGINT on Win32?
by VinsWorldcom (Prior) on Jun 09, 2009 at 12:03 UTC
    Is launching another CMD window to execute the Perl script an option?
    @echo off start perl sigint.pl
      No, not really. The keyboard handler is integrated into a command line tool that shall be simply called from the command line. For convenience, in order to avoid on Windoze the suffix ".pl", I would like to call the tool from a little batch similar to the example. The console handler even refuses to respond, when started in a new window. It appears that the keyboard is somehow still linked to the old window.
      Anyway, thanks a lot for the hint!
      herqles
Re: How to Flush SIGINT on Win32?
by Anonymous Monk on Jun 09, 2009 at 12:48 UTC
      I can see that pl2bat would make things "neater" (just one file rather than two). But as there's still a cmd interpreter running a BAT script which launches perl to run a Perl script, I don' think this would address the Ctrl-C issue at all.

      --
      use JAPH;
      print JAPH::asString();