7 use open OUT => ':utf8', ':std';
10 our $VERSION = '1.00';
13 "\xAD" => {string => '-'},
14 "\x{200E}" => {string => '→'},
15 "\x{200F}" => {string => '←'},
17 $info{chr $_} //= {} for 32 .. 126;
20 'digraphs.inc.pl' => 'u-di',
24 my $di = do $_ or die "Error reading digraphs file $_: ", $@ || $!;
25 while (my ($mnem, $cp) = each %$di) {
26 length $mnem == 2 or next; # limit to digraphs
27 $cp = chr $cp->[0] if ref $cp; # old style array
28 $info{$cp}->{di} //= $mnem;
29 $info{$cp}->{class}->{$_}++ for $diinc{$_};
33 for my $chr (keys %info) {
35 # attempt to get unicode character information
38 Unicode::UCD::charinfo($cp)
39 || { block => '?', category => 'Xn', name => '', script => '' }
42 $info->{$_} = $info{$chr}->{$_} for qw(di class string);
44 # categorise by unicode types and writing script
45 $info->{class}->{$_}++ for $info->{category};
46 $info->{class}->{"u-$_"}++ for $info->{script} || ();
48 # add custom categories for certain blocks
49 $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
50 $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
53 when ($info->{string}) {
54 # keep predefined presentational string
56 when ($info->{combining}) {
57 # overlay combining accents
58 $info->{string} = chr(9676) . $chr;
60 when (($cp & ~0b1001_1111) == 0 or $cp == 127) {
61 # control characters (first 32 chars from 0 and 128)
62 # rename to something more descriptive
63 $info->{name} = $info->{unicode10}
64 ? '<'.$info->{unicode10}.'>' # the old name was much more useful
65 : sprintf('<control U+%04X>', $cp); # at least identify by value
66 # show descriptive symbols instead of control chars themselves
67 $info->{string} = $cp < 32 ? chr($cp + 0x2400) : chr(0xFFFD);
74 # output perl code of hash
76 for my $cp (sort keys %info) {
77 $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
78 # convert info hashes into arrays of strings to output in display order
79 my $row = [ map { $info{$cp}->{$_} } qw/classstr name di string/ ];
80 # strip off trailing missing values (especially string may be unknown)
81 defined $row->[-1] ? last : pop @$row for 1 .. @$row;
82 # final line (assume safe within single quotes)
83 say sprintf '"\x{%X}" => [%s],',
84 ord $cp, join(',', map { escapeq($_) } @$row);
90 return 'undef' if not defined;
99 convert-unicode.pl - Gather Unicode character details in Perl array
103 convert-unicode > unicode-char.inc.pl
105 Test by printing the description of U+0041 (latin A):
107 perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
111 Mischa POSLAWSKY <perl@shiar.org>
115 Licensed under the GNU Affero General Public License version 3.