- 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
6our $VERSION = '0.02';
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
6cf5bcf2 23# ::Indexed implementation
5f654d8e 24
31d40d73 25sub at {
26 my ($hash, $index) = @_;
27 $hash->{$index};
28}
29
30sub put {
31 my ($hash, $index, $value) = @_;
32 $hash->{$index} = $value;
33}
34
5f654d8e 35sub exists {
36 my ($hash, $key) = @_;
37 CORE::exists $hash->{$key};
38}
e6bb88b0 39
5f654d8e 40sub keys {
41 my ($hash) = @_;
e6bb88b0 42 [ CORE::keys %$hash ];
5f654d8e 43}
44
45sub values {
46 my ($hash) = @_;
e6bb88b0 47 [ CORE::values %$hash ];
48}
49
50sub kv {
51 my ($hash) = @_;
52 [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ];
5f654d8e 53}
54
31d40d73 551;
56
57__END__
58
59=pod
60
61=head1 NAME
62
63Moose::Autobox::Hash - the Hash role
64
65=head1 SYNOPOSIS
66
67 use Moose::Autobox;
31d40d73 68
8937074a 69 print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
31d40d73 70
71=head1 DESCRIPTION
72
8937074a 73This is a role to describes a Hash value.
74
260cc81f 75=head1 METHODS
76
77=over 4
78
260cc81f 79=item B<delete>
80
bc9177cb 81=item B<merge>
82
83Takes a hashref and returns a new hashref with right precedence
84shallow merging.
85
260cc81f 86=back
87
5272f13f 88=head2 Indexed implementation
260cc81f 89
90=over 4
91
92=item B<at>
93
94=item B<put>
95
96=item B<exists>
97
98=item B<keys>
99
100=item B<values>
101
102=item B<kv>
103
104=back
105
5272f13f 106=over 4
107
108=item B<meta>
109
110=back
111
31d40d73 112=head1 BUGS
113
114All complex software has bugs lurking in it, and this module is no
115exception. If you find a bug please either email me, or add the bug
116to cpan-RT.
117
118=head1 AUTHOR
119
120Stevan Little E<lt>stevan@iinteractive.comE<gt>
121
bc9177cb 122Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
123
31d40d73 124=head1 COPYRIGHT AND LICENSE
125
126Copyright 2006 by Infinity Interactive, Inc.
127
128L<http://www.iinteractive.com>
129
130This library is free software; you can redistribute it and/or modify
131it under the same terms as Perl itself.
132
bc9177cb 133=cut
134