Unicode::UCD - Unicode character database
use Unicode::UCD 'charinfo';my $charinfo = charinfo($codepoint);use Unicode::UCD 'casefold';my $casefold = casefold(0xFB00);use Unicode::UCD 'casespec';my $casespec = casespec(0xFB00);use Unicode::UCD 'charblock';my $charblock = charblock($codepoint);use Unicode::UCD 'charscript';my $charscript = charscript($codepoint);use Unicode::UCD 'charblocks';my $charblocks = charblocks();use Unicode::UCD 'charscripts';my $charscripts = charscripts();use Unicode::UCD qw(charscript charinrange);my $range = charscript($script);print "looks like $script\n" if charinrange($range, $codepoint);use Unicode::UCD qw(general_categories bidi_types);my $categories = general_categories();my $types = bidi_types();use Unicode::UCD 'compexcl';my $compexcl = compexcl($codepoint);use Unicode::UCD 'namedseq';my $namedseq = namedseq($named_sequence_name);my $unicode_version = Unicode::UCD::UnicodeVersion();
The Unicode::UCD module offers a series of functions that provide a simple interface to the Unicode Character Database.
Some of the functions are called with acode point argument, which is either a decimal or a hexadecimal scalar designating a Unicode code point, orU+
followed by hexadecimals designating a Unicode code point. In other words, if you want a code point to be interpreted as a hexadecimal number, you must prefix it with either0x
orU+
, because a string like e.g.123
will be interpreted as a decimal code point. Also note that Unicode isnot limited to 16 bits (the number of Unicode code points is open-ended, in theory unlimited): you may have more than 4 hexdigits.
use Unicode::UCD 'charinfo';my $charinfo = charinfo(0x41);
This returns information about the input"code point argument" as a reference to a hash of fields as defined by the Unicode standard. If the"code point argument" is not assigned in the standard (i.e., has the general categoryCn
meaningUnassigned
) or is a non-character (meaning it is guaranteed to never be assigned in the standard),undef is returned.
Fields that aren't applicable to the particular code point argument exist in the returned hash, and are empty.
The keys in the hash with the meanings of their values are:
the input"code point argument" expressed in hexadecimal, with leading zeros added if necessary to make it contain at least four hexdigits
name ofcode, all IN UPPER CASE. Some control-type code points do not have names. This field will be empty forSurrogate
andPrivate Use
code points, and for the others without a name, it will contain a description enclosed in angle brackets, like<control>
.
The short name of the general category ofcode. This will match one of the keys in the hash returned by"general_categories()".
the combining class number forcode used in the Canonical Ordering Algorithm. For Unicode 5.1, this is described in Section 3.11Canonical Ordering Behavior
available athttp://www.unicode.org/versions/Unicode5.1.0/
bidirectional type ofcode. This will match one of the keys in the hash returned by"bidi_types()".
is empty ifcode has no decomposition; or is one or more codes (separated by spaces) that taken in order represent a decomposition forcode. Each has at least four hexdigits. The codes may be preceded by a word enclosed in angle brackets then a space, like<compat>
, giving the type of decomposition
ifcode is a decimal digit this is its integer numeric value
ifcode represents a whole number, this is its integer numeric value
ifcode represents a whole or rational number, this is its numeric value. Rational values are expressed as a string like1/4
.
Y
orN
designating ifcode is mirrored in bidirectional text
name ofcode in the Unicode 1.0 standard if one existed for this code point and is different from the current name
ISO 10646 comment field. It appears in parentheses in the ISO 10646 names list, or contains an asterisk to indicate there is a note for this code point in Annex P of that standard.
is empty if there is no single code point uppercase mapping forcode; otherwise it is that mapping expressed as at least four hexdigits. ("casespec()" should be used in addition tocharinfo() for case mappings when the calling program can cope with multiple code point mappings.)
is empty if there is no single code point lowercase mapping forcode; otherwise it is that mapping expressed as at least four hexdigits. ("casespec()" should be used in addition tocharinfo() for case mappings when the calling program can cope with multiple code point mappings.)
is empty if there is no single code point titlecase mapping forcode; otherwise it is that mapping expressed as at least four hexdigits. ("casespec()" should be used in addition tocharinfo() for case mappings when the calling program can cope with multiple code point mappings.)
blockcode belongs to (used in \p{In...}). See"Blocks versus Scripts".
scriptcode belongs to. See"Blocks versus Scripts".
Note that you cannot do (de)composition and casing based solely on thedecomposition,combining,lower,upper, andtitle fields; you will need also the"compexcl()", and"casespec()" functions.
use Unicode::UCD 'charblock';my $charblock = charblock(0x41);my $charblock = charblock(1234);my $charblock = charblock(0x263a);my $charblock = charblock("U+263a");my $range = charblock('Armenian');
With a"code point argument" charblock() returns theblock the code point belongs to, e.g.Basic Latin
. If the code point is unassigned, this returns the block it would belong to if it were assigned (which it may in future versions of the Unicode Standard).
See also"Blocks versus Scripts".
If supplied with an argument that can't be a code point, charblock() tries to do the opposite and interpret the argument as a code point block. The return value is arange: an anonymous list of lists that containstart-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 code point block,undef is returned.
use Unicode::UCD 'charscript';my $charscript = charscript(0x41);my $charscript = charscript(1234);my $charscript = charscript("U+263a");my $range = charscript('Thai');
With a"code point argument" charscript() returns thescript the code point belongs to, e.g.Latin
,Greek
,Han
. If the code point is unassigned, it returnsundef
If supplied with an argument that can't be a code point, charscript() tries to do the opposite and interpret the argument as a code point script. The return value is arange: an anonymous list of lists that containstart-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 code point script,undef is returned.
See also"Blocks versus Scripts".
use Unicode::UCD '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.
See also"Blocks versus Scripts".
use Unicode::UCD 'charscripts';my $charscripts = charscripts();
charscripts() returns a reference to a hash with the known script names as the keys, and the code point ranges (see"charscript()") as the values.
See also"Blocks versus Scripts".
In addition to using the\p{In...}
and\P{In...}
constructs, you can also test whether a code point is in therange as returned by"charblock()" and"charscript()" or as the values of the hash returned by"charblocks()" and"charscripts()" by using charinrange():
use Unicode::UCD qw(charscript charinrange);$range = charscript('Hiragana');print "looks like hiragana\n" if charinrange($range, $codepoint);
use Unicode::UCD 'general_categories';my $categories = general_categories();
This returns a reference to a hash which has short general category names (such asLu
,Nd
,Zs
,S
) as keys and long names (such asUppercaseLetter
,DecimalNumber
,SpaceSeparator
,Symbol
) as values. The hash is reversible in case you need to go from the long names to the short names. The general category is the one returned from"charinfo()" under thecategory
key.
use Unicode::UCD 'bidi_types';my $categories = bidi_types();
This returns a reference to a hash which has the short bidi (bidirectional) type names (such asL
,R
) as keys and long names (such asLeft-to-Right
,Right-to-Left
) as values. The hash is reversible in case you need to go from the long names to the short names. The bidi type is the one returned from"charinfo()" under thebidi
key. For the exact meaning of the various bidi classes the Unicode TR9 is recommended reading:http://www.unicode.org/reports/tr9/ (as of Unicode 5.0.0)
use Unicode::UCD 'compexcl';my $compexcl = compexcl(0x09dc);
This returnstrue if the"code point argument" should not be produced by composition normalization,AND if that fact is not otherwise determinable from the Unicode data base. It currently does not returntrue if the code point has a decomposition consisting of another single code point, nor if its decomposition starts with a code point whose combining class is non-zero. Code points that meet either of these conditions should also not be produced by composition normalization.
It returnsfalse otherwise.
use Unicode::UCD 'casefold';my $casefold = casefold(0xDF);if (defined $casefold) { my @full_fold_hex = split / /, $casefold->{'full'}; my $full_fold_string = join "", map {chr(hex($_))} @full_fold_hex; my @turkic_fold_hex = split / /, ($casefold->{'turkic'} ne "") ? $casefold->{'turkic'} : $casefold->{'full'}; my $turkic_fold_string = join "", map {chr(hex($_))} @turkic_fold_hex;}if (defined $casefold && $casefold->{'simple'} ne "") { my $simple_fold_hex = $casefold->{'simple'}; my $simple_fold_string = chr(hex($simple_fold_hex));}
This returns the (almost) locale-independent case folding of the character specified by the"code point argument".
If there is no case folding for that code point,undef is returned.
If there is a case folding for that code point, a reference to a hash with the following fields is returned:
the input"code point argument" expressed in hexadecimal, with leading zeros added if necessary to make it contain at least four hexdigits
one or more codes (separated by spaces) that taken in order give the code points for the case folding forcode. Each has at least four hexdigits.
is empty, or is exactly one code with at least four hexdigits which can be used as an alternative case folding when the calling program cannot cope with the fold being a sequence of multiple code points. Iffull is just one code point, thensimple equalsfull. If there is no single code point folding defined forcode, thensimple is the empty string. Otherwise, it is an inferior, but still better-than-nothing alternative folding tofull.
is the same assimple ifsimple is not empty, and it is the same asfull otherwise. It can be considered to be the simplest possible folding forcode. It is defined primarily for backwards compatibility.
isC
(forcommon
) if the best possible fold is a single code point (simple equalsfull equalsmapping). It isS
if there are distinct folds,simple andfull (mapping equalssimple). And it isF
if there only afull fold (mapping equalsfull;simple is empty). Note that this describes the contents ofmapping. It is defined primarily for backwards compatibility.
On versions 3.1 and earlier of Unicode,status can also beI
which is the same asC
but is a special case for dotted uppercase I and dotless lowercase i:
contains any special folding for Turkic languages. For versions of Unicode starting with 3.2, this field is empty unlesscode has a different folding in Turkic languages, in which case it is one or more codes (separated by spaces) that taken in order give the code points for the case folding forcode in those languages. Each code has at least four hexdigits. Note that this folding does not maintain canonical equivalence without additional processing.
For versions of Unicode 3.1 and earlier, this field is empty unless there is a special folding for Turkic languages, in which casestatus isI
, andmapping,full,simple, andturkic are all equal.
Programs that want complete generality and the best folding results should use the folding contained in thefull field. But note that the fold for some code points will be a sequence of multiple code points.
Programs that can't cope with the fold mapping being multiple code points can use the folding contained in thesimple field, with the loss of some generality. In Unicode 5.1, about 7% of the defined foldings have no single code point folding.
Themapping andstatus fields are provided for backwards compatibility for existing programs. They contain the same values as in previous versions of this function.
Locale is not completely independent. Theturkic field contains results to use when the locale is a Turkic language.
For more information about case mappings seehttp://www.unicode.org/unicode/reports/tr21
use Unicode::UCD 'casespec';my $casespec = casespec(0xFB00);
This returns the potentially locale-dependent case mappings of the"code point argument". The mappings may be longer than a single code point (which the basic Unicode case mappings as returned by"charinfo()" never are).
If there are no case mappings for the"code point argument", or if all three possible mappings (lower,title andupper) result in single code points and are locale independent and unconditional,undef is returned (which means that the case mappings, if any, for the code point are those returned by"charinfo()").
Otherwise, a reference to a hash giving the mappings (or a reference to a hash of such hashes, explained below) is returned with the following keys and their meanings:
The keys in the bottom layer hash with the meanings of their values are:
the input"code point argument" expressed in hexadecimal, with leading zeros added if necessary to make it contain at least four hexdigits
one or more codes (separated by spaces) that taken in order give the code points for the lower case ofcode. Each has at least four hexdigits.
one or more codes (separated by spaces) that taken in order give the code points for the title case ofcode. Each has at least four hexdigits.
one or more codes (separated by spaces) that taken in order give the code points for the upper case ofcode. Each has at least four hexdigits.
the conditions for the mappings to be valid. Ifundef, the mappings are always valid. When defined, this field is a list of conditions, all of which must be true for the mappings to be valid. The list consists of one or morelocales (see below) and/orcontexts (explained in the next paragraph), separated by spaces. (Other than as used to separate elements, spaces are to be ignored.) Case distinctions in the condition list are not significant. Conditions preceded by "NON_" represent the negation of the condition.
Acontext is one of those defined in the Unicode standard. For Unicode 5.1, they are defined in Section 3.13Default Case Operations
available athttp://www.unicode.org/versions/Unicode5.1.0/. These are for context-sensitive casing.
The hash described above is returned for locale-independent casing, where at least one of the mappings has length longer than one. Ifundef is returned, the code point may have mappings, but if so, all are length one, and are returned by"charinfo()". Note that when this function does return a value, it will be for the complete set of mappings for a code point, even those whose length is one.
If there are additional casing rules that apply only in certain locales, an additional key for each will be defined in the returned hash. Each such key will be its locale name, 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 lists of all possible locales, seeLocale::Country andLocale::Language. (In Unicode 5.1, the only locales returned by this function arelt
,tr
, andaz
.)
Each locale key is a reference to a hash that has the form above, and gives the casing rules for that particular locale, which take precedence over the locale-independent ones when in that locale.
If the only casing for a code point is locale-dependent, then the returned hash will not have any of the base keys, likecode
,upper
, etc., but will contain only locale keys.
For more information about case mappings seehttp://www.unicode.org/unicode/reports/tr21/
use Unicode::UCD 'namedseq';my $namedseq = namedseq("KATAKANA LETTER AINU P");my @namedseq = namedseq("KATAKANA LETTER AINU P");my %namedseq = namedseq();
If used with a single argument in a scalar context, returns the string consisting of the code points of the named sequence, orundef if no named sequence by that name exists. If used with a single argument in a list context, it returns the list of the ordinals of the code points. If used with no arguments in a list context, returns a hash with the names of the named sequences as the keys and the named sequences as strings as the values. Otherwise, it returnsundef or an empty list depending on the context.
This function only operates on officially approved (not provisional) named sequences.
This returns the version of the Unicode Character Database, in other words, the version of the Unicode standard the database implements. The version is a string of numbers delimited by dots ('.'
).
The difference between a block and a script is that scripts are closer to the linguistic notion of a set of code points required to present languages, while block is more of an artifact of the Unicode code point numbering and separation into blocks of (mostly) 256 code points.
For example the Latinscript is spread over severalblocks, such asBasic Latin
,Latin 1 Supplement
,Latin Extended-A
, andLatin Extended-B
. On the other hand, the Latin script does not contain all the characters of theBasic Latin
block (also known as ASCII): it includes only the letters, and not, for example, the digits or the punctuation.
For blocks seehttp://www.unicode.org/Public/UNIDATA/Blocks.txt
For scripts see UTR #24:http://www.unicode.org/unicode/reports/tr24/
Scripts are matched with the regular-expression construct\p{...}
(e.g.\p{Tibetan}
matches characters of the Tibetan script), while\p{In...}
is used for blocks (e.g.\p{InTibetan}
matches any of the 256 code points in the Tibetan block).
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. In other words, if you are wondering where one of your filehandles went, that's where.
Does not yet support EBCDIC platforms.
"compexcl()" should give a complete list of excluded code points.
Jarkko Hietaniemi
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.