Perl has two different contexts, scalar context, and list context. An array '@_
', if used in scalar context returns the size of the array.
So given these two examples, the first one gives you the size of the @_
array, and the other gives you the first element.
my $string = @_ ;
my ($string) = @_ ;
Perl has three 'Default' variables $_
, @_
, and depending on who you ask %_
. Many operations will use these variables, if you don't give them a variable to work on. The only exception is there is no operation that currently will by default use %_
.
For example we have push
, pop
, shift
, and unshift
, that all will accept an array as the first parameter.
If you don't give them a parameter, they will use the 'default' variable instead. So 'shift;
' is the same as 'shift @_;
'
The way that subroutines were designed, you couldn't formally tell the compiler which values you wanted in which variables. Well it made sense to just use the 'default' array variable '@_
' to hold the arguments.
So these three subroutines are (nearly) identical.
sub myjoin{
my ( $stringl, $stringr ) = @_;
return "$stringl$stringr";
}
sub myjoin{
my $stringl = shift;
my $stringr = shift;
return "$stringl$stringr";
}
sub myjoin{
my $stringl = shift @_;
my $stringr = shift @_;
return "$stringl$stringr";
}
I think the first one is slightly faster than the other two, because you aren't modifying the @_
variable.