1Sub::HandlesVia::HandleUrsLeirbrCaornyt:r:iHbausthe(d3S)Puebr:l:HDaoncdulmeesnVtiaat:i:oHnandlerLibrary::Hash(3)
2
3
4
6 Sub::HandlesVia::HandlerLibrary::Hash - library of hash-related methods
7
9 package My::Class {
10 use Moo;
11 use Sub::HandlesVia;
12 use Types::Standard 'HashRef';
13 has attr => (
14 is => 'rwp',
15 isa => HashRef,
16 handles_via => 'Hash',
17 handles => {
18 'my_accessor' => 'accessor',
19 'my_all' => 'all',
20 'my_clear' => 'clear',
21 'my_count' => 'count',
22 'my_defined' => 'defined',
23 'my_delete' => 'delete',
24 'my_delete_where' => 'delete_where',
25 'my_elements' => 'elements',
26 'my_exists' => 'exists',
27 'my_for_each_key' => 'for_each_key',
28 'my_for_each_pair' => 'for_each_pair',
29 'my_for_each_value' => 'for_each_value',
30 'my_get' => 'get',
31 'my_is_empty' => 'is_empty',
32 'my_keys' => 'keys',
33 'my_kv' => 'kv',
34 'my_reset' => 'reset',
35 'my_set' => 'set',
36 'my_shallow_clone' => 'shallow_clone',
37 'my_sorted_keys' => 'sorted_keys',
38 'my_values' => 'values',
39 },
40 );
41 }
42
44 This is a library of methods for Sub::HandlesVia.
45
47 "accessor( $key, $value? )"
48 Arguments: Str, Optional[Any].
49
50 Acts like "get" if given just a key, or "set" if given a key and a
51 value.
52
53 all()
54 Returns the hash in list context.
55
56 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
57 my %hash = $object->my_all;
58
59 clear()
60 Empties the hash.
61
62 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
63 $object->my_clear;
64 say exists $object->attr->{foo}; ## ==> false
65 say exists $object->attr->{bar}; ## ==> false
66
67 count()
68 Returns the number of keys in the hash.
69
70 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
71 say $object->my_count; ## ==> 2
72
73 defined( $key )
74 Arguments: Str.
75
76 Indicates whether a value exists and is defined in the hashref by its
77 key.
78
79 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
80 say $object->my_defined( 'foo' ); ## ==> 1
81
82 delete( $key )
83 Removes a value from the hashref by its key.
84
85 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
86 $object->my_delete( 'foo' );
87 say exists $object->attr->{foo}; ## ==> false
88
89 delete_where( $match )
90 Arguments: CodeRef|RegexpRef.
91
92 Removes values from the hashref by matching keys against a coderef or
93 regexp.
94
95 my $object = My::Class->new( attr => { foo => 0, bar => 1, baz => 2 } );
96 $object->my_delete_where( sub { $_ eq 'foo' or $_ eq 'bar' } );
97 say Dumper( $object->attr ); ## ==> { baz => 2 }
98
99 my $object2 = My::Class->new( attr => { foo => 0, bar => 1, baz => 2 } );
100 $object2->my_delete_where( qr/^b/ );
101 say Dumper( $object2->attr ); ## ==> { foo => 0 }
102
103 elements()
104 Returns the hash in list context.
105
106 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
107 my %hash = $object->my_elements;
108
109 exists( $key )
110 Arguments: Str.
111
112 Indicates whether a value exists in the hashref by its key.
113
114 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
115 say $object->my_exists( 'foo' ); ## ==> true
116 say $object->my_exists( 'baz' ); ## ==> false
117
118 for_each_key( $coderef )
119 Arguments: CodeRef.
120
121 Chainable method which calls the coderef for each key in the hash,
122 passing just the key to the coderef.
123
124 for_each_pair( $coderef )
125 Arguments: CodeRef.
126
127 Chainable method which calls the coderef for each key in the hash,
128 passing the key and value to the coderef.
129
130 for_each_value( $coderef )
131 Arguments: CodeRef.
132
133 Chainable method which calls the coderef for each value in the hash,
134 passing just the value to the coderef.
135
136 get( $key )
137 Returns a value from the hashref by its key.
138
139 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
140 say $object->my_get( 'bar' ); ## ==> 1
141
142 is_empty()
143 Returns true iff there are no keys in the hash.
144
145 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
146 say $object->my_is_empty; ## ==> false
147 $object->_set_attr( {} );
148 say $object->my_is_empty; ## ==> true
149
150 keys()
151 Returns the list of keys in the hash.
152
153 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
154 # says 'foo' and 'bar' in an unpredictable order
155 say for $object->my_keys;
156
157 kv()
158 Returns a list of arrayrefs, where each arrayref is a key-value pair.
159
160 reset()
161 Resets the attribute to its default value, or an empty hashref if it
162 has no default.
163
164 "set( $key, $value, ... )"
165 Given a key and value, adds the key to the hashref with the given
166 value.
167
168 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
169 $object->my_set( bar => 2, baz => 1 );
170 say $object->attr->{foo}; ## ==> 0
171 say $object->attr->{baz}; ## ==> 1
172 say $object->attr->{bar}; ## ==> 2
173
174 shallow_clone()
175 Creates a new hashref with the same keys and values as the original.
176
177 sorted_keys()
178 Returns an alphabetically sorted list of keys in the hash.
179
180 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
181 # says 'bar' then 'foo'
182 say for $object->my_sorted_keys;
183
184 values()
185 Returns the list of values in the hash.
186
187 my $object = My::Class->new( attr => { foo => 0, bar => 1 } );
188 # says '0' and '1' in an unpredictable order
189 say for $object->my_values;
190
192 Please report any bugs to
193 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
194
196 Sub::HandlesVia.
197
199 Toby Inkster <tobyink@cpan.org>.
200
202 This software is copyright (c) 2020, 2022 by Toby Inkster.
203
204 This is free software; you can redistribute it and/or modify it under
205 the same terms as the Perl 5 programming language system itself.
206
208 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
209 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
210 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
211
212
213
214perl v5.38.0 2023-S0u7b-:2:1HandlesVia::HandlerLibrary::Hash(3)