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