One of the many things that I hate about Windows is how it automatically ("helpfully") closes DOS windows when they quit. This is most annoying when the program die()d, as you don't get a chance to read the error message. So here's a snippet that detects if the program was double-clicked, or run from a command line, and alters %SIG{__DIE__} appropriatly.
$SIG{__DIE__} = sub { # PROMPT is for NT/2000, CMDLINE is for 95/98 return unless !defined $ENV{PROMPT} or (defined $ENV{CMDLINE} and $E +NV{CMDLINE} eq 'WIN'); print shift,"This window will close in 10 seconds." and sleep 10 and + exit; } if $^O =~ /Win32/;
Update: Per tye's suggestion, I moved the return unless $^O =~ /Win32/ outside of the __DIE__ assignment, so the handler is not even changed if we're not under Windows.
Update the second: A fellow monk has made this code into a module and put it on CPAN as Win32::Die. Check it out!
|
---|
Replies are listed 'Best First'. | |
---|---|
(tye)Re: Watching Perl die() under Win32
by tye (Sage) on Feb 28, 2001 at 11:39 UTC | |
by Chmrr (Vicar) on Feb 28, 2001 at 11:42 UTC | |
MsgBox vs. print
by osfameron (Hermit) on Feb 28, 2001 at 14:19 UTC | |
by Anonymous Monk on Aug 25, 2003 at 01:21 UTC | |
Re: Watching Perl die() under Win32
by Ido (Hermit) on Mar 02, 2001 at 18:03 UTC | |
by Chmrr (Vicar) on Mar 03, 2001 at 03:53 UTC | |
Re: Watching Perl die() under Win32
by draconis22 (Initiate) on Mar 09, 2001 at 10:26 UTC | |
by Anonymous Monk on Apr 02, 2001 at 05:31 UTC |
Back to
Cool Uses for Perl