I sometimes see Perl code like this:
我有时会看到像这样的Perl代码:
my ( $variable ) = blah....
What is the point of putting parentheses around a single variable? I thought parentheses were only used when declaring multiple variables, like:
将括号括在单个变量周围有什么意义?我认为括号仅在声明多个变量时使用,例如:
my ( $var1, $var2, $var3 ) = blah...
5 个解决方案
#1
36
There are several scenarios when there is a difference:
有几种情况存在差异:
-
When array is on right side
当数组在右侧时
my @array = ('a', 'b', 'c'); my $variable = @array; # 3 size of @array my ($variable) = @array; # 'a' $array[0]
-
When list is on right side
当列表在右侧时
my $variable = qw/ a b c d /; # 'd' last item of the list my ($variable) = qw/ a b c d /; # 'a' first item of the list
-
Subroutine with variable (array/scalar) return value
具有变量(数组/标量)返回值的子例程
sub myFunction { ... return (wantarray() ? @array : $scalar); } my $variable = myFunction(...); # $scalar from the subroutine my ($variable) = myFunction(...); # $array[0] from the subroutine
#2
19
The parentheses create a list context which affects how the right hand side of the assignment is evaluated.
括号创建一个列表上下文,该上下文会影响如何评估赋值的右侧。
Compare
比较
my $x = grep { /s/ } qw(apples bananas cherries);
print $x;
with
同
my ($x) = grep { /s/ } qw(apples bananas cherries);
print $x;
You will often use this construction when you just want to grab the first element of a list and discard the rest.
当你只想抓住列表的第一个元素并丢弃其余元素时,你经常会使用这种结构。
#3
7
I'm not a Perl pro (by all means, I'm not), but AFAIK it has to do with lists. Perl has different contexts (scalar, list). Using ($var)
switches to list context, $var
is scalar context.
我不是Perl pro(无论如何,我不是),但AFAIK与列表有关。 Perl有不同的上下文(标量,列表)。使用($ var)切换到列表上下文,$ var是标量上下文。
my $var = (1, 2, 4); # $var = 4 (last element)
my ($var) = (1, 2, 4); # $var = 1
Please downvote this answer, if it is totally wrong :)
如果它是完全错误的话,请低估这个答案:)
#4
7
You are confusing two different things. First off, when using my
to declare several variables, you need to use parentheses:
你混淆了两件不同的事情。首先,当使用my声明几个变量时,需要使用括号:
my $foo, $bar;
Does not work, as it is considered to be two different statements:
不起作用,因为它被认为是两种不同的陈述:
my $foo;
$bar;
So you need parentheses to group together the argument into an argument list to the function my
:
所以你需要使用括号将参数组合在一个参数列表中,并将其放入函数my:
my($foo, $bar);
Secondly, you have explicit grouping in order to invoke list context:
其次,为了调用列表上下文,您有明确的分组:
$foo, $bar = "a", "b"; # wrong!
Will be considered three separate statements:
将被视为三个单独的陈述:
$foo;
$bar = "a";
"b";
But if you use parentheses to group $foo
and $bar
into a list, the assignment operator will use a list context:
但是如果使用括号将$ foo和$ bar分组到列表中,赋值运算符将使用列表上下文:
($foo, $bar) = ("a", "b");
Curiously, if you remove the RHS parentheses, you will also experience a hickup:
奇怪的是,如果你删除RHS括号,你也会遇到一个hickup:
($foo, $bar) = "a", "b"; # Useless use of a constant (b) in void context
But that is because the =
operator has higher precedence than comma ,
, which you can see in perlop. If you try:
但那是因为=运算符的优先级高于逗号,你可以在perlop中看到它。如果你试试:
my @array = ("a", "b");
($foo, $bar) = @array;
You will get the desired behaviour without parentheses.
您将获得没有括号的所需行为。
Now to complete the circle, lets remove the list context in the above and see what happens:
现在要完成圆圈,让我们删除上面的列表上下文,看看会发生什么:
my @array = ("a", "b");
$foo = @array;
print $foo;
This prints 2
, because the array is evaluated in scalar context, and arrays in scalar context return the number of elements they contain. In this case, it is 2
.
这打印2,因为数组在标量上下文中计算,标量上下文中的数组返回它们包含的元素数。在这种情况下,它是2。
Hence, statements such as these use list context:
因此,这些语句使用列表上下文:
my ($foo) = @array; # $foo is set to $array[0], first array element
my ($bar) = ("a", "b", "c"); # $bar is set to "a", first list element
It is a way of overriding the scalar context which is implied in scalar assignment. For comparison, these assignments are in scalar context:
它是一种覆盖标量赋值中隐含的标量上下文的方法。为了比较,这些分配在标量上下文中:
my $foo = @array; # $foo is set to the number of elements in the array
my $bar = ("a", "b", "c"); # $bar is set to "c", last list element
#5
2
I have posted an in-depth explanation on the subject on another site.
我已经在另一个网站上发布了关于这个主题的深入解释。
[I can't post it here. * doesn't support tables. Not that anyone usually has a problem with offsite linking to documentation.]
[我不能在这里张贴。 *不支持表。并非任何人通常都有异地链接到文档的问题。]
#1
36
There are several scenarios when there is a difference:
有几种情况存在差异:
-
When array is on right side
当数组在右侧时
my @array = ('a', 'b', 'c'); my $variable = @array; # 3 size of @array my ($variable) = @array; # 'a' $array[0]
-
When list is on right side
当列表在右侧时
my $variable = qw/ a b c d /; # 'd' last item of the list my ($variable) = qw/ a b c d /; # 'a' first item of the list
-
Subroutine with variable (array/scalar) return value
具有变量(数组/标量)返回值的子例程
sub myFunction { ... return (wantarray() ? @array : $scalar); } my $variable = myFunction(...); # $scalar from the subroutine my ($variable) = myFunction(...); # $array[0] from the subroutine
#2
19
The parentheses create a list context which affects how the right hand side of the assignment is evaluated.
括号创建一个列表上下文,该上下文会影响如何评估赋值的右侧。
Compare
比较
my $x = grep { /s/ } qw(apples bananas cherries);
print $x;
with
同
my ($x) = grep { /s/ } qw(apples bananas cherries);
print $x;
You will often use this construction when you just want to grab the first element of a list and discard the rest.
当你只想抓住列表的第一个元素并丢弃其余元素时,你经常会使用这种结构。
#3
7
I'm not a Perl pro (by all means, I'm not), but AFAIK it has to do with lists. Perl has different contexts (scalar, list). Using ($var)
switches to list context, $var
is scalar context.
我不是Perl pro(无论如何,我不是),但AFAIK与列表有关。 Perl有不同的上下文(标量,列表)。使用($ var)切换到列表上下文,$ var是标量上下文。
my $var = (1, 2, 4); # $var = 4 (last element)
my ($var) = (1, 2, 4); # $var = 1
Please downvote this answer, if it is totally wrong :)
如果它是完全错误的话,请低估这个答案:)
#4
7
You are confusing two different things. First off, when using my
to declare several variables, you need to use parentheses:
你混淆了两件不同的事情。首先,当使用my声明几个变量时,需要使用括号:
my $foo, $bar;
Does not work, as it is considered to be two different statements:
不起作用,因为它被认为是两种不同的陈述:
my $foo;
$bar;
So you need parentheses to group together the argument into an argument list to the function my
:
所以你需要使用括号将参数组合在一个参数列表中,并将其放入函数my:
my($foo, $bar);
Secondly, you have explicit grouping in order to invoke list context:
其次,为了调用列表上下文,您有明确的分组:
$foo, $bar = "a", "b"; # wrong!
Will be considered three separate statements:
将被视为三个单独的陈述:
$foo;
$bar = "a";
"b";
But if you use parentheses to group $foo
and $bar
into a list, the assignment operator will use a list context:
但是如果使用括号将$ foo和$ bar分组到列表中,赋值运算符将使用列表上下文:
($foo, $bar) = ("a", "b");
Curiously, if you remove the RHS parentheses, you will also experience a hickup:
奇怪的是,如果你删除RHS括号,你也会遇到一个hickup:
($foo, $bar) = "a", "b"; # Useless use of a constant (b) in void context
But that is because the =
operator has higher precedence than comma ,
, which you can see in perlop. If you try:
但那是因为=运算符的优先级高于逗号,你可以在perlop中看到它。如果你试试:
my @array = ("a", "b");
($foo, $bar) = @array;
You will get the desired behaviour without parentheses.
您将获得没有括号的所需行为。
Now to complete the circle, lets remove the list context in the above and see what happens:
现在要完成圆圈,让我们删除上面的列表上下文,看看会发生什么:
my @array = ("a", "b");
$foo = @array;
print $foo;
This prints 2
, because the array is evaluated in scalar context, and arrays in scalar context return the number of elements they contain. In this case, it is 2
.
这打印2,因为数组在标量上下文中计算,标量上下文中的数组返回它们包含的元素数。在这种情况下,它是2。
Hence, statements such as these use list context:
因此,这些语句使用列表上下文:
my ($foo) = @array; # $foo is set to $array[0], first array element
my ($bar) = ("a", "b", "c"); # $bar is set to "a", first list element
It is a way of overriding the scalar context which is implied in scalar assignment. For comparison, these assignments are in scalar context:
它是一种覆盖标量赋值中隐含的标量上下文的方法。为了比较,这些分配在标量上下文中:
my $foo = @array; # $foo is set to the number of elements in the array
my $bar = ("a", "b", "c"); # $bar is set to "c", last list element
#5
2
I have posted an in-depth explanation on the subject on another site.
我已经在另一个网站上发布了关于这个主题的深入解释。
[I can't post it here. * doesn't support tables. Not that anyone usually has a problem with offsite linking to documentation.]
[我不能在这里张贴。 *不支持表。并非任何人通常都有异地链接到文档的问题。]