I have a string of Japanese text in utf-8 format (I've looked at the bytes and they seem to be correctly addressing the utf-8 code pages). Some of the characters are 3 bytes long (e4 bb 96 - 他).

Encode::decode complains about the wide characters. However, Encode::decode_utf8 doesn't. Using the debugger typing 'x $var' outputs the string to the screen properly (although my terminal can't display the 3 byte characters, it replaces them with boxes).

However, using warn or print causes the string to be mangled. It's also getting mangled when it goes to the database. I'm running perl with -CS, and I've tried setting binmode to utf8 for both STDOUT and STDERR.

I feel like I'm missing something fundamental about these strings, but I keep having to relearn about utf8 encoding every time I run into encoding problems.

Update

Turns out I hadn't looked at the strong close enough. There were \u200b characters in the string that Encode didn't like. Stripping them out resolved the issue.


In reply to Triple Byte UTF8 Characters by rwadkins

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.