Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes |
Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes
version 2.1605
package Stuff; use Moose;
has 'options' => ( traits => ['Array'], is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, handles => { all_options => 'elements', add_option => 'push', map_options => 'map', filter_options => 'grep', find_option => 'first', get_option => 'get', join_options => 'join', count_options => 'count', has_options => 'count', has_no_options => 'is_empty', sorted_options => 'sort', }, );
no Moose; 1;
This trait provides native delegation methods for array references.
If you don't provide an isa
value for your attribute, it will default to
ArrayRef
.
$stuff = Stuff->new; $stuff->options( [ "foo", "bar", "baz", "boo" ] );
print $stuff->count_options; # prints 4
This method does not accept any arguments.
$stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
This method does not accept any arguments.
my @option = $stuff->all_options; print "@options\n"; # prints "foo bar baz boo"
This method does not accept any arguments.
my $option = $stuff->get_option(1); print "$option\n"; # prints "bar"
If the specified element does not exist, this will return undef
.
This method accepts just one argument.
pop
.
This method does not accept any arguments.
push
. Returns the number of elements in the new
array.
This method accepts any number of arguments.
shift
.
This method does not accept any arguments.
unshift
. Returns the number of elements in the new
array.
This method accepts any number of arguments.
splice
. In scalar context, this returns the last
element removed, or undef
if no elements were removed. In list context,
this returns all the elements removed from the array.
This method requires at least one argument.
first
function. The matching is done with a subroutine
reference you pass to this method. The subroutine will be called against each
element in the array until one matches or all elements have been checked.
Each list element will be available to the sub in $_
.
my $found = $stuff->find_option( sub {/^b/} ); print "$found\n"; # prints "bar"
This method requires a single argument.
first_index
function. The matching is done with a
subroutine reference you pass to this method. The subroutine will be called
against each element in the array until one matches or all elements have been
checked. Each list element will be available to the sub in $_
.
This method requires a single argument.
grep
function. This method requires a subroutine which implements the
matching logic; each list element will be available to the sub in $_
.
my @found = $stuff->filter_options( sub {/^b/} ); print "@found\n"; # prints "bar baz boo"
This method requires a single argument.
map
function. This method requires a subroutine which
implements the transformation; each list element will be available to the sub
in $_
.
my @mod_options = $stuff->map_options( sub { $_ . "-tag" } ); print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
This method requires a single argument.
reduce
function. The reducing is done with a subroutine reference you pass
to this method; each list element will be available to the sub in $_
.
my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } ); print "$found\n"; # prints "foobarbazboo"
This method requires a single argument.
elements
, returns the number of elements in the array in scalar context.
You can provide an optional subroutine reference to sort with (as you can with
Perl's core sort
function). However, instead of using $a
and $b
in
this subroutine, you will need to use $_[0]
and $_[1]
.
# 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"
This method accepts a single argument.
You can provide an optional subroutine reference to sort with (as you can with
Perl's core sort
function). However, instead of using $a
and $b
, you
will need to use $_[0]
and $_[1]
instead.
This method does not define a return value.
This method accepts a single argument.
shuffle
from
the List::Util manpage.
This method does not accept any arguments.
uniq
from
the List::MoreUtils manpage.
This method does not accept any arguments.
join
function.
my $joined = $stuff->join_options(':'); print "$joined\n"; # prints "foo:bar:baz:boo"
This method requires a single argument.
This method returns the value at $index
after the set.
This method requires two arguments.
This method returns the deleted value. Note that if no value exists, it will
return undef
.
This method requires one argument.
This method returns the new value at $index
.
This method requires two arguments.
@array = ()
.
This method does not define a return value.
This method does not accept any arguments.
When called as a setter, this method returns the new value at $index
.
This method accepts one or two arguments.
$n
more items
from the array, in order, like natatime
from the List::MoreUtils manpage.
If you pass a coderef as the second argument, then this code ref will be
called on each group of $n
elements in the array until the array is
exhausted.
This method accepts one or two arguments.
See Moose/BUGS for details on reporting bugs.
This software is copyright (c) 2006 by 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.
Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes |