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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.