develooper Front page | perl.unicode | Postings from November 2016

select a variable as stdout and utf8 flag behaviour

Thread Next
Gert Brinkmann
November 9, 2016 14:55
select a variable as stdout and utf8 flag behaviour
Message ID:

I have the following example code:

use strict;
use utf8;
use Encode;
use FileHandle;

binmode STDOUT, ":utf8";

my $html = '';

#-- open filehandle to write into the $html variable as utf8
open(my $fh, '>:encoding(UTF-8)', \$html);
my $orig_stdout = select( $fh );

print "Ümläut Test ßaß; 使用下列语言\n";

select( $orig_stdout );

#You need to activate this line to make utf8 output correct

print $html;

This prints out the utf8 characters corrupted. You have to flag the
Variable after writing into it with Encode::_utf8_on() as utf8 to make
it work correctly. (So activate the commented line.)

Using this _utf8_on() usually means that I am doing something wrong. Is
there a better way to achieve the correct behaviour?

Btw. there was a change in the behaviour between perl v5.14.2 and
v5.20.2: In older perl versions you could do a

my $html = '';

before opening the file handle onto this variable. In newer perl
versions the utf8 flag is reset on open() and print() to the variable's
file handle.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About