File Coverage

blib/lib/Locale/CA.pm
Criterion Covered Total %
statement 48 50 96.0
branch 14 16 87.5
condition 10 12 83.3
subroutine 9 9 100.0
pod 3 3 100.0
total 84 90 93.3


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