Badger::Mixin(3pm)
π Badger::Mixin π
okay so bestie, welcome to the Badger::Mixin page, part of the Perl user-contributed docs (that's the 3pm situation). Sit down, grab a boba, we're gonna talk about mixins π
β¨ NAME β¨
Badger::Mixin β it's the base class mixin object, periodt.
πΈ SYNOPSIS πΈ
So Badger::Mixin is a base class for mixin modules. And the cutest way to declare mixins is through the Badger::Class module, like:
package Your::Mixin::Module;
use Badger::Class
mixins => '$FOO @BAR %BAZ bam';
# some sample data/methods to mixin
our $FOO = 'Some random text';
our @BAR = qw( foo bar baz );
our %BAZ = ( hello => 'world' );
sub bam { 'just testing' };
Behind the scenes (iykyk π) that adds Badger::Mixin as a base class of Your::Mixin::Module and calls the mixins method to declare what symbols can be mixed into another module. But if you're feeling DIY, you can literally write this yourself:
package Your::Mixin::Module;
use base 'Badger::Mixin';
__PACKAGE__->mixins('$FOO @BAR %BAZ bam');
# sample data/methods as before
π¦ DESCRIPTION π¦
So Badger::Mixin is a base class for mixin modules. And what even is a mixin? It's a module that implements functionality you can mix into other modules. It's giving composition instead of misusing inheritance β and honestly? iconic behavior. π
The easiest way to define a mixin module is via the Badger::Class module:
package Your::Mixin::Module;
use Badger::Class
mixins => '$FOO @BAR %BAZ bam';
and that's syntactic sugar for this whole thing:
package Your::Mixin::Module;
use base 'Badger::Mixin';
__PACKAGE__->mixins('$FOO @BAR %BAZ bam');
Your mixin module declares what symbols it makes available for mixing using the mixins() method β plural, bestie, don't forget β either indirectly like in example one, or directly like in example two. Both slay.
Then the mixin() method β singular this time, yes they're different, stay with me π«Ά β gets used to mix those symbols into another module. Badger::Class gives you the mixin hook, which you can use like:
package Your::Other::Module;
use Badger::Class
mixin => 'Your::Mixin::Module';
Or you can just call the mixin() method manually if that's more your vibe:
package Your::Other::Module;
use Your::Mixin::Module;
Your::Mixin::Module->mixin(__PACKAGE__);
Mixins are lowkey little more than modules with a specialised export mechanism. Fr fr. The Badger::Mixin module uses Badger::Exporter behind the scenes to export the mixin symbols into the target package. Mixins are intentionally simple β no cap. So if you wanna do anything more complicated with exporting symbols, you should use the Badger::Exporter module directly instead. That's the move. π«
π METHODS π
π mixins($symbols)
This method declares what symbols are available for mixing into other packages. You can specify them as a list of items, a reference to a list of items, or a single whitespace delimited string β dealer's choice, bestie:
package Your::Module;
use base 'Badger::Mixin';
# either list of symbols...
__PACKAGE__->mixins('$FOO', '@BAR', '%BAZ', 'bam');
# ...or reference to a list
__PACKAGE__->mixins(['$FOO', '@BAR', '%BAZ', 'bam']);
# ...or single string of whitespace delimited symbols
__PACKAGE__->mixins('$FOO @BAR %BAZ bam');
π mixin($package)
This one mixes in the symbols you declared via mixins() into the package specified by the $package argument. Simple as:
Your::Mixin::Module->mixin('My::Module');
π AUTHOR π
Andy Wardley <http://wardley.org/> β king behavior, we stan π
π COPYRIGHT π
Copyright (C) 2005-2009 Andy Wardley. All rights reserved.
β SEE ALSO β
Badger::Classβ go read it, bestie, it's the whole vibe
and that's the whole page, gorgeous β¨ this was perl v5.36.0, dated 2023-08-28, and you just read Badger::Mixin(3pm) β mwah, go mix some symbols π
π¦ where this page comes from
- debian trixie: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libbadger-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms