You can see that I could use eval (commented out) but again, I am unsure of the best practice!?

That's how I'd do it: die (or croak) in your module if necessary, and have the caller wrap the call in question in an eval to catch the exception (since that's what it really is).

Of course this can get cumbersome, so if your subroutine can only return certain values (e.g. only values that are true), you could also return false or undef, and have the caller invoke it like this:

$splat->splat() or die "splat() returned false"; unless(defined $splat->splat()) { die "splat() returned undef"; }

(Hmm, it sure would be nice if perl had a defined-or version of the or operator.)

Whether this is feasible depends on what splat() is supposed to be return, of course. For numbers there's a trick: you can return a string along the lines of "0 but true"; this'll numify to zero, but evaluate to true in boolean contexts. For strings, I don't think there's any such trick for the empty string.

There is no single best solution; it's a matter of taste.


In reply to Re: Croak, return et al. by AppleFritter
in thread Croak, return et al. by packetstormer

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.