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   26737 use strict;
  1         4  
  1         58  
2 1     1   8 use warnings;
  1         1  
  1         68  
3             package Net::Continental;
4             # ABSTRACT: code to map countries to continents, esp. with nerd.dk dnsbl
5             $Net::Continental::VERSION = '0.015';
6 1     1   7 use Carp ();
  1         2  
  1         18  
7 1     1   752 use Locale::Codes::Country ();
  1         41698  
  1         34  
8 1     1   606 use Net::Continental::Zone;
  1         3  
  1         50  
9 1     1   12 use Scalar::Util qw(blessed);
  1         2  
  1         1135  
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             ec => [ S => q{Ecuador} ],
279             fk => [ S => q{Falkland Islands (Malvina)} ],
280             gf => [ S => q{French Guiana} ],
281             gy => [ S => q{Guyana} ],
282             pe => [ S => q{Peru} ],
283             py => [ S => q{Paraguay} ],
284             sr => [ S => q{Suriname} ],
285             uy => [ S => q{Uruguay} ],
286             ve => [ S => q{Venezuela} ],
287             );
288              
289             #pod =head1 NAME
290             #pod
291             #pod Net::Continental - IP addresses of the world, by country and continent
292             #pod
293             #pod =head1 METHODS
294             #pod
295             #pod =head2 zone
296             #pod
297             #pod # Get the zone for the US.
298             #pod my $zone = Net::Continental->zone('us');
299             #pod
300             #pod This returns a L object for the given ISO code.
301             #pod
302             #pod =cut
303              
304             my %tld_for_code = (gb => 'uk');
305             my %code_for_tld = reverse %tld_for_code;
306              
307             sub zone {
308 7     7 1 2195 my ($self, $code) = @_;
309              
310 7 100       25 unless (exists $zone{$code}) {
311 2 100 66     12 $code = $code_for_tld{$code}
312             if exists $code_for_tld{$code}
313             && exists $zone{ $code_for_tld{ $code } };
314             }
315              
316 7 100       97 Carp::croak("unknown code $code") unless exists $zone{$code};
317              
318 6 100       27 unless (blessed $zone{ $code }) {
319 5         43 $zone{ $code } = Net::Continental::Zone->_new([
320             $code,
321 5   66     5 @{ $zone{ $code } },
322             $tld_for_code{ $code } || $code,
323             ])
324             }
325              
326 6         14 return $zone{ $code };
327             }
328              
329             #pod =head2 zone_for_nerd_ip
330             #pod
331             #pod # get the zone for nerd's response for the US
332             #pod my $zone = Net::Continental->zone_for_nerd_ip('127.0.3.72');
333             #pod
334             #pod =cut
335              
336             sub zone_for_nerd_ip {
337 2     2 1 733 my ($self, $ip) = @_;
338              
339 2         3 my ($matched, $top, $bot);
340 2         3 $matched = do {
341 1     1   7 no warnings 'uninitialized';
  1         1  
  1         256  
342 2         9 ($top, $bot) = $ip =~ /\A127\.0\.([0-9]+)\.([0-9]+)\z/;
343             };
344              
345 2 100       7 unless ($matched) {
346 1 50       5 my $str = defined $ip ? $ip : '(undef)';
347 1         160 Carp::croak("invalid input to zone_for_nerd_ip: $str");
348             }
349              
350 1         3 my $cc = ($top << 8) + $bot;
351              
352 1         4 my $code = Locale::Codes::Country::country_code2code(
353             $cc,
354             'numeric',
355             'alpha-2',
356             );
357              
358 1 50       94 Carp::croak("unknown nerd ip $ip") unless $code;
359              
360 1         4 return $self->zone($code);
361             }
362              
363             #pod =head2 known_zone_codes
364             #pod
365             #pod my @codes = Net::Continental->known_zone_codes;
366             #pod
367             #pod This returns a list of all known zone codes, in no particular order.
368             #pod
369             #pod =cut
370              
371             sub known_zone_codes {
372 0     0 1   return keys %zone
373             }
374              
375             #pod =head1 AUTHOR
376             #pod
377             #pod This code was written in 2009 by Ricardo SIGNES.
378             #pod
379             #pod The development of this code was sponsored by Pobox.com. Thanks, Pobox!
380             #pod
381             #pod =cut
382              
383              
384             1;
385              
386             __END__