Subject: | enhacement list_short_range patch |
Hello,
Can you please add this patch to the module? It adds
"->list_short_range" method that outputs C subnets ranges.
It includes pod and a few tests.
Jozef.
Subject: | Net-CIDR-Lite_list_short_range.patch |
==== Patch <jozef_Net-CIDR-Lite_changes> level 1
Source: a0d78d31-34c8-4843-b369-1f7acfd529b6:/local/Net-CIDR-Lite:233 [local]
Target: b0c1b02c-7ba7-4df7-b273-855bf36df2ab:/cpan/not-mine/Net-CIDR-Lite:227 [mirrored]
(https://cle.sk/repos/pub)
Log:
r230@ant: jk | 2007-11-15 17:39:43 +0100
Net-CIDR-Lite local copy
r231@ant: jk | 2007-11-15 17:46:05 +0100
list_short_range function added
r232@ant: jk | 2007-11-16 11:59:41 +0100
->list_short_range tests
r233@ant: jk | 2007-11-16 12:36:36 +0100
variable names cleanup
=== t/base.t
==================================================================
--- t/base.t (revision 227)
+++ t/base.t (patch jozef_Net-CIDR-Lite_changes level 1)
@@ -8,7 +8,7 @@
use Test;
use strict;
$|++;
-BEGIN { plan tests => 34 };
+BEGIN { plan tests => 38 };
use Net::CIDR::Lite;
ok(1); # If we made it this far, we are ok.
@@ -111,3 +111,22 @@
eval { $err_cidr->add("209.152.214.112/33") };
ok($@ =~ /Bad mask/);
+# Test list shor range
+my @list_short_range = Net::CIDR::Lite->new('0.0.0.0/32')->list_short_range;
+ok(scalar(@list_short_range), 1, 'should have one "range"');
+ok($list_short_range[0], '0.0.0.0', 'that is 0.0.0.0');
+
+@list_short_range = sort Net::CIDR::Lite->new(qw{
+ 10.0.0.1
+ 10.0.0.5
+ 10.0.0.2
+})->list_short_range;
+ok(join(', ', @list_short_range), '10.0.0.1-2, 10.0.0.5');
+
+@list_short_range = sort Net::CIDR::Lite->new(qw{
+ 10.0.0.250-10.0.1.20
+ 10.0.1.22
+ 10.0.2.250-10.0.5.8
+})->list_short_range;
+ok(join(', ', @list_short_range), '10.0.0.250-255, 10.0.1.0-20, 10.0.1.22, 10.0.2.250-255, 10.0.3.0-255, 10.0.4.0-255, 10.0.5.0-8');
+
=== Lite.pm
==================================================================
--- Lite.pm (revision 227)
+++ Lite.pm (patch jozef_Net-CIDR-Lite_changes level 1)
@@ -101,6 +101,54 @@
wantarray ? @results : \@results;
}
+sub list_short_range {
+ my $self = shift;
+
+ my $start;
+ my $total;
+ my @results;
+
+ for my $ip (sort keys %{$$self{RANGES}}) {
+ # we begin new range when $total is zero
+ $start = $ip if not $total;
+
+ # add to total (1 for start of the range or -1 for end of the range)
+ $total += $$self{RANGES}{$ip};
+
+ # in case of end of range
+ if (not $total) {
+ while ($ip gt $start) {
+ $ip = $self->_minus_one($ip);
+
+ # in case of single ip not a range
+ if ($ip eq $start) {
+ push @results,
+ $self->{UNPACK}->($start);
+ next;
+ }
+
+ # get the last ip octet number
+ my $to_octet = ( unpack('C5', $ip) )[4];
+
+ # next ip end will be current end masked by c subnet mask 255.255.255.0 - /24
+ $ip = $ip & $self->{MASKS}[32];
+
+ # if the ip range is in the same c subnet
+ if ($ip eq ($start & $self->{MASKS}[32])) {
+ push @results,
+ $self->{UNPACK}->($start) . "-" . $to_octet;
+ }
+ # otherwise the range start is .0 (end of range masked by c subnet mask)
+ else {
+ push @results,
+ $self->{UNPACK}->($ip & $self->{MASKS}[32]) . "-" . $to_octet;
+ }
+ };
+ }
+ }
+ wantarray ? @results : \@results;
+}
+
sub _init {
my $self = shift;
my $ip = shift;
@@ -504,13 +552,31 @@
=item $cidr->list_range()
- @cidr_list = $cidr->list;
- $list_ref = $cidr->list;
+ @cidr_list = $cidr->list_range;
+ $list_ref = $cidr->list_range;
Returns a list of the merged addresses, but in hyphenated range
format. Returns an array if called in list context, an array reference
if not.
+=item $cidr->list_short_range()
+
+ @cidr_list = $cidr->list_short_range;
+ $list_ref = $cidr->list_short_range;
+
+Returns a list of the C subnet merged addresses, in short hyphenated range
+format. Returns an array if called in list context, an array reference
+if not.
+
+Example:
+
+ 1.1.1.1-2
+ 1.1.1.5-7
+ 1.1.1.254-255
+ 1.1.2.0-2
+ 1.1.3.5
+ 1.1.3.7
+
=item $cidr->find()
$found = $cidr->find($ip);