blib/lib/MooseX/Types/IPv4.pm

filter: Statementstmt 66.6% 34 / 51 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 26.0% 6 / 23 Podpod n/a totaltotal 52.6% 40 / 76 CC 2 SCAR 5.2
CC
2
Cov
64%
__ANON__
6.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.7
↑ Summary
1 package MooseX::Types::IPv4; {
2  
3 1 ▶ use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 use MooseX::Types
6 1 ▶ -declare => [qw/cidr ip2 ip3 ip4 ip4_binary netmask netmask4_binary/];
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 1 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use MooseX::Types::Moose qw/Int Num Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.03';
13  
14  
15 1 my $ip2valr .= '';
16 1 my $ip3valr .= '';
17  
18 1 my $ip4valr .= '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}';
19 1 $ip4valr .= '(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$';
20  
21  
22 1 my $nmvalr .= '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}';
23 1 $nmvalr .= '(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$';
24  
25  
26 subtype cidr,
27 as Int,
28 0 ▶ where { /^(?:[0-2]?[0-9]||3[0-2])/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 ▶ message { "$_ is not a valid CIDR " };
Subroutine __ANON__: not called CC=1 SCAR=1.2
30  
31  
32 ## No validation here yet. This is for a.b notation.
33 ## I don't use it, but prototyped it in anyway.
34 subtype ip2,
35 as Str,
36 0 ▶ where { /$ip2valr/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 ▶ message { "$_ is not a valid ip address " };
Subroutine __ANON__: not called CC=1 SCAR=1.2
38  
39  
40 ## No validation here yet. This is for a.b.c notation.
41 ## I don't use it, but prototyped it in anyway.
42 subtype ip3,
43 as Str,
44 0 ▶ where { /$ip3valr/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 ▶ message { "$_ is not a valid ip address " };
Subroutine __ANON__: not called CC=1 SCAR=1.2
46  
47  
48 ## This will validate a regular dot-quad ip address. I personally only use
49 ## this to validate user input before converting my ip addresses to binary
50 ## which is how I do all of my ip address manipulations.
51 subtype ip4,
52 as Str,
53 2 ▶ where { /$ip4valr/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 1 ▶ message { "$_ is not a valid ip address " };
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56  
57 subtype ip4_binary,
58 as Num, #[ Base => '2', Precision => '32' ],
59 0 ▶ where { /^[01]{32}$/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 ▶ message { "$_ is not a valid binary IP address " };
Subroutine __ANON__: not called CC=1 SCAR=1.2
61  
62  
63 coerce ip4_binary,
64 from 'MooseX::Types::IPv4::ip4',
65 0 ▶ via { unpack('B32', pack('C4', split(/\D/, $_))); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
66  
67  
68 coerce ip4_binary,
69 from 'MooseX::Types::IPv4::ip3',
70 0 ▶ via { unpack('B32', pack('C2S', split(/\D/, $_))); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
71  
72  
73 coerce ip4_binary,
74 from 'MooseX::Types::IPv4::ip2',
75 0 ▶ via { unpack('B32', pack('CL', split(/\D/, $_))); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
76  
77  
78 subtype netmask,
79 as Str,
80 0 ▶ where { /$nmvalr/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 ▶ message { "$_ is not a valid netmask: " };
Subroutine __ANON__: not called CC=1 SCAR=1.2
82  
83  
84 subtype netmask4_binary,
85 as Num, #[ Base => '2', Precision => '32' ],
86 0 ▶ where { length($_) == 32 and /^(?:[1]{0,32}[0]{0,32})$/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 32 and /^(?:[1]{0,32}[0]{0,32})$/ 0 0 0
87 0 ▶ message { "$_ is not a valid binary netmask address " };
Subroutine __ANON__: not called CC=1 SCAR=1.2
88  
89  
90 coerce netmask4_binary,
91 from 'MooseX::Types::IPv4::netmask',
92 0 ▶ via { unpack('B32', pack('C4', split(/\D/, $_))); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
93  
94  
95 coerce netmask4_binary,
96 from 'MooseX::Types::IPv4::cidr',
97 0 ▶ via { ( ("1" x $_) . ("0" x (32 - $_)) ); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
98  
99 }
100  
101 1 1;
102  
103 __END__
↑ Summary