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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>