UnicodeCD - Unicode character database
use UnicodeCD 'charinfo'; my $charinfo = charinfo($codepoint); use UnicodeCD 'charblock'; my $charblock = charblock($codepoint); use UnicodeCD 'charscript'; my $charscript = charblock($codepoint);
The Unicode module offers a simple interface to the Unicode Character Database.
use UnicodeCD 'charinfo'; my $charinfo = charinfo(0x41);
charinfo() returns a reference to a hash that has the following fields as defined by the Unicode standard:
key code code point with at least four hexdigits name name of the character IN UPPER CASE category general category of the character combining classes used in the Canonical Ordering Algorithm bidi bidirectional category decomposition character decomposition mapping decimal if decimal digit this is the integer numeric value digit if digit this is the numeric value numeric if numeric is the integer or rational numeric value mirrored if mirrored in bidirectional text unicode10 Unicode 1.0 name if existed and different comment ISO 10646 comment field upper uppercase equivalent mapping lower lowercase equivalent mapping title titlecase equivalent mapping block block the character belongs to (used in \p{In...}) script script the character belongs to
If no match is found, a reference to an empty hash is returned.
The block property is the same as as returned by charinfo(). It is not defined in the Unicode Character Database proper (Chapter 4 of the Unicode 3.0 Standard) but instead in an auxiliary database (Chapter 14 of TUS3). Similarly for the script property.
block
script
Note that you cannot do (de)composition and casing based solely on the above decomposition and lower, upper, title, properties, you will need also the compexcl(), casefold(), and casespec() functions.
decomposition
lower
upper
title
use UnicodeCD 'charblock'; my $charblock = charblock(0x41); my $charblock = charblock(1234); my $charblock = charblock("0x263a"); my $charblock = charblock("U+263a"); my $ranges = charblock('Armenian');
With a code point argument charblock() returns the block the character belongs to, e.g. Basic Latin. Note that not all the character positions within all blocks are defined.
Basic Latin
If supplied with an argument that can't be a code point, charblock() tries to do the opposite and interpret the argument as a character block. The return value is a range: an anonymous list that contains anonymous lists, which in turn contain start-of-range, end-of-range code point pairs. You can test whether a code point is in a range using the "charinrange" function. If the argument is not a known charater block, undef is returned.
undef
use UnicodeCD 'charscript'; my $charscript = charscript(0x41); my $charscript = charscript(1234); my $charscript = charscript("U+263a"); my $ranges = charscript('Thai');
With a code point argument charscript() returns the script the character belongs to, e.g. Latin, Greek, Han.
Latin
Greek
Han
If supplied with an argument that can't be a code point, charscript() tries to do the opposite and interpret the argument as a character script. The return value is a range: an anonymous list that contains anonymous lists, which in turn contain start-of-range, end-of-range code point pairs. You can test whether a code point is in a range using the "charinrange" function. If the argument is not a known charater script, undef is returned.
use UnicodeCD 'charblocks'; my $charblocks = charblocks();
charblocks() returns a reference to a hash with the known block names as the keys, and the code point ranges (see "charblock") as the values.
use UnicodeCD 'charscripts'; my %charscripts = charscripts();
charscripts() returns a hash with the known script names as the keys, and the code point ranges (see "charscript") as the values.
The difference between a block and a script is that scripts are closer to the linguistic notion of a set of characters required to present languages, while block is more of an artifact of the Unicode character numbering and separation into blocks of 256 characters.
For example the Latin script is spread over several blocks, such as Basic Latin, Latin 1 Supplement, Latin Extended-A, and Latin Extended-B. On the other hand, the Latin script does not contain all the characters of the Basic Latin block (also known as the ASCII): it includes only the letters, not for example the digits or the punctuation.
Latin 1 Supplement
Latin Extended-A
Latin Extended-B
For blocks see http://www.unicode.org/Public/UNIDATA/Blocks.txt
For scripts see UTR #24: http://www.unicode.org/unicode/reports/tr24/
Both scripts and blocks can be matched using the regular expression construct \p{In...} and its negation \P{In...}.
\p{In...}
\P{In...}
The name of the script or the block comes after the In, for example \p{InCyrillic}, \P{InBasicLatin}. Spaces and dashes ('-') are removed from the names for the \p{In...}, for example LatinExtendedA instead of Latin Extended-A.
In
\p{InCyrillic}
\P{InBasicLatin}
LatinExtendedA
There are a few cases where there exists both a script and a block by the same name, in these cases the block version has Block appended: \p{InKatakana} is the script, \p{InKatakanaBlock} is the block.
Block
\p{InKatakana}
\p{InKatakanaBlock}
A <code point argument> is either a decimal or a hexadecimal scalar, or "U+" followed by hexadecimals.
In addition to using the \p{In...} and \P{In...} constructs, you can also test whether a code point is in the range as returned by "charblock" and "charscript" or as the values of the hash returned by "charblocks" and </charscripts> by using charinrange():
use UnicodeCD qw(charscript charinrange); $range = charscript('Hiragana'); print "looks like hiragana\n" if charinrange($range, $code);
use UnicodeCD 'compexcl'; my $compexcl = compexcl("09dc");
The compexcl() returns the composition exclusion (that is, if the character cannot be decomposed) of the character specified by a code point argument.
If there is a composition exclusion for the character, true is returned. Otherwise, false is returned.
use UnicodeCD 'casefold'; my %casefold = casefold("09dc");
The casefold() returns the locale-independent case folding of the character specified by a code point argument.
If there is a case folding for that character, a reference to a hash with the following fields is returned:
key code code point with at least four hexdigits status "C", "F", "S", or "I" mapping one or more codes separated by spaces
The meaning of the status is as follows:
C common case folding, common mappings shared by both simple and full mappings F full case folding, mappings that cause strings to grow in length. Multiple characters are separated by spaces S simple case folding, mappings to single characters where different from F I special case for dotted uppercase I and dotless lowercase i - If this mapping is included, the result is case-insensitive, but dotless and dotted I's are not distinguished - If this mapping is excluded, the result is not fully case-insensitive, but dotless and dotted I's are distinguished
If there is no case folding for that character, undef is returned.
For more information about case mappings see http://www.unicode.org/unicode/reports/tr21/
use UnicodeCD 'casespec'; my %casespec = casespec("09dc");
The casespec() returns the potentially locale-dependent case mapping of the character specified by a code point argument. The mapping may change the length of the string (which the basic Unicode case mappings as returned by charinfo() never do).
key code code point with at least four hexdigits lower lowercase title titlecase upper uppercase condition condition list (may be undef)
The condition is optional. Where present, it consists of one or more locales or contexts, separated by spaces (other than as used to separate elements, spaces are to be ignored). A condition list overrides the normal behavior if all of the listed conditions are true. Case distinctions in the condition list are not significant. Conditions preceded by "NON_" represent the negation of the condition
condition
A locale is defined as a 2-letter ISO 3166 country code, possibly followed by a "_" and a 2-letter ISO language code (, possibly followed by a "_" and a variant code). You can find the list of those codes in Locale::Country and Locale::Language.
A context is one of the following choices:
FINAL The letter is not followed by a letter of general category L (e.g. Ll, Lt, Lu, Lm, or Lo) MODERN The mapping is only used for modern text AFTER_i The last base character was "i" 0069
UnicodeCD::UnicodeVersion() returns the version of the Unicode Character Database, in other words, the version of the Unicode standard the database implements.
The first use of charinfo() opens a read-only filehandle to the Unicode Character Database (the database is included in the Perl distribution). The filehandle is then kept open for further queries.
Jarkko Hietaniemi
To install Env, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Env
CPAN shell
perl -MCPAN -e shell install Env
For more information on module installation, please visit the detailed CPAN module installation guide.