Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I'm not a big fan of "validating inheritance" (especially in Perl where you can make perfectly functional objects [that simply implement all of the needed methods] that fail the 'isa' test) so rather than using UNIVERSAL::isa(), I'd probably either do something like have all error objects define an isError() method and add

sub UNIVERSAL::isError { 0 }
or have all error objects define a getError() method and test with $data->can("getError").

Better still, have your error objects overload boolean context so that they return "false" and just do

if( $data ) {
which starts heading toward what I've been wanting to write for years now: Error objects that fake all possible method calls to return themselves when called in a "object/method" context. They overload boolean context such that they return "false" and note that they have been "tested". If they get used in any other context or if they get destroyed without having been "tested", then they die with a full error message and stack trace.

Then you don't have to worry about checking for errors. If you don't check for success, then your code automatically dies on failure. If you do check for success but get it wrong, your code again dies on failures. If you do check for success and get it right, nothing dies. And you don't have to check right away so you can write:

# Dies if any method fails: $value= $Registry->SetOption(Delimiter=>"/") ->OpenKey("This")->GetValue("That"); # Doesn't die, no matter which method fails: if( ! $Registry->OpenKey("Foo")->GetValue("Bar") ) { # Create missing bits here }
(: [updated]

                - tye

In reply to Re: Best Practices for Exception Handling (no isa) by tye
in thread Best Practices for Exception Handling by Ovid

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2018-04-19 18:08 GMT
    Find Nodes?
      Voting Booth?