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

What is the difference between `my $foo` and `my ($foo)`?

有什么区别

my $foo

my ($foo)

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.