>You are getting diverted by how perl happens internally to store a
>string. This is almost always completely irrelevant
This would be the case perhaps, but for fairly inconsistent and buggy support for UTF8 in Perl;
In 5.6 perhaps; 5.8.x is relatively bugless.
(And apart from that, the Perl docs make it very clear that the internals are UTF8, so it's fairly difficult to ignore it.)
I cannot stress this enough: if you are concerning yourself with perl's internal representation of a string, you are almost certianly looking in the wrong place for your problem.
I was pointing out that your claim that the strings in your example code are different doesn't make sense to me; if you use, say, unpack("H*", ..) to dump out the contents of the string both before and after encode_utf8, you find that they are byte-for-byte identical.
Forget H*. That is just misleading you. That is peeking into the internal details of how a string happens to be stored at moment in time. Almost all Perl-level string handling functions work on *characters* not bytes: length(), regular expressions etc. Think at all times in terms of characters, not bytes. Perl regards those two strings as completely different. For example:
use Encode; # create a string with one character my $a = "\x{100}"; # create a string with two characters my $b = Encode::encode_utf8($a); # Equivalent to $b = "\x80\xc4"; print $b; # outputs the two bytes C4 80 binmode(STDOUT, ":utf8"); print $a; # outputs the two bytes C4 80 print $b; # outputs the four bytes C3 84 C2 80
The only issue is in terms of the initial input and final out to/from external sources (eg sockets). At these interfaces there has to be a convertion to/from chararacters aka codepoints, into whatever encoding is expected at the other end. This is done either by setting the encoding on the socket, by the module doing the reading/writing, or explicitly by an Encode function.
You showed some code that merely demonstrated that the length function was interpreting the contents either as characters or bytes (presumably because one has the utf8 flag on and the other doesn't).
This is crucial. Until you understand why that sentence is wrong, you will continue living in a world of pain. The encode_utf8() function is not diddling with the internal format of a string, it is creating a completly new string that has a different number of characters in it, and that will behave differently with respect to all Perl string handling functions such as m// index(), chop(), lc() etc.

Dave.


In reply to Re^7: What does Encode::encode_utf8 do to UTF-8 data ? by dave_the_m
in thread What does Encode::encode_utf8 do to UTF-8 data ? by scollyer

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.