标量上下文中的列表赋值返回右侧的元素数:
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)