mojolicious
real-time web framework




NPW2012, Stockholm        Lars Thegler
meta

I’m not a mojolicious expert
I will talk about how to start
I will talk about what to expect
I will hint at how to continue
agenda

Mojolicious::Lite
  routes and placeholders
  templates and layouts
  graduating from ::Lite to Mojolicious proper
Mojo as client
mojolicious -
	 what? who?

“real-time web application framework”
written by Sebastian Riedel (sri) &c
who also wrote Catalyst
current core: ams, tempire, mramberg
timeline

 first release 0.2 2008-09-24
 release 1.0 ‘snowflake’ 2010-12-26
 release 2.0 ‘leaf fluttering in the wind’ 2011-10-17
 release 3.0 ‘rainbow’ 2012-06-25
 release 3.46 2012-10-11
installation

 no dependencies
 # cpanm Mojolicious
 easy curl recipe
 # curl get.mojolicio.us | sh
execution

builtin (simple, morbo, hypnotoad)
nginx
apache/mod_proxy, CGI
PSGI/plack
anatomy of a
web framework

incoming request =>
  dispatcher =>
    action code =>
      outgoing response
dispatcher


small language
matching incoming request => action code
“routes”
getting started

# GET /
get '/' => sub {
  my $self = shift;
  $self->render(text => 'Hello');
};

# 200 OK, text/html, Hello
placeholder
# /hello/Lars
get '/hello/:name' => sub {
  my $self = shift;
  my $name = $self->param(‘name’);
  $self->render(text => “Hello $name”);
};

# 200 OK, text/html, Hello Lars
url parameters
# /hello/Lars?greet=Hej
get '/hello/:name' => sub {
  my $self = shift;
  my $name = $self->param(‘name’);
  my $greet = $self->param(‘greet’);
  $self->render(text => “$greet $name”);
} => ‘greeting’;

# 200 OK, text/html, Hej Lars
placeholders and routes,
next level
 slurpy placeholders
 wildcard placeholders
 formats (‘.html’, ‘.txt’)
 bridging routes
 conditional routes
stash
nonpresistent data store
keep data for the duration of the request
carries data from action code to template
with a bunch of reserved keys
format, text, template, ...


      $self->stash(name => ‘Lars’);
template

builtin ‘Embedded Perl’
variables in templates from stash
instantiated as normal values


           Hello, <%= $name %>
structures in templates

 It’s all Perl, so Perl control structures just work:



            % for my $i (0..3) {
              ... <%= $i %> ...
            % }
inline templates
__DATA__

@@ hello.html.ep
Hello, World!

@@ goodbye.html.ep
Goodbye, and thanks!
template selection
 explicit template name in render() call
  $self->render(template => ‘hello’);
 name in stash
  $self->stash(template => ‘hello’);
 defaults to name of router
  get ‘/welcome’ => ‘hello’;
auto render
no need to call render() yourself
if you don’t, mojo will do it for you


            get ‘/’ => ‘index’;

            __DATA__
            @@ index.html.ep
layouts
__DATA__
@@ index.html.ep
% layout ‘default’
Hello, World!

@@ layouts/default.html.ep
<html>
  <head><title>moo</title></head>
  <body><%= $content %></body>
</html>
templates, next level
 includes
 partial templates
 404, 500 pages
 blocks
 template inheritance
 ...
sessions

built in
data stored in cookies, client-side
cookie signed to prevent tampering
you can use something else, if you want
growing out of ::Lite

 inflate inline templates into proper files
 move routes into application class
 move action code into controller class
 testing with Test::Mojo
but wait, there’s more!
 Mojo::UserAgent
   nice web client
 Mojo::DOM
   excellent HTML/XML parser
 ojo
   for one-liners
resources

http://mojolicio.us
Mojolicious::Guides::{Routing,Rendering,Growing,...}
http://mojocasts.com by tempire
https://github.com/kraih/mojo
  incl wiki
thank you :)
 tagg @ irc
 taggg @ twitter
 lars@thegler.dk
 LTHEGLER@cpan.org
 lth@FreeBSD.org
 lars.thegler@teliasonera.com

Mojolicious, real-time web framework