2 package Moose::Meta::Attribute::Native::Trait::Hash;
5 with 'Moose::Meta::Attribute::Native::Trait';
7 sub _helper_type { 'HashRef' }
13 # ABSTRACT: Helper trait for HashRef attributes
27 isa => 'HashRef[Str]',
28 default => sub { {} },
32 has_no_options => 'is_empty',
33 num_options => 'count',
34 delete_option => 'delete',
41 This trait provides native delegation methods for hash references.
43 =head1 PROVIDED METHODS
47 =item B<get($key, $key2, $key3...)>
49 Returns values from the hash.
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.
54 This method requires at least one argument.
56 =item B<set($key =E<gt> $value, $key2 =E<gt> $value2...)>
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.
61 This method requires at least two arguments, and expects an even number of
64 =item B<delete($key, $key2, $key3...)>
66 Removes the elements with the given keys.
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.
73 Returns the list of keys in the hash.
75 This method does not accept any arguments.
79 Returns true if the given key is present in the hash.
81 This method requires a single argument.
83 =item B<defined($key)>
85 Returns true if the value of a given key is defined.
87 This method requires a single argument.
91 Returns the list of values in the hash.
93 This method does not accept any arguments.
97 Returns the key/value pairs in the hash as an array of array references.
99 for my $pair ( $object->options->kv ) {
100 print "$pair->[0] = $pair->[1]\n";
103 This method does not accept any arguments.
107 Returns the key/value pairs in the hash as a flattened list..
109 This method does not accept any arguments.
113 Resets the hash to an empty value, like C<%hash = ()>.
115 This method does not accept any arguments.
119 Returns the number of elements in the hash. Also useful for not empty:
120 C<< has_options => 'count' >>.
122 This method does not accept any arguments.
126 If the hash is populated, returns false. Otherwise, returns true.
128 This method does not accept any arguments.
130 =item B<accessor($key)>
132 =item B<accessor($key, $value)>
134 If passed one argument, returns the value of the specified key. If passed two
135 arguments, sets the value of the specified key.
137 When called as a setter, this method returns the value that was set.
141 Note that C<each> is deliberately omitted, due to its stateful interaction
142 with the hash iterator. C<keys> or C<kv> are much safer.
154 See L<Moose/BUGS> for details on reporting bugs.