Country/Medium.pm

filter: Statementstmt 67.2% 37 / 55 Branchbran 20.8% 5 / 24 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 80.0% 8 / 10 Podpod 20.0% 1 / 5 totaltotal 46.2% 55 / 119 CC 18 SCAR 28.0
CC
18
Cov
49%
inet_atocc
31.3
inet_ntocc
21.3
cache
12.0
CRAP 16.4
« Country/Slow.pm ↑ Summary Country/MaxMind.pm »
1 package IP::Country::Medium;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Socket qw ( inet_aton inet_ntoa AF_INET );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use IP::Country::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use vars qw ( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 $VERSION = '0.05';
9  
10 2 my $singleton = undef;
11  
12 2 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 2 my $private_ip = qr/^(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.)/o; # RFC1918
14 2 my $tld_match = qr/\.([a-zA-Z][a-zA-Z])$/o;
15  
16 2 my %cache;
17 2 my $cache = 1; # cache is switched on
18  
19 sub new
20 2 ▶{
Subroutine new: called CC=3 SCAR=11.8
Pod undocumented
21 2 my $caller = shift;
22 2 ▶ unless (defined $singleton){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $singleton) 2 0 2
23 2 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
020
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 2 $singleton = bless {}, $class;
25 }
26 2 return $singleton;
27 }
28  
29 sub cache
30 0 ▶{
Subroutine cache: not called CC=3 SCAR=24.8
Pod documented
31 0 ▶ my $bool = defined $_[1] ? $_[1] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
32 0 ▶ if ($bool){
Branch: 1 branch
BranchTrueFalseTotal
if ($bool) { } 0 0 0
33 0 $cache = 1;
34 } else {
35 0 $cache = 0;
36 0 %cache = ();
37 }
38 }
39  
40 sub db_time
41 0 ▶{
Subroutine db_time: not called CC=1 SCAR=6.9
Pod undocumented
42 0 return 0;
43 }
44  
45 sub inet_atocc
46 29 ▶{
Subroutine inet_atocc: called CC=7 SCAR=34.4
Pod undocumented
47 29 ▶ my $hostname = $_[1] || $_[0];
Condition33%1/3
$_[1] || $_[0]
l!l&&r!l&&!r
2900
MC/DC0%0/2
$_[1] || $_[0]
$_[1] $_[0]
Truth table33%1/3
A || B
A= $_[1]B= $_[0]
ABresult
1X1
011
000
48 29 ▶ if ($hostname =~ $ip_match){
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname =~ /$ip_match/) { } 29 0 29
49 # IP address
50 29 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 ($hostname =~ $tld_match){
Branch: 1 branch
BranchTrueFalseTotal
if ($hostname =~ /$tld_match/) { } 0 0 0
57 0 return uc($1);
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 29 ▶{
Subroutine inet_ntocc: called CC=8 SCAR=30.6
Pod undocumented
69 29 ▶ my $ip_addr = $_[1] || $_[0];
Condition33%1/3
$_[1] || $_[0]
l!l&&r!l&&!r
0290
MC/DC0%0/2
$_[1] || $_[0]
$_[1] $_[0]
Truth table33%1/3
A || B
A= $_[1]B= $_[0]
ABresult
1X1
011
000
70 29 ▶ if ($cache && exists $cache{$ip_addr}){
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and exists $cache{$ip_addr}) { } 0 29 29
Condition33%1/3
$cache and exists $cache{$ip_addr}
!ll&&!rl&&r
0290
MC/DC0%0/2
$cache and exists $cache{$ip_addr}
$cache exists $cache{$ip_addr}
Truth table33%1/3
A and B
A= $cacheB= exists $cache{$ip_addr}
ABresult
0X0
100
111
71 0 return $cache{$ip_addr};
72 } else {
73 29 my $ip_dotted = inet_ntoa($ip_addr);
74 29 ▶ return undef if ($ip_dotted =~ $private_ip);
Branch: 1 branch
BranchTrueFalseTotal
if $ip_dotted =~ /$private_ip/ 0 29 29
75  
76 29 ▶ if (my $cc = IP::Country::Fast::inet_ntocc($ip_addr)){
Branch: 2 branches
BranchTrueFalseTotal
if (my $cc = IP::Country::Fast::inet_ntocc($ip_addr)) { } 29 0 29
elsif (gethostbyaddr($ip_addr, 2) =~ /$tld_match/) { } 0 0 0
77 29 return $cc;
78 } elsif (gethostbyaddr($ip_addr, AF_INET) =~ $tld_match){
79 0 my $cc = uc($1);
80 0 ▶ $cache{$ip_addr} = $cc if $cache;
Branch: 1 branch
BranchTrueFalseTotal
if $cache 0 0 0
81 0 return $cc;
82 } else {
83 }
84 }
85 0 return undef;
86 }
87  
88 2 1;
89 __END__
« Country/Slow.pm ↑ Summary Country/MaxMind.pm »