3 my $fname = 'unicode/UnicodeData-Latest.txt';
6 # This is not optimized in any way yet
9 $txt = do "unicode/Name.pl" unless $txt;
11 if ($^H{charnames_full} and $txt =~ /\t\t$name$/m) {
12 @off = ($-[0], $+[0]);
15 if ($^H{charnames_short} and $name =~ /^(.*?):(.*)/s) {
16 my ($script, $cname) = ($1,$2);
17 my $case = ( $cname =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL");
18 if ($txt =~ m/\t\t\U$script\E (?:$case )?LETTER \U$cname$/m) {
19 @off = ($-[0], $+[0]);
24 my $case = ( $name =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL");
25 for ( @{$^H{charnames_scripts}} ) {
26 (@off = ($-[0], $+[0])), last
27 if $txt =~ m/\t\t$_ (?:$case )?LETTER \U$name$/m;
30 die "Unknown charname '$name'" unless @off;
32 my $ord = hex substr $txt, $off[0] - 4, 4;
33 if ($^H & 0x8) { # "use bytes" in effect?
35 return chr $ord if $ord <= 255;
36 my $hex = sprintf '%X=0%o', $ord, $ord;
37 my $fname = substr $txt, $off[0] + 2, $off[1] - $off[0] - 2;
38 die "Character 0x$hex with name '$fname' is above 0xFF";
45 die "No scripts for `use charnames'" unless @_;
47 $^H{charnames} = \&charnames ;
50 $^H{charnames_full} = delete $h{':full'};
51 $^H{charnames_short} = delete $h{':short'};
52 $^H{charnames_scripts} = [map uc, keys %h];
61 charnames - define character names for C<\N{named}> string literal escape.
65 use charnames ':full';
66 print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";
68 use charnames ':short';
69 print "\N{greek:Sigma} is an upper-case sigma.\n";
71 use charnames qw(cyrillic greek);
72 print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";
76 Pragma C<use charnames> supports arguments C<:full>, C<:short> and
77 script names. If C<:full> is present, for expansion of
78 C<\N{CHARNAME}}> string C<CHARNAME> is first looked in the list of
79 standard Unicode names of chars. If C<:short> is present, and
80 C<CHARNAME> has the form C<SCRIPT:CNAME>, then C<CNAME> is looked up
81 as a letter in script C<SCRIPT>. If pragma C<use charnames> is used
82 with script name arguments, then for C<\N{CHARNAME}}> the name
83 C<CHARNAME> is looked up as a letter in the given scripts (in the
86 For lookup of C<CHARNAME> inside a given script C<SCRIPTNAME>
87 F<charcodes.pm> looks for the names
89 SCRIPTNAME CAPITAL LETTER CHARNAME
90 SCRIPTNAME SMALL LETTER CHARNAME
91 SCRIPTNAME LETTER CHARNAME
93 in the table of standard Unicode names. If C<CHARNAME> is lowercase,
94 then the C<CAPITAL> variant is ignored, otherwise C<SMALL> variant is
97 =head1 CUSTOM TRANSLATORS
99 The mechanism of translation is C<\N{...}> escapes is general and not
100 hardwired into F<charnames.pm>. A module can install custom
101 translations (inside the scope which C<use>s the module) by the
102 following magic incantation:
107 $^H{charnames} = \&translator;
110 Here translator() is a subroutine which takes C<CHARNAME> as an
111 argument, and returns text to insert into the string instead of the
112 C<\N{CHARNAME}> escape. Since the text to insert should be different
113 in C<utf8> mode and out of it, the function should check the current
114 state of C<utf8>-flag as in
118 return utf_translator(@_);
120 return no_utf_translator(@_);
126 Since evaluation of the translation function happens in a middle of
127 compilation (of a string literal), the translation function should not
128 do any C<eval>s or C<require>s. This restriction should be lifted in
129 a future version of Perl.