Dear Monks,
Unicoding is driving me batty today. I'm trying to understand this test output:
Ein <#c3><#96>konomisches Modell
Ein <#d6>konomisches Modell 1
Ein <#d6>konomisches Modell 1
Ein <#c3><#83><#c2><#96>konomisches Modell
Ein <#c3><#96>konomisches Modell
Ein <#c3><#96>konomisches Modell
Where strings supposedly in perl's internal utf8 show <#d6> in some cases and <#c3><#96> in others.
This matters because we use Storable::nfreeze() to store the data, which just copies Perl's internal format. What I expected from the script was the two byte sequence in all cases.
#!/usr/bin/perl -w
no utf8;
use Encode;
my ($ustring1, $ustring2, $ustring3);
$ustring1 = "Ein Ökonomisches Modell";
$ustring2 = "Ein \326konomisches Modell";
utf8::upgrade($ustring2);
$ustring3 = "Ein \326konomisches Modell";
$ustring3 = decode('latin1',$ustring3, 1);
print "\n";
print print_chrcodes($ustring1)." ".utf8::is_utf8($ustring1)."\n";
print print_chrcodes($ustring2)." ".utf8::is_utf8($ustring2)."\n";
print print_chrcodes($ustring3)." ".utf8::is_utf8($ustring3)."\n";
print "\n";
print print_chrcodes(encode('utf-8-strict',$ustring1))."\n";
print print_chrcodes(encode('utf-8-strict',$ustring2))."\n";
print print_chrcodes(encode('utf-8-strict',$ustring3))."\n";
sub print_chrcodes
{
my $str = shift;
my $ret;
foreach my $ascval (unpack("C*", $str)) {
if($ascval == 13) {
$ret .= '<cr>';
next;
}
if($ascval == 10) {
$ret .= '<nl>';
next;
}
if($ascval < 128) {
$ret .= chr($ascval);
next;
}
$ret .= sprintf ("<#%x>",$ascval) if($ascval >= 128);
}
return $ret;
}
How can I force the internal perl representation to be two-byte utf-8, so that Storable::nfreeze() output approximates utf-8-strict?
Keywords: Unicode, utf-8, utf-8-strict, Perl 5.10, Storable.
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.