brycen has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
Unicoding is driving me batty today. I'm trying to understand this test output:
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.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
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.#!/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; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Why does perl's internal utf8 seem to allow single-byte latin1?
by ikegami (Patriarch) on Mar 24, 2010 at 03:30 UTC | |
by brycen (Monk) on Mar 24, 2010 at 04:36 UTC | |
by ikegami (Patriarch) on Mar 24, 2010 at 05:08 UTC | |
by ikegami (Patriarch) on Mar 24, 2010 at 05:15 UTC | |
Re: Why does perl's internal utf8 seem to allow single-byte latin1?
by repellent (Priest) on Mar 24, 2010 at 06:48 UTC | |
Re: Why does perl's internal utf8 seem to allow single-byte latin1?
by JavaFan (Canon) on Mar 24, 2010 at 08:25 UTC | |
by brycen (Monk) on Mar 24, 2010 at 18:13 UTC |
Back to
Seekers of Perl Wisdom