nallasiv has asked for the wisdom of the Perl Monks concerning the following question:

I tried using Shift() in PERL and it is not working when I use it in a PERL root program. But when I declare a function in the program and use Shift() in that it is working. Can anyone say why is it so?

The following is the program.

#!/usr/bin/perl -w shift @ARGV; # here its is not working print ; sub func { my $a=shift; # here its is working my $b=shift; print $a,$b; return $a+$b; } print func(10,10);

20040503 Edit by Corion: added formatting

I have used like that because in the book "Perl Weekend Crash Course" by Joe Merlino the code was given in Session 11 - Basic Input and Output.

Replies are listed 'Best First'.
Re: Usage of Shift function in PERL
by bart (Canon) on May 03, 2004 at 10:35 UTC
    It's working alright, you're changing the value of @ARGV, but you're not doing anything with the value it returns.

    You seem to be expecting it'll put its return value into $_ when in void context, but it doesn't. I can't even think of any built-in function that does that, for the moment, though split clobbers up @_ in scalar and void context — and that usage is deprecated. Using $_ as a default argument: yes; as a return value: no.

    Try:

    $_ = shift @ARGV; print; # This does use $_ by default
      Well, there's while in combination with the diamond operator. Although that's technically boolean context - and it's special cased anyway.

      Abigail

Re: Usage of Shift function in PERL
by perlinux (Deacon) on May 03, 2004 at 10:40 UTC
    @ARGV is the list of your scripts arguments. Try:
    ./script.pl 15
    and change your code:
    print shift; #@ARGV is the default in the main program
    or:
    $_ = shift and print;
      Thanks for your clarification. Now its working fine.
Re: Usage of Shift function in PERL
by Corion (Patriarch) on May 03, 2004 at 10:48 UTC

    Your style of parameter checking is very cumbersome and will get unwieldly if you have more than one argument to parse. It's much more readable to write your parameter code like this:

    #!/usr/bin/perl -w use strict; # usage: first parameter is user name # second parameter is message my ($user, $message) = @ARGV; print "Hello $user. Your message is: $message\n"; sub func { # note: $a and $b are bad variables to use, # as they are special variables used by sort() my ($c,$d) = @_; print $c,$d; return $c+$d; }; print func(10,10);

    Update: Changed $a and $b in func

Re: Usage of Shift function in PERL
by matija (Priest) on May 03, 2004 at 10:43 UTC
    Maybe it is "not working" because you throw the data away? shift does not automatically assign to $_.

    Try it with

    $_=shift @ARGV;
Re: Usage of Shift function in PERL
by virtualsue (Vicar) on May 03, 2004 at 13:03 UTC
    If that code is supposed to be a sample of actual working code, rather than a "spot the error" exercise, then you might want to think about submitting a bug report to Wiley. In addition to the main goof which has already been pointed out (the assumption that shift assigns a value to $_), I think it is poor practice to use $a and $b as run-of-the-mill temporary variables, since they have a special meaning to the sort function. I don't think Perl instruction books should get beginners in the habit of using those variable names, because that can cause very perplexing time-wasting errors later on in scripts which use sort.