Why does Term::Size seem to mess up Perl's output encoding?
The Term::Size-module jumbles up the encoding. How can I fix this?
#!/usr/bin/env perl
use warnings; use strict;
use 5.010;
use utf8;
binmode STDOUT, ':encoding(UTF-8)';
use Term::Size;
my $string = 'Hällö';
say $strin开发者_运维百科g;
my $columns = ( Term::Size::chars *STDOUT{IO} )[0];
say $columns;
say $string;
Output:
Hällö
140 H�ll�
Setting the binmode after getting the column count seems to do the trick:
say $string;
my $columns = ( Term::Size::chars *STDOUT{IO} )[0];
binmode STDOUT, ':encoding(UTF-8)';
say $columns;
say $string;
Outputs
Hällö
80
Hällö
The strange thing is that this code works fine with perl 5.8 (the output is correct) without having tho reset the binmode
Or using "chars":
#!/usr/bin/env perl
use strict;
use warnings;
use 5.012;
use utf8;
binmode STDOUT, ':encoding(UTF-8)';
use Term::Size qw(chars);
my $string = 'Hällö';
say $string;
my $columns = ( chars )[0];
say $columns;
say $string;
Output:
Hällö
82
Hällö
精彩评论