1MooseX::AttributeHelperUss:e:rMeCMtoohnootsdrePiXrb:ou:vtAietddterrPi:eb:ruLltiesDHtoe(cl3uppmemer)nst:a:tMieotnhodProvider::List(3pm)
2
3
4
6 MooseX::AttributeHelpers::MethodProvider::List
7
9 version 0.25
10
12 package Stuff;
13 use Moose;
14 use MooseX::AttributeHelpers;
15
16 has 'options' => (
17 metaclass => 'Collection::List',
18 is => 'rw',
19 isa => 'ArrayRef[Str]',
20 default => sub { [] },
21 auto_deref => 1,
22 provides => {
23 elements => 'all_options',
24 map => 'map_options',
25 grep => 'filter_options',
26 find => 'find_option',
27 first => 'first_option',
28 last => 'last_option',
29 get => 'get_option',
30 join => 'join_options',
31 count => 'count_options',
32 empty => 'do_i_have_options',
33 sort => 'sorted_options',
34 }
35 );
36
37 no Moose;
38 1;
39
41 This is a role which provides the method generators for
42 MooseX::AttributeHelpers::Collection::List.
43
45 meta
46
48 count
49 Returns the number of elements in the list.
50
51 $stuff = Stuff->new;
52 $stuff->options(["foo", "bar", "baz", "boo"]);
53
54 my $count = $stuff->count_options;
55 print "$count\n"; # prints 4
56
57 empty
58 If the list is populated, returns true. Otherwise, returns false.
59
60 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
61
62 find
63 This method accepts a subroutine reference as its argument. That
64 sub will receive each element of the list in turn. If it returns
65 true for an element, that element will be returned by the "find"
66 method.
67
68 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
69 print "$found\n"; # prints "bar"
70
71 grep
72 This method accepts a subroutine reference as its argument. This
73 method returns every element for which that subroutine reference
74 returns a true value.
75
76 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
77 print "@found\n"; # prints "bar baz boo"
78
79 map This method accepts a subroutine reference as its argument. The
80 subroutine will be executed for each element of the list. It is
81 expected to return a modified version of that element. The return
82 value of the method is a list of the modified options.
83
84 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
85 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
86
87 sort
88 Sorts and returns the elements of the list.
89
90 You can provide an optional subroutine reference to sort with (as
91 you can with the core "sort" function). However, instead of using
92 $a and $b, you will need to use $_[0] and $_[1] instead.
93
94 # ascending ASCIIbetical
95 my @sorted = $stuff->sort_options();
96
97 # Descending alphabetical order
98 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
99 print "@sorted_options\n"; # prints "foo boo baz bar"
100
101 elements
102 Returns all of the elements of the list
103
104 my @option = $stuff->all_options;
105 print "@options\n"; # prints "foo bar baz boo"
106
107 join
108 Joins every element of the list using the separator given as
109 argument.
110
111 my $joined = $stuff->join_options( ':' );
112 print "$joined\n"; # prints "foo:bar:baz:boo"
113
114 get Returns an element of the list by its index.
115
116 my $option = $stuff->get_option(1);
117 print "$option\n"; # prints "bar"
118
119 first
120 Returns the first element of the list.
121
122 my $first = $stuff->first_option;
123 print "$first\n"; # prints "foo"
124
125 last
126 Returns the last element of the list.
127
128 my $last = $stuff->last_option;
129 print "$last\n"; # prints "boo"
130
132 Bugs may be submitted through the RT bug tracker
133 <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-
134 AttributeHelpers> (or bug-MooseX-AttributeHelpers@rt.cpan.org
135 <mailto:bug-MooseX-AttributeHelpers@rt.cpan.org>).
136
137 There is also a mailing list available for users of this distribution,
138 at <http://lists.perl.org/list/moose.html>.
139
140 There is also an irc channel available for users of this distribution,
141 at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>.
142
144 Stevan Little <stevan@iinteractive.com>
145
147 This software is copyright (c) 2007 by Stevan Little and Infinity
148 Interactive, Inc.
149
150 This is free software; you can redistribute it and/or modify it under
151 the same terms as the Perl 5 programming language system itself.
152
153
154
155perl v5.28.0 MooseX:2:0A1t6t-r0i2b-u1t6eHelpers::MethodProvider::List(3pm)