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