File Coverage

blib/lib/Locale/CA.pm
Criterion Covered Total %
statement 53 55 96.3
branch 19 22 86.3
condition 14 21 66.6
subroutine 8 8 100.0
pod 3 3 100.0
total 97 109 88.9


line stmt bran cond sub pod time code
1             package Locale::CA;
2              
3 6     6   1284204 use warnings;
  6         10  
  6         351  
4 6     6   27 use strict;
  6         11  
  6         134  
5 6     6   28 use Carp;
  6         8  
  6         337  
6 6     6   2859 use Data::Section::Simple;
  6         4629  
  6         4566  
7              
8             =head1 NAME
9              
10             Locale::CA - two letter codes for province identification in Canada and vice versa
11              
12             =head1 VERSION
13              
14             Version 0.08
15              
16             =cut
17              
18             our $VERSION = '0.08';
19              
20             =head1 SYNOPSIS
21              
22             use Locale::CA;
23              
24             my $u = Locale::CA->new();
25              
26             # Returns the French names of the provinces if $LANG starts with 'fr' or
27             # the lang parameter is set to 'fr'
28             print $u->{code2province}{'ON'}, "\n"; # prints ONTARIO
29             print $u->{province2code}{'ONTARIO'}, "\n"; # prints ON
30              
31             my @province = $u->all_province_names();
32             my @code = $u->all_province_codes();
33              
34             =head1 SUBROUTINES/METHODS
35              
36             =head2 new
37              
38             Creates a Locale::CA object.
39              
40             Can be called both as a class method (Locale::CA->new()) and as an object method ($object->new()).
41              
42             =cut
43              
44             sub new {
45 10     10 1 890734 my $proto = shift;
46 10   66     69 my $class = ref($proto) || $proto;
47              
48             # If the class is undefined, fallback to the current package name
49 10 100       31 if(!defined($class)) {
50             # Use Locale::CA->new(), not Locale::CA::new()
51             # Carp::carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
52             # return;
53              
54             # FIXME: this only works when no arguments are given
55 1         3 $class = __PACKAGE__;
56             }
57              
58 10         23 my %params;
59 10 100       58 if(ref($_[0]) eq 'HASH') {
    100          
    50          
60             # If the first argument is a hash reference, dereference it
61 1         2 %params = %{$_[0]};
  1         6  
62             } elsif(scalar(@_) % 2 == 0) {
63             # If there is an even number of arguments, treat them as key-value pairs
64 8         23 %params = @_;
65             } elsif(scalar(@_) == 1) {
66             # If there is one argument, assume the first is 'lang'
67 1         4 $params{'lang'} = shift;
68             } else {
69             # If there is an odd number of arguments, treat it as an error
70 0         0 Carp::croak(__PACKAGE__, ': Invalid arguments passed to new()');
71 0         0 return;
72             }
73              
74             # Determine the language and load the corresponding data
75 10         23 my $data;
76 10 100 100     47 if(defined(my $lang = ($params{'lang'} || _get_language()))) {
77 6 100 100     32 if(($lang eq 'fr') || ($lang eq 'en')) {
78             # Load data for the specified language (English or French)
79 5         24 $data = Data::Section::Simple::get_data_section("provinces_$lang");
80             } else {
81             # Invalid language specified, throw an error
82 1         26 Carp::croak("lang can only be one of 'en' or 'fr', given $lang");
83             }
84             } else {
85             # If no language is specified, fallback to English
86 4         20 $data = Data::Section::Simple::get_data_section('provinces_en');
87             }
88              
89             # Parse the data into bidirectional mappings
90 9         3275 my $self = {};
91 9         60 for(split /\n/, $data) {
92 130         307 my($code, $province) = split /:/;
93             # Map codes to provinces
94 130         293 $self->{code2province}{$code} = $province;
95             # Map provinces to codes
96 130         372 $self->{province2code}{$province} = $code;
97             }
98              
99             # Bless the hash reference into an object of the specified class
100 9         77 return bless $self, $class;
101             }
102              
103             # https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
104             # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
105             sub _get_language
106             {
107 7 100   7   35 if(my $language = $ENV{'LANGUAGE'}) {
108 1         5 foreach my $l(split/:/, $language) {
109 1 50 33     10 if(($l eq 'en') || ($l eq 'fr')) {
110 1         21 return $l;
111             }
112             }
113             }
114 6         18 foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') {
115 18         42 my $val = $ENV{$variable};
116 18 100       48 next unless(defined($val));
117              
118 2         7 $val = substr($val, 0, 2);
119 2 100 66     15 if(($val eq 'en') || ($val eq 'fr')) {
120 1         7 return $val;
121             }
122             }
123 5 50 33     25 if(defined($ENV{'LANG'}) && (($ENV{'LANG'} =~ /^C\./) || ($ENV{'LANG'} eq 'C'))) {
      66        
124 1         6 return 'en';
125             }
126 4         19 return; # undef
127             }
128              
129             =head2 all_province_codes
130              
131             Returns an array (not arrayref) of all province codes in alphabetical form.
132              
133             =cut
134              
135             sub all_province_codes {
136 1     1 1 575 my $self = shift;
137              
138 1         2 return(sort keys %{$self->{code2province}});
  1         13  
139             }
140              
141             =head2 all_province_names
142              
143             Returns an array (not arrayref) of all province names in alphabetical form
144              
145             =cut
146              
147             sub all_province_names {
148 1     1 1 7048 my $self = shift;
149              
150 1         2 return(sort keys %{$self->{province2code}});
  1         12  
151             }
152              
153             =head2 $self->{code2province}
154              
155             This is a hashref which has two-letter province names as the key and the long
156             name as the value.
157              
158             =head2 $self->{province2code}
159              
160             This is a hashref which has the long name as the key and the two-letter
161             province name as the value.
162              
163             =head1 SEE ALSO
164              
165             L
166              
167             =head1 AUTHOR
168              
169             Nigel Horne, C<< >>
170              
171             =head1 BUGS
172              
173             =over 4
174              
175             =item * The province name is returned in C format.
176              
177             =item * neither hash is strict, though they should be.
178              
179             =back
180              
181             =head1 SUPPORT
182              
183             You can find documentation for this module with the perldoc command.
184              
185             perldoc Locale::CA
186              
187             You can also look for information at:
188              
189             =over 4
190              
191             =item * RT: CPAN's request tracker
192              
193             L
194              
195             =item * Search CPAN
196              
197             L
198              
199             =back
200              
201             =head1 ACKNOWLEDGEMENTS
202              
203             Based on L - Copyright (c) 2002 - C<< $present >> Terrence Brannon.
204              
205             =head1 LICENSE AND COPYRIGHT
206              
207             Copyright 2012-2025 Nigel Horne.
208              
209             This program is released under the following licence: GPL2
210              
211             =cut
212              
213             1; # End of Locale::CA
214              
215             # Put the one you want to expand in code2province second
216             # so it overwrites the other when it's loaded
217              
218             __DATA__