Perl has two kinds of strings. Strings of bytes and strings of characters. When you use use utf8, literals are decoded from a string of UTF-8 bytes into a string characters.

use Encode qw( decode ); $s = "高"; print(length($s), "\n"); # 3 $s = decode('utf8', "高"); print(length($s), "\n"); # 1 $s = do { use utf8; "高" }; print(length($s), "\n"); # 1

→ Replace 高 with the UTF-8 encoding of 高 in the above source.

Writting to a ':utf8' handle reverses the process.

use Encode qw( encode ); my $s = do { use utf8; "高" }; print(length($s), "\n"); # 1 open my $fh, '>:utf8', \$s2; print $fh $s; print(length($s2), "\n"); # 3 $s2 = encode('utf8', $s); print(length($s2), "\n"); # 3

→ Replace 高 with the UTF-8 encoding of 高 in the above source.

In context that means $str contains 6 characters, while $output2 contains 18 bytes. Since both contain wildly different contents, it's to be expected that they behave differently.

The warning is given when a string of characters is outputed in :bytes mode (the default, the opposite of :utf8). The gibberish comes from encoding a string twice. One solution:

use strict; use warnings; use utf8; use Encode qw( decode ); my $chars_src = "采样速率太高"; my $bytes_dst; { open my $fh, '>:utf8', \$bytes_dst; print $fh $chars_src; } my $chars_dst = decode('utf8', $bytes_dst); binmode(STDOUT, ':utf8'); print("$chars_src\n"); print("$chars_dst\n");

→ Replace 采样速率太&#39640 with the UTF-8 encoding of 采样速率太高 in the above source.

Another solution:

... my $bytes_src = encode('utf8', $chars_src); print("$bytes_src\n"); print("$bytes_dst\n");

Another solution:

... binmode(STDOUT, ':utf8'); print("$chars_src\n"); binmode(STDOUT, ':bytes'); print("$bytes_dst\n");

I like using hungarian notation (bytes_ vs chars_) when dealing with encoded and unencoded values. Making Wrong Code Look Wrong is a good essay on the subject.

Update: Added extra solutions.
Update: Added "reverses the process" bit.


In reply to Re: utf8 encoding and warnings woe by ikegami
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.