1 package Moose::Manual::Delegation;
3 # ABSTRACT: Attribute delegation
9 =head1 WHAT IS DELEGATION?
11 Delegation is a feature that lets you create "proxy" methods that do nothing
12 more than call some other method on an attribute. This lets you simplify a
13 complex set of "has-a" relationships and present a single unified API from one
16 With delegation, consumers of a class don't need to know about all the
17 objects it contains, reducing the amount of API they need to learn.
19 Delegations are defined as a mapping between one or more methods
20 provided by the "real" class (the delegatee), and a set of
21 corresponding methods in the delegating class. The delegating class
22 can re-use the method names provided by the delegatee or provide its
25 Delegation is also a great way to wrap an existing class, especially a
26 non-Moose class or one that is somehow hard (or impossible) to
29 =head1 DEFINING A MAPPING
31 Moose offers a number of options for defining a delegation's mapping,
32 ranging from simple to complex.
34 The simplest form is to simply specify a list of methods:
43 handles => [qw( host path )],
46 With this definition, we can call C<< $website->host >> and it "just
47 works". Under the hood, Moose will call C<< $website->uri->host >> for
48 you. Note that C<$website> is not automatically passed to the C<host>
49 method; the invocant is C<< $website->uri >>.
51 We can also define a mapping as a hash reference. This allows you to
52 rename methods as part of the mapping:
67 In this example, we've created a C<< $website->hostname >> method,
68 rather than using C<URI.pm>'s name, C<host>.
70 These two mapping forms are the ones you will use most often. The
71 remaining methods are a bit more complex.
76 handles => qr/^(?:host|path|query.*)/,
79 This is similar to the array version, except it uses the regex to
80 match against all the methods provided by the delegatee. In order for
81 this to work, you must provide an C<isa> parameter for the attribute,
82 and it must be a class. Moose uses this to introspect the delegatee
83 class and determine what methods it provides.
85 You can use a role name as the value of C<handles>:
93 Moose will introspect the role to determine what methods it provides
94 and create a mapping for each of those methods.
96 Finally, you can also provide a sub reference to I<generate> a
97 mapping. You probably won't need this version often (if ever). See the
98 L<Moose> docs for more details on exactly how this works.
100 =head1 NATIVE DELEGATION
102 Native delegations allow you to delegate to standard Perl data structures as
103 if they were objects.
107 isa => 'ArrayRef[Item]',
108 default => sub { [ ] },
111 next_item => 'shift',
115 The C<Array> trait in the C<traits> parameter tells Moose that you would like
116 to use the set of Array helpers. Moose will then create C<add_item> and
117 C<next_item> methods that "just work". Behind the scenes C<add_item> is
121 my ($self, @items) = @_;
123 for my $item (@items) {
124 $Item_TC->validate($item);
127 push @{ $self->queue }, @items;
130 Moose includes the following traits for native delegation:
134 =item * L<Array|Moose::Meta::Attribute::Native::Trait::Array>
136 =item * L<Bool|Moose::Meta::Attribute::Native::Trait::Bool>
138 =item * L<Code|Moose::Meta::Attribute::Native::Trait::Code>
140 =item * L<Counter|Moose::Meta::Attribute::Native::Trait::Counter>
142 =item * L<Hash|Moose::Meta::Attribute::Native::Trait::Hash>
144 =item * L<Number|Moose::Meta::Attribute::Native::Trait::Number>
146 =item * L<String|Moose::Meta::Attribute::Native::Trait::String>
152 Currying allows you to create a method with some pre-set parameters. You can
153 create a curried delegation method:
160 isa => 'HTTP::Request',
162 set_user_agent => [ header => 'UserAgent' ],
166 With this definition, calling C<< $spider->set_user_agent('MyClient') >> will
167 call C<< $spider->request->header('UserAgent', 'MyClient') >> behind the
170 Note that with currying, the currying always start with the first parameter to
171 a method (C<$_[0]>). Any arguments you pass to the delegation come after the
174 =head1 MISSING ATTRIBUTES
176 It is perfectly valid to delegate methods to an attribute which is not
177 required or can be undefined. When a delegated method is called, Moose
178 will throw a runtime error if the attribute does not contain an