in reply to Death can be such a Tragedy

I also like to use warn or die with calls that might cause odd behaviour if they fail. For example, its easy to figure out why your code failed when an open doesn't work, but you can get some weird behaviour if you can't close a file, and you might not be able to trace it to the close. (And yes, its a good idea to close file handles explicitly, rather then banking on Perl to do it for you, which it will most of the time.) In addition to the Carp.pm module, you can also capture the __DIE__ and __WARN__ signals yourself and print extra useful debugging information. I'm going to guess that PerlMonks has sections on %SIG and caller, and I recommend you read them for more info.

Update: A quick check reveals that %SIG doesn't lead you anywhere useful, so I will give you a quick overview here: the keys to the SIG hash are the signals, like __DIE__ and __WARN__, and the values are references to handlers. So, for example, if you wanted to print the time that something died you would say:

%SIG{__DIE__}=sub {print STDERR localtime, "\n", @_ };
I still recommend reading the docs on this useful tool, but that should get you started.

Update (2): Thanks chromatic for finding the docs for %SIG. They are part of perlipc.

Replies are listed 'Best First'.
Re: RE: Death can be such a Tragedy
by yodabjorn (Monk) on Apr 03, 2003 at 01:29 UTC
    late in the thread i know but usefull anyhow. I was reading the 7 sins of perl again recently and saw that the latter versions of perl support passing more than just a string to die() and warn(). (sin #7) This is also verry usefull, and altho not capturing the signal it still allows for cleanup and verbosity
    #!/usr/bin/perl use strict; use warnings; open my $fh, "/etc/shadow" or warn my_warn() ; open $fh, "/etc/shadow" or die die_hard() ; sub my_warn { print "SHE can't take it much longer captian!\n"; } sub die_hard { print "OH NO CAPTAIN SHES BREAKING UP\n"; }


    An intellectual is someone whose mind watches itself.
    - Albert Camus