2 package Moose::Meta::Attribute::Native::Trait::Array;
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use Moose::Meta::Attribute::Native::MethodProvider::Array;
11 with 'Moose::Meta::Attribute::Native::Trait';
13 has 'method_provider' => (
16 predicate => 'has_method_provider',
17 default => 'Moose::Meta::Attribute::Native::MethodProvider::Array'
20 sub _helper_type { 'ArrayRef' }
32 Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes
42 isa => 'ArrayRef[Str]',
43 default => sub { [] },
45 all_options => 'elements',
48 filter_options => 'grep',
49 find_option => 'first',
51 join_options => 'join',
52 count_options => 'count',
53 has_options => 'count',
54 has_no_options => 'is_empty',
55 sorted_options => 'sort',
64 This module provides an Array attribute which provides a number of
67 =head1 PROVIDED METHODS
69 These methods are implemented in
70 L<Moose::Meta::Attribute::Native::MethodProvider::Array>.
76 Returns the number of elements in the array.
79 $stuff->options(["foo", "bar", "baz", "boo"]);
81 my $count = $stuff->count_options;
82 print "$count\n"; # prints 4
86 Returns a boolean value that is true when the array has no elements.
88 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
92 Returns all of the elements of the array.
94 my @option = $stuff->all_options;
95 print "@options\n"; # prints "foo bar baz boo"
99 Returns an element of the array by its index. You can also use negative index
100 numbers, just as with Perl's core array handling.
102 my $option = $stuff->get_option(1);
103 print "$option\n"; # prints "bar"
107 =item B<push($value1, $value2, value3 ...)>
111 =item B<unshift($value1, $value2, value3 ...)>
113 =item B<splice($offset, $length, @values)>
115 These methods are all equivalent to the Perl core functions of the same name.
117 =item B<first( sub { ... } )>
119 This method returns the first item matching item in the array, just like
120 L<List::Util>'s C<first> function. The matching is done with a subroutine
121 reference you pass to this method. The reference will be called against each
122 element in the array until one matches or all elements have been checked.
124 my $found = $stuff->find_option( sub { /^b/ } );
125 print "$found\n"; # prints "bar"
127 =item B<grep( sub { ... } )>
129 This method returns every element matching a given criteria, just like Perl's
130 core C<grep> function. This method requires a subroutine which implements the
133 my @found = $stuff->filter_options( sub { /^b/ } );
134 print "@found\n"; # prints "bar baz boo"
136 =item B<map( sub { ... } )>
138 This method transforms every element in the array and returns a new array,
139 just like Perl's core C<map> function. This method requires a subroutine which
140 implements the transformation.
142 my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
143 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
145 =item B<reduce( sub { ... } )>
147 This method condenses an array into a single value, by passing a function the
148 value so far and the next value in the array, just like L<List::Util>'s
149 C<reduce> function. The reducing is done with a subroutine reference you pass
152 my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
153 print "$found\n"; # prints "foobarbazboo"
155 =item B<sort( sub { ... } )>
157 Returns a the array in sorted order.
159 You can provide an optional subroutine reference to sort with (as you can with
160 Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
161 will need to use C<$_[0]> and C<$_[1]> instead.
163 # ascending ASCIIbetical
164 my @sorted = $stuff->sort_options();
166 # Descending alphabetical order
167 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
168 print "@sorted_options\n"; # prints "foo boo baz bar"
170 =item B<sort_in_place>
172 Sorts the array I<in place>, modifying the value of the attribute.
174 You can provide an optional subroutine reference to sort with (as you can with
175 Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
176 will need to use C<$_[0]> and C<$_[1]> instead.
180 Returns the array, with indices in random order, like C<shuffle> from
185 Returns the array, with all duplicate elements removed, like C<uniq> from
190 Joins every element of the array using the separator given as argument, just
191 like Perl's core C<join> function.
193 my $joined = $stuff->join_options( ':' );
194 print "$joined\n"; # prints "foo:bar:baz:boo"
196 =item B<set($index, $value)>
198 Given an index and a value, sets the specified array element's value.
200 =item B<delete($index)>
202 Removes the element at the given index from the array.
204 =item B<insert($index, $value)>
206 Inserts a new element into the array at the given index.
210 Empties the entire array, like C<@array = ()>.
214 This method provides a get/set accessor for the array, based on array indexes.
215 If passed one argument, it returns the value at the specified index. If
216 passed two arguments, it sets the value of the specified index.
218 =item B<natatime($n, $code)>
220 This method returns an iterator which, on each call, returns C<$n> more items
221 from the array, in order, like C<natatime> from L<List::MoreUtils>. A coderef
222 can optionally be provided; it will be called on each group of C<$n> elements
233 =item B<method_provider>
235 =item B<has_method_provider>
241 See L<Moose/BUGS> for details on reporting bugs.
245 Stevan Little E<lt>stevan@iinteractive.comE<gt>
247 =head1 COPYRIGHT AND LICENSE
249 Copyright 2007-2009 by Infinity Interactive, Inc.
251 L<http://www.iinteractive.com>
253 This library is free software; you can redistribute it and/or modify
254 it under the same terms as Perl itself.