Kelp::Base - Simple lazy attributes
use Kelp::Base; attr source => 'dbi:mysql:users'; attr user => 'test'; attr pass => 'secret'; attr opts => { PrintError => 1, RaiseError => 1 }; attr dbh => sub { my $self = shift; DBI->connect( $self->sourse, $self->user, $self->pass, $self->opts ); }; # Later ... sub do_stuff { my $self = shift; $self->dbh->do('DELETE FROM accounts'); }
or
use Kelp::Base 'Module::Name'; # Extend Module::Name
use Kelp::Base -strict; # Only use strict, warnings and v5.10 # No magic
This module provides simple lazy attributes.
Some users will naturally want to ask "Why not use Moose/Mouse/Moo/Mo?". The answer is that the Kelp web framework needs lazy attributes, but the author wanted to keep the code light and object manager agnostic. This allows the users of the framework to choose an object manager to their liking. There is nothing more annoying than a module that forces you to use Moose when you are perfectly fine with Moo or Mo, for example.
use Kelp::Base;
The above will automatically include strict, warnings and v5.10. It will also inject a new sub in the current class called attr.
strict
warnings
v5.10
attr
attr name1 => 1; # Fixed value attr name2 => sub { [ 1, 2, 3 ] }; # Array attr name3 => sub { $_[0]->other; } ... say $self->name1; # 1 $self->name2( [ 6, 7, 8 ] ); # Set new value
All those attributes will be available for reading and writing in each instance of the current class. If you want to create a read-only attribute, prefix its name with a dash.
attr -readonly => "something"; # Later say $self->readonly; # something $self->readonly("nothing"); # no change
Kelp, Moose, Moo, Mo, Any::Moose
To install Kelp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Kelp
CPAN shell
perl -MCPAN -e shell install Kelp
For more information on module installation, please visit the detailed CPAN module installation guide.