Works/Address.pm

filter: Statementstmt 96.5% 84 / 87 Branchbran 80.0% 16 / 20 Conditioncond 57.8% 11 / 19 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 87.5% 21 / 24 Podpod 80.0% 8 / 10 totaltotal 84.2% 144 / 171 CC 18 SCAR 12.2
CC
18
Cov
88%
_compare_overload
8.7
new_from_string
6.0
new_from_integer
3.4
CRAP 3.4
« Works/Util.pm ↑ Summary Works/Role/IP.pm »
1 package Net::Works::Address;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.22';
7  
8 4 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Math::Int128 0.06 qw( uint128 uint128_to_hex uint128_to_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Net::Works::Types qw( PackedBinary Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Net::Works::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 _integer_address_to_binary
13 _integer_address_to_string
14 _string_address_to_integer
15 _validate_ip_string
16 4 );
17 4 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Socket 1.99 qw( AF_INET AF_INET6 inet_pton inet_ntop );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Using this currently breaks overloading - see
23 # https://rt.cpan.org/Ticket/Display.html?id=50938
24 #
25 #use namespace::autoclean;
26  
27 4 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 q{""} => '_overloaded_as_string',
29 '<=>' => '_compare_overload',
30 'cmp' => '_compare_overload',
31 4 );
32  
33 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 4 with 'Net::Works::Role::IP';
36  
37 4 has _binary => (
38 is => 'ro',
39 reader => 'as_binary',
40 isa => PackedBinary,
41 lazy => 1,
42 builder => '_build_binary',
43 );
44  
45 4 has _string => (
46 is => 'ro',
47 reader => 'as_string',
48 isa => Str,
49 lazy => 1,
50 builder => '_build_string',
51 );
52  
53 2525 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
54 2525 my $self = shift;
55  
56 2525 $self->_validate_ip_integer();
57  
58 2523 return;
59 }
60  
61 127 ▶sub new_from_string {
Subroutine new_from_string: called CC=6 SCAR=17.9
Pod documented
62 127 my $class = shift;
63 127 my %p = @_;
64  
65 127 my $str = delete $p{string};
66 127 my $version = delete $p{version};
67  
68 127 ▶ if ( defined $str && inet_pton( AF_INET, $str ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str and inet_pton(2, $str)) { } 43 84 127
Condition100%3/3
defined $str and inet_pton(2, $str)
!ll&&!rl&&r
28243
MC/DC100%2/2
defined $str and inet_pton(2, $str)
defined $str inet_pton(2, $str)
Truth table100%3/3
A and B
A= defined $strB= inet_pton(2, $str)
ABresult
0X0
100
111
69 43 ▶ $version ||= 4;
Condition100%2/2
$version ||= 4
l!l
2221
MC/DC100%1/1
$version ||= 4
$version
Truth table100%2/2
A
A= $version
Aresult
11
00
70 43 ▶ $str = '::' . $str if $version == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $version == 6 2 41 43
71 }
72 else {
73 84 ▶ $version ||= 6;
Condition100%2/2
$version ||= 6
l!l
6420
MC/DC100%1/1
$version ||= 6
$version
Truth table100%2/2
A
A= $version
Aresult
11
00
74 84 _validate_ip_string( $str, $version );
75 }
76  
77 110 return $class->new(
78 _integer => _string_address_to_integer( $str, $version ),
79 version => $version,
80 %p,
81 );
82 }
83  
84 2425 ▶sub new_from_integer {
Subroutine new_from_integer: called CC=3 SCAR=12.3
Pod documented
85 2425 my $class = shift;
86 2425 my %p = @_;
87  
88 2425 my $int = delete $p{integer};
89 2425 my $version = delete $p{version};
90 2425 ▶ $version ||= ref $int ? 6 : 4;
Branch: 1 branch
BranchTrueFalseTotal
ref $int ? : 0 0 0
Condition33%1/3
$version ||= ref $int ? 6 : 4
l!l&&r!l&&!r
242500
MC/DC0%0/2
$version ||= ref $int ? 6 : 4
$version ref $int ? 6 : 4
Truth table33%1/3
A ||= B
A= $versionB= ref $int ? 6 : 4
ABresult
1X1
011
000
91  
92 2425 return $class->new(
93 _integer => $int,
94 version => $version,
95 %p,
96 );
97 }
98  
99 2008 ▶sub _build_string {
Subroutine _build_string: called CC=1 SCAR=0.0
100 2008 my $self = shift;
101  
102 2008 return _integer_address_to_string( $self->_integer() );
103 }
104  
105 0 ▶sub _build_binary { _integer_address_to_binary( $_[0]->as_integer() ) }
Subroutine _build_binary: not called CC=1 SCAR=6.9
106  
107 1848 ▶sub as_integer { $_[0]->_integer() }
Subroutine as_integer: called CC=1 SCAR=0.0
Pod documented
108  
109 657 ▶sub as_ipv4_string {
Subroutine as_ipv4_string: called CC=3 SCAR=11.0
Pod documented
110 657 my $self = shift;
111  
112 657 ▶ return $self->as_string() if $self->version() == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->version == 4 34 623 657
113  
114 623 ▶ confess
Branch: 1 branch
BranchTrueFalseTotal
if $self->as_integer >= "4294967296" 373 250 623
115 'Cannot represent IP address larger than 2**32-1 as an IPv4 string'
116 if $self->as_integer() >= 2**32;
117  
118 250 return __PACKAGE__->new_from_integer(
119 integer => $self->as_integer(),
120 version => 4,
121 )->as_string();
122 }
123  
124 6 ▶sub as_bit_string {
Subroutine as_bit_string: called CC=2 SCAR=6.9
Pod documented
125 6 my $self = shift;
126  
127 6 ▶ if ( $self->version == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version == 6) { } 4 2 6
128 4 my $hex = uint128_to_hex( $self->as_integer() );
129 4 my @ha = $hex =~ /.{8}/g;
130 4 return join q{}, map { sprintf( '%032b', hex($_) ) } @ha;
131 }
132 else {
133 2 return sprintf( '%032b', $self->as_integer() );
134 }
135 }
136  
137 0 ▶sub prefix_length { $_[0]->bits() }
Subroutine prefix_length: not called CC=1 SCAR=6.9
Pod documented
138  
139 0 ▶sub mask_length { $_[0]->prefix_length() }
Subroutine mask_length: not called CC=1 SCAR=6.9
Pod undocumented
140  
141 5 ▶sub next_ip {
Subroutine next_ip: called CC=2 SCAR=6.9
Pod documented
142 5 my $self = shift;
143  
144 5 ▶ confess "$self is the last address in its range"
Branch: 1 branch
BranchTrueFalseTotal
if $self->as_integer == $self->_max 2 3 5
145 if $self->as_integer() == $self->_max;
146  
147 3 return __PACKAGE__->new_from_integer(
148 integer => $self->as_integer() + 1,
149 version => $self->version(),
150 );
151 }
152  
153 4 ▶sub previous_ip {
Subroutine previous_ip: called CC=2 SCAR=6.9
Pod documented
154 4 my $self = shift;
155  
156 4 ▶ confess "$self is the first address in its range"
Branch: 1 branch
BranchTrueFalseTotal
if $self->as_integer == 0 2 2 4
157 if $self->as_integer() == 0;
158  
159 2 return __PACKAGE__->new_from_integer(
160 integer => $self->as_integer() - 1,
161 version => $self->version(),
162 );
163 }
164  
165 211 ▶sub _compare_overload {
Subroutine _compare_overload: called CC=6 SCAR=21.6
166 211 my $self = shift;
167 211 my $other = shift;
168 211 ▶ my $flip = shift() ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 211 211
169  
170 confess 'Cannot compare unless both objects are '
171 . __PACKAGE__
172 . ' objects'
173 unless blessed $self
174 && blessed $other
175 211 ▶ && eval { $self->isa(__PACKAGE__) && $other->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) and builtin::blessed($other) and eval { do { $self->isa(__PACKAGE__) and $other->isa(__PACKAGE__) } } 0 211 211
Condition33%3/9
builtin::blessed($self) and builtin::blessed($other) and eval { do { $self->isa(__PACKAGE__) and $other->isa(__PACKAGE__) } }
!ll&&!rl&&r
00211
builtin::blessed($self) and builtin::blessed($other)
!ll&&!rl&&r
00211
$self->isa(__PACKAGE__) && $other->isa(__PACKAGE__)
!ll&&!rl&&r
00211
MC/DC0%0/3
builtin::blessed($self) and builtin::blessed($other) and eval { do { $self->isa(__PACKAGE__) and $other->isa(__PACKAGE__) } }
builtin::blessed($self) builtin::blessed($other) eval { do { $self->isa(__PACKAGE__) and $other->isa(__PACKAGE__) } }
MC/DC0%0/2
$self->isa(__PACKAGE__) && $other->isa(__PACKAGE__)
$self->isa(__PACKAGE__) $other->isa(__PACKAGE__)
Truth table25%1/4
A and B and C
A= builtin::blessed($self)B= builtin::blessed($other)C= eval { do { $self->isa(__PACKAGE__) and $other->isa(__PACKAGE__) } }
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A && B
A= $self->isa(__PACKAGE__)B= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
176  
177 211 return $flip * ( $self->as_integer() <=> $other->as_integer() );
178 }
179  
180 4 __PACKAGE__->meta()->make_immutable();
181  
182 4 1;
183  
184 # ABSTRACT: An object representing a single IP (4 or 6) address
185  
186 __END__
« Works/Util.pm ↑ Summary Works/Role/IP.pm »