You just need to do binmode OUTVAR2, ":utf8"; before you print any wide characters to that output file handle.

UTF-8 output mode is not turned on by default, nor is it automatically set when scalars whose utf8 flag is set happen to be printed to the file handle.

(update: you should do binmode STDOUT, ":utf8"; as well, if you're going to print wide characters to STDOUT.)

Another update: (sorry -- I should have read the whole post, carefully, sooner) So you are printing utf8 data to an in-memory scalar-variable-as-file, and then you want to just print the contents of that scalar as if it were not an in-memory file (but just a utf8 string).

The problem there is that using the scalar as a file makes it impossible for perl to realize that it's really a utf8 string (the fact that you "opened" the file with ">:utf8" does not cause the variable's "utf8 flag" to be turned on).

So if you want perl to treat that variable as a utf8 string, you have to set the utf8 flag, and the supported way to do that is to use Encode::decode...

use Encode; # ... treat $output2 as an output file, then: $output2 = decode( "utf8", $output2 ); # assuming that STDOUT is set to utf8 mode, you can now safely print "$output2\n";
When the utf8 flag isn't set, but there are high-bit bytes in the string and the output file handle is set to utf8, perl will pretend that the string is actually Latin1, and "promote" it to utf8, which of course is a form of corruption in your case.

In reply to Re: utf8 encoding and warnings woe by graff
in thread utf8 encoding and warnings woe by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.