View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Jarkko Hietaniemi > perl-5.7.3 > charnames



Annotate this POD

Module Version: 1.01   Source   Latest Release: perl-5.28.0-RC1


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


  use charnames ':full';
  print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";

  use charnames ':short';
  print "\N{greek:Sigma} is an upper-case sigma.\n";

  use charnames qw(cyrillic greek);
  print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";

  print charname::viacode(0x1234); # prints "ETHIOPIC SYLLABLE SEE"
  printf "%04X", charname::vianame("GOTHIC LETTER AHSA"); # prints "10330"


Pragma use charnames supports arguments :full, :short and script names. If :full is present, for expansion of \N{CHARNAME}} string CHARNAME is first looked in the list of standard Unicode names of chars. 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).

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


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 some naming changes will happen since ISO 6429 has been updated. Also note that the U+UU80, U+0081, U+0084, and U+0099 do not have names even in ISO 6429.


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

    use charnames ();           # for $charnames::hint_bits
    sub import {
        $^H |= $charnames::hint_bits;
        $^H{charnames} = \&translator;

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:

    use bytes ();                       # for $bytes::hint_bits
    sub translator {
        if ($^H & $bytes::hint_bits) {
            return bytes_translator(@_);
        else {
            return utf8_translator(@_);

charnames::viacode(code) ^

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

    print charnames::viacode(0x2722);


Returns undef if no name is known for the code.

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

charnames::vianame(code) ^

Returns the code point indicated by the name. The example

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

prints "2722".

Returns undef if no name is known for the name.

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


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


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.

syntax highlighting: