lib/NetObj/IPv4Address.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 66.6% 2 / 3 totaltotal 98.8% 89 / 90 CC 9 SCAR 9.2
CC
9
Cov
100%
BUILDARGS
6.0
_to_binary
3.0
is_valid
2.0
CRAP 2.5
↑ Summary
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package NetObj::IPv4Address;
5 5 $NetObj::IPv4Address::VERSION = '1.0';
6 # ABSTRACT: represent a IPv4 address
7  
8 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use List::MoreUtils qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 32 ▶sub _to_binary {
Subroutine _to_binary: called CC=3 SCAR=11.0
13 32 my ($ipaddr) = @_;
14  
15 32 my @octets = split(qr{\.}, $ipaddr);
16 32 ▶ return unless @octets == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @octets == 4 6 26 32
17 return unless all {
18 ($_ =~ m{\A \d+ \Z}xms) and ($_ >=0) and ($_ <= 255);
19 26 ▶ } @octets;
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { $_ =~ /\A \d+ \Z/msux and $_ >= 0 and $_ <= 255; } , @octets) 10 16 26
20  
21 16 return pack('CCCC', @octets);
22 }
23  
24 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 13 ▶sub is_valid {
Subroutine is_valid: called CC=2 SCAR=6.9
Pod documented
27 13 my ($ipaddr) = @_;
28 13 ▶ croak 'NetObj::IPv4Adress::is_valid is a class method only'
Branch: 1 branch
BranchTrueFalseTotal
if ref $ipaddr eq __PACKAGE__ 1 12 13
29 if ref($ipaddr) eq __PACKAGE__;
30  
31 12 return !! _to_binary($ipaddr);
32 }
33  
34 5 has binary => (
35 is => 'ro',
36 );
37  
38 36 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=6 SCAR=17.9
Pod undocumented
39 36 my ($class, $ip, @args) = @_;
40 36 ▶ croak 'no IPv4 address given' unless defined($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 35 36
41 35 ▶ croak 'too many arguments in constructor for ' . __PACKAGE__ if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 1 34 35
42  
43 34 ▶ return { binary => $ip->binary() } if ref($ip) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $ip eq __PACKAGE__ 7 27 34
44  
45 27 ▶ $ip = _to_binary($ip) unless length($ip) == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless length $ip == 4 20 7 27
46 27 ▶ croak 'invalid IPv4 address' unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 8 19 27
47  
48 19 return { binary => $ip };
49 }
50  
51 26 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
52 26 my ($self) = @_;
53  
54 26 return join('.', unpack('CCCC', $self->binary()));
55 }
56  
57 5 ▶use overload q("") => sub {shift->to_string};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
58  
59 5 ▶use overload q(<=>) => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
60 10 my ($a, $b) = @_;
61 10 return ($a->binary() cmp NetObj::IPv4Address->new($b)->binary());
62 5 };
63  
64 5 ▶use overload q(cmp) => sub { my ($a, $b) = @_; return $a <=> $b; };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 5 1;
67  
68 __END__
↑ Summary