From: Anders Nor Berle Date: Wed, 11 Apr 2007 01:15:22 +0000 (+0000) Subject: Add merge method for hashes. X-Git-Tag: 0_04~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose-Autobox.git;a=commitdiff_plain;h=bc9177cb2a3f27e0480556babfdc55ee4fa66628 Add merge method for hashes. --- diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm index 7fd63cd..270f4ae 100644 --- a/lib/Moose/Autobox/Hash.pm +++ b/lib/Moose/Autobox/Hash.pm @@ -1,7 +1,9 @@ package Moose::Autobox::Hash; use Moose::Role 'with'; -our $VERSION = '0.01'; +use Carp qw(croak); + +our $VERSION = '0.02'; with 'Moose::Autobox::Ref', 'Moose::Autobox::Indexed'; @@ -11,6 +13,13 @@ sub delete { CORE::delete $hash->{$key}; } +sub merge { + my ($left, $right) = @_; + croak "You must pass a hashref as argument to merge" + unless ref $right eq 'HASH'; + return { %$left, %$right }; +} + # ::Indexed implementation sub at { @@ -69,6 +78,11 @@ This is a role to describes a Hash value. =item B +=item B + +Takes a hashref and returns a new hashref with right precedence +shallow merging. + =back =head2 Indexed implementation @@ -105,6 +119,8 @@ to cpan-RT. Stevan Little Estevan@iinteractive.comE +Anders Nor Berle Edebolaz@gmail.comE + =head1 COPYRIGHT AND LICENSE Copyright 2006 by Infinity Interactive, Inc. @@ -114,4 +130,5 @@ L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut + diff --git a/t/001_basic.t b/t/001_basic.t index 1e24b4b..bff5f20 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 55; +use Test::More tests => 56; BEGIN { use_ok('Moose::Autobox'); @@ -212,4 +212,9 @@ $h, { one => 1, two => 2, three => 3 }, '... got the value deleted correctly'); +is_deeply( +$h->merge({ three => 33, four => 44 }), +{ one => 1, two => 2, three => 33, four => 44 }, +'... got the hashes merged correctly'); +