There's a bit of a gotcha involved with redirecting filehandles when layers are involved. What I wanted to do was:
- Redirect STDOUT to a file, with the :utf8 layer
- Redirect STDERR to STDOUT (assuming STDOUT's :utf8 layer would still apply)
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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.