blob: 5e49d29482e17f9643de9b6ee7c6f963c413912a [file] [log] [blame]
Rashed Abdel-Tawab4db47f42019-09-06 10:38:22 -07001package locale;
2
3our $VERSION = '1.09';
4use Config;
5
6$Carp::Internal{ (__PACKAGE__) } = 1;
7
8# A separate bit is used for each of the two forms of the pragma, to save
9# having to look at %^H for the normal case of a plain 'use locale' without an
10# argument.
11
12$locale::hint_bits = 0x4;
13$locale::partial_hint_bits = 0x10; # If pragma has an argument
14
15# The pseudo-category :characters consists of 2 real ones; but it also is
16# given its own number, -1, because in the complement form it also has the
17# side effect of "use feature 'unicode_strings'"
18
19sub import {
20 shift; # should be 'locale'; not checked
21
22 $^H{locale} = 0 unless defined $^H{locale};
23 if (! @_) { # If no parameter, use the plain form that changes all categories
24 $^H |= $locale::hint_bits;
25
26 }
27 else {
28 my @categories = ( qw(:ctype :collate :messages
29 :numeric :monetary :time) );
30 for (my $i = 0; $i < @_; $i++) {
31 my $arg = $_[$i];
32 $complement = $arg =~ s/ : ( ! | not_ ) /:/x;
33 if (! grep { $arg eq $_ } @categories, ":characters") {
34 require Carp;
35 Carp::croak("Unknown parameter '$_[$i]' to 'use locale'");
36 }
37
38 if ($complement) {
39 if ($i != 0 || $i < @_ - 1) {
40 require Carp;
41 Carp::croak("Only one argument to 'use locale' allowed"
42 . "if is $complement");
43 }
44
45 if ($arg eq ':characters') {
46 push @_, grep { $_ ne ':ctype' && $_ ne ':collate' }
47 @categories;
48 # We add 1 to the category number; This category number
49 # is -1
50 $^H{locale} |= (1 << 0);
51 }
52 else {
53 push @_, grep { $_ ne $arg } @categories;
54 }
55 next;
56 }
57 elsif ($arg eq ':characters') {
58 push @_, ':ctype', ':collate';
59 next;
60 }
61
62 $^H |= $locale::partial_hint_bits;
63
64 # This form of the pragma overrides the other
65 $^H &= ~$locale::hint_bits;
66
67 $arg =~ s/^://;
68
69 eval { require POSIX; import POSIX 'locale_h'; };
70
71 # Map our names to the ones defined by POSIX
72 my $LC = "LC_" . uc($arg);
73
74 my $bit = eval "&POSIX::$LC";
75 if (defined $bit) { # XXX Should we warn that this category isn't
76 # supported on this platform, or make it
77 # always be the C locale?
78
79 # Verify our assumption.
80 if (! ($bit >= 0 && $bit < 31)) {
81 require Carp;
82 Carp::croak("Cannot have ':$arg' parameter to 'use locale'"
83 . " on this platform. Use the 'perlbug' utility"
84 . " to report this problem, or send email to"
85 . " 'perlbug\@perl.org'. $LC=$bit");
86 }
87
88 # 1 is added so that the pseudo-category :characters, which is
89 # -1, comes out 0.
90 $^H{locale} |= 1 << ($bit + 1);
91 }
92 }
93 }
94
95}
96
97sub unimport {
98 $^H &= ~($locale::hint_bits|$locale::partial_hint_bits);
99 $^H{locale} = 0;
100}
101
1021;