File Coverage

blib/lib/Locale/CA.pm
Criterion Covered Total %
statement 50 54 92.5
branch 14 20 70.0
condition 9 21 42.8
subroutine 8 8 100.0
pod 3 3 100.0
total 84 106 79.2


line stmt bran cond sub pod time code
1             package Locale::CA;
2              
3 5     5   622184 use warnings;
  5         43  
  5         163  
4 5     5   27 use strict;
  5         18  
  5         91  
5 5     5   22 use Carp;
  5         14  
  5         305  
6 5     5   2254 use Data::Section::Simple;
  5         2860  
  5         3126  
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.05
15              
16             =cut
17              
18             our $VERSION = '0.05';
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             my $province = $u->{code2province}{$code};
29             my $code = $u->{province2code}{$province};
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             =cut
41              
42             sub new {
43 8     8 1 5866 my $proto = shift;
44 8   66     59 my $class = ref($proto) || $proto;
45              
46 8 100       30 if(!defined($class)) {
47             # Use Lingua::CA->new(), not Lingua::CA::new()
48             # Carp::carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
49             # return;
50              
51             # FIXME: this only works when no arguments are given
52 1         15 $class = __PACKAGE__;
53             }
54              
55 8         15 my %params;
56 8 100       46 if(ref($_[0]) eq 'HASH') {
    100          
57 1         2 %params = %{$_[0]};
  1         5  
58             } elsif(scalar(@_) % 2 == 0) {
59 6         16 %params = @_;
60             } else {
61 1         10 $params{'lang'} = shift;
62             }
63              
64 8         13 my $data;
65 8 100 100     30 if(defined(my $lang = ($params{'lang'} || _get_language()))) {
66 4 100 66     16 if(($lang eq 'fr') || ($lang eq 'en')) {
67 3         12 $data = Data::Section::Simple::get_data_section("provinces_$lang");
68             } else {
69 1         19 Carp::croak("lang can only be one of 'en' or 'fr', given $lang");
70             }
71             } else {
72 4         16 $data = Data::Section::Simple::get_data_section('provinces_en');
73             }
74              
75 7         1957 my @lines = split /\n/, $data;
76              
77 7         20 my $self = {};
78 7         16 for (@lines) {
79 84         168 my($code, $province) = split /:/;
80 84         149 $self->{code2province}{$code} = $province;
81 84         181 $self->{province2code}{$province} = $code;
82             }
83              
84 7         59 return bless $self, $class;
85             }
86              
87             # https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
88             # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html
89             sub _get_language
90             {
91 5 50   5   17 if(my $language = $ENV{'LANGUAGE'}) {
92 0         0 foreach my $l(split/:/, $language) {
93 0 0 0     0 if(($l eq 'en') || ($l eq 'fr')) {
94 0         0 return $l;
95             }
96             }
97             }
98 5         15 foreach my $variable('LC_ALL', 'LC_MESSAGES', 'LANG') {
99 15         26 my $val = $ENV{$variable};
100 15 100       35 next unless(defined($val));
101              
102 1         7 $val = substr($val, 0, 2);
103 1 50 33     8 if(($val eq 'en') || ($val eq 'fr')) {
104 1         6 return $val;
105             }
106             }
107 4 0 0     16 if(defined($ENV{'LANG'}) && (($ENV{'LANG'} =~ /^C\./) || ($ENV{'LANG'} eq 'C'))) {
      33        
108 0         0 return 'en';
109             }
110 4         18 return; # undef
111             }
112              
113             =head2 all_province_codes
114              
115             Returns an array (not arrayref) of all province codes in alphabetical form.
116              
117             =cut
118              
119             sub all_province_codes {
120 1     1 1 638 my $self = shift;
121              
122 1         14 return(sort keys %{$self->{code2province}});
  1         13  
123             }
124              
125             =head2 all_province_names
126              
127             Returns an array (not arrayref) of all province names in alphabetical form
128              
129             =cut
130              
131             sub all_province_names {
132 1     1 1 5612 my $self = shift;
133              
134 1         3 return(sort keys %{$self->{province2code}});
  1         12  
135             }
136              
137             =head2 $self->{code2province}
138              
139             This is a hashref which has two-letter province names as the key and the long
140             name as the value.
141              
142             =head2 $self->{province2code}
143              
144             This is a hashref which has the long name as the key and the two-letter
145             province name as the value.
146              
147             =head1 SEE ALSO
148              
149             L
150              
151             =head1 AUTHOR
152              
153             Nigel Horne, C<< >>
154              
155             =head1 BUGS
156              
157             =over 4
158              
159             =item * The province name is returned in C format.
160              
161             =item * neither hash is strict, though they should be.
162              
163             =back
164              
165             =head1 SUPPORT
166              
167             You can find documentation for this module with the perldoc command.
168              
169             perldoc Locale::CA
170              
171             You can also look for information at:
172              
173             =over 4
174              
175             =item * RT: CPAN's request tracker
176              
177             L
178              
179             =item * CPAN Ratings
180              
181             L
182              
183             =item * Search CPAN
184              
185             L
186              
187             =back
188              
189             =head1 ACKNOWLEDGEMENTS
190              
191             Based on L - Copyright (c) 2002 - C<< $present >> Terrence Brannon.
192              
193             =head1 LICENSE AND COPYRIGHT
194              
195             Copyright 2012-2023 Nigel Horne.
196              
197             This program is released under the following licence: GPL2
198              
199             =cut
200              
201             1; # End of Locale::CA
202             __DATA__