| 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
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | my $class = ref($caller) || $caller; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $caller || $caller
MC/DC0%0/2
ref $caller || $caller
ref $caller
$caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if ($bool){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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]
MC/DC0%0/2
$_[1] || $_[0]
$_[1]
$_[0]
Truth table33%1/3
A || B
A= $_[1]B= $_[0]
| |||||||||||||||||||||||||||||
| 48 | 29 | ▶ | if ($hostname =~ $ip_match){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$cache and exists $cache{$hostname}
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}
| |||||||||||||||||||||||||||||
| 54 | 0 | return $cache{$hostname}; | |||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($hostname =~ $tld_match){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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]
MC/DC0%0/2
$_[1] || $_[0]
$_[1]
$_[0]
Truth table33%1/3
A || B
A= $_[1]B= $_[0]
| |||||||||||||||||||||||||||||
| 70 | 29 | ▶ | if ($cache && exists $cache{$ip_addr}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cache and exists $cache{$ip_addr}
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 29 | ▶ | if (my $cc = IP::Country::Fast::inet_ntocc($ip_addr)){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 81 | 0 | return $cc; | |||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 0 | return undef; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | 1; | |||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||