NAME
Mojo::Base - Minimal Object System For Mojo Related Projects
SYNOPSIS
package Car;
use base 'Mojo::Base';
__PACKAGE__->attr('driver');
__PACKAGE__->attr('doors', default => 2);
__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 base class containing a simple and fast object system for Perl objects. Main design goals are minimalism and staying out of your way. The syntax is a bit like Ruby and the performance better than Class::Accessor::Fast.
Note that this is just an accessor generator, look at Moose if you want a more comprehensive object system.
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'});
This class provides a standard object constructor. You can pass arguments to it either as a hash or as a hashref, and they will be set in the object's internal hash reference.
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'}
);
The attr
method generates one or more accessors, depending on the number of arguments, which work as both getters and setters. You can modify the accessor behavior by passing arguments to attr
either as a hash or a hashref.
Currently there are three 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 when the attribute has been
called.
weak: Weakens the attribute value, use to avoid memory leaks with
circular references.