Venus::Class

Class Builder

Class Builder for Perl 5

function: attr function: base function: catch function: error function: false function: from function: mixin function: raise function: role function: test function: true function: with

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')

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.

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

attr(string $name) (string)

{ since => '1.00', }

=example-1 attr

package Example;

use Venus::Class;

attr 'name';

# "Example"

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.

base(string $name) (string)

{ since => '1.00', }

=example-1 base

package Entity;

use Venus::Class;

sub output {
  return;
}

package Example;

use Venus::Class;

base 'Entity';

# "Example"

The catch function executes the code block trapping errors and returning the caught exception in scalar context, and also returning the result as a second argument in list context. This function isn't export unless requested.

catch(coderef $block) (Venus::Error, any)

{ since => '1.01', }

=example-1 catch

package Example;

use Venus::Class 'catch';

sub attempt {
  catch {die};
}

package main;

my $example = Example->new;

my $error = $example->attempt;

$error;

# "Died at ..."

The error function throws a Venus::Error exception object using the exception object arguments provided. This function isn't export unless requested.

error(maybe[hashref] $args) (Venus::Error)

{ since => '1.01', }

=example-1 error

package Example;

use Venus::Class 'error';

sub attempt {
  error;
}

package main;

my $example = Example->new;

my $error = $example->attempt;

# bless({...}, 'Venus::Error')

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.

false() (boolean)

{ since => '1.00', }

=example-1 false

package Example;

use Venus::Class;

my $false = false;

# 0

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.

from(string $name) (string)

{ since => '1.00', }

=example-1 from

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"

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.

mixin(string $name) (string)

{ since => '1.02', }

=example-1 mixin

package YesNo;

use Venus::Mixin;

sub no {
  return 0;
}

sub yes {
  return 1;
}

sub EXPORT {
  ['no', 'yes']
}

package Answer;

use Venus::Class;

mixin 'YesNo';

# "Answer"

The raise function generates and throws a named exception object derived from Venus::Error, or provided base class, using the exception object arguments provided. This function isn't export unless requested.

raise(string $class | tuple[string, string] $class, maybe[hashref] $args) (Venus::Error)

{ since => '1.01', }

=example-1 raise

package Example;

use Venus::Class 'raise';

sub attempt {
  raise 'Example::Error';
}

package main;

my $example = Example->new;

my $error = $example->attempt;

# bless({...}, 'Example::Error')

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.

role(string $name) (string)

{ since => '1.00', }

=example-1 role

package Ability;

use Venus::Role;

sub action {
  return;
}

package Example;

use Venus::Class;

role 'Ability';

# "Example"

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.

test(string $name) (string)

{ since => '1.00', }

=example-1 test

package Actual;

use Venus::Role;

package Example;

use Venus::Class;

test 'Actual';

# "Example"

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.

true() (boolean)

{ since => '1.00', }

=example-1 true

package Example;

use Venus::Class;

my $true = true;

# 1

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.

with(string $name) (string)

{ since => '1.00', }

=example-1 with

package Understanding;

use Venus::Role;

sub knowledge {
  return;
}

package Example;

use Venus::Class;

with 'Understanding';

# "Example"

t/Venus.t: present: authors t/Venus.t: present: license

48 POD Errors

The following errors were encountered while parsing the POD:

Around line 13:

Unknown directive: =name

Around line 21:

Unknown directive: =tagline

Around line 29:

Unknown directive: =abstract

Around line 37:

Unknown directive: =includes

Around line 56:

Unknown directive: =synopsis

Around line 155:

Unknown directive: =description

Around line 165:

Unknown directive: =function

Around line 170:

Unknown directive: =signature

Around line 174:

Unknown directive: =metadata

Around line 206:

Unknown directive: =function

Around line 212:

Unknown directive: =signature

Around line 216:

Unknown directive: =metadata

Around line 253:

Unknown directive: =function

Around line 259:

Unknown directive: =signature

Around line 263:

Unknown directive: =metadata

Around line 299:

Unknown directive: =function

Around line 304:

Unknown directive: =signature

Around line 308:

Unknown directive: =metadata

Around line 344:

Unknown directive: =function

Around line 350:

Unknown directive: =signature

Around line 354:

Unknown directive: =metadata

Around line 380:

Unknown directive: =function

Around line 386:

Unknown directive: =signature

Around line 390:

Unknown directive: =metadata

Around line 431:

Unknown directive: =function

Around line 436:

Unknown directive: =signature

Around line 440:

Unknown directive: =metadata

Around line 522:

=cut found outside a pod block. Skipping to next block.

Around line 535:

Unknown directive: =function

Around line 541:

Unknown directive: =signature

Around line 545:

Unknown directive: =metadata

Around line 582:

Unknown directive: =function

Around line 587:

Unknown directive: =signature

Around line 591:

Unknown directive: =metadata

Around line 648:

=cut found outside a pod block. Skipping to next block.

Around line 659:

Unknown directive: =function

Around line 666:

Unknown directive: =signature

Around line 670:

Unknown directive: =metadata

Around line 719:

=cut found outside a pod block. Skipping to next block.

Around line 729:

Unknown directive: =function

Around line 735:

Unknown directive: =signature

Around line 739:

Unknown directive: =metadata

Around line 775:

=cut found outside a pod block. Skipping to next block.

Around line 785:

Unknown directive: =function

Around line 792:

Unknown directive: =signature

Around line 796:

Unknown directive: =metadata

Around line 853:

=cut found outside a pod block. Skipping to next block.

Around line 865:

Unknown directive: =partials