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