Programming Perl

Programming PerlSearch this book
Previous: 7.2.36 Getopt::Std - Process Single-Character Options with Option ClusteringChapter 7
The Standard Perl Library
Next: 7.2.38 integer - Do Arithmetic in Integer Instead of Double
 

7.2.37 I18N::Collate - Compare 8-bit Scalar Data According to the Current Locale

use I18N::Collate;

setlocale(LC_COLLATE, $locale);         # uses POSIX::setlocale
$s1 = new I18N::Collate "scalar_data_1";
$s2 = new I18N::Collate "scalar_data_2";

This module provides you with objects that can be collated (ordered) according to your national character set, provided that Perl's POSIX module and the POSIX setlocale(3) and strxfrm(3) functions are available on your system. $locale in the setlocale() invocation shown above must be an argument acceptable to setlocale(3) on your system. See the setlocale(3) manpage for further information. Available locales depend upon your operating system.

Here is an example of collation within the standard `C' locale:

use I18N::Collate;

setlocale(LC_COLLATE, 'C');
$s1 = new I18N::Collate "Hello";
$s2 = new I18N::Collate "Goodbye";
# following line prints "Hello comes before Goodbye"
print "$$s1 comes before $$s2" if $s2 le $s1;

The objects returned by the new() method are references. You can get at their values by dereferencing them - for example, $$s1 and $$s2. However, Perl's built-in comparison operators are overloaded by I18N::Collate, so that they operate on the objects returned by new() without the necessity of dereference. The print line above dereferences $s1 and $s2 to access their values directly, but does not dereference the variables passed to the le operator. The comparison operators you can use in this way are the following:

<   <=  >   >=  ==  !=  <=>
lt  le  gt  ge  eq  ne  cmp

I18N::Collate uses POSIX::setlocale() and POSIX::strxfrm() to perform the collation. Unlike strxfrm(), however, I18N::Collate handles embedded NULL characters gracefully.

To determine which locales are available with your operating system, check whether the command:

locale -a

lists them. You can also check the locale(5) or nlsinfo manpages, or look at the filenames within one of these directories (or their subdirectories): /usr/lib/nls, /usr/share/lib/locale, or /etc/locale. Not all locales your vendor supports are necessarily installed. Please consult your operating system's documentation and possibly your local system administrator.


Previous: 7.2.36 Getopt::Std - Process Single-Character Options with Option ClusteringProgramming PerlNext: 7.2.38 integer - Do Arithmetic in Integer Instead of Double
7.2.36 Getopt::Std - Process Single-Character Options with Option ClusteringBook Index7.2.38 integer - Do Arithmetic in Integer Instead of Double