Re: difference between "@_" and "shift"

From: Chris Devers (cdevers_at_pobox.com)
Date: 09/30/04


Date: Thu, 30 Sep 2004 15:49:18 -0400 (EDT)
To: Graeme McLaren <iamnotregistered@hotmail.com>

On Thu, 30 Sep 2004, Graeme McLaren wrote:

> Can anyone tell me what the difference between @_ and shift is?

The former is a variable; the latter is a function.

@_ is one of Perl's "pronoun" variables: just as $_ refers to "that"
which you were most recently working with, @_ refers to "those" which
you were most recently working with. $_ is a scalar -- "it" or "that"; a
single thing. @_ is a list -- "those" or "these"; a plural thing.

shift is a function for pulling off the first element from a list. If
you've ever had to take a data structures class, you may remember that
two of the main ways to deal with collections of things as as stacks
(where you only deal with the top, and you can push onto it or pop off
of it) and queues (where everything moves in a line, and you enqueue
onto the end and dequeue from the front). Perl has functions that let
you work with arrays in these ways. You can push to and pop from the end
of lists to treat them as stacks; you can shift from and unshift to the
front of lists to, well, also treat them as stacks; and you can push &
shift or pop & unshift to treat it as forward or reverse queues.

So. In the context of subroutine arguments, you're generally passing in
one or more arguments. If you're only passing one, then you're right --

   my $arg = @_;
   my $arg = shift;
   my $arg = $_;

-- are all equivalent. If, on the other hand, you have multiple args,
then these will all do different things.

   my @args = @_;

   my $arg[0] = shift;
   my $arg[0] = $_;

If you pass the wrong thing and aren't ready for it, you can throw away
incoming data and possibly break things.

> As far as I know there is no difference except "shift" removes the
> parameter from the @_ array so if you were to "shift" all parameters
> passed to a function nothing would be containted in @_ is this
> correct?

Well, yes, in that this will deplete @_, but that doesn't mean that
there is no difference between shift and @_ -- they're different things.
 

-- 
Chris Devers


Relevant Pages

  • RE: Joining/Merging AoA
    ... push my @c, [@{shift @a}]; ... Subject: Joining/Merging AoA ... so you can just loop though both lists. ...
    (perl.beginners)
  • Re: (trivial) unshift and shift vs push and pop
    ... Which would make unshift and shift ... considerably less efficient, time-wise, than push and pop. ... probably use them when modelling stacks. ...
    (comp.lang.perl.misc)
  • Re: $# array creation and data rotation
    ... ,, now, as data comes in via the serial port ... ,, first want to "shift" the data: ... ,, and then push the new data: ... an array, or adding an element to an array. ...
    (comp.lang.perl.misc)
  • Re: Joining/Merging AoA
    ... push my @c, [@{shift @a}]; ... perldoc -f shift ... better off starting a new thread on understanding references. ...
    (perl.beginners)
  • Re: Return subset to drop down box
    ... I have a workbook to track production. ... input which shift they are inputting data for via drop ... employees who are on second shift to another drop down box on the ...
    (microsoft.public.excel)