in reply to Using 'Shift' in subroutine argument

perldoc -f shift tells us:
shift ARRAY shift Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If t +here are no elements in the array, returns the undefined value. + If ARRAY is omitted, shifts the @_ array within the lexical s +cope of subroutines and formats, and the @ARGV array at file sc +opes or within the lexical scopes established by the "eval ''", "BEGIN {}", "INIT {}", "CHECK {}", and "END {}" constructs +.
In this case, it's likely that it's being used within another subroutine, so it's shifting the @_ array.

Replies are listed 'Best First'.
Re^2: Using 'Shift' in subroutine argument
by narainhere (Monk) on Nov 02, 2007 at 11:31 UTC
    If you execute this
    use strict; use warnings; one(1,2,3,4); sub one{ &two (shift, shift); } sub two { print "Elements : \n @_ \n"; }
    you would get
    Elements : 1 2
    Which means that it's same as passing the first two elements of the list the sub-routine one has received.Though I was not able to compile the code
    sub one{ &two shift, shift; }
    which is your case.Provide the some context of your snippet that would help everyone to learn.

    The world is so big for any individual to conquer

      Though I was not able to compile the code
      sub one{ &two shift, shift; }

      That's a syntax error. Subroutine invocations with & require parens.

      Drop the ampersand and move the sub two definition up:

      use strict; use warnings; one(1,2,3,4); sub two { print "Elements : \n @_ \n"; } sub one { two shift, shift; }

      To resolve the bareword two as a subroutine call (in two shift, shift), the compiler needs to have seen the sub declaration already. Alternative:

      use strict; use warnings; sub two; # forward declaration one(1,2,3,4); sub one { two shift, shift; } sub two { print "Elements : \n @_ \n"; }

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}