我猜你可以这样写,以最少的假设来处理你所处理的输入类型(只需传递适当的比较子):
use List::Util;
sub identical {
my @this = @{ +shift };
my @that = @{ +shift };
my $cmp = shift // sub { shift eq shift };
return '' unless @this == @that;
for my $idx (List::Util::shuffle keys @this) {
return '' unless $cmp->($this[$idx], $that[$idx]);
}
return 1;
}
它的行为类似于:
0> identical([0..100], [0..100])
$res[0] = 1
1> identical([0..100], ['0.0', 1..100])
$res[1] = ''
2> identical([0..100], ['0.0', 1..100], sub {shift == shift})
$res[2] = 1
3> identical(['0.66666666666', 0..10], [2/3, 0..10], sub {shift == shift})
$res[3] = ''
4> identical(['0.66666666666', 0..10], [2/3, 0..10], sub {shift() - shift() < 1e-5})
$res[4] = 1
5> identical([0..100], [List::Util::shuffle(0..100)])
$res[5] = ''