File Coverage

blib/lib/Net/Continental.pm
Criterion Covered Total %
statement 40 41 97.5
branch 12 14 85.7
condition 4 6 66.6
subroutine 9 10 90.0
pod 3 3 100.0
total 68 74 91.8


line stmt bran cond sub pod time code
1 1     1   76893 use strict;
  1         11  
  1         33  
2 1     1   5 use warnings;
  1         2  
  1         42  
3             package Net::Continental 0.017;
4             # ABSTRACT: code to map countries to continents, esp. with nerd.dk dnsbl
5              
6 1     1   6 use Carp ();
  1         2  
  1         14  
7 1     1   1346 use Locale::Codes::Country ();
  1         47695  
  1         31  
8 1     1   703 use Net::Continental::Zone;
  1         4  
  1         42  
9 1     1   10 use Scalar::Util qw(blessed);
  1         3  
  1         1031  
10              
11             our %Continent = (
12             N => 'North America',
13             S => 'South America',
14             E => 'Europe',
15             A => 'Asia',
16             F => 'Africa',
17             O => 'Oceania',
18             Q => 'Antarctica',
19             );
20              
21             # qw(continent description)
22              
23             my %zone = (
24             ae => [ A => q{United Arab Emirates} ],
25             af => [ A => q{Afghanistan} ],
26             az => [ A => q{Azerbaijan} ],
27             bd => [ A => q{Bangladesh} ],
28             bh => [ A => q{Bahrain} ],
29             bt => [ A => q{Bhutan} ],
30             bn => [ A => q{Brunei Darussalam} ],
31             cn => [ A => q{China} ],
32              
33             # classification of Georgia in Europe or Asia is touchy
34             ge => [ A => q{Georgia} ],
35              
36             hk => [ A => q{Hong Kong} ],
37             il => [ A => q{Israel} ],
38             in => [ A => q{India} ],
39             id => [ A => q{Indonesia} ],
40             iq => [ A => q{Iraq} ],
41             ir => [ A => q{Iran (Islamic Republic of)} ],
42             jo => [ A => q{Jordan} ],
43             jp => [ A => q{Japan} ],
44             kg => [ A => q{Kyrgyzstan} ],
45             kh => [ A => q{Cambodia} ],
46             kp => [ A => q{Korea, Democratic People's Republic} ],
47             kr => [ A => q{Korea, Republic of} ],
48             kw => [ A => q{Kuwait} ],
49             kz => [ A => q{Kazakhstan} ],
50             la => [ A => q{Lao People's Democratic Republic} ],
51             lb => [ A => q{Lebanon} ],
52             lk => [ A => q{Sri Lanka} ],
53             mm => [ A => q{Myanmar} ],
54             mn => [ A => q{Mongolia} ],
55             mo => [ A => q{Macau} ],
56             mv => [ A => q{Maldives} ],
57             my => [ A => q{Malaysia} ],
58             np => [ A => q{Nepal} ],
59             om => [ A => q{Oman} ],
60             ph => [ A => q{Philippines} ],
61             pk => [ A => q{Pakistan} ],
62             ps => [ A => q{Palestinian Territories} ],
63             qa => [ A => q{Qatar} ],
64             ru => [ A => q{Russian Federation} ],
65             sa => [ A => q{Saudi Arabia} ],
66             sg => [ A => q{Singapore} ],
67             su => [ A => q{Soviet Union} ],
68             sy => [ A => q{Syrian Arab Republic} ],
69             th => [ A => q{Thailand} ],
70             tj => [ A => q{Tajikistan} ],
71             tl => [ A => q{Timor-Leste} ],
72             tm => [ A => q{Turkmenistan} ],
73             tp => [ A => q{East Timor} ],
74             tr => [ A => q{Turkey} ],
75             tw => [ A => q{Taiwan} ],
76             uz => [ A => q{Uzbekistan} ],
77             vn => [ A => q{Vietnam} ],
78             ye => [ A => q{Yemen} ],
79              
80             ad => [ E => q{Andorra} ],
81             al => [ E => q{Albania} ],
82             am => [ E => q{Armenia} ],
83             at => [ E => q{Austria} ],
84             ax => [ E => q(Aland Islands) ],
85             ba => [ E => q{Bosnia and Herzegovina} ],
86             be => [ E => q{Belgium} ],
87             bg => [ E => q{Bulgaria} ],
88             by => [ E => q{Belarus} ],
89             ch => [ E => q{Switzerland} ],
90             cy => [ E => q{Cyprus} ],
91             cz => [ E => q{Czech Republic} ],
92             de => [ E => q{Germany} ],
93             dk => [ E => q{Denmark} ],
94             ee => [ E => q{Estonia} ],
95             es => [ E => q{Spain} ],
96             eu => [ E => q{European Union} ],
97             fi => [ E => q{Finland} ],
98             fo => [ E => q{Faroe Islands} ],
99             fr => [ E => q{France} ],
100             fx => [ E => q{France, Metropolitan} ],
101             gb => [ E => q{United Kingdom} ],
102             gg => [ E => q{Guernsey} ],
103             gi => [ E => q{Gibraltar} ],
104             gr => [ E => q{Greece} ],
105             hr => [ E => q{Croatia/Hrvatska} ],
106             hu => [ E => q{Hungary} ],
107             ie => [ E => q{Ireland} ],
108             im => [ E => q{Isle of Man} ],
109             is => [ E => q{Iceland} ],
110             it => [ E => q{Italy} ],
111             je => [ E => q{Jersey} ],
112             li => [ E => q{Liechtenstein} ],
113             lt => [ E => q{Lithuania} ],
114             lu => [ E => q{Luxembourg} ],
115             lv => [ E => q{Latvia} ],
116             mc => [ E => q{Monaco} ],
117             md => [ E => q{Moldova, Republic of} ],
118             me => [ E => q(Montenegro) ],
119             mk => [ E => q{Macedonia, Former Yugoslav Republic} ],
120             mt => [ E => q{Malta} ],
121             nl => [ E => q{Netherlands} ],
122             no => [ E => q{Norway} ],
123             pl => [ E => q{Poland} ],
124             pt => [ E => q{Portugal} ],
125             ro => [ E => q{Romania} ],
126             rs => [ E => q(Serbia) ],
127             se => [ E => q{Sweden} ],
128             si => [ E => q{Slovenia} ],
129             sj => [ E => q{Svalbard and Jan Mayen Islands} ],
130             sk => [ E => q{Slovak Republic} ],
131             sm => [ E => q{San Marino} ],
132             ua => [ E => q{Ukraine} ],
133             # uk => [ E => q{United Kingdom} ],
134             va => [ E => q{Holy See (City Vatican State)} ],
135             yu => [ E => q{Yugoslavia} ],
136              
137             ac => [ F => q{Ascension Island} ],
138             ao => [ F => q{Angola} ],
139             bf => [ F => q{Burkina Faso} ],
140             bi => [ F => q{Burundi} ],
141             bj => [ F => q{Benin} ],
142             bw => [ F => q{Botswana} ],
143             cd => [ F => q{Congo, Democratic Republic of the} ],
144             cf => [ F => q{Central African Republic} ],
145             cg => [ F => q{Congo, Republic of} ],
146             ci => [ F => q{Cote d'Ivoire} ],
147             cm => [ F => q{Cameroon} ],
148             cv => [ F => q{Cap Verde} ],
149             dj => [ F => q{Djibouti} ],
150             dz => [ F => q{Algeria} ],
151             eg => [ F => q{Egypt} ],
152             eh => [ F => q{Western Sahara} ],
153             er => [ F => q{Eritrea} ],
154             et => [ F => q{Ethiopia} ],
155             ga => [ F => q{Gabon} ],
156             gh => [ F => q{Ghana} ],
157             gm => [ F => q{Gambia} ],
158             gn => [ F => q{Guinea} ],
159             gq => [ F => q{Equatorial Guinea} ],
160             gw => [ F => q{Guinea-Bissau} ],
161             ke => [ F => q{Kenya} ],
162             km => [ F => q{Comoros} ],
163             lr => [ F => q{Liberia} ],
164             ls => [ F => q{Lesotho} ],
165             ly => [ F => q{Libyan Arab Jamahiriya} ],
166             ma => [ F => q{Morocco} ],
167             mg => [ F => q{Madagascar} ],
168             ml => [ F => q{Mali} ],
169             mr => [ F => q{Mauritania} ],
170             mu => [ F => q{Mauritius} ],
171             mw => [ F => q{Malawi} ],
172             mz => [ F => q{Mozambique} ],
173             na => [ F => q{Namibia} ],
174             ne => [ F => q{Niger} ],
175             ng => [ F => q{Nigeria} ],
176             re => [ F => q{Reunion Island} ],
177             rw => [ F => q{Rwanda} ],
178             sc => [ F => q{Seychelles} ],
179             sd => [ F => q{Sudan} ],
180             sh => [ F => q{St. Helena} ],
181             sl => [ F => q{Sierra Leone} ],
182             sn => [ F => q{Senegal} ],
183             so => [ F => q{Somalia} ],
184             st => [ F => q{Sao Tome and Principe} ],
185             sz => [ F => q{Swaziland} ],
186             td => [ F => q{Chad} ],
187             tg => [ F => q{Togo} ],
188             tn => [ F => q{Tunisia} ],
189             tz => [ F => q{Tanzania} ],
190             ug => [ F => q{Uganda} ],
191             yt => [ F => q{Mayotte} ],
192             za => [ F => q{South Africa} ],
193             zm => [ F => q{Zambia} ],
194             zr => [ F => q{Zaire} ],
195             zw => [ F => q{Zimbabwe} ],
196              
197             ag => [ N => q{Antigua and Barbuda} ],
198             ai => [ N => q{Anguilla} ],
199             an => [ N => q{Netherlands Antilles} ],
200             aw => [ N => q{Aruba} ],
201             bb => [ N => q{Barbados} ],
202             bl => [ N => q(Saint Barthelemy) ],
203             bm => [ N => q{Bermuda} ],
204             bs => [ N => q{Bahamas} ],
205             bz => [ N => q{Belize} ],
206             ca => [ N => q{Canada} ],
207             cr => [ N => q{Costa Rica} ],
208             cu => [ N => q{Cuba} ],
209             dm => [ N => q{Dominica} ],
210             do => [ N => q{Dominican Republic} ],
211             gd => [ N => q{Grenada} ],
212             gl => [ N => q{Greenland} ],
213             gp => [ N => q{Guadeloupe} ],
214             gt => [ N => q{Guatemala} ],
215             hn => [ N => q{Honduras} ],
216             ht => [ N => q{Haiti} ],
217             jm => [ N => q{Jamaica} ],
218             kn => [ N => q{Saint Kitts and Nevis} ],
219             lc => [ N => q{Saint Lucia} ],
220             mf => [ N => q{Saint Martin (French part)} ],
221             mq => [ N => q{Martinique} ],
222             ms => [ N => q{Montserrat} ],
223             mx => [ N => q{Mexico} ],
224             ni => [ N => q{Nicaragua} ],
225             pa => [ N => q{Panama} ],
226             pm => [ N => q{St. Pierre and Miquelon} ],
227             pr => [ N => q{Puerto Rico} ],
228             sv => [ N => q{El Salvador} ],
229             tc => [ N => q{Turks and Caicos Islands} ],
230             tt => [ N => q{Trinidad and Tobago} ],
231             us => [ N => q{United States} ],
232             vc => [ N => q{Saint Vincent and the Grenadines} ],
233             vg => [ N => q{Virgin Islands (British)} ],
234             vi => [ N => q{Virgin Islands (USA)} ],
235              
236             as => [ O => q{American Samoa} ],
237             au => [ O => q{Australia} ],
238             cc => [ O => q{Cocos (Keeling) Islands} ],
239             ck => [ O => q{Cook Islands} ],
240             cx => [ O => q{Christmas Island} ],
241             fj => [ O => q{Fiji} ],
242             fm => [ O => q{Micronesia, Federated States of} ],
243             gu => [ O => q{Guam} ],
244             io => [ O => q{British Indian Ocean Territory} ],
245             ki => [ O => q{Kiribati} ],
246             ky => [ O => q{Cayman Islands} ],
247             mh => [ O => q{Marshall Islands} ],
248             mp => [ O => q{Northern Mariana Islands} ],
249             nc => [ O => q{New Caledonia} ],
250             nf => [ O => q{Norfolk Island} ],
251             nr => [ O => q{Nauru} ],
252             nu => [ O => q{Niue} ],
253             nz => [ O => q{New Zealand} ],
254             pf => [ O => q{French Polynesia} ],
255             pg => [ O => q{Papua New Guinea} ],
256             pn => [ O => q{Pitcairn Island} ],
257             pw => [ O => q{Palau} ],
258             sb => [ O => q{Solomon Islands} ],
259             tk => [ O => q{Tokelau} ],
260             to => [ O => q{Tonga} ],
261             tv => [ O => q{Tuvalu} ],
262             um => [ O => q{US Minor Outlying Islands} ],
263             vu => [ O => q{Vanuatu} ],
264             wf => [ O => q{Wallis and Futuna Islands} ],
265             ws => [ O => q{Western Samoa} ],
266              
267             aq => [ Q => q{Antartica} ],
268             bv => [ Q => q{Bouvet Island} ],
269             gs => [ Q => q{South Georgia and the South Sandwich Islands} ],
270             hm => [ Q => q{Heard and McDonald Islands} ],
271             tf => [ Q => q{French Southern Territories} ],
272              
273             ar => [ S => q{Argentina} ],
274             bo => [ S => q{Bolivia} ],
275             br => [ S => q{Brazil} ],
276             cl => [ S => q{Chile} ],
277             co => [ S => q{Colombia} ],
278             cw => [ S => q{Curacao} ],
279             ec => [ S => q{Ecuador} ],
280             fk => [ S => q{Falkland Islands (Malvina)} ],
281             gf => [ S => q{French Guiana} ],
282             gy => [ S => q{Guyana} ],
283             pe => [ S => q{Peru} ],
284             py => [ S => q{Paraguay} ],
285             sr => [ S => q{Suriname} ],
286             uy => [ S => q{Uruguay} ],
287             ve => [ S => q{Venezuela} ],
288             );
289              
290             #pod =head1 NAME
291             #pod
292             #pod Net::Continental - IP addresses of the world, by country and continent
293             #pod
294             #pod =head1 METHODS
295             #pod
296             #pod =head2 zone
297             #pod
298             #pod # Get the zone for the US.
299             #pod my $zone = Net::Continental->zone('us');
300             #pod
301             #pod This returns a L object for the given ISO code.
302             #pod
303             #pod =cut
304              
305             my %tld_for_code = (gb => 'uk');
306             my %code_for_tld = reverse %tld_for_code;
307              
308             sub zone {
309 7     7 1 2614 my ($self, $code) = @_;
310              
311 7 100       30 unless (exists $zone{$code}) {
312             $code = $code_for_tld{$code}
313             if exists $code_for_tld{$code}
314 2 100 66     17 && exists $zone{ $code_for_tld{ $code } };
315             }
316              
317 7 100       105 Carp::croak("unknown code $code") unless exists $zone{$code};
318              
319 6 100       24 unless (blessed $zone{ $code }) {
320             $zone{ $code } = Net::Continental::Zone->_new([
321             $code,
322 5         47 @{ $zone{ $code } },
323 5   66     9 $tld_for_code{ $code } || $code,
324             ])
325             }
326              
327 6         30 return $zone{ $code };
328             }
329              
330             #pod =head2 zone_for_nerd_ip
331             #pod
332             #pod # get the zone for nerd's response for the US
333             #pod my $zone = Net::Continental->zone_for_nerd_ip('127.0.3.72');
334             #pod
335             #pod =cut
336              
337             sub zone_for_nerd_ip {
338 2     2 1 864 my ($self, $ip) = @_;
339              
340 2         6 my ($matched, $top, $bot);
341 2         4 $matched = do {
342 1     1   10 no warnings 'uninitialized';
  1         2  
  1         281  
343 2         12 ($top, $bot) = $ip =~ /\A127\.0\.([0-9]+)\.([0-9]+)\z/;
344             };
345              
346 2 100       9 unless ($matched) {
347 1 50       6 my $str = defined $ip ? $ip : '(undef)';
348 1         188 Carp::croak("invalid input to zone_for_nerd_ip: $str");
349             }
350              
351 1         5 my $cc = ($top << 8) + $bot;
352              
353 1         5 my $code = Locale::Codes::Country::country_code2code(
354             $cc,
355             'numeric',
356             'alpha-2',
357             );
358              
359 1 50       163 Carp::croak("unknown nerd ip $ip") unless $code;
360              
361 1         5 return $self->zone($code);
362             }
363              
364             #pod =head2 known_zone_codes
365             #pod
366             #pod my @codes = Net::Continental->known_zone_codes;
367             #pod
368             #pod This returns a list of all known zone codes, in no particular order.
369             #pod
370             #pod =cut
371              
372             sub known_zone_codes {
373 0     0 1   return keys %zone
374             }
375              
376             #pod =head1 AUTHOR
377             #pod
378             #pod This code was written in 2009 by Ricardo SIGNES.
379             #pod
380             #pod The development of this code was sponsored by Pobox.com. Thanks, Pobox!
381             #pod
382             #pod =cut
383              
384              
385             1;
386              
387             __END__