2 package Moose::Meta::Attribute::Native::Trait::Array;
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use Moose::Meta::Method::Accessor::Native::Array::accessor;
10 use Moose::Meta::Method::Accessor::Native::Array::clear;
11 use Moose::Meta::Method::Accessor::Native::Array::count;
12 use Moose::Meta::Method::Accessor::Native::Array::delete;
13 use Moose::Meta::Method::Accessor::Native::Array::elements;
14 use Moose::Meta::Method::Accessor::Native::Array::first;
15 use Moose::Meta::Method::Accessor::Native::Array::get;
16 use Moose::Meta::Method::Accessor::Native::Array::grep;
17 use Moose::Meta::Method::Accessor::Native::Array::insert;
18 use Moose::Meta::Method::Accessor::Native::Array::is_empty;
19 use Moose::Meta::Method::Accessor::Native::Array::join;
20 use Moose::Meta::Method::Accessor::Native::Array::map;
21 use Moose::Meta::Method::Accessor::Native::Array::natatime;
22 use Moose::Meta::Method::Accessor::Native::Array::pop;
23 use Moose::Meta::Method::Accessor::Native::Array::push;
24 use Moose::Meta::Method::Accessor::Native::Array::reduce;
25 use Moose::Meta::Method::Accessor::Native::Array::set;
26 use Moose::Meta::Method::Accessor::Native::Array::shift;
27 use Moose::Meta::Method::Accessor::Native::Array::shuffle;
28 use Moose::Meta::Method::Accessor::Native::Array::splice;
29 use Moose::Meta::Method::Accessor::Native::Array::sort;
30 use Moose::Meta::Method::Accessor::Native::Array::sort_in_place;
31 use Moose::Meta::Method::Accessor::Native::Array::uniq;
32 use Moose::Meta::Method::Accessor::Native::Array::unshift;
34 with 'Moose::Meta::Attribute::Native::Trait';
36 sub _helper_type { 'ArrayRef' }
48 Moose::Meta::Attribute::Native::Trait::Array - Helper trait for array delegation
58 isa => 'ArrayRef[Str]',
59 default => sub { [] },
61 all_options => 'elements',
64 filter_options => 'grep',
65 find_option => 'first',
67 join_options => 'join',
68 count_options => 'count',
69 has_options => 'count',
70 has_no_options => 'is_empty',
71 sorted_options => 'sort',
80 This module provides an Array attribute which provides a number of
83 =head1 PROVIDED METHODS
89 Returns the number of elements in the array.
92 $stuff->options( [ "foo", "bar", "baz", "boo" ] );
94 print $stuff->count_options; # prints 4
96 This method does not accept any arguments.
100 Returns a boolean value that is true when the array has no elements.
102 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
104 This method does not accept any arguments.
108 Returns all of the elements of the array as an array (not an array reference).
110 my @option = $stuff->all_options;
111 print "@options\n"; # prints "foo bar baz boo"
113 This method does not accept any arguments.
115 =item * B<get($index)>
117 Returns an element of the array by its index. You can also use negative index
118 numbers, just as with Perl's core array handling.
120 my $option = $stuff->get_option(1);
121 print "$option\n"; # prints "bar"
123 If the specified element does not exist, this will return C<undef>.
125 This method does accepts just one argument.
129 Just like Perl's builtin C<pop>.
131 This method does not accept any arguments.
133 =item * B<push($value1, $value2, value3 ...)>
135 Just like Perl's builtin C<push>. Returns the number of elements in the new
138 This method accepts any number of arguments.
142 Just like Perl's builtin C<shift>.
144 This method does not accept any arguments.
146 =item * B<unshift($value1, $value2, value3 ...)>
148 Just like Perl's builtin C<unshift>. Returns the number of elements in the new
151 This method accepts any number of arguments.
153 =item * B<splice($offset, $length, @values)>
155 Just like Perl's builtin C<splice>. In scalar context, this returns the last
156 element removed, or C<undef> if no elements were removed. In list context,
157 this returns all the elements removed from the array.
159 This method requires at least one argument.
161 =item * B<first( sub { ... } )>
163 This method returns the first item matching item in the array, just like
164 L<List::Util>'s C<first> function. The matching is done with a subroutine
165 reference you pass to this method. The subroutine will be called against each
166 element in the array until one matches or all elements have been checked.
168 my $found = $stuff->find_option( sub {/^b/} );
169 print "$found\n"; # prints "bar"
171 This method requires a single argument.
173 =item * B<grep( sub { ... } )>
175 This method returns every element matching a given criteria, just like Perl's
176 core C<grep> function. This method requires a subroutine which implements the
179 my @found = $stuff->filter_options( sub {/^b/} );
180 print "@found\n"; # prints "bar baz boo"
182 This method requires a single argument.
184 =item * B<map( sub { ... } )>
186 This method transforms every element in the array and returns a new array,
187 just like Perl's core C<map> function. This method requires a subroutine which
188 implements the transformation.
190 my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
191 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
193 This method requires a single argument.
195 =item * B<reduce( sub { ... } )>
197 This method turns an array into a single value, by passing a function the
198 value so far and the next value in the array, just like L<List::Util>'s
199 C<reduce> function. The reducing is done with a subroutine reference you pass
202 my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
203 print "$found\n"; # prints "foobarbazboo"
205 This method requires a single argument.
209 =item * B<sort( sub { ... } )>
211 Returns the elements of the array in sorted order.
213 You can provide an optional subroutine reference to sort with (as you can with
214 Perl's core C<sort> function). However, instead of using C<$a> and C<$b> in
215 this subroutine, you will need to use C<$_[0]> and C<$_[1]>.
217 # ascending ASCIIbetical
218 my @sorted = $stuff->sort_options();
220 # Descending alphabetical order
221 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
222 print "@sorted_options\n"; # prints "foo boo baz bar"
224 This method accepts a single argument.
226 =item * B<sort_in_place>
228 =item * B<sort_in_place( sub { ... } )>
230 Sorts the array I<in place>, modifying the value of the attribute.
232 You can provide an optional subroutine reference to sort with (as you can with
233 Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
234 will need to use C<$_[0]> and C<$_[1]> instead.
236 This method does not define a return value.
238 This method accepts a single argument.
242 Returns the elements of the array in random order, like C<shuffle> from
245 This method does not accept any arguments.
249 Returns the array with all duplicate elements removed, like C<uniq> from
252 This method does not accept any arguments.
254 =item * B<join($str)>
256 Joins every element of the array using the separator given as argument, just
257 like Perl's core C<join> function.
259 my $joined = $stuff->join_options(':');
260 print "$joined\n"; # prints "foo:bar:baz:boo"
262 This method requires a single argument.
264 =item * B<set($index, $value)>
266 Given an index and a value, sets the specified array element's value.
268 This method returns the value at C<$index> after the set.
270 This method requires two arguments.
272 =item * B<delete($index)>
274 Removes the element at the given index from the array.
276 This method returns the deleted value. Note that if no value exists, it will
279 This method requires one argument.
281 =item * B<insert($index, $value)>
283 Inserts a new element into the array at the given index.
285 This method returns the new value at C<$index>.
287 This method requires two arguments.
291 Empties the entire array, like C<@array = ()>.
293 This method does not define a return value.
295 This method does not accept any arguments.
297 =item * B<accessor($index)>
299 =item * B<accessor($index, $value)>
301 This method provides a get/set accessor for the array, based on array indexes.
302 If passed one argument, it returns the value at the specified index. If
303 passed two arguments, it sets the value of the specified index.
305 When called as a setter, this method returns the new value at C<$index>.
307 This method accepts one or two arguments.
309 =item * B<natatime($n)>
311 =item * B<natatime($n, $code)>
313 This method returns an iterator which, on each call, returns C<$n> more items
314 from the array, in order, like C<natatime> from L<List::MoreUtils>. A coderef
315 can optionally be provided; it will be called on each group of C<$n> elements
318 This method accepts one or two arguments.
324 See L<Moose/BUGS> for details on reporting bugs.
328 Stevan Little E<lt>stevan@iinteractive.comE<gt>
330 =head1 COPYRIGHT AND LICENSE
332 Copyright 2007-2009 by Infinity Interactive, Inc.
334 L<http://www.iinteractive.com>
336 This library is free software; you can redistribute it and/or modify
337 it under the same terms as Perl itself.