This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mktables: Add comment
[perl5.git] / lib / locale.pm
1 package locale;
2
3 our $VERSION = '1.03';
4 use Config;
5
6 $Carp::Internal{ (__PACKAGE__) } = 1;
7
8 =head1 NAME
9
10 locale - Perl pragma to use or avoid POSIX locales for built-in operations
11
12 =head1 SYNOPSIS
13
14     @x = sort @y;      # Native-platform/Unicode code point sort order
15     {
16         use locale;
17         @x = sort @y;  # Locale-defined sort order
18     }
19     @x = sort @y;      # Native-platform/Unicode code point sort order
20                        # again
21
22 =head1 DESCRIPTION
23
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.
29
30 See L<perllocale> for more detailed information on how Perl supports
31 locales.
32
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.
35
36 =cut
37
38 # A separate bit is used for each of the two forms of the pragma, as they are
39 # mostly independent, and interact with each other and the unicode_strings
40 # feature.  This allows for fast determination of which one(s) of the three
41 # are to be used at any given point, and no code has to be written to deal
42 # with coming in and out of scopes--it falls automatically out from the hint
43 # handling
44
45 $locale::hint_bits = 0x4;
46 $locale::not_chars_hint_bits = 0x10;
47
48 sub import {
49     shift;  # should be 'locale'; not checked
50
51     my $found_not_chars = 0;
52     while (defined (my $arg = shift)) {
53         if ($arg eq ":not_characters") {
54             $^H |= $locale::not_chars_hint_bits;
55
56             # This form of the pragma overrides the other
57             $^H &= ~$locale::hint_bits;
58             $found_not_chars = 1;
59         }
60         else {
61             require Carp;
62             Carp::croak("Unknown parameter '$arg' to 'use locale'");
63         }
64     }
65
66     # Use the plain form if not doing the :not_characters one.
67     $^H |= $locale::hint_bits unless $found_not_chars;
68 }
69
70 sub unimport {
71     $^H &= ~($locale::hint_bits|$locale::not_chars_hint_bits);
72 }
73
74 1;