Thus, $node->parent->parent can sometimes work and sometimes not.

Then the situation is flow control logic rather than error handling.

If the node doesn't have a parent, you can't get it's grandparent, and so there is no logical option but to split the statement into parts. That isn't really a case of the chaining being the problem so much as simply a bad algorithm.

Taking it back to the OPs example, if you're chaining your way back to the root, you have to check whether you've reached there or not at each iteration, but if you've successfully retrieved a defined something from if( defined obj->parent ), you don't need to then check that it is a reference, or whether that reference can( 'parent' ), because the parent method should not be able to return anything other than a valid node, or undef.

A simple:

sub root { my $self = shift; my $root = $self; $root = $_ while defined( $_ = $root->parent ); $root //= $self; return $root; }

Or, if you're dealing with an algorithm that requires access to grandparent nodes, you have to deal with nodes that don't have a parent never mind a grandparent:

sub grandparent { my $self = shift; my $parent = $self->parent // return; return $parent->parent; } ... if( my $gp = $obj->grandparent ) { ## use it; } else{ ## take a different path }

But you don't (shouldn't) have to deal with getting a defined something that isn't a node.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re^3: Error handling in chained method calls by BrowserUk
in thread Error handling in chained method calls by szabgab

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.