Cot - super lightweight perl framework based on Plack
#!/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.
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
Added plugins to Cot App.
use Cot; plugin qw/Config Session/;
Receive GET request:
use Cot; get '/api/echo' => sub { my $self = shift; #code };
Receive POST request:
use Cot; post '/api/echo' => sub { my $self = shift; #code };
Receive PUT request:
use Cot; put '/api/echo' => sub { my $self = shift; #code };
Receive DELETE request:
use Cot; delete '/api/echo' => sub { my $self = shift; #code };
Receive OPTIONS request:
use Cot; options '/api/echo' => sub { my $self = shift; #code };
Receive PATCH request:
use Cot; patch '/api/echo' => sub { my $self = shift; #code };
Receive any HTTP request:
use Cot; any '/api/echo' => sub { my $self = shift; #code };
Serve static files:
use Cot; static => '/ui';
Up the Plack execution loop. You can set plackup arguments.
use Cot; run("-- port 5001 -R public");
use Cot; get '/secret' => sub { $self->res->status(403); $self->res->body('forbidden'); };
same as below.
use Cot; get '/secret' => sub { $self->forbidden_response; };
use Cot; get '/secret' => sub { $self->res->status(404); $self->res->body('notfound'); };
use Cot; get '/secret' => sub { $self->notfound_response; };
use Cot; get '/secret' => sub { $self->res->redirect('/', 301); };
use Cot; get '/secret' => sub { $self->redirect_response; };
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 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 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 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 is requested PATH string
use Cot; # if called /test/hello/myname get '/test' => sub { my $self = shift; my $path = $self->path; # /test/hello/myname ... };
You can set ENVIRONMENT variables for change behaviour.
Default value is " B <.>". For example mod_perl configuration, you can set
PerlSetEnv COT_ROOT /www/TestApp/
Default value is developement. You can change COT_ENV for configration.
#!/bin/sh export COT_ENV=production cot run
Default value is none. If you use static method, automatically serve DIRECTORYINDEX
#!/bin/sh export DIRECTORYINDEX=index.html:index.xhtml cot run
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 ... };
This module has been written by Yusuke Shibata <shibata@yusukeshibata.jp> and others, see the AUTHORS file that comes with this distribution for details.
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!
The following modules are mandatory (Cot cannot run without them):
Copyright (C) Yusuke Shibata
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Yusuke Shibata <shibata@yusukeshibata.jp>
To install Cot, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Cot
CPAN shell
perl -MCPAN -e shell install Cot
For more information on module installation, please visit the detailed CPAN module installation guide.