prep next release
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Hash.pm
CommitLineData
5f654d8e 1package Moose::Autobox::Hash;
2use Moose::Role 'with';
3
e3598a18 4our $VERSION = '0.10';
5f654d8e 5
6cf5bcf2 6with 'Moose::Autobox::Ref',
7 'Moose::Autobox::Indexed';
8
9sub delete {
10 my ($hash, $key) = @_;
11 CORE::delete $hash->{$key};
12}
13
bc9177cb 14sub merge {
15 my ($left, $right) = @_;
bf86a67c 16 Carp::confess "You must pass a hashref as argument to merge"
bc9177cb 17 unless ref $right eq 'HASH';
18 return { %$left, %$right };
19}
20
f7ea23f6 21sub hslice {
22 my ($hash, $keys) = @_;
23 return { map { $_ => $hash->{$_} } @$keys };
24}
25
2197a7c0 26sub flatten {
27 return %{$_[0]}
28}
29
6cf5bcf2 30# ::Indexed implementation
5f654d8e 31
31d40d73 32sub at {
33 my ($hash, $index) = @_;
34 $hash->{$index};
35}
36
37sub put {
38 my ($hash, $index, $value) = @_;
39 $hash->{$index} = $value;
40}
41
5f654d8e 42sub exists {
43 my ($hash, $key) = @_;
44 CORE::exists $hash->{$key};
45}
e6bb88b0 46
5f654d8e 47sub keys {
48 my ($hash) = @_;
e6bb88b0 49 [ CORE::keys %$hash ];
5f654d8e 50}
51
52sub values {
53 my ($hash) = @_;
e6bb88b0 54 [ CORE::values %$hash ];
55}
56
57sub kv {
58 my ($hash) = @_;
59 [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ];
5f654d8e 60}
61
18ef6e1c 62sub slice {
63 my ($hash, $keys) = @_;
64 return [ @{$hash}{@$keys} ];
2dcdd7d7 65}
66
67sub each {
68 my ($hash, $sub) = @_;
69 for my $key (CORE::keys %$hash) {
70 $sub->($key, $hash->{$key});
71 }
72}
73
74sub each_key {
75 my ($hash, $sub) = @_;
76 $sub->($_) for CORE::keys %$hash;
77}
78
79sub each_value {
80 my ($hash, $sub) = @_;
81 $sub->($_) for CORE::values %$hash;
82}
83
84
85# End Indexed
18ef6e1c 86
3f4dd8b7 87sub print { CORE::print %{$_[0]} }
88sub say { CORE::print %{$_[0]}, "\n" }
89
31d40d73 901;
91
92__END__
93
94=pod
95
96=head1 NAME
97
98Moose::Autobox::Hash - the Hash role
99
100=head1 SYNOPOSIS
101
102 use Moose::Autobox;
31d40d73 103
8937074a 104 print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
31d40d73 105
106=head1 DESCRIPTION
107
8937074a 108This is a role to describes a Hash value.
109
260cc81f 110=head1 METHODS
111
112=over 4
113
260cc81f 114=item B<delete>
115
bc9177cb 116=item B<merge>
117
118Takes a hashref and returns a new hashref with right precedence
119shallow merging.
120
f7ea23f6 121=item B<hslice>
122
123Slices a hash but returns the keys and values as a new hashref.
124
2197a7c0 125=item B<flatten>
126
260cc81f 127=back
128
5272f13f 129=head2 Indexed implementation
260cc81f 130
131=over 4
132
133=item B<at>
134
135=item B<put>
136
137=item B<exists>
138
139=item B<keys>
140
141=item B<values>
142
143=item B<kv>
144
18ef6e1c 145=item B<slice>
146
260cc81f 147=back
148
5272f13f 149=over 4
150
151=item B<meta>
152
3f4dd8b7 153=item B<print>
154
155=item B<say>
156
5272f13f 157=back
158
31d40d73 159=head1 BUGS
160
161All complex software has bugs lurking in it, and this module is no
162exception. If you find a bug please either email me, or add the bug
163to cpan-RT.
164
165=head1 AUTHOR
166
167Stevan Little E<lt>stevan@iinteractive.comE<gt>
168
169=head1 COPYRIGHT AND LICENSE
170
ea4e64bf 171Copyright 2006-2008 by Infinity Interactive, Inc.
31d40d73 172
173L<http://www.iinteractive.com>
174
175This library is free software; you can redistribute it and/or modify
176it under the same terms as Perl itself.
177
bc9177cb 178=cut
179