1charnames(3pm) Perl Programmers Reference Guide charnames(3pm)
2
3
4
6 charnames - access to Unicode character names and named character
7 sequences; also define character names
8
10 use charnames ':full';
11 print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";
12 print "\N{LATIN CAPITAL LETTER E WITH VERTICAL LINE BELOW}",
13 " is an officially named sequence of two Unicode characters\n";
14
15 use charnames ':loose';
16 print "\N{Greek small-letter sigma}",
17 "can be used to ignore case, underscores, most blanks,"
18 "and when you aren't sure if the official name has hyphens\n";
19
20 use charnames ':short';
21 print "\N{greek:Sigma} is an upper-case sigma.\n";
22
23 use charnames qw(cyrillic greek);
24 print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";
25
26 use charnames ":full", ":alias" => {
27 e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE",
28 mychar => 0xE8000, # Private use area
29 };
30 print "\N{e_ACUTE} is a small letter e with an acute.\n";
31 print "\N{mychar} allows me to name private use characters.\n";
32
33 use charnames ();
34 print charnames::viacode(0x1234); # prints "ETHIOPIC SYLLABLE SEE"
35 printf "%04X", charnames::vianame("GOTHIC LETTER AHSA"); # prints
36 # "10330"
37 print charnames::vianame("LATIN CAPITAL LETTER A"); # prints 65 on
38 # ASCII platforms;
39 # 193 on EBCDIC
40 print charnames::string_vianame("LATIN CAPITAL LETTER A"); # prints "A"
41
43 Pragma "use charnames" is used to gain access to the names of the
44 Unicode characters and named character sequences, and to allow you to
45 define your own character and character sequence names.
46
47 All forms of the pragma enable use of the following 3 functions:
48
49 · "charnames::string_vianame(name)" for run-time lookup of a either a
50 character name or a named character sequence, returning its string
51 representation
52
53 · "charnames::vianame(name)" for run-time lookup of a character name
54 (but not a named character sequence) to get its ordinal value (code
55 point)
56
57 · "charnames::viacode(code)" for run-time lookup of a code point to
58 get its Unicode name.
59
60 Starting in Perl v5.16, any occurrence of "\N{CHARNAME}" sequences in a
61 double-quotish string automatically loads this module with arguments
62 ":full" and ":short" (described below) if it hasn't already been loaded
63 with different arguments, in order to compile the named Unicode
64 character into position in the string. Prior to v5.16, an explicit
65 "use charnames" was required to enable this usage. (However, prior to
66 v5.16, the form "use charnames ();" did not enable "\N{CHARNAME}".)
67
68 Note that "\N{U+...}", where the ... is a hexadecimal number, also
69 inserts a character into a string. The character it inserts is the one
70 whose code point (ordinal value) is equal to the number. For example,
71 "\N{U+263a}" is the Unicode (white background, black foreground) smiley
72 face equivalent to "\N{WHITE SMILING FACE}". Also note, "\N{...}" can
73 mean a regex quantifier instead of a character name, when the ... is a
74 number (or comma separated pair of numbers (see "QUANTIFIERS" in
75 perlreref), and is not related to this pragma.
76
77 The "charnames" pragma supports arguments ":full", ":loose", ":short",
78 script names and customized aliases.
79
80 If ":full" is present, for expansion of "\N{CHARNAME}", the string
81 CHARNAME is first looked up in the list of standard Unicode character
82 names.
83
84 ":loose" is a variant of ":full" which allows CHARNAME to be less
85 precisely specified. Details are in "LOOSE MATCHES".
86
87 If ":short" is present, and CHARNAME has the form "SCRIPT:CNAME", then
88 CNAME is looked up as a letter in script SCRIPT, as described in the
89 next paragraph. Or, if "use charnames" is used with script name
90 arguments, then for "\N{CHARNAME}" the name CHARNAME is looked up as a
91 letter in the given scripts (in the specified order). Customized
92 aliases can override these, and are explained in "CUSTOM ALIASES".
93
94 For lookup of CHARNAME inside a given script SCRIPTNAME, this pragma
95 looks in the table of standard Unicode names for the names
96
97 SCRIPTNAME CAPITAL LETTER CHARNAME
98 SCRIPTNAME SMALL LETTER CHARNAME
99 SCRIPTNAME LETTER CHARNAME
100
101 If CHARNAME is all lowercase, then the "CAPITAL" variant is ignored,
102 otherwise the "SMALL" variant is ignored, and both CHARNAME and
103 SCRIPTNAME are converted to all uppercase for look-up. Other than
104 that, both of them follow loose rules if ":loose" is also specified;
105 strict otherwise.
106
107 Note that "\N{...}" is compile-time; it's a special form of string
108 constant used inside double-quotish strings; this means that you cannot
109 use variables inside the "\N{...}". If you want similar run-time
110 functionality, use charnames::string_vianame().
111
112 Since Unicode 6.0, it is deprecated to use "BELL". Instead use "ALERT"
113 (but "BEL" will continue to work).
114
115 If the input name is unknown, "\N{NAME}" raises a warning and
116 substitutes the Unicode REPLACEMENT CHARACTER (U+FFFD).
117
118 For "\N{NAME}", it is a fatal error if "use bytes" is in effect and the
119 input name is that of a character that won't fit into a byte (i.e.,
120 whose ordinal is above 255).
121
122 Otherwise, any string that includes a "\N{charname}" or
123 "\N{U+code point}" will automatically have Unicode semantics (see "Byte
124 and Character Semantics" in perlunicode).
125
127 By specifying ":loose", Unicode's loose character name matching
128 <http://www.unicode.org/reports/tr44#Matching_Rules> rules are selected
129 instead of the strict exact match used otherwise. That means that
130 CHARNAME doesn't have to be so precisely specified. Upper/lower case
131 doesn't matter (except with scripts as mentioned above), nor do any
132 underscores, and the only hyphens that matter are those at the
133 beginning or end of a word in the name (with one exception: the hyphen
134 in U+1180 "HANGUL JUNGSEONG O-E" does matter). Also, blanks not
135 adjacent to hyphens don't matter. The official Unicode names are quite
136 variable as to where they use hyphens versus spaces to separate word-
137 like units, and this option allows you to not have to care as much.
138 The reason non-medial hyphens matter is because of cases like U+0F60
139 "TIBETAN LETTER -A" versus U+0F68 "TIBETAN LETTER A". The hyphen here
140 is significant, as is the space before it, and so both must be
141 included.
142
143 ":loose" slows down look-ups by a factor of 2 to 3 versus ":full", but
144 the trade-off may be worth it to you. Each individual look-up takes
145 very little time, and the results are cached, so the speed difference
146 would become a factor only in programs that do look-ups of many
147 different spellings, and probably only when those look-ups are through
148 vianame() and string_vianame(), since "\N{...}" look-ups are done at
149 compile time.
150
152 Starting in Unicode 6.1 and Perl v5.16, Unicode defines many
153 abbreviations and names that were formerly Perl extensions, and some
154 additional ones that Perl did not previously accept. The list is
155 getting too long to reproduce here, but you can get the complete list
156 from the Unicode web site:
157 <http://www.unicode.org/Public/UNIDATA/NameAliases.txt>.
158
159 Earlier versions of Perl accepted almost all the 6.1 names. These were
160 most extensively documented in the v5.14 version of this pod:
161 <http://perldoc.perl.org/5.14.0/charnames.html#ALIASES>.
162
164 You can add customized aliases to standard (":full") Unicode naming
165 conventions. The aliases override any standard definitions, so, if
166 you're twisted enough, you can change "\N{LATIN CAPITAL LETTER A}" to
167 mean "B", etc.
168
169 Note that an alias should not be something that is a legal curly brace-
170 enclosed quantifier (see "QUANTIFIERS" in perlreref). For example
171 "\N{123}" means to match 123 non-newline characters, and is not treated
172 as a charnames alias. Aliases are discouraged from beginning with
173 anything other than an alphabetic character and from containing
174 anything other than alphanumerics, spaces, dashes, parentheses, and
175 underscores. Currently they must be ASCII.
176
177 An alias can map to either an official Unicode character name (not a
178 loose matched name) or to a numeric code point (ordinal). The latter
179 is useful for assigning names to code points in Unicode private use
180 areas such as U+E800 through U+F8FF. A numeric code point must be a
181 non-negative integer or a string beginning with "U+" or "0x" with the
182 remainder considered to be a hexadecimal integer. A literal numeric
183 constant must be unsigned; it will be interpreted as hex if it has a
184 leading zero or contains non-decimal hex digits; otherwise it will be
185 interpreted as decimal.
186
187 Aliases are added either by the use of anonymous hashes:
188
189 use charnames ":alias" => {
190 e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE",
191 mychar1 => 0xE8000,
192 };
193 my $str = "\N{e_ACUTE}";
194
195 or by using a file containing aliases:
196
197 use charnames ":alias" => "pro";
198
199 This will try to read "unicore/pro_alias.pl" from the @INC path. This
200 file should return a list in plain perl:
201
202 (
203 A_GRAVE => "LATIN CAPITAL LETTER A WITH GRAVE",
204 A_CIRCUM => "LATIN CAPITAL LETTER A WITH CIRCUMFLEX",
205 A_DIAERES => "LATIN CAPITAL LETTER A WITH DIAERESIS",
206 A_TILDE => "LATIN CAPITAL LETTER A WITH TILDE",
207 A_BREVE => "LATIN CAPITAL LETTER A WITH BREVE",
208 A_RING => "LATIN CAPITAL LETTER A WITH RING ABOVE",
209 A_MACRON => "LATIN CAPITAL LETTER A WITH MACRON",
210 mychar2 => "U+E8001",
211 );
212
213 Both these methods insert ":full" automatically as the first argument
214 (if no other argument is given), and you can give the ":full"
215 explicitly as well, like
216
217 use charnames ":full", ":alias" => "pro";
218
219 ":loose" has no effect with these. Input names must match exactly,
220 using ":full" rules.
221
222 Also, both these methods currently allow only single characters to be
223 named. To name a sequence of characters, use a custom translator
224 (described below).
225
227 This is a runtime equivalent to "\N{...}". name can be any expression
228 that evaluates to a name accepted by "\N{...}" under the ":full" option
229 to "charnames". In addition, any other options for the controlling
230 "use charnames" in the same scope apply, like ":loose" or any script
231 list, ":short" option, or custom aliases you may have defined.
232
233 The only difference is that if the input name is unknown,
234 "string_vianame" returns "undef" instead of the REPLACEMENT CHARACTER
235 and does not raise a warning message.
236
238 This is similar to "string_vianame". The main difference is that under
239 most circumstances, vianame returns an ordinal code point, whereas
240 "string_vianame" returns a string. For example,
241
242 printf "U+%04X", charnames::vianame("FOUR TEARDROP-SPOKED ASTERISK");
243
244 prints "U+2722".
245
246 This leads to the other two differences. Since a single code point is
247 returned, the function can't handle named character sequences, as these
248 are composed of multiple characters (it returns "undef" for these.
249 And, the code point can be that of any character, even ones that aren't
250 legal under the "use bytes" pragma,
251
252 See "BUGS" for the circumstances in which the behavior differs from
253 that described above.
254
256 Returns the full name of the character indicated by the numeric code.
257 For example,
258
259 print charnames::viacode(0x2722);
260
261 prints "FOUR TEARDROP-SPOKED ASTERISK".
262
263 The name returned is the "best" (defined below) official name or alias
264 for the code point, if available; otherwise your custom alias for it,
265 if defined; otherwise "undef". This means that your alias will only be
266 returned for code points that don't have an official Unicode name (nor
267 alias) such as private use code points.
268
269 If you define more than one name for the code point, it is
270 indeterminate which one will be returned.
271
272 As mentioned, the function returns "undef" if no name is known for the
273 code point. In Unicode the proper name of these is the empty string,
274 which "undef" stringifies to. (If you ask for a code point past the
275 legal Unicode maximum of U+10FFFF that you haven't assigned an alias
276 to, you get "undef" plus a warning.)
277
278 The input number must be a non-negative integer, or a string beginning
279 with "U+" or "0x" with the remainder considered to be a hexadecimal
280 integer. A literal numeric constant must be unsigned; it will be
281 interpreted as hex if it has a leading zero or contains non-decimal hex
282 digits; otherwise it will be interpreted as decimal.
283
284 As mentioned above under "ALIASES", Unicode 6.1 defines extra names
285 (synonyms or aliases) for some code points, most of which were already
286 available as Perl extensions. All these are accepted by "\N{...}" and
287 the other functions in this module, but "viacode" has to choose which
288 one name to return for a given input code point, so it returns the
289 "best" name. To understand how this works, it is helpful to know more
290 about the Unicode name properties. All code points actually have only
291 a single name, which (starting in Unicode 2.0) can never change once a
292 character has been assigned to the code point. But mistakes have been
293 made in assigning names, for example sometimes a clerical error was
294 made during the publishing of the Standard which caused words to be
295 misspelled, and there was no way to correct those. The Name_Alias
296 property was eventually created to handle these situations. If a name
297 was wrong, a corrected synonym would be published for it, using
298 Name_Alias. "viacode" will return that corrected synonym as the "best"
299 name for a code point. (It is even possible, though it hasn't happened
300 yet, that the correction itself will need to be corrected, and so
301 another Name_Alias can be created for that code point; "viacode" will
302 return the most recent correction.)
303
304 The Unicode name for each of the control characters (such as LINE FEED)
305 is the empty string. However almost all had names assigned by other
306 standards, such as the ASCII Standard, or were in common use.
307 "viacode" returns these names as the "best" ones available. Unicode
308 6.1 has created Name_Aliases for each of them, including alternate
309 names, like NEW LINE. "viacode" uses the original name, "LINE FEED" in
310 preference to the alternate. Similarly the name returned for U+FEFF is
311 "ZERO WIDTH NO-BREAK SPACE", not "BYTE ORDER MARK".
312
313 Until Unicode 6.1, the 4 control characters U+0080, U+0081, U+0084, and
314 U+0099 did not have names nor aliases. To preserve backwards
315 compatibility, any alias you define for these code points will be
316 returned by this function, in preference to the official name.
317
318 Some code points also have abbreviated names, such as "LF" or "NL".
319 "viacode" never returns these.
320
321 Because a name correction may be added in future Unicode releases, the
322 name that "viacode" returns may change as a result. This is a rare
323 event, but it does happen.
324
326 The mechanism of translation of "\N{...}" escapes is general and not
327 hardwired into charnames.pm. A module can install custom translations
328 (inside the scope which "use"s the module) with the following magic
329 incantation:
330
331 sub import {
332 shift;
333 $^H{charnames} = \&translator;
334 }
335
336 Here translator() is a subroutine which takes CHARNAME as an argument,
337 and returns text to insert into the string instead of the
338 "\N{CHARNAME}" escape.
339
340 This is the only way you can create a custom named sequence of code
341 points.
342
343 Since the text to insert should be different in "bytes" mode and out of
344 it, the function should check the current state of "bytes"-flag as in:
345
346 use bytes (); # for $bytes::hint_bits
347 sub translator {
348 if ($^H & $bytes::hint_bits) {
349 return bytes_translator(@_);
350 }
351 else {
352 return utf8_translator(@_);
353 }
354 }
355
356 See "CUSTOM ALIASES" above for restrictions on CHARNAME.
357
358 Of course, "vianame", "viacode", and "string_vianame" would need to be
359 overridden as well.
360
362 vianame() normally returns an ordinal code point, but when the input
363 name is of the form "U+...", it returns a chr instead. In this case,
364 if "use bytes" is in effect and the character won't fit into a byte, it
365 returns "undef" and raises a warning.
366
367 Names must be ASCII characters only, which means that you are out of
368 luck if you want to create aliases in a language where some or all the
369 characters of the desired aliases are non-ASCII.
370
371 Since evaluation of the translation function (see "CUSTOM TRANSLATORS")
372 happens in the middle of compilation (of a string literal), the
373 translation function should not do any "eval"s or "require"s. This
374 restriction should be lifted (but is low priority) in a future version
375 of Perl.
376
377
378
379perl v5.16.3 2013-03-04 charnames(3pm)