mixin::with(3pm)

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

πŸ’– mixin::with β€” declaring a mix-in class πŸ’–

okay so bestie, welcome to the mixin::with page πŸ’… this one lives in the User Contributed Perl Documentation corner of your Perl 5 manual pages, and it's about a tiny little pragma with a huge personality. it lets you declare a mix-in class. that's it. that's the whole vibe. let's go πŸ¦‹


✨ SYNOPSIS ✨

package Dog::Retriever;
use mixin::with 'Dog';

like. that's the whole thing. four lines. iconic behavior honestly πŸŽ€


🌸 DESCRIPTION 🌸

mixin::with is what you use to declare mix-in classes. periodt.

πŸŽ€ When to use a mixin?

mix-in classes are the move when you wanna add new functionality to a class that already exists 🫢 if you catch yourself writing something like this:

package Foo::ExtraStuff;
use base 'Foo';
sub new_method { ... }

package Bar;
use base qw(Foo Foo::ExtraStuff);

then ngl? that's a pretty solid sign that Foo::ExtraStuff would lowkey do way better as a mixin πŸ’…

but hey β€” instead of mixins, please consider using traits. check out Class::Trait for an implementation. iykyk ✨

πŸ¦‹ How?

basic usage is, and i cannot stress this enough, simple:

package Foo::Extra;
use mixin::with 'Foo';

sub new_thing {
    my($self) = shift;
    ...normal method...
}

use mixin::with 'Foo' is similar to subclassing from 'Foo' 🐢

all public methods of Foo::Extra will be mixed in. and what counts as public, you ask? mixin::with considers all methods that don't start with an '_' as public. so the underscore is basically the "do not perceive me" prefix πŸ™ˆ

πŸ’… Limitations of mixins

here's the one critical difference between a normal subclass and one that's meant to be a mixin: it can have no private methods. 😭 none. zero. instead, you use lexical methods:

my $private = sub { ... };
$self->$private(@args);

instead of

sub _private { ... }
$self->_private(@args);

don't worry though, it's the same thing. fr fr 🫢


πŸ”₯ FAQ πŸ”₯

⭐ What if I want to mixin with anything?

sometimes a mixin genuinely does not care what it mixes in with. think a logging mixin, or an error handling mixin. for those, you simply mix in with UNIVERSAL:

package My::Errors;
use mixin::with qw(UNIVERSAL);

main character energy, mixing in with literally everything πŸ’€βœ¨

πŸ’œ Why do I have to declare what I mixin with?

two reasons, bestie. one is technical: it allows "SUPER" to work πŸ‘‘

the other is organizational. it's rare that a mixin is intended to be mixed with any old class. it often uses methods as if it were a subclass. so it's a good thing that it declares this relationship explicitly β€” otherwise the mixee won't be aware of the mixin's expectations. communication is key, we love a healthy relationship 🫢

🌈 Why use mixins instead of traits?

good question!! traits are definitely a better idea than mixins, no cap. but mixins have two advantages:

  • they're simpler to explain. they act like a gateway drug to traits, introducing the concept of OO reuse by class composition rather than inheritance 🌱
  • mixins work more like a drop-in replacement for multiple inheritance. in a large, hairy hierarchy, mixins can often be used to trim the inheritance bush and make sense of things with a minimum of modification to the code. once this basic repair is done, the work of converting to traits can begin 🧚

if these advantages don't apply to your situation? proceed directly to traits. don't pass go. don't collect $200 πŸ’…


πŸ‘‘ 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-2010 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 🌈

  • mixin
  • ruby, from which I stole this idea πŸ“

and that's the tea, bestie πŸŽ€ this page is from perl v5.42.0, dated 2025-07-25. go forth and mix in responsibly πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from