This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/loc_tools.pl: Add fcn to return valid locale cats
authorKarl Williamson <khw@cpan.org>
Sun, 4 Mar 2018 17:45:05 +0000 (10:45 -0700)
committerKarl Williamson <khw@cpan.org>
Sun, 4 Mar 2018 18:53:10 +0000 (11:53 -0700)
This new function will return the locale categories known to this
platform.

t/loc_tools.pl

index db0bfac..8623e12 100644 (file)
@@ -15,6 +15,11 @@ use strict;
 eval { require POSIX; import POSIX 'locale_h'; };
 my $has_locale_h = ! $@;
 
+my @known_categories = ( qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY
+                            LC_NUMERIC LC_TIME LC_ADDRESS LC_IDENTIFICATION
+                            LC_MEASUREMENT LC_PAPER LC_TELEPHONE));
+my @platform_categories;
+
 # 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
@@ -30,9 +35,8 @@ my %category_name;
 my %category_number;
 if ($has_locale_h) {
     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";
-
+    foreach my $name (@known_categories) {
+        my $number = eval "&POSIX::$name";
         if ($@) {
             # Use a negative number (smaller than any legitimate category
             # number) if the platform doesn't support this category, so we
@@ -45,9 +49,13 @@ if ($has_locale_h) {
         {
             # 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"
+            die "Unexpected locale category number '$number' for $name"
+        }
+        else {
+            push @platform_categories, $name;
         }
 
+        $name =~ s/LC_//;
         $category_name{$number} = "$name";
         $category_number{$name} = $number;
     }
@@ -160,6 +168,13 @@ sub _decode_encodings { # For use only by other functions in this file!
     return @enc;
 }
 
+sub valid_locale_categories() {
+    # Returns a list of the locale categories (expressed as strings, like
+    # "LC_ALL) known to this program that are available on this platform.
+
+    return @platform_categories;
+}
+
 sub locales_enabled(;$) {
     # Returns 0 if no locale handling is available on this platform; otherwise
     # 1.
@@ -170,7 +185,7 @@ sub locales_enabled(;$) {
     # taken to be the C enum for the category (e.g., &POSIX::LC_CTYPE).
     # Otherwise it should be a string name of the category, like 'LC_TIME'.
     # The initial 'LC_' is optional.  It is a fatal error to call this with
-    # something that isn't a known category to the platform.
+    # something that isn't a known category to this file.
     #
     # This optional parameter denotes which POSIX locale categories must be
     # available on the platform.  If any aren't available, this function