1mixin::with(3)        User Contributed Perl Documentation       mixin::with(3)
2
3
4

NAME

6       mixin::with - declaring a mix-in class
7

SYNOPSIS

9           package Dog::Retriever;
10           use mixin::with 'Dog';
11

DESCRIPTION

13       mixin::with is used to declare mix-in classes.
14
15   When to use a mixin?
16       Mixin classes useful for those that add new functionality to an
17       existing class.  If you find yourself doing:
18
19           package Foo::ExtraStuff;
20           use base 'Foo';
21           sub new_method { ... }
22
23           package Bar;
24           use base qw(Foo Foo::ExtraStuff);
25
26       it's a good indication that Foo::ExtraStuff might do better as a mixin.
27
28       Instead of mixins, please consider using traits.  See Class::Trait for
29       an implementaiton.
30
31   How?
32       Basic usage is simple:
33
34           package Foo::Extra;
35           use mixin::with 'Foo';
36
37           sub new_thing {
38               my($self) = shift;
39               ...normal method...
40           }
41
42       "use mixin::with 'Foo'" is similar to subclassing from 'Foo'.
43
44       All public methods of Foo::Extra will be mixed in.  mixin::with
45       considers all methods that don't start with an '_' as public.
46
47   Limitations of mixins
48       There's one critical difference between a normal subclass and one
49       intended to be mixin.  It can have no private methods.  Instead, use
50       lexical methods.
51
52         my $private = sub { ... };
53         $self->$private(@args);
54
55       instead of
56
57         sub _private { ... }
58         $self->_private(@args);
59
60       Don't worry, it's the same thing.
61

FAQ

63       What if I want to mixin with anything?
64           Sometimes a mixin does not care what it mixes in with.  Consider a
65           logging or error handling mixin.  For these, simply mixin with
66           UNIVERSAL.
67
68               package My::Errors;
69               use mixin::with qw(UNIVERSAL);
70
71       Why do I have to declare what I mixin with?
72           Two reasons.  One is technical, it allows "SUPER" to work.
73
74           The other is organizational.  It is rare that a mixin is intended
75           to be mixed with any old class.  It often uses methods as if it
76           were a subclass.  For this reason it is good that it declares this
77           relationship explicitly else the mixee won't be aware of the
78           mixin's expectations.
79
80       Why use mixins instead of traits?
81           Good question.  Traits are definately a better idea then mixins,
82           but mixins have two advantages.  They're simpler to explain, acting
83           like a gateway drug to traits by introducing the concept of OO
84           reuse by class composition rather than inheritance.
85
86           The other is mixins work more like a drop-in replacement for
87           multiple inheritance.  In a large, hairy hierarchy mixins can often
88           be used to trim the inheritance bush and make sense of things with
89           a minimum of modification to the code.  Once this basic repair is
90           done, the work of converting to traits can begin.
91
92           If these advantages don't apply, proceed directly to traits.
93

AUTHOR

95       Michael G Schwern <schwern@pobox.com>
96

LICENSE

98       Copyright 2002-2010 by Michael G Schwern
99
100       This library is free software; you can redistribute it and/or modify it
101       under the same terms as Perl itself.
102
103       <http://dev.perl.org/licenses/>
104

SEE ALSO

106       mixin, ruby from which I stole this idea.
107
108
109
110perl v5.32.1                      2021-01-27                    mixin::with(3)
Impressum