On Tue, Aug 14, 2001 at 09:04:06AM -0500, David Simcik wrote:
> I've been perusing the Camel book, the Cookbook, CGI Programming w/Perl, and
> Effective Perl for answers to this question, but have yet to find one or two
> definitive solutions. I've seen the standard die/eval() statements and the
> use of the various incarnations of Carp, but I have yet to see anyone say
> something along the lines of "this is the most common approach". I find
> myself longing for the consistency of try/catch blocks. Can anyone shed some
> light on the situation?
Use 'foo() or die' when a failure is truly unrecoverable in your
program. For example, if you can't write to your output files,
make a directory, locate a required program, etc.
eval {} is *really* try {} with a different spelling. (Not to be confused
with eval ""; which is different.) The problem is that the subsequent catch
statement is a little more unwieldy (if ($!) ... ), and there is no
finally clause.
die is pretty darn final. It's not a good idea to use die within
a module (there are exceptions, and this rule is not written in
stone). Use some variant of carp to throw an exception within a
module, but return an error to the caller, and let the caller
determine whether or not the error is truly unrecoverable.
That's a reasonable snapshot of the basic language features. You can
use Error.pm or other modules if you want something more structured.
I believe there is an exceptions module on CPAN that uses try {} catch {}
syntax (like C++/Perl6).
HTH,
Z.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]