5 I18N::Collate - compare 8-bit scalar data according to the current locale
9 WARNING: starting from the Perl version 5.003_06
10 the I18N::Collate interface for comparing 8-bit scalar data
11 according to the current locale
15 That is, please do not use it anymore for any new applications
16 and please migrate the old applications away from it because its
17 functionality was integrated into the Perl core language in the
20 See the perllocale manual page for further information.
27 setlocale(LC_COLLATE, 'locale-of-your-choice');
28 $s1 = new I18N::Collate "scalar_data_1";
29 $s2 = new I18N::Collate "scalar_data_2";
33 This module provides you with objects that will collate
34 according to your national character set, provided that the
35 POSIX setlocale() function is supported on your system.
37 You can compare $s1 and $s2 above with
41 to extract the data itself, you'll need a dereference: $$s1
43 This module uses POSIX::setlocale(). The basic collation conversion is
44 done by strxfrm() which terminates at NUL characters being a decent C
45 routine. collate_xfrm() handles embedded NUL characters gracefully.
47 The available locales depend on your operating system; try whether
48 C<locale -a> shows them or man pages for "locale" or "nlsinfo" or the
49 direct approach C<ls /usr/lib/nls/loc> or C<ls /usr/lib/nls> or
50 C<ls /usr/lib/locale>. Not all the locales that your vendor supports
51 are necessarily installed: please consult your operating system's
52 documentation and possibly your local system administration. The
53 locale names are probably something like C<xx_XX.(ISO)?8859-N> or
54 C<xx_XX.(ISO)?8859N>, for example C<fr_CH.ISO8859-1> is the Swiss (CH)
55 variant of French (fr), ISO Latin (8859) 1 (-1) which is the Western
56 European character set.
62 # Author: Jarkko Hietaniemi <F<jhi@iki.fi>>
63 # Helsinki University of Technology, Finland
65 # Acks: Guy Decoux <F<decoux@moulon.inra.fr>> understood
66 # overloading magic much deeper than I and told
67 # how to cut the size of this code by more than half.
68 # (my first version did overload all of lt gt eq le ge cmp)
70 # Purpose: compare 8-bit scalar data according to the current locale
72 # Requirements: Perl5 POSIX::setlocale() and POSIX::strxfrm()
74 # Exports: setlocale 1)
79 # Usage: use I18N::Collate;
80 # setlocale(LC_COLLATE, 'locale-of-your-choice'); # 4)
81 # $s1 = new I18N::Collate "scalar_data_1";
82 # $s2 = new I18N::Collate "scalar_data_2";
84 # now you can compare $s1 and $s2: $s1 le $s2
85 # to extract the data itself, you need to deref: $$s1
88 # 1) this uses POSIX::setlocale
89 # 2) the basic collation conversion is done by strxfrm() which
90 # terminates at NUL characters being a decent C routine.
91 # collate_xfrm handles embedded NUL characters gracefully.
92 # 3) due to cmp and overload magic, lt le eq ge gt work also
93 # 4) the available locales depend on your operating system;
94 # try whether "locale -a" shows them or man pages for
95 # "locale" or "nlsinfo" work or the more direct
96 # approach "ls /usr/lib/nls/loc" or "ls /usr/lib/nls".
97 # Not all the locales that your vendor supports
98 # are necessarily installed: please consult your
99 # operating system's documentation.
100 # The locale names are probably something like
101 # 'xx_XX.(ISO)?8859-N' or 'xx_XX.(ISO)?8859N',
102 # for example 'fr_CH.ISO8859-1' is the Swiss (CH)
103 # variant of French (fr), ISO Latin (8859) 1 (-1)
104 # which is the Western European character set.
110 use POSIX
qw(strxfrm LC_COLLATE);
111 use warnings
::register
;
116 @EXPORT = qw(collate_xfrm setlocale LC_COLLATE);
127 if (warnings
::enabled
() && $] >= 5.003_06
) {
128 unless ($please_use_I18N_Collate_even_if_deprecated) {
129 warnings
::warn <<___EOD___
;
132 WARNING
: starting from the Perl version
5.003_06
133 the I18N
::Collate interface
for comparing
8-bit
scalar data
134 according to the current locale
138 That is
, please
do not use it anymore
for any new applications
139 and please migrate the old applications away from it because its
140 functionality was integrated into the Perl core language
in the
143 See the perllocale manual page
for further information
.
147 $please_use_I18N_Collate_even_if_deprecated++;
155 my ($category, $locale) = @_[0,1];
157 POSIX
::setlocale
($category, $locale) if (defined $category);
158 # the current $LOCALE
159 $LOCALE = $locale || $ENV{'LC_COLLATE'} || $ENV{'LC_ALL'} || '';
165 $C->{$LOCALE}->{$s} = collate_xfrm
($s)
166 unless (defined $C->{$LOCALE}->{$s}); # cache when met
175 for (split(/(\000+)/, $s)) {
176 $x .= (/^\000/) ?
$_ : strxfrm
("$_\000");
183 &C
($_[0]) cmp &C
($_[1]);
188 &I18N
::Collate
::setlocale
();
190 1; # keep require happy