tags:

views:

6020

answers:

7

what could this line possibly mean?

my $x = shift ;
+2  A: 

This is usually an idiom for: $x is a local variable assigned to the first parameter passed to the subroutine, although.

my ($x) = @_;

is probably clearer (and it doesn't modify the argument list).

David Nehme
+4  A: 

The shift function removes the first element from an array, and returns it. The array is shortened by one element.

The default array (if one isn't given as a parameter) is @_ if you're in a function, or @ARGV if you're at file scope.

So in this case $x is either being set to the first function parameter, or to the first command line parameter.

Alnitak
It should be added that @_ is the array containing the parameters given to a function, and @ARGV is the array containing the parameters given to a program.
Svante
A: 

If you are in a subroutine this line will shift on @_ (the params that are passed in). So $x would be the first item popped from the @_ array.

So usually you would see $x = shift if @_;

No real need for the if. "my $x = shift;" and "my $x; $x = shift if @_;" are equivalent and will both result in $x being undef if @_ is empty, nor will either violate strict or emit a warning. The only difference is the amount of typing you have to do.
Dave Sherohman
+6  A: 

Perl has built-in docs for every standard function. They're also online. In this case, "perldoc -f shift."

Sherm Pendley
+17  A: 

shift() is a built in Perl subroutine that takes an array as an argument, then returns and deletes the first item in that array. It is common practice to obtain all parameters passed into a subroutine with shift calls. For example, say you have a subroutine foo that takes three arguments. One way to get these parameters assigned to local variables is with shift like so:

sub foo() {
  my $x = shift;
  my $y = shift;
  my $z = shift;
  # do something
}

The confusion here is that it appears shift is not being passed an array as an argument. In fact, it is being passed the "default" array implicitly, which is @_ inside a subroutine or @ARGV outside a subroutine.

cowgod
or @ARGV, if you're not in a function (see my answer)
Alnitak
you don't want the '()' after the name of the sub - it should be 'sub foo {...'. With the '()' you are saying that foo accepts no arguments, which is not what you want in this case
EvdB
A: 

In Perl, many methods use the default variables ($_ and @_) if you don't explicitly specify arguments. Your code is identical to:

my $x = shift @_;

As pointed out by PullMonkey earlier, within a subroutine, @_ contains the arguments passed to that subroutine (as described in perlsub). shift will remove the first argument value from @_ and store it in $x, so $_[0] will now give you the second argument passed to your subroutine.

Gaurav
A: 

in a layman's language,from a very highlevel view, shift is taking the first element of an array (the leftmost part), while the opposite is pop which is taking the last element of array (the rightmost part).

my @array1=(5,6,7,8,9);
my $x = shift @array1;
print "$x\n"; # 5
print "@array1\n"; # 6 7 8 9
mhd