Handles also will allow you to delegate to "helper" methods that work on
common Perl data structures. If you remember or have ever used
-L<MooseX::AttributeHelpers|MooseX::AttributeHelpers> the mechanisim is very
-similar.
-
+L<MooseX::AttributeHelpers|MooseX::AttributeHelpers> the mechanism is very
+similar.
+
has 'queue' => (
- isa => 'ArrayRef[Item]',
- traits => ['Array'],
+ isa => 'ArrayRef[Item]',
+ traits => ['Array'],
default => sub { [ ] },
handles => {
- add_item => 'push',
+ add_item => 'push',
next_item => 'shift',
- }
+ },
)
-By providing using C<Array> trait to the C<traits> parameter you signal to
+By providing the C<Array> trait to the C<traits> parameter you signal to
Moose that you would like to use the set of Array helpers. Moose will then
-create an C<add_item> and a C<next_item> method that "just works". Behind the
+create C<add_item> and C<next_item> method that "just works". Behind the
scenes C<add_item> is something like
- sub add_item {
+ sub add_item {
my ($self, @items) = @_;
+
+ for my $item (@items) {
+ $Item_TC->validate($item);
+ }
+
push @{ $self->queue }, @items;
}
=head1 CURRYING
Currying is a way of creating a method or function from another method or
-function with one of the parameters pre-defined. Moose provides the ability to
-curry methods when creating delegates.
+function with some of the parameters pre-defined. Moose provides the ability to
+curry methods when creating delegates.
package Spider;
use Moose;
has request => (
- is => 'ro'
- isa => 'HTTP::Request',
+ is => 'ro'
+ isa => 'HTTP::Request',
handles => {
- set_user_agent => [ header => 'UserAgent'],
- }
+ set_user_agent => [ header => 'UserAgent' ],
+ },
)
-With this definiton calling C<$spider->set_user_agent('MyClient')> will behind
-the scenes call C<$spider->request->header('UserAgent', 'MyClient')>.
+With this definition, calling C<< $spider->set_user_agent('MyClient') >> will
+behind the scenes call C<< $spider->request->header('UserAgent', 'MyClient') >>.
=head1 MISSING ATTRIBUTES