File Coverage

lib/Net/IPTrie/Node.pm
Criterion Covered Total %
statement 43 43 100.0
branch 6 6 100.0
condition 2 3 66.6
subroutine 12 12 100.0
pod 3 4 75.0
total 66 68 97.0


line stmt bran cond sub pod time code
1             package Net::IPTrie::Node;
2              
3 1     1   5 use warnings;
  1         2  
  1         38  
4 1     1   5 use strict;
  1         2  
  1         33  
5 1     1   4 use Carp;
  1         1  
  1         127  
6 1     1   26992 use Class::Struct;
  1         2982  
  1         7  
7 1     1   287 use Scalar::Util qw(weaken);
  1         3  
  1         130  
8 1     1   5 use vars qw($VERSION);
  1         3  
  1         86  
9             $VERSION = '0.7';
10              
11             BEGIN {
12 1     1   16 struct (
13             "Net::IPTrie::_Node" => {
14             'up' => '$',
15             'left' => '$',
16             'right' => '$',
17             'address' => '$',
18             'iaddress' => '$',
19             'prefix' => '$',
20             'data' => '$',
21             });
22             }
23              
24 1     1   1500 use base qw (Net::IPTrie::_Node);
  1         2  
  1         1021  
25              
26             =head1 NAME
27              
28             Net::IPTrie::Node
29              
30             =head1 SYNOPSIS
31              
32             See Net::IPTrie
33              
34             =head1 DESCRIPTION
35              
36             See Net::IPTrie
37              
38             =head1 CLASS METHODS
39              
40             =head2 new - Constructor
41              
42             Arguments:
43             up - Parent node
44             left - Left child node
45             right - Right child node
46             address - Address string
47             prefix - IP prefix (defaults to host mask)
48             iaddress - Integer address
49             data - Scalar (could be a reference to any data structure)
50             Returns:
51             New Net::IPTrie::Node object
52             Examples:
53             my $n = Net::IPTrie::Node->new(up=>$up, address=>"10.0.0.1")
54              
55             =cut
56              
57             sub new {
58 165     165 1 3856 my $ret = shift->SUPER::new(@_);
59 165 100       5739 if ( defined($ret->{'Net::IPTrie::_Node::up'}) ) {
60 163         17811 weaken $ret->{'Net::IPTrie::_Node::up'};
61             }
62 165         5060 return $ret;
63             }
64              
65             sub up {
66 347     347 0 6720 my $self = shift;
67 347 100       1980 if (@_) {
68 165         1967 $self->{'Net::IPTrie::_Node::up'} = shift;
69 165 100       397 if ( defined($self->{'Net::IPTrie::_Node::up'}) ) {
70 163         1464 weaken $self->{'Net::IPTrie::_Node::up'};
71             }
72             }
73 347         9315 return $self->{'Net::IPTrie::_Node::up'};
74             }
75              
76             =head1 INSTANCE METHODS
77             =cut
78              
79             ############################################################################
80              
81             =head2 parent - Find closest parent node with IP information
82              
83             Arguments:
84             None
85             Returns:
86             Node object with address, or undef
87             Examples:
88             my $parent = $node->parent;
89              
90             =cut
91              
92             sub parent {
93 10     10 1 4194 my ($self) = @_;
94              
95 10         68 my $p = $self->up;
96 10   66     312 while ( defined $p && !defined $p->iaddress ){
97 172         1656 $p = $p->up;
98             }
99 10         264 return $p;
100             }
101              
102             ############################################################################
103              
104             =head2 delete - Delete an IP node from the tree
105              
106             Note: The node is actually emptied, not deleted
107              
108             Arguments:
109             None
110             Returns:
111             Deleted (empty) Node object
112             Examples:
113             my $n = $tr->find("10.0.0.1");
114             $n->delete();
115              
116             =cut
117              
118             sub delete {
119 2     2 1 5184 my ($self) = @_;
120 2         52 $self->address(undef);
121 2         53 $self->iaddress(undef);
122 2         53 $self->prefix(undef);
123 2         50 $self->data(undef);
124 2         13 return $self;
125             }
126              
127             # Make sure to return 1
128             1;
129              
130             =head1 AUTHOR
131              
132             Carlos Vicente
133              
134             =head1 SEE ALSO
135              
136             Net::IPTrie
137              
138             =head1 LICENCE AND COPYRIGHT
139              
140             Copyright (c) 2007-2010, Carlos Vicente . All rights reserved.
141              
142             This module is free software; you can redistribute it and/or
143             modify it under the same terms as Perl itself. See L.
144              
145              
146             =head1 DISCLAIMER OF WARRANTY
147              
148             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
149             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
150             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
151             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
152             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
153             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
154             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
155             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
156             NECESSARY SERVICING, REPAIR, OR CORRECTION.
157              
158             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
159             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
160             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
161             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
162             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
163             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
164             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
165             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
166             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
167             SUCH DAMAGES.
168             =cut