我知道
&
会禁用原型,但括号也不会这样做。这两个代码块有什么不同,顶部不能像底部那样工作是否有原因,
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);
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));
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)