What is the meaning of @_ in Perl?

PerlVariables

Perl Problem Overview


What is the meaning of @_ in Perl?

Perl Solutions


Solution 1 - Perl

perldoc perlvar is the first place to check for any special-named Perl variable info.

Quoting:

> @_: Within a subroutine the array @_ contains the parameters passed to that subroutine.

More details can be found in perldoc perlsub (Perl subroutines) linked from the perlvar:

> 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.

Solution 2 - Perl

Usually, you expand the parameters passed to a sub using the @_ variable:

sub test{
  my ($a, $b, $c) = @_;
  ...
}

# call the test sub with the parameters
test('alice', 'bob', 'charlie');

That's the way claimed to be correct by perlcritic.

Solution 3 - Perl

First hit of a search for perl @_ says this:

> @_ is the list of incoming parameters to a sub.

It also has a longer and more detailed explanation of the same.

Solution 4 - Perl

The question was what @_ means in Perl. The answer to that question is that, insofar as $_ means it in Perl, @_ similarly means they.

No one seems to have mentioned this critical aspect of its meaning — as well as theirs.

They’re consequently both used as pronouns, or sometimes as topicalizers.

They typically have nominal antecedents, although not always.

Solution 5 - Perl

You can also use shift for individual variables in most cases:

$var1 = shift;

This is a topic in which you should research further as Perl has a number of interesting ways of accessing outside information inside your sub routine.

Solution 6 - Perl

All Perl's "special variables" are listed in the perlvar documentation page.

Solution 7 - Perl

Also if a function returns an array, but the function is called without assigning its returned data to any variable like below. Here split() is called, but it is not assigned to any variable. We can access its returned data later through @_:

$str = "Mr.Bond|Chewbaaka|Spider-Man";
split(/\|/, $str);

print @_[0]; # 'Mr.Bond'

This will split the string $str and set the array @_.

Solution 8 - Perl

@ is used for an array.

In a subroutine or when you call a function in Perl, you may pass the parameter list. In that case, @_ is can be used to pass the parameter list to the function:

sub Average{

    # Get total number of arguments passed.
    $n = scalar(@_);
    $sum = 0;

    foreach $item (@_){

        # foreach is like for loop... It will access every
        # array element by an iterator
        $sum += $item;
    }

    $average = $sum / $n;

    print "Average for the given numbers: $average\n";
}

Function call

Average(10, 20, 30);

If you observe the above code, see the foreach $item(@_) line... Here it passes the input parameter.

Solution 9 - Perl

Never try to edit to @_ variable!!!! They must be not touched.. Or you get some unsuspected effect. For example...

my $size=1234;
sub sub1{
  $_[0]=500;
}
sub1 $size;

Before call sub1 $size contain 1234. But after 500(!!) So you Don't edit this value!!! You may pass two or more values and change them in subroutine and all of them will be changed! I've never seen this effect described. Programs I've seen also leave @_ array readonly. And only that you may safely pass variable don't changed internal subroutine You must always do that:

sub sub2{
  my @m=@_;
  ....
}

assign @_ to local subroutine procedure variables and next worked with them. Also in some deep recursive algorithms that returun array you may use this approach to reduce memory used for local vars. Only if return @_ array the same.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreiView Question on Stackoverflow
Solution 1 - PerlDVKView Answer on Stackoverflow
Solution 2 - PerleckesView Answer on Stackoverflow
Solution 3 - PerlPiskvor left the buildingView Answer on Stackoverflow
Solution 4 - PerltchristView Answer on Stackoverflow
Solution 5 - Perlhockfan86View Answer on Stackoverflow
Solution 6 - PerlascheplerView Answer on Stackoverflow
Solution 7 - PerlAfser2000View Answer on Stackoverflow
Solution 8 - PerlOmar Faroque AnikView Answer on Stackoverflow
Solution 9 - PerlAnatolyView Answer on Stackoverflow