为什么 `sort List::Util::uniq(BAR, BAZ);` 与 `sort &List::Util::uniq(BAR, BAZ);` 不同

问题描述 投票:0回答:2

我知道

&
会禁用原型,但括号也不会这样做。这两个代码块有什么不同,顶部不能像底部那样工作是否有原因, use List::Util; use constant FOO => (1,2,3); use constant BAR => (2,3,4,5,6); use constant FOOBAR => sort List::Util::uniq(FOO, BAR);

use List::Util;
use constant FOO => (1,2,3);
use constant BAR => (2,3,4,5,6);
use constant FOOBAR => sort &List::Util::uniq(FOO, BAR);

perl prototype
2个回答
0
投票
排序

很特别。

警告:对列表进行排序时需要注意语法 从函数返回。如果你想对返回的列表进行排序 函数调用“find_records(@key)”,您可以使用:

my @contact = sort { $a cmp $b } find_records @key; my @contact = sort +find_records(@key); my @contact = sort &find_records(@key); my @contact = sort(find_records(@key));

如果您想通过比较对数组 @key 进行排序 例程“find_records()”然后你可以使用:

my @contact = sort { find_records() } @key; my @contact = sort find_records(@key); my @contact = sort(find_records @key); my @contact = sort(find_records (@key));
    

0
投票
sort

语法复杂。

sort LIST
sort BLOCK LIST
sort FUNCTION LIST

正如你可以想象的那样,有很多歧义。

当前的问题是您希望

sort List::Util::uniq(FOO, BAR)

被解析为

sort LIST
,但它被解析为
sort FUNCTION LIST
也可以按需要工作(不覆盖原型):

sort +List::Util::uniq(FOO, BAR)

sort { $a cmp $b } List::Util::uniq(FOO, BAR)
© www.soinside.com 2019 - 2024. All rights reserved.