Start anew
[msysgit.git] / lib / perl5 / 5.6.1 / I18N / Collate.pm
blob64a03a284b6715752c12475141ca75d1e0a40e62
1 package I18N::Collate;
3 =head1 NAME
5 I18N::Collate - compare 8-bit scalar data according to the current locale
7 ***
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
13 HAS BEEN DEPRECATED
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
18 release 5.003_06.
20 See the perllocale manual page for further information.
22 ***
24 =head1 SYNOPSIS
26 use I18N::Collate;
27 setlocale(LC_COLLATE, 'locale-of-your-choice');
28 $s1 = new I18N::Collate "scalar_data_1";
29 $s2 = new I18N::Collate "scalar_data_2";
31 =head1 DESCRIPTION
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
39 $s1 le $s2
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.
58 =cut
60 # I18N::Collate.pm
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)
75 # collate_xfrm 2)
77 # Overloads: cmp # 3)
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
87 # Notes:
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.
106 # Updated: 19961005
108 # ---
110 use POSIX qw(strxfrm LC_COLLATE);
111 use warnings::register;
113 require Exporter;
115 @ISA = qw(Exporter);
116 @EXPORT = qw(collate_xfrm setlocale LC_COLLATE);
117 @EXPORT_OK = qw();
119 use overload qw(
120 fallback 1
121 cmp collate_cmp
124 sub new {
125 my $new = $_[1];
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
136 HAS BEEN DEPRECATED
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
141 release 5.003_06.
143 See the perllocale manual page for further information.
146 ___EOD___
147 $please_use_I18N_Collate_even_if_deprecated++;
151 bless \$new;
154 sub setlocale {
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'} || '';
162 sub C {
163 my $s = ${$_[0]};
165 $C->{$LOCALE}->{$s} = collate_xfrm($s)
166 unless (defined $C->{$LOCALE}->{$s}); # cache when met
168 $C->{$LOCALE}->{$s};
171 sub collate_xfrm {
172 my $s = $_[0];
173 my $x = '';
175 for (split(/(\000+)/, $s)) {
176 $x .= (/^\000/) ? $_ : strxfrm("$_\000");
182 sub collate_cmp {
183 &C($_[0]) cmp &C($_[1]);
186 # init $LOCALE
188 &I18N::Collate::setlocale();
190 1; # keep require happy