View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Stefan G. > Kelp-0.455 > Kelp::Module



Annotate this POD



Open  0
View/Report Bugs
Source   Latest Release: Kelp-1.02


Kelp::Module - Base class for Kelp modules


    package Kelp::Module::MyModule;
    use parent 'Kelp::Module';

    sub build {
        my ( $self, %args ) = @_;
        $self->register( greet => sub { print "Hi there." } );


Provides the base class for creating Kelp modules. Creating a Kelp module means extending this class and overriding the build method. Kelp modules usually register a new method into the web application.

Registering methods

Modules use the "register" method to register new methods into the underlying web application. All the registrations are done in the "build" subroutine. All types of values can be registered and then accessed as a read-only attribute from the web app. The simplest thing you can register is a scalar value:


    # lib/Kelp/Module/
    package Kelp::Module::Month;
    use Kelp::Base 'Kelp::Module';

    sub build {
        my ( $self, %args ) = @_;
        $self->register( month => 'October' );

Then ...

    # lib/
    package MyApp;
    use parent 'Kelp';

    sub build {

    sub is_it_october_yet {
        my $self = shift;
        if ( $self->month eq 'October' ) {
            return "It is October";
        return "Not yet.";

The above example doesn't do anything meaningful, but it's a good way to show how to create and use Kelp modules. Pay attention to the next example, as it will show you how to register an anonymous subroutine:

    package Kelp::Module::Date;
    use Kelp::Base 'Kelp::Module';
    use DateTime;

    sub build {
        my ( $self, %args ) = @_;
            date => sub {
                return DateTime->from_epoch( epoch => time );

Now, each time you use $self->date in the web application, you will create a new DateTime object for the current time.

It is more practical to register an already created object. Consider this example, which uses Redis, initializes an instance of it and registers it as a method in the web app:

    package Kelp::Module::Redis;
    use Kelp::Base 'Kelp::Module';
    use Redis;

    sub build {
        my ( $self, %args ) = @_;
        my $redis = Redis->new(%args);
        $self->register( redis => $redis );

Passing arguments to your module

The arguments for all modules are taken from the configuration. If you want to pass arguments for your Redis module (example above), you will have to have a structure in your config, similar to this:

Example of conf/myapp.conf:

        # Load module Redis on start
        modules      => ['Redis'],
        modules_init => {
            Redis => {
                server   => '',
                encoding => 'UTF-8',
                password => 'boo'

The hash specified by Redis will be sent as %args in the build method of the module.



build( %args )

Each module must override this one in order to register new methods. The %args hash will be taken from the configuration.


register( %items )

Registers one or many methods into the web application.

        json => JSON->new,
        yaml => YAML->new
syntax highlighting: