blib/lib/Geography/States.pm

filter: Statementstmt 100% 186 / 186 Branchbran 88.2% 30 / 34 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 9 / 9 Podpod 33.3% 1 / 3 totaltotal 96.0% 241 / 251 CC 21 SCAR 20.4
CC
21
Cov
97%
new
13.2
state
5.0
_norm
4.0
CRAP 7.7
↑ Summary
1 package Geography::States;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '2015072102';
10  
11 sub init_data;
12  
13 1 my %states;
14  
15 254 ▶sub _c_length ($) {
Subroutine _c_length: called CC=2 SCAR=6.9
16 254 ▶ lc $_ [0] eq "australia" ? 3 : 2
Branch: 1 branch
BranchTrueFalseTotal
lc $_[0] eq 'australia' ? : 16 238 254
17 }
18  
19 254 ▶sub _norm ($$) {
Subroutine _norm: called CC=4 SCAR=13.9
20 254 my ($str, $country) = @_;
21 254 ▶ if (_c_length ($country) >= length $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_c_length($country) >= length $str) { } 131 123 254
22 131 $str = uc $str;
23 }
24 else {
25 123 $str = join " " => map {ucfirst lc} split /\s+/ => $str;
26 123 ▶ $str =~ s/\bOf\b/of/ if $country eq lc 'USA';
Branch: 1 branch
BranchTrueFalseTotal
if $country eq "usa" 1 122 123
27 123 ▶ $str =~ s/\bD([eo])\b/d$1/ if $country eq lc 'Brazil';
Branch: 1 branch
BranchTrueFalseTotal
if $country eq "brazil" 0 123 123
28 }
29  
30 254 $str;
31 }
32  
33 {
34 1 my $data = init_data;
35  
36 1 ▶ while (my ($country, $country_data) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 5 1 6
37 5 ▶ while (my ($code, $state_data) = each %$country_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$country_data) 121 5 126
38 121 my ($strict, $name) = @$state_data;
39 121 my $info = [$code, $name, !$strict];
40 121 my $_code = _norm ($code, $country);
41 121 my $_name = _norm ($name, $country);
42 121 my $_country = lc $country;
43 121 $states {$_country} -> {$_code} = $info;
44 121 $states {$_country} -> {$_name} = $info;
45 }
46 }
47 }
48  
49  
50 7 ▶sub new {
Subroutine new: called CC=13 SCAR=25.8
Pod undocumented
51 7 ▶ die "Not enough arguments for Geography::States -> new ()\n" unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 7 7
52  
53 7 my $proto = shift;
54 7 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
55  
56 7 my $country = lc shift;
57 7 $country =~ s/\s+/ /g;
58  
59 7 ▶ die "No such country $country\n" unless $states {$country};
Branch: 1 branch
BranchTrueFalseTotal
unless $states{$country} 0 7 7
60  
61 7 my $strict = shift;
62  
63 7 my $self;
64 7 my ($cs, $info);
65 7 ▶ while (($cs, $info) = each %{$states {$country}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$states{$country};}) 388 7 395
66 388 ▶ next unless $cs eq $info -> [0];
Branch: 1 branch
BranchTrueFalseTotal
unless $cs eq $info->[0] 193 195 388
67 195 ▶ next if $strict && $info -> [2];
Branch: 1 branch
BranchTrueFalseTotal
if $strict and $info->[2] 11 184 195
Condition100%3/3
$strict and $info->[2]
!ll&&!rl&&r
1216311
MC/DC100%2/2
$strict and $info->[2]
$strict $info->[2]
Truth table100%3/3
A and B
A= $strictB= $info->[2]
ABresult
0X0
100
111
68 184 my $inf = [@$info [0, 1]];
69 184 foreach my $i (0 .. 1) {
70 #
71 # Hardcoded exception.
72 #
73 368 ▶ next if $country eq 'canada' &&
Branch: 1 branch
BranchTrueFalseTotal
if $country eq "canada" and $inf->[0] eq "PQ" and $i == 1 1 367 368
Condition100%6/6
$country eq "canada" and $inf->[0] eq "PQ" and $i == 1
!ll&&!rl&&r
36611
$country eq "canada" and $inf->[0] eq "PQ"
!ll&&!rl&&r
312542
MC/DC100%3/3
$country eq "canada" and $inf->[0] eq "PQ" and $i == 1
$country eq "canada" $inf->[0] eq "PQ" $i == 1
Truth table100%4/4
A and B and C
A= $country eq "canada"B= $inf->[0] eq "PQ"C= $i == 1
ABCresult
0XX0
10X0
1100
1111
74 $$inf [0] eq 'PQ' &&
75 $i == 1;
76 $self -> {cs} -> {$info -> [$i]} = $inf unless
77 367 ▶ exists $self -> {cs} -> {$info -> [$i]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"cs"}{$info->[$i]} 367 0 367
78 }
79 }
80 7 $self -> {country} = $country;
81  
82 7 bless $self => $class;
83 }
84  
85  
86 14 ▶sub state {
Subroutine state: called CC=5 SCAR=16.1
Pod documented
87 14 my $self = shift;
88 14 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 2 12 14
89 2 my %h;
90 2 return grep {!$h {$_} ++} values %{$self -> {cs}};
91 }
92 12 my $query = _norm shift, $self -> {country};
93 12 ▶ my $answer = $self -> {cs} -> {$query} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $answer = $self->{"cs"}{$query} or return 10 2 12
94 10 ▶ return @$answer if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 9 10
95 9 ▶ $answer -> [$answer -> [0] eq $query ? 1 : 0];
Branch: 1 branch
BranchTrueFalseTotal
$answer->[0] eq $query ? : 7 2 9
96 }
97  
98  
99 1 1;
100
101 =pod
102  
103 =head1 NAME
104  
105 Geography::States - Map states and provinces to their codes, and vica versa.
106  
107 =head1 SYNOPSIS
108  
109 use Geography::States;
110  
111 my $obj = Geography::States -> new (COUNTRY [, STRICT]);
112  
113  
114 =head1 EXAMPLES
115  
116 my $canada = Geography::States -> new ('Canada');
117  
118 my $name = $canada -> state ('NF'); # Newfoundland.
119 my $code = $canada -> state ('Ontario'); # ON.
120 my ($code, $name) = $canada -> state ('BC'); # BC, British Columbia.
121 my @all_states = $canada -> state; # List code/name pairs.
122  
123  
124 =head1 DESCRIPTION
125  
126 This module lets you map states and provinces to their codes, and codes
127 to names of provinces and states.
128  
129 The C<< Geography::States -> new () >> call takes 1 or 2 arguments. The
130 first, required, argument is the country we are interested in. Current
131 supported countries are I<USA>, I<Brazil>, I<Canada>, I<The Netherlands>,
132 and I<Australia>. If a second non-false argument is given, we use I<strict
133 mode>. In non-strict mode, we will map territories and alternative codes
134 as well, while we do not do that in strict mode. For example, if the
135 country is B<USA>, in non-strict mode, we will map B<GU> to B<Guam>,
136 while in strict mode, neither B<GU> and B<Guam> will be found.
137  
138 =head2 The state() method
139  
140 All queries are done by calling the C<state> method in the object. This method
141 takes an optional argument. If an argument is given, then in scalar context,
142 it will return the name of the state if a code of a state is given, and the
143 code of a state, if the argument of the method is a name of a state. In list
144 context, both the code and the state will be returned.
145  
146 If no argument is given, then the C<state> method in list context will return
147 a list of all code/name pairs for that country. In scalar context, it will
148 return the number of code/name pairs. Each code/name pair is a 2 element
149 anonymous array.
150  
151 Arguments can be given in a case insensitive way; if a name consists of
152 multiple parts, the number of spaces does not matter, as long as there is
153 some whitespace. (That is "NewYork" is wrong, but S<"new YORK"> is fine.)
154  
155 =head1 ODDITIES AND OPEN QUESTIONS
156  
157 I found conflicting abbreviations for the US I<Northern Mariana Islands>,
158 listed as I<NI> and I<MP>. I picked I<MP> from the USPS site.
159  
160 One site listed I<Midway Islands> as having code I<MD>. It is not listed by
161 the USPS site, and because it conflicts with I<Maryland>, it is not put in
162 this listing.
163  
164 The USPS also has so-called I<Military "States">, with non-unique codes.
165 Those are not listed here.
166  
167 Canada's I<Quebec> has two codes, the older I<PQ> and the modern I<QC>. Both
168 I<PQ> and I<QC> will map to I<Quebec>, but I<Quebec> will only map to I<QC>.
169 With strict mode, I<PQ> will not be listed. Similary, Newfoundland has an
170 old code I<NF>, and a new code I<NL> (the province is now called
171 I<Newfoundland and Labrador>).
172  
173 =head1 DEVELOPMENT
174
175 The current sources of this module are found on github,
176 L<< git://github.com/Abigail/geography--states.git >>.
177  
178 =head1 AUTHOR
179  
180 Abigail L<< mailto:geography-states@abigail.be >>.
181  
182 =head1 COPYRIGHT and LICENSE
183  
184 Copyright (C) 1999 - 2001, 2009 by Abigail.
185  
186 Permission is hereby granted, free of charge, to any person obtaining a
187 copy of this software and associated documentation files (the "Software"),
188 to deal in the Software without restriction, including without limitation
189 the rights to use, copy, modify, merge, publish, distribute, sublicense,
190 and/or sell copies of the Software, and to permit persons to whom the
191 Software is furnished to do so, subject to the following conditions:
192  
193 The above copyright notice and this permission notice shall be included
194 in all copies or substantial portions of the Software.
195
196 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
197 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
198 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
199 THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
200 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
201 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
202 THE SOFTWARE.
203  
204 =cut
205  
206  
207 1 ▶sub init_data {
Subroutine init_data: called CC=1 SCAR=0.0
Pod undocumented
208 1 my $data;
209  
210 1 $$data {'USA'} {AK} = [1 => "Alaska"];
211 1 $$data {'USA'} {AL} = [1 => "Alabama"];
212 1 $$data {'USA'} {AR} = [1 => "Arkansas"];
213 1 $$data {'USA'} {AS} = [0 => "American Samoa"];
214 1 $$data {'USA'} {AZ} = [1 => "Arizona"];
215 1 $$data {'USA'} {CA} = [1 => "California"];
216 1 $$data {'USA'} {CO} = [1 => "Colorado"];
217 1 $$data {'USA'} {CT} = [1 => "Connecticut"];
218 1 $$data {'USA'} {DC} = [0 => "District of Columbia"];
219 1 $$data {'USA'} {DE} = [1 => "Delaware"];
220 1 $$data {'USA'} {FL} = [1 => "Florida"];
221 1 $$data {'USA'} {FM} = [0 => "Federated States of Micronesia"];
222 1 $$data {'USA'} {GA} = [1 => "Georgia"];
223 1 $$data {'USA'} {GU} = [0 => "Guam"];
224 1 $$data {'USA'} {HI} = [1 => "Hawaii"];
225 1 $$data {'USA'} {IA} = [1 => "Iowa"];
226 1 $$data {'USA'} {ID} = [1 => "Idaho"];
227 1 $$data {'USA'} {IL} = [1 => "Illinois"];
228 1 $$data {'USA'} {IN} = [1 => "Indiana"];
229 1 $$data {'USA'} {KS} = [1 => "Kansas"];
230 1 $$data {'USA'} {KY} = [1 => "Kentucky"];
231 1 $$data {'USA'} {LA} = [1 => "Louisiana"];
232 1 $$data {'USA'} {MA} = [1 => "Massachusetts"];
233 1 $$data {'USA'} {MD} = [1 => "Maryland"];
234 1 $$data {'USA'} {ME} = [1 => "Maine"];
235 1 $$data {'USA'} {MH} = [0 => "Marshall Islands"];
236 1 $$data {'USA'} {MI} = [1 => "Michigan"];
237 1 $$data {'USA'} {MN} = [1 => "Minnesota"];
238 1 $$data {'USA'} {MO} = [1 => "Missouri"];
239 1 $$data {'USA'} {MP} = [0 => "Northern Mariana Islands"];
240 1 $$data {'USA'} {MS} = [1 => "Mississippi"];
241 1 $$data {'USA'} {MT} = [1 => "Montana"];
242 1 $$data {'USA'} {NC} = [1 => "North Carolina"];
243 1 $$data {'USA'} {ND} = [1 => "North Dakota"];
244 1 $$data {'USA'} {NE} = [1 => "Nebraska"];
245 1 $$data {'USA'} {NH} = [1 => "New Hampshire"];
246 1 $$data {'USA'} {NJ} = [1 => "New Jersey"];
247 1 $$data {'USA'} {NM} = [1 => "New Mexico"];
248 1 $$data {'USA'} {NV} = [1 => "Nevada"];
249 1 $$data {'USA'} {NY} = [1 => "New York"];
250 1 $$data {'USA'} {OH} = [1 => "Ohio"];
251 1 $$data {'USA'} {OK} = [1 => "Oklahoma"];
252 1 $$data {'USA'} {OR} = [1 => "Oregon"];
253 1 $$data {'USA'} {PA} = [1 => "Pennsylvania"];
254 1 $$data {'USA'} {PR} = [0 => "Puerto Rico"];
255 1 $$data {'USA'} {PW} = [0 => "Palau"];
256 1 $$data {'USA'} {RI} = [1 => "Rhode Island"];
257 1 $$data {'USA'} {SC} = [1 => "South Carolina"];
258 1 $$data {'USA'} {SD} = [1 => "South Dakota"];
259 1 $$data {'USA'} {TN} = [1 => "Tennessee"];
260 1 $$data {'USA'} {TX} = [1 => "Texas"];
261 1 $$data {'USA'} {UT} = [1 => "Utah"];
262 1 $$data {'USA'} {VA} = [1 => "Virginia"];
263 1 $$data {'USA'} {VI} = [0 => "Virgin Islands"];
264 1 $$data {'USA'} {VT} = [1 => "Vermont"];
265 1 $$data {'USA'} {WA} = [1 => "Washington"];
266 1 $$data {'USA'} {WI} = [1 => "Wisconsin"];
267 1 $$data {'USA'} {WV} = [1 => "West Virginia"];
268 1 $$data {'USA'} {WY} = [1 => "Wyoming"];
269  
270 1 $$data {'Brazil'} {AC} = [1 => "Acre"];
271 1 $$data {'Brazil'} {AL} = [1 => "Alagoas"];
272 1 $$data {'Brazil'} {AM} = [1 => "Amazonas"];
273 1 $$data {'Brazil'} {AP} = [1 => "Amap\x{e1}"];
274 1 $$data {'Brazil'} {BA} = [1 => "Bahia"];
275 1 $$data {'Brazil'} {CE} = [1 => "Cear\x{e1}"];
276 1 $$data {'Brazil'} {DF} = [1 => "Distrito Federal"];
277 1 $$data {'Brazil'} {ES} = [1 => "Espir\x{ed}to Santo"];
278 1 $$data {'Brazil'} {FN} = [1 => "Fernando de Noronha"];
279 1 $$data {'Brazil'} {GO} = [1 => "Goi\x{e1}s"];
280 1 $$data {'Brazil'} {MA} = [1 => "Maranh\x{e3}o"];
281 1 $$data {'Brazil'} {MG} = [1 => "Minas Gerais"];
282 1 $$data {'Brazil'} {MS} = [1 => "Mato Grosso do Sul"];
283 1 $$data {'Brazil'} {MT} = [1 => "Mato Grosso"];
284 1 $$data {'Brazil'} {PA} = [1 => "Par\x{e1}"];
285 1 $$data {'Brazil'} {PB} = [1 => "Para\x{ed}ba"];
286 1 $$data {'Brazil'} {PE} = [1 => "Pernambuco"];
287 1 $$data {'Brazil'} {PI} = [1 => "Piau\x{ed}"];
288 1 $$data {'Brazil'} {PR} = [1 => "Paran\x{e1}"];
289 1 $$data {'Brazil'} {RJ} = [1 => "Rio de Janeiro"];
290 1 $$data {'Brazil'} {RN} = [1 => "Rio Grande do Norte"];
291 1 $$data {'Brazil'} {RO} = [1 => "Rond\x{f4}nia"];
292 1 $$data {'Brazil'} {RR} = [1 => "Roraima"];
293 1 $$data {'Brazil'} {RS} = [1 => "Rio Grande do Sul"];
294 1 $$data {'Brazil'} {SC} = [1 => "Santa Catarina"];
295 1 $$data {'Brazil'} {SE} = [1 => "Sergipe"];
296 1 $$data {'Brazil'} {SP} = [1 => "S\x{e3}o Paulo"];
297 1 $$data {'Brazil'} {TO} = [1 => "Tocantins"];
298  
299 1 $$data {'Canada'} {AB} = [1 => "Alberta"];
300 1 $$data {'Canada'} {BC} = [1 => "British Columbia"];
301 1 $$data {'Canada'} {MB} = [1 => "Manitoba"];
302 1 $$data {'Canada'} {NB} = [1 => "New Brunswick"];
303 1 $$data {'Canada'} {NF} = [0 => "Newfoundland"];
304 1 $$data {'Canada'} {NL} = [1 => "Newfoundland and Labrador"];
305 1 $$data {'Canada'} {NS} = [1 => "Nova Scotia"];
306 1 $$data {'Canada'} {NT} = [1 => "Northwest Territories"];
307 1 $$data {'Canada'} {NU} = [1 => "Nunavut"];
308 1 $$data {'Canada'} {ON} = [1 => "Ontario"];
309 1 $$data {'Canada'} {PE} = [1 => "Prince Edward Island"];
310 1 $$data {'Canada'} {PQ} = [0 => "Quebec"];
311 1 $$data {'Canada'} {QC} = [1 => "Quebec"];
312 1 $$data {'Canada'} {SK} = [1 => "Saskatchewan"];
313 1 $$data {'Canada'} {YT} = [1 => "Yukon Territory"];
314  
315 1 $$data {'The Netherlands'} {DR} = [1 => "Drente"];
316 1 $$data {'The Netherlands'} {FL} = [1 => "Flevoland"];
317 1 $$data {'The Netherlands'} {FR} = [1 => "Friesland"];
318 1 $$data {'The Netherlands'} {GL} = [1 => "Gelderland"];
319 1 $$data {'The Netherlands'} {GR} = [1 => "Groningen"];
320 1 $$data {'The Netherlands'} {LB} = [1 => "Limburg"];
321 1 $$data {'The Netherlands'} {NB} = [1 => "Noord Brabant"];
322 1 $$data {'The Netherlands'} {NH} = [1 => "Noord Holland"];
323 1 $$data {'The Netherlands'} {OV} = [1 => "Overijssel"];
324 1 $$data {'The Netherlands'} {UT} = [1 => "Utrecht"];
325 1 $$data {'The Netherlands'} {ZH} = [1 => "Zuid Holland"];
326 1 $$data {'The Netherlands'} {ZL} = [1 => "Zeeland"];
327  
328 1 $$data {'Australia'} {ACT} = [1 => "Australian Capital Territory"];
329 1 $$data {'Australia'} {NSW} = [1 => "New South Wales"];
330 1 $$data {'Australia'} {QLD} = [1 => "Queensland"];
331 1 $$data {'Australia'} {SA} = [1 => "South Australia"];
332 1 $$data {'Australia'} {TAS} = [1 => "Tasmania"];
333 1 $$data {'Australia'} {VIC} = [1 => "Victoria"];
334 1 $$data {'Australia'} {WA} = [1 => "Western Australia"];
335  
336 1 $data;
337 }
↑ Summary