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

Well, $node->parent can either return another node, or undef if the node has no parent (e.g. it is a root of a tree). Thus, $node->parent->parent can sometimes work and sometimes not. The problem is the chaining.

Replies are listed 'Best First'.
Re^3: Error handling in chained method calls
by LanX (Saint) on Oct 25, 2010 at 11:16 UTC
    Thanx for this clarification of a use case. :)

    IMHO again this could be solved with a coderef wrapper $c_parent which calls the real method only on definedness of the object.

     $node->$c_parent->$c_parent

    Another (classical) approach would be defining a NULL-node object which is returned instead of undef and wouldn't cause an error.

    But the latter would only work if one has access to the design of underlying library.

    Cheers Rolf

Re^3: Error handling in chained method calls
by BrowserUk (Patriarch) on Oct 25, 2010 at 12:11 UTC
    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.
      ... 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.