views:

38

answers:

2

Hello! 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�

+1  A: 

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

Romuald Brunet
A: 

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ö

sid_com