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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.