File Coverage

blib/lib/Geography/USStates.pm
Criterion Covered Total %
statement 32 41 78.0
branch 9 12 75.0
condition 8 10 80.0
subroutine 10 17 58.8
pod 0 16 0.0
total 59 96 61.4


line stmt bran cond sub pod time code
1             # -------------------------------------------------------------------------
2             # Name: Geography::USStates.pm
3             # Auth: Dion Almaer (dion)
4             # Desc: Get info on US States / Dependant Areas
5             # Date Created: Sun Nov 15 17:50:29 1998
6             # Version: 0.12
7             # $Modified: Wed May 26 02:15:53 1999 by dion $
8             # -------------------------------------------------------------------------
9             package Geography::USStates;
10              
11 1     1   707 use strict;
  1         2  
  1         1246  
12             require 5.002;
13             require Exporter;
14              
15             $Geography::USStates::VERSION = '0.12';
16             @Geography::USStates::ISA = qw(Exporter);
17             @Geography::USStates::EXPORT = qw(getState getStates getStateNames getStateAbbrevs);
18             @Geography::USStates::EXPORT_OK = qw(getArea getAreas getAreaNames getAreaAbbrevs getStateOrArea getStatesAndAreas
19             getStatesAndAreasNames getStatesAndAreasAbbrevs);
20             %Geography::USStates::EXPORT_TAGS =(states=> [qw|getState getStates getStateNames getStateAbbrevs|],
21             areas => [qw|getArea getAreas getAreaNames getAreaAbbrevs|],
22             both => [qw|getStateOrArea getStatesAndAreas getStatesAndAreasNames getStatesAndAreasAbbrevs|],
23             all => [qw|getStateOrArea getStatesAndAreas getStatesAndAreasNames getStatesAndAreasAbbrevs
24             getState getStates getStateNames getStateAbbrevs
25             getArea getAreas getAreaNames getAreaAbbrevs|]);
26              
27             %Geography::USStates::STATES =
28             (AL => 'Alabama', AK => 'Alaska', AZ => 'Arizona',
29             AR => 'Arkansas', CA => 'California', CO => 'Colorado',
30             CT => 'Connecticut', DE => 'Delaware', FL => 'Florida',
31             GA => 'Georgia', HI => 'Hawaii', ID => 'Idaho',
32             IL => 'Illinois', IN => 'Indiana', IA => 'Iowa',
33             KS => 'Kansas', KY => 'Kentucky',
34             LA => 'Louisiana', ME => 'Maine', MD => 'Maryland',
35             MA => 'Massachusetts', MI => 'Michigan', MN => 'Minnesota',
36             MS => 'Mississippi', MO => 'Missouri', MT => 'Montana',
37             'NE' => 'Nebraska', NJ => 'New Jersey', NH => 'New Hampshire',
38             NV => 'Nevada', NM => 'New Mexico', NY => 'New York',
39             NC => 'North Carolina', ND => 'North Dakota', OH => 'Ohio',
40             OK => 'Oklahoma', OR => 'Oregon', PA => 'Pennsylvania',
41             RI => 'Rhode Island', SC => 'South Carolina',
42             SD => 'South Dakota', TN => 'Tennessee', TX => 'Texas',
43             UT => 'Utah', VT => 'Vermont', VA => 'Virginia',
44             WA => 'Washington', WV => 'West Virginia', WI => 'Wisconsin',
45             WY => 'Wyoming');
46              
47             %Geography::USStates::AREAS = (AS => 'American Samoa', DC => 'District Of Columbia', GU => 'Guam',
48             MD => 'Midway Islands', NI => 'Northern Mariana Islands',
49             PR => 'Puerto Rico', VI => 'Virgin Islands');
50              
51             %Geography::USStates::BOTH = (%Geography::USStates::STATES, %Geography::USStates::AREAS);
52              
53             # ----------------------------------------------------------------------------
54             # CORE FUNCTIONS: These functions do the real work
55             # ----------------------------------------------------------------------------
56              
57             # ----------------------------------------------------------------------------
58             # Subroutine: getKey - Given a hashref and (abbrev get full name, name get abbrev)
59             # ----------------------------------------------------------------------------
60             sub getKey {
61 4     4 0 6 my $hashref = shift;
62 4         5 my $value = shift;
63 4 100       11 if (length $value > 2) {
64 2         6 my %states = getKeys($hashref, 'case' => 'lower', 'hashkey' => 'name');
65 2         23 return $states{lc $value};
66             } else {
67 2         10 return $hashref->{uc $value};
68             }
69             }
70              
71             # ----------------------------------------------------------------------------
72             # Subroutine: getKeys - return a hash: $hash{'MN'} = 'Minnesota'
73             # ----------------------------------------------------------------------------
74             sub getKeys {
75 8     8 0 10 my $hashref = shift;
76 8 50       31 my %hash = (ref $_[0] eq 'HASH') ? %{ $_[0] } : @_;
  0         0  
77              
78             # -- do something to the states
79 8   100     29 $hash{hashkey} ||= ''; # -- for -w
80 8   100     21 $hash{case} ||= ''; # -- for -w
81              
82 8 50 66     34 return %{ $hashref }
  2   66     57  
83             unless @_ || $hash{case} || $hash{hashkey} eq 'name';
84              
85 6         6 my %states;
86 6         8 while ( my ($abbrev, $name) = each %{ $hashref } ) {
  177         516  
87 171 50       501 if ($hash{case} =~ /^[lu]/i) {
88 171 100       458 $name = ($hash{case} =~ /^u/i) ? uc $name : lc $name;
89             }
90 171 100       291 if ($hash{hashkey} eq 'name') {
91 114         340 $states{$name} = $abbrev;
92             } else {
93 57         139 $states{$abbrev} = $name;
94             }
95             }
96 6         152 return %states;
97             }
98              
99 2     2 0 3 sub getNames { return sort values %{ $_[0] }; }
  2         48  
100 0     0 0 0 sub getAbbrevs { return sort keys %{ $_[0] }; }
  0         0  
101              
102             # ----------------------------------------------------------------------------
103             # US STATE FUNCTIONS
104             # ----------------------------------------------------------------------------
105              
106             # ----------------------------------------------------------------------------
107             # Subroutine: getState - given abbrev get full name, given name get abbrev
108             # ----------------------------------------------------------------------------
109 2     2 0 22 sub getState { getKey(\%Geography::USStates::STATES, @_); }
110              
111             # ----------------------------------------------------------------------------
112             # Subroutine: getStates - return a states hash: $hash{'MN'} = 'Minnesota'
113             # ----------------------------------------------------------------------------
114 3     3 0 62 sub getStates { getKeys(\%Geography::USStates::STATES, @_); }
115              
116             # ----------------------------------------------------------------------------
117             # Subroutine: getStateNames - return an array of states by name
118             # ----------------------------------------------------------------------------
119 1     1 0 12 sub getStateNames { getNames(\%Geography::USStates::STATES); }
120              
121             # ----------------------------------------------------------------------------
122             # Subroutine: getStateAbbrevs - return an array of states by 2 letter abbrev
123             # ----------------------------------------------------------------------------
124 0     0 0 0 sub getStateAbbrevs { getAbbrevs(\%Geography::USStates::STATES); }
125              
126             # ----------------------------------------------------------------------------
127             # DEPENDANT AREAS: These are areas that aren't states, but are part of the US
128             # ----------------------------------------------------------------------------
129              
130             # ----------------------------------------------------------------------------
131             # Subroutine: getArea - given abbrev get full name, given name get abbrev
132             # ----------------------------------------------------------------------------
133 2     2 0 25 sub getArea { getKey(\%Geography::USStates::AREAS, @_); }
134              
135             # ----------------------------------------------------------------------------
136             # Subroutine: getAreas - return an areas hash: $hash{'GU'} = 'Guam'
137             # ----------------------------------------------------------------------------
138 3     3 0 28 sub getAreas { getKeys(\%Geography::USStates::AREAS, @_); }
139              
140             # ----------------------------------------------------------------------------
141             # Subroutine: getAreaNames - return an array of areas by name
142             # ----------------------------------------------------------------------------
143 1     1 0 10 sub getAreaNames { getNames(\%Geography::USStates::AREAS); }
144              
145             # ----------------------------------------------------------------------------
146             # Subroutine: getAreaAbbrevs - return an array of areas by 2 letter abbrev
147             # ----------------------------------------------------------------------------
148 0     0 0   sub getAreaAbbrevs { getAbbrevs(\%Geography::USStates::AREAS); }
149              
150             # ----------------------------------------------------------------------------
151             # BOTH: Look up both states and dependant areas
152             # ----------------------------------------------------------------------------
153            
154             # ----------------------------------------------------------------------------
155             # Subroutine: getStateOrArea - given abbrev get full name, given name get abbrev
156             # ----------------------------------------------------------------------------
157 0     0 0   sub getStateOrArea { getKey(\%Geography::USStates::BOTH, @_); }
158              
159             # ----------------------------------------------------------------------------
160             # Subroutine: getStatesAndAreas - return a state or areas hash
161             # ----------------------------------------------------------------------------
162 0     0 0   sub getStatesAndAreas { getKeys(\%Geography::USStates::BOTH, @_); }
163              
164             # ----------------------------------------------------------------------------
165             # Subroutine: getStatesAndAreasNames - return an array of both by name
166             # ----------------------------------------------------------------------------
167 0     0 0   sub getStatesAndAreasNames { getNames(\%Geography::USStates::BOTH); }
168              
169             # ----------------------------------------------------------------------------
170             # Subroutine: getStatesAndAreasAbbrevs - return an array of both by 2 letter abbrev
171             # ----------------------------------------------------------------------------
172 0     0 0   sub getStatesAndAreasAbbrevs { getAbbrevs(\%Geography::USStates::BOTH); }
173              
174             # ----------------------------------------------------------------------------
175             1; # End of Geography::USStates.pm
176             # ----------------------------------------------------------------------------
177              
178             __END__