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
42 isa => 'ArrayRef[Str]',
43 default => sub { [] },
45 all_options => 'elements',
47 filter_options => 'grep',
48 find_option => 'first',
49 first_option => 'head',
50 all_but_first_option => 'tail',
51 last_option => 'last',
53 join_options => 'join',
54 count_options => 'count',
55 has_no_options => 'is_empty',
56 sorted_options => 'sort',
65 This module provides an Array attribute which provides a number of
68 =head1 PROVIDED METHODS
70 These methods are implemented in
71 L<Moose::Meta::Attribute::Native::MethodProvider::Array>.
77 Returns the number of elements in the array.
80 $stuff->options(["foo", "bar", "baz", "boo"]);
82 my $count = $stuff->count_options;
83 print "$count\n"; # prints 4
87 Returns a boolean value indicating whether or not the array has any elements.
89 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
93 Returns all of the elements of the array.
95 my @option = $stuff->all_options;
96 print "@options\n"; # prints "foo bar baz boo"
100 Returns an element of the array by its index. You can also use negative index
101 numbers, just as with Perl's core array handling.
103 my $option = $stuff->get_option(1);
104 print "$option\n"; # prints "bar"
108 =item B<push($value)>
112 =item B<unshift($value)>
114 =item B<splice($offset, $length, @values)>
116 These methods are all equivalent to the Perl core functions of the same name.
118 =item B<first( sub { ... } )>
120 This method returns the first item matching item in the array. The matching is
121 done with a subroutine reference you pass to this method. The reference will
122 be called against each element in the array until one matches or all elements
125 my $found = $stuff->find_option( sub { /^b/ } );
126 print "$found\n"; # prints "bar"
128 =item B<grep( sub { ... } )>
130 This method returns every element matching a given criteria, just like Perl's
131 core C<grep> function. This method requires a subroutine which implements the
134 my @found = $stuff->filter_options( sub { /^b/ } );
135 print "@found\n"; # prints "bar baz boo"
137 =item B<map( sub { ... } )>
139 This method transforms every element in the array and returns a new array,
140 just like Perl's core C<map> function. This method requires a subroutine which
141 implements the transformation.
143 my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
144 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
146 =item B<sort( sub { ... } )>
148 Returns a the array in sorted order.
150 You can provide an optional subroutine reference to sort with (as you can with
151 Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
152 will need to use C<$_[0]> and C<$_[1]> instead.
154 # ascending ASCIIbetical
155 my @sorted = $stuff->sort_options();
157 # Descending alphabetical order
158 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
159 print "@sorted_options\n"; # prints "foo boo baz bar"
161 =item B<sort_in_place>
163 Sorts the array I<in place>, modifying the value of the attribute.
165 You can provide an optional subroutine reference to sort with (as you can with
166 Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
167 will need to use C<$_[0]> and C<$_[1]> instead.
171 Joins every element of the array using the separator given as argument, just
172 like Perl's core C<join> function.
174 my $joined = $stuff->join_options( ':' );
175 print "$joined\n"; # prints "foo:bar:baz:boo"
177 =item B<set($index, $value)>
179 Given an index and a value, sets the specified array element's value.
181 =item B<delete($index)>
183 Removes the element at the given index from the array.
185 =item B<insert($index, $value)>
187 Inserts a new element into the array at the given index.
191 Empties the entire array, like C<@array = ()>.
195 Returns the first element of the array.
197 my $first = $stuff->first_option;
198 print "$first\n"; # prints "foo"
202 Returns all elements of the array after the first.
204 my @tail = $stuff->all_but_first_option;
205 print join(', ', @tail), "\n"; # prints "bar, baz, boo"
209 Returns the last element of the array.
211 my $last = $stuff->last_option;
212 print "$last\n"; # prints "boo"
216 This method provides a get/set accessor for the array, based on array indexes.
217 If passed one argument, it returns the value at the specified index. If
218 passed two arguments, it sets the value of the specified index.
228 =item B<method_provider>
230 =item B<has_method_provider>
236 All complex software has bugs lurking in it, and this module is no
237 exception. If you find a bug please either email me, or add the bug
242 Stevan Little E<lt>stevan@iinteractive.comE<gt>
244 =head1 COPYRIGHT AND LICENSE
246 Copyright 2007-2009 by Infinity Interactive, Inc.
248 L<http://www.iinteractive.com>
250 This library is free software; you can redistribute it and/or modify
251 it under the same terms as Perl itself.