a while ago i had asked the same question (or one on similar lines). my specific need 
was
error handling for modules to be used by somebody else. in such cases die/eval can 
become
very cumbersome.
error handling can be designed after two streams of logic:

1) error to be handled where the error occured.
2) error to be handled by the caller (which in turn might return an error indication 
to the
        caller without handling the error itself.

the first option usually occurs only in small scripts. code which needs to recover 
from errors
at the same sub-routine call level and at the caller level would look horrible with 
die/eval
blocks IMO. 

so with that in mind i have designed a small OO error handling module which i use for 
the
general purpose module that i am building. a user of this module could do something 
like this

use Nix::Err;

sub foo 
{
        sysopen (FH, 'boo', O_RDONLY) || return (Nix::Err->new ('sysopen'));
}       

as the object is overloaded to return a false in boolean context it can be used by the 
caller
as below

if the caller is not interested in debugging and just needs to die

foo () or die ('foo: failed');
or
$retval = foo () or $retval->die ('foo: failed');
or 
$retval = foo () or $retval->warn ('foo: failed');

raising the debuglevel one could even get a Carp/Croak like stack backtrace at the 
point of
error.
Nix::Err->debuglevel (2);
# now call die/warn methods


i would be extremely glad to get criticisms, comments, suggestions, improvements, on 
it.
here is the url for the code

www.extremix.net/Err.pm

karthik


On Tue, Aug 14, 2001 at 08:53:34AM -0700, Peter Scott wrote:
> At 09:04 AM 8/14/01 -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?
> 
> try/catch blocks will be in Perl 6.
> 
> If you want them in Perl 5, use the module Error.pm from CPAN.  I do, and I 
> like it.
> 
> Some people think this is a needless embellishment on using die/eval, which 
> is what Error.pm turns try/catch into anyway.  To each his/her own.
> 
> --
> Peter Scott
> Pacific Systems Design Technologies
> http://www.perldebugged.com
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to