7 use bytes (); # for $bytes::hint_bits
8 $charnames::hint_bits = 0x20000;
11 # Icky 3.2 names with parentheses.
12 'LINE FEED' => 'LINE FEED (LF)',
13 'FORM FEED' => 'FORM FEED (FF)',
14 'CARRIAGE RETURN' => 'CARRIAGE RETURN (CR)',
15 'NEXT LINE' => 'NEXT LINE (NEL)',
17 'LF' => 'LINE FEED (LF)',
18 'FF' => 'FORM FEED (FF)',
19 'CR' => 'CARRIAGE RETURN (LF)',
20 'NEL' => 'NEXT LINE (NEL)',
21 'BOM' => 'BYTE ORDER MARK',
25 # Pre-3.2 compatibility (only for the first 256 characters).
26 'HORIZONTAL TABULATION' => 'CHARACTER TABULATION',
27 'VERTICAL TABULATION' => 'LINE TABULATION',
28 'FILE SEPARATOR' => 'INFORMATION SEPARATOR FOUR',
29 'GROUP SEPARATOR' => 'INFORMATION SEPARATOR THREE',
30 'RECORD SEPARATOR' => 'INFORMATION SEPARATOR TWO',
31 'UNIT SEPARATOR' => 'INFORMATION SEPARATOR ONE',
32 'PARTIAL LINE DOWN' => 'PARTIAL LINE FORWARD',
33 'PARTIAL LINE UP' => 'PARTIAL LINE BACKWARD',
38 # This is not optimized in any way yet
43 if (exists $alias1{$name}) {
44 $name = $alias1{$name};
46 if (exists $alias2{$name}) {
48 warnings::warnif('deprecated', qq{Unicode character name "$name" is deprecated, use "$alias2{$name}" instead});
49 $name = $alias2{$name};
56 if ($name eq "BYTE ORDER MARK") {
60 ## Suck in the code/name list as a big string.
62 ## "0052\t\tLATIN CAPITAL LETTER R\n"
63 $txt = do "unicore/Name.pl" unless $txt;
65 ## @off will hold the index into the code/name string of the start and
66 ## end of the name as we find it.
68 ## If :full, look for the the name exactly
69 if ($^H{charnames_full} and $txt =~ /\t\t\Q$name\E$/m) {
70 @off = ($-[0], $+[0]);
73 ## If we didn't get above, and :short allowed, look for the short name.
74 ## The short name is like "greek:Sigma"
76 if ($^H{charnames_short} and $name =~ /^(.+?):(.+)/s) {
77 my ($script, $cname) = ($1,$2);
78 my $case = ( $cname =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL");
79 if ($txt =~ m/\t\t\U$script\E (?:$case )?LETTER \U\Q$cname\E$/m) {
80 @off = ($-[0], $+[0]);
85 ## If we still don't have it, check for the name among the loaded
89 my $case = ( $name =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL");
90 for my $script ( @{$^H{charnames_scripts}} )
92 if ($txt =~ m/\t\t$script (?:$case )?LETTER \U\Q$name\E$/m) {
93 @off = ($-[0], $+[0]);
99 ## If we don't have it by now, give up.
101 carp "Unknown charname '$name'";
106 ## Now know where in the string the name starts.
107 ## The code, in hex, is befor that.
109 ## The code can be 4-6 characters long, so we've got to sort of
110 ## go look for it, just after the newline that comes before $off[0].
112 ## This would be much easier if unicore/Name.pl had info in
113 ## a name/code order, instead of code/name order.
115 ## The +1 after the rindex() is to skip past the newline we're finding,
116 ## or, if the rindex() fails, to put us to an offset of zero.
118 my $hexstart = rindex($txt, "\n", $off[0]) + 1;
120 ## we know where it starts, so turn into number -
121 ## the ordinal for the char.
122 $ord = hex substr($txt, $hexstart, $off[0] - $hexstart);
125 if ($^H & $bytes::hint_bits) { # "use bytes" in effect?
127 return chr $ord if $ord <= 255;
128 my $hex = sprintf "%04x", $ord;
129 if (not defined $fname) {
130 $fname = substr $txt, $off[0] + 2, $off[1] - $off[0] - 2;
132 croak "Character 0x$hex with name '$fname' is above 0xFF";
135 no warnings 'utf8'; # allow even illegal characters
136 return pack "U", $ord;
141 shift; ## ignore class name
145 carp("`use charnames' needs explicit imports list");
147 $^H |= $charnames::hint_bits;
148 $^H{charnames} = \&charnames ;
151 ## fill %h keys with our @_ args.
156 $^H{charnames_full} = delete $h{':full'};
157 $^H{charnames_short} = delete $h{':short'};
158 $^H{charnames_scripts} = [map uc, keys %h];
161 ## If utf8? warnings are enabled, and some scripts were given,
162 ## see if at least we can find one letter of each script.
164 if (warnings::enabled('utf8') && @{$^H{charnames_scripts}})
166 $txt = do "unicore/Name.pl" unless $txt;
168 for my $script (@{$^H{charnames_scripts}})
170 if (not $txt =~ m/\t\t$script (?:CAPITAL |SMALL )?LETTER /) {
171 warnings::warn('utf8', "No such script: '$script'");
177 require Unicode::UCD; # for Unicode::UCD::_getcode()
184 carp "charnames::viacode() expects one numeric argument";
189 my $code = Unicode::UCD::_getcode($arg);
194 $hex = sprintf "%04X", $arg;
196 carp("unexpected arg \"$arg\" to charnames::viacode()");
200 if ($code > 0x10FFFF) {
201 carp "Unicode characters only allocated up to 0x10FFFF (you asked for $hex)";
205 return $viacode{$hex} if exists $viacode{$hex};
207 $txt = do "unicore/Name.pl" unless $txt;
209 if ($txt =~ m/^$hex\t\t(.+)/m) {
210 return $viacode{$hex} = $1;
211 } elsif ($hex eq 'FFFE') {
212 return $viacode{$hex} = "BYTE ORDER MARK";
214 carp "Unknown charcode '$hex'";
224 carp "charnames::vianame() expects one name argument";
230 return $vianame{$arg} if exists $vianame{$arg};
232 $txt = do "unicore/Name.pl" unless $txt;
234 if ($txt =~ m/^([0-9A-F]+)\t\t($arg)/m) {
235 return $vianame{$arg} = hex $1;
247 charnames - define character names for C<\N{named}> string literal escapes.
251 use charnames ':full';
252 print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";
254 use charnames ':short';
255 print "\N{greek:Sigma} is an upper-case sigma.\n";
257 use charnames qw(cyrillic greek);
258 print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";
260 print charnames::viacode(0x1234); # prints "ETHIOPIC SYLLABLE SEE"
261 printf "%04X", charnames::vianame("GOTHIC LETTER AHSA"); # prints "10330"
265 Pragma C<use charnames> supports arguments C<:full>, C<:short> and
266 script names. If C<:full> is present, for expansion of
267 C<\N{CHARNAME}}> string C<CHARNAME> is first looked in the list of
268 standard Unicode names of chars. If C<:short> is present, and
269 C<CHARNAME> has the form C<SCRIPT:CNAME>, then C<CNAME> is looked up
270 as a letter in script C<SCRIPT>. If pragma C<use charnames> is used
271 with script name arguments, then for C<\N{CHARNAME}}> the name
272 C<CHARNAME> is looked up as a letter in the given scripts (in the
275 For lookup of C<CHARNAME> inside a given script C<SCRIPTNAME>
276 this pragma looks for the names
278 SCRIPTNAME CAPITAL LETTER CHARNAME
279 SCRIPTNAME SMALL LETTER CHARNAME
280 SCRIPTNAME LETTER CHARNAME
282 in the table of standard Unicode names. If C<CHARNAME> is lowercase,
283 then the C<CAPITAL> variant is ignored, otherwise the C<SMALL> variant
286 Note that C<\N{...}> is compile-time, it's a special form of string
287 constant used inside double-quoted strings: in other words, you cannot
288 use variables inside the C<\N{...}>. If you want similar run-time
289 functionality, use charnames::vianame().
291 For the C0 and C1 control characters (U+0000..U+001F, U+0080..U+009F)
292 as of Unicode 3.1, there are no official Unicode names but you can
293 use instead the ISO 6429 names (LINE FEED, ESCAPE, and so forth).
294 In Unicode 3.2 some naming changes will happen since ISO 6429 has been
295 updated. Also note that the U+UU80, U+0081, U+0084, and U+0099
296 do not have names even in ISO 6429.
298 =head1 CUSTOM TRANSLATORS
300 The mechanism of translation of C<\N{...}> escapes is general and not
301 hardwired into F<charnames.pm>. A module can install custom
302 translations (inside the scope which C<use>s the module) with the
303 following magic incantation:
305 use charnames (); # for $charnames::hint_bits
308 $^H |= $charnames::hint_bits;
309 $^H{charnames} = \&translator;
312 Here translator() is a subroutine which takes C<CHARNAME> as an
313 argument, and returns text to insert into the string instead of the
314 C<\N{CHARNAME}> escape. Since the text to insert should be different
315 in C<bytes> mode and out of it, the function should check the current
316 state of C<bytes>-flag as in:
318 use bytes (); # for $bytes::hint_bits
320 if ($^H & $bytes::hint_bits) {
321 return bytes_translator(@_);
324 return utf8_translator(@_);
328 =head1 charnames::viacode(code)
330 Returns the full name of the character indicated by the numeric code.
333 print charnames::viacode(0x2722);
335 prints "FOUR TEARDROP-SPOKED ASTERISK".
337 Returns undef if no name is known for the code.
339 This works only for the standard names, and does not yet aply
340 to custom translators.
342 =head1 charnames::vianame(code)
344 Returns the code point indicated by the name.
347 printf "%04X", charnames::vianame("FOUR TEARDROP-SPOKED ASTERISK");
351 Returns undef if no name is known for the name.
353 This works only for the standard names, and does not yet aply
354 to custom translators.
358 A few aliases have been defined for convenience: instead of having
359 to use the official names
366 (yes, with parentheses) one can use
382 though that is of course not a legal character as such.
384 For backward compatibility one can use the old names for
385 certain C0 and C1 controls
389 HORIZONTAL TABULATION CHARACTER TABULATION
390 VERTICAL TABULATION LINE TABULATION
391 FILE SEPARATOR INFORMATION SEPARATOR FOUR
392 GROUP SEPARATOR INFORMATION SEPARATOR THREE
393 RECORD SEPARATOR INFORMATION SEPARATOR TWO
394 UNIT SEPARATOR INFORMATION SEPARATOR ONE
395 PARTIAL LINE DOWN PARTIAL LINE FORWARD
396 PARTIAL LINE UP PARTIAL LINE BACKWARD
398 but the old names in addition to giving the character
399 will also give a warning about being deprecated.
401 =head1 ILLEGAL CHARACTERS
403 If you ask for a character that does not exist, a warning is given
404 and the Unicode I<replacement character> "\x{FFFD}" is returned.
408 Since evaluation of the translation function happens in a middle of
409 compilation (of a string literal), the translation function should not
410 do any C<eval>s or C<require>s. This restriction should be lifted in
411 a future version of Perl.