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