From: Chris Prather Date: Sun, 23 Aug 2009 19:04:49 +0000 (-0400) Subject: add documentation on the new AH delegation (Currying and Perl Data Structures) X-Git-Tag: 0.89_01~5^2~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=50f346d77d9505cae6370603be0ab51fe3298aa2;p=gitmo%2FMoose.git add documentation on the new AH delegation (Currying and Perl Data Structures) --- diff --git a/lib/Moose/Manual/Delegation.pod b/lib/Moose/Manual/Delegation.pod index 359acd4..0203892 100644 --- a/lib/Moose/Manual/Delegation.pod +++ b/lib/Moose/Manual/Delegation.pod @@ -6,7 +6,7 @@ Moose::Manual::Delegation - Attribute delegation =head1 WHAT IS DELEGATION? -Delegation is a feature that lets you create "shadow" methods that +Delegation is a feature that lets you create "proxy" methods that do nothing more than call some other method on an attribute. This is quite handy since it lets you simplify a complex set of "has-a" relationships and present a single unified API from one class. @@ -94,6 +94,57 @@ Finally, you can also provide a sub reference to I a mapping. You probably won't need this version often (if ever). See the L docs for more details on exactly how this works. +=head1 PERL DATA STRUCTURES + +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 the mechanisim is very +similar. + + has 'queue' => ( + isa => 'ArrayRef[Item]', + traits => ['Array'], + default => sub { [ ] }, + handles => { + add_item => 'push', + next_item => 'shift', + } + ) + +By providing using C trait to the C parameter you signal to +Moose that you would like to use the set of Array helpers. Moose will then +create an C and a C method that "just works". Behind the +scenes C is something like + + sub add_item { + my ($self, @items) = @_; + push @{ $self->queue }, @items; + } + +There are traits for not only C but also C, C, C, +C, and C. For more information see the documentation in +L. + +=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. + + package Spider; + use Moose; + + has request => ( + is => 'ro' + isa => 'HTTP::Request', + handles => { + 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')>. + =head1 MISSING ATTRIBUTES It is perfectly valid to delegate methods to an attribute which is not