Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Using 'Shift' in subroutine argument

by Anonymous Monk
on Nov 02, 2007 at 10:29 UTC ( #648616=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,
I am going through a Perl script where I found
mysub shift, shift;
where mysub is subroutine name. Please explain what how arguments are being passed through shift itself. As far as I know shift is used to get the very first element of an array. But here I cant see any array. Are these 'shift's' taking value from command lines?? Pl help.

Comment on Using 'Shift' in subroutine argument
Download Code
Re: Using 'Shift' in subroutine argument
by merlyn (Sage) on Nov 02, 2007 at 10:49 UTC
    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.
      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}
Re: Using 'Shift' in subroutine argument
by sen (Hermit) on Nov 02, 2007 at 10:54 UTC

    Hi,

    A subroutine's arguments come in via the special @_ array. The shift without an argument defaults to @_.

    for example,

    sub emp { 
      $name = shift; 
      $id = shift; 
      print "name, $name, id $id"; 
    } 
    &emp('abc', 10); 
    
      The shift without an argument defaults to @_.

      except in main scope (outside a sub) where shift operates on @ARGV by default. Consider:

      perl -e "print shift" wibble

      Prints:

      wibble

      Perl is environmentally friendly - it saves trees
Re: Using 'Shift' in subroutine argument
by FunkyMonk (Canon) on Nov 03, 2007 at 01:38 UTC
    I've always believed it was a no-no to change a variable (@_ or @ARGV in this case) more than once in a single statement. As far as I know, Perl[1] doesn't define which shift will be done first.

    [1] perl, does of course, do one or the other first.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://648616]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-07-13 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls