-sub _trylocale ($$$$) { # Adds the locale given by the first parameter to the
- # list given by the 3rd iff the platform supports the
- # locale in each of the categories given by the 2nd
- # parameter, which is either a single category or a
- # reference to a list of categories
- # The 4th parameter is true if to reject locales that
- # aren't apparently fully compatible with Perl.
+# Functions whose names begin with underscore are internal helper functions
+# for this file, and are not to be used by outside callers.
+
+use strict;
+
+eval { require POSIX; import POSIX 'locale_h'; };
+my $has_locale_h = ! $@;
+
+# LC_ALL can be -1 on some platforms. And, in fact the implementors could
+# legally use any integer to represent any category. But it makes the most
+# sense for them to have used small integers. Below, we create new locale
+# numbers for ones missing from this machine. We make them very negative,
+# hopefully more negative than anything likely to be a valid category on the
+# platform, but also below is a check to be sure that our guess is valid.
+my $max_bad_category_number = -1000000;
+
+# Initialize this hash so that it looks like e.g.,
+# 6 => 'CTYPE',
+# where 6 is the value of &POSIX::LC_CTYPE
+my %category_name;
+my %category_number;
+unless ($@) {
+ my $number_for_missing_category = $max_bad_category_number;
+ foreach my $name (qw(ALL COLLATE CTYPE MESSAGES MONETARY NUMERIC TIME)) {
+ my $number = eval "&POSIX::LC_$name";
+
+ if ($@) {
+ # Use a negative number (smaller than any legitimate category
+ # number) if the platform doesn't support this category, so we
+ # have an entry for all the ones that might be specified in calls
+ # to us.
+ $number = $number_for_missing_category-- if $@;
+ }
+ elsif ( $number !~ / ^ -? \d+ $ /x
+ || $number <= $max_bad_category_number)
+ {
+ # We think this should be an int. And it has to be larger than
+ # any of our synthetic numbers.
+ die "Unexpected locale category number '$number' for LC_$name"
+ }
+
+ $category_name{$number} = "$name";
+ $category_number{$name} = $number;
+ }
+}
+
+sub _trylocale ($$$$) { # For use only by other functions in this file!
+
+ # Adds the locale given by the first parameter to the list given by the
+ # 3rd iff the platform supports the locale in each of the category numbers
+ # given by the 2nd parameter, which is either a single category or a
+ # reference to a list of categories. The list MUST be sorted so that
+ # CTYPE is first, COLLATE is last unless ALL is present, in which case
+ # that comes after COLLATE. This is because locale.c detects bad locales
+ # only with CTYPE, and COLLATE on some platforms can core dump if it is a
+ # bad locale.
+ #
+ # The 4th parameter is true if to accept locales that aren't apparently
+ # fully compatible with Perl.
+