MacAddress.pm

filter: Statementstmt 100% 74 / 74 Branchbran 100% 24 / 24 Conditioncond 87.5% 7 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 21 / 21 Podpod 88.8% 8 / 9 totaltotal 97.8% 137 / 140 CC 16 SCAR 15.0
CC
16
Cov
99%
BUILDARGS
11.0
to_string
4.0
_to_binary
2.0
CRAP 4.5
↑ Summary MacAddress/Formatter/Base16.pm »
1 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package NetObj::MacAddress;
5 8 $NetObj::MacAddress::VERSION = '1.0.2';
6 # ABSTRACT: represent a MAC address
7  
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 39 ▶sub _to_binary {
Subroutine _to_binary: called CC=2 SCAR=6.9
11 39 my ($macaddr) = @_;
12  
13 39 $macaddr =~ s{[-:\.]}{}xmsgi;
14 39 ▶ return unless $macaddr =~ m{\A [\d a-f]{12} \Z}xmsi;
Branch: 1 branch
BranchTrueFalseTotal
unless $macaddr =~ /\A [\d a-f]{12} \Z/imsx 10 29 39
15  
16 29 return pack('H2' x 6, unpack('A2' x 6, $macaddr));
17 }
18  
19 8 ▶sub is_valid {
Subroutine is_valid: called CC=2 SCAR=6.9
Pod documented
20 8 my ($macaddr) = @_;
21 8 ▶ croak 'NetObj::MacAddress::is_valid is a class method only'
Branch: 1 branch
BranchTrueFalseTotal
if ref $macaddr eq __PACKAGE__ 1 7 8
22 if ref($macaddr) eq __PACKAGE__;
23  
24 7 return !! _to_binary($macaddr);
25 }
26  
27 88 ▶sub binary {
Subroutine binary: called CC=1 SCAR=0.0
Pod documented
28 88 my ($self) = @_;
29 88 return $self->{binary};
30 };
31  
32 45 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=11 SCAR=24.0
Pod undocumented
33 45 my ($class, $mac, @args) = @_;
34 45 ▶ croak 'no MAC address given' unless defined($mac);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mac 1 44 45
35  
36 44 ▶ if ($mac eq 'binary') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mac eq "binary") 5 39 44
37 5 $mac = shift(@args);
38 5 ▶ if (length($mac) == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $mac == 6) 1 4 5
39 1 return { binary => $mac };
40 }
41 4 croak 'invalid MAC address';
42 }
43 39 ▶ if ((ref($mac) eq 'HASH') and exists($mac->{binary}) and (length($mac->{binary}) == 6)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mac eq "HASH" and exists $mac->{"binary"} and length $mac->{"binary"} == 6) 1 38 39
Condition83%5/6
ref $mac eq "HASH" and exists $mac->{"binary"} and length $mac->{"binary"} == 6
!ll&&!rl&&r
3441
ref $mac eq "HASH" and exists $mac->{"binary"}
!ll&&!rl&&r
3405
MC/DC66%2/3
ref $mac eq "HASH" and exists $mac->{"binary"} and length $mac->{"binary"} == 6
ref $mac eq "HASH" exists $mac->{"binary"} length $mac->{"binary"} == 6
Truth table75%3/4
A and B and C
A= ref $mac eq "HASH"B= exists $mac->{"binary"}C= length $mac->{"binary"} == 6
ABCresult
0XX0
10X0
1100
1111
44 1 return { binary => $mac->{binary} };
45 }
46 38 ▶ croak 'too many arguments in constructor for ' . __PACKAGE__ if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 1 37 38
47  
48 37 ▶ return { binary => $mac->binary() } if ref($mac) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $mac eq __PACKAGE__ 2 35 37
49  
50 35 ▶ $mac = _to_binary($mac) unless length($mac) == 6;
Branch: 1 branch
BranchTrueFalseTotal
unless length $mac == 6 32 3 35
51 35 ▶ croak 'invalid MAC address' unless $mac;
Branch: 1 branch
BranchTrueFalseTotal
unless $mac 7 28 35
52 28 return { binary => $mac };
53 }
54  
55 45 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
56 45 my ($class, @args) = @_;
57 45 return bless BUILDARGS(@_), $class;
58 }
59  
60 8 ▶use NetObj::MacAddress::Formatter::Base16;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 49 ▶sub to_string {
Subroutine to_string: called CC=4 SCAR=13.9
Pod documented
62 49 my ($self, $format) = @_;
63 49 ▶ $format //= 'base16';
Condition100%2/2
$format //= "base16"
l!l
742
MC/DC100%1/1
$format //= "base16"
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
64 49 $format = lc($format);
65  
66 49 state $formatter = {};
67  
68 49 ▶ if (not exists($formatter->{$format})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $formatter->{$format}) 10 39 49
69 10 my $pkg = ucfirst($format);
70 10 my $sub = "NetObj::MacAddress::Formatter::${pkg}::format";
71 10 ▶ if (defined(&$sub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$sub) { } 9 1 10
72 9 $formatter->{$format} = \&$sub;
73 }
74 else {
75 1 croak "no formatter for $format";
76 }
77 }
78 48 return $formatter->{$format}($self);
79 }
80  
81 8 ▶use overload q("") => sub {shift->to_string};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
82  
83 8 ▶use overload q(<=>) => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
84 3 my ($x, $y) = @_;
85 3 return $x->binary() cmp $y->binary()
86 8 };
87 8 ▶use overload q(cmp) => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
88 5 my ($x, $y) = @_;
89 5 return "$x" cmp "$y"
90 8 };
91  
92  
93 # NOTE: vec(EXPR, OFFSET, BITS) treats EXPR as little endian on all platforms
94 # see: perldoc -f vec
95  
96 16 ▶sub is_unicast {
Subroutine is_unicast: called CC=1 SCAR=0.0
Pod documented
97 16 my ($self) = @_;
98 16 return not vec($self->binary(), 0, 1);
99 }
100  
101 8 ▶sub is_multicast {
Subroutine is_multicast: called CC=1 SCAR=0.0
Pod documented
102 8 my ($self) = @_;
103 8 return not $self->is_unicast();
104 }
105  
106 16 ▶sub is_global {
Subroutine is_global: called CC=1 SCAR=0.0
Pod documented
107 16 my ($self) = @_;
108 16 return not vec($self->binary(), 1, 1);
109 }
110  
111 8 ▶sub is_local {
Subroutine is_local: called CC=1 SCAR=0.0
Pod documented
112 8 my ($self) = @_;
113 8 return not $self->is_global();
114 }
115  
116 8 1;
117  
118 __END__
↑ Summary MacAddress/Formatter/Base16.pm »