X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FAutobox%2FHash.pm;h=ed2305216e554174846a6ad73d7f6122d99fdce3;hb=c0ab09e353689f340e840f44b4158fc5a33790db;hp=5c318dc2903f7fef35d4e40ef371001ade338135;hpb=8937074ab98e81964dee9ccf9bbd939ebf3252a0;p=gitmo%2FMoose-Autobox.git
diff --git a/lib/Moose/Autobox/Hash.pm b/lib/Moose/Autobox/Hash.pm
index 5c318dc..ed23052 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.10';
with 'Moose::Autobox::Ref',
'Moose::Autobox::Indexed';
@@ -11,6 +11,22 @@ 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 {
@@ -43,6 +59,34 @@ 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;
+}
+
+
+# End Indexed
+
+sub print { CORE::print %{$_[0]} }
+sub say { CORE::print %{$_[0]}, "\n" }
+
1;
__END__
@@ -56,7 +100,6 @@ Moose::Autobox::Hash - the Hash role
=head1 SYNOPOSIS
use Moose::Autobox;
- use autobox;
print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
@@ -68,13 +111,22 @@ This is a role to describes a Hash value.
=over 4
-=item B
-
=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 Moose::Autobox::Indexed implementation
+=head2 Indexed implementation
=over 4
@@ -90,6 +142,24 @@ This is a role to describes a Hash value.
=item B
+=item B
+
+=item B
+
+=item B
+
+=item B
+
+=back
+
+=over 4
+
+=item B
+
+=item B
+
+=item B
+
=back
=head1 BUGS
@@ -104,11 +174,12 @@ Stevan Little Estevan@iinteractive.comE
=head1 COPYRIGHT AND LICENSE
-Copyright 2006 by Infinity Interactive, Inc.
+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
\ No newline at end of file
+=cut
+