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":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2021-10-27 10:37 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (91 votes). Check out past polls.