blib/lib/Net/IPAddress/Filter/IPFilterDat.pm

filter: Statementstmt 100% 41 / 41 Branchbran 91.6% 11 / 12 Conditioncond 50.0% 5 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 87.3% 69 / 79 CC 11 SCAR 15.1
CC
11
Cov
90%
load_file
8.4
add_rule
3.0
_parse_rule
2.0
CRAP 4.5
↑ Summary
1 package Net::IPAddress::Filter::IPFilterDat;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: A fast IP address filter from ipfilter.dat
7 2 our $VERSION = '20121119.02'; # VERSION
8  
9 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Net::IPAddress::Filter 20121117;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use base qw( Net::IPAddress::Filter );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # 000.000.000.000 - 000.255.255.255 , 000 , invalid ip
18 2 RULE_REGEX => qr{
19 \A \s*
20 ([0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3}) # Start IP address
21 \s* - \s*
22 ([0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3}) # End IP address
23 \s* , \s*
24 (\d+) # Score
25 \s* , \s*
26 (.*?) # Label
27 \s* \z
28 }xms,
29 2 };
30  
31  
32 2 ▶sub load_file {
Subroutine load_file: called CC=8 SCAR=21.3
Pod documented
33 2 my $self = shift;
34 2 ▶ my $file = shift || return;
Condition50%1/2
shift() || (return)
l!l
20
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
35  
36 2 my $FH;
37  
38 # A filehandle can be a GLOB ref, or a blessed ref to one of the IO::
39 # packages. reftype() handles boths cases.
40 2 ▶ if ( ref($file)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file and builtin::reftype($file) eq 'GLOB' || builtin::reftype(\$file) eq 'GLOB') { } 1 1 2
Condition50%3/6
ref $file and builtin::reftype($file) eq "GLOB" || builtin::reftype(\$file) eq "GLOB"
!ll&&!rl&&r
101
builtin::reftype($file) eq "GLOB" or builtin::reftype(\$file) eq "GLOB"
l!l&&r!l&&!r
100
MC/DC33%1/3
ref $file and builtin::reftype($file) eq "GLOB" || builtin::reftype(\$file) eq "GLOB"
ref $file builtin::reftype($file) eq "GLOB" builtin::reftype(\$file) eq "GLOB"
Truth table50%2/4
A and B or C
A= ref $fileB= builtin::reftype($file) eq "GLOB"C= builtin::reftype(\$file) eq "GLOB"
ABCresult
0XX0
1000
11X1
1011
41 && ( Scalar::Util::reftype($file) eq 'GLOB' || Scalar::Util::reftype( \$file ) eq 'GLOB' ) )
42 {
43 1 $FH = $file;
44 }
45 else {
46 1 ▶ open $FH, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open $FH, "<", $file or die 'Net::IPAddress::Filter::IPFilterDat' . "::load_file() unable to open $file for reading: $!" 1 0 1
47 or die __PACKAGE__ . "::load_file() unable to open $file for reading: $!";
48 }
49  
50 2 my $rules_added = 0;
51 2 ▶ while ( my $line = <$FH> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $FH)) 5 2 7
52 5 ▶ $rules_added++ if $self->add_rule($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->add_rule($line) 3 2 5
53 }
54  
55 2 close $FH;
56  
57 2 return $rules_added;
58 }
59  
60  
61 7 ▶sub add_rule {
Subroutine add_rule: called CC=3 SCAR=11.0
Pod documented
62 7 my $self = shift;
63 7 ▶ my $rule = shift || return 0;
Condition50%1/2
shift() || (return 0)
l!l
70
MC/DC0%0/1
shift() || (return 0)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
64  
65 7 ▶ if ( my $data = _parse_rule($rule) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = _parse_rule($rule)) 5 2 7
66 5 $self->add_range_with_value( $data->{label}, $data->{start_ip}, $data->{end_ip} );
67 5 return 1;
68 }
69  
70 2 return 0;
71 }
72  
73  
74 13 ▶sub _parse_rule {
Subroutine _parse_rule: called CC=2 SCAR=6.9
75 13 my $rule = shift;
76  
77 # 000.000.000.000 - 000.255.255.255 , 000 , invalid ip
78 13 ▶ if ( $rule =~ RULE_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ / \A \s* ([0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3}) # Start IP address \s* - \s* ([0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3} \. [0-9]{1,3}) # End IP address \s* , \s* (\d+) # Score \s* , \s* (.*?) # Label \s* \z /msx) 9 4 13
79 return {
80 9 start_ip => $1,
81 end_ip => $2,
82 score => $3,
83 label => $4,
84 };
85 }
86 4 return;
87 }
88  
89 2 1;
90  
91 __END__
↑ Summary