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

Hello Monks,

One of my classes has a sub DESTROY. I found (by putting a print into the function's body), that this function is not called when I terminate a program with control-C (more precisely, when I have a batch file calling my command line Perl program, and the batch file is terminated by first pressing control-C and then answer Y to the question "terminate batch file (Y/N)?").

It looks as if Windows doesn't give the program the chance to gracefully shut down. Could someone confirm this observation?

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Windows: Destructor not executed on Control-C
by moritz (Cardinal) on Aug 20, 2008 at 14:45 UTC
    This is not a windows things:
    cat foo.pl #!/usr/bin/perl use strict; use warnings; sub DESTROY { print "Called main::DESTROY()\n"; } END { print "END called\n" } # $SIG{INT} = sub { print "SIGINT received\n"; exit }; my $x = bless []; 1 while 1; $ perl foo.pl ^C $ perl -i.orig -pe 's/^#// if $. > 2' foo.pl perl foo.pl ^CSIGINT received Called main::DESTROY() END called

    You can see that on linux neither END block nor DESTROY sub are called, unless SIGINT is caught and a regular exit is done instead.