Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Error wrapping in an OO module

by Fang (Pilgrim)
on Jun 28, 2005 at 08:54 UTC ( [id://470531]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I am writing an OO module for my own needs (and as a learning experience as well), and I am trying to gather all the error messages possibly emitted by other modules (DBI, mainly). So far, I have laid down the following course of action:

  1. any potentially error-emitting method (eg. DBI->connect()) should be called with or die and enclosed in an eval block
  2. if an error ends up in $@, set a private variable to a meaningful string and exit returning a value representing failure, most probably undef

The private variable is simply called _error, accessed by an error() method that simply returns the string and resets _error to the empty string.

So far, this works fine under the environment I am using (mod_perl), with one little issue: if the constructor method encounters an error and returns undef, I can't use it like this for obvious reason:

my $obj = MyModule->new() or die $obj->error();

DBI uses a global variable for a similar purpose I believe, any caveat I should be aware of before trying that myself?

To sum it up, I am looking for advices concerning any better method I couldn't think of to achieve the same thing, whether it is a commonly used and proven solution, or an existing module (I have searched for "error", "wrapper", and many variations thereof but didn't quite find a fitting tool).

Thanks in advance for any advice you will provide.

Replies are listed 'Best First'.
Re: Error wrapping in an OO module
by tlm (Prior) on Jun 28, 2005 at 09:59 UTC

      Thanks for the links. I feel slightly ashamed not to have thought about searching for "handling" and its variations... I'll go read those nodes for sure.

Re: Error wrapping in an OO module
by mattk (Pilgrim) on Jun 28, 2005 at 23:37 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://470531]
Approved by marto
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found