Mars::Role - Role Declaration
Role Declaration for Perl 5
package Person; use Mars::Class 'attr'; attr 'fname'; attr 'lname'; package Identity; use Mars::Role 'attr'; attr 'id'; attr 'login'; attr 'password'; sub EXPORT { # explicitly declare routines to be consumed ['id', 'login', 'password'] } package Authenticable; use Mars::Role; 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; use Mars::Class; base 'Person'; with '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')
This package provides a role builder which when used causes the consumer to inherit from Mars::Kind::Role which provides role construction and lifecycle hooks. A role differs from a "class" in that it can't be instantiated using the new method. A role can act as an interface by defining an "audit" hook, which is invoked automatically by the "test" function.
new
This package provides the following functions:
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
0.01
package Example; use Mars::Class; attr 'name'; # "Example"
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.
package Entity; use Mars::Class; sub output { return; } package Example; use Mars::Class; base 'Entity'; # "Example"
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.
0
package Example; use Mars::Class; my $false = false; # 0
package Example; use Mars::Class; my $true = !false; # 1
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
0.03
package Entity; use Mars::Role; attr 'startup'; attr 'shutdown'; sub EXPORT { ['startup', 'shutdown'] } package Record; use Mars::Class; sub AUDIT { my ($self, $from) = @_; die "Missing startup" if !$from->can('startup'); die "Missing shutdown" if !$from->can('shutdown'); } package Example; use Mars::Class; with 'Entity'; from 'Record'; # "Example"
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
0.05
package YesNo; use Mars::Mixin; sub no { return 0; } sub yes { return 1; } sub EXPORT { ['no', 'yes'] } package Example; use Mars::Class; mixin 'YesNo'; # "Example"
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.
package Ability; use Mars::Role; sub action { return; } package Example; use Mars::Class; role 'Ability'; # "Example"
package Ability; use Mars::Role; sub action { return; } sub EXPORT { return ['action']; } package Example; use Mars::Class; role 'Ability'; # "Example"
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.
package Actual; use Mars::Role; package Example; use Mars::Class; test 'Actual'; # "Example"
package Actual; use Mars::Role; sub AUDIT { die "Example is not an 'actual' thing" if $_[1]->isa('Example'); } package Example; use Mars::Class; test 'Actual'; # "Example"
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.
1
package Example; use Mars::Class; my $true = true; # 1
package Example; use Mars::Class; my $false = !true; # 0
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.
package Understanding; use Mars::Role; sub knowledge { return; } package Example; use Mars::Class; with 'Understanding'; # "Example"
package Understanding; use Mars::Role; sub knowledge { return; } sub EXPORT { return ['knowledge']; } package Example; use Mars::Class; with 'Understanding'; # "Example"
Awncorp, awncorp@cpan.org
awncorp@cpan.org
To install Mars, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mars
CPAN shell
perl -MCPAN -e shell install Mars
For more information on module installation, please visit the detailed CPAN module installation guide.