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