File Coverage

blib/lib/IP/Country/Slow.pm
Criterion Covered Total %
statement 20 52 38.4
branch 1 28 3.5
condition 1 15 6.6
subroutine 6 11 54.5
pod 0 5 0.0
total 28 111 25.2


line stmt bran cond sub pod time code
1             package IP::Country::Slow;
2 1     1   557 use strict;
  1         3  
  1         34  
3 1     1   4 use Carp;
  1         3  
  1         60  
4 1     1   4 use Socket qw ( inet_aton inet_ntoa AF_INET );
  1         3  
  1         53  
5 1     1   4 use IP::Country::Fast;
  1         1  
  1         73  
6              
7 1     1   11 use vars qw ( $VERSION );
  1         2  
  1         666  
8             $VERSION = '0.04';
9              
10             my $singleton = undef;
11              
12             my $ip_match = qr/^(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])\.(\d|[01]?\d\d|2[0-4]\d|25[0-5])$/o;
13             my $private_ip = qr/^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.)/o; # RFC1918
14             my $tld_match = qr/\.([a-zA-Z][a-zA-Z])$/o;
15              
16             my %cache;
17             my $cache = 1; # cache is switched on
18              
19             sub new
20             {
21 1     1 0 4 my $caller = shift;
22 1 50       4 unless (defined $singleton){
23 1   33     7 my $class = ref($caller) || $caller;
24 1         3 $singleton = bless {}, $class;
25             }
26 1         5 return $singleton;
27             }
28              
29             sub db_time
30             {
31 0     0 0   return 0;
32             }
33              
34             sub cache
35             {
36 0 0   0 0   my $bool = defined $_[1] ? $_[1] : $_[0];
37 0 0         if ($bool){
38 0           $cache = 1;
39             } else {
40 0           $cache = 0;
41 0           %cache = ();
42             }
43             }
44              
45             sub inet_atocc
46             {
47 0   0 0 0   my $hostname = $_[1] || $_[0];
48 0 0         if ($hostname =~ $ip_match){
49             # IP address
50 0           return inet_ntocc(inet_aton($hostname));
51             } else {
52             # assume domain name
53 0 0 0       if ($cache && exists $cache{$hostname}){
54 0           return $cache{$hostname};
55             } else {
56 0 0         if (my $cc = _get_cc_from_tld($hostname)){
57 0           return $cc;
58             } else {
59 0           my $cc = IP::Country::Fast::inet_atocc($hostname);
60 0 0         $cache{$hostname} = $cc if $cache;
61 0           return $cc;
62             }
63             }
64             }
65             }
66              
67             sub inet_ntocc
68             {
69 0   0 0 0   my $ip_addr = $_[1] || $_[0];
70 0 0 0       if ($cache && exists $cache{$ip_addr}){
71 0           return $cache{$ip_addr};
72             } else {
73 0           my $ip_dotted = inet_ntoa($ip_addr);
74 0 0         return undef if $ip_dotted =~ $private_ip;
75 0 0         if (my $hostname = gethostbyaddr($ip_addr, AF_INET)){
76 0 0         if (my $cc = _get_cc_from_tld($hostname)){
77 0 0         $cache{$ip_addr} = $cc if $cache;
78 0           return $cc;
79             }
80             }
81 0           my $cc = IP::Country::Fast::inet_ntocc($ip_addr);
82 0 0         $cache{$ip_addr} = $cc if $cache;
83 0           return $cc;
84             }
85             }
86              
87             sub _get_cc_from_tld ($)
88             {
89 0     0     my $hostname = shift;
90 0 0         if ($hostname =~ $tld_match){
91 0           return uc $1;
92             } else {
93 0           return undef;
94             }
95             }
96              
97              
98             1;
99             __END__