In the perlvar documentation there is a text about @_:
Within a subroutine the array @_ contains the parameters passed to that subroutine. Inside a subroutine, @_ is the default array for the array operators push, pop, shift, and unshift.
It is a common way to use shift without parameters to get first element from the array. It is very often used as:
sub some_method {
my $self = shift; # the same as `my $self = shift @_;`
...
}
But in the documentation there is written that it can be used with push, but I can't create working example without explicitly specifying @_ to the push. From reading this doc I'm expecting push 123; to push to @_, but it is not working.
push (@myNames, @moreNames); Note for beginning programmers: Perl arrays begin with an @ symbol. Each complete line of code must end with a semicolon. If it doesn't, it won't execute.
Perl Array Accessing To access a single element of a Perl array, use ($) sign before variable name. You can assume that $ sign represents singular value and @ sign represents plural values. Variable name will be followed by square brackets with index number inside it.
Perl provides a shorter syntax for accessing the last element of an array: negative indexing. Negative indices track the array from the end, so -1 refers to the last element, -2 the second to last element and so on.
Dereferencing an array It is done by placing the @ symbol (the sigil representing arrays) in-front of the reference. This can be written either wrapped in curly braces: @{$names_ref} or without the curly braces: @$names_ref.
I believe you've found an error in the documentation.
@_ is the default for pop and shift, but not for push and unshift. For both push and unshift, the array has to be specified explicitly.
perldoc -f push shows the syntax as:
push ARRAY,LIST
push EXPR,LIST
with no option to leave the array unspecified; likewise for perldoc -f unshift.
(The OP has submitted a Perl bug report; see https://gist.github.com/bessarabov/2e938f4bbb79e78d1941)
UPDATE:
This has been corrected. In the git repo for Perl, it was corrected in commit 256ca3d37ed232e10fbb2884be45691e745be7a9, 2015-06-21. The fix appears in releases 5.23.1 and 5.24.0. This doesn't seem to be mentioned in any of the perldelta*.pod files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With