Perl INT Signal Problem

I have the following perl code for windows activestate perl 5.8

$SIG{INT}=\&clean; ... sub clean { print 'cleaning...'; ... ... exit 0; } 

but when I try to close my program using Ctrl ^ c, it did not go into sub clean at all, can someone help, why did I miss something?

+4
source share
3 answers

Windows does not seem to provide signals like in Unix.

From man perlwin32 :

Signal processing may not behave the same as on Unix platforms (where it is not exactly "behave", either :). For example, calling die () or exit () will throw an exception from the signal handlers, since most Win32 signal () implementations are badly crippled. Thus, signals can only work for simple things, such as setting a flag variable in a handler. The use of signals in this port should currently be considered unsupported.

+3
source

I would say no. I see nothing wrong with what you do. I wrote a test program that actually runs:

 #!/usr/bin/perl use strict; use warnings; $SIG{INT}=\&clean; sub clean { print 'caught'; } sleep 10; 

Tested on Linux, it works as expected, but I don't have AS perl to try. Try it yourself on your car.

Also, type in STDERR to make sure that something very strange is not happening with print spooling.

+1
source

I found that the script given by @ijw (modified to be what it is below) does not work in Active State Perl version 5.10.1:

 This is perl, v5.10.1 built for MSWin32-x86-multi-thread (with 2 registered patches, see perl -V for more detail) 

My change below adds autoflush calls (otherwise sleep below will not display output of print output at all while sleeping):

 #!/usr/bin/perl use IO; use strict; use warnings; # Set autoflushing on to stdout and stderr. Otherwise, system() call and stdout output does not show up in proper sequence, # especially on Windows: STDOUT->autoflush(1); STDERR->autoflush(1); $SIG{INT}=\&clean; sub clean { print "caught\n"; exit (0); } print "before sleep\n"; sleep 100; print "after sleep and then exiting\n"; exit (0); 

When I commented on the following lines in the script above:

 $SIG{INT}=\&clean; sub clean { print "caught\n"; exit (0); } 

And then pressing CTRL-C during sleep, the script terminates and displays this message:

 Terminating on signal SIGINT(2) 

Therefore, it should still be true (well, for ActiveState Perl v5.10.1) that man perlwin32 indicates:

... most of the "signal ()" implementations on Win32 are badly crippled ....

For future reference:

0
source

Source: https://habr.com/ru/post/1336494/


All Articles