2 # NOTE! This module is deprecated (obsolete) after the Perl release
3 # 5.003_06 as the functionality has been integrated into the Perl core.
10 I18N::Collate - compare 8-bit scalar data according to the current locale
15 setlocale(LC_COLLATE, 'locale-of-your-choice');
16 $s1 = new I18N::Collate "scalar_data_1";
17 $s2 = new I18N::Collate "scalar_data_2";
21 This module provides you with objects that will collate
22 according to your national character set, provided that the
23 POSIX setlocale() function is supported on your system.
25 You can compare $s1 and $s2 above with
29 to extract the data itself, you'll need a dereference: $$s1
31 This uses POSIX::setlocale(). The basic collation conversion is done by
32 strxfrm() which terminates at NUL characters being a decent C routine.
33 collate_xfrm() handles embedded NUL characters gracefully. Due to C<cmp>
34 and overload magic, C<lt>, C<le>, C<eq>, C<ge>, and C<gt> work also. The
35 available locales depend on your operating system; try whether C<locale
36 -a> shows them or man pages for "locale" or "nlsinfo" or
37 the direct approach C<ls /usr/lib/nls/loc> or C<ls
38 /usr/lib/nls>. Not all the locales that your vendor supports
39 are necessarily installed: please consult your operating system's
40 documentation and possibly your local system administration.
42 The locale names are probably something like
43 C<"xx_XX.(ISO)?8859-N"> or C<"xx_XX.(ISO)?8859N">, for example
44 C<"fr_CH.ISO8859-1"> is the Swiss (CH) variant of French (fr),
45 ISO Latin (8859) 1 (-1) which is the Western European character set.
51 # Author: Jarkko Hietaniemi <Jarkko.Hietaniemi@hut.fi>
52 # Helsinki University of Technology, Finland
54 # Acks: Guy Decoux <decoux@moulon.inra.fr> understood
55 # overloading magic much deeper than I and told
56 # how to cut the size of this code by more than half.
57 # (my first version did overload all of lt gt eq le ge cmp)
59 # Purpose: compare 8-bit scalar data according to the current locale
61 # Requirements: Perl5 POSIX::setlocale() and POSIX::strxfrm()
63 # Exports: setlocale 1)
68 # Usage: use I18N::Collate;
69 # setlocale(LC_COLLATE, 'locale-of-your-choice'); # 4)
70 # $s1 = new I18N::Collate "scalar_data_1";
71 # $s2 = new I18N::Collate "scalar_data_2";
73 # now you can compare $s1 and $s2: $s1 le $s2
74 # to extract the data itself, you need to deref: $$s1
77 # 1) this uses POSIX::setlocale
78 # 2) the basic collation conversion is done by strxfrm() which
79 # terminates at NUL characters being a decent C routine.
80 # collate_xfrm handles embedded NUL characters gracefully.
81 # 3) due to cmp and overload magic, lt le eq ge gt work also
82 # 4) the available locales depend on your operating system;
83 # try whether "locale -a" shows them or man pages for
84 # "locale" or "nlsinfo" work or the more direct
85 # approach "ls /usr/lib/nls/loc" or "ls /usr/lib/nls".
86 # Not all the locales that your vendor supports
87 # are necessarily installed: please consult your
88 # operating system's documentation.
89 # The locale names are probably something like
90 # 'xx_XX.(ISO)?8859-N' or 'xx_XX.(ISO)?8859N',
91 # for example 'fr_CH.ISO8859-1' is the Swiss (CH)
92 # variant of French (fr), ISO Latin (8859) 1 (-1)
93 # which is the Western European character set.
99 use POSIX qw(strxfrm LC_COLLATE);
104 @EXPORT = qw(collate_xfrm setlocale LC_COLLATE);
115 if ($^W && $] >= 5.003_06) {
116 unless ($please_use_I18N_Collate_even_if_deprecated) {
120 WARNING: starting from the Perl version 5.003_06 the I18N::Collate
121 interface for comparing 8-bit scalar data according to the current locale
125 (that is, please do not use it anymore for any new applications and please
126 migrate the old applications away from it) because its functionality
127 was integrated into the Perl core language in the release 5.003_06.
129 All scalar data is now collated according to the current locale setting.
130 Also, Perl does automatically the setlocale(LC_COLLATE, "") for you.
132 To convert: forget I18N::Collate completely and use scalar data in
133 a completely normal way.
137 $please_use_I18N_Collate_even_if_deprecated++;
145 my ($category, $locale) = @_[0,1];
147 POSIX::setlocale($category, $locale) if (defined $category);
148 # the current $LOCALE
149 $LOCALE = $locale || $ENV{'LC_COLLATE'} || $ENV{'LC_ALL'} || '';
155 $C->{$LOCALE}->{$s} = collate_xfrm($s)
156 unless (defined $C->{$LOCALE}->{$s}); # cache when met
165 for (split(/(\000+)/, $s)) {
166 $x .= (/^\000/) ? $_ : strxfrm("$_\000");
173 &C($_[0]) cmp &C($_[1]);
178 &I18N::Collate::setlocale();
180 1; # keep require happy