相关疑难解决方法(0)

标量上下文中的列表赋值

标量上下文中的列表赋值返回右侧的元素数:

scalar(my ($hello, $there, $world) = (7,8)); #evaluates to 2
Run Code Online (Sandbox Code Playgroud)

为什么它评估右侧并生成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
Run Code Online (Sandbox Code Playgroud)

perl scalar list variable-assignment

12
推荐指数
2
解决办法
1848
查看次数

标签 统计

list ×1

perl ×1

scalar ×1

variable-assignment ×1