X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FAutobox%2FHash.pm;h=2ec40e324b7082fa030538c09693059c77816d11;hb=a1f00ce9bc1114d8573e56079e46db8687a01602;hp=50d77dc289da9e148a00614fdcd17dc8bcd6028c;hpb=6cf5bcf23399cc82e234f91a2e78f262bf70eab1;p=gitmo%2FMoose-Autobox.git diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm index 50d77dc..2ec40e3 100644 --- a/lib/Moose/Autobox/Hash.pm +++ b/lib/Moose/Autobox/Hash.pm @@ -1,7 +1,7 @@ package Moose::Autobox::Hash; use Moose::Role 'with'; -our $VERSION = '0.01'; +our $VERSION = '0.13'; with 'Moose::Autobox::Ref', 'Moose::Autobox::Indexed'; @@ -11,8 +11,34 @@ sub delete { CORE::delete $hash->{$key}; } +sub merge { + my ($left, $right) = @_; + 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 { + my ($hash, $index) = @_; + $hash->{$index}; +} + +sub put { + my ($hash, $index, $value) = @_; + $hash->{$index} = $value; +} + sub exists { my ($hash, $key) = @_; CORE::exists $hash->{$key}; @@ -33,4 +59,141 @@ sub kv { [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ]; } -1; \ No newline at end of file +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" } + +1; + +__END__ + +=pod + +=head1 NAME + +Moose::Autobox::Hash - the Hash role + +=head1 SYNOPOSIS + + use Moose::Autobox; + + print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two' + +=head1 DESCRIPTION + +This is a role to describes a Hash value. + +=head1 METHODS + +=over 4 + +=item B + +=item B + +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 + +=over 4 + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=back + +=over 4 + +=item B + +=item B + +=item B + +=back + +=head1 BUGS + +All complex software has bugs lurking in it, and this module is no +exception. If you find a bug please either email me, or add the bug +to cpan-RT. + +=head1 AUTHOR + +Stevan Little Estevan@iinteractive.comE + +=head1 COPYRIGHT AND LICENSE + +Copyright 2006-2008 by Infinity Interactive, Inc. + +L + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut +