From: Jonathan Rockway Date: Thu, 17 Apr 2008 06:52:02 +0000 (+0000) Subject: add hash slices X-Git-Tag: 0_08~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose-Autobox.git;a=commitdiff_plain;h=18ef6e1c6698a79fb754a81ee1277c8d9bef80f9 add hash slices --- diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm index 8000d29..0bcb420 100644 --- a/lib/Moose/Autobox/Hash.pm +++ b/lib/Moose/Autobox/Hash.pm @@ -3,7 +3,7 @@ use Moose::Role 'with'; use Carp qw(croak); -our $VERSION = '0.02'; +our $VERSION = '0.03'; with 'Moose::Autobox::Ref', 'Moose::Autobox::Indexed'; @@ -52,6 +52,11 @@ sub kv { [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ]; } +sub slice { + my ($hash, $keys) = @_; + return [ @{$hash}{@$keys} ]; +}; + sub print { CORE::print %{$_[0]} } sub say { CORE::print %{$_[0]}, "\n" } @@ -104,6 +109,8 @@ shallow merging. =item B +=item B + =back =over 4 diff --git a/lib/Moose/Autobox/Indexed.pm b/lib/Moose/Autobox/Indexed.pm index 414531b..feb0174 100644 --- a/lib/Moose/Autobox/Indexed.pm +++ b/lib/Moose/Autobox/Indexed.pm @@ -9,6 +9,7 @@ requires 'exists'; requires 'keys'; requires 'values'; requires 'kv'; +requires 'slice'; 1; @@ -74,4 +75,4 @@ 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 dd6c4af..18fecd6 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 62; +use Test::More tests => 63; BEGIN { use_ok('Moose::Autobox'); @@ -244,3 +244,6 @@ is( $h->dump, $h->perl, '... the value is correctly dumped with perl()' ); +is_deeply( { one => 1, two => 2, three => 3 }->slice([qw/three one/]), + [ qw/3 1/ ], + '... hash slices ok' );