sid_com
sid_com

Reputation: 25117

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 $string;

my $columns = ( Term::Size::chars *STDOUT{IO} )[0];

say $columns;
say $string;

Output:

Hällö
140
H�ll�

Upvotes: 1

Views: 96

Answers (2)

sid_com
sid_com

Reputation: 25117

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ö

Upvotes: 0

Romuald Brunet
Romuald Brunet

Reputation: 5831

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

Upvotes: 1

Related Questions