Mojo::Base - Minimal Object System For Mojo Related Projects
package Car; use base 'Mojo::Base'; __PACKAGE__->attr('driver'); __PACKAGE__->attr('doors', default => 2); __PACKAGE__->attr([qw/passengers seats/], chained => 0, 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);
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.
MOJO_BASE_DEBUG
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 => 0, default => 'foo'); __PACKAGE__->attr(name => (chained => 0, default => 'foo')); __PACKAGE__->attr('name', {chained => 0, default => 'foo'}); __PACKAGE__->attr([qw/name1 name2 name3/] => { chained => 0, 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. (This will be activated by default and can be deactivated by setting chained to false) default: Default value for the attribute, can be a coderef or constant value. (Not a normal reference!) 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.
To install Mojo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo
CPAN shell
perl -MCPAN -e shell install Mojo
For more information on module installation, please visit the detailed CPAN module installation guide.