diff --git a/IP.pm b/IP.pm index f3c7668..7b74a85 100644 --- a/IP.pm +++ b/IP.pm @@ -1,6 +1,6 @@ #!/usr/bin/perl -w -# $Id: IP.pm,v 1.22 2004/03/02 20:23:57 lem Exp $ +# $Id: IP.pm,v 1.24 2004/10/11 15:40:29 lem Exp $ package NetAddr::IP; @@ -37,7 +37,7 @@ =cut -require 5.6.0; +require 5.006_000; use Carp; use Socket; use strict; @@ -48,7 +48,7 @@ our @ISA = qw(Exporter); -our $VERSION = '3.20'; +our $VERSION = '3.21'; ############################################# # These are the overload methods, placed here @@ -70,9 +70,7 @@ $_[0]->{bits} ]; }, - '""' => sub { - $_[0]->cidr(); - }, + '""' => sub { $_[0]->cidr(); }, 'eq' => sub { my $a = ref $_[0] eq 'NetAddr::IP' ? $_[0]->cidr : $_[0]; @@ -780,6 +778,48 @@ =back +=head2 Serializing and Deserializing + +This module defines hooks to collaborate with L for +serializing C objects, through compact and human readable +strings. You can revert to the old format by invoking this module as + + use NetAddr::IP ':old_storable'; + +You must do this if you have legacy data files containing NetAddr::IP +objects stored using the L module. + +=cut + +sub import +{ + unless (grep { $_ eq ':old_storable' } @_) + { + *{STORABLE_freeze} = sub + { + my $self = shift; + return $self->cidr(); # use stringification + }; + *{STORABLE_thaw} = sub + { + my $self = shift; + my $cloning = shift; # Not used + my $serial = shift; + + my $ip = new NetAddr::IP $serial; + $self->{addr} = $ip->{addr}; + $self->{mask} = $ip->{mask}; + $self->{bits} = $ip->{bits}; + return; + }; + } + + @_ = grep { $_ ne ':old_storable' } @_; + NetAddr::IP->export_to_level(1, @_); +} + +=pod + =head2 Methods =over @@ -1586,7 +1626,7 @@ =head1 HISTORY -$Id: IP.pm,v 1.22 2004/03/02 20:23:57 lem Exp $ +$Id: IP.pm,v 1.24 2004/10/11 15:40:29 lem Exp $ =over @@ -2161,6 +2201,12 @@ Fixed rt bug #5478 in t/00-load.t. +=item 3.21 + +Fixed minor v-string problem pointed out by Steve Snodgrass (Thanks +Steve!). NetAddr::IP can now collaborate with Storable to serialize +itself. + =back =head1 AUTHOR diff --git a/MANIFEST b/MANIFEST index 4b2290a..58f8ab1 100644 --- a/MANIFEST +++ b/MANIFEST @@ -29,6 +29,8 @@ t/v4-basem.t t/v4-first.t t/wildcard.t +t/old-store.t +t/new-store.t t/v4-compact.t t/v4-numeric.t t/v6-numeric.t diff --git a/META.yml b/META.yml index f98ab68..90c6e65 100644 --- a/META.yml +++ b/META.yml @@ -1,7 +1,7 @@ # http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: NetAddr-IP -version: 3.20 +version: 3.21 version_from: IP.pm installdirs: site requires: diff --git a/Makefile.PL b/Makefile.PL index b9e4b53..bd47210 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -2,7 +2,7 @@ # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. -# $Id: Makefile.PL,v 1.8 2003/11/27 20:01:45 lem Exp $ +# $Id: Makefile.PL,v 1.9 2004/10/11 15:40:29 lem Exp $ my $checker = 0; @@ -26,6 +26,26 @@ ; } +print < 'NetAddr::IP', 'VERSION_FROM' => 'IP.pm', # finds $VERSION @@ -37,7 +57,3 @@ (ABSTRACT_FROM => 'IP.pm', AUTHOR => 'Luis E. Mu�oz ') : ()), ); - - - - diff --git a/t/new-store.t b/t/new-store.t new file mode 100644 index 0000000..82d8b15 --- /dev/null +++ b/t/new-store.t @@ -0,0 +1,41 @@ +# t/new-store.t - test new Storable related - methods +# $Id: new-store.t,v 1.1 2004/10/11 15:40:29 lem Exp $ + +use Test::More; + +my $tests = 7; + +plan tests => $tests; + +SKIP: +{ + skip "Failed to use Storable", $tests + unless use_ok("Storable", 'freeze', 'thaw'); + + skip "Failed to use NetAddr::IP", $tests + unless use_ok("NetAddr::IP"); + + my $oip = new NetAddr::IP "localhost"; + my $nip; + + isa_ok($oip, 'NetAddr::IP', 'Correct return type'); + + my $serialized; + + eval { $serialized = freeze($oip) }; + unless (ok(!$@, "Freezing")) + { + diag $@; + } + +# diag "Result is '$serialized'"; + + eval { $nip = thaw($serialized) }; + unless (ok(!$@, "Thawing")) + { + diag $@; + } + + isa_ok($nip, 'NetAddr::IP', 'Recovered correct type'); + is("$nip", "$oip", "New object eq original object"); +} diff --git a/t/old-store.t b/t/old-store.t new file mode 100644 index 0000000..a732fae --- /dev/null +++ b/t/old-store.t @@ -0,0 +1,41 @@ +# t/old-store.t - test backwards compatible Storable interaction +# $Id: old-store.t,v 1.1 2004/10/11 15:40:29 lem Exp $ + +use Test::More; + +my $tests = 7; + +plan tests => $tests; + +SKIP: +{ + skip "Failed to use Storable", $tests + unless use_ok("Storable", 'freeze', 'thaw'); + + skip "Failed to use NetAddr::IP", $tests + unless use_ok("NetAddr::IP", ':old_storable'); + + my $oip = new NetAddr::IP "localhost"; + my $nip; + + isa_ok($oip, 'NetAddr::IP', 'Correct return type'); + + my $serialized; + + eval { $serialized = freeze($oip) }; + unless (ok(!$@, "Freezing")) + { + diag $@; + } + +# diag "Result is '$serialized'"; + + eval { $nip = thaw($serialized) }; + unless (ok(!$@, "Thawing")) + { + diag $@; + } + + isa_ok($nip, 'NetAddr::IP', 'Recovered correct type'); + is("$nip", "$oip", "New object eq original object"); +}