http://qs1969.pair.com?node_id=830439

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:
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.