tags:

views:

563

answers:

4

I was glancing through some code I had written in my Perl class and I noticed this.

my ($string) = @_;
my @stringarray = split(//, $string);

I am wondering two things: The first line where the variable is in parenthesis, this is something you do when declaring more than one variable and if I removed them it would still work right?

The second question would be what does the @_ do?

+5  A: 

The variable @_ is an array (hence the @ prefix) that holds all of the parameters to the current function.

1800 INFORMATION
+19  A: 

The @_ variable is an array that contains all the parameters passed into a subroutine.

The parentheses around the $string variable are absolutely necessary. They designate that you are assigning variables from an array. Without them, the @_ array is assigned to $string in a scalar context, which means that $string would be equal to the number of parameters passed into the subroutine. For example:

sub foo {
  my $bar = @_;
  print $bar;
}

foo('bar');

The output here is 1--definitely not what you are expecting in this case.

Alternatively, you could assign the $string variable without using the @_ array and using the shift function instead:

sub foo {
  my $bar = shift;
  print $bar;
}

Using one method over the other is quite a matter of taste. I asked this very question which you can check out if you are interested.

cowgod
+6  A: 

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.

Brad Gilbert
Good response. I don't think %_ is ever used in Perl, however. (I mean, it's never assumed and it's not a special variable of any other kind. The user can use it, of course, but they wouldn't even save any keystrokes by typing, e.g., "keys" instead of "keys %_".)
A. Rex
I don't think I have ever come across any built in Perl feature that will use %_, by default, but it is still considered as one of the default variables. Or at least I think I remember reading that somewhere.
Brad Gilbert
There isn't any word about %_ in perlvar. I haven't seen it at any place during my many years professional practice in perl.
Hynek -Pichi- Vychodil
+6  A: 

When you encounter a special (or punctuation) variable in Perl, check out the perlvar documentation. It lists them all, gives you an English equivalent, and tells you what it does.

brian d foy