File Coverage

blib/lib/IP/Country/Medium.pm
Criterion Covered Total %
statement 29 47 61.7
branch 5 24 20.8
condition 4 15 26.6
subroutine 8 10 80.0
pod 1 5 20.0
total 47 101 46.5


line stmt bran cond sub pod time code
1             package IP::Country::Medium;
2 2     2   7061 use strict;
  2         5  
  2         85  
3 2     2   13 use Carp;
  2         6  
  2         161  
4 2     2   1159 use Socket qw ( inet_aton inet_ntoa AF_INET );
  2         4648  
  2         310  
5 2     2   630 use IP::Country::Fast;
  2         6  
  2         87  
6              
7 2     2   15 use vars qw ( $VERSION );
  2         11  
  2         1441  
8             $VERSION = '0.05';
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 2     2 0 18 my $caller = shift;
22 2 50       16 unless (defined $singleton){
23 2   33     14 my $class = ref($caller) || $caller;
24 2         8 $singleton = bless {}, $class;
25             }
26 2         9 return $singleton;
27             }
28              
29             sub cache
30             {
31 0 0   0 1 0 my $bool = defined $_[1] ? $_[1] : $_[0];
32 0 0       0 if ($bool){
33 0         0 $cache = 1;
34             } else {
35 0         0 $cache = 0;
36 0         0 %cache = ();
37             }
38             }
39              
40             sub db_time
41             {
42 0     0 0 0 return 0;
43             }
44              
45             sub inet_atocc
46             {
47 29   33 29 0 6015 my $hostname = $_[1] || $_[0];
48 29 50       248 if ($hostname =~ $ip_match){
49             # IP address
50 29         122 return inet_ntocc(inet_aton($hostname));
51             } else {
52             # assume domain name
53 0 0 0     0 if ($cache && exists $cache{$hostname}){
54 0         0 return $cache{$hostname};
55             } else {
56 0 0       0 if ($hostname =~ $tld_match){
57 0         0 return uc($1);
58             } else {
59 0         0 my $cc = IP::Country::Fast::inet_atocc($hostname);
60 0 0       0 $cache{$hostname} = $cc if $cache;
61 0         0 return $cc;
62             }
63             }
64             }
65             }
66              
67             sub inet_ntocc
68             {
69 29   33 29 0 87 my $ip_addr = $_[1] || $_[0];
70 29 50 33     121 if ($cache && exists $cache{$ip_addr}){
71 0         0 return $cache{$ip_addr};
72             } else {
73 29         129 my $ip_dotted = inet_ntoa($ip_addr);
74 29 50       132 return undef if ($ip_dotted =~ $private_ip);
75              
76 29 50       82 if (my $cc = IP::Country::Fast::inet_ntocc($ip_addr)){
    0          
77 29         137 return $cc;
78             } elsif (gethostbyaddr($ip_addr, AF_INET) =~ $tld_match){
79 0           my $cc = uc($1);
80 0 0         $cache{$ip_addr} = $cc if $cache;
81 0           return $cc;
82             } else {
83             }
84             }
85 0           return undef;
86             }
87              
88             1;
89             __END__