1Moose::Meta::Attribute:U:sNeartiCvoen:t:rMTiorboausitete::d::MAPeretrraal:y:(DA3ot)cturmiebnuttaet:i:oNnative::Trait::Array(3)
2
3
4
6 Moose::Meta::Attribute::Native::Trait::Array - Helper trait for
7 ArrayRef attributes
8
10 version 2.2013
11
13 package Stuff;
14 use Moose;
15
16 has 'options' => (
17 traits => ['Array'],
18 is => 'ro',
19 isa => 'ArrayRef[Str]',
20 default => sub { [] },
21 handles => {
22 all_options => 'elements',
23 add_option => 'push',
24 map_options => 'map',
25 filter_options => 'grep',
26 find_option => 'first',
27 get_option => 'get',
28 join_options => 'join',
29 count_options => 'count',
30 has_options => 'count',
31 has_no_options => 'is_empty',
32 sorted_options => 'sort',
33 },
34 );
35
36 no Moose;
37 1;
38
40 This trait provides native delegation methods for array references.
41
43 If you don't provide an "isa" value for your attribute, it will default
44 to "ArrayRef".
45
47 · count
48
49 Returns the number of elements in the array.
50
51 $stuff = Stuff->new;
52 $stuff->options( [ "foo", "bar", "baz", "boo" ] );
53
54 print $stuff->count_options; # prints 4
55
56 This method does not accept any arguments.
57
58 · is_empty
59
60 Returns a boolean value that is true when the array has no
61 elements.
62
63 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
64
65 This method does not accept any arguments.
66
67 · elements
68
69 In list context, returns all of the elements of the array as a
70 list.
71
72 In scalar context, returns the number of elements in the array.
73
74 my @options = $stuff->all_options;
75 print "@options"; # prints "foo bar baz boo"
76 print scalar $stuff->all_options; # prints 4
77
78 This method does not accept any arguments.
79
80 · get($index)
81
82 Returns an element of the array by its index. You can also use
83 negative index numbers, just as with Perl's core array handling.
84
85 my $option = $stuff->get_option(1);
86 print "$option\n"; # prints "bar"
87
88 If the specified element does not exist, this will return "undef".
89
90 This method accepts just one argument.
91
92 · pop
93
94 Just like Perl's builtin "pop".
95
96 This method does not accept any arguments.
97
98 · push($value1, $value2, value3 ...)
99
100 Just like Perl's builtin "push". Returns the number of elements in
101 the new array.
102
103 This method accepts any number of arguments.
104
105 · shift
106
107 Just like Perl's builtin "shift".
108
109 This method does not accept any arguments.
110
111 · unshift($value1, $value2, value3 ...)
112
113 Just like Perl's builtin "unshift". Returns the number of elements
114 in the new array.
115
116 This method accepts any number of arguments.
117
118 · splice($offset, $length, @values)
119
120 Just like Perl's builtin "splice". In scalar context, this returns
121 the last element removed, or "undef" if no elements were removed.
122 In list context, this returns all the elements removed from the
123 array.
124
125 This method requires at least one argument.
126
127 · first( sub { ... } )
128
129 This method returns the first matching item in the array, just like
130 List::Util's "first" function. The matching is done with a
131 subroutine reference you pass to this method. The subroutine will
132 be called against each element in the array until one matches or
133 all elements have been checked. Each list element will be
134 available to the sub in $_.
135
136 my $found = $stuff->find_option( sub {/^b/} );
137 print "$found\n"; # prints "bar"
138
139 This method requires a single argument.
140
141 · first_index( sub { ... } )
142
143 This method returns the index of the first matching item in the
144 array, just like "first_index" in List::SomeUtils. The matching is
145 done with a subroutine reference you pass to this method. The
146 subroutine will be called against each element in the array until
147 one matches or all elements have been checked. Each list element
148 will be available to the sub in $_. If no match is made, -1 is
149 returned.
150
151 This method requires a single argument.
152
153 · grep( sub { ... } )
154
155 This method returns every element matching a given criteria, just
156 like Perl's core "grep" function. This method requires a subroutine
157 which implements the matching logic; each list element will be
158 available to the sub in $_.
159
160 my @found = $stuff->filter_options( sub {/^b/} );
161 print "@found\n"; # prints "bar baz boo"
162
163 This method requires a single argument.
164
165 · map( sub { ... } )
166
167 This method transforms every element in the array and returns a new
168 array, just like Perl's core "map" function. This method requires a
169 subroutine which implements the transformation; each list element
170 will be available to the sub in $_.
171
172 my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
173 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
174
175 This method requires a single argument.
176
177 · reduce( sub { ... } )
178
179 This method turns an array into a single value, by passing a
180 function the value so far and the next value in the array, just
181 like List::Util's "reduce" function. The reducing is done with a
182 subroutine reference you pass to this method; each list element
183 will be available to the sub in $_.
184
185 my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
186 print "$found\n"; # prints "foobarbazboo"
187
188 This method requires a single argument.
189
190 · sort
191
192 · sort( sub { ... } )
193
194 Returns the elements of the array (not an array reference) in
195 sorted order, or, like "elements", returns the number of elements
196 in the array in scalar context.
197
198 You can provide an optional subroutine reference to sort with (as
199 you can with Perl's core "sort" function). However, instead of
200 using $a and $b in this subroutine, you will need to use $_[0] and
201 $_[1].
202
203 # ascending ASCIIbetical
204 my @sorted = $stuff->sort_options();
205
206 # Descending alphabetical order
207 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
208 print "@sorted_options\n"; # prints "foo boo baz bar"
209
210 This method accepts a single argument.
211
212 · sort_in_place
213
214 · sort_in_place( sub { ... } )
215
216 Sorts the array in place, modifying the value of the attribute.
217
218 You can provide an optional subroutine reference to sort with (as
219 you can with Perl's core "sort" function). However, instead of
220 using $a and $b, you will need to use $_[0] and $_[1] instead.
221
222 This method does not define a return value.
223
224 This method accepts a single argument.
225
226 · shuffle
227
228 Returns the elements of the array in random order, like "shuffle"
229 from List::Util.
230
231 This method does not accept any arguments.
232
233 · uniq
234
235 Returns the array with all duplicate elements removed, like "uniq"
236 in List::Util.
237
238 This method does not accept any arguments.
239
240 · join($str)
241
242 Joins every element of the array using the separator given as
243 argument, just like Perl's core "join" function.
244
245 my $joined = $stuff->join_options(':');
246 print "$joined\n"; # prints "foo:bar:baz:boo"
247
248 This method requires a single argument.
249
250 · set($index, $value)
251
252 Given an index and a value, sets the specified array element's
253 value.
254
255 This method returns the value at $index after the set.
256
257 This method requires two arguments.
258
259 · delete($index)
260
261 Removes the element at the given index from the array.
262
263 This method returns the deleted value. Note that if no value
264 exists, it will return "undef".
265
266 This method requires one argument.
267
268 · insert($index, $value)
269
270 Inserts a new element into the array at the given index.
271
272 This method returns the new value at $index.
273
274 This method requires two arguments.
275
276 · clear
277
278 Empties the entire array, like "@array = ()".
279
280 This method does not define a return value.
281
282 This method does not accept any arguments.
283
284 · accessor($index)
285
286 · accessor($index, $value)
287
288 This method provides a get/set accessor for the array, based on
289 array indexes. If passed one argument, it returns the value at the
290 specified index. If passed two arguments, it sets the value of the
291 specified index.
292
293 When called as a setter, this method returns the new value at
294 $index.
295
296 This method accepts one or two arguments.
297
298 · natatime($n)
299
300 · natatime($n, $code)
301
302 This method returns an iterator which, on each call, returns $n
303 more items from the array, in order, like "natatime" in
304 List::SomeUtils.
305
306 If you pass a coderef as the second argument, then this code ref
307 will be called on each group of $n elements in the array until the
308 array is exhausted.
309
310 This method accepts one or two arguments.
311
312 · shallow_clone
313
314 This method returns a shallow clone of the array reference. The
315 return value is a reference to a new array with the same elements.
316 It is shallow because any elements that were references in the
317 original will be the same references in the clone.
318
320 See "BUGS" in Moose for details on reporting bugs.
321
323 · Stevan Little <stevan.little@iinteractive.com>
324
325 · Dave Rolsky <autarch@urth.org>
326
327 · Jesse Luehrs <doy@tozt.net>
328
329 · Shawn M Moore <code@sartak.org>
330
331 · יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
332
333 · Karen Etheridge <ether@cpan.org>
334
335 · Florian Ragwitz <rafl@debian.org>
336
337 · Hans Dieter Pearcey <hdp@weftsoar.net>
338
339 · Chris Prather <chris@prather.org>
340
341 · Matt S Trout <mst@shadowcat.co.uk>
342
344 This software is copyright (c) 2006 by Infinity Interactive, Inc.
345
346 This is free software; you can redistribute it and/or modify it under
347 the same terms as the Perl 5 programming language system itself.
348
349
350
351perl v5.32.0 Moo2s0e2:0:-M0e7t-a2:8:Attribute::Native::Trait::Array(3)