X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FAttribute%2FNative%2FTrait%2FArray.pm;h=cb350975b1be56bce8f18ea8e55ca3208796ed47;hb=60f0816092ffe11986388dd2bba56a356b697843;hp=10903fccce96f0df47d3841b4f6de1bd9ed83f6c;hpb=af44c00c180f0262a59e60070e0dbde797b663ab;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm index 10903fc..cb35097 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Array.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Array.pm @@ -2,7 +2,7 @@ package Moose::Meta::Attribute::Native::Trait::Array; use Moose::Role; -our $VERSION = '0.89'; +our $VERSION = '1.09'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -29,7 +29,7 @@ __END__ =head1 NAME -Moose::Meta::Attribute::Native::Trait::Array +Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes =head1 SYNOPSIS @@ -41,28 +41,28 @@ Moose::Meta::Attribute::Native::Trait::Array is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, - handles => { - all_options => 'elements', - map_options => 'map', - filter_options => 'grep', - find_option => 'find', - first_option => 'first', - last_option => 'last', - get_option => 'get', - join_options => 'join', - count_options => 'count', - has_no_options => 'empty', - sorted_options => 'sort', - } + 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; - + =head1 DESCRIPTION This module provides an Array attribute which provides a number of -array operations. +array operations. =head1 PROVIDED METHODS @@ -81,47 +81,84 @@ Returns the number of elements in the array. my $count = $stuff->count_options; print "$count\n"; # prints 4 -=item B +=item B -If the array is populated, returns false. Otherwise, returns true. +Returns a boolean value that is true when the array has no elements. $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n"; -=item B +=item B + +Returns all of the elements of the array. + + my @option = $stuff->all_options; + print "@options\n"; # prints "foo bar baz boo" + +=item B + +Returns an element of the array by its index. You can also use negative index +numbers, just as with Perl's core array handling. + + my $option = $stuff->get_option(1); + print "$option\n"; # prints "bar" + +=item B + +=item B + +=item B + +=item B + +=item B -This method accepts a subroutine reference as its argument. That sub -will receive each element of the array in turn. If it returns true for -an element, that element will be returned by the C method. +These methods are all equivalent to the Perl core functions of the same name. - my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } ); +=item B + +This method returns the first item matching item in the array, just like +L's C function. The matching is done with a subroutine +reference you pass to this method. The reference will be called against each +element in the array until one matches or all elements have been checked. + + my $found = $stuff->find_option( sub { /^b/ } ); print "$found\n"; # prints "bar" -=item B +=item B -This method accepts a subroutine reference as its argument. This -method returns every element for which that subroutine reference -returns a true value. +This method returns every element matching a given criteria, just like Perl's +core C function. This method requires a subroutine which implements the +matching logic. - my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } ); + my @found = $stuff->filter_options( sub { /^b/ } ); print "@found\n"; # prints "bar baz boo" -=item B +=item B -This method accepts a subroutine reference as its argument. The -subroutine will be executed for each element of the array. It is -expected to return a modified version of that element. The return -value of the method is a list of the modified options. +This method transforms every element in the array and returns a new array, +just like Perl's core C function. This method requires a subroutine which +implements the transformation. - my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } ); + my @mod_options = $stuff->map_options( sub { $_ . "-tag" } ); print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" -=item B +=item B + +This method condenses an array into a single value, by passing a function the +value so far and the next value in the array, just like L's +C function. The reducing is done with a subroutine reference you pass +to this method. -Sorts and returns the elements of the array. + my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } ); + print "$found\n"; # prints "foobarbazboo" -You can provide an optional subroutine reference to sort with (as you -can with the core C function). However, instead of using C<$a> -and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead. +=item B + +Returns a the array in sorted order. + +You can provide an optional subroutine reference to sort with (as you can with +Perl's core C function). However, instead of using C<$a> and C<$b>, you +will need to use C<$_[0]> and C<$_[1]> instead. # ascending ASCIIbetical my @sorted = $stuff->sort_options(); @@ -130,71 +167,60 @@ and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead. my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } ); print "@sorted_options\n"; # prints "foo boo baz bar" -=item B - -Returns all of the elements of the array - - my @option = $stuff->all_options; - print "@options\n"; # prints "foo bar baz boo" - -=item B - -Joins every element of the array using the separator given as argument. +=item B - my $joined = $stuff->join_options( ':' ); - print "$joined\n"; # prints "foo:bar:baz:boo" +Sorts the array I, modifying the value of the attribute. -=item B +You can provide an optional subroutine reference to sort with (as you can with +Perl's core C function). However, instead of using C<$a> and C<$b>, you +will need to use C<$_[0]> and C<$_[1]> instead. -Returns an element of the array by its index. +=item B - my $option = $stuff->get_option(1); - print "$option\n"; # prints "bar" +Returns the array, with indices in random order, like C from +L. -=item B +=item B -Returns the first element of the array. +Returns the array, with all duplicate elements removed, like C from +L. - my $first = $stuff->first_option; - print "$first\n"; # prints "foo" +=item B -=item B +Joins every element of the array using the separator given as argument, just +like Perl's core C function. -Returns the last element of the array. + my $joined = $stuff->join_options( ':' ); + print "$joined\n"; # prints "foo:bar:baz:boo" - my $last = $stuff->last_option; - print "$last\n"; # prints "boo" +=item B -=item B +Given an index and a value, sets the specified array element's value. -=item B +=item B -=item B +Removes the element at the given index from the array. -=item B +=item B -=item B +Inserts a new element into the array at the given index. =item B -=item B - -=item B - -=item B +Empties the entire array, like C<@array = ()>. -=item B - -Sorts the array I, modifying the value of the attribute. +=item B -You can provide an optional subroutine reference to sort with (as you -can with the core C function). However, instead of using C<$a> -and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead. +This method provides a get/set accessor for the array, based on array indexes. +If passed one argument, it returns the value at the specified index. If +passed two arguments, it sets the value of the specified index. -=item B +=item B -If passed one argument, returns the value of the requested element. -If passed two arguments, sets the value of the requested element. +This method returns an iterator which, on each call, returns C<$n> more items +from the array, in order, like C from L. A coderef +can optionally be provided; it will be called on each group of C<$n> elements +in the array. =back @@ -212,9 +238,7 @@ If passed two arguments, sets the value of the requested element. =head1 BUGS -All complex software has bugs lurking in it, and this module is no -exception. If you find a bug please either email me, or add the bug -to cpan-RT. +See L for details on reporting bugs. =head1 AUTHOR