学习perl点滴(二)

shift用法:

#!/usr/bin/perl
sub check_required_items {
    my $who = shift;
    print "$who\n";
    my %who_items = map {$_, 1} @_;
    my @required = qw(preserver sunserver water_bottle jacket);
    for my $item (@required) {
        unless ($who_items{$item}) {
                 print "$who is missing $item.\n";
        }
    }
}
my @gilligan = qw(red_shirt hat luc water_bottle);
check_required_items('gilligan',@gilligan);


输入结果:

gilligan

gilligan is missing preserver.

gilligan is missing sunserver.

gilligan is missing jacket.

shift 取数组中第一个变量,并在列表中剔除变量,有点像pop;


你可能感兴趣的:(perl,shift)