标量上下文中的列表分配 [英] List Assignment in Scalar Context
问题描述
标量上下文中的列表分配返回右侧的元素数:
A list assignment in scalar context returns the number of elements on the right hand side:
scalar(my ($hello, $there, $world) = (7,8)); #evaluates to 2
为什么它要评估右侧并生成2,而不是评估新定义的列表并返回3?
Why does it evaluate the right hand side and produce 2, instead of the newly defined list being evaluated and returning 3?
在我看来,$hello
得到7,$there
得到8,而$world
得到undef
,然后该列表在标量上下文中求值,结果为3,因为这是数字列表中的元素($hello $there $world
).对我来说,上下文影响返回所评估表达式的哪一部分似乎很奇怪:
To me, it seems like $hello
gets 7, $there
gets 8, and $world
gets undef
, then that list is evaluated in scalar context, which would result in 3, as that is the number of elements in the list ($hello $there $world
). It seems weird to me that context affects which part of the evaluated expression is returned:
my $greeting = (($hello, $there, $world) = (7,8)); #2
my @greeting = (($hello, $there, $world) = (7,8));
my $greeting_length = @greeting; #3
推荐答案
该文档记录了 perlop (赋值运算符"部分的最后一句话):
It's documented to count the elements on the right in perlop (the last sentence in the Assignment Operators section):
类似地,列表上下文中的列表赋值产生分配给它的左值的列表,而标量上下文中的列表赋值返回由赋值右侧的表达式产生的元素数.
Similarly, a list assignment in list context produces the list of lvalues assigned to, and a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment.
之所以这样工作,是因为您可以编写如下内容:
The reason it works like that is so that you can write things like this:
while (my ($key, $value) = each %hash) { ... }
如果它计算赋值左侧的元素数,那将是一个无限循环.
If it counted the number of elements on the left hand side of the assignment, that would be an infinite loop.
考虑一下,左侧的元素数量与右侧的元素数量相同或为常数(当您分配给标量列表时).在第一种情况下,计数哪一侧没有区别,在第二种情况下,计数右手侧更有用.
If you think about it, the number of elements on the left hand side is either the same as on the right hand side or it's a constant (when you're assigning to a list of scalars). In the first case, it makes no difference which side you count, and in the second case, counting the right hand side is more useful.
另一方面,在列表上下文中,赋值运算符返回左侧列表,因为这样做更有用.如果在修改列表元素的上下文中使用它,则要修改刚刚分配给的变量.
On the other hand, in list context the assignment operator returns the left hand list, because that's more useful. If you use it in a context that modifies the list elements, you want to modify the variables that were just assigned to.
回复:您的评论在您的示例中,(7,8)
是一个包含两个元素的列表,这就是赋值运算符返回2的原因.当您将一个较短的列表分配给一个较长的标量列表时,分配发生之前,右侧不会用undef
填充".相反,从右侧列表中没有与之关联的任何变量都将重置为其默认值.对于标量变量,为undef
.对于数组,这是一个空数组.对于哈希,这是一个空哈希.
Re: your comment In your example, (7,8)
is a two-element list, which is why the assignment operator returns 2. When you assign a shorter list to a longer list of scalars, the right hand side is not "padded out" with undef
before the assignment happens. Instead, any variables that did not have a value associated with them from the right hand list are reset to their default value. For a scalar variable, that's undef
. For arrays, that's an empty array. For hashes, that's an empty hash.
这篇关于标量上下文中的列表分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!