mixin(3pm)

πŸ“¦
version 0.09 Β· 2 versions
, in fedora 43

πŸ’– 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.
  • mro and Class::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