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

标签 perl prototype

我知道 & 会禁用原型(prototype),但括号也不会这样做。这两个代码块有什么不同,顶部不能像底部一样运行是否有原因,

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);

最佳答案

sort很特别。

Warning: syntactical care is required when sorting the list returned from a function. If you want to sort the list returned by the function call "find_records(@key)", you can use:

           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));

If instead you want to sort the array @key with the comparison routine "find_records()" then you can use:

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

关于perl - 为什么 `sort List::Util::uniq(BAR, BAZ);` 与 `sort &List::Util::uniq(BAR, BAZ);` 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77533584/

相关文章:

regex - 如何将字符串的所有数字子字符串括在大括号中?

perl - 我可以使用 Catalyst 来显示图像或图表吗?

multithreading - 我如何终止 Perl 线程?

javascript - 为什么定义的属性中的 "this"范围错误?

javascript - 在 JS 中将参数作为单独的参数传递

javascript - 如何定义新 Element TD 的 COLSPAN

perl - 如何开始使用 Perl 6?

通过 .netrc 对 perl CGI 脚本进行 Git 身份验证

c++ - 如何正确使用模板?我的模板 <> 存在数据类型问题

javascript - 通过原型(prototype)定义方法与在构造函数中使用它相比——真的有性能差异吗?