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

select a variable as stdout and utf8 flag behaviour

Thread Next
From:
Gert Brinkmann
Date:
November 9, 2016 14:55
Subject:
select a variable as stdout and utf8 flag behaviour
Message ID:
90839dbc-288d-dff6-af0b-f98c8e62480d@netcologne.de
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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About