README
1NAME
2 mixin - Mix-in inheritance, an alternative to multiple inheritance
3
4SYNOPSIS
5 package Dog;
6 sub speak { print "Bark!\n" }
7 sub new { my $class = shift; bless {}, $class }
8
9 package Dog::Small;
10 use base 'Dog';
11 sub speak { print "Yip!\n"; }
12
13 package Dog::Retriever;
14 use mixin::with 'Dog';
15 sub fetch { print "Get your own stinking $_[1]\n" }
16
17 package Dog::Small::Retriever;
18 use base 'Dog::Small';
19 use mixin 'Dog::Retriever';
20
21 my $small_retriever = Dog::Small::Retriever->new;
22 $small_retriever->speak; # Yip!
23 $small_retriever->fetch('ball'); # Get your own stinking ball
24
25DESCRIPTION
26 NOTE You probably want to look into the similar but superior concept of
27 traits/roles instead. See "SEE ALSO" for suggested modules.
28
29 Mixin inheritance is an alternative to the usual multiple-inheritance
30 and solves the problem of knowing which parent will be called. It also
31 solves a number of tricky problems like diamond inheritence.
32
33 The idea is to solve the same sets of problems which MI solves without
34 the problems of MI. For all practical purposes you can think of a mixin
35 as multiple inheritance without the actual inheritance.
36
37 Mixins are a band-aid for the problems of MI. A better solution is to
38 use traits (called "Roles" in Perl 6), which are like mixins on
39 steroids. Class::Trait implements this.
40
41 Using a mixin class
42 There are two steps to using a mixin-class.
43
44 First, make sure you are inherited from the class with which the
45 mixin-class is to be mixed.
46
47 package Dog::Small::Retriever;
48 use base 'Dog::Small';
49
50 Since Dog::Small isa Dog, that does it. Then simply mixin the new
51 functionality
52
53 use mixin 'Dog::Retriever';
54
55 and now you can use fetch().
56
57 Writing a mixin class
58 See mixin::with.
59
60 Mixins, Inheritance and SUPER
61 A class which uses a mixin *does not* inherit from it. However, through
62 some clever trickery, "SUPER" continues to work. Here's an example.
63
64 {
65 package Parent;
66 sub foo { "Parent" }
67 }
68
69 {
70 package Middle;
71 use mixin::with "Parent";
72
73 sub foo {
74 my $self = shift;
75 return $self->SUPER::foo(), "Middle";
76 }
77 }
78
79 {
80 package Child;
81 use base "Parent";
82 use mixin "Middle";
83
84 sub foo {
85 my $self = shift;
86 return $self->SUPER::foo(), "Child";
87 }
88 }
89
90 print join " ", Child->foo; # Parent Middle Child
91
92 This will print "Parent Middle Child". You'll note that this is the same
93 result if Child inherited from Middle and Middle from Parent. Its also
94 the same result if Child multiply inherited from Middle and Parent but
95 *NOT* if it inherited from Parent then Middle. The advantage of mixins
96 vs multiple inheritance is such ambiguities do not exist.
97
98 Note that even though both the Child and Middle define foo() the Middle
99 mixin does not overwrite Child's foo(). A mixin does not simply export
100 its methods into the mixer and thus does not blow over existing methods.
101
102NOTES
103 A mixin will not warn if the mixin and the user define the same method.
104
105AUTHOR
106 Michael G Schwern <schwern@pobox.com>
107
108LICENSE
109 Copyright 2002-2015 by Michael G Schwern
110
111 This library is free software; you can redistribute it and/or modify it
112 under the same terms as Perl itself.
113
114 <http://dev.perl.org/licenses/>
115
116SEE ALSO
117 Role::Tiny - A stand alone implementation of traits/roles, like mixins
118 but better.
119
120 Moose::Role - Moose's implementation of traits/roles.
121
122 mro and Class::C3 make multiple inheritance work more sensibly.
123
124