line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::AttributeHelpers::MethodProvider::List; |
2
|
22
|
|
|
22
|
|
88
|
use Moose::Role; |
|
22
|
|
|
|
|
28
|
|
|
22
|
|
|
|
|
117
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.25'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub count : method { |
7
|
5
|
|
|
5
|
1
|
29
|
my ($attr, $reader, $writer) = @_; |
8
|
|
|
|
|
|
|
return sub { |
9
|
17
|
|
|
17
|
|
3860
|
scalar @{$reader->($_[0])} |
|
17
|
|
|
21
|
|
60
|
|
10
|
5
|
|
|
|
|
42
|
}; |
11
|
|
|
|
|
|
|
} |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub empty : method { |
14
|
4
|
|
|
4
|
1
|
23
|
my ($attr, $reader, $writer) = @_; |
15
|
|
|
|
|
|
|
return sub { |
16
|
8
|
100
|
|
8
|
|
22687
|
scalar @{$reader->($_[0])} ? 1 : 0 |
|
8
|
|
|
|
|
40
|
|
17
|
4
|
|
|
|
|
43
|
}; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub find : method { |
21
|
2
|
|
|
10
|
1
|
9
|
my ($attr, $reader, $writer) = @_; |
22
|
|
|
|
|
|
|
return sub { |
23
|
2
|
|
|
2
|
|
1025
|
my ($instance, $predicate) = @_; |
24
|
2
|
|
|
|
|
4
|
foreach my $val (@{$reader->($instance)}) { |
|
2
|
|
|
|
|
10
|
|
25
|
4
|
100
|
|
|
|
66
|
return $val if $predicate->($val); |
26
|
|
|
|
|
|
|
} |
27
|
0
|
|
|
|
|
0
|
return; |
28
|
2
|
|
|
|
|
12
|
}; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub map : method { |
32
|
4
|
|
|
4
|
1
|
17
|
my ($attr, $reader, $writer) = @_; |
33
|
|
|
|
|
|
|
return sub { |
34
|
4
|
|
|
4
|
|
937
|
my ($instance, $f) = @_; |
35
|
4
|
|
|
|
|
6
|
CORE::map { $f->($_) } @{$reader->($instance)} |
|
40
|
|
|
|
|
173
|
|
|
4
|
|
|
|
|
14
|
|
36
|
4
|
|
|
|
|
29
|
}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub sort : method { |
40
|
4
|
|
|
8
|
1
|
18
|
my ($attr, $reader, $writer) = @_; |
41
|
|
|
|
|
|
|
return sub { |
42
|
8
|
|
|
8
|
|
3083
|
my ($instance, $predicate) = @_; |
43
|
8
|
100
|
100
|
|
|
57
|
die "Argument must be a code reference" |
44
|
|
|
|
|
|
|
if $predicate && ref $predicate ne 'CODE'; |
45
|
|
|
|
|
|
|
|
46
|
6
|
100
|
|
|
|
10
|
if ($predicate) { |
47
|
4
|
|
|
|
|
7
|
CORE::sort { $predicate->($a, $b) } @{$reader->($instance)}; |
|
60
|
|
|
|
|
210
|
|
|
4
|
|
|
|
|
16
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
else { |
50
|
2
|
|
|
|
|
2
|
CORE::sort @{$reader->($instance)}; |
|
2
|
|
|
|
|
8
|
|
51
|
|
|
|
|
|
|
} |
52
|
4
|
|
|
|
|
31
|
}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub grep : method { |
56
|
6
|
|
|
14
|
1
|
28
|
my ($attr, $reader, $writer) = @_; |
57
|
|
|
|
|
|
|
return sub { |
58
|
6
|
|
|
6
|
|
828
|
my ($instance, $predicate) = @_; |
59
|
6
|
|
|
|
|
8
|
CORE::grep { $predicate->($_) } @{$reader->($instance)} |
|
56
|
|
|
|
|
260
|
|
|
6
|
|
|
|
|
22
|
|
60
|
6
|
|
|
|
|
31
|
}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub elements : method { |
64
|
2
|
|
|
8
|
1
|
11
|
my ($attr, $reader, $writer) = @_; |
65
|
|
|
|
|
|
|
return sub { |
66
|
2
|
|
|
2
|
|
5
|
my ($instance) = @_; |
67
|
2
|
|
|
|
|
3
|
@{$reader->($instance)} |
|
2
|
|
|
|
|
10
|
|
68
|
2
|
|
|
|
|
11
|
}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub join : method { |
72
|
4
|
|
|
6
|
1
|
18
|
my ($attr, $reader, $writer) = @_; |
73
|
|
|
|
|
|
|
return sub { |
74
|
4
|
|
|
4
|
|
948
|
my ($instance, $separator) = @_; |
75
|
4
|
|
|
|
|
6
|
join $separator, @{$reader->($instance)} |
|
4
|
|
|
|
|
15
|
|
76
|
4
|
|
|
|
|
22
|
}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub get : method { |
80
|
2
|
|
|
6
|
1
|
10
|
my ($attr, $reader, $writer) = @_; |
81
|
|
|
|
|
|
|
return sub { |
82
|
2
|
|
|
2
|
|
1012
|
$reader->($_[0])->[$_[1]] |
83
|
2
|
|
|
|
|
11
|
}; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub first : method { |
87
|
2
|
|
|
4
|
1
|
11
|
my ($attr, $reader, $writer) = @_; |
88
|
|
|
|
|
|
|
return sub { |
89
|
2
|
|
|
2
|
|
823
|
$reader->($_[0])->[0] |
90
|
2
|
|
|
|
|
11
|
}; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub last : method { |
94
|
2
|
|
|
4
|
1
|
10
|
my ($attr, $reader, $writer) = @_; |
95
|
|
|
|
|
|
|
return sub { |
96
|
2
|
|
|
2
|
|
878
|
$reader->($_[0])->[-1] |
97
|
2
|
|
|
|
|
12
|
}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
__END__ |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=pod |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=encoding UTF-8 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 NAME |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
MooseX::AttributeHelpers::MethodProvider::List |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 VERSION |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
version 0.25 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SYNOPSIS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
package Stuff; |
119
|
|
|
|
|
|
|
use Moose; |
120
|
|
|
|
|
|
|
use MooseX::AttributeHelpers; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
has 'options' => ( |
123
|
|
|
|
|
|
|
metaclass => 'Collection::List', |
124
|
|
|
|
|
|
|
is => 'rw', |
125
|
|
|
|
|
|
|
isa => 'ArrayRef[Str]', |
126
|
|
|
|
|
|
|
default => sub { [] }, |
127
|
|
|
|
|
|
|
auto_deref => 1, |
128
|
|
|
|
|
|
|
provides => { |
129
|
|
|
|
|
|
|
elements => 'all_options', |
130
|
|
|
|
|
|
|
map => 'map_options', |
131
|
|
|
|
|
|
|
grep => 'filter_options', |
132
|
|
|
|
|
|
|
find => 'find_option', |
133
|
|
|
|
|
|
|
first => 'first_option', |
134
|
|
|
|
|
|
|
last => 'last_option', |
135
|
|
|
|
|
|
|
get => 'get_option', |
136
|
|
|
|
|
|
|
join => 'join_options', |
137
|
|
|
|
|
|
|
count => 'count_options', |
138
|
|
|
|
|
|
|
empty => 'do_i_have_options', |
139
|
|
|
|
|
|
|
sort => 'sorted_options', |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
no Moose; |
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 DESCRIPTION |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This is a role which provides the method generators for |
149
|
|
|
|
|
|
|
L<MooseX::AttributeHelpers::Collection::List>. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 METHODS |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=over 4 |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item B<meta> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=back |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 PROVIDED METHODS |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=over 4 |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item B<count> |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Returns the number of elements in the list. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
$stuff = Stuff->new; |
168
|
|
|
|
|
|
|
$stuff->options(["foo", "bar", "baz", "boo"]); |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
my $count = $stuff->count_options; |
171
|
|
|
|
|
|
|
print "$count\n"; # prints 4 |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item B<empty> |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
If the list is populated, returns true. Otherwise, returns false. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
$stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item B<find> |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
This method accepts a subroutine reference as its argument. That sub |
182
|
|
|
|
|
|
|
will receive each element of the list in turn. If it returns true for |
183
|
|
|
|
|
|
|
an element, that element will be returned by the C<find> method. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } ); |
186
|
|
|
|
|
|
|
print "$found\n"; # prints "bar" |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item B<grep> |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
This method accepts a subroutine reference as its argument. This |
191
|
|
|
|
|
|
|
method returns every element for which that subroutine reference |
192
|
|
|
|
|
|
|
returns a true value. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } ); |
195
|
|
|
|
|
|
|
print "@found\n"; # prints "bar baz boo" |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item B<map> |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
This method accepts a subroutine reference as its argument. The |
200
|
|
|
|
|
|
|
subroutine will be executed for each element of the list. It is |
201
|
|
|
|
|
|
|
expected to return a modified version of that element. The return |
202
|
|
|
|
|
|
|
value of the method is a list of the modified options. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } ); |
205
|
|
|
|
|
|
|
print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item B<sort> |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Sorts and returns the elements of the list. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
You can provide an optional subroutine reference to sort with (as you |
212
|
|
|
|
|
|
|
can with the core C<sort> function). However, instead of using C<$a> |
213
|
|
|
|
|
|
|
and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# ascending ASCIIbetical |
216
|
|
|
|
|
|
|
my @sorted = $stuff->sort_options(); |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# Descending alphabetical order |
219
|
|
|
|
|
|
|
my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } ); |
220
|
|
|
|
|
|
|
print "@sorted_options\n"; # prints "foo boo baz bar" |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item B<elements> |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Returns all of the elements of the list |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
my @option = $stuff->all_options; |
227
|
|
|
|
|
|
|
print "@options\n"; # prints "foo bar baz boo" |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item B<join> |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Joins every element of the list using the separator given as argument. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
my $joined = $stuff->join_options( ':' ); |
234
|
|
|
|
|
|
|
print "$joined\n"; # prints "foo:bar:baz:boo" |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item B<get> |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Returns an element of the list by its index. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
my $option = $stuff->get_option(1); |
241
|
|
|
|
|
|
|
print "$option\n"; # prints "bar" |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=item B<first> |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Returns the first element of the list. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
my $first = $stuff->first_option; |
248
|
|
|
|
|
|
|
print "$first\n"; # prints "foo" |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=item B<last> |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Returns the last element of the list. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
my $last = $stuff->last_option; |
255
|
|
|
|
|
|
|
print "$last\n"; # prints "boo" |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=back |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=head1 SUPPORT |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-AttributeHelpers> |
262
|
|
|
|
|
|
|
(or L<bug-MooseX-AttributeHelpers@rt.cpan.org|mailto:bug-MooseX-AttributeHelpers@rt.cpan.org>). |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
There is also a mailing list available for users of this distribution, at |
265
|
|
|
|
|
|
|
L<http://lists.perl.org/list/moose.html>. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
There is also an irc channel available for users of this distribution, at |
268
|
|
|
|
|
|
|
L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>. |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head1 AUTHOR |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
Stevan Little <stevan@iinteractive.com> |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
This software is copyright (c) 2007 by Stevan Little and Infinity Interactive, Inc. |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
279
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=cut |