Just to rephrase this a little. Scalar variables are containers that hold a value. When you assign to a scalar you throw away the value previously contained and overwrite it with a new value.
References are a scalar value that point at (or refer to) other containers. This should be distinguished from an alias which is just a different textual name for the same container. For instance if you modify the first sub:
sub foo { for my $ref ($_[0]) { $ref = { all => new }; } } my $hash={}; print $hash,"\n"; foo($hash); print $hash,"\n";
$ref is now an alias to $_[0] which is itself automagically an alias to whatever scalar was passed into the subroutine which in this case is the scalar $hash. In this case assigning to $ref inside of the sub is the same as assigning to $hash outside of the sub.
On rereading this it may just cause more confusion, if so I apologise.
In reply to Re: Modifying values by reference: what works and what doesn't
by demerphq
in thread Modifying values by reference: what works and what doesn't
by markjugg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |