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.


In reply to Error wrapping in an OO module by Fang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.