Runtime Lookups
This pragma also provides three functions for converting between
character names and numbers at runtime, rather than at compile time the
way \N{
interpolation works. These are:CHARNAME}
charnames::vianameTakes an official name, official alias, or custom alias and returns a single integer codepoint. For example, it converts the string
"LATIN SMALL LETTER A"into 0x61.charnames::string_vianameTakes a string that can be an official name, an official alias, or a named sequence and gives back a string. For example, this converts
"LATIN SMALL LETTER A"into"a". Because of named sequences, the string returned may (rarely) be longer than length 1.charnames::viacodeTakes an integer and returns the official alias if there is one, and the official name if there is not. For example, this converts 0x61 into the string
"LATIN SMALL LETTER A". Will return custom names only if no official name exists, such as for private-use area codepoints.
These functions are not exported, so you must fully qualify them to use them. They also provide runtime access to any custom aliases you may have created. This shows how each works:
use v5.14; use warnings; use warnings FATAL => "utf8"; use open qw(:std :utf8); use charnames ":full", ":alias" => { ecute => "LATIN SMALL LETTER E WITH ACUTE", "APPLE LOGO" => 0xF8FF, # private use character }; printf "U+%04X is named '%s'.\n", 0xE9 => charnames::viacode(0xE9); printf "%s is code U+%04X.\n", ecute => charnames::vianame("ecute"); printf "%s is string ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access