| 1 | #!/bin/false | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Count addresses in named groups of subnets | ||||||||||||||||||||||||||||
| 3 | # PODNAME: NetAddr::IP::Count | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package NetAddr::IP::Count; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '4.080_09'; # TRIAL VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Scalar::Util qw(blessed looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # the cache size NetAddr::IP::Count 2.01 reported for a new counter | ||||||||||||||||||||||||||||
| 16 | 2 | my $DEFAULT_CACHE_SIZE = 5; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # what an argument must be, as the croak messages name it | ||||||||||||||||||||||||||||
| 19 | 2 | my $WANT_ADDRESS = q{a NetAddr::IP object}; | |||||||||||||||||||||||||||
| 20 | 2 | my $WANT_NUMBER = q{a number}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 21 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 21 | my ($proto, @pairs) = @_; | |||||||||||||||||||||||||||
| 24 | 21 | ▶ | croak sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | '%s::new takes group name and subnet pairs, got %d arguments', | ||||||||||||||||||||||||||||
| 26 | __PACKAGE__, scalar @pairs | ||||||||||||||||||||||||||||
| 27 | ) if @pairs % 2; | ||||||||||||||||||||||||||||
| 28 | 20 | my %groups = @pairs; | |||||||||||||||||||||||||||
| 29 | 20 | ▶ | my $self = bless { | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 30 | cache_size => $DEFAULT_CACHE_SIZE, | ||||||||||||||||||||||||||||
| 31 | subnets => {}, | ||||||||||||||||||||||||||||
| 32 | totals => {}, | ||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||
| 34 | ref $proto || $proto; | ||||||||||||||||||||||||||||
| 35 | 20 | $self->add($_, $groups{$_}) for keys %groups; | |||||||||||||||||||||||||||
| 36 | 19 | return $self; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 41 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 41 | my ($self, $name, @subnets) = @_; | |||||||||||||||||||||||||||
| 41 | 41 | ▶ | croak sprintf('%s::add needs a group name', __PACKAGE__) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | unless defined $name; | ||||||||||||||||||||||||||||
| 43 | 40 | ▶ | @subnets = @{ $subnets[0] } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@subnets == 1 and ref $subnets[0] eq "ARRAY"
MC/DC100%2/2
@subnets == 1 and ref $subnets[0] eq "ARRAY"
@subnets == 1
ref $subnets[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @subnets == 1B= ref $subnets[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 44 | if @subnets == 1 && ref $subnets[0] eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 45 | 40 | for my $i (0 .. $#subnets) { | |||||||||||||||||||||||||||
| 46 | 50 | ▶ | croak _bad_argument('add', $i + 2, $subnets[$i], $WANT_ADDRESS) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | unless _is_address($subnets[$i]); | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 38 | push @{ $self->{subnets}{$name} }, @subnets; | |||||||||||||||||||||||||||
| 50 | 38 | ▶ | $self->{totals}{$name} //= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"totals"}{$name} //= 0
MC/DC100%1/1
$self->{"totals"}{$name} //= 0
$self->{"totals"}{$name}
Truth table100%2/2
A
A= $self->{"totals"}{$name}
| |||||||||||||||||||||||||||||
| 51 | 38 | return $self; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 26 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 26 | my ($self, @addresses) = @_; | |||||||||||||||||||||||||||
| 56 | 26 | for my $i (0 .. $#addresses) { | |||||||||||||||||||||||||||
| 57 | 62 | ▶ | croak _bad_argument('count', $i + 1, $addresses[$i], $WANT_ADDRESS) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | unless _is_address($addresses[$i]); | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 22 | $self->_add_to_totals($_, 1) for @addresses; | |||||||||||||||||||||||||||
| 61 | 22 | return $self; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 4 | ▶ | sub valcount { | ||||||||||||||||||||||||||
|
Subroutine
valcount: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 4 | my ($self, @pairs) = @_; | |||||||||||||||||||||||||||
| 66 | 4 | ▶ | croak sprintf( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | '%s::valcount takes address and value pairs, got %d arguments', | ||||||||||||||||||||||||||||
| 68 | __PACKAGE__, scalar @pairs | ||||||||||||||||||||||||||||
| 69 | ) if @pairs % 2; | ||||||||||||||||||||||||||||
| 70 | 3 | for my $i (0 .. $#pairs) { | |||||||||||||||||||||||||||
| 71 | 13 | ▶ | if ($i % 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 6 | ▶ | croak _bad_argument('valcount', $i + 1, $pairs[$i], $WANT_NUMBER) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | unless looks_like_number($pairs[$i]); | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 7 | ▶ | croak _bad_argument('valcount', $i + 1, $pairs[$i], $WANT_ADDRESS) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | unless _is_address($pairs[$i]); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 1 | ▶ | while (my ($address, $value) = splice @pairs, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 4 | $self->_add_to_totals($address, $value); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 1 | return $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 17 | ▶ | sub result { | ||||||||||||||||||||||||||
|
Subroutine
result: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||
| 88 | 17 | return { %{ $self->{totals} } }; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 8 | ▶ | sub cache { | ||||||||||||||||||||||||||
|
Subroutine
cache: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 8 | my ($self, @size) = @_; | |||||||||||||||||||||||||||
| 93 | 8 | my $old = $self->{cache_size}; | |||||||||||||||||||||||||||
| 94 | 8 | ▶ | if (@size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 5 | ▶ | croak _bad_argument('cache', 1, $size[0], $WANT_NUMBER) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | unless looks_like_number($size[0]); | ||||||||||||||||||||||||||||
| 97 | 4 | ▶ | $self->{cache_size} = $size[0] if $size[0] >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 7 | return $old; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # every group with a subnet that contains the address takes the value, once | ||||||||||||||||||||||||||||
| 103 | 58 | ▶ | sub _add_to_totals { | ||||||||||||||||||||||||||
|
Subroutine
_add_to_totals: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 104 | 58 | my ($self, $address, $value) = @_; | |||||||||||||||||||||||||||
| 105 | 58 | my $subnets = $self->{subnets}; | |||||||||||||||||||||||||||
| 106 | GROUP: | ||||||||||||||||||||||||||||
| 107 | 58 | for my $name (keys %{$subnets}) { | |||||||||||||||||||||||||||
| 108 | 197 | for my $subnet (@{ $subnets->{$name} }) { | |||||||||||||||||||||||||||
| 109 | 242 | ▶ | next unless $subnet->contains($address); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 69 | $self->{totals}{$name} += $value; | |||||||||||||||||||||||||||
| 111 | 69 | next GROUP; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 58 | return; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 119 | ▶ | sub _is_address { | ||||||||||||||||||||||||||
|
Subroutine
_is_address: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 119 | my ($arg) = @_; | |||||||||||||||||||||||||||
| 119 | 119 | ▶ | return blessed($arg) && $arg->isa('NetAddr::IP::Lite'); | ||||||||||||||||||||||||||
|
Condition100%3/3
builtin::blessed($arg) && $arg->isa('NetAddr::IP::Lite')
MC/DC100%2/2
builtin::blessed($arg) && $arg->isa('NetAddr::IP::Lite')
builtin::blessed($arg)
$arg->isa('NetAddr::IP::Lite')
Truth table100%3/3
A && B
A= builtin::blessed($arg)B= $arg->isa('NetAddr::IP::Lite')
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 9 | ▶ | sub _bad_argument { | ||||||||||||||||||||||||||
|
Subroutine
_bad_argument: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 123 | 9 | my ($method, $position, $arg, $wanted) = @_; | |||||||||||||||||||||||||||
| 124 | 9 | ▶ | my $got = | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 125 | !defined $arg ? 'undefined' | ||||||||||||||||||||||||||||
| 126 | : ref $arg ? ref $arg | ||||||||||||||||||||||||||||
| 127 | : "'$arg'"; | ||||||||||||||||||||||||||||
| 128 | 9 | return sprintf( | |||||||||||||||||||||||||||
| 129 | '%s::%s needs %s for argument %d, got %s', | ||||||||||||||||||||||||||||
| 130 | __PACKAGE__, $method, $wanted, $position, $got | ||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||