perl列表::AllUtils uniq&;分类

perl列表::AllUtils uniq&;分类,perl,Perl,试一试: perl -Mv5.14 -MList::AllUtils=uniq -E 'my(@x) = qw(2 3 1 2); say "@x"; my(@y) = uniq @x; say "@y"' 它能正确打印 2 3 1 2 2 3 1 现在需要排序的输出,因此尝试: perl -Mv5.14 -MList::AllUtils=uniq -E 'my(@x) = qw(2 3 1 2); say "@x"; my(@y) = sort uniq @x; say "@y"' 令人

试一试:

perl -Mv5.14 -MList::AllUtils=uniq -E 'my(@x) = qw(2 3 1 2); say "@x"; my(@y) = uniq @x; say "@y"'
它能正确打印

2 3 1 2
2 3 1
现在需要排序的输出,因此尝试:

perl -Mv5.14 -MList::AllUtils=uniq -E 'my(@x) = qw(2 3 1 2); say "@x"; my(@y) = sort uniq @x; say "@y"'
令人惊讶的是,它打印了:

2 3 1 2
2 1 3 2
切换
uniq
sort

perl -Mv5.14 -MList::AllUtils=uniq -E 'my(@x) = qw(2 3 1 2); say "@x"; my(@y) = uniq sort @x; say "@y"'
给出正确的结果

2 3 1 2
1 2 3
因此,使用
MO=Deparse
对它们进行比较

第一条:

perl -MO=Deparse -Mv5.14 -MList::AllUtils=uniq -E 'my(@x) = qw(2 3 1 2); say "@x"; my(@y) = uniq @x; say "@y"'
sub BEGIN {
    require v5.14;
}
use List::AllUtils (split(/,/u, 'uniq', 0));
use strict;
use feature 'current_sub', 'evalbytes', 'fc', 'postderef_qq', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
my(@x) = ('2', '3', '1', '2');
say join($", @x);
my(@y) = &uniq(@x);
say join($", @y);
-e syntax OK
第二点:

perl -MO=Deparse -Mv5.14 -MList::AllUtils=uniq -E 'my(@x) = qw(2 3 1 2); say "@x"; my(@y) = sort uniq @x; say "@y"'
sub BEGIN {
    require v5.14;
}
use List::AllUtils (split(/,/u, 'uniq', 0));
use strict;
use feature 'current_sub', 'evalbytes', 'fc', 'postderef_qq', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
my(@x) = ('2', '3', '1', '2');
say join($", @x);
my(@y) = (sort uniq @x);
say join($", @y);
-e syntax OK
第三:

perl -MO=Deparse -Mv5.14 -MList::AllUtils=uniq -E 'my(@x) = qw(2 3 1 2); say "@x"; my(@y) = uniq sort @x; say "@y"'
sub BEGIN {
    require v5.14;
}
use List::AllUtils (split(/,/u, 'uniq', 0));
use strict;
use feature 'current_sub', 'evalbytes', 'fc', 'postderef_qq', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
my(@x) = ('2', '3', '1', '2');
say join($", @x);
my(@y) = &uniq(sort(@x));
say join($", @y);
-e syntax OK
区别在于
uniq
子例程的调用方式:

my(@y) = (sort uniq @x);  # sort uniq @x
my(@y) = &uniq(sort(@x)); # uniq sort @x
据我所知,
uniq
是由
List::AllUtils
提供的子例程,
sort
是一个内置函数,但使用
uniq
如下:

my(@y) = &uniq(sort(@x));
对我来说似乎不是很直观


我必须以
&uniq(…)
的形式使用它,例如使用
&
和括号吗?如果您使用
use strict
use warnings
运行它,它会告诉您出了什么问题

use strict;
use warnings;
use feature 'say';
use List::AllUtils 'uniq';

my (@x) = qw(2 3 1 2);
say "@x";
my (@y) = sort uniq @x;
say "@y";
这会警告排序子例程在处未返回单个值
sort
认为
uniq
是它应该用来对列表进行排序的子例程

这就是原因

排序子名称列表
排序块列表
排序表

您还可以为其指定子名称。使用该子名称直接(即,不作为字符串或代码引用)有点违反直觉,但它应该是这样的。文件中甚至有一个例子


现在没有任何警告
sort+uniq(@x)
也可以工作,并且更易于阅读。

使用-Mv5.14启用
strict
,即使您的示例带有
警告
也不会打印任何警告。至少对我来说-使用
v5.24.0
@kobame-hmm这很奇怪。我得了5.20分。但这仍然是正在发生的事情。警告或不警告。只要
uniq
# sort using explicit subroutine name
sub byage {
    $age{$a} <=> $age{$b};  # presuming numeric
}
my @sortedclass = sort byage @class;
my (@x) = qw(2 3 1 2);
say "@x";
my (@y) = sort +uniq @x;
say "@y";

__END__
2 3 1 2
1 2 3