NAME
Venus::Class - Class Builder
ABSTRACT
Class Builder for Perl 5
SYNOPSIS
package Person;
use Venus::Class 'attr';
attr 'fname';
attr 'lname';
package Identity;
use Venus::Role 'attr';
attr 'id';
attr 'login';
attr 'password';
sub EXPORT {
# explicitly declare routines to be consumed
['id', 'login', 'password']
}
package Authenticable;
use Venus::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 Venus::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')
DESCRIPTION
This package provides a class builder which when used causes the consumer to inherit from Venus::Core::Class which provides object construction and lifecycle hooks.
METHODS
This package provides the following methods:
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.10
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.10
- base example 1
-
package Entity; use Venus::Class; sub output { return; } package Example; use Venus::Class; 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.10
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.10
- from example 1
-
package Entity; use Venus::Class; sub AUDIT { my ($self, $from) = @_; die "Missing startup" if !$from->can('startup'); die "Missing shutdown" if !$from->can('shutdown'); } package Example; use Venus::Class; attr 'startup'; attr 'shutdown'; from 'Entity'; # "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.10
- role example 1
-
package Ability; use Venus::Role; sub action { return; } package Example; use Venus::Class; role 'Ability'; # "Example"
- role example 2
-
package Ability; use Venus::Role; sub action { return; } sub EXPORT { return ['action']; } package Example; use Venus::Class; 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.10
- test example 1
-
package Actual; use Venus::Role; package Example; use Venus::Class; test 'Actual'; # "Example"
- test example 2
-
package Actual; use Venus::Role; sub AUDIT { die "Example is not an 'actual' thing" if $_[1]->isa('Example'); } package Example; use Venus::Class; 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.10
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.10