| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | =head1 NAME | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | Net::CIDR::Lookup::Tie | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | This is a L interface to L, see there for | 
| 8 |  |  |  |  |  |  | details. | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | The tied hash accepts net blocks as keys in the same syntax as | 
| 11 |  |  |  |  |  |  | C's C or C and stores arbitrary (with the | 
| 12 |  |  |  |  |  |  | exception of C) scalar values under these. The same coalescing as in | 
| 13 |  |  |  |  |  |  | C takes place, so if you add any number of different keys | 
| 14 |  |  |  |  |  |  | you may end up with a hash containing I keys if any mergers took place. | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | Of course you can retrieve the corresponding net block's value for any key that | 
| 17 |  |  |  |  |  |  | is I within that block, so the number of possible lookup keys is | 
| 18 |  |  |  |  |  |  | usually far greater than that of explicitly stored key/value pairs. | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | use Net::CIDR::Lookup::Tie; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | tie my %t, 'Net::CIDR::Lookup::Tie'; | 
| 25 |  |  |  |  |  |  | $t{'192.168.42.0/24'}   = 1;    # Add first network | 
| 26 |  |  |  |  |  |  | $t{'192.168.43.0/24'}   = 1;    # Automatic coalescing to a /23 | 
| 27 |  |  |  |  |  |  | $t{'192.168.41.0/24'}   = 2;    # Stays separate due to different value | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | print $t{'192.168.42.100'};		 # prints "1" | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | foreach(keys %h) { ... }			 # Do anything you'd do with a regular hash | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head1 VERSION HISTORY | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | See L | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =head1 METHODS | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =cut | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | package Net::CIDR::Lookup::Tie; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 2 |  |  | 2 |  | 50926 | use strict; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 338 |  | 
| 44 | 2 |  |  | 2 |  | 7 | use warnings; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 37 |  | 
| 45 | 2 |  |  | 2 |  | 5 | use Carp; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 82 |  | 
| 46 | 2 |  |  | 2 |  | 379 | use Net::CIDR::Lookup; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 555 |  | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | our $VERSION = '0.51'; | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub TIEHASH {   ## no critic (Subroutines::RequireArgUnpacking) | 
| 51 | 3 |  |  | 3 |  | 2550 | my $class = shift; | 
| 52 | 3 |  |  |  |  | 9 | bless { tree => Net::CIDR::Lookup->new(@_) }, $class; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =head2 STORE | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | Stores a value under a given key | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =cut | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub STORE { ## no critic (Subroutines::RequireArgUnpacking) | 
| 62 | 4 |  |  | 4 |  | 271 | my $self = shift; | 
| 63 | 4 |  |  |  |  | 4 | undef $self->{keys}; | 
| 64 | 4 | 50 |  |  |  | 11 | if($_[0] =~ /-/) { | 
| 65 | 0 |  |  |  |  | 0 | $self->{tree}->add_range(@_); | 
| 66 |  |  |  |  |  |  | } else { | 
| 67 | 4 |  |  |  |  | 9 | $self->{tree}->add(@_); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =head2 FETCH | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | Fetches the value stored under a given key | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =cut | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub FETCH { | 
| 78 | 3 |  |  | 3 |  | 401 | my ($self, $key) = @_; | 
| 79 | 3 |  |  |  |  | 7 | $self->{tree}->lookup($key); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =head2 FIRSTKEY | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | Gets the first key in the hash. Used for iteration with each() | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =cut | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub FIRSTKEY { | 
| 89 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 90 | 0 |  |  |  |  |  | $self->_updkeys; | 
| 91 | 0 |  |  |  |  |  | each %{$self->{keys}}; | 
|  | 0 |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =head2 NEXTKEY | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | Gets the next key from the hash. Used for iteration with each() | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | =cut | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub NEXTKEY { | 
| 101 | 0 |  |  | 0 |  |  | each %{shift->{keys}}; | 
|  | 0 |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =head2 EXISTS | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | Tests if a key is in the hash. Also returns true for blocks or addresses | 
| 107 |  |  |  |  |  |  | contained within a block that was actually stored. | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =cut | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | sub EXISTS { | 
| 112 | 0 |  |  | 0 |  |  | my ($self, $key) = @_; | 
| 113 | 0 |  |  |  |  |  | $self->_updkeys; | 
| 114 | 0 |  |  |  |  |  | exists $self->{keys}{$key}; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head2 DELETE | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | Delete a key from the hash. Note that the same restrictions as for Net::CIDR::Lookup | 
| 120 |  |  |  |  |  |  | regarding netblock splitting apply! | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =cut | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub DELETE { | 
| 125 | 0 |  |  | 0 |  |  | carp('Deletions are not supported by tied ' . __PACKAGE__ . ' objects yet!'); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head2 CLEAR | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | Deletes all keys and their values. | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =cut | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | sub CLEAR { | 
| 135 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 136 | 0 |  |  |  |  |  | $self->{tree}->clear; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =head2 SCALAR | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | Returns the number of keys in the hash | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =cut | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | sub SCALAR { | 
| 146 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 147 | 0 |  |  |  |  |  | $self->_updkeys; | 
| 148 | 0 |  |  |  |  |  | scalar keys %{$self->{keys}}; | 
|  | 0 |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =head2 _updkeys | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | Private method to update the internal key cache used for iteration | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =cut | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | sub _updkeys { | 
| 158 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 0 | 0 |  |  |  |  | if(defined $self->{keys}) { | 
| 161 | 0 |  |  |  |  |  | keys %{$self->{keys}};                  # Call in void context to reset | 
|  | 0 |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | } else { | 
| 163 | 0 |  |  |  |  |  | $self->{keys} = $self->{tree}->to_hash; # Recreate hash | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  | 1; |