in reply to Getting parameters in subs

Just to give you the basic stuff... shift removes the first element from an array and returns it. If no array is provided, it uses @_. So, yes, they are both kinda equivalent except that with my ($p1,$p2,$p3) = @_; you drop the remaining elements in the array and with shifting, you preserve the remaining elements. (my bad)</B<

Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.

Replies are listed 'Best First'.
Re: Re: Getting parameters in subs
by TheHobbit (Pilgrim) on Jun 16, 2002 at 17:33 UTC

    Hi,
    Sorry, but this is not exact. Doing

    my ($a,$b) = @_;
    you do not drop anything. Just try this:
    sub test1 { my ($a) = @_; print "\$a = $a\n"; print "\@_= (",join(",",@_),")\n"; } sub test2 { my $a = shift; print "\$a = $a\n"; print "\@_= (",join(",",@_),")\n"; } test1(1,2,3,4,5,6,7); test2(1,2,3,4,5,6,7);
    the call to <emph>test1</emph> prints
    $a = 1 @_= (1,2,3,4,5,6,7)
    while the one to <emph>test2</emph> prints:
    $a = 1 @_= (2,3,4,5,6,7)

    The basic difference should be obvious from the above examples: assigning @_ does not modify @_ (that is obviously consistent with standard assigement semantics), while using shift physically modify @_ </>

    Cheers


    Leo TheHobbit