In Perl, all function arguments are passed through the @_ array. When calling a subroutine, all passed parameters end up inside this array, and initially, it is a list, not a copy, meaning that if you modify $_[0], you modify the original.
sub foo { my ($arg1, $arg2) = @_; $arg1 = 10; # Only the local variable changes }
sub bar { my ($array_ref) = @_; push @$array_ref, 42; } my @data = (1,2,3); bar(\@data); # @data is now (1,2,3,42)
my ($a, $b) = @_, copies are created.If you pass an array to a Perl function like this:
myfunc(@arr), and inside the function refer to$_[0], what will be there?
Correct answer: There will be the first element value of the array, not a reference to the entire array! To pass an array as a whole object, use a reference: myfunc(\@arr).
sub print_first { print $_[0], " "; } my @a = qw/foo bar baz/; print_first(@a); # Will print 'foo', not a reference to the array print_first(\@a); # Will print ARRAY(0x...) — a reference to the entire array
History
update_hash(%global). Inside, $_[0] was modified. As a result, only the local slice of the argument array was changed, while the global hash remained unmodified. The correct solution was to pass a reference: update_hash(\%global).History
myfunc(@arr, %opts). It turned out that some hash keys were replaced by the values of the array, and it was not easy to detect the error.History
When implementing recursive tree traversal, there was a need to modify internal elements. Arrays were passed "as is" to the function, not by reference. They created copies, and changes did not affect the external context. The problem was resolved by switching to reference-style passing.