Subject: | aliases inside reduce give strange results usder utf8 |
test.txt:
__BE
bb bbbbbbbbbbbbbbb
aaaaaa
program:
#!/usr/bin/env perl
use 5.010;
use warnings;
#use open ':encoding(UTF-8)';
use List::Util qw(reduce);
use Encode;
my( @list, $longest, $len );
open my $fh, '<:utf8', 'test.txt' or die $!;
while( my $line = readline( $fh ) ) {
chomp $line;
push @list, split( /\s+/, $line );
}
close $fh;
$longest = reduce{ my $c=length($a) > length($b) ? $a : $b;say "'$a'
l=".length($a).' - '."'$b' l=".length($b)." => '$c'";$c } @list;
foreach $el (@list) {
say "$el - ".length($el);
}
$len = length $longest;
say $longest; # aaaaaa
say $len; # 6
Result from inside reduce are very strange.
Use of uninitialized value $a in concatenation (.) or string at a.pl
line 16.
Use of uninitialized value $a in length at a.pl line 16.
'' l=0 - 'bb' l=2 => '__BE'
'__BE' l=4 - 'bbbbbbbbbbbbbbb' l=15 => 'bbbbbbbbbbbbbbb'
'bbbbbbbbbbbbbbb' l=4 - 'aaaaaa' l=6 => 'aaaaaa'
__BE - 4
bb - 2
bbbbbbbbbbbbbbb - 15
aaaaaa - 6
aaaaaa
6
With ":utf8" removed from open (operator not pragma), results are
better, but first line still prints $a as empty.
perl 5.10.1 and 5.12.1 , List::Util 1.23_03
--
Alexandr Ciornii, http://chorny.net