First, the code you posted and the code you ran is actually different. There's only one byte in the string assigned to $test in the code you posted. There was two in the code you ran.

Second, you'll find Devel::Peek's Dump more useful at debugging this kind of problem.

Internally >perl -MDevel::Peek -e"Dump(qq{\x{C2AE}});" __ encoded SV = PV(0x23734c) at 0x235fc0 / as UTF-8 REFCNT = 1 / FLAGS = (PADBUSY,PADTMP,POK,READONLY,pPOK,UTF8) PV = 0x182f574 "\354\212\256"\0 [UTF8 "\x{c2ae}"] CUR = 3 -----v------ ---v---- LEN = 4 \ \____ String \ \_______________________ Internal encoding (bytes) >perl -MDevel::Peek -e"Dump('abc');" SV = PV(0x236e00) at 0x182f778 REFCNT = 1 FLAGS = (PADBUSY,PADTMP,POK,READONLY,pPOK) PV = 0x182f56c "abc"\0 CUR = 3 -v- Bytes treated as LEN = 4 \________ iso-latin-1 by perl functions

Can anyone tell me why the following code:

It all boils down to the following: Until you tell it otherwise by using "use utf8;", perl treats source code as iso-latin-1. You created an UTF-8 source file, and failed to notify perl.

$test is assigned two bytes or two iso-latin-1 chars. (Same thing, the difference is in how its used.) Adding "use utf8;" before the constant will cause the constant to be decoded from UTF-8.

When you call encode('UTF-8'), you're encoding characters that you've never decoded, producing junk.

On a related note, what is the difference between:
$test = "\x{05D0}\x{20AC}";
and
$test = "\x05\xD0\x20\xAC";

The first is a string of two UNICODE characters (internally encoded as UTF-8).
The second is a string of four bytes or four iso-latin-1 characters.

>perl -MDevel::Peek -e"Dump(qq{\x{05D0}\x{20AC}});" SV = PV(0x237354) at 0x235fc8 REFCNT = 1 FLAGS = (PADBUSY,PADTMP,POK,READONLY,pPOK,UTF8) PV = 0x18307bc "\327\220\342\202\254"\0 [UTF8 "\x{5d0}\x{20ac}"] CUR = 5 LEN = 8 >perl -MDevel::Peek -e"Dump(qq{\x05\xD0\x20\xAC});" SV = PV(0x237354) at 0x235fc8 REFCNT = 1 FLAGS = (PADBUSY,PADTMP,POK,READONLY,pPOK) PV = 0x18307bc "\5\320 \254"\0 CUR = 4 LEN = 8

In reply to Re: UTF-8 representation question by ikegami
in thread UTF-8 representation question by bpa

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.