Zonemaster/NSArray.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 126 CC 5 SCAR 13.3
CC
5
Cov
0%
FETCH
12.0
EXISTS
6.0
_load_name
6.0
CRAP 3.8
1 package Zonemaster::NSArray v1.0.0;
2  
3 use 5.14.2;
4 use warnings;
5 use Carp;
6 use Zonemaster::Recursor;
7 use Zonemaster::Nameserver;
8  
9 use Moose;
10  
11 has 'names' => ( is => 'ro', isa => 'ArrayRef', required => 1 );
12 has 'ary' => ( is => 'ro', isa => 'ArrayRef', default => sub { [] } );
13  
14 sub TIEARRAY {
15 my ( $class, @names ) = @_;
16  
17 return $class->new( { names => [ sort { $a cmp $b } @names ] } );
18 }
19  
20 sub STORE {
21 my ( $self, $index, $value ) = @_;
22  
23 croak "STORE forbidden for this type of array.";
24 }
25  
26 sub STORESIZE {
27 my ( $self, $index, $value ) = @_;
28  
29 croak "STORESIZE forbidden for this type of array.";
30 }
31  
32 sub FETCH {
33 my ( $self, $index ) = @_;
34  
35 if ( exists $self->ary->[$index] ) {
36 return $self->ary->[$index];
37 }
38 elsif ( scalar( @{ $self->names } ) == 0 ) {
39 return;
40 }
41 else {
42 $self->_load_name( shift @{ $self->names } );
43 return $self->FETCH( $index );
44 }
45 }
46  
47 sub FETCHSIZE {
48 my ( $self ) = @_;
49  
50 while ( my $name = shift @{ $self->names } ) {
51 $self->_load_name( $name );
52 }
53  
54 return scalar( @{ $self->ary } );
55 }
56  
57 sub EXISTS {
58 my ( $self, $index ) = @_;
59  
60 if ( $self->FETCH( $index ) ) {
61 return 1;
62 }
63 else {
64 return;
65 }
66 }
67  
68 sub DELETE {
69 my ( $self, $index ) = @_;
70  
71 croak "DELETE forbidden for this type of array.";
72 }
73  
74 sub CLEAR {
75 my ( $self ) = @_;
76  
77 croak "CLEAR forbidden for this type of array.";
78 }
79  
80 sub PUSH {
81 my ( $self, @values ) = @_;
82  
83 croak "PUSH forbidden for this type of array.";
84 }
85  
86 sub UNSHIFT {
87 my ( $self, @values ) = @_;
88  
89 croak "UNSHIFT forbidden for this type of array.";
90 }
91  
92 sub POP {
93 my ( $self ) = @_;
94  
95 croak "POP forbidden for this type of array.";
96 }
97  
98 sub SHIFT {
99 my ( $self ) = @_;
100  
101 croak "SHIFT forbidden for this type of array.";
102 }
103  
104 sub SPLICE {
105 my ( $self, $offset, $length, @values ) = @_;
106  
107 croak "SPLICE forbidden for this type of array.";
108 }
109  
110 sub UNTIE {
111 my ( $self ) = @_;
112  
113 return;
114 }
115  
116 sub _load_name {
117 my ( $self, $name ) = @_;
118 my @addrs = Zonemaster::Recursor->get_addresses_for( $name );
119 foreach my $addr ( sort { $a->ip cmp $b->ip } @addrs ) {
120 my $ns = Zonemaster::Nameserver->new( { name => $name, address => $addr } );
121 if ( not grep { "$ns" eq "$_" } @{ $self->ary } ) {
122 push @{ $self->ary }, $ns;
123 }
124 }
125  
126 return;
127 }
128  
129 no Moose;
130 __PACKAGE__->meta->make_immutable;
131  
132 1;
133  
134 =head1 NAME
135  
136 Zonemaster::NSArray - Class implementing arrays that lazily looks up name server addresses from their names
137  
138 =head1 SYNOPSIS
139  
140 tie @ary, 'Zonemaster::NSArray', @ns_names
141  
142 =head1 DESCRIPTION
143  
144 This class is used for the C<glue> and C<ns> attributes of the
145 L<Zonemaster::Zone> class. It is initially seeded with a list of
146 names, which will be expanded into proper L<Zonemaster::Nameserver>
147 objects on demand. Be careful with using Perl functions that act on
148 whole arrays (particularly C<foreach>), since they will usually force
149 the entire array to expand, negating the use of the lazy-loading.
150  
151 =head1 METHODS
152  
153 These are all methods implementing the Perl tie interface. They have no independent use.
154  
155 =over
156  
157 =item TIEARRAY
158  
159 =item STORE
160  
161 =item STORESIZE
162  
163 =item FETCH
164  
165 =item FETCHSIZE
166  
167 =item EXISTS
168  
169 =item DELETE
170  
171 =item CLEAR
172  
173 =item PUSH
174  
175 =item UNSHIFT
176  
177 =item POP
178  
179 =item SHIFT
180  
181 =item SPLICE
182  
183 =item UNTIE
184  
185 =back
186  
187 =head1 AUTHOR
188  
189 Calle Dybedahl, C<< <calle at init.se> >>
190  
191 =cut