Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Binary Search Tree Debug Question

by Perlbotics (Archbishop)
on Aug 12, 2011 at 18:19 UTC ( [id://920061]=note: print w/replies, xml ) Need Help??


in reply to Binary Search Tree Debug Question

When called for the first time, $root is undef. insert() detects that and creates a brand new initial node. By using $_[0], insert() accesses $root as an alias - and thus assignes the inital node to $root. So, when insert() returns, $root is no longer undef but a reference to the newly created hash.
This is (one) perlish way to realise the concept of call by reference.

Update: Quote from perlsub:

Any arguments passed in show up in the array @_. Therefore, if you called a function with two arguments, those would be stored in $_[0] and $_[1]. The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). If an argument is an array or hash element which did not exist when the function was called, that element is created only when (and if) it is modified or a reference to it is taken. (Some earlier versions of Perl created the element whether or not the element was assigned to.) Assigning to the whole array @_ removes that aliasing, and does not update any arguments.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://920061]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found