| 1 | package mixin; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | $VERSION = '0.09'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | mixin - Mix-in inheritance, an alternative to multiple inheritance | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | package Dog; | ||||||||||||||||||||||||||||
| 17 | sub speak { print "Bark!\n" } | ||||||||||||||||||||||||||||
| 18 | sub new { my $class = shift; bless {}, $class } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package Dog::Small; | ||||||||||||||||||||||||||||
| 21 | use base 'Dog'; | ||||||||||||||||||||||||||||
| 22 | sub speak { print "Yip!\n"; } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | package Dog::Retriever; | ||||||||||||||||||||||||||||
| 25 | use mixin::with 'Dog'; | ||||||||||||||||||||||||||||
| 26 | sub fetch { print "Get your own stinking $_[1]\n" } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | package Dog::Small::Retriever; | ||||||||||||||||||||||||||||
| 29 | use base 'Dog::Small'; | ||||||||||||||||||||||||||||
| 30 | use mixin 'Dog::Retriever'; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $small_retriever = Dog::Small::Retriever->new; | ||||||||||||||||||||||||||||
| 33 | $small_retriever->speak; # Yip! | ||||||||||||||||||||||||||||
| 34 | $small_retriever->fetch('ball'); # Get your own stinking ball | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | B<NOTE> You probably want to look into the similar but superior | ||||||||||||||||||||||||||||
| 39 | concept of traits/roles instead. See L</"SEE ALSO"> for suggested | ||||||||||||||||||||||||||||
| 40 | modules. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Mixin inheritance is an alternative to the usual multiple-inheritance | ||||||||||||||||||||||||||||
| 43 | and solves the problem of knowing which parent will be called. | ||||||||||||||||||||||||||||
| 44 | It also solves a number of tricky problems like diamond inheritence. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | The idea is to solve the same sets of problems which MI solves without | ||||||||||||||||||||||||||||
| 47 | the problems of MI. For all practical purposes you can think of a | ||||||||||||||||||||||||||||
| 48 | mixin as multiple inheritance without the actual inheritance. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Mixins are a band-aid for the problems of MI. A better solution is to | ||||||||||||||||||||||||||||
| 51 | use traits (called "Roles" in Perl 6), which are like mixins on | ||||||||||||||||||||||||||||
| 52 | steroids. Class::Trait implements this. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head2 Using a mixin class | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | There are two steps to using a mixin-class. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | First, make sure you are inherited from the class with which the | ||||||||||||||||||||||||||||
| 60 | mixin-class is to be mixed. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | package Dog::Small::Retriever; | ||||||||||||||||||||||||||||
| 63 | use base 'Dog::Small'; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Since Dog::Small isa Dog, that does it. Then simply mixin the new | ||||||||||||||||||||||||||||
| 66 | functionality | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | use mixin 'Dog::Retriever'; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | and now you can use fetch(). | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 Writing a mixin class | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | See L<mixin::with>. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 Mixins, Inheritance and SUPER | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | A class which uses a mixin I<does not> inherit from it. However, | ||||||||||||||||||||||||||||
| 81 | through some clever trickery, C<SUPER> continues to work. Here's an | ||||||||||||||||||||||||||||
| 82 | example. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | package Parent; | ||||||||||||||||||||||||||||
| 86 | sub foo { "Parent" } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | package Middle; | ||||||||||||||||||||||||||||
| 91 | use mixin::with "Parent"; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub foo { | ||||||||||||||||||||||||||||
| 94 | my $self = shift; | ||||||||||||||||||||||||||||
| 95 | return $self->SUPER::foo(), "Middle"; | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | package Child; | ||||||||||||||||||||||||||||
| 101 | use base "Parent"; | ||||||||||||||||||||||||||||
| 102 | use mixin "Middle"; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub foo { | ||||||||||||||||||||||||||||
| 105 | my $self = shift; | ||||||||||||||||||||||||||||
| 106 | return $self->SUPER::foo(), "Child"; | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | print join " ", Child->foo; # Parent Middle Child | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | This will print C<Parent Middle Child>. You'll note that this is the | ||||||||||||||||||||||||||||
| 113 | same result if Child inherited from Middle and Middle from Parent. | ||||||||||||||||||||||||||||
| 114 | Its also the same result if Child multiply inherited from Middle and | ||||||||||||||||||||||||||||
| 115 | Parent but I<NOT> if it inherited from Parent then Middle. The | ||||||||||||||||||||||||||||
| 116 | advantage of mixins vs multiple inheritance is such ambiguities do not | ||||||||||||||||||||||||||||
| 117 | exist. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Note that even though both the Child and Middle define foo() the | ||||||||||||||||||||||||||||
| 120 | Middle mixin does not overwrite Child's foo(). A mixin does not | ||||||||||||||||||||||||||||
| 121 | simply export its methods into the mixer and thus does not blow over | ||||||||||||||||||||||||||||
| 122 | existing methods. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 12 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 127 | 12 | my($class, @mixins) = @_; | |||||||||||||||||||||||||||
| 128 | 12 | my $caller = caller; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 12 | foreach my $mixin (@mixins) { | |||||||||||||||||||||||||||
| 131 | # XXX This is lousy, but it will do for now. | ||||||||||||||||||||||||||||
| 132 | 12 | ▶ | unless( defined ${$mixin.'::VERSION'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 12 | eval qq{ require $mixin; }; | |||||||||||||||||||||||||||
| 134 | 12 | ▶ | _croak($@) if $@ and $@ !~ /^Can't locate .*? at /; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$@ and not $@ =~ /^Can't locate .*? at /
MC/DC100%2/2
$@ and not $@ =~ /^Can't locate .*? at /
$@
not $@ =~ /^Can't locate .*? at /
Truth table100%3/3
A and not(B)
A= $@B= not $@ =~ /^Can't locate .*? at /
| |||||||||||||||||||||||||||||
| 135 | 11 | ▶ | unless( %{$mixin."::"} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 1 | _croak(<<ERROR); | |||||||||||||||||||||||||||
| 137 | Mixin class package "$mixin" is empty. | ||||||||||||||||||||||||||||
| 138 | (Perhaps you need to 'use' the module which defines that package first?) | ||||||||||||||||||||||||||||
| 139 | ERROR | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 10 | _mixup($mixin, $caller); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 10 | ▶ | sub _mixup { | ||||||||||||||||||||||||||
|
Subroutine
_mixup: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 147 | 10 | my($mixin, $caller) = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 10 | require mixin::with; | |||||||||||||||||||||||||||
| 150 | 10 | my($with, $pkg) = mixin::with->__mixers($mixin); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 10 | ▶ | _croak("$mixin is not a mixin") unless $with; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 9 | ▶ | _croak("$caller must be a subclass of $with to mixin $mixin") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | unless $caller->isa($with); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # This has to happen here and not in mixin::with because "use | ||||||||||||||||||||||||||||
| 157 | # mixin::with" typically runs *before* the rest of the mixin's | ||||||||||||||||||||||||||||
| 158 | # subroutines are declared. | ||||||||||||||||||||||||||||
| 159 | 8 | _thieve_public_methods( $mixin, $pkg ); | |||||||||||||||||||||||||||
| 160 | 8 | _thieve_isa( $mixin, $pkg, $with ); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 8 | unshift @{$caller.'::ISA'}, $pkg; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 8 | my %Thieved = (); | |||||||||||||||||||||||||||
| 167 | 8 | ▶ | sub _thieve_public_methods { | ||||||||||||||||||||||||||
|
Subroutine
_thieve_public_methods: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 168 | 8 | my($mixin, $pkg) = @_; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 8 | ▶ | return if $Thieved{$mixin}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 7 | local *glob; | |||||||||||||||||||||||||||
| 173 | 7 | ▶ | while( my($sym, $glob) = each %{$mixin.'::'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 28 | ▶ | next if $sym =~ /^_/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 27 | ▶ | next unless defined $glob; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 27 | *glob = *{$mixin.'::'.$sym}; | |||||||||||||||||||||||||||
| 177 | 27 | ▶ | *{$pkg.'::'.$sym} = *glob{CODE} if *glob{CODE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 7 | return 1; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 8 | ▶ | sub _thieve_isa { | ||||||||||||||||||||||||||
|
Subroutine
_thieve_isa: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 184 | 8 | my($mixin, $pkg, $with) = @_; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 8 | @{$pkg.'::ISA'} = grep $_ ne $with, @{$mixin.'::ISA'}; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 8 | return 1; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 4 | ▶ | sub _croak { | ||||||||||||||||||||||||||
|
Subroutine
_croak: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | 4 | require Carp; | |||||||||||||||||||||||||||
| 194 | 4 | goto &Carp::croak; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 NOTES | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | A mixin will not warn if the mixin and the user define the same method. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Current ( ~ 2024 ) maintainer: Shlomi Fish ( L<https://www.shlomifish.org/me/contact-me/> ). | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Original author/maintainer: | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Michael G Schwern E<lt>schwern@pobox.comE<gt> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Copyright 2002-2015 by Michael G Schwern | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | This library is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 216 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | L<http://dev.perl.org/licenses/> | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Shlomi Fish disclaims all copyright ownership to his changes. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | L<Role::Tiny> - A stand alone implementation of traits/roles, like mixins but better. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | L<Moose::Role> - Moose's implementation of traits/roles. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | L<mro> and L<Class::C3> make multiple inheritance work more sensibly. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 8 | 1; | |||||||||||||||||||||||||||