You are viewing the version of this documentation from Perl 5.38.2. View the latest version

CONTENTS

NAME

Unicode::UCD - Unicode character database

SYNOPSIS

use Unicode::UCD 'charinfo';
my $charinfo   = charinfo($codepoint);

use Unicode::UCD 'charprop';
my $value  = charprop($codepoint, $property);

use Unicode::UCD 'charprops_all';
my $all_values_hash_ref = charprops_all($codepoint);

use Unicode::UCD 'casefold';
my $casefold = casefold($codepoint);

use Unicode::UCD 'all_casefolds';
my $all_casefolds_ref = all_casefolds();

use Unicode::UCD 'casespec';
my $casespec = casespec($codepoint);

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 'prop_aliases';
my @space_names = prop_aliases("space");

use Unicode::UCD 'prop_value_aliases';
my @gc_punct_names = prop_value_aliases("Gc", "Punct");

use Unicode::UCD 'prop_values';
my @all_EA_short_names = prop_values("East_Asian_Width");

use Unicode::UCD 'prop_invlist';
my @puncts = prop_invlist("gc=punctuation");

use Unicode::UCD 'prop_invmap';
my ($list_ref, $map_ref, $format, $missing)
                                  = prop_invmap("General Category");

use Unicode::UCD 'search_invlist';
my $index = search_invlist(\@invlist, $code_point);

# The following function should be used only internally in
# implementations of the Unicode Normalization Algorithm, and there
# are better choices than it.
use Unicode::UCD 'compexcl';
my $compexcl = compexcl($codepoint);

use Unicode::UCD 'namedseq';
my $namedseq = namedseq($named_sequence_name);

my $unicode_version = Unicode::UCD::UnicodeVersion();

my $convert_to_numeric =
          Unicode::UCD::num("\N{RUMI DIGIT ONE}\N{RUMI DIGIT TWO}");

DESCRIPTION

The Unicode::UCD module offers a series of functions that provide a simple interface to the Unicode Character Database.

code point argument

Some of the functions are called with a code point argument, which is either a decimal or a hexadecimal scalar designating a code point in the platform's native character set (extended to Unicode), or a string containing U+ followed by hexadecimals designating a Unicode code point. A leading 0 will force a hexadecimal interpretation, as will a hexadecimal digit that isn't a decimal digit.

Examples:

223     # Decimal 223 in native character set
0223    # Hexadecimal 223, native (= 547 decimal)
0xDF    # Hexadecimal DF, native (= 223 decimal)
'0xDF'  # String form of hexadecimal (= 223 decimal)
'U+DF'  # Hexadecimal DF, in Unicode's character set
                          (= LATIN SMALL LETTER SHARP S)

Note that the largest code point in Unicode is U+10FFFF.

charinfo()

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 category Cn meaning Unassigned) 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.

For results that are less "raw" than this function returns, or to get the values for any property, not just the few covered by this function, use the "charprop()" function.

The keys in the hash with the meanings of their values are:

code

the input native "code point argument" expressed in hexadecimal, with leading zeros added if necessary to make it contain at least four hexdigits

name

name of code, all IN UPPER CASE. Some control-type code points do not have names. This field will be empty for Surrogate and Private Use code points, and for the others without a name, it will contain a description enclosed in angle brackets, like <control>.

category

The short name of the general category of code. This will match one of the keys in the hash returned by "general_categories()".

The "prop_value_aliases()" function can be used to get all the synonyms of the category name.

combining

the combining class number for code used in the Canonical Ordering Algorithm. For Unicode 5.1, this is described in Section 3.11 Canonical Ordering Behavior available at http://www.unicode.org/versions/Unicode5.1.0/

The "prop_value_aliases()" function can be used to get all the synonyms of the combining class number.

bidi

bidirectional type of code. This will match one of the keys in the hash returned by "bidi_types()".

The "prop_value_aliases()" function can be used to get all the synonyms of the bidi type name.

decomposition

is empty if code has no decomposition; or is one or more codes (separated by spaces) that, taken in order, represent a decomposition for code. 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

This decomposition may be an intermediate one whose components are also decomposable. Use Unicode::Normalize to get the final decomposition in one step.

decimal

if code represents a decimal digit this is its integer numeric value

digit

if code represents some other digit-like number, this is its integer numeric value

numeric

if code represents a whole or rational number, this is its numeric value. Rational values are expressed as a string like 1/4.

mirrored

Y or N designating if code is mirrored in bidirectional text

unicode10

name of code in the Unicode 1.0 standard if one existed for this code point and is different from the current name

comment

As of Unicode 6.0, this is always empty.

upper

is, if non-empty, the uppercase mapping for code expressed as at least four hexdigits. This indicates that the full uppercase mapping is a single character, and is identical to the simple (single-character only) mapping. When this field is empty, it means that the simple uppercase mapping is code itself; you'll need some other means, (like "charprop()" or "casespec()" to get the full mapping.

lower

is, if non-empty, the lowercase mapping for code expressed as at least four hexdigits. This indicates that the full lowercase mapping is a single character, and is identical to the simple (single-character only) mapping. When this field is empty, it means that the simple lowercase mapping is code itself; you'll need some other means, (like "charprop()" or "casespec()" to get the full mapping.

title

is, if non-empty, the titlecase mapping for code expressed as at least four hexdigits. This indicates that the full titlecase mapping is a single character, and is identical to the simple (single-character only) mapping. When this field is empty, it means that the simple titlecase mapping is code itself; you'll need some other means, (like "charprop()" or "casespec()" to get the full mapping.

block

the block code belongs to (used in \p{Blk=...}). The "prop_value_aliases()" function can be used to get all the synonyms of the block name.

See "Blocks versus Scripts".

script

the script code belongs to. The "prop_value_aliases()" function can be used to get all the synonyms of the script name. Note that this is the older "Script" property value, and not the improved "Script_Extensions" value.

See "Blocks versus Scripts".

Note that you cannot do (de)composition and casing based solely on the decomposition, combining, lower, upper, and title fields; you will need also the "casespec()" function and the Composition_Exclusion property. (Or you could just use the lc(), uc(), and ucfirst() functions, and the Unicode::Normalize module.)

charprop()

  use Unicode::UCD 'charprop';

  print charprop(0x41, "Gc"), "\n";
  print charprop(0x61, "General_Category"), "\n";

prints
  Lu
  Ll

This returns the value of the Unicode property given by the second parameter for the "code point argument" given by the first.

The passed-in property may be specified as any of the synonyms returned by "prop_aliases()".

The return value is always a scalar, either a string or a number. For properties where there are synonyms for the values, the synonym returned by this function is the longest, most descriptive form, the one returned by "prop_value_aliases()" when called in a scalar context. Of course, you can call "prop_value_aliases()" on the result to get other synonyms.

The return values are more "cooked" than the "charinfo()" ones. For example, the "uc" property value is the actual string containing the full uppercase mapping of the input code point. You have to go to extra trouble with charinfo to get this value from its upper hash element when the full mapping differs from the simple one.

Special note should be made of the return values for a few properties:

Block

The value returned is the new-style (see "Old-style versus new-style block names").

Decomposition_Mapping

Like "charinfo()", the result may be an intermediate decomposition whose components are also decomposable. Use Unicode::Normalize to get the final decomposition in one step.

Unlike "charinfo()", this does not include the decomposition type. Use the Decomposition_Type property to get that.

Name_Alias

If the input code point's name has more than one synonym, they are returned joined into a single comma-separated string.

Numeric_Value

If the result is a fraction, it is converted into a floating point number to the accuracy of your platform.

Script_Extensions

If the result is multiple script names, they are returned joined into a single comma-separated string.

When called with a property that is a Perl extension that isn't expressible in a compound form, this function currently returns undef, as the only two possible values are true or false (1 or 0 I suppose). This behavior may change in the future, so don't write code that relies on it. Present_In is a Perl extension that is expressible in a bipartite or compound form (for example, \p{Present_In=4.0}), so charprop accepts it. But Any is a Perl extension that isn't expressible that way, so charprop returns undef for it. Also charprop returns undef for all Perl extensions that are internal-only.

charprops_all()

use Unicode::UCD 'charprops_all';

my $%properties_of_A_hash_ref = charprops_all("U+41");

This returns a reference to a hash whose keys are all the distinct Unicode (no Perl extension) properties, and whose values are the respective values for those properties for the input "code point argument".

Each key is the property name in its longest, most descriptive form. The values are what "charprop()" would return.

This function is expensive in time and memory.

charblock()

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 the block the code point belongs to, e.g. Basic Latin. The old-style block name is returned (see "Old-style versus new-style block names"). The "prop_value_aliases()" function can be used to get all the synonyms of the block name.

If the code point is unassigned, this returns the block it would belong to if it were assigned. (If the Unicode version being used is so early as to not have blocks, all code points are considered to be in No_Block.)

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 an old-style block name. On an ASCII platform, the return value is a range set with one range: an anonymous array with a single element that consists of another anonymous array whose first element is the first code point in the block, and whose second element is the final code point in the block. On an EBCDIC platform, the first two Unicode blocks are not contiguous. Their range sets are lists containing start-of-range, end-of-range code point pairs. You can test whether a code point is in a range set using the "charinrange()" function. (To be precise, each range set contains a third array element, after the range boundary ones: the old_style block name.)

If the argument to charblock() is not a known block, undef is returned.

charscript()

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 the script the code point belongs to, e.g., Latin, Greek, Han. If the code point is unassigned or the Unicode version being used is so early that it doesn't have scripts, this function returns "Unknown". The "prop_value_aliases()" function can be used to get all the synonyms of the script name.

Note that the Script_Extensions property is an improved version of the Script property, and you should probably be using that instead, with the "charprop()" function.

If supplied with an argument that can't be a code point, charscript() tries to do the opposite and interpret the argument as a script name. The return value is a range set: an anonymous array of arrays that contain start-of-range, end-of-range code point pairs. You can test whether a code point is in a range set using the "charinrange()" function. (To be precise, each range set contains a third array element, after the range boundary ones: the script name.)

If the charscript() argument is not a known script, undef is returned.

See also "Blocks versus Scripts".

charblocks()

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.

The names are in the old-style (see "Old-style versus new-style block names").

prop_invmap("block") can be used to get this same data in a different type of data structure.

prop_values("Block") can be used to get all the known new-style block names as a list, without the code point ranges.

See also "Blocks versus Scripts".

charscripts()

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.

prop_invmap("script") can be used to get this same data in a different type of data structure. Since the Script_Extensions property is an improved version of the Script property, you should instead use prop_invmap("scx").

prop_values("Script") can be used to get all the known script names as a list, without the code point ranges.

See also "Blocks versus Scripts".

charinrange()

In addition to using the \p{Blk=...} and \P{Blk=...} 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 Unicode::UCD qw(charscript charinrange);

$range = charscript('Hiragana');
print "looks like hiragana\n" if charinrange($range, $codepoint);

general_categories()

use Unicode::UCD 'general_categories';

my $categories = general_categories();

This returns a reference to a hash which has short general category names (such as Lu, Nd, Zs, S) as keys and long names (such as UppercaseLetter, 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 the category key.

The "prop_values()" and "prop_value_aliases()" functions can be used as an alternative to this function; the first returning a simple list of the short category names; and the second gets all the synonyms of a given category name.

bidi_types()

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 as L, R) as keys and long names (such as Left-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 the bidi 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)

The "prop_values()" and "prop_value_aliases()" functions can be used as an alternative to this function; the first returning a simple list of the short bidi type names; and the second gets all the synonyms of a given bidi type name.

compexcl()

WARNING: Unicode discourages the use of this function or any of the alternative mechanisms listed in this section (the documentation of compexcl()), except internally in implementations of the Unicode Normalization Algorithm. You should be using Unicode::Normalize directly instead of these. Using these will likely lead to half-baked results.

use Unicode::UCD 'compexcl';

my $compexcl = compexcl(0x09dc);

This routine returns undef if the Unicode version being used is so early that it doesn't have this property.

compexcl() is included for backwards compatibility, but as of Perl 5.12 and more modern Unicode versions, for most purposes it is probably more convenient to use one of the following instead:

my $compexcl = chr(0x09dc) =~ /\p{Comp_Ex};
my $compexcl = chr(0x09dc) =~ /\p{Full_Composition_Exclusion};

or even

my $compexcl = chr(0x09dc) =~ /\p{CE};
my $compexcl = chr(0x09dc) =~ /\p{Composition_Exclusion};

The first two forms return true if the "code point argument" should not be produced by composition normalization. For the final two forms to return true, it is additionally required that this fact not otherwise be determinable from the Unicode data base.

This routine behaves identically to the final two forms. That is, it does not return true 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, which is probably why you should use the Full_Composition_Exclusion property instead, as shown above.

The routine returns false otherwise.

casefold()

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". (Starting in Perl v5.16, the core function fc() returns the full mapping (described below) faster than this does, and for entire strings.)

If there is no case folding for the input 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:

code

the input native "code point argument" expressed in hexadecimal, with leading zeros added if necessary to make it contain at least four hexdigits

full

one or more codes (separated by spaces) that, taken in order, give the code points for the case folding for code. Each has at least four hexdigits.

simple

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. If full is just one code point, then simple equals full. If there is no single code point folding defined for code, then simple is the empty string. Otherwise, it is an inferior, but still better-than-nothing alternative folding to full.

mapping

is the same as simple if simple is not empty, and it is the same as full otherwise. It can be considered to be the simplest possible folding for code. It is defined primarily for backwards compatibility.

status

is C (for common) if the best possible fold is a single code point (simple equals full equals mapping). It is S if there are distinct folds, simple and full (mapping equals simple). And it is F if there is only a full fold (mapping equals full; simple is empty). Note that this describes the contents of mapping. It is defined primarily for backwards compatibility.

For Unicode versions between 3.1 and 3.1.1 inclusive, status can also be I which is the same as C but is a special case for dotted uppercase I and dotless lowercase i:

* If you use this I mapping

the result is case-insensitive, but dotless and dotted I's are not distinguished

* If you exclude this I mapping

the result is not fully case-insensitive, but dotless and dotted I's are distinguished

turkic

contains any special folding for Turkic languages. For versions of Unicode starting with 3.2, this field is empty unless code 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 for code in those languages. Each code has at least four hexdigits. Note that this folding does not maintain canonical equivalence without additional processing.

For Unicode versions between 3.1 and 3.1.1 inclusive, this field is empty unless there is a special folding for Turkic languages, in which case status is I, and mapping, full, simple, and turkic are all equal.

Programs that want complete generality and the best folding results should use the folding contained in the full 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 the simple field, with the loss of some generality. In Unicode 5.1, about 7% of the defined foldings have no single code point folding.

The mapping and status 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. The turkic field contains results to use when the locale is a Turkic language.

For more information about case mappings see http://www.unicode.org/reports/tr21

all_casefolds()

use Unicode::UCD 'all_casefolds';

my $all_folds_ref = all_casefolds();
foreach my $char_with_casefold (sort { $a <=> $b }
                                keys %$all_folds_ref)
{
    printf "%04X:", $char_with_casefold;
    my $casefold = $all_folds_ref->{$char_with_casefold};

    # Get folds for $char_with_casefold

    my @full_fold_hex = split / /, $casefold->{'full'};
    my $full_fold_string =
                join "", map {chr(hex($_))} @full_fold_hex;
    print " full=", join " ", @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;
    print "; turkic=", join " ", @turkic_fold_hex;
    if (defined $casefold && $casefold->{'simple'} ne "") {
        my $simple_fold_hex = $casefold->{'simple'};
        my $simple_fold_string = chr(hex($simple_fold_hex));
        print "; simple=$simple_fold_hex";
    }
    print "\n";
}

This returns all the case foldings in the current version of Unicode in the form of a reference to a hash. Each key to the hash is the decimal representation of a Unicode character that has a casefold to other than itself. The casefold of a semi-colon is itself, so it isn't in the hash; likewise for a lowercase "a", but there is an entry for a capital "A". The hash value for each key is another hash, identical to what is returned by "casefold()" if called with that code point as its argument. So the value all_casefolds()->{ord("A")}' is equivalent to casefold(ord("A"));