12

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.

1

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.