is_utf8 is used to check if perl's internal representation of a string has been "upgraded" to utf8. That is, the string cannot possibly be represented in ascii, such as your: my $test = "\x{100}\x{2764}";. When you decode a utf8 string it is converted to perl's internal representation. This just happens to be utf8, or something close, but it doesn't have to be and worrying about it is just a waste of time.

Really, you only have to worry about decoding input and encoding output (edit: I had encoding/decoding backwards!). You received mangled text on your files/screen because you are having JSON encode it's results as a utf8-encoded string. JSON returns a string of bytes that are utf8 encoded.

Next, when you write to the filehandle, having set :utf8 on the filehandle, your bytes are automatically encoded to utf8 again. You have doubly-encoded the string and it is quite predictably garbage.

Quoth the utf8 manpage:

Do not use this pragma for anything else than telling Perl that your script is written in UTF-8.

Read the links under the SEE ALSO section of the utf8 docs for more about unicode. I also had this bookmarked, I remember it being pretty good: http://www.ahinea.com/en/tech/perl-unicode-struggle.html


In reply to Re: JSON, UTF-8 and Filehandles by juster
in thread JSON, UTF-8 and Filehandles by Kirsle

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.