Think about it as most perl defaults do it, as well as the various common modules.

open FILE, ">file.txt" or die $!;
Here, the OS error is shuffled into the $! global, which is specifically there to hold perl-level errors.

my $sth = $dbi->prepare("SELECT * FROM TABLE") or die DBi->errstr;
Again, while not storing the error in a global, it does store it in a package-level variable, and that value is only changed when there is an error. You still get back useful returns from the call (an sth object or undef).

So I'd say the best way to do it is to make sure you use packages and for each one, create a package-level variable that is to store any error messages. Then your functions should return any defined value if no error occured, or undef if one did, then you can use the typcal  $var=function() or die $msg pattern above. The only gotcha to all this is if 0 or '' is a valid return from a function in addition to undef, in which case you might have to handle the checking via  defined($var=function()) or die $msg.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Re: style for returning errors from subroutines by Masem
in thread style for returning errors from subroutines by Boldra

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.