in reply to Trying to learn about References
Subroutine foo() tries to pass two arrays, and as a result they are 'squashed' into one array. Subroutine bar() passes the two arrays by reference, and because it 'asks' for references in this line:use strict; my @a = (0..4); my @b = ('a'..'e'); &foo(@a,@b); &bar(\@a,\@b); sub foo { my (@a,@b) = @_; print "a: ", join(',',@a), "\n"; print "b: ", join(',',@b), "\n"; } sub bar { my ($a,$b) = @_; print "a: ", join(',',@$a), "\n"; print "b: ", join(',',@$b), "\n"; }
the arrays are not squashed. Maybe it would be clearer if you understand that @_ is an array itself, from perldata (with a paraphrase by me):my ($a,$b) = @_;
Hence, in subroutine foo(), @a soaks up all of the elements that were meant for @b.You can actually put an array or hash anywhere in the list [subroutine arguments which are passed to @_], but the first one in the list will soak up all the values, and anything after it will become undefined.
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR F--F--F--F--F--F--F--F-- (the triplet paradiddle)
|
|---|