62b894670ba2f1eb129390a7ebf5eb60d67429b1
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Hash.pm
1 package Moose::Autobox::Hash;
2 use Moose::Role 'with';
3
4 our $VERSION = '0.03';
5
6 with 'Moose::Autobox::Ref',
7      'Moose::Autobox::Indexed';
8
9 sub delete { 
10     my ($hash, $key) = @_;
11     CORE::delete $hash->{$key}; 
12 }
13
14 sub merge {
15     my ($left, $right) = @_;
16     Carp::confess "You must pass a hashref as argument to merge"
17         unless ref $right eq 'HASH';
18     return { %$left, %$right };
19 }
20
21 sub hslice {
22     my ($hash, $keys) = @_;
23     return { map { $_ => $hash->{$_} } @$keys };
24 }
25
26 # ::Indexed implementation
27
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
38 sub exists { 
39     my ($hash, $key) = @_;
40     CORE::exists $hash->{$key}; 
41 }
42
43 sub keys { 
44     my ($hash) = @_;
45     [ CORE::keys %$hash ];
46 }
47
48 sub values { 
49     my ($hash) = @_;    
50     [ CORE::values %$hash ]; 
51 }
52
53 sub kv {
54     my ($hash) = @_;    
55     [ CORE::map { [ $_, $hash->{$_} ] } CORE::keys %$hash ];    
56 }
57
58 sub slice {
59     my ($hash, $keys) = @_;
60     return [ @{$hash}{@$keys} ];
61 };
62
63 sub print   { CORE::print %{$_[0]} }
64 sub say     { CORE::print %{$_[0]}, "\n" }
65
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;
79   
80   print { one => 1, two => 2 }->keys->join(', '); # prints 'one, two'
81
82 =head1 DESCRIPTION
83
84 This is a role to describes a Hash value. 
85
86 =head1 METHODS
87
88 =over 4
89
90 =item B<delete>
91
92 =item B<merge>
93
94 Takes a hashref and returns a new hashref with right precedence
95 shallow merging.
96
97 =item B<hslice>
98
99 Slices a hash but returns the keys and values as a new hashref.
100
101 =back
102
103 =head2 Indexed implementation
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
119 =item B<slice>
120
121 =back
122
123 =over 4
124
125 =item B<meta>
126
127 =item B<print>
128
129 =item B<say>
130
131 =back
132
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
145 Copyright 2006-2008 by Infinity Interactive, Inc.
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
152 =cut
153