- Mojolicious is a web development framework for Perl that aims to rethink web development
- It provides a powerful routing system, full HTTP implementation, simple templating, built-in JSON support, elegant plugin system, and class reloader
- Installation is simple using CPAN and has no dependencies beyond Perl 5.8.1
- It includes classes for requests, responses, templates, JSON encoding/decoding, and more
- Plugins can hook into various stages of the request lifecycle
- Supports generating applications, running commands, and provides a simple but powerful way to build web applications and services in Perl
14. With an unconfigured Perl
★ $ curl -L cpanmin.us | perl - Mojolicious
Fetching http://search.cpan.org/CPAN/
authors/id/K/KR/KRAIH/
Mojolicious-0.999922.tar.gz ... OK
Configuring Mojolicious-0.999922 ... OK
Building and testing Mojolicious-0.999922
for Mojolicious ... OK
Successfully installed Mojolicious-0.999922
15. With an unconfigured Perl
★ $ curl -L cpanmin.us | perl - Mojolicious
Fetching http://search.cpan.org/CPAN/
authors/id/K/KR/KRAIH/
Mojolicious-0.999922.tar.gz ... OK
Configuring Mojolicious-0.999922 ... OK
Building and testing Mojolicious-0.999922
for Mojolicious ... OK
Successfully installed Mojolicious-0.999922
★ Gratuitous App::cpanminus plug :)
16. $ mojolicious generate lite_app
[exist] /Users/marcus
[write] /Users/marcus/foosball
[chmod] foosball 744
That was easy. Now let’s check out the contents of
the file
17. Lite app
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => 'index';
get '/:groovy' => sub {
my $self = shift;
$self->render_text(
$self->param('groovy'));
};
shagadelic;
18. Lite app (enterprise edition)
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => 'index';
get '/:synergy' => sub {
my $self = shift;
$self->render_text(
$self->param('synergy'));
};
app->start;
20. Let’s fire her up:
$ ./foosball
usage: foosball COMMAND [OPTIONS]
These commands are currently available:
generate Generate files and directories from templates.
routes Show available routes.
cgi Start application with CGI backend.
daemon Start application with HTTP 1.1 backend.
daemon_prefork Start application with preforking HTTP 1.1 backend.
fastcgi Start application with FastCGI backend.
get Get file from URL.
psgi Start application with PSGI backend.
test Run unit tests.
version Show versions of installed modules.
See 'foosball help COMMAND' for more information on a specific command.
$ ./foosball daemon
Server available at http://Command-Central.local:3000.
24. Features
★ A powerful routes based dispatcher
★ Full HTTP 1/1 implementation (Client/
Server)
25. Features
★ A powerful routes based dispatcher
★ Full HTTP 1/1 implementation (Client/
Server)
★ Simple Template System
26. Features
★ A powerful routes based dispatcher
★ Full HTTP 1/1 implementation (Client/
Server)
★ Simple Template System
★ JSON support built-in
27. Features
★ A powerful routes based dispatcher
★ Full HTTP 1/1 implementation (Client/
Server)
★ Simple Template System
★ JSON support built-in
★ Elegant plugin system
28. Features
★ A powerful routes based dispatcher
★ Full HTTP 1/1 implementation (Client/
Server)
★ Simple Template System
★ JSON support built-in
★ Elegant plugin system
★ Class reloader
29. Features
★ A powerful routes based dispatcher
★ Full HTTP 1/1 implementation (Client/
Server)
★ Simple Template System
★ JSON support built-in
★ Elegant plugin system
★ Class reloader
★ And a lot more
30. Routes - snakes and ladders
# ::Lite
get ‘/’ => ‘index’;
# sub refs for functions
post ‘/login’ => sub { .. };
# Placeholders & Actions:
get ‘/:foo’ => sub {},‘ctrl’
# All together
get '/everything/:stuff' => [stuff => qr/d+/] =>
{stuff => 23} =>
sub { shift->render('welcome'); }
31. Routes - snakes and ladders
ladder sub {
my $self = shift
# Authenticated
my $name = $self->param('name') || '';
return 1 if $name eq 'Bender';
# Not authenticated
$self->render('denied');
return;
}
32. More routes
$r->route(‘/’)->to
(controller=>‘foo’,action=> ‘bar’);
('lists#new', id => 1)->name('new');
('/:controller/:action/:id')->to
('example#welcome', id => 1)
# Bridges
my $auth=$r->bridge->to(‘auth#check’);
$auth->route(...)
33. HTTP 1.1 stack
• HTTP Stack implemented
based on RFCs
• Full HTTP implementation,
including pipelining
• Does not use LWP, No request
body in memory.
36. Mojo::Template
% my $player=$self->stash(‘players’);
%= $player; # print
%== $player; # raw
<%= player %> # inline style
% # Normal comment
<% # inline comment %>
.ep - prepopulates stash for you
.epl - same templates, less magic
42. HTTP Client
my $client=Mojo::Client->new;
$client->get(
‘http://iusethis.com/new.rss’ => sub {
my ($self,$tx)=@_;
say $tx->res;
})->process;
#inside mojolicious
$self->client->post(...);
# also works with the ioloop.