It's a TIMTOWTDI and philosophical issue as to how your API returns errors - just make sure your API is consistent once you choose an approach! The first two of your code examples are pretty common ways of handling it. On the other hand, the "user has to check an additional variable after calling my method" design is not to my personal taste, even though it's a classic (e.g. errno).

If the error condition is something that happens relatively often, do you really want to force a user to eval (or equivalent) all of your methods (personally I wouldn't), or is it really a fatal error which interrupts the program flow? Is the error something internal to the method that really shouldn't have gone wrong (like assertions; I might die in that case), is it an error from an underlying layer such as a database that your code is simply passing along, or is it something the user of the method did wrong?

Remember that there are other ways to return things from methods, such as returning multiple values, pass-by-reference parameters, setting fields on the object, etc.

In a language like Java, where exception handling is built-in, using exceptions is a bit more common. I'd say that exception handling is not "truly built in" to Perl because for a long time eval didn't have a fully reliable $@ error message (see for example "Background" in Try::Tiny). Instead, many Perl APIs chose to return false values on failures.

Note you may want to look into Carp instead of die, especially if the errors are something the caller of the method did wrong the error messages are more helpful.

Regarding your third example, because of the aforementioned issues with $@, the more reliable way to use eval is: my $success = eval { $result = whatever(); 1 };

(By the way, in your code examples, do you really mean to interpolate $object into the string?)


In reply to Re: Using die() in methods by Anonymous Monk
in thread Using die() in methods by v_melnik

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.