在標量上下文中的列表賦值返回右手邊的元素個數:列表賦值
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 2012-02-17 00:32:12
@darch,這沒有任何意義。 '($ hello,$ there,$ world)'(和'(7,8)')不能在'($ hello,$ there,$ world)=(7,8)'的標量上下文中執行。列表分配可能會返回3,如果它想,但沒有理由。 – ikegami 2012-02-17 05:11:17
我的回答正是「那麼[列表($ hello,$ there,$ world)]在標量上下文中進行評估」。評估我的($ h,$ t,$ w)=(7,8,undef);標量($ h,$ t,$ w)'並且看到列表不是數組。 – darch 2012-02-17 20:06:42