rvosa has asked for the wisdom of the Perl Monks concerning the following question:
That is to say, I make the $self->get_parent method call twice, once to check if there really is a parent, the second time to step to that parent during my traversal (here we're traversing a tree from initial node $self to the root, but this issue is more broadly applicable, obviously).sub path_to_root { my $self = shift; my $path = $self->get_branch_length; while ( $self ) { if ( $self->get_parent ) { $self = $self->get_parent; $path += $self->get_branch_length; } else { return $path; } } }
sub path_to_root { my $self = shift; my $path = $self->get_branch_length; while ( $self ) { if ( $self = $self->get_parent ) { $path += $self->get_branch_length; } else { return $path; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: assignment operator return values
by Aristotle (Chancellor) on Feb 22, 2006 at 01:30 UTC | |
|
Re: assignment operator return values
by GrandFather (Saint) on Feb 22, 2006 at 01:29 UTC | |
|
Re: assignment operator return values
by QM (Parson) on Feb 22, 2006 at 01:34 UTC |