From: Jonathan Rockway Date: Thu, 17 Apr 2008 06:52:09 +0000 (+0000) Subject: add hslice X-Git-Tag: 0_08~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose-Autobox.git;a=commitdiff_plain;h=f7ea23f6550a15ee9ef222ff2b851eb78f803eac add hslice --- diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm index 0bcb420..2f58a33 100644 --- a/lib/Moose/Autobox/Hash.pm +++ b/lib/Moose/Autobox/Hash.pm @@ -20,6 +20,11 @@ sub merge { return { %$left, %$right }; } +sub hslice { + my ($hash, $keys) = @_; + return { map { $_ => $hash->{$_} } @$keys }; +} + # ::Indexed implementation sub at { @@ -91,6 +96,10 @@ This is a role to describes a Hash value. Takes a hashref and returns a new hashref with right precedence shallow merging. +=item B + +Slices a hash but returns the keys and values as a new hashref. + =back =head2 Indexed implementation diff --git a/t/001_basic.t b/t/001_basic.t index 18fecd6..ebf9016 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 63; +use Test::More tests => 64; BEGIN { use_ok('Moose::Autobox'); @@ -247,3 +247,7 @@ is( $h->dump, is_deeply( { one => 1, two => 2, three => 3 }->slice([qw/three one/]), [ qw/3 1/ ], '... hash slices ok' ); + +is_deeply( { one => 1, two => 2, three => 3 }->hslice([qw/two three/]), + { two => 2, three => 3 }, + '... hash hslices ok' );