Perl 子程序省略&号和非标量返回值

2021-07-04  本文已影响0人  PETJO

7. 省略&号

my @cards = shuffle(@deck_of_cards);    # &shuffle上的 &是多余的

sub division {
    $_[0]/$_[1];
}

my $quotient = division(355, 113);    # 用之前定义的 &division
print $quotient . "\n";    

sub chomp {
    print "Munch, munch!\n";
}

&chomp;    #必须使用 &,绝不能省略,内置函数歧义

8. 非标量返回值

my $fred = 11;
my $barney = 6;
my @array_rev = &list_from_fred_to_barney;
$" = ",";
print "@array_rev \n";

sub list_from_fred_to_barney {
    if ($fred < $barney) {
        $fred..$barney;
    } else {
        reverse $barney..$fred;
    }
}
上一篇下一篇

猜你喜欢

热点阅读