blib/lib/SNMP/Effective/HostList.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 74 CC 3 SCAR 13.5
CC
3
Cov
0%
add_host
12.0
new
2.0
length
2.0
CRAP 3.9
1 package SNMP::Effective::HostList;
2  
3 =head1 NAME
4  
5 SNMP::Effective::HostList - Helper module for SNMP::Effective
6  
7 =head1 DESCRIPTION
8  
9 An object from this class holds an hash-ref where the keys are
10 hostnames and the values are L<SNMP::Effective::Host> objects.
11  
12 =cut
13  
14 use warnings;
15 use strict;
16 use Carp qw/ confess cluck /;
17 use SNMP::Effective::Host;
18  
19 use overload '""' => sub { 0 + keys %{ $_[0] } };
20 use overload '@{}' => sub { my @array; tie @array, ref $_[0], $_[0]; \@array };
21  
22 =head1 METHODS
23  
24 =head2 new
25  
26 Object constructor.
27  
28 =cut
29  
30 sub new {
31 return bless {}, (ref $_[0] || $_[0]);
32 }
33  
34 =head2 length
35  
36 $int = $self->length;
37  
38 Returns the number of hosts in this hostlist.
39  
40 =cut
41  
42 sub length {
43 return 0 + keys %{$_[0]};
44 }
45  
46 =head2 get_host
47  
48 $host_obj = $self->get_host($address);
49  
50 =cut
51  
52 sub get_host {
53 return $_[0]->{$_[1]};
54 }
55  
56 =head2 add_host
57  
58 $self->add_host(address => $str, ...);
59 $self->add_host($host_obj);
60  
61 =cut
62  
63 sub add_host {
64 my $self = shift;
65 my $host;
66  
67 if(ref $_[0]) {
68 return $self->{ $_[0]->address } = $_[0];
69 }
70 elsif(my %args = @_) {
71 return $self->{ $args{'address'} } = SNMP::Effective::Host->new(\%args);
72 }
73  
74 confess 'Invalid input to $self->add_host(...)';
75 }
76  
77 =head2 shift
78  
79 $host = $self->shift;
80  
81 Remove a host object from this hostlist, and return it.
82  
83 =cut
84  
85 sub shift {
86 my $self = shift;
87 my $key = (keys %$self)[0] or return;
88 return delete $self->{$key};
89 }
90  
91 sub TIEARRAY {
92 cluck "Working on a tied HostList is deprecated";
93 return $_[1];
94 }
95  
96 sub FETCHSIZE {
97 cluck "Working on a tied HostList is deprecated";
98 &length;
99 }
100  
101 sub SHIFT {
102 cluck "Working on a tied HostList is deprecated";
103 &shift;
104 }
105  
106 =head1 AUTHOR
107  
108 =head1 ACKNOWLEDGEMENTS
109  
110 =head1 COPYRIGHT & LICENSE
111  
112 See L<SNMP::Effective>
113  
114 =cut
115  
116 1;