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 | |
6cf5bcf2 |
26 | # ::Indexed implementation |
5f654d8e |
27 | |
31d40d73 |
28 | sub at { |
29 | my ($hash, $index) = @_; |
30 | $hash->{$index}; |
31 | } |
32 | |
33 | sub put { |
34 | my ($hash, $index, $value) = @_; |
35 | $hash->{$index} = $value; |
36 | } |
37 | |
5f654d8e |
38 | sub exists { |
39 | my ($hash, $key) = @_; |
40 | CORE::exists $hash->{$key}; |
41 | } |
e6bb88b0 |
42 | |
5f654d8e |
43 | sub keys { |
44 | my ($hash) = @_; |
e6bb88b0 |
45 | [ CORE::keys %$hash ]; |
5f654d8e |
46 | } |
47 | |
48 | sub values { |
49 | my ($hash) = @_; |
e6bb88b0 |
50 | [ CORE::values %$hash ]; |
51 | } |
52 | |
53 | sub kv { |
54 | my ($hash) = @_; |
55 | [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ]; |
5f654d8e |
56 | } |
57 | |
18ef6e1c |
58 | sub slice { |
59 | my ($hash, $keys) = @_; |
60 | return [ @{$hash}{@$keys} ]; |
61 | }; |
62 | |
3f4dd8b7 |
63 | sub print { CORE::print %{$_[0]} } |
64 | sub say { CORE::print %{$_[0]}, "\n" } |
65 | |
31d40d73 |
66 | 1; |
67 | |
68 | __END__ |
69 | |
70 | =pod |
71 | |
72 | =head1 NAME |
73 | |
74 | Moose::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 |
84 | This 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 | |
94 | Takes a hashref and returns a new hashref with right precedence |
95 | shallow merging. |
96 | |
f7ea23f6 |
97 | =item B<hslice> |
98 | |
99 | Slices 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 | |
135 | All complex software has bugs lurking in it, and this module is no |
136 | exception. If you find a bug please either email me, or add the bug |
137 | to cpan-RT. |
138 | |
139 | =head1 AUTHOR |
140 | |
141 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
142 | |
143 | =head1 COPYRIGHT AND LICENSE |
144 | |
ea4e64bf |
145 | Copyright 2006-2008 by Infinity Interactive, Inc. |
31d40d73 |
146 | |
147 | L<http://www.iinteractive.com> |
148 | |
149 | This library is free software; you can redistribute it and/or modify |
150 | it under the same terms as Perl itself. |
151 | |
bc9177cb |
152 | =cut |
153 | |