List::Util - 减少 - 长度 - 编码 - 问题
为什么第一个reduce 示例得到错误的结果?
测试.txt
__BE
bb bbbbbbbbbbbbbbb
aaaaaa
测试.pl
#!/usr/bin/env perl
use warnings; use 5.012;
use open ':encoding(UTF-8)';
use List::Util qw(reduce);
use Encode;
my( @list, $longest, $len );
open my $fh, '<', 'test.txt' or die $!;
while( my $line = readline( $fh ) ) {
chomp $line;
push @list, split( /\s+/, $line );
}
close $fh;
$longest = reduce{ length($a) > length($b) ? $a : $b } @list;
$len = length $longest;
say $longest; # aaaaaa
say $len; # 6
$longest = reduce{ length(Encode::encode_utf8($a)) > length(Encode::encode_utf8($b)) ? $a : $b } @list;
$len = length(Encode::encode_utf8($longest));
say $longest; # bbbbbbbbbbbbbbb
say $len; # 15
$longest = $list[0];
$len = length $longest;
for my $str (@list) {
if ( length($str) > $len ) {
$longest = $str;
$len = length($str);
}
}
say $longest; # bbbbbbbbbbbbbbb
say $len; # 15
Why do I get a wrong result with the first reduce example?
test.txt
__BE
bb bbbbbbbbbbbbbbb
aaaaaa
test.pl
#!/usr/bin/env perl
use warnings; use 5.012;
use open ':encoding(UTF-8)';
use List::Util qw(reduce);
use Encode;
my( @list, $longest, $len );
open my $fh, '<', 'test.txt' or die $!;
while( my $line = readline( $fh ) ) {
chomp $line;
push @list, split( /\s+/, $line );
}
close $fh;
$longest = reduce{ length($a) > length($b) ? $a : $b } @list;
$len = length $longest;
say $longest; # aaaaaa
say $len; # 6
$longest = reduce{ length(Encode::encode_utf8($a)) > length(Encode::encode_utf8($b)) ? $a : $b } @list;
$len = length(Encode::encode_utf8($longest));
say $longest; # bbbbbbbbbbbbbbb
say $len; # 15
$longest = $list[0];
$len = length $longest;
for my $str (@list) {
if ( length($str) > $len ) {
$longest = $str;
$len = length($str);
}
}
say $longest; # bbbbbbbbbbbbbbb
say $len; # 15
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAICS,它甚至可能是 Perl 中的一个错误......它的行为是否正确当然并不明显。我修改了第一个reduce以打印诊断信息:
当使用Perl 5.13.4在MacOS X(10.6.5)上运行时,我得到的输出是:
从所有情况来看,第一个reduce的第一个参数始终是零长度字符串,即使在它包含一些数据的奇怪情况下也是如此。
如果删除 '
use open ':encoding(UTF-8)';
' 行,则它的行为正常。这可能表明该错误存在于文件 I/O、UTF-8 编码和 List::Util 交互中的某个位置。另一方面,它可能在更隐蔽的地方。但我的印象是,您有一个可重现的测试用例,并且可以在 Perl 及其核心模块中的某个位置报告为可能的错误。
AFAICS, it might even be a bug in Perl...it certainly isn't obvious that it is behaving correctly. I modified the first reduce to print diagnostics as it goes:
When run on MacOS X (10.6.5) using Perl 5.13.4, the output I get is:
To all appearances, the first argument to the first reduce is always a zero length string, even on those odd occasions when it contains some data.
If the '
use open ':encoding(UTF-8)';
' line is removed, then it behaves sanely.That might suggest that the bug is somewhere in the interaction of file I/O, UTF-8 encoding and List::Util. On the other hand, it could be somewhere more obscure. But my impression is that you have a test case that is reproducible and could be reported as a possible bug somewhere in Perl and its core modules.
我已将此 报告为 List::Util 中的错误修改这个程序。
I've reported this as bug in List::Util after trying to modify this program.