mixin(3pm)
π mixin β the coolest little Perl module you've never met
okay bestie, sit down, grab a boba, because we're talking about mixin: mix-in inheritance, aka the alternative to multiple inheritance. yes, the whole manual page is for the mixin Perl module (that's the 3pm part, iykyk π
). it's giving "I want multiple inheritance but without the drama."
β¨ SYNOPSIS β¨
like, here's the vibe in code, no cap:
package Dog;
sub speak { print "Bark!\n" }
sub new { my $class = shift; bless {}, $class }
package Dog::Small;
use base 'Dog';
sub speak { print "Yip!\n"; }
package Dog::Retriever;
use mixin::with 'Dog';
sub fetch { print "Get your own stinking $_[1]\n" }
package Dog::Small::Retriever;
use base 'Dog::Small';
use mixin 'Dog::Retriever';
my $small_retriever = Dog::Small::Retriever->new;
$small_retriever->speak; # Yip!
$small_retriever->fetch('ball'); # Get your own stinking ball
the way that little dog both speaks and fetches?? main character energy fr fr πΆβ¨
πΈ DESCRIPTION πΈ
NOTE okay so real talk β you probably want to look into the similar but superior concept of traits/roles instead. peek at "SEE ALSO" for the suggested modules. do not skip this, bestie, I'm being so serious. π
Mixin inheritance is an alternative to the usual multiple-inheritance, and it solves that cursed little problem of knowing which parent will be called. it also solves a whole bunch of tricky problems like diamond inheritance. iconic. π
so the idea is: solve the same sets of problems that MI solves, without the problems of MI. for all practical purposes you can think of a mixin as multiple inheritance without the actual inheritance. it's the vibe without the commitment. π¦
ngl, mixins are kind of a band-aid for the problems of MI. a better solution is to use traits (called "Roles" in Perl 6), which are like mixins on steroids. Class::Trait implements this.
π Using a mixin class
there are two steps to using a mixin-class. only two. we love a low-effort queen.
First, make sure you are inherited from the class with which the mixin-class is to be mixed.
package Dog::Small::Retriever;
use base 'Dog::Small';
since Dog::Small isa Dog, that does it. β
Then simply mixin the new functionality:
use mixin 'Dog::Retriever';
and now you can use fetch(). periodt. π
π¦ Writing a mixin class
see mixin::with. (that's it, that's the whole section, she's brief and mysterious and we respect it π«Ά)
π Mixins, Inheritance and SUPER
so a class which uses a mixin does not inherit from it. nope. however β through some clever trickery β "SUPER" continues to work. here's the example, look at her go:
{
package Parent;
sub foo { "Parent" }
}
{
package Middle;
use mixin::with "Parent";
sub foo {
my $self = shift;
return $self->SUPER::foo(), "Middle";
}
}
{
package Child;
use base "Parent";
use mixin "Middle";
sub foo {
my $self = shift;
return $self->SUPER::foo(), "Child";
}
}
print join " ", Child->foo; # Parent Middle Child
this will print "Parent Middle Child". you'll note that this is the same result if Child inherited from Middle and Middle from Parent. it's also the same result if Child multiply inherited from Middle and Parent β but NOT if it inherited from Parent then Middle. π¬
the advantage of mixins vs multiple inheritance is that such ambiguities do not exist. no chaos, only vibes. β¨
note that even though both the Child and Middle define foo(), the Middle mixin does not overwrite Child's foo(). a mixin does not simply export its methods into the mixer and thus does not blow over existing methods. she's polite, she knows her lane. π
π NOTES π
a mixin will not warn if the mixin and the user define the same method. just silently letting you live your truth. (yes this is the entire section, we're keeping it real.)
π AUTHOR π
Current ( ~ 2024 ) maintainer: Shlomi Fish ( <https://www.shlomifish.org/me/contact-me/> ).
Original author/maintainer:
Michael G Schwern <schwern@pobox.com>
π LICENSE π
Copyright 2002-2015 by Michael G Schwern
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
<http://dev.perl.org/licenses/>
Shlomi Fish disclaims all copyright ownership to his changes.
π«Ά SEE ALSO π«Ά
Role::Tinyβ a stand alone implementation of traits/roles, like mixins but better. bestie upgrade path fr.Moose::Roleβ Moose's implementation of traits/roles.mroandClass::C3β they make multiple inheritance work more sensibly. π
and that's the whole page, bestie!! this one's from perl v5.42.0, dated 2025-07-25, and it's the mixin(3pm) page from the User Contributed Perl Documentation. go forth and mix in responsibly. πβ¨ππ¦πΈπ
π«Ά
π¦ where this page comes from
- fedora 43: perl-mixin 0.09-3.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms