lib/NetAddr/IP/LazyInit.pm

filter: Statementstmt 73.5% 86 / 117 Branchbran 45.8% 22 / 48 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 64.7% 22 / 34 Podpod 33.3% 3 / 9 totaltotal 62.4% 133 / 213 CC 28 SCAR 15.6
CC
28
Cov
64%
mask
12.0
__ANON__
12.0
__ANON__
12.0
CRAP 4.7
↑ Summary
1 package NetAddr::IP::LazyInit;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use NetAddr::IP qw(Zero Zeros Ones V4mask V4net netlimit);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use NetAddr::IP::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 33 our $VERSION = eval '0.7';
10  
11 =head1 NAME
12  
13 NetAddr::IP::LazyInit - NetAddr::IP objects with deferred validation B<SEE DESCRIPTION BEFORE USING>
14  
15 =head1 VERSION
16  
17 0.6
18  
19 =head1 SYNOPSIS
20  
21 use NetAddr::IP::LazyInit;
22  
23 my $ip = new NetAddr::IP::LazyInit( '10.10.10.5' );
24  
25 =head1 DESCRIPTION
26  
27 This module is designed to quickly create objects that may become NetAddr::IP
28 objects. It accepts anything you pass to it without validation. Once a
29 method is called that requires operating on the value, the full NetAddr::IP
30 object is constructed.
31  
32 You can see from the benchmarks that once you need to instantiate NetAddr::IP
33 the speed becomes worse than if you had not used this module. What I mean is
34 that this adds unneeded overhead if you intend to do IP operations on every
35 object you create.
36  
37 =head1 WARNING
38  
39  
40 Because validation is deferred, this module assumes you will B<only ever give
41 it valid data>. If you try to give it anything else, it will happily accept it
42 and then die once it needs to inflate into a NetAddr::IP object.
43  
44  
45 =head1 CREDITS
46  
47 This module was inspired by discussion with Jan Henning Thorsen, E<lt>jhthorsen
48 at cpan.orgE<gt>, and example code he provided. The namespace and part of the
49 documentation/source is inspired by DateTime::LazyInit by
50 Rick Measham, E<lt>rickm@cpan.orgE<gt>
51  
52 I didn't have to do much so I hate to take author credit, but I am providing
53 the module, so complaints can go to me.
54  
55 Robert Drake, E<lt>rdrake@cpan.orgE<gt>
56  
57 =head1 TODO
58  
59 If we could actually load NetAddr::IP objects in the background while nothing
60 is going on that would be neat. Or we could create shortcut methods when the
61 user knows what type of input he has. new_from_ipv4('ip','mask'). We might
62 be able to use Socket to build the raw materials and bless a new NetAddr::IP
63 object without going through it's validation.
64  
65 =head1 COPYRIGHT AND LICENSE
66  
67 Copyright (C) 2014 by Robert Drake
68  
69 This library is free software; you can redistribute it and/or modify
70 it under the same terms as Perl itself, either Perl version 5.8.7 or,
71 at your option, any later version of Perl 5 you may have available.
72  
73 =cut
74  
75 33 require Exporter;
76 33 our @ISA = qw(Exporter);
77 33 our @EXPORT_OK = qw(Compact Coalesce Zero Zeros Ones V4mask V4net netlimit);
78  
79 =head1 METHODS
80  
81 =head2 new
82  
83 This replaces the NetAddr::IP->new method with a stub that stores the
84 arguments supplied in a temporary variable and returns immediately. No
85 validation is performed.
86  
87 Once you call a method that can't be handled by LazyInit, a full NetAddr::IP
88 object is built and the request passed into that object.
89  
90 my $ip = NetAddr::IP::LazyInit->new("127.0.0.1");
91  
92 =cut
93  
94 1576 ▶sub new { my $class = shift; bless {x=>[@_]}, $class }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
95  
96 =head2 addr
97  
98 Returns the IP address of the object. If we can extract the IP as a string
99 without converting to a real NetAddr::IP object, then we return that.
100 Currently it only returns IPv6 strings in lower case, which may break your
101 application if you aren't using the new standard.
102  
103 my $ip = NetAddr::IP::LazyInit->new("127.0.0.1");
104 print $ip->addr;
105  
106 =cut
107  
108 4 ▶sub addr {
Subroutine addr: called CC=2 SCAR=7.6
Pod documented
109 4 my $self = shift;
110 4 ▶ if ($self->{x}->[0] =~ /^(.*?)(?:\/|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'x'}[0] =~ m[^(.*?)(?:/|$)]) { } 4 0 4
111 4 return lc($1);
112 } else {
113 0 return $self->inflate->addr;
114 }
115 }
116  
117 =head2 mask
118  
119 Returns the subnet mask of the object. If the user used the two argument
120 option then it returns the string they provided for the second argument.
121 Otherwise this will inflate to build a real NetAddr::IP object and return the
122 mask.
123  
124 my $ip = NetAddr::IP::LazyInit->new("127.0.0.1", "255.255.255.0");
125 print $ip->mask;
126  
127 =cut
128  
129 0 ▶sub mask {
Subroutine mask: not called CC=3 SCAR=24.8
Pod documented
130 0 my $self = shift;
131 0 ▶ if ($self->{x}->[1] && $self->{x}->[1] =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'x'}[1] and $self->{'x'}[1] =~ /\D/) { } 0 0 0
Condition0%0/3
$self->{"x"}[1] and $self->{"x"}[1] =~ /\D/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"x"}[1] and $self->{"x"}[1] =~ /\D/
$self->{"x"}[1] $self->{"x"}[1] =~ /\D/
Truth table0%0/3
A and B
A= $self->{"x"}[1]B= $self->{"x"}[1] =~ /\D/
ABresult
0X0
100
111
132 0 return $self->{x}->[1];
133 } else {
134 0 return $self->inflate->mask;
135 }
136 }
137  
138 # everything below here aren't ment for speed or for users to reference.
139 # They're purely for compatibility with NetAddr::IP so that users can use this
140 # module like the real one.
141  
142 0 ▶sub can { NetAddr::IP->can($_[1]); }
Subroutine can: not called CC=1 SCAR=6.9
Pod undocumented
143  
144 11 ▶sub Compact {
Subroutine Compact: called CC=3 SCAR=11.0
Pod undocumented
145 11 for (@_) {
146 14363 ▶ $_->inflate if (ref($_) eq 'NetAddr::IP::LazyInit');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "NetAddr::IP::LazyInit" 283 14080 14363
147 }
148 11 return NetAddr::IP::Compact(@_);
149 }
150  
151  
152  
153 6 ▶sub Coalesce {
Subroutine Coalesce: called CC=3 SCAR=11.0
Pod undocumented
154 6 for (@_) {
155 4622 ▶ $_->inflate if (ref($_) eq 'NetAddr::IP::LazyInit');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "NetAddr::IP::LazyInit" 770 3852 4622
156 }
157 6 return NetAddr::IP::Coalesce(@_);
158 }
159  
160 35 ▶sub import {
Subroutine import: called CC=6 SCAR=18.0
161 35 ▶ if (grep { $_ eq ':rfc3021' } @_)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":rfc3021";} @_) 1 34 35
162 {
163 1 $NetAddr::IP::rfc3021 = 1;
164 1 @_ = grep { $_ ne ':rfc3021' } @_;
165 }
166 35 ▶ if (grep { $_ eq ':old_storable' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":old_storable";} @_) 1 34 35
167 1 @_ = grep { $_ ne ':old_storable' } @_;
168 }
169 35 ▶ if (grep { $_ eq ':old_nth' } @_)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":old_nth";} @_) 1 34 35
170 {
171 1 $NetAddr::IP::Lite::Old_nth = 1;
172 1 @_ = grep { $_ ne ':old_nth' } @_;
173 }
174 35 ▶ if (grep { $_ eq ':lower' } @_)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":lower";} @_) 1 34 35
175 {
176 1 NetAddr::IP::Util::lower();
177 1 @_ = grep { $_ ne ':lower' } @_;
178 }
179 35 ▶ if (grep { $_ eq ':upper' } @_)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":upper";} @_) 0 35 35
180 {
181 0 NetAddr::IP::Util::upper();
182 0 @_ = grep { $_ ne ':upper' } @_;
183 }
184  
185 35 NetAddr::IP::LazyInit->export_to_level(1, @_);
186 }
187  
188 # need to support everything that NetAddr::IP does
189 33 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=2.9
190 3 ▶ '@{}' => sub { return [ $_[0]->inflate->hostenum ]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
191 151 ▶ '""' => sub { return $_[0]->inflate->cidr() },
Subroutine __ANON__: called CC=1 SCAR=0.0
192 12 ▶ '<=>' => sub { inflate_args_and_run(\&NetAddr::IP::Lite::comp_addr_mask, @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
193 0 ▶ 'cmp' => sub { inflate_args_and_run(\&NetAddr::IP::Lite::comp_addr_mask, @_); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 1 ▶ '++' => sub { inflate_args_and_run(\&NetAddr::IP::Lite::plusplus, @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
195 1 ▶ '+' => sub { inflate_args_and_run(\&NetAddr::IP::Lite::plus, @_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
196 0 ▶ '--' => sub { inflate_args_and_run(\&NetAddr::IP::Lite::minusminus, @_); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 0 ▶ '-' => sub { inflate_args_and_run(\&NetAddr::IP::Lite::minus, @_); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 0 ▶ '=' => sub { inflate_args_and_run(\&NetAddr::IP::Lite::copy, @_); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 ▶ '==' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
200 0 my $a = $_[0];
201 0 ▶ $a->inflate if ref($_[0]) =~ /NetAddr::IP::LazyInit/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($_[0]) =~ /NetAddr::IP::LazyInit/ 0 0 0
202 0 my $b = $_[1];
203 0 ▶ $b->inflate if ref($_[1]) =~ /NetAddr::IP::LazyInit/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($_[1]) =~ /NetAddr::IP::LazyInit/ 0 0 0
204 0 return ($a eq $b);
205 },
206 0 ▶ '!=' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
207 0 my $a = $_[0];
208 0 ▶ $a->inflate if ref($_[0]) eq 'NetAddr::IP::LazyInit';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "NetAddr::IP::LazyInit" 0 0 0
209 0 my $b = $_[1];
210 0 ▶ $b->inflate if ref($_[1]) eq 'NetAddr::IP::LazyInit';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "NetAddr::IP::LazyInit" 0 0 0
211 0 return ($a ne $b);
212 },
213 0 ▶ 'ne' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
214 0 my $a = $_[0];
215 0 ▶ $a->inflate if ref($_[0]) eq 'NetAddr::IP::LazyInit';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "NetAddr::IP::LazyInit" 0 0 0
216 0 my $b = $_[1];
217 0 ▶ $b->inflate if ref($_[1]) eq 'NetAddr::IP::LazyInit';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "NetAddr::IP::LazyInit" 0 0 0
218 0 return ($a ne $b);
219 },
220 2 ▶ 'eq' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
221 2 my $a = $_[0];
222 2 ▶ $a->inflate if ref($_[0]) eq 'NetAddr::IP::LazyInit';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "NetAddr::IP::LazyInit" 2 0 2
223 2 my $b = $_[1];
224 2 ▶ $b->inflate if ref($_[1]) eq 'NetAddr::IP::LazyInit';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "NetAddr::IP::LazyInit" 0 2 2
225 2 return ($a eq $b);
226 },
227 9 ▶ '>' => sub { return &comp_addr_mask > 0 ? 1 : 0; },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&comp_addr_mask > 0 ? : 5 4 9
228 0 ▶ '<' => sub { return &comp_addr_mask < 0 ? 1 : 0; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
&comp_addr_mask < 0 ? : 0 0 0
229 0 ▶ '>=' => sub { return &comp_addr_mask < 0 ? 0 : 1; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
&comp_addr_mask < 0 ? : 0 0 0
230 0 ▶ '<=' => sub { return &comp_addr_mask > 0 ? 0 : 1; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
&comp_addr_mask > 0 ? : 0 0 0
231  
232 33 );
233  
234 9 ▶sub comp_addr_mask {
Subroutine comp_addr_mask: called CC=1 SCAR=0.0
Pod undocumented
235 9 return inflate_args_and_run(\&NetAddr::IP::Lite::comp_addr_mask, @_);
236 }
237  
238 23 ▶sub inflate_args_and_run {
Subroutine inflate_args_and_run: called CC=3 SCAR=11.0
Pod undocumented
239 23 my $func = shift;
240 23 ▶ $_[0]->inflate if ref($_[0]) eq 'NetAddr::IP::LazyInit';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "NetAddr::IP::LazyInit" 23 0 23
241 23 ▶ $_[1]->inflate if ref($_[1]) eq 'NetAddr::IP::LazyInit';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "NetAddr::IP::LazyInit" 21 2 23
242 23 return &{$func}(@_);
243 }
244  
245 325 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
246 325 my $self = shift;
247 325 my $obj = NetAddr::IP->new(@{ $self->{x} });
248 325 %$self = %$obj;
249 325 bless $self, 'NetAddr::IP';
250 325 our $AUTOLOAD =~ /::(\w+)$/;
251 325 return $self->$1(@_);
252 }
253  
254 1253 ▶sub inflate {
Subroutine inflate: called CC=2 SCAR=7.0
Pod undocumented
255 1253 my $self = shift;
256 1253 my $method = shift;
257 1253 my $obj = NetAddr::IP->new(@{ $self->{x} });
258 1253 %$self = %$obj;
259 1253 bless $self, 'NetAddr::IP';
260 1253 ▶ return $method ? $self->method( @_ ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
$method ? : 0 1253 1253
261 }
262  
263 33 1;
↑ Summary