prep next release
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Hash.pm
CommitLineData
5f654d8e 1package Moose::Autobox::Hash;
2use Moose::Role 'with';
3
a1f00ce9 4our $VERSION = '0.13';
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
f089b856 84sub each_n_values {
85 my ($hash, $n, $sub) = @_;
86 my @keys = CORE::keys %$hash;
87 my $it = List::MoreUtils::natatime($n, @keys);
88
89 while (my @vals = $it->()) {
90 $sub->(@$hash{ @vals });
91 }
92
93 return;
94}
95
2dcdd7d7 96
97# End Indexed
18ef6e1c 98
3f4dd8b7 99sub print { CORE::print %{$_[0]} }
100sub say { CORE::print %{$_[0]}, "\n" }
101
31d40d73 1021;
103
104__END__
105
106=pod
107
108=head1 NAME
109
110Moose::Autobox::Hash - the Hash role
111
112=head1 SYNOPOSIS
113
114 use Moose::Autobox;
31d40d73 115
8937074a 116 print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
31d40d73 117
118=head1 DESCRIPTION
119
8937074a 120This is a role to describes a Hash value.
121
260cc81f 122=head1 METHODS
123
124=over 4
125
260cc81f 126=item B<delete>
127
bc9177cb 128=item B<merge>
129
130Takes a hashref and returns a new hashref with right precedence
131shallow merging.
132
f7ea23f6 133=item B<hslice>
134
135Slices a hash but returns the keys and values as a new hashref.
136
2197a7c0 137=item B<flatten>
138
260cc81f 139=back
140
5272f13f 141=head2 Indexed implementation
260cc81f 142
143=over 4
144
145=item B<at>
146
147=item B<put>
148
149=item B<exists>
150
151=item B<keys>
152
153=item B<values>
154
155=item B<kv>
156
18ef6e1c 157=item B<slice>
158
c0ab09e3 159=item B<each>
160
161=item B<each_key>
162
163=item B<each_value>
164
27fc2bf8 165=item B<each_n_values>
166
260cc81f 167=back
168
5272f13f 169=over 4
170
171=item B<meta>
172
3f4dd8b7 173=item B<print>
174
175=item B<say>
176
5272f13f 177=back
178
31d40d73 179=head1 BUGS
180
181All complex software has bugs lurking in it, and this module is no
182exception. If you find a bug please either email me, or add the bug
183to cpan-RT.
184
185=head1 AUTHOR
186
187Stevan Little E<lt>stevan@iinteractive.comE<gt>
188
189=head1 COPYRIGHT AND LICENSE
190
ea4e64bf 191Copyright 2006-2008 by Infinity Interactive, Inc.
31d40d73 192
193L<http://www.iinteractive.com>
194
195This library is free software; you can redistribute it and/or modify
196it under the same terms as Perl itself.
197
bc9177cb 198=cut
199