bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:
This is from the Perl Cookbook Recipe 11.17 (Program: Binary Trees).
But I still don't get it. If I removed this line, I don't get any output.
I ran the debugger but to no avail, I still could not figure out what is the relevance of that line. Why did he write it? And is there another way to write it maybe a longer version?
Can someone help understand this? Thanks.
I am trying to understand the relevance of this line:#!/usr/bin/perl use strict; use Data::Dumper; my $root; insert( $root, 5 ); insert( $root, 3 ); print Dumper \$root; sub insert { my ($tree, $value) = @_; unless ($tree) { $tree = {}; $tree->{VALUE} = $value; $tree->{LEFT} = undef; $tree->{RIGHT} = undef; $_[0] = $tree; # $_[0] is reference param! return; } if ( $tree->{VALUE} > $value ) { insert( $tree->{LEFT}, $value ) } elsif ( $tree->{VALUE} < $value ) { insert( $tree->{RIGHT}, $value + ) }; }
He mentioned that this is a reference param. And also he mentioned that "The assignment of the new node back to $_[0] alters the value in its caller."$_[0] = $tree;
But I still don't get it. If I removed this line, I don't get any output.
I ran the debugger but to no avail, I still could not figure out what is the relevance of that line. Why did he write it? And is there another way to write it maybe a longer version?
Can someone help understand this? Thanks.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Binary Search Tree Debug Question
by jdporter (Chancellor) on Aug 12, 2011 at 18:29 UTC | |
Re: Binary Search Tree Debug Question
by Perlbotics (Bishop) on Aug 12, 2011 at 18:19 UTC | |
Re: Binary Search Tree Debug Question
by FunkyMonk (Chancellor) on Aug 13, 2011 at 13:56 UTC | |
Re: Binary Search Tree Debug Question
by Anonymous Monk on Aug 12, 2011 at 19:19 UTC | |
by Tanktalus (Canon) on Aug 12, 2011 at 19:36 UTC |
Back to
Seekers of Perl Wisdom