This will attempt to close (and therefore flush) all open files (with filenos 0 .. 255) when ^C is received:
require POSIX;
$SIG{INT} = sub{
warn 'Interupted';
POSIX::close( $_ ) for 0 .. 255;
exit;
};
You might also want to install the same signal handler for ^Break also:
require POSIX;
$SIG{BREAK} = $SIG{INT} = sub{
warn 'Interrupted';
POSIX::close( $_ ) for 0 .. 255;
exit;
};
If you are opening and closing many files then you might need to increase the range. The limit will depend upon who/how your copy of perl was built. With the version of AS perl on my system the limit appears to be 2047 concurrent handles. I'm not sure if that information is available via Config somewhere?
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|