0.04
[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
3f4dd8b7 55sub print { CORE::print %{$_[0]} }
56sub say { CORE::print %{$_[0]}, "\n" }
57
31d40d73 581;
59
60__END__
61
62=pod
63
64=head1 NAME
65
66Moose::Autobox::Hash - the Hash role
67
68=head1 SYNOPOSIS
69
70 use Moose::Autobox;
31d40d73 71
8937074a 72 print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
31d40d73 73
74=head1 DESCRIPTION
75
8937074a 76This is a role to describes a Hash value.
77
260cc81f 78=head1 METHODS
79
80=over 4
81
260cc81f 82=item B<delete>
83
bc9177cb 84=item B<merge>
85
86Takes a hashref and returns a new hashref with right precedence
87shallow merging.
88
260cc81f 89=back
90
5272f13f 91=head2 Indexed implementation
260cc81f 92
93=over 4
94
95=item B<at>
96
97=item B<put>
98
99=item B<exists>
100
101=item B<keys>
102
103=item B<values>
104
105=item B<kv>
106
107=back
108
5272f13f 109=over 4
110
111=item B<meta>
112
3f4dd8b7 113=item B<print>
114
115=item B<say>
116
5272f13f 117=back
118
31d40d73 119=head1 BUGS
120
121All complex software has bugs lurking in it, and this module is no
122exception. If you find a bug please either email me, or add the bug
123to cpan-RT.
124
125=head1 AUTHOR
126
127Stevan Little E<lt>stevan@iinteractive.comE<gt>
128
129=head1 COPYRIGHT AND LICENSE
130
b3cb7038 131Copyright 2006-2007 by Infinity Interactive, Inc.
31d40d73 132
133L<http://www.iinteractive.com>
134
135This library is free software; you can redistribute it and/or modify
136it under the same terms as Perl itself.
137
bc9177cb 138=cut
139