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',
+ all_options => 'elements',
+ map_options => 'map',
+ filter_options => 'grep',
+ find_option => 'first',
+ first_option => 'head',
+ all_but_first_option => 'tail',
+ last_option => 'last',
+ get_option => 'get',
+ join_options => 'join',
+ count_options => 'count',
+ has_no_options => 'empty',
+ sorted_options => 'sort',
}
);
These methods are all equivalent to the Perl core functions of the same name.
-=item B<find( sub { ... } )>
+=item B<first( sub { ... } )>
This method returns the first item matching item in the array. The matching is
done with a subroutine reference you pass to this method. The reference will
Empties the entire array, like C<@array = ()>.
-=item B<first>
+=item B<head>
Returns the first element of the array.
my $first = $stuff->first_option;
print "$first\n"; # prints "foo"
+=item B<tail>
+
+Returns all elements of the array after the first.
+
+ my @tail = $stuff->all_but_first_option;
+ print join(', ', @tail), "\n"; # prints "bar, baz, boo"
+
=item B<last>
Returns the last element of the array.
use strict;
use warnings;
-use Test::More tests => 33;
+use Test::More tests => 34;
use Test::Exception;
use Test::Moose 'does_ok';
init_arg => 'options',
default => sub { [] },
handles => {
- 'num_options' => 'count',
- 'has_no_options' => 'empty',
- 'map_options', => 'map',
- 'filter_options' => 'grep',
- 'find_option' => 'find',
- 'options' => 'elements',
- 'join_options' => 'join',
- 'get_option_at' => 'get',
- 'get_first_option' => 'first',
- 'get_last_option' => 'last',
- 'sorted_options' => 'sort',
- 'less_than_five' => [ grep => [ $less = sub { $_ < 5 } ] ],
- 'up_by_one' => [ map => [ $up = sub { $_ + 1 } ] ],
+ 'num_options' => 'count',
+ 'has_no_options' => 'empty',
+ 'map_options', => 'map',
+ 'filter_options' => 'grep',
+ 'find_option' => 'first',
+ 'options' => 'elements',
+ 'join_options' => 'join',
+ 'get_option_at' => 'get',
+ 'get_first_option' => 'head',
+ 'all_but_first_option' => 'tail',
+ 'get_last_option' => 'last',
+ 'sorted_options' => 'sort',
+ 'less_than_five' => [ grep => [ $less = sub { $_ < 5 } ] ],
+ 'up_by_one' => [ map => [ $up = sub { $_ + 1 } ] ],
'dashify' => [ join => ['-'] ],
'descending' => [ sort => [ $sort = sub { $_[1] <=> $_[0] } ] ],
},
ok( !$stuff->has_no_options, '... we have options' );
is( $stuff->num_options, 10, '... got 2 options' );
cmp_ok( $stuff->get_option_at(0), '==', 1, '... get option 0' );
-cmp_ok( $stuff->get_first_option, '==', 1, '... get first' );
+cmp_ok( $stuff->get_first_option, '==', 1, '... get head' );
+is_deeply( [ $stuff->all_but_first_option ], [ 2 .. 10 ], '... get tail' );
cmp_ok( $stuff->get_last_option, '==', 10, '... get last' );
is_deeply(
is_deeply(
$options->handles,
{
- 'num_options' => 'count',
- 'has_no_options' => 'empty',
- 'map_options', => 'map',
- 'filter_options' => 'grep',
- 'find_option' => 'find',
- 'options' => 'elements',
- 'join_options' => 'join',
- 'get_option_at' => 'get',
- 'get_first_option' => 'first',
- 'get_last_option' => 'last',
- 'sorted_options' => 'sort',
- 'less_than_five' => [ grep => [$less] ],
- 'up_by_one' => [ map => [$up] ],
- 'dashify' => [ join => ['-'] ],
- 'descending' => [ sort => [$sort] ],
+ 'num_options' => 'count',
+ 'has_no_options' => 'empty',
+ 'map_options', => 'map',
+ 'filter_options' => 'grep',
+ 'find_option' => 'first',
+ 'options' => 'elements',
+ 'join_options' => 'join',
+ 'get_option_at' => 'get',
+ 'get_first_option' => 'head',
+ 'all_but_first_option' => 'tail',
+ 'get_last_option' => 'last',
+ 'sorted_options' => 'sort',
+ 'less_than_five' => [ grep => [$less] ],
+ 'up_by_one' => [ map => [$up] ],
+ 'dashify' => [ join => ['-'] ],
+ 'descending' => [ sort => [$sort] ],
},
'... got the right handles mapping'
);