Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Watching Perl die() under Win32

by Chmrr (Vicar)
on Feb 28, 2001 at 11:09 UTC ( #61300=snippet: print w/replies, xml ) Need Help??

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.

Of course, Windows 9* does this differently than Windows NT/2000, so there are two different environment variables that get checked. Actually, I only had a Windows 98 and a Windows 2000 machine to test this on, so /msg me if this works / doesn't work on Windows 95 or NT.
$SIG{__DIE__} = sub {
  # PROMPT is for NT/2000, CMDLINE is for 95/98
  return unless !defined $ENV{PROMPT} or (defined $ENV{CMDLINE} and $E
  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

    I'd rather you check for Win32 first and just not set a __DIE__ handle if one isn't needed. Otherwise, quite handy.

            - tye (but my friends call me "Tye")
      True -- so you would move the return unless $^O =~ /Win32/ outside the __DIE__ handler? Makes sense. (There is the sound of an edit being made..)

      perl -e 'print "I love $^X$\"$]!$/"#$&V"+@( NO CARRIER'

MsgBox vs. print
by osfameron (Hermit) on Feb 28, 2001 at 14:19 UTC
    The PROMPT variable is also not set from the Start - Run command.

    Another usage for this would be to determine whether to print to a console, or to do a Win32::MsgBox instead. (I tried to figure out how the script could figure where it was called from some time ago and couldn't work it out...)


      As far Windows 98SE is concerned, the PROMPT is set $p$g whether a script is invoked from the command line, clicked, or from the Start-Run dialog box. The CMDLINE is set to whatever was typed on the command line and to WIN if invoked by clicking or from the Start-Run box. If you use the Start-Run box you can pass arguments just as you can from the command line.
Re: Watching Perl die() under Win32
by Ido (Hermit) on Mar 02, 2001 at 18:03 UTC
    I don't know about Win2k or NT. But on 95/98/ME the /k flag of might be useful. Changing the Open "Action" of .pl files (In folder options->file types) to " /k c:\perl\bin\perl.exe" should do.
      True. I would point out that the solution I provided above is also useful after programs have been run through perl2exe or any equivalent -- which was actually my original impetus.

      perl -e 'print "I love $^X$\"$]!$/"#$&V"+@( NO CARRIER'

Re: Watching Perl die() under Win32
by draconis22 (Initiate) on Mar 09, 2001 at 10:26 UTC
    I personally recommend creating a batch file that has something like

    perl.exe -w %1

    Whala.... as long as perl is set in your enviroment variables, all you have to do is run through the batch file instead of adding in code to each project. If you use Ultraedit, you can even plug this into a macro and it saves you lots of time-and headaches


      Doesn't DOS shells (I use that term loosely) offer an errorlevel check? So, you could pause only if the process returns a non-zero exist status: perl.exe -w %1 if errorlevel 1 pause
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://61300]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-06-20 06:27 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.