/usr/share/perl5/MouseX/NativeTraits/ArrayRef.pm is in libmousex-nativetraits-perl 1.07-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | package MouseX::NativeTraits::ArrayRef;
use Mouse::Role;
with 'MouseX::NativeTraits';
sub method_provider_class {
return 'MouseX::NativeTraits::MethodProvider::ArrayRef';
}
sub helper_type {
return 'ArrayRef';
}
no Mouse::Role;
1;
__END__
=head1 NAME
MouseX::NativeTraits::ArrayRef - Helper trait for ArrayRef attributes
=head1 SYNOPSIS
package Stuff;
use Mouse;
has 'options' => (
traits => ['Array'],
is => 'ro',
isa => 'ArrayRef[Str]',
default => sub { [] },
handles => {
all_options => 'elements',
add_option => 'push',
map_options => 'map',
filter_options => 'grep',
find_option => 'first',
get_option => 'get',
join_options => 'join',
count_options => 'count',
has_options => 'count',
has_no_options => 'is_empty',
sorted_options => 'sort',
},
);
=head1 DESCRIPTION
This module provides an Array attribute which provides a number of
array operations.
=head1 PROVIDED METHODS
These methods are implemented in
L<MouseX::NativeTraits::MethodProvider::ArrayRef>.
=over 4
=item B<count>
Returns the number of elements in the array.
$stuff = Stuff->new;
$stuff->options(["foo", "bar", "baz", "boo"]);
my $count = $stuff->count_options;
print "$count\n"; # prints 4
=item B<is_empty>
Returns a boolean value that is true when the array has no elements.
$stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
=item B<elements>
Returns all of the elements of the array.
my @option = $stuff->all_options;
print "@options\n"; # prints "foo bar baz boo"
=item B<get($index)>
Returns an element of the array by its index. You can also use negative index
numbers, just as with Perl's core array handling.
my $option = $stuff->get_option(1);
print "$option\n"; # prints "bar"
=item B<pop>
=item B<push($value1, $value2, value3 ...)>
=item B<shift>
=item B<unshift($value1, $value2, value3 ...)>
=item B<splice($offset, $length, @values)>
These methods are all equivalent to the Perl core functions of the same name.
=item B<first( sub { ... } )>
This method returns the first item matching item in the array, just like
L<List::Util>'s C<first> function. The matching is done with a subroutine
reference you pass to this method. The reference will be called against each
element in the array until one matches or all elements have been checked.
my $found = $stuff->find_option( sub { /^b/ } );
print "$found\n"; # prints "bar"
=item B<any( sub { ... } )>
This method returns true if any item in the array meets the criterion given
through the subroutine, otherwise returns false. It sets $_ for each item
in the array.
=item B<grep( sub { ... } )>
This method returns every element matching a given criteria, just like Perl's
core C<grep> function. This method requires a subroutine which implements the
matching logic.
my @found = $stuff->filter_options( sub { /^b/ } );
print "@found\n"; # prints "bar baz boo"
=item B<map( sub { ... } )>
This method transforms every element in the array and returns a new array,
just like Perl's core C<map> function. This method requires a subroutine which
implements the transformation.
my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
=item B<apply( sub { ... } )>
This method also transform every element in the array and returns a new array,
just like L<List::MoreUtils>'s C<apply> function.his is similar to C<map>,
but does not modify the element of the array.
=item B<reduce( sub { ... } )>
This method condenses an array into a single value, by passing a function the
value so far and the next value in the array, just like L<List::Util>'s
C<reduce> function. The reducing is done with a subroutine reference you pass
to this method.
my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
print "$found\n"; # prints "foobarbazboo"
=item B<sort( \&compare )>
Returns the array in sorted order.
You can provide an optional subroutine reference to sort with (as you can with
Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
will need to use C<$_[0]> and C<$_[1]> instead.
# ascending ASCIIbetical
my @sorted = $stuff->sort_options();
# Descending alphabetical order
my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
print "@sorted_options\n"; # prints "foo boo baz bar"
=item B<sort_in_place( \&compare )>
Sorts the array I<in place>, modifying the value of the attribute.
You can provide an optional subroutine reference to sort with (as you can with
Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
will need to use C<$_[0]> and C<$_[1]> instead.
=item B<sort_by( \&by, \&compare )>
Returns the array in sorted order, applying I<\&by> function to each item.
This is equivalent to C<< sort(sub{ by($_[0]) cmp by($_[1]) }) >>, but
implemented effectively.
Currently (as of Moose 0.98) this is a Mouse specific method.
=item B<sort_in_place_by( \&by, \&compare )>
Sorts the array, applying I<\&by> function to each item, modifying the value
of the attribute.
This is equivalent to C<< sort_in_place(sub{ by($_[0]) cmp by($_[1]) }) >>, but
implemented effectively.
Currently (as of Moose 0.98) this is a Mouse specific method.
=item B<shuffle>
Returns the array, with indices in random order, like C<shuffle> from
L<List::Util>.
=item B<uniq>
Returns the array, with all duplicate elements removed, like C<uniq> from
L<List::MoreUtils>.
=item B<join($str)>
Joins every element of the array using the separator given as argument, just
like Perl's core C<join> function.
my $joined = $stuff->join_options( ':' );
print "$joined\n"; # prints "foo:bar:baz:boo"
=item B<set($index, $value)>
Given an index and a value, sets the specified array element's value.
=item B<delete($index)>
Removes the element at the given index from the array.
=item B<insert($index, $value)>
Inserts a new element into the array at the given index.
=item B<clear>
Empties the entire array, like C<@array = ()>.
=item B<accessor>
This method provides a get/set accessor for the array, based on array indexes.
If passed one argument, it returns the value at the specified index. If
passed two arguments, it sets the value of the specified index.
=item B<for_each(sub{ ... })>
This method calls the given subroutine with each element of the array,
like Perl's core C<foreach> statement.
Currently (as of Moose 0.98) this is a Mouse specific method.
=item B<for_each_pair( sub{ ... } )>
This method calls the given subroutine with each two element of the array,
as if the array is a list of pairs.
Currently (as of Moose 0.98) this is a Mouse specific method.
=back
=head1 METHODS
=over 4
=item B<meta>
=item B<method_provider_class>
=item B<helper_type>
=back
=head1 SEE ALSO
L<MouseX::NativeTraits>
=cut
|