Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Binary Search Tree Debug Question

by jdporter (Chancellor)
on Aug 12, 2011 at 18:29 UTC ( #920066=note: print w/replies, xml ) Need Help??


in reply to Binary Search Tree Debug Question

Ok, you know that arguments are passed to subs in the special array @_. It just so happens that the actual elements of @_ are not simply copies of the actual argument values, but are aliases to them. If you were to do this:

$x = 5; @a = ( $x ); $a[0] = 7; print $x; # prints 5
$x remains unchanged, because $a[0] is completely separate from $x. The assignment of ( $x ) to @a copies the value (5).

It's different with @_ when it's carrying the arguments of a sub call.

$x = 5; foo( $x ); print $x; # prints 7 sub foo { $_[0] = 7; }
That's because $_[0] is not separate from $x; in fact, it is an alias to it. The "assignment" of ( $x ) to @_ does not copy values; it sets up aliases.

This behavior can be used to create "OUT" parameters; that is, parameters which can alter their arguments.

(Note that the actual argument would have to be writable for this to work. If we called foo( 5 ); with the above code, we'd get a "Modification of a read-only value attempted" error.)

I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2022-12-01 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?