Question

I was always sure that if I pass a Perl subroutine a simple scalar, it can never change its value outside the subroutine. That is:

my $x = 100;
foo($x);
# without knowing anything about foo(), I'm sure $x still == 100

So if I want foo() to change x, I must pass it a reference to x.

Then I found out this is not the case:

sub foo {
 $_[0] = 'CHANGED!';
}
my $x = 100;
foo($x);
print $x, "\n"; # prints 'CHANGED!'

And the same goes for array elements:

my @arr = (1,2,3);
print $arr[0], "\n"; # prints '1'
foo($arr[0]);
print $arr[0], "\n"; # prints 'CHANGED!'

That kinda surprised me. How does this work? Isn't the subroutine only gets the value of the argument? How does it know its address?

Was it helpful?

Solution

In Perl, the subroutine arguments stored in @_ are always aliases to the values at the call site. This aliasing only persists in @_, if you copy values out, that's what you get, values.

so in this sub:

sub example {
   # @_ is an alias to the arguments
   my ($x, $y, @rest) = @_;  # $x $y and @rest contain copies of the values
   my $args = \@_;  # $args contains a reference to @_ which maintains aliases
}

Note that this aliasing happens after list expansion, so if you passed an array to example, the array expands in list context, and @_ is set to aliases of each element of the array (but the array itself is not available to example). If you wanted the latter, you would pass a reference to the array.

Aliasing of subroutine arguments is a very useful feature, but must be used with care. To prevent unintended modification of external variables, in Perl 6 you must specify that you want writable aliased arguments with is rw.

One of the lesser known but useful tricks is to use this aliasing feature to create array refs of aliases

my ($x, $y) = (1, 2);

my $alias = sub {\@_}->($x, $y);

$$alias[1]++;  # $y is now 3

or aliased slices:

my $slice = sub {\@_}->(@somearray[3 .. 10]);  

it also turns out that using sub {\@_}->(LIST) to create an array from a list is actually faster than
[ LIST ] since Perl does not need to copy every value. Of course the downside (or upside depending on your perspective) is that the values remain aliased, so you can't change them without changing the originals.

As tchrist mentions in a comment to another answer, when you use any of Perl's aliasing constructs on @_, the $_ that they provide you is also an alias to the original subroutine arguments. Such as:

sub trim {s!^\s+!!, s!\s+$!! for @_}  # in place trimming of white space

Lastly all of this behavior is nestable, so when using @_ (or a slice of it) in the argument list of another subroutine, it also gets aliases to the first subroutine's arguments:

sub add_1 {$_[0] += 1}

sub add_2 {
    add_1(@_) for 1 .. 2;
}

OTHER TIPS

This is all documented in detail in perldoc perlsub. For example:

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.

Perl passes arguments by reference, not by value. See http://www.troubleshooters.com/codecorn/littperl/perlsub.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top