MooseX::AttributeHelpers::MethodProvider::List(3pm) | User Contributed Perl Documentation | MooseX::AttributeHelpers::MethodProvider::List(3pm) |
MooseX::AttributeHelpers::MethodProvider::List - method generator for MooseX::AttributeHelpers::Collection::List
version 0.25
package Stuff; use Moose; use MooseX::AttributeHelpers; has 'options' => ( metaclass => 'Collection::List', is => 'rw', isa => 'ArrayRef[Str]', default => sub { [] }, auto_deref => 1, provides => { elements => 'all_options', map => 'map_options', grep => 'filter_options', find => 'find_option', first => 'first_option', last => 'last_option', get => 'get_option', join => 'join_options', count => 'count_options', empty => 'do_i_have_options', sort => 'sorted_options', } ); no Moose; 1;
This is a role which provides the method generators for MooseX::AttributeHelpers::Collection::List.
$stuff = Stuff->new; $stuff->options(["foo", "bar", "baz", "boo"]); my $count = $stuff->count_options; print "$count\n"; # prints 4
$stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } ); print "$found\n"; # prints "bar"
my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } ); print "@found\n"; # prints "bar baz boo"
my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } ); print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
You can provide an optional subroutine reference to sort with (as you can with the core "sort" function). However, instead of using $a and $b, you will need to use $_[0] and $_[1] instead.
# ascending ASCIIbetical my @sorted = $stuff->sort_options(); # Descending alphabetical order my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } ); print "@sorted_options\n"; # prints "foo boo baz bar"
my @option = $stuff->all_options; print "@options\n"; # prints "foo bar baz boo"
my $joined = $stuff->join_options( ':' ); print "$joined\n"; # prints "foo:bar:baz:boo"
my $option = $stuff->get_option(1); print "$option\n"; # prints "bar"
my $first = $stuff->first_option; print "$first\n"; # prints "foo"
my $last = $stuff->last_option; print "$last\n"; # prints "boo"
Bugs may be submitted through the RT bug tracker <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-AttributeHelpers> (or bug-MooseX-AttributeHelpers@rt.cpan.org <mailto:bug-MooseX-AttributeHelpers@rt.cpan.org>).
There is also a mailing list available for users of this distribution, at <http://lists.perl.org/list/moose.html>.
There is also an irc channel available for users of this distribution, at "#moose" on "irc.perl.org" <irc://irc.perl.org/#moose>.
Stevan Little <stevan@iinteractive.com>
This software is copyright (c) 2007 by Stevan Little and Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2022-06-15 | perl v5.34.0 |