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