Your test isn't runnable, so I had to adapt it a bit

#!/usr/bin/perl use strict; use warnings; use Encode qw( encode from_to ); print("Content-Type: text/html; charset=utf-8\n\n"); my $name = encode('utf-16le', "\x{65E5}\x{672C}\x{8A9E}"); eval { from_to($name, 'utf-16le', 'utf-8', Encode::FB_CROAK); 1 } or print("coaked\n"); print $name;

It works fine. It displays "Japanese (language)" in Japanese.

From the prompt, I get:

$ test.cgi | od -c 0000000 C o n t e n t - T y p e : t +e 0000020 x t / h t m l ; c h a r s e +t 0000040 = u t f - 8 \n \n 346 227 245 346 234 254 350 25 +2 0000060 236 0000061

Find what differs, and you'll find which bad assumption you made.

By the way, the following is a better model:

#!/usr/bin/perl use strict; use warnings; use Encode qw( encode decode ); binmode(STDOUT, ':encoding(utf-8)'); print("Content-Type: text/html; charset=utf-8\n\n"); # Input my $name = encode('utf-16le', "\x{65E5}\x{672C}\x{8A9E}"); eval { $name = decode('utf-16le', $name, Encode::FB_CROAK); 1 } or print("coaked\n"); # Process # ... # Output print $name;

In reply to Re: Encoding problem in perl by ikegami
in thread Encoding problem in perl by bluewhale

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.