Hello,

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 );
$fh->close();

#You need to activate this line to make utf8 output correct
#Encode::_utf8_on($html);

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 = '';
Encode::_utf8_on($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.

Greetings
Gert

Reply via email to