There's a bit of a gotcha involved with redirecting filehandles when layers are involved. What I wanted to do was:

I found that text going to STDOUT was correctly UTF8 encoded, but text going to STDERR wasn't.

You need to add the :utf8 layer to STDERR separately. As far as I can see, there is no way of combining layers with redirection in the same open statement. Instead, you need to use binmode on STDERR after redirecting it to STDOUT.

#!/usr/bin/perl use strict; use warnings; my $utf8="\xe9"; close (STDOUT); open( STDOUT, ">>:utf8", 'test.txt' ) or die $!; close STDERR; open( STDERR, ">>&STDOUT" ) or die $!; print ("STDOUT: ",$utf8,"\n"); print STDERR ("STDERR: ",$utf8,"\n"); binmode (STDERR,':utf8'); print STDERR ("STDERR with binmode: ",$utf8,"\n"); PRINTS: STDOUT: é STDERR: \xe9 (not literally - prints character \xe9, no +t UTF-8 encoded) STDERR with binmode: é

Clint

(thanks to tye and Joost for helping me figure this out)

Update: Filed as bug 44701

Replies are listed 'Best First'.
Re: Redirecting STDERR to STDOUT with UTF8 layer
by clinton (Priest) on Aug 16, 2007 at 12:39 UTC
    Something else I've noticed. According to perlfunc, if you try to re-open STDOUT or STDERR as an "in memory" file, you have to close it first, implying that if you want to re-open it to as a file, you DON'T need to close it first.

    However, if you don't close it first, then the ':utf8' layer won't be applied:

    #!/usr/bin/perl use strict; use warnings; my $utf8="\xe9"; open STDOUT, ">>:utf8", 'test.txt' or die $!; print "No close: ",$utf8,"\n"; close STDOUT; open STDOUT, ">>:utf8", 'test.txt' or die $!; print "Close: ",$utf8,"\n"; PRINTS: No close: \xe9 (not literally - prints character \xe9, not UTF- +8 encoded) Close: é

    Clint

    Update: Filed as bug 44703