6 $Carp::Internal{ (__PACKAGE__) } = 1;
10 locale - Perl pragma to use or avoid POSIX locales for built-in operations
14 @x = sort @y; # Native-platform/Unicode code point sort order
17 @x = sort @y; # Locale-defined sort order
19 @x = sort @y; # Native-platform/Unicode code point sort order
24 This pragma tells the compiler to enable (or disable) the use of POSIX
25 locales for built-in operations (for example, LC_CTYPE for regular
26 expressions, LC_COLLATE for string comparison, and LC_NUMERIC for number
27 formatting). Each "use locale" or "no locale"
28 affects statements to the end of the enclosing BLOCK.
30 See L<perllocale> for more detailed information on how Perl supports
33 On systems that don't have locales, this pragma will cause your operations
34 to behave as if in the "C" locale; attempts to change the locale will fail.
38 # A separate bit is used for each of the two forms of the pragma, to save
39 # having to look at %^H for the normal case of a plain 'use locale' without an
42 $locale::hint_bits = 0x4;
43 $locale::partial_hint_bits = 0x10; # If pragma has an argument
45 # The pseudo-category :characters consists of 2 real ones; but it also is
46 # given its own number, -1, because in the complement form it also has the
47 # side effect of "use feature 'unicode_strings'"
50 shift; # should be 'locale'; not checked
52 $^H{locale} = 0 unless defined $^H{locale};
53 if (! @_) { # If no parameter, use the plain form that changes all categories
54 $^H |= $locale::hint_bits;
58 my @categories = ( qw(:ctype :collate :messages
59 :numeric :monetary :time) );
60 for (my $i = 0; $i < @_; $i++) {
62 $complement = $arg =~ s/ : ( ! | not_ ) /:/x;
63 if (! grep { $arg eq $_ } @categories, ":characters") {
65 Carp::croak("Unknown parameter '$_[$i]' to 'use locale'");
69 if ($i != 0 || $i < @_ - 1) {
71 Carp::croak("Only one argument to 'use locale' allowed"
72 . "if is $complement");
75 if ($arg eq ':characters') {
76 push @_, grep { $_ ne ':ctype' && $_ ne ':collate' }
78 # We add 1 to the category number; This category number
80 $^H{locale} |= (1 << 0);
83 push @_, grep { $_ ne $arg } @categories;
87 elsif ($arg eq ':characters') {
88 push @_, ':ctype', ':collate';
92 $^H |= $locale::partial_hint_bits;
94 # This form of the pragma overrides the other
95 $^H &= ~$locale::hint_bits;
99 eval { require POSIX; import POSIX 'locale_h'; };
100 unless (defined &POSIX::LC_CTYPE) {
104 # Map our names to the ones defined by POSIX
105 $arg = "LC_" . uc($arg);
107 my $bit = eval "&POSIX::$arg";
109 # 1 is added so that the pseudo-category :characters, which is
111 $^H{locale} |= 1 << ($bit + 1);
119 $^H &= ~($locale::hint_bits|$locale::partial_hint_bits);