1b39c5158Smillert#!/usr/local/bin/perl -w
2b39c5158Smillertuse strict;
3b39c5158Smillert
4b39c5158Smillertuse 5.007003;
5b39c5158Smillertuse Hash::Util qw(lock_hash unlock_hash lock_keys);
6b39c5158Smillertuse Storable qw(nfreeze);
7b39c5158Smillert
8b39c5158Smillert# If this looks like a hack, it's probably because it is :-)
9b39c5158Smillertsub uuencode_it {
10b39c5158Smillert  my ($data, $name) = @_;
11b39c5158Smillert  my $frozen = nfreeze $data;
12b39c5158Smillert
13b39c5158Smillert  my $uu = pack 'u', $frozen;
14b39c5158Smillert
15b39c5158Smillert  printf "begin %3o $name\n", ord 'A';
16b39c5158Smillert  print $uu;
17b39c5158Smillert  print "\nend\n\n";
18b39c5158Smillert}
19b39c5158Smillert
20b39c5158Smillert
21b39c5158Smillertmy %hash = (perl=>"rules");
22b39c5158Smillert
23b39c5158Smillertlock_hash %hash;
24b39c5158Smillert
25b39c5158Smillertuuencode_it (\%hash, "Locked hash");
26b39c5158Smillert
27b39c5158Smillertunlock_hash %hash;
28b39c5158Smillert
29b39c5158Smillertlock_keys %hash, 'perl', 'rules';
30b39c5158Smillertlock_hash %hash;
31b39c5158Smillert
32b39c5158Smillertuuencode_it (\%hash, "Locked hash placeholder");
33b39c5158Smillert
34b39c5158Smillertunlock_hash %hash;
35b39c5158Smillert
36b39c5158Smillertlock_keys %hash, 'perl';
37b39c5158Smillert
38b39c5158Smillertuuencode_it (\%hash, "Locked keys");
39b39c5158Smillert
40b39c5158Smillertunlock_hash %hash;
41b39c5158Smillert
42b39c5158Smillertlock_keys %hash, 'perl', 'rules';
43b39c5158Smillert
44b39c5158Smillertuuencode_it (\%hash, "Locked keys placeholder");
45b39c5158Smillert
46b39c5158Smillertunlock_hash %hash;
47b39c5158Smillert
48b39c5158Smillertmy $utf8 = "\x{DF}\x{100}";
49b39c5158Smillertchop $utf8;
50b39c5158Smillert
51b39c5158Smillertuuencode_it (\$utf8, "Short 8 bit utf8 data");
52b39c5158Smillert
53b39c5158Smillertmy $utf8b = $utf8;
54b39c5158Smillertutf8::encode ($utf8b);
55b39c5158Smillert
56b39c5158Smillertuuencode_it (\$utf8b, "Short 8 bit utf8 data as bytes");
57b39c5158Smillert
58b39c5158Smillert$utf8 x= 256;
59b39c5158Smillert
60b39c5158Smillertuuencode_it (\$utf8, "Long 8 bit utf8 data");
61b39c5158Smillert
62b39c5158Smillert$utf8 = "\x{C0FFEE}";
63b39c5158Smillert
64b39c5158Smillertuuencode_it (\$utf8, "Short 24 bit utf8 data");
65b39c5158Smillert
66b39c5158Smillert$utf8b = $utf8;
67b39c5158Smillertutf8::encode ($utf8b);
68b39c5158Smillert
69b39c5158Smillertuuencode_it (\$utf8b, "Short 24 bit utf8 data as bytes");
70b39c5158Smillert
71b39c5158Smillert$utf8 x= 256;
72b39c5158Smillert
73b39c5158Smillertuuencode_it (\$utf8, "Long 24 bit utf8 data");
74b39c5158Smillert
75b39c5158Smillert# Hash which has the utf8 bit set, but no longer has any utf8 keys
76b39c5158Smillertmy %uhash = ("\x{100}", "gone", "perl", "rules");
77b39c5158Smillertdelete $uhash{"\x{100}"};
78b39c5158Smillert
79b39c5158Smillert# use Devel::Peek; Dump \%uhash;
80b39c5158Smillertuuencode_it (\%uhash, "Hash with utf8 flag but no utf8 keys");
81b39c5158Smillert
82b39c5158Smillert$utf8 = "Schlo\xdf" . chr 256;
83b39c5158Smillertchop $utf8;
84b39c5158Smillertmy $a_circumflex = (ord ('A') == 193 ? "\x47" : "\xe5");
85b39c5158Smillert%uhash = (map {$_, $_} 'castle', "ch${a_circumflex}teau", $utf8, "\x{57CE}");
86b39c5158Smillert
87b39c5158Smillertuuencode_it (\%uhash, "Hash with utf8 keys");
88b39c5158Smillert
89b39c5158Smillertlock_hash %uhash;
90b39c5158Smillert
91b39c5158Smillertuuencode_it (\%uhash, "Locked hash with utf8 keys");
92b39c5158Smillert
93*256a93a4Safresh1my %pre58;
94b39c5158Smillert
95b39c5158Smillertwhile (my ($key, $val) = each %uhash) {
96b39c5158Smillert  # hash keys are always stored downgraded to bytes if possible, with a flag
97b39c5158Smillert  # to say "promote back to utf8"
98b39c5158Smillert  # Whereas scalars are stored as is.
99b39c5158Smillert  utf8::encode ($key) if ord $key > 256;
100b39c5158Smillert  $pre58{$key} = $val;
101b39c5158Smillert
102b39c5158Smillert}
103b39c5158Smillertuuencode_it (\%pre58, "Hash with utf8 keys for 5.6");
104