【发布时间】:2012-03-07 14:33:27
【问题描述】:
标量上下文中的列表赋值返回右侧元素的数量:
scalar(my ($hello, $there, $world) = (7,8)); #evaluates to 2
为什么它评估右侧并产生 2,而不是评估新定义的列表并返回 3?
在我看来,$hello 得到 7,$there 得到 8,$world 得到 undef,然后在标量上下文中评估该列表,结果为 3,因为那是数字列表中的元素 ($hello $there $world)。对我来说,上下文影响返回的评估表达式的哪一部分似乎很奇怪:
my $greeting = (($hello, $there, $world) = (7,8)); #2
my @greeting = (($hello, $there, $world) = (7,8));
my $greeting_length = @greeting; #3
【问题讨论】:
-
确实,不:如果左侧列表是在标量上下文中评估的事物,则值为
undef。列表不是数组。 -
@darch,这没有任何意义。
($hello, $there, $world)(和(7,8))不能在($hello, $there, $world) = (7,8)的标量上下文中执行。如果需要,列表分配可以返回 3,但没有理由这样做。 -
我准确地回应了“然后 [列表 ($hello, $there, $world)] 在标量上下文中求值”这句话。评估
my ($h, $t, $w) = (7, 8, undef); scalar ($h, $t, $w)并查看列表不是数组。 -
@darcy
scalar($h, $t, $w)与scalar($w)相同......这是一个序列运算符,而不是一个列表。实时调频:perldoc.perl.org/functions/scalar.html
标签: perl list variable-assignment scalar