Badger
view release on metacpan or search on metacpan
lib/Badger/Mixin.pm view on Meta::CPAN
#========================================================================
#
# Badger::Mixin
#
# DESCRIPTION
# Base class for mixins that allow you to "mix in" functionality using
# composition rather than inheritance. Similar in concept to roles,
# although operating at a slightly lower level.
#
# AUTHOR
# Andy Wardley <abw@wardley.org>
#
#========================================================================
package Badger::Mixin;
use Badger::Class
version => 3.00,
debug => 0,
base => 'Badger::Exporter',
import => 'class',
constants => 'PKG REFS ONCE ARRAY DELIMITER',
words => 'EXPORT_TAGS MIXINS';
sub mixin {
my $self = shift;
my $target = shift || (caller())[0];
my $class = $self->class;
my $mixins = $class->list_vars(MIXINS);
$self->debug("mixinto($target): ", $self->dump_data($mixins), "\n") if $DEBUG;
$self->export($target, $mixins);
}
sub mixins {
my $self = shift;
my $syms = @_ == 1 ? shift : [ @_ ];
my $class = $self->class;
my $mixins = $class->var_default(MIXINS, [ ]);
$syms = [ split(DELIMITER, $syms) ]
unless ref $syms eq ARRAY;
push(@$mixins, @$syms);
$self->export_any($syms);
return $mixins;
}
1;
=head1 NAME
Badger::Mixin - base class mixin object
=head1 SYNOPSIS
The C<Badger::Mixin> module is a base class for mixin modules.
You can use the L<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 C<Badger::Mixin> as a base class of
C<Your::Mixin::Module> and calls the L<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
=head1 DESCRIPTION
The L<Badger::Mixin> module is a base class for mixin modules. Mixins are
( run in 2.240 seconds using v1.01-cache-2.11-cpan-5b529ec07f3 )