zdog has asked for the wisdom of the Perl Monks concerning the following question:

I just recently am getting used to using or die "..."; in my code. However, this is confusing me a little. Currently, I use or die with the functions open, opendir, and mkdir. However, are there any other functions where it is customary to use or die? Because in theory, you can use it with every function. Also, where would I use or warn or should I even use it?

I also just recently discovered use Carp; . However, another question popped into my head. Where do I use or confess and where do I use or croak ?

Thanx.

Zenon Zabinski | zdog | zdog7@hotmail.com

Replies are listed 'Best First'.
Re: Death can be such a Tragedy
by chromatic (Archbishop) on Aug 12, 2000 at 21:14 UTC
    Always check for success on system calls.

    Always check for success on system calls.

    Anything that can go wrong (that depends on Perl interacting with your system in some way, whether opening a file, making a database connection, executing a program via system or backticks, or the like) needs to be checked. If your program can't continue if it can't open its configuration file, die. If you have a reasonable set of defaults available, warn.

    You would use Carp in places where you need to get the context of the error. carp <=> warn and croak <=> die, but they report more details, making debugging easier.

    Update: confess gives you a full stack trace -- which functions were called, what arguments did they get. It's industrial strength.

Re: Death can be such a Tragedy
by ZZamboni (Curate) on Aug 12, 2000 at 17:37 UTC
    You should check for errors everywhere something could potentially go wrong. Most of us assume certain operations (e.g. print to STDOUT) will always succeed, which is a pretty good bet, so you don't check on every single print (but you should check on print's to files or pipes, where failure is more likely).

    If your program can recover and continue operations, use warn to let the user know that something didn't happen as expected. If there is no way the program can continue, use die.

    Just be careful to properly check for errors and not use or die/warn blindly. The example that comes to mind is system, which returns the status as returned by the command executed, so that zero means "no error" and non-zero values indicate error conditions (i.e., the opposite of the usual indication in Perl).

    --ZZamboni

RE: Death can be such a Tragedy
by Adam (Vicar) on Aug 12, 2000 at 17:07 UTC
    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.

      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
Re: Death can be such a Tragedy
by jeorgen (Pilgrim) on Aug 12, 2000 at 12:21 UTC
    I also would like some guidance in this area, so I was happy to see zdog's post. Does any one have any experience with try/catch and what advantages it would give in real situations?

    Thanx

    jeorgen

      Eval may provide you with the construct youre looking for

      eval { $sth->execute; }; if ($@) { die or log or whatever...... }