Sponsoring The Perl Toolchain Summit 2025: Help make this important event another success Learn more

NAME

Mars::Mixin - Mixin Declaration

ABSTRACT

Mixin Declaration for Perl 5

SYNOPSIS

package Person;
use Mars::Class 'attr';
attr 'fname';
attr 'lname';
package Identity;
use Mars::Mixin 'attr';
attr 'id';
attr 'login';
attr 'password';
sub EXPORT {
# explicitly declare routines to be consumed
['id', 'login', 'password']
}
package Authenticable;
sub authenticate {
return true;
}
sub AUDIT {
my ($self, $from) = @_;
# ensure the caller has a login and password when consumed
die "${from} missing the login attribute" if !$from->can('login');
die "${from} missing the password attribute" if !$from->can('password');
}
sub BUILD {
my ($self, $data) = @_;
$self->{auth} = undef;
return $self;
}
sub EXPORT {
# explicitly declare routines to be consumed
['authenticate']
}
package User;
base 'Person';
mixin 'Identity';
attr 'email';
test 'Authenticable';
sub valid {
my ($self) = @_;
return $self->login && $self->password ? true : false;
}
package main;
my $user = User->new(
fname => 'Elliot',
lname => 'Alderson',
);
# bless({fname => 'Elliot', lname => 'Alderson'}, 'User')

DESCRIPTION

This package provides a mixin builder which when used causes the consumer to inherit from Mars::Kind::Mixin which provides mixin building and lifecycle hooks. A mixin can do almost everything that a role can do but differs from a "role" in that whatever routines are declared using "export" will be exported and will overwrite routines of the same name in the consumer.

FUNCTIONS

This package provides the following functions:

attr

attr(Str $name) (Str)

The attr function creates attribute accessors for the calling package. This function is always exported unless a routine of the same name already exists.

Since 0.01

attr example 1
package Example;
attr 'name';
# "Example"

base

base(Str $name) (Str)

The base function registers one or more base classes for the calling package. This function is always exported unless a routine of the same name already exists.

Since 0.01

base example 1
package Entity;
sub output {
return;
}
package Example;
base 'Entity';
# "Example"

false

false() (Bool)

The false function returns a falsy boolean value which is designed to be practically indistinguishable from the conventional numerical 0 value. This function is always exported unless a routine of the same name already exists.

Since 0.01

false example 1
package Example;
my $false = false;
# 0
false example 2
package Example;
my $true = !false;
# 1

from

from(Str $name) (Str)

The from function registers one or more base classes for the calling package and performs an "audit". This function is always exported unless a routine of the same name already exists.

Since 0.03

from example 1
package Entity;
attr 'startup';
attr 'shutdown';
sub EXPORT {
['startup', 'shutdown']
}
package Record;
sub AUDIT {
my ($self, $from) = @_;
die "Missing startup" if !$from->can('startup');
die "Missing shutdown" if !$from->can('shutdown');
}
package Example;
with 'Entity';
from 'Record';
# "Example"

mixin

mixin(Str $name) (Str)

The mixin function registers and consumes mixins for the calling package. This function is always exported unless a routine of the same name already exists.

Since 0.05

mixin example 1
package YesNo;
sub no {
return 0;
}
sub yes {
return 1;
}
sub EXPORT {
['no', 'yes']
}
package Example;
mixin 'YesNo';
# "Example"

role

role(Str $name) (Str)

The role function registers and consumes roles for the calling package. This function is always exported unless a routine of the same name already exists.

Since 0.01

role example 1
package Ability;
sub action {
return;
}
package Example;
role 'Ability';
# "Example"
role example 2
package Ability;
sub action {
return;
}
sub EXPORT {
return ['action'];
}
package Example;
role 'Ability';
# "Example"

test

test(Str $name) (Str)

The test function registers and consumes roles for the calling package and performs an "audit", effectively allowing a role to act as an interface. This function is always exported unless a routine of the same name already exists.

Since 0.01

test example 1
package Actual;
package Example;
test 'Actual';
# "Example"
test example 2
package Actual;
sub AUDIT {
die "Example is not an 'actual' thing" if $_[1]->isa('Example');
}
package Example;
test 'Actual';
# "Example"

true

true() (Bool)

The true function returns a truthy boolean value which is designed to be practically indistinguishable from the conventional numerical 1 value. This function is always exported unless a routine of the same name already exists.

Since 0.01

true example 1
package Example;
my $true = true;
# 1
true example 2
package Example;
my $false = !true;
# 0

with

with(Str $name) (Str)

The with function registers and consumes roles for the calling package. This function is an alias of the "test" function and will perform an "audit" if present. This function is always exported unless a routine of the same name already exists.

Since 0.01

with example 1
package Understanding;
sub knowledge {
return;
}
package Example;
# "Example"
with example 2
package Understanding;
sub knowledge {
return;
}
sub EXPORT {
return ['knowledge'];
}
package Example;
# "Example"

AUTHORS

Awncorp, awncorp@cpan.org