blib/lib/Geography/States.pm

filter: Statementstmt 100% 66 / 66 Branchbran 89.4% 34 / 38 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 92.7% 115 / 124 CC 18 SCAR 17.9
CC
18
Cov
95%
new
10.1
state
5.0
_norm
4.0
CRAP 6.0
↑ 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 = '2009040901';
10  
11 1 my %states;
12  
13 sub _c_length ($) {
14 254 ▶ lc $_ [0] eq "australia" ? 3 : 2
Subroutine _c_length: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
lc $_[0] eq 'australia' ? : 16 238 254
15 }
16  
17 sub _norm ($$) {
18 254 ▶ my ($str, $country) = @_;
Subroutine _norm: called CC=4 SCAR=13.9
19 254 ▶ if (_c_length ($country) >= length $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_c_length($country) >= length $str) { } 131 123 254
20 131 $str = uc $str;
21 }
22 else {
23 123 $str = join " " => map {ucfirst lc} split /\s+/ => $str;
24 123 ▶ $str =~ s/\bOf\b/of/ if $country eq lc 'USA';
Branch: 1 branch
BranchTrueFalseTotal
if $country eq "usa" 60 63 123
25 123 ▶ $str =~ s/\bD([eo])\b/d$1/ if $country eq lc 'Brazil';
Branch: 1 branch
BranchTrueFalseTotal
if $country eq "brazil" 28 95 123
26 }
27  
28 254 $str;
29 }
30  
31 # This was originally wrapped in an INIT block, to avoid having it
32 # run when only compilation was wanted. However, that seems to fail
33 # when used in combination with mod_perl.
34 my $country;
35 1 ▶while (<DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 132 0 132
36 132 chomp;
37 132 ▶ last if $_ eq '__END__';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "__END__" 1 131 132
38 131 s/#.*//;
39 131 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 5 126 131
40 126 ▶ if (/^<(.*)>/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^<(.*)>/) 5 121 126
41 5 $country = lc $1;
42 5 $country =~ s/\s+/ /g;
43 5 next;
44 }
45 121 my ($code, $state) = split /\s+/ => $_, 2;
46 121 ▶ next unless defined $state;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state 0 121 121
47 121 my $fake = $code =~ s/\*$//;
48 121 my $info = [$code, $state, $fake];
49 121 $states {$country} -> {_norm ($code, $country)} = $info;
50 121 $states {$country} -> {_norm ($state, $country)} = $info;
51 }
52  
53 sub new {
54 7 ▶ die "Not enough arguments for Geography::States -> new ()\n" unless @_ > 1;
Subroutine new: called CC=10 SCAR=23.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 7 7
55  
56 7 my $proto = shift;
57 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
58  
59 7 my $country = lc shift;
60 7 $country =~ s/\s+/ /g;
61  
62 7 ▶ die "No such country $country\n" unless $states {$country};
Branch: 1 branch
BranchTrueFalseTotal
unless $states{$country} 0 7 7
63  
64 7 my $strict = shift;
65  
66 7 my $self;
67 7 my ($cs, $info);
68 7 ▶ while (($cs, $info) = each %{$states {$country}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$states{$country};}) 388 7 395
69 388 ▶ next unless $cs eq $info -> [0];
Branch: 1 branch
BranchTrueFalseTotal
unless $cs eq $info->[0] 193 195 388
70 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
71 184 my $inf = [@$info [0, 1]];
72 184 foreach my $i (0 .. 1) {
73 $self -> {cs} -> {$info -> [$i]} = $inf unless
74 368 ▶ exists $self -> {cs} -> {$info -> [$i]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"cs"}{$info->[$i]} 367 1 368
75 }
76 }
77 7 $self -> {country} = $country;
78  
79 7 bless $self => $class;
80 }
81  
82  
83 sub state {
84 14 ▶ my $self = shift;
Subroutine state: called CC=5 SCAR=16.1
Pod documented
85 14 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 2 12 14
86 2 my %h;
87 2 return grep {!$h {$_} ++} values %{$self -> {cs}};
88 }
89 12 my $query = _norm shift, $self -> {country};
90 12 ▶ my $answer = $self -> {cs} -> {$query} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $answer = $self->{"cs"}{$query} or return 2 10 12
91 10 ▶ return @$answer if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 9 10
92 9 ▶ $answer -> [$answer -> [0] eq $query ? 1 : 0];
Branch: 1 branch
BranchTrueFalseTotal
$answer->[0] eq $query ? : 7 2 9
93 }
94  
95  
96 1 1;
97
98 =pod
99  
100 =head1 NAME
101  
102 Geography::States - Map states and provinces to their codes, and vica versa.
103  
104 =head1 SYNOPSIS
105  
106 use Geography::States;
107  
108 my $obj = Geography::States -> new (COUNTRY [, STRICT]);
109  
110  
111 =head1 EXAMPLES
112  
113 my $canada = Geography::States -> new ('Canada');
114  
115 my $name = $canada -> state ('NF'); # Newfoundland.
116 my $code = $canada -> state ('Ontario'); # ON.
117 my ($code, $name) = $canada -> state ('BC'); # BC, British Columbia.
118 my @all_states = $canada -> state; # List code/name pairs.
119  
120  
121 =head1 DESCRIPTION
122  
123 This module lets you map states and provinces to their codes, and codes
124 to names of provinces and states.
125  
126 The C<Geography::States -> new ()> call takes 1 or 2 arguments. The
127 first, required, argument is the country we are interested in. Current
128 supported countries are I<USA>, I<Brazil>, I<Canada>, I<The Netherlands>,
129 and I<Australia>. If a second non-false argument is given, we use I<strict
130 mode>. In non-strict mode, we will map territories and alternative codes
131 as well, while we do not do that in strict mode. For example, if the
132 country is B<USA>, in non-strict mode, we will map B<GU> to B<Guam>,
133 while in strict mode, neither B<GU> and B<Guam> will be found.
134  
135 =head2 The state() method
136  
137 All queries are done by calling the C<state> method in the object. This method
138 takes an optional argument. If an argument is given, then in scalar context,
139 it will return the name of the state if a code of a state is given, and the
140 code of a state, if the argument of the method is a name of a state. In list
141 context, both the code and the state will be returned.
142  
143 If no argument is given, then the C<state> method in list context will return
144 a list of all code/name pairs for that country. In scalar context, it will
145 return the number of code/name pairs. Each code/name pair is a 2 element
146 anonymous array.
147  
148 Arguments can be given in a case insensitive way; if a name consists of
149 multiple parts, the number of spaces does not matter, as long as there is
150 some whitespace. (That is "NewYork" is wrong, but S<"new YORK"> is fine.)
151  
152 =head1 ODDITIES AND OPEN QUESTIONS
153  
154 I found conflicting abbreviations for the US I<Northern Mariana Islands>,
155 listed as I<NI> and I<MP>. I picked I<MP> from the USPS site.
156  
157 One site listed I<Midway Islands> as having code I<MD>. It is not listed by
158 the USPS site, and because it conflicts with I<Maryland>, it is not put in
159 this listing.
160  
161 The USPS also has so-called I<Military "States">, with non-unique codes.
162 Those are not listed here.
163  
164 Canada's I<Quebec> has two codes, the older I<PQ> and the modern I<QC>. Both
165 I<PQ> and I<QC> will map to I<Quebec>, but I<Quebec> will only map to I<QC>.
166 With strict mode, I<PQ> will not be listed. Similary, Newfoundland has an
167 old code I<NF>, and a new code I<NL> (the province is now called
168 I<Newfoundland and Labrador>).
169  
170 =head1 DEVELOPMENT
171
172 The current sources of this module are found on github,
173 L<< git://github.com/Abigail/geography--states.git >>.
174  
175 =head1 AUTHOR
176  
177 Abigail L<< mailto:geography-states@abigail.be >>.
178  
179 =head1 COPYRIGHT and LICENSE
180  
181 Copyright (C) 1999 - 2001, 2009 by Abigail.
182  
183 Permission is hereby granted, free of charge, to any person obtaining a
184 copy of this software and associated documentation files (the "Software"),
185 to deal in the Software without restriction, including without limitation
186 the rights to use, copy, modify, merge, publish, distribute, sublicense,
187 and/or sell copies of the Software, and to permit persons to whom the
188 Software is furnished to do so, subject to the following conditions:
189  
190 The above copyright notice and this permission notice shall be included
191 in all copies or substantial portions of the Software.
192
193 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
194 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
196 THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
197 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
198 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
199 THE SOFTWARE.
200  
201 =cut
202  
203 __DATA__
↑ Summary