Country/Slow.pm

filter: Statementstmt 46.6% 28 / 60 Branchbran 3.5% 1 / 28 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 5 totaltotal 27.9% 36 / 129 CC 20 SCAR 37.4
CC
20
Cov
29%
inet_ntocc
90.0
inet_atocc
56.0
cache
12.0
CRAP 42.2
↑ Summary Country/Medium.pm »
1 package IP::Country::Slow;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Socket qw ( inet_aton inet_ntoa AF_INET );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IP::Country::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use vars qw ( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $VERSION = '0.04';
9  
10 1 my $singleton = undef;
11  
12 1 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 1 my $private_ip = qr/^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.)/o; # RFC1918
14 1 my $tld_match = qr/\.([a-zA-Z][a-zA-Z])$/o;
15  
16 1 my %cache;
17 1 my $cache = 1; # cache is switched on
18  
19 sub new
20 1 ▶{
Subroutine new: called CC=3 SCAR=11.8
Pod undocumented
21 1 my $caller = shift;
22 1 ▶ unless (defined $singleton){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $singleton) 1 0 1
23 1 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
24 1 $singleton = bless {}, $class;
25 }
26 1 return $singleton;
27 }
28  
29 sub db_time
30 0 ▶{
Subroutine db_time: not called CC=1 SCAR=6.9
Pod undocumented
31 0 return 0;
32 }
33  
34 sub cache
35 0 ▶{
Subroutine cache: not called CC=3 SCAR=24.8
Pod undocumented
36 0 ▶ my $bool = defined $_[1] ? $_[1] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
37 0 ▶ if ($bool){
Branch: 1 branch
BranchTrueFalseTotal
if ($bool) { } 0 0 0
38 0 $cache = 1;
39 } else {
40 0 $cache = 0;
41 0 %cache = ();
42 }
43 }
44  
45 sub inet_atocc
46 0 ▶{
Subroutine inet_atocc: not called CC=7 SCAR=40.3
Pod undocumented
47 0 ▶ my $hostname = $_[1] || $_[0];
Condition0%0/3
$_[1] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] || $_[0]
$_[1] $_[0]
Truth table0%0/3
A || B
A= $_[1]B= $_[0]
ABresult
1X1
011
000
48 0 ▶ if ($hostname =~ $ip_match){
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname =~ /$ip_match/) { } 0 0 0
49 # IP address
50 0 return inet_ntocc(inet_aton($hostname));
51 } else {
52 # assume domain name
53 0 ▶ if ($cache && exists $cache{$hostname}){
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and exists $cache{$hostname}) { } 0 0 0
Condition0%0/3
$cache and exists $cache{$hostname}
!ll&&!rl&&r
000
MC/DC0%0/2
$cache and exists $cache{$hostname}
$cache exists $cache{$hostname}
Truth table0%0/3
A and B
A= $cacheB= exists $cache{$hostname}
ABresult
0X0
100
111
54 0 return $cache{$hostname};
55 } else {
56 0 ▶ if (my $cc = _get_cc_from_tld($hostname)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $cc = &_get_cc_from_tld($hostname)) { } 0 0 0
57 0 return $cc;
58 } else {
59 0 my $cc = IP::Country::Fast::inet_atocc($hostname);
60 0 ▶ $cache{$hostname} = $cc if $cache;
Branch: 1 branch
BranchTrueFalseTotal
if $cache 0 0 0
61 0 return $cc;
62 }
63 }
64 }
65 }
66  
67 sub inet_ntocc
68 0 ▶{
Subroutine inet_ntocc: not called CC=9 SCAR=45.0
Pod undocumented
69 0 ▶ my $ip_addr = $_[1] || $_[0];
Condition0%0/3
$_[1] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] || $_[0]
$_[1] $_[0]
Truth table0%0/3
A || B
A= $_[1]B= $_[0]
ABresult
1X1
011
000
70 0 ▶ if ($cache && exists $cache{$ip_addr}){
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and exists $cache{$ip_addr}) { } 0 0 0
Condition0%0/3
$cache and exists $cache{$ip_addr}
!ll&&!rl&&r
000
MC/DC0%0/2
$cache and exists $cache{$ip_addr}
$cache exists $cache{$ip_addr}
Truth table0%0/3
A and B
A= $cacheB= exists $cache{$ip_addr}
ABresult
0X0
100
111
71 0 return $cache{$ip_addr};
72 } else {
73 0 my $ip_dotted = inet_ntoa($ip_addr);
74 0 ▶ return undef if $ip_dotted =~ $private_ip;
Branch: 1 branch
BranchTrueFalseTotal
if $ip_dotted =~ /$private_ip/ 0 0 0
75 0 ▶ if (my $hostname = gethostbyaddr($ip_addr, AF_INET)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $hostname = gethostbyaddr($ip_addr, 2)) 0 0 0
76 0 ▶ if (my $cc = _get_cc_from_tld($hostname)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $cc = &_get_cc_from_tld($hostname)) 0 0 0
77 0 ▶ $cache{$ip_addr} = $cc if $cache;
Branch: 1 branch
BranchTrueFalseTotal
if $cache 0 0 0
78 0 return $cc;
79 }
80 }
81 0 my $cc = IP::Country::Fast::inet_ntocc($ip_addr);
82 0 ▶ $cache{$ip_addr} = $cc if $cache;
Branch: 1 branch
BranchTrueFalseTotal
if $cache 0 0 0
83 0 return $cc;
84 }
85 }
86  
87 sub _get_cc_from_tld ($)
88 0 ▶{
Subroutine _get_cc_from_tld: not called CC=2 SCAR=17.9
89 0 my $hostname = shift;
90 0 ▶ if ($hostname =~ $tld_match){
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname =~ /$tld_match/) { } 0 0 0
91 0 return uc $1;
92 } else {
93 0 return undef;
94 }
95 }
96  
97  
98 1 1;
99 __END__
↑ Summary Country/Medium.pm »