mixin::with(3pm)
π 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 π
mixinruby, 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
- 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