Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Unicode strings internals

by Krambambuli (Deacon)
on May 10, 2013 at 16:49 UTC ( #1032983=note: print w/ replies, xml ) Need Help??


in reply to [SOLVED] Unicode strings internals

Have a look into the results of

#!/usr/bin/perl use strict; use warnings; use Devel::Peek; use Encode; #use utf8; binmode STDOUT, ":utf8"; my $string1 = "123\x{444}\x{444}\x{444}\x{444}"; _display ($string1, 'STRING1' ); my $utfstring = "123 \x{439}\x{439}\x{439}\x{439}"; _display ($utfstring, 'UTF_STRING' ); my ($ascii_but_utf, undef) = split ' ', $utfstring; _display ($ascii_but_utf, 'ASCII_BUT_UTF' ); #my $bytestring = encode ("UTF-8", "\x{444}\x{444}\x{444}\x{444}"); my $bytestring = "\x{444}\x{444}\x{444}\x{444}"; _display ($bytestring, 'BYTESTRING' ); my $mixedstring = "$ascii_but_utf$bytestring"; # simulate The Unicode +Bug here _display ($mixedstring, 'MIXEDSTRING' ); print "MIXEDSTRING and STRING1 are supposed to be identical...\n"; exit; ############### sub _display { my ($string, $name) = @_; print "$name:\n"; Dump $string; my $l1 = length($string); my $l2 = bytes::length($string); if ($l1 != $l2) { print "LENGTHs DIFFERS: length: $l1, bytes: $l2\n" } print "UTF IS ON\n" if utf8::is_utf8($string); print "\n"; }
and then check the difference you see for BYTESTRING when running

my $bytestring = encode ("UTF-8", "\x{444}\x{444}\x{444}\x{444}");

versus

my $bytestring = "\x{444}\x{444}\x{444}\x{444}";

The Encode documentation has an Caveat about it:

CAVEAT: When you run "$octets = encode("utf8", $string)", then $octets might not be equal to $string. Though
both contain the same data, the UTF8 flag for $octets is always off. When you encode anything, the UTF8 flag
on the result is always off, even when it contains a completely valid utf8 string. See "The UTF8 flag" below.


Comment on Re: Unicode strings internals
Download Code
Re^2: Unicode strings internals
by vsespb (Hermit) on May 10, 2013 at 17:12 UTC
    Yes, I understand that result of encode("utf8", ... ) is a byte string with UTF-8 flag off. But that does not answer the question in my post. In my example, both poc1.pl and poc2.pl print strings with UTF-8 on, with length <> bytes::length, but those strings behave differently. Why?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032983]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-09-17 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (98 votes), past polls