File Coverage

blib/lib/App/Manoc/IPAddress/IPv4.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package App::Manoc::IPAddress::IPv4;
2             #ABSTRACT: IPv4 Addresses
3              
4 2     2   4527 use Moose;
  2         407101  
  2         13  
5              
6             our $VERSION = '2.99.2'; ##TRIAL VERSION
7              
8              
9 2     2   13174 use namespace::autoclean;
  2         6351  
  2         12  
10              
11 2     2   535 use App::Manoc::Utils::IPAddress qw(ip2int int2ip padded_ipaddr check_addr);
  0            
  0            
12              
13             use overload (
14             '""' => sub { shift->_stringify() },
15             'cmp' => \&_cmp_op,
16             '<=>' => \&_cmp_op,
17             );
18              
19              
20             has 'numeric' => (
21             is => 'ro',
22             isa => 'Int',
23             required => 1,
24             );
25              
26              
27             has 'padded' => (
28             is => 'ro',
29             isa => 'Str',
30             init_arg => undef,
31             lazy => 1,
32             builder => '_build_padded'
33             );
34              
35              
36             has 'unpadded' => (
37             is => 'ro',
38             isa => 'Str',
39             init_arg => undef,
40             lazy => 1,
41             builder => '_build_unpadded'
42             );
43              
44             around BUILDARGS => sub {
45             my $orig = shift;
46             my $class = shift;
47              
48             return unless $_[0];
49              
50             if ( @_ == 1 && !ref $_[0] ) {
51             return $class->$orig( numeric => ip2int( $_[0] ) );
52             }
53             else {
54             return $class->$orig(@_);
55             }
56             };
57              
58              
59             sub address {
60             return $_[0]->unpadded;
61             }
62              
63             sub _build_padded {
64             padded_ipaddr( $_[0]->unpadded );
65             }
66              
67             sub _build_unpadded {
68             int2ip( $_[0]->numeric );
69             }
70              
71             sub _stringify {
72             return $_[0]->unpadded;
73             }
74              
75             sub _cmp_op {
76             my ( $first, $second ) = @_;
77             if ( blessed($second) && $second->isa("App::Manoc::IPAddress::IPv4") ) {
78             return $first->numeric <=> $second->numeric;
79             }
80             check_addr("$second") and
81             return ( $first->padded cmp padded_ipaddr("$second") );
82             return -1;
83             }
84              
85             __PACKAGE__->meta->make_immutable;
86             1;
87              
88             # Local Variables:
89             # mode: cperl
90             # indent-tabs-mode: nil
91             # cperl-indent-level: 4
92             # cperl-indent-parens-as-block: t
93             # End:
94              
95             __END__
96              
97             =pod
98              
99             =head1 NAME
100              
101             App::Manoc::IPAddress::IPv4 - IPv4 Addresses
102              
103             =head1 VERSION
104              
105             version 2.99.2
106              
107             =head1 DESCRIPTION
108              
109             A class for IPv4 addresses. Supports padding, unpadding,
110             stringification and comparison operators.
111              
112             =head1 ATTRIBUTES
113              
114             =head2 numeric
115              
116             Address integer representation.
117              
118             =head2 padded
119              
120             =head2 unpadded
121              
122             =head1 METHODS
123              
124             =head2 address
125              
126             Return the address in unpadded form. Automatically called by stringification.
127              
128             =head1 SYNOSPIS
129              
130             my $addr = App::Manoc::IPAddress::IPv4->new('10.1.100.1');
131              
132              
133             $addr->padded; # '010.001.100.001'
134             $addr->unpadded; # '10.1.100.1'
135              
136             "$addr" eq '10.1.100.1'; # true
137              
138             $addr > App::Manoc::IPAddress::IPv4->new('2.1.1.1'); # also true
139              
140             =head1 AUTHORS
141              
142             =over 4
143              
144             =item *
145              
146             Gabriele Mambrini <gmambro@cpan.org>
147              
148             =item *
149              
150             Enrico Liguori
151              
152             =back
153              
154             =head1 COPYRIGHT AND LICENSE
155              
156             This software is copyright (c) 2017 by Gabriele Mambrini.
157              
158             This is free software; you can redistribute it and/or modify it under
159             the same terms as the Perl 5 programming language system itself.
160              
161             =cut