http://qs321.pair.com?node_id=920066


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.