fix the erroneous ->kv call in the docs
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / Hash.pm
1
2 package Moose::Meta::Attribute::Native::Trait::Hash;
3 use Moose::Role;
4
5 with 'Moose::Meta::Attribute::Native::Trait';
6
7 sub _helper_type { 'HashRef' }
8
9 no Moose::Role;
10
11 1;
12
13 # ABSTRACT: Helper trait for HashRef attributes
14
15 __END__
16
17 =pod
18
19 =head1 SYNOPSIS
20
21   package Stuff;
22   use Moose;
23
24   has 'options' => (
25       traits    => ['Hash'],
26       is        => 'ro',
27       isa       => 'HashRef[Str]',
28       default   => sub { {} },
29       handles   => {
30           set_option     => 'set',
31           get_option     => 'get',
32           has_no_options => 'is_empty',
33           num_options    => 'count',
34           delete_option  => 'delete',
35           option_pairs   => 'kv',
36       },
37   );
38
39 =head1 DESCRIPTION
40
41 This trait provides native delegation methods for hash references.
42
43 =head1 PROVIDED METHODS
44
45 =over 4
46
47 =item B<get($key, $key2, $key3...)>
48
49 Returns values from the hash.
50
51 In list context it returns a list of values in the hash for the given keys. In
52 scalar context it returns the value for the last key specified.
53
54 This method requires at least one argument.
55
56 =item B<set($key =E<gt> $value, $key2 =E<gt> $value2...)>
57
58 Sets the elements in the hash to the given values. It returns the new values
59 set for each key, in the same order as the keys passed to the method.
60
61 This method requires at least two arguments, and expects an even number of
62 arguments.
63
64 =item B<delete($key, $key2, $key3...)>
65
66 Removes the elements with the given keys.
67
68 In list context it returns a list of values in the hash for the deleted
69 keys. In scalar context it returns the value for the last key specified.
70
71 =item B<keys>
72
73 Returns the list of keys in the hash.
74
75 This method does not accept any arguments.
76
77 =item B<exists($key)>
78
79 Returns true if the given key is present in the hash.
80
81 This method requires a single argument.
82
83 =item B<defined($key)>
84
85 Returns true if the value of a given key is defined.
86
87 This method requires a single argument.
88
89 =item B<values>
90
91 Returns the list of values in the hash.
92
93 This method does not accept any arguments.
94
95 =item B<kv>
96
97 Returns the key/value pairs in the hash as an array of array references.
98
99   for my $pair ( $object->option_pairs ) {
100       print "$pair->[0] = $pair->[1]\n";
101   }
102
103 This method does not accept any arguments.
104
105 =item B<elements>
106
107 Returns the key/value pairs in the hash as a flattened list..
108
109 This method does not accept any arguments.
110
111 =item B<clear>
112
113 Resets the hash to an empty value, like C<%hash = ()>.
114
115 This method does not accept any arguments.
116
117 =item B<count>
118
119 Returns the number of elements in the hash. Also useful for not empty:
120 C<< has_options => 'count' >>.
121
122 This method does not accept any arguments.
123
124 =item B<is_empty>
125
126 If the hash is populated, returns false. Otherwise, returns true.
127
128 This method does not accept any arguments.
129
130 =item B<accessor($key)>
131
132 =item B<accessor($key, $value)>
133
134 If passed one argument, returns the value of the specified key. If passed two
135 arguments, sets the value of the specified key.
136
137 When called as a setter, this method returns the value that was set.
138
139 =item B<shallow_clone>
140
141 This method returns a shallow clone of the hash reference.  The return value
142 is a reference to a new hash with the same keys and values.  It is I<shallow>
143 because any values that were references in the original will be the I<same>
144 references in the clone.
145
146 =back
147
148 Note that C<each> is deliberately omitted, due to its stateful interaction
149 with the hash iterator. C<keys> or C<kv> are much safer.
150
151 =head1 METHODS
152
153 =over 4
154
155 =item B<meta>
156
157 =back
158
159 =head1 BUGS
160
161 See L<Moose/BUGS> for details on reporting bugs.
162
163 =cut