charnames - define character names for \N{named} string literal escapes.
\N{named}
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).
use charnames
:full
:short
\N{CHARNAME}}
CHARNAME
SCRIPT:CNAME
CNAME
SCRIPT
For lookup of CHARNAME inside a given script SCRIPTNAME this pragma looks for the names
SCRIPTNAME
SCRIPTNAME CAPITAL LETTER CHARNAME SCRIPTNAME SMALL LETTER CHARNAME 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.
CAPITAL
SMALL
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().
\N{...}
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 charnames.pm. A module can install custom translations (inside the scope which uses the module) with the following magic incantation:
use
use charnames (); # for $charnames::hint_bits sub import { shift; $^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:
\N{CHARNAME}
bytes
use bytes (); # for $bytes::hint_bits sub translator { if ($^H & $bytes::hint_bits) { return bytes_translator(@_); } else { return utf8_translator(@_); } }
Returns the full name of the character indicated by the numeric code. The example
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 aply to custom translators.
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.
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.
eval
require
To install if, copy and paste the appropriate command in to your terminal.
cpanm
cpanm if
CPAN shell
perl -MCPAN -e shell install if
For more information on module installation, please visit the detailed CPAN module installation guide.