Set.pm

filter: Statementstmt 92.4% 220 / 238 Branchbran 86.7% 59 / 68 Conditioncond 58.3% 21 / 36 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 87.7% 43 / 49 Podpod 96.1% 25 / 26 totaltotal 84.9% 372 / 438 CC 53 SCAR 13.9
CC
53
Cov
88%
new
10.1
equals
9.5
__ANON__
6.0
CRAP 4.0
« Set/IPv4.pm ↑ Summary
1 package Net::CIDR::Set;
2  
3 # ABSTRACT: Manipulate sets of IP addresses
4  
5 9 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Carp qw( croak confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Net::CIDR::Set::IPv4;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Net::CIDR::Set::IPv6;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use overload '""' => 'as_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 our $VERSION = '0.22';
17  
18  
19 {
20 9 my %type_map = (
21 ipv4 => 'Net::CIDR::Set::IPv4',
22 ipv6 => 'Net::CIDR::Set::IPv6',
23 );
24  
25 sub new {
26 113 ▶ my $self = shift;
Subroutine new: called CC=8 SCAR=23.2
Pod documented
27 113 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
16970
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
28 113 my $set = bless { ranges => [] }, $class;
29 113 ▶ my $opt = 'HASH' eq ref $_[0] ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_[0] ? : 0 113 113
30 113 ▶ if ( defined( my $type = delete $opt->{type} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $type = delete $opt->{'type'})) { } 0 113 113
elsif (ref $self) { } 16 97 113
31 0 ▶ my $coder_class = $type_map{$type} || $type;
Condition0%0/3
$type_map{$type} || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$type_map{$type} || $type
$type_map{$type} $type
Truth table0%0/3
A || B
A= $type_map{$type}B= $type
ABresult
1X1
011
000
32 0 $set->{coder} = $coder_class->new;
33 }
34 elsif ( ref $self ) {
35 16 $set->{coder} = $self->{coder};
36 }
37 113 my @unk = keys %$opt;
38 113 ▶ croak "Unknown options: ", _and( sort @unk ) if @unk;
Branch: 1 branch
BranchTrueFalseTotal
if @unk 0 113 113
39 113 ▶ $set->add( @_ ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 86 27 113
40 110 return $set;
41 }
42 }
43  
44 # Return the index of the first element >= the supplied value. If the
45 # supplied value is larger than any element in the list the returned
46 # value will be equal to the size of the list.
47 9 sub _find_pos {
48 330 ▶ my $self = shift;
Subroutine _find_pos: called CC=5 SCAR=16.1
49 330 my $val = shift;
50 330 ▶ my $low = shift || 0;
Condition100%2/2
shift() || 0
l!l
68262
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
51  
52 330 my $high = scalar( @{ $self->{ranges} } );
53  
54 330 ▶ while ( $low < $high ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low < $high) 192 278 470
55 192 my $mid = int( ( $low + $high ) / 2 );
56 192 my $cmp = $val cmp $self->{ranges}[$mid];
57 192 ▶ if ( $cmp < 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp < 0) { } 43 149 192
elsif ($cmp > 0) { } 97 52 149
58 43 $high = $mid;
59 }
60 elsif ( $cmp > 0 ) {
61 97 $low = $mid + 1;
62 }
63 else {
64 52 return $mid;
65 }
66 }
67  
68 278 return $low;
69 }
70  
71 sub _inc {
72 681 ▶ my @b = reverse unpack 'C*', shift;
Subroutine _inc: called CC=3 SCAR=11.0
73 681 for ( @b ) {
74 2999 ▶ last unless ++$_ == 256;
Branch: 1 branch
BranchTrueFalseTotal
unless ++$_ == 256 680 2319 2999
75 2319 $_ = 0;
76 }
77 681 return pack 'C*', reverse @b;
78 }
79  
80 sub _dec {
81 158 ▶ my @b = reverse unpack 'C*', shift;
Subroutine _dec: called CC=3 SCAR=11.0
82 158 for ( @b ) {
83 605 ▶ last unless $_-- == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $_-- == 0 157 448 605
84 448 $_ = 255;
85 }
86 158 return pack 'C*', reverse @b;
87 }
88  
89 sub _guess_coder {
90 96 ▶ my ( $self, $ip ) = @_;
Subroutine _guess_coder: called CC=3 SCAR=11.0
91 96 for my $class ( qw( Net::CIDR::Set::IPv4 Net::CIDR::Set::IPv6 ) ) {
92 137 my $coder = $class->new;
93 137 ( eval { $coder->encode( $ip ) } );
94 137 ▶ return $coder unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 88 49 137
95 }
96 8 croak "Can't decode $ip as an IPv4 or IPv6 address";
97 }
98  
99 sub _encode {
100 159 ▶ my ( $self, $ip ) = @_;
Subroutine _encode: called CC=2 SCAR=6.9
101 159 ▶ my $cdr = $self->{coder} ||= $self->_guess_coder( $ip );
Condition100%3/3
$self->{"coder"} ||= $self->_guess_coder($ip)
l!l&&r!l&&!r
63924
MC/DC100%2/2
$self->{"coder"} ||= $self->_guess_coder($ip)
$self->{"coder"} $self->_guess_coder($ip)
Truth table100%3/3
A ||= B
A= $self->{"coder"}B= $self->_guess_coder($ip)
ABresult
1X1
011
000
102 151 return $cdr->encode( $ip );
103 }
104  
105 {
106 9 for my $dele ( qw( _decode _nbits ) ) {
107 9 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
108 18 my $meth = $dele =~ s/^_//r;
109 18 *{$dele} = sub {
110 107 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.6
111 107 ▶ my $cdr = $self->{coder} || croak "Don't know how to $meth yet";
Condition33%1/3
$self->{'coder'} || croak("Don't know how to $meth yet")
l!l&&r!l&&!r
10700
MC/DC0%0/2
$self->{'coder'} || croak("Don't know how to $meth yet")
$self->{'coder'} croak("Don't know how to $meth yet")
Truth table33%1/3
A || B
A= $self->{'coder'}B= croak("Don't know how to $meth yet")
ABresult
1X1
011
000
112 107 return $cdr->$meth( @_ );
113 18 };
114 }
115 }
116  
117 sub _conjunction {
118 2 ▶ my ( $conj, @list ) = @_;
Subroutine _conjunction: called CC=1 SCAR=0.0
119 2 my $last = pop @list;
120 2 return join " $conj ", join( ', ', @list ), $last;
121 }
122  
123 1 ▶sub _and { _conjunction( 'and', @_ ) }
Subroutine _and: called CC=1 SCAR=0.0
124  
125 sub _check_and_coerce {
126 9 ▶ my ( $self, @others ) = @_;
Subroutine _check_and_coerce: called CC=5 SCAR=16.3
127  
128 my %class = map {
129 20 ▶ eval { ( defined $_ && $_->nbits || '' ) => $_ }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_->nbits or "" 4 16 20
Condition66%2/3
defined $_ and $_->nbits
!ll&&!rl&&r
4016
MC/DC50%1/2
defined $_ and $_->nbits
defined $_ $_->nbits
Truth table66%2/3
A and B
A= defined $_B= $_->nbits
ABresult
0X0
100
111
130 9 } map { $_->{coder} } grep { defined } $self, @others;
131  
132 9 my @found = sort grep $_, keys %class;
133  
134 9 ▶ croak "Can't mix ", _and( @found ), " bit addresses"
Branch: 1 branch
BranchTrueFalseTotal
if @found > 1 0 9 9
135 if @found > 1;
136  
137 9 ▶ $self->{coder} ||= $class{ $found[0] };
Condition66%2/3
$self->{"coder"} ||= $class{$found[0]}
l!l&&r!l&&!r
540
MC/DC0%0/2
$self->{"coder"} ||= $class{$found[0]}
$self->{"coder"} $class{$found[0]}
Truth table66%2/3
A ||= B
A= $self->{"coder"}B= $class{$found[0]}
ABresult
1X1
011
000
138 9 return $self;
139 }
140  
141  
142 sub invert {
143 15 ▶ my $self = shift;
Subroutine invert: called CC=4 SCAR=14.0
Pod documented
144  
145 15 my @pad = ( 0 ) x ( $self->_nbits / 8 );
146 15 my ( $min, $max ) = map { pack 'C*', $_, @pad } 0, 1;
147  
148 15 ▶ if ( $self->is_empty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_empty) 0 15 15
149 0 $self->{ranges} = [ $min, $max ];
150 0 return;
151 }
152  
153 15 ▶ if ( $self->{ranges}[0] eq $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ranges'}[0] eq $min) { } 8 7 15
154 8 shift @{ $self->{ranges} };
155 }
156 else {
157 7 unshift @{ $self->{ranges} }, $min;
158 }
159  
160 15 ▶ if ( $self->{ranges}[-1] eq $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ranges'}[-1] eq $max) { } 8 7 15
161 8 pop @{ $self->{ranges} };
162 }
163 else {
164 7 push @{ $self->{ranges} }, $max;
165 }
166 }
167  
168  
169 sub copy {
170 16 ▶ my $self = shift;
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
171 16 my $copy = $self->new;
172 16 @{ $copy->{ranges} } = @{ $self->{ranges} };
173 16 return $copy;
174 }
175  
176 sub _add_range {
177 165 ▶ my ( $self, $from, $to ) = @_;
Subroutine _add_range: called CC=3 SCAR=11.0
178 165 my $fpos = $self->_find_pos( $from );
179 165 my $tpos = $self->_find_pos( _inc( $to ), $fpos );
180  
181 165 ▶ $from = $self->{ranges}[ --$fpos ] if ( $fpos & 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $fpos & 1 52 113 165
182 165 ▶ $to = $self->{ranges}[ $tpos++ ] if ( $tpos & 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $tpos & 1 5 160 165
183  
184 165 splice @{ $self->{ranges} }, $fpos, $tpos - $fpos, ( $from, $to );
185 }
186  
187  
188 sub add {
189 99 ▶ my ( $self, @addr ) = @_;
Subroutine add: called CC=3 SCAR=11.1
Pod documented
190 99 for my $ip ( map { split /\s*,\s*/ } @addr ) {
191 159 ▶ my ( $lo, $hi ) = $self->_encode( $ip )
Branch: 1 branch
BranchTrueFalseTotal
() = $self->_encode($ip) or croak("Can't decode $ip") 0 149 149
192 or croak "Can't decode $ip";
193 149 $self->_add_range( $lo, $hi );
194 }
195 }
196  
197  
198 sub remove {
199 1 ▶ my $self = shift;
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
200  
201 1 $self->invert;
202 1 $self->add( @_ );
203 1 $self->invert;
204 }
205  
206  
207 sub merge {
208 9 ▶ my $self = shift;
Subroutine merge: called CC=3 SCAR=11.0
Pod documented
209 9 $self->_check_and_coerce( @_ );
210  
211 # TODO: This isn't very efficient - and merge gets called from all
212 # sorts of other places.
213 9 for my $other ( @_ ) {
214 11 my $iter = $other->_iterate_runs;
215 11 ▶ while ( my ( $from, $to ) = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iter()) 16 11 27
216 16 $self->_add_range( $from, $to );
217 }
218 }
219 }
220  
221  
222 9 *contains = *contains_all;
223  
224 sub contains_all {
225 1 ▶ my $self = shift;
Subroutine contains_all: called CC=1 SCAR=0.0
Pod documented
226 1 my $class = ref $self;
227 1 return $class->new( @_ )->subset( $self );
228 }
229  
230  
231 sub contains_any {
232 0 ▶ my $self = shift;
Subroutine contains_any: not called CC=1 SCAR=6.9
Pod documented
233 0 my $class = ref $self;
234 0 return !$class->new( @_ )->intersection( $self )->is_empty;
235 }
236  
237 sub _iterate_runs {
238 62 ▶ my $self = shift;
Subroutine _iterate_runs: called CC=1 SCAR=0.0
239  
240 62 my $pos = 0;
241 62 my $limit = scalar( @{ $self->{ranges} } );
242  
243 return sub {
244 147 ▶ return if $pos >= $limit;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $limit 62 85 147
245 85 my @r = @{ $self->{ranges} }[ $pos, $pos + 1 ];
246 85 $pos += 2;
247 85 return @r;
248 62 };
249 }
250  
251 sub compliment {
252 0 ▶ croak "That's very kind of you - but I expect you meant complement";
Subroutine compliment: not called CC=1 SCAR=6.9
Pod undocumented
253 }
254  
255  
256 sub complement {
257 10 ▶ my $new = shift->copy;
Subroutine complement: called CC=1 SCAR=0.0
Pod documented
258 # TODO: What if it's empty?
259 10 $new->invert;
260 10 return $new;
261 }
262  
263  
264 sub union {
265 6 ▶ my $new = shift->copy;
Subroutine union: called CC=1 SCAR=0.0
Pod documented
266 6 $new->merge( @_ );
267 6 return $new;
268 }
269  
270  
271 sub intersection {
272 3 ▶ my $self = shift;
Subroutine intersection: called CC=1 SCAR=0.0
Pod documented
273 3 my $class = ref $self;
274 3 my $new = $class->new;
275 3 $new->merge( map { $_->complement } $self, @_ );
276 3 $new->invert;
277 3 return $new;
278 }
279  
280  
281 sub xor {
282 0 ▶ my $self = shift;
Subroutine xor: not called CC=1 SCAR=6.9
Pod documented
283 0 return $self->union( @_ )
284 ->intersection( $self->intersection( @_ )->complement );
285 }
286  
287  
288 sub diff {
289 0 ▶ my $self = shift;
Subroutine diff: not called CC=1 SCAR=6.9
Pod documented
290 0 my $other = shift;
291 0 return $self->intersection( $other->union( @_ )->complement );
292 }
293  
294  
295 sub is_empty {
296 16 ▶ my $self = shift;
Subroutine is_empty: called CC=1 SCAR=0.0
Pod documented
297 16 return @{ $self->{ranges} } == 0;
298 }
299  
300  
301 sub superset {
302 0 ▶ my $other = pop;
Subroutine superset: not called CC=1 SCAR=6.9
Pod documented
303 0 return $other->subset( reverse( @_ ) );
304 }
305  
306  
307 sub subset {
308 0 ▶ my $self = shift;
Subroutine subset: not called CC=2 SCAR=17.9
Pod documented
309 0 ▶ my $other = shift || croak "I need two sets to compare";
Condition0%0/3
shift() || croak('I need two sets to compare')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || croak('I need two sets to compare')
shift() croak('I need two sets to compare')
Truth table0%0/3
A || B
A= shift()B= croak('I need two sets to compare')
ABresult
1X1
011
000
310 0 return $self->equals( $self->intersection( $other ) );
311 }
312  
313  
314 sub equals {
315 36 ▶ return unless @_;
Subroutine equals: called CC=9 SCAR=22.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 36 36
316  
317 # Array of array refs
318 36 my @edges = map { $_->{ranges} } @_;
319 36 my $medge = scalar( @edges ) - 1;
320  
321 36 POS: for ( my $pos = 0;; $pos++ ) {
322 114 my $v = $edges[0]->[$pos];
323 114 ▶ if ( defined( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 78 36 114
324 78 for ( @edges[ 1 .. $medge ] ) {
325 78 my $vv = $_->[$pos];
326 78 ▶ return unless defined( $vv ) && $vv eq $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vv and $vv eq $v 0 78 78
Condition33%1/3
defined $vv and $vv eq $v
!ll&&!rl&&r
0078
MC/DC0%0/2
defined $vv and $vv eq $v
defined $vv $vv eq $v
Truth table33%1/3
A and B
A= defined $vvB= $vv eq $v
ABresult
0X0
100
111
327 }
328 }
329 else {
330 36 for ( @edges[ 1 .. $medge ] ) {
331 36 ▶ return if defined $_->[$pos];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->[$pos] 0 36 36
332 }
333 }
334  
335 114 ▶ last POS unless defined( $v );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 36 78 114
336 }
337  
338 36 return 1;
339 }
340  
341  
342 sub iterate_addresses {
343 1 ▶ my ( $self, @args ) = @_;
Subroutine iterate_addresses: called CC=1 SCAR=0.0
Pod documented
344 1 my $iter = $self->_iterate_runs;
345 1 my @r = ();
346 return sub {
347 2 ▶ while ( 1 ) {
Subroutine __ANON__: called CC=4 SCAR=13.9
348 3 ▶ @r = $iter->() or return unless @r;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 2 1 3
Condition80%4/5
@r || ((@r = &$iter()) || (return))
l!l&&r!l&&!r
120
@r = &$iter() or return
l!l
11
MC/DC0%0/2
@r || ((@r = &$iter()) || (return))
@r @r = &$iter()
Truth table66%2/3
A || B
A= @rB= @r = &$iter()
ABresult
1X1
011
000
349 2 ▶ return $self->_decode( ( my $last, $r[0] )
Branch: 1 branch
BranchTrueFalseTotal
unless $r[0] eq $r[1] 1 1 2
350 = ( $r[0], _inc( $r[0] ) ), @args )
351 unless $r[0] eq $r[1];
352 1 @r = ();
353 }
354 1 };
355 }
356  
357  
358 sub iterate_cidr {
359 8 ▶ my ( $self, @args ) = @_;
Subroutine iterate_cidr: called CC=1 SCAR=0.0
Pod documented
360 8 my $iter = $self->_iterate_runs;
361 8 my $size = $self->_nbits;
362 8 my @r = ();
363 return sub {
364 39 ▶ while ( 1 ) {
Subroutine __ANON__: called CC=6 SCAR=17.9
365 55 ▶ @r = $iter->() or return unless @r;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 24 31 55
Condition80%4/5
@r || ((@r = &$iter()) || (return))
l!l&&r!l&&!r
31240
@r = &$iter() or return
l!l
168
MC/DC0%0/2
@r || ((@r = &$iter()) || (return))
@r @r = &$iter()
Truth table66%2/3
A || B
A= @rB= @r = &$iter()
ABresult
1X1
011
000
366 47 ▶ unless ( $r[0] eq $r[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r[0] eq $r[1]) 31 16 47
367 31 ( my $bits = unpack 'B*', $r[0] ) =~ /(0*)$/;
368 31 my $pad = length $1;
369 31 ▶ $pad = $size if $pad > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $pad > $size 3 28 31
370 31 while ( 1 ) {
371 205 my $next = _inc( $r[0] | pack 'B*',
372 ( '0' x ( length( $bits ) - $pad ) ) . ( '1' x $pad ) );
373 205 ▶ return $self->_decode( ( my $last, $r[0] ) = ( $r[0], $next ),
Branch: 1 branch
BranchTrueFalseTotal
if $next le $r[1] 31 174 205
374 @args )
375 if $next le $r[1];
376 174 $pad--;
377 }
378 }
379 16 @r = ();
380 }
381 8 };
382 }
383  
384  
385 sub iterate_ranges {
386 42 ▶ my ( $self, @args ) = @_;
Subroutine iterate_ranges: called CC=1 SCAR=0.0
Pod documented
387 42 my $iter = $self->_iterate_runs;
388 return sub {
389 94 ▶ return unless my @r = $iter->();
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless my(@r) = &$iter() 42 52 94
390 52 return $self->_decode( @r, @args );
391 42 };
392 }
393  
394  
395 sub as_array {
396 51 ▶ my ( $self, $iter ) = @_;
Subroutine as_array: called CC=2 SCAR=6.9
Pod documented
397 51 my @addr = ();
398 51 ▶ while ( my $addr = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addr = &$iter()) 84 51 135
399 84 push @addr, $addr;
400 }
401 51 return @addr;
402 }
403  
404  
405 sub as_address_array {
406 1 ▶ my $self = shift;
Subroutine as_address_array: called CC=1 SCAR=0.0
Pod documented
407 1 return $self->as_array( $self->iterate_addresses( @_ ) );
408 }
409  
410  
411 sub as_cidr_array {
412 8 ▶ my $self = shift;
Subroutine as_cidr_array: called CC=1 SCAR=0.0
Pod documented
413 8 return $self->as_array( $self->iterate_cidr( @_ ) );
414 }
415  
416  
417 sub as_range_array {
418 42 ▶ my $self = shift;
Subroutine as_range_array: called CC=1 SCAR=0.0
Pod documented
419 42 return $self->as_array( $self->iterate_ranges( @_ ) );
420 }
421  
422  
423 36 ▶sub as_string { join ', ', shift->as_range_array( @_ ) }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
424  
425 9 1;
426  
427 __END__
« Set/IPv4.pm ↑ Summary