NAME
Mojo::Base - Once Upon A Midnight Dreary!
SYNOPSIS
package Car;
use base 'Mojo::Base';
__PACKAGE__->attr('driver');
__PACKAGE__->attr('doors',
default => 2,
filter => sub { s/\D//g; $_ }
);
__PACKAGE__->attr([qw/passengers seats/],
chained => 1,
default => sub { 2 }
);
__PACKAGE__->attr('trailer', weak => 1);
package main;
use Car;
my $bmw = Car->new;
print $bmw->doors;
print $bmw->passengers(5)->doors;
my $mercedes = Car->new(driver => 'Sebastian');
print $mercedes->passengers(7)->passengers;
$mercedes->trailer(Trailer->new);
DESCRIPTION
Mojo::Base is a universal base class containing simple and fast helpers for object oriented Perl programming.
Main design goals are minimalism and staying out of your way.
The syntax is a bit like Moose or Ruby and the performance better than Class::Accessor::Fast. (Note that Mojo::Base was never meant as a replacement for Moose, both are solutions to completely different problems.)
For debugging you can set the MOJO_BASE_DEBUG
environment variable.
METHODS
new
my $instance = BaseSubClass->new;
my $instance = BaseSubClass->new(name => 'value');
my $instance = BaseSubClass->new({name => 'value'});
attr
__PACKAGE__->attr('name');
__PACKAGE__->attr([qw/name1 name2 name3/]);
__PACKAGE__->attr('name', chained => 1, default => 'foo');
__PACKAGE__->attr(name => (chained => 1, default => 'foo'));
__PACKAGE__->attr('name', {chained => 1, default => 'foo'});
__PACKAGE__->attr([qw/name1 name2 name3/] => {
chained => 1,
default => 'foo'}
);
Currently there are four options supported.
chained: Whenever you call an attribute with arguments the instance
is returned instead of the value.
default: Default value for the attribute, can also be a coderef.
Note that the default value is "lazy", which means it only
gets assigned to the instance after the attribute has been
called.
filter: Filters the value before assigning it to the instance,
must be a coderef.
weak: Weakens the attribute value.