Add another recommendation, always call inner() in the most specific subclass
[gitmo/Moose.git] / lib / Moose / Manual / Delegation.pod
CommitLineData
093a09aa 1=pod
2
3=head1 NAME
4
d67ce58f 5Moose::Manual::Delegation - Attribute delegation
093a09aa 6
7=head1 WHAT IS DELEGATION?
8
d3e02c74 9Delegation is a feature that lets you create create "shadow" methods
10that do nothing more than call some other method on an attribute. This
11is quite handy since it lets you simplify a complex set of "has-a"
093a09aa 12relationships and present a single unified API from one class.
13
d3e02c74 14With delegation, consumers of a class don't need to know about all the
15objects it contains, reducing the amount of API they need to learn.
093a09aa 16
17Delegations are defined as a mapping between one or more methods
18provided by the "real" class (the delegatee), and a set of
19corresponding methods in the delegating class. The delegating class
d3e02c74 20can re-use the method names provided by the delegatee or provide its
093a09aa 21own names.
22
23Delegation is also a great way to wrap an existing class, especially a
24non-Moose class or one that is somehow hard (or impossible) to
25subclass.
26
27=head1 DEFINING A MAPPING
28
29Moose offers a number of options for defining a delegation's mapping,
30ranging from simple to complex.
31
32The simplest form is to simply specify a list of methods:
33
34 package Website;
35
36 use Moose;
37
38 has 'uri' => (
39 is => 'ro',
40 isa => 'URI',
41 handles => [qw( host path )],
42 );
43
44With this definition, we can call C<< $website->host >> and it "just
45works". Under the hood, Moose will call C<< $website->uri->host >> for
46you.
47
48We can also define a mapping as a hash reference. This allows you to
49rename methods as part of the mapping:
50
51 package Website;
52
53 use Moose;
54
55 has 'uri' => (
56 is => 'ro',
57 isa => 'URI',
58 handles => {
59 hostname => 'host',
60 path => 'path',
61 },
62 );
63
64In this example, we've created a C<< $website->hostname >> method,
65rather than using C<URI.pm>'s name, C<host>.
66
67These two mapping forms are the ones you will use most often. The
68remainder are a bit more complex, and less common.
69
70 has 'uri' => (
71 is => 'ro',
72 isa => 'URI',
73 handles => qr/^(?:host|path|query.*)/,
74 );
75
76This is similar to the array version, except it uses the regex to
77match against all the methods provided by the delegatee. In order for
78this to work, you must provide an C<isa> parameter for the attribute,
79and it must be a class. Moose uses this to introspect the delegatee
80class and determine what methods it provides.
81
82You can use a role name as the value of C<handles>:
83
84 has 'uri' => (
85 is => 'ro',
86 isa => 'URI',
87 handles => 'HasURI',
88 );
89
90Moose will introspect the role to determine what methods it provides
91and create a mapping for each of those methods.
92
93Finally, you can also provide a sub reference to I<generate> a
94mapping. You probably won't need this version often (if ever). See the
95L<Moose> docs for more details on exactly how this works.
96
97=head1 MISSING ATTRIBUTES
98
99It is perfectly valid to delegate methods to an attribute which is not
d3e02c74 100required or can be undefined. When a delegated method is called, Moose
101will throw a runtime error if the attribute does not contain an
102object.
093a09aa 103
104=head1 AUTHOR
105
106Dave Rolsky E<lt>autarch@urth.orgE<gt>
107
108=head1 COPYRIGHT AND LICENSE
109
2840a3b2 110Copyright 2009 by Infinity Interactive, Inc.
093a09aa 111
112L<http://www.iinteractive.com>
113
114This library is free software; you can redistribute it and/or modify
115it under the same terms as Perl itself.
116
117=cut