Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

charnames

Perl 5 version 10.1 documentation
Recently read

charnames

NAME

charnames - define character names for \N{named} string literal escapes

SYNOPSIS

  1. use charnames ':full';
  2. print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";
  3. use charnames ':short';
  4. print "\N{greek:Sigma} is an upper-case sigma.\n";
  5. use charnames qw(cyrillic greek);
  6. print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";
  7. use charnames ":full", ":alias" => {
  8. e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE",
  9. };
  10. print "\N{e_ACUTE} is a small letter e with an acute.\n";
  11. use charnames ();
  12. print charnames::viacode(0x1234); # prints "ETHIOPIC SYLLABLE SEE"
  13. printf "%04X", charnames::vianame("GOTHIC LETTER AHSA"); # prints "10330"

DESCRIPTION

Pragma use charnames supports arguments :full , :short , script names and customized aliases. If :full is present, for expansion of \N{CHARNAME} , the string CHARNAME is first looked up in the list of standard Unicode character names. If :short is present, and CHARNAME has the form SCRIPT:CNAME , then CNAME is looked up as a letter in script SCRIPT . If pragma use charnames is used with script name arguments, then for \N{CHARNAME} the name CHARNAME is looked up as a letter in the given scripts (in the specified order). Customized aliases are explained in CUSTOM ALIASES.

For lookup of CHARNAME inside a given script SCRIPTNAME this pragma looks for the names

  1. SCRIPTNAME CAPITAL LETTER CHARNAME
  2. SCRIPTNAME SMALL LETTER CHARNAME
  3. SCRIPTNAME LETTER CHARNAME

in the table of standard Unicode names. If CHARNAME is lowercase, then the CAPITAL variant is ignored, otherwise the SMALL variant is ignored.

Note that \N{...} is compile-time, it's a special form of string constant used inside double-quoted strings: in other words, you cannot use variables inside the \N{...} . If you want similar run-time functionality, use charnames::vianame().

For the C0 and C1 control characters (U+0000..U+001F, U+0080..U+009F) as of Unicode 3.1, there are no official Unicode names but you can use instead the ISO 6429 names (LINE FEED, ESCAPE, and so forth). In Unicode 3.2 (as of Perl 5.8) some naming changes take place ISO 6429 has been updated, see ALIASES. Also note that the U+UU80, U+0081, U+0084, and U+0099 do not have names even in ISO 6429.

Since the Unicode standard uses "U+HHHH", so can you: "\N{U+263a}" is the Unicode smiley face, or "\N{WHITE SMILING FACE}".

ALIASES

A few aliases have been defined for convenience: instead of having to use the official names

  1. LINE FEED (LF)
  2. FORM FEED (FF)
  3. CARRIAGE RETURN (CR)
  4. NEXT LINE (NEL)

(yes, with parentheses) one can use

  1. LINE FEED
  2. FORM FEED
  3. CARRIAGE RETURN
  4. NEXT LINE
  5. LF
  6. FF
  7. CR
  8. NEL

One can also use

  1. BYTE ORDER MARK
  2. BOM

and

  1. ZWNJ
  2. ZWJ

for ZERO WIDTH NON-JOINER and ZERO WIDTH JOINER.

For backward compatibility one can use the old names for certain C0 and C1 controls

  1. old new
  2. HORIZONTAL TABULATION CHARACTER TABULATION
  3. VERTICAL TABULATION LINE TABULATION
  4. FILE SEPARATOR INFORMATION SEPARATOR FOUR
  5. GROUP SEPARATOR INFORMATION SEPARATOR THREE
  6. RECORD SEPARATOR INFORMATION SEPARATOR TWO
  7. UNIT SEPARATOR INFORMATION SEPARATOR ONE
  8. PARTIAL LINE DOWN PARTIAL LINE FORWARD
  9. PARTIAL LINE UP PARTIAL LINE BACKWARD

but the old names in addition to giving the character will also give a warning about being deprecated.

CUSTOM ALIASES

This version of charnames supports three mechanisms of adding local or customized aliases to standard Unicode naming conventions (:full)

Anonymous hashes

  1. use charnames ":full", ":alias" => {
  2. e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE",
  3. };
  4. my $str = "\N{e_ACUTE}";

Alias file

  1. use charnames ":full", ":alias" => "pro";
  2. will try to read "unicore/pro_alias.pl" from the @INC path. This
  3. file should return a list in plain perl:
  4. (
  5. A_GRAVE => "LATIN CAPITAL LETTER A WITH GRAVE",
  6. A_CIRCUM => "LATIN CAPITAL LETTER A WITH CIRCUMFLEX",
  7. A_DIAERES => "LATIN CAPITAL LETTER A WITH DIAERESIS",
  8. A_TILDE => "LATIN CAPITAL LETTER A WITH TILDE",
  9. A_BREVE => "LATIN CAPITAL LETTER A WITH BREVE",
  10. A_RING => "LATIN CAPITAL LETTER A WITH RING ABOVE",
  11. A_MACRON => "LATIN CAPITAL LETTER A WITH MACRON",
  12. );

Alias shortcut

  1. use charnames ":alias" => ":pro";
  2. works exactly the same as the alias pairs, only this time,
  3. ":full" is inserted automatically as first argument (if no
  4. other argument is given).

charnames::viacode(code)

Returns the full name of the character indicated by the numeric code. The example

  1. print charnames::viacode(0x2722);

prints "FOUR TEARDROP-SPOKED ASTERISK".

Returns undef if no name is known for the code.

This works only for the standard names, and does not yet apply to custom translators.

Notice that the name returned for of U+FEFF is "ZERO WIDTH NO-BREAK SPACE", not "BYTE ORDER MARK".

charnames::vianame(name)

Returns the code point indicated by the name. The example

  1. printf "%04X", charnames::vianame("FOUR TEARDROP-SPOKED ASTERISK");

prints "2722".

Returns undef if the name is unknown.

This works only for the standard names, and does not yet apply to custom translators.

CUSTOM TRANSLATORS

The mechanism of translation of \N{...} escapes is general and not hardwired into charnames.pm. A module can install custom translations (inside the scope which uses the module) with the following magic incantation:

  1. sub import {
  2. shift;
  3. $^H{charnames} = \&translator;
  4. }

Here translator() is a subroutine which takes CHARNAME as an argument, and returns text to insert into the string instead of the \N{CHARNAME} escape. Since the text to insert should be different in bytes mode and out of it, the function should check the current state of bytes -flag as in:

  1. use bytes (); # for $bytes::hint_bits
  2. sub translator {
  3. if ($^H & $bytes::hint_bits) {
  4. return bytes_translator(@_);
  5. }
  6. else {
  7. return utf8_translator(@_);
  8. }
  9. }

ILLEGAL CHARACTERS

If you ask by name for a character that does not exist, a warning is given and the Unicode replacement character "\x{FFFD}" is returned.

If you ask by code for a character that does not exist, no warning is given and undef is returned. (Though if you ask for a code point past U+10FFFF you do get a warning.)

BUGS

Unicode standard named sequences are not recognized, such as LATIN CAPITAL LETTER A WITH MACRON AND GRAVE (which should mean LATIN CAPITAL LETTER A WITH MACRON with an additional COMBINING GRAVE ACCENT ).

Since evaluation of the translation function happens in a middle of compilation (of a string literal), the translation function should not do any evals or requires. This restriction should be lifted in a future version of Perl.