in reply to Re^6: What does Encode::encode_utf8 do to UTF-8 data ?
in thread What does Encode::encode_utf8 do to UTF-8 data ?
>You are getting diverted by how perl happens internally to store aIn 5.6 perhaps; 5.8.x is relatively bugless.
>string. This is almost always completely irrelevant
This would be the case perhaps, but for fairly inconsistent and buggy support for UTF8 in Perl;
(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:
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.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
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: What does Encode::encode_utf8 do to UTF-8 data ?
by scollyer (Sexton) on Oct 03, 2005 at 20:04 UTC |