use charnames

use charnames HOW;
print "\N{CHARSPEC} is a funny character";

This lexically scoped pragma enables named characters to be interpolated into strings using the \N{CHARSPEC} notation:

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";

The pragma supports HOW arguments :full and :short, as well as specific script names.[1] The HOW argument determines how the character specified by the CHARSPEC in \N{CHARSPEC}} is to be searched for. If :full is present, the CHARSPEC is first looked for in the Unicode character tables as a complete Unicode character name. If :short is present and CHARSPEC has the form SCRIPTNAME:CHARNAME, CHARNAME is looked for as a letter in script SCRIPTNAME. If HOW contains specific script names, CHARSPEC is looked for as an individual CHARNAME in each of the given scripts, in the specified order.

For lookup of CHARNAME inside a given script SCRIPTNAME, the pragma looks in the table of standard Unicode names for patterns of the form:

SCRIPTNAME CAPITAL LETTER CHARNAME
SCRIPTNAME SMALL LETTER CHARNAME
SCRIPTNAME LETTER CHARNAME

If CHARNAME is entirely lowercase (as in \N{sigma}), the CAPITAL variant is ignored. Otherwise, the SMALL variant is ignored.

You can write your own module that works like the charnames pragma but defines character names differently. However, ...

Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.