X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FAutobox%2FHash.pm;h=9ff1b24dfa39b9a6ebf80765581b87634b558070;hb=0cb8c0c845da4b2599d82328eebfd3ba99a50f6d;hp=96cbe5250b8349393e444fd678d877f3f063b938;hpb=3f4dd8b74b711eefe941a391a327389fb05abf7f;p=gitmo%2FMoose-Autobox.git diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm index 96cbe52..9ff1b24 100644 --- a/lib/Moose/Autobox/Hash.pm +++ b/lib/Moose/Autobox/Hash.pm @@ -1,9 +1,7 @@ package Moose::Autobox::Hash; use Moose::Role 'with'; -use Carp qw(croak); - -our $VERSION = '0.02'; +our $VERSION = '0.12'; with 'Moose::Autobox::Ref', 'Moose::Autobox::Indexed'; @@ -15,11 +13,20 @@ sub delete { sub merge { my ($left, $right) = @_; - croak "You must pass a hashref as argument to merge" + Carp::confess "You must pass a hashref as argument to merge" unless ref $right eq 'HASH'; return { %$left, %$right }; } +sub hslice { + my ($hash, $keys) = @_; + return { map { $_ => $hash->{$_} } @$keys }; +} + +sub flatten { + return %{$_[0]} +} + # ::Indexed implementation sub at { @@ -52,6 +59,43 @@ sub kv { [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ]; } +sub slice { + my ($hash, $keys) = @_; + return [ @{$hash}{@$keys} ]; +} + +sub each { + my ($hash, $sub) = @_; + for my $key (CORE::keys %$hash) { + $sub->($key, $hash->{$key}); + } +} + +sub each_key { + my ($hash, $sub) = @_; + $sub->($_) for CORE::keys %$hash; +} + +sub each_value { + my ($hash, $sub) = @_; + $sub->($_) for CORE::values %$hash; +} + +sub each_n_values { + my ($hash, $n, $sub) = @_; + my @keys = CORE::keys %$hash; + my $it = List::MoreUtils::natatime($n, @keys); + + while (my @vals = $it->()) { + $sub->(@$hash{ @vals }); + } + + return; +} + + +# End Indexed + sub print { CORE::print %{$_[0]} } sub say { CORE::print %{$_[0]}, "\n" } @@ -86,6 +130,12 @@ 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. + +=item B + =back =head2 Indexed implementation @@ -104,6 +154,16 @@ shallow merging. =item B +=item B + +=item B + +=item B + +=item B + +=item B + =back =over 4 @@ -126,11 +186,9 @@ to cpan-RT. Stevan Little Estevan@iinteractive.comE -Anders Nor Berle Edebolaz@gmail.comE - =head1 COPYRIGHT AND LICENSE -Copyright 2006 by Infinity Interactive, Inc. +Copyright 2006-2008 by Infinity Interactive, Inc. L