Permalink
Browse files

Add Storable serialization support

  • Loading branch information...
1 parent 7e6c2ba commit 3d22ddbbdaa6229fc4d837da1ba57b42647967c5 @ap ap committed Feb 13, 2012
Showing with 42 additions and 0 deletions.
  1. +1 −0 Changes
  2. +1 −0 MANIFEST
  3. +16 −0 lib/Hash/MultiValue.pm
  4. +24 −0 t/storable.t
View
@@ -2,6 +2,7 @@ Revision history for Perl extension Hash::MultiValue
- Reapply set method optimisation that was lost in 0.11
- *really* fix uninitialized warnings in tests (RT#74096)
+ - Add support for Storable serialization
0.11 Sun Feb 12 13:04:54 PST 2012
- Fix segfaulting splice invocation on perls < 5.8.7
View
@@ -30,6 +30,7 @@ t/hash.t
t/multi.t
t/ref.t
t/set.t
+t/storable.t
t/threads.t
t/write.t
tools/benchmark.pl
View
@@ -254,6 +254,22 @@ sub as_hashref_multi {
sub multi { $_[0]->as_hashref_multi }
+sub STORABLE_freeze {
+ my $self = shift;
+ my $this = refaddr $self;
+ return '', $keys{$this}, $values{$this};
+}
+
+sub STORABLE_thaw {
+ my $self = shift;
+ my ($is_cloning, $serialised, $k, $v) = @_;
+ my $this = refaddr $self;
+ $keys {$this} = $k;
+ $values{$this} = $v;
+ @{$self}{@$k} = @$v;
+ return $self;
+}
+
1;
__END__
View
@@ -0,0 +1,24 @@
+use strict;
+use Test::More;
+use Hash::MultiValue;
+use Storable qw(freeze thaw dclone);
+
+my $l = [ qw( foo a bar baz foo b bar quux ) ];
+
+my $hash = Hash::MultiValue->new( @$l );
+
+is_deeply [ $hash->flatten ], $l, 'flattening works';
+
+my $frozen = freeze $hash;
+undef $hash;
+$hash = thaw $frozen;
+is_deeply [ $hash->flatten ], $l, '... even after deserialisation';
+
+my $clone = dclone $hash;
+is_deeply [ $clone->flatten ], $l, '... and cloning';
+
+$clone->remove('foo');
+my ($n_hash, $n_clone) = map scalar @{[$_->flatten]}, $hash, $clone;
+ok $n_hash > $n_clone, '... which makes independent objects';
+
+done_testing;

0 comments on commit 3d22ddb

Please sign in to comment.