1Moose::Meta::Attribute:U:sNeartiCvoen:t:rMTiorboausitete::d::MAPeretrraal:y:(DA3ot)cturmiebnuttaet:i:oNnative::Trait::Array(3)
2
3
4
6 Moose::Meta::Attribute::Native::Trait::Array - Helper trait for
7 ArrayRef attributes
8
10 package Stuff;
11 use Moose;
12
13 has 'options' => (
14 traits => ['Array'],
15 is => 'ro',
16 isa => 'ArrayRef[Str]',
17 default => sub { [] },
18 handles => {
19 all_options => 'elements',
20 add_option => 'push',
21 map_options => 'map',
22 filter_options => 'grep',
23 find_option => 'first',
24 get_option => 'get',
25 join_options => 'join',
26 count_options => 'count',
27 has_options => 'count',
28 has_no_options => 'is_empty',
29 sorted_options => 'sort',
30 },
31 );
32
33 no Moose;
34 1;
35
37 This module provides an Array attribute which provides a number of
38 array operations.
39
41 These methods are implemented in
42 Moose::Meta::Attribute::Native::MethodProvider::Array.
43
44 count
45 Returns the number of elements in the array.
46
47 $stuff = Stuff->new;
48 $stuff->options(["foo", "bar", "baz", "boo"]);
49
50 my $count = $stuff->count_options;
51 print "$count\n"; # prints 4
52
53 is_empty
54 Returns a boolean value that is true when the array has no
55 elements.
56
57 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
58
59 elements
60 Returns all of the elements of the array.
61
62 my @option = $stuff->all_options;
63 print "@options\n"; # prints "foo bar baz boo"
64
65 get($index)
66 Returns an element of the array by its index. You can also use
67 negative index numbers, just as with Perl's core array handling.
68
69 my $option = $stuff->get_option(1);
70 print "$option\n"; # prints "bar"
71
72 pop
73 push($value1, $value2, value3 ...)
74 shift
75 unshift($value1, $value2, value3 ...)
76 splice($offset, $length, @values)
77 These methods are all equivalent to the Perl core functions of the
78 same name.
79
80 first( sub { ... } )
81 This method returns the first item matching item in the array, just
82 like List::Util's "first" function. The matching is done with a
83 subroutine reference you pass to this method. The reference will be
84 called against each element in the array until one matches or all
85 elements have been checked.
86
87 my $found = $stuff->find_option( sub { /^b/ } );
88 print "$found\n"; # prints "bar"
89
90 grep( sub { ... } )
91 This method returns every element matching a given criteria, just
92 like Perl's core "grep" function. This method requires a subroutine
93 which implements the matching logic.
94
95 my @found = $stuff->filter_options( sub { /^b/ } );
96 print "@found\n"; # prints "bar baz boo"
97
98 map( sub { ... } )
99 This method transforms every element in the array and returns a new
100 array, just like Perl's core "map" function. This method requires a
101 subroutine which implements the transformation.
102
103 my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
104 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
105
106 reduce( sub { ... } )
107 This method condenses an array into a single value, by passing a
108 function the value so far and the next value in the array, just
109 like List::Util's "reduce" function. The reducing is done with a
110 subroutine reference you pass to this method.
111
112 my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
113 print "$found\n"; # prints "foobarbazboo"
114
115 sort( sub { ... } )
116 Returns a the array in sorted order.
117
118 You can provide an optional subroutine reference to sort with (as
119 you can with Perl's core "sort" function). However, instead of
120 using $a and $b, you will need to use $_[0] and $_[1] instead.
121
122 # ascending ASCIIbetical
123 my @sorted = $stuff->sort_options();
124
125 # Descending alphabetical order
126 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
127 print "@sorted_options\n"; # prints "foo boo baz bar"
128
129 sort_in_place
130 Sorts the array in place, modifying the value of the attribute.
131
132 You can provide an optional subroutine reference to sort with (as
133 you can with Perl's core "sort" function). However, instead of
134 using $a and $b, you will need to use $_[0] and $_[1] instead.
135
136 shuffle
137 Returns the array, with indices in random order, like "shuffle"
138 from List::Util.
139
140 uniq
141 Returns the array, with all duplicate elements removed, like "uniq"
142 from List::MoreUtils.
143
144 join($str)
145 Joins every element of the array using the separator given as
146 argument, just like Perl's core "join" function.
147
148 my $joined = $stuff->join_options( ':' );
149 print "$joined\n"; # prints "foo:bar:baz:boo"
150
151 set($index, $value)
152 Given an index and a value, sets the specified array element's
153 value.
154
155 delete($index)
156 Removes the element at the given index from the array.
157
158 insert($index, $value)
159 Inserts a new element into the array at the given index.
160
161 clear
162 Empties the entire array, like "@array = ()".
163
164 accessor
165 This method provides a get/set accessor for the array, based on
166 array indexes. If passed one argument, it returns the value at the
167 specified index. If passed two arguments, it sets the value of the
168 specified index.
169
170 natatime($n, $code)
171 This method returns an iterator which, on each call, returns $n
172 more items from the array, in order, like "natatime" from
173 List::MoreUtils. A coderef can optionally be provided; it will be
174 called on each group of $n elements in the array.
175
177 meta
178 method_provider
179 has_method_provider
180
182 See "BUGS" in Moose for details on reporting bugs.
183
185 Stevan Little <stevan@iinteractive.com>
186
188 Copyright 2007-2009 by Infinity Interactive, Inc.
189
190 <http://www.iinteractive.com>
191
192 This library is free software; you can redistribute it and/or modify it
193 under the same terms as Perl itself.
194
195
196
197perl v5.12.2 Moo2s0e1:0:-M0e8t-a2:8:Attribute::Native::Trait::Array(3)