Badger::Mixin(3pm)

πŸ“¦
version 0.16 Β· 4 versions
, in debian trixie, ubuntu noble, ubuntu resolute

πŸ’– 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 (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