Badger::Mixin
The Badger::Mixin
module is a base class for mixin modules.
You can use the Badger::Class module to declare mixins:
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 this 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. You can write this code manually if you
prefer:
package Your::Mixin::Module; use base 'Badger::Mixin'; __PACKAGE__->mixins('$FOO @BAR %BAZ bam'); # sample data/methods as before
The Badger::Mixin module is a base class for mixin modules. Mixins are modules that implement functionality that can be mixed into other modules. This allows you to create modules using composition instead of misuing inheritance.
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';
This is syntactic sugar for the following code:
package Your::Mixin::Module; use base 'Badger::Mixin'; __PACKAGE__->mixins('$FOO @BAR %BAZ bam');
The mixin module declares what symbols it makes available for mixing using the mixins() (plural) method (either indirectly as in the first example, or directly as in the second).
The mixin() (singular) method can then be used to mix those symbols into another module. Badger::Class provides the mixin hook which you can use:
package Your::Other::Module; use Badger::Class mixin => 'Your::Mixin::Module';
Or you can call the mixin() method manually if you prefer.
package Your::Other::Module; use Your::Mixin::Module; Your::Mixin::Module->mixin(__PACKAGE__);
Mixins are little more than modules with a specialised export mechanism.
In fact, the Badger::Mixin
module uses the Badger::Exporter behind the scenes
to export the mixin symbols into the target package. Mixins are
intentionally simple. If you want to do anything more complicated in
terms of exporting symbols then you should use the Badger::Exporter module directly
instead.
This method is used to declare what symbols are available for mixing in to other packages. Symbols can be specified as a list of items, a reference to a list of items or as a single whitespace delimited string.
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');
Andy Wardley http://wardley.org/