The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Cot - super lightweight perl framework based on Plack

SYNOPSIS

#!/usr/bin/env perl -w

use Cot;

get '/' => sub {
    my $self = shift;
    $self->res->status(200);
    $self->res->headers( { 'Content-Type' => 'text/plain', } );
    $self->res->body('Hello world!');
};

run;

The above is a basic but functional web app created with Cot.

DESCRIPTION

Cot is super lightweight perl framework base on Plack! you can extend Cot by using many plugins.

You can initialize application skelton using cotto utility executable.

% cot init Test
% cd ./Test
% cot run

METHODS

plugin

Added plugins to Cot App.

use Cot;

plugin qw/Config Session/;

get

Receive GET request:

use Cot;

get '/api/echo' => sub {
    my $self = shift;
    #code
};

post

Receive POST request:

use Cot;

post '/api/echo' => sub {
    my $self = shift;
    #code
};

put

Receive PUT request:

use Cot;

put '/api/echo' => sub {
    my $self = shift;
    #code
};

delete

Receive DELETE request:

use Cot;

delete '/api/echo' => sub {
    my $self = shift;
    #code
};

options

Receive OPTIONS request:

use Cot;

options '/api/echo' => sub {
    my $self = shift;
    #code
};

patch

Receive PATCH request:

use Cot;

patch '/api/echo' => sub {
    my $self = shift;
    #code
};

any

Receive any HTTP request:

use Cot;

any '/api/echo' => sub {
    my $self = shift;
    #code
};

static

Serve static files:

use Cot;

static => '/ui';

run

Up the Plack execution loop. You can set plackup arguments.

use Cot;

run("-- port 5001 -R public");

forbidden_response

use Cot;

get '/secret' => sub {
    $self->res->status(403);
    $self->res->body('forbidden');
};

same as below.

use Cot;

get '/secret' => sub {
    $self->forbidden_response;
};

notfound_response

use Cot;

get '/secret' => sub {
    $self->res->status(404);
    $self->res->body('notfound');
};

same as below.

use Cot;

get '/secret' => sub {
    $self->notfound_response;
};

redirect_response

use Cot;

get '/secret' => sub {
    $self->res->redirect('/', 301);
};

same as below.

use Cot;

get '/secret' => sub {
    $self->redirect_response;
};

Context METHODS

req

req is a Plack::Request Object. You can call all methods of Plack::Request.

use Cot;

get '/test' => sub {
    my $self = shift;
    my $req = $self->req;
    my $test = $req->param('test');
};

res

res is a Plack::Response Object. You can call all methods of Plack::Response.

use Cot;

get '/test' => sub {
    my $self = shift;
    $self->res->status(200);
    $self->res->headers({'Content-Type' => 'text/plain' });
    $self->res->body($self->config->{sample});
};

path_info

path_info is PATH_INFO Array ref object.

use Cot;

# if called /test/hello
get '/test' => sub {
    my $self = shift;
    my $info = $self->path_info->[0]; #hello
    ...
};

env

env is Plack environment variable.

use Cot;

get '/test' => sub {
    my $self = shift;
    my $remote_addr = $self->env->{REMOTE_ADDR}; # same as $self->req->address
    ...
};

path

path is requested PATH string

use Cot;

# if called /test/hello/myname
get '/test' => sub {
    my $self = shift;
    my $path = $self->path; # /test/hello/myname
    ...
};

ENV

You can set ENVIRONMENT variables for change behaviour.

COT_ROOT

Default value is " B <.>". For example mod_perl configuration, you can set

PerlSetEnv COT_ROOT /www/TestApp/

COT_ENV

Default value is developement. You can change COT_ENV for configration.

#!/bin/sh
export COT_ENV=production
cot run

COT_DIRECTORYINDEX

Default value is none. If you use static method, automatically serve DIRECTORYINDEX

#!/bin/sh
export DIRECTORYINDEX=index.html:index.xhtml
cot run

PLUGINS

Cot has plaggable interface. For default install only Cot::Plugin::Config can be used.

config file($ENV{COT_ROOT}/config.yaml):

developement:
  hello: world

application code:

use Cot;
use Cot::Plugin qw/Config/;
# or use Cot::Plugin::Config;

get '/' => sub {
   my $self = shift;
   my $hello = $c->config->{'hello'}; # world
   ...
};

AUTHORS

This module has been written by Yusuke Shibata shibata@yusukeshibata.jp and others, see the AUTHORS file that comes with this distribution for details.

SOURCE CODE

The source code for this module is hosted on GitHub https://github.com/yusukeshibata/Cot. Feel free to fork the repository and submit pull requests!

DEPENDENCIES

The following modules are mandatory (Cot cannot run without them):

LICENSE

Copyright (C) Yusuke Shibata

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Yusuke Shibata shibata@yusukeshibata.jp