`my $ foo`和`my($ foo)`有什么区别?

时间:2015-02-04 17:43:40

标签: perl

之间有什么区别
my $foo

my ($foo)

1 个答案:

答案 0 :(得分:7)

my $amy ($a)之间没有区别。

然而,

之间存在着天壤之别
my $a = some_function();

my ($a) = some_function();

第一个示例在标量上下文中调用some_function。第二个在列表上下文中调用它。

如果你的功能如下:

sub some_function {
    return ( 'Larry', 'Moe', 'Curly' );
}

然后你的结果将是:

my $a = some_function();  # $a gets "Curly", the last element in the list.
my ($a) = some_function();  # $a gets "Larry" and the other two values are discarded.
相关问题