Sebastian Riedel > Mojo > Mojo::Base

Download:
Mojo-0.991236.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Source  

NAME ^

Mojo::Base - Minimal Base Class For Mojo Projects

SYNOPSIS ^

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

DESCRIPTION ^

Mojo::Base is a minimalistic base class for Mojo projects. 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 => 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.