1MooseX::AttributeHelperUss:e:rMeCtohnMotodroPisrbeouXvt:ie:ddAetrPt:er:riLlbiusDttoe(cH3ue)mlepnetrast:i:oMnethodProvider::List(3)
2
3
4
6 MooseX::AttributeHelpers::MethodProvider::List
7
9 package Stuff;
10 use Moose;
11 use MooseX::AttributeHelpers;
12
13 has 'options' => (
14 metaclass => 'Collection::List',
15 is => 'rw',
16 isa => 'ArrayRef[Str]',
17 default => sub { [] },
18 auto_deref => 1,
19 provides => {
20 elements => 'all_options',
21 map => 'map_options',
22 grep => 'filter_options',
23 find => 'find_option',
24 first => 'first_option',
25 last => 'last_option',
26 get => 'get_option',
27 join => 'join_options',
28 count => 'count_options',
29 empty => 'do_i_have_options',
30 sort => 'sorted_options',
31 }
32 );
33
34 no Moose;
35 1;
36
38 This is a role which provides the method generators for
39 MooseX::AttributeHelpers::Collection::List.
40
42 meta
43
45 count
46 Returns the number of elements in the list.
47
48 $stuff = Stuff->new;
49 $stuff->options(["foo", "bar", "baz", "boo"]);
50
51 my $count = $stuff->count_options;
52 print "$count\n"; # prints 4
53
54 empty
55 If the list is populated, returns true. Otherwise, returns false.
56
57 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
58
59 find
60 This method accepts a subroutine reference as its argument. That
61 sub will receive each element of the list in turn. If it returns
62 true for an element, that element will be returned by the "find"
63 method.
64
65 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
66 print "$found\n"; # prints "bar"
67
68 grep
69 This method accepts a subroutine reference as its argument. This
70 method returns every element for which that subroutine reference
71 returns a true value.
72
73 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
74 print "@found\n"; # prints "bar baz boo"
75
76 map This method accepts a subroutine reference as its argument. The
77 subroutine will be executed for each element of the list. It is
78 expected to return a modified version of that element. The return
79 value of the method is a list of the modified options.
80
81 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
82 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
83
84 sort
85 Sorts and returns the elements of the list.
86
87 You can provide an optional subroutine reference to sort with (as
88 you can with the core "sort" function). However, instead of using
89 $a and $b, you will need to use $_[0] and $_[1] instead.
90
91 # ascending ASCIIbetical
92 my @sorted = $stuff->sort_options();
93
94 # Descending alphabetical order
95 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
96 print "@sorted_options\n"; # prints "foo boo baz bar"
97
98 elements
99 Returns all of the elements of the list
100
101 my @option = $stuff->all_options;
102 print "@options\n"; # prints "foo bar baz boo"
103
104 join
105 Joins every element of the list using the separator given as
106 argument.
107
108 my $joined = $stuff->join_options( ':' );
109 print "$joined\n"; # prints "foo:bar:baz:boo"
110
111 get Returns an element of the list by its index.
112
113 my $option = $stuff->get_option(1);
114 print "$option\n"; # prints "bar"
115
116 first
117 Returns the first element of the list.
118
119 my $first = $stuff->first_option;
120 print "$first\n"; # prints "foo"
121
122 last
123 Returns the last element of the list.
124
125 my $last = $stuff->last_option;
126 print "$last\n"; # prints "boo"
127
129 All complex software has bugs lurking in it, and this module is no
130 exception. If you find a bug please either email me, or add the bug to
131 cpan-RT.
132
134 Stevan Little <stevan@iinteractive.com>
135
137 Copyright 2007-2009 by Infinity Interactive, Inc.
138
139 <http://www.iinteractive.com>
140
141 This library is free software; you can redistribute it and/or modify it
142 under the same terms as Perl itself.
143
144
145
146perl v5.12.0 Moose2X0:1:0A-t0t1r-i0b1uteHelpers::MethodProvider::List(3)