Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl Monk, Perl Meditation.
 
PerlMonks

Watching Perl die() under Win32

by Chmrr (Vicar)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

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

Description:

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
+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!

Comment on Watching Perl die() under Win32
Select or Download Code
(tye)Re: Watching Perl die() under Win32
by tye (Archbishop) 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")
[reply]
      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'

[reply]
[d/l]
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...)
    Thanks!

    Cheerio!
    Osfameron

[reply]
      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.
[reply]
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 command.com might be useful. Changing the Open "Action" of .pl files (In folder options->file types) to "command.com /k c:\perl\bin\perl.exe" should do.
[reply]
      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'

[reply]
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
    pause

    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

    Derek

[reply]
      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
[reply]

Back to Snippets Section

Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: snippet [id://61300]
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others exploiting the Monastery: (10)
GrandFather
atcroft
herveus
Eyck
biohisham
Haarg
matze77
lamprecht
gnosti
im2
As of 2009-11-21 09:02 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls