in reply to Re^2: Error handling in chained method calls
in thread Error handling in chained method calls

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

Replies are listed 'Best First'.
Re^4: Error handling in chained method calls
by choroba (Cardinal) on Oct 25, 2010 at 12:34 UTC
    ... there is no logical option but to split the statement into parts
    ...
    ... 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
    Very true. I did not see chainig mentioned in your post, but this reply makes your comment complete.