diff --git a/IP.pm b/IP.pm index 39756b7..5572b51 100644 --- a/IP.pm +++ b/IP.pm @@ -423,14 +423,24 @@ return &coalesce; } +=head2 Subtract + + Title : Subtract + Usage : my $remaining_networks = Subtract(\@list_of_networks,\@networks_to_subtract); + Function : computes the network definitions resulting from subtracting @networks_to_subtract from @list_of_networks + Returns : reference to a sorted list of NetAddr::IP objects representing the remaining networks + Args : reference to a sorted list of NetAddr::IP objects (the list must be sorted! eg. by using Compact) + : reference to a sorted list of NetAddr::IP objects to subtract (the list must be sorted! eg. by using Compact) + +=cut sub Subtract { my @add = ref $_[0] eq __PACKAGE__ ? ($_[0]) : @{$_[0]}; my @sub = @{$_[1]}; my @ret; while (@add && @sub) { - if ($sub[0]->contains($add[0])) { + if ($add[0]->within($sub[0])) { shift @add; - } elsif ($add[0]->contains($sub[0])) { + } elsif ($sub[0]->within($add[0])) { my ($low,$up) = Exclude(shift @add,shift @sub); push @ret, @$low; unshift @add, @$up; @@ -446,6 +456,16 @@ return \@ret; } +=head2 Exclude + + Title : Exclude + Usage : my ($remaining_lower_networks,$remaining_upper_networks) = Exclude($original_network,$network_to_exclude); + Function : computes the network definitions resulting from removing the $network_to_exclude from $original_network. + Returns : two list references containing the sorted network definitions 'below' and 'above' the excluded network + Args : NetAddr::IP object of the original network + : NetAddr::IP object of the network to exclude + +=cut sub Exclude { return ([],[]) if $_[0] == $_[1]; return ([$_[0]],[]) unless $_[1]->within($_[0]);