develooper Front page | perl.beginners | Postings from February 2012

Re: how to understand shift?

Thread Previous | Thread Next
From:
Jeff Peng
Date:
February 7, 2012 00:01
Subject:
Re: how to understand shift?
Message ID:
4F30DA71.8040804@staff.dnsbed.com
于 2012-2-7 15:45, lina 写道:
> I am sorry, still don't get.
>
> $year = shift
>
> part


Try with this code:

use strict;
my $month = "December";
my $year = "2007" ;

header($month,$year);

sub header {
         print '@_ is: ' . "@_\n";
         my $month = shift ;
         print '@_ is: ' . "@_\n";
         my $year = shift ;
         print '@_ is: ' . "@_\n";
}


The output:

@_ is: December 2007
@_ is: 2007
@_ is:


"shift" means "shift @_" in Perl.
So the first shift, $month get the value shifted from @_, and the first 
element in @_ get removed.
the second shift, $year get the value shifted from @_, the second 
element in @_ get removed.
Since @_ has only two elements, so after two shift, it becomes null.
That's all.

HTH.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About