NAME

Mojolicious - The Web In A Box!

SYNOPSIS

    # Mojolicious application
    package MyApp;

    use base 'Mojolicious';

    sub startup {
        my $self = shift;

        # Routes
        my $r = $self->routes;

        # Default route
        $r->route('/:controller/:action/:id')->to('foo#welcome');
    }

    # Mojolicious controller
    package MyApp::Foo;

    use base 'Mojolicious::Controller';

    # Say hello
    sub welcome {
        my $self = shift;
        $self->render_text('Hi there!');
    }

    # Say goodbye from a template (foo/bye.html.ep)
    sub bye { shift->render }

DESCRIPTION

Back in the early days of the web there was this wonderful Perl library called CGI, many people only learned Perl because of it. It was simple enough to get started without knowing much about the language and powerful enough to keep you going, learning by doing was much fun. While most of the techniques used are outdated now, the idea behind it is not. Mojolicious is a new attempt at implementing this idea using state of the art technology.

Features

    An amazing MVC web framework supporting a simplified single file mode through Mojolicious::Lite.

    Very clean, portable and Object Oriented pure Perl API without any hidden magic and no requirements besides Perl 5.8.1.

    Full stack HTTP 1.1 and WebSocket client/server implementation with IPv6, TLS, IDNA, pipelining, chunking and multipart support.

    Builtin async IO and prefork web server supporting epoll, kqueue, hot deployment and UNIX domain socket sharing, perfect for embedding.

    CGI, FastCGI and PSGI support.

    Fresh code, based upon years of experience developing Catalyst.

    Powerful out of the box with RESTful routes, plugins, sessions, signed cookies, static file server, testing framework, Perl-ish templates, JSON, I18N, first class Unicode support and much more for you to discover!

Duct Tape For The HTML5 Web

Web development for humans, making hard things possible and everything fun.

    use Mojolicious::Lite;

    get '/time' => 'clock';

    websocket '/echo' => sub {
        my $self = shift;
        $self->receive_message(
            sub {
                my ($self, $message) = @_;
                $self->send_message("echo: $message");
            }
        );
    };

    get '/fetch' => sub {
        my $self = shift;
        $self->render_data(
            $self->client->get('http://mojolicious.org')->res->body);
    };

    post '/:name' => sub {
        my $self = shift;
        my $name = $self->param('name') || 'Mojo';
        $self->render_text("Hello $name!");
    };

    shagadelic;
    __DATA__

    @@ clock.html.ep
    % my ($second, $minute, $hour) = (localtime(time))[0, 1, 2];
    The time is <%= $hour %>:<%= $minute %>:<%= $second %>.

For more user friendly documentation see Mojolicious::Guides and Mojolicious::Lite.

The Cake

    .---------------------------------------------------------------.
    |                             Fun!                              |
    '---------------------------------------------------------------'
    .---------------------------------------------------------------.
    |                                                               |
    |                .----------------------------------------------'
    |                | .--------------------------------------------.
    |   Application  | |              Mojolicious::Lite             |
    |                | '--------------------------------------------'
    |                | .--------------------------------------------.
    |                | |                 Mojolicious                |
    '----------------' '--------------------------------------------'
    .---------------------------------------------------------------.
    |                             Mojo                              |
    '---------------------------------------------------------------'
    .-------. .-----------. .--------. .------------. .-------------.
    |  CGI  | |  FastCGI  | |  PSGI  | |  HTTP 1.1  | |  WebSocket  |
    '-------' '-----------' '--------' '------------' '-------------'

ATTRIBUTES

Mojolicious inherits all attributes from Mojo and implements the following new ones.

mode

    my $mode = $mojo->mode;
    $mojo    = $mojo->mode('production');

The operating mode for your application. It defaults to the value of the environment variable MOJO_MODE or development. Mojo will name the log file after the current mode and modes other than development will result in limited log output.

If you want to add per mode logic to your application, you can add a sub to your application named $mode_mode.

    sub development_mode {
        my $self = shift;
    }

    sub production_mode {
        my $self = shift;
    }

plugins

    my $plugins = $mojo->plugins;
    $mojo       = $mojo->plugins(Mojolicious::Plugins->new);

The plugin loader, by default a Mojolicious::Plugins object. You can usually leave this alone, see Mojolicious::Plugin if you want to write a plugin.

renderer

    my $renderer = $mojo->renderer;
    $mojo        = $mojo->renderer(MojoX::Renderer->new);

Used in your application to render content, by default a MojoX::Renderer object. The two main renderer plugins Mojolicious::Plugin::EpRenderer and Mojolicious::Plugin::EplRenderer contain more specific information.

routes

    my $routes = $mojo->routes;
    $mojo      = $mojo->routes(MojoX::Dispatcher::Routes->new);

The routes dispatcher, by default a MojoX::Dispatcher::Routes object. You use this in your startup method to define the url endpoints for your application.

    sub startup {
        my $self = shift;

        my $r = $self->routes;
        $r->route('/:controller/:action')->to('test#welcome');
    }

secret

    my $secret = $mojo->secret;
    $mojo      = $mojo->secret('passw0rd');

A secret passphrase used for signed cookies and the like, defaults to the application name which is not very secure, so you should change it!!! As long as you are using the unsecure default there will be debug messages in the log file reminding you to change your passphrase.

static

    my $static = $mojo->static;
    $mojo      = $mojo->static(MojoX::Dispatcher::Static->new);

For serving static assets from your public directory, by default a MojoX::Dispatcher::Static object.

types

    my $types = $mojo->types;
    $mojo     = $mojo->types(MojoX::Types->new);

Responsible for tracking the types of content you want to serve in your application, by default a MojoX::Types object. You can easily register new types.

    $mojo->types->type(vti => 'help/vampire');

METHODS

Mojolicious inherits all methods from Mojo and implements the following new ones.

new

    my $mojo = Mojolicious->new;

Construct a new Mojolicious application. Will automatically detect your home directory and set up logging based on your current operating mode. Also sets up the renderer, static dispatcher and a default set of plugins.

dispatch

    $mojo->dispatch($c);

The heart of every Mojolicious application, calls the static and routes dispatchers for every request.

finish

    $mojo->finish($c);

Clean up after processing a request, usually called automatically.

handler

    $tx = $mojo->handler($tx);

Sets up the default controller and calls process for every request.

plugin

    $mojo->plugin('something');
    $mojo->plugin('something', foo => 23);
    $mojo->plugin('something', {foo => 23});

Load a plugin.

process

    $mojo->process($c);

This method can be overloaded to do logic on a per request basis, by default just calls dispatch. Generally you will use a plugin or controller instead of this, consider it the sledgehammer in your toolbox.

    sub process {
        my ($self, $c) = @_;
        $self->dispatch($c);
    }

start

    Mojolicious->start;
    Mojolicious->start('daemon');

Start the Mojolicious::Commands command line interface for your application.

startup

    $mojo->startup;

This is your main hook into the application, it will be called at application startup.

    sub startup {
        my $self = shift;
    }

SUPPORT

Web

    http://mojolicious.org

IRC

    #mojo on irc.perl.org

Mailing-List

    http://groups.google.com/group/mojolicious

DEVELOPMENT

Repository

    http://github.com/kraih/mojo

AUTHOR

Sebastian Riedel, sri@cpan.org.

CREDITS

In alphabetical order:

Adam Kennedy

Adriano Ferreira

Alex Salimon

Alexey Likhatskiy

Anatoly Sharifulin

Andre Vieth

Andreas Koenig

Andy Grundman

Aristotle Pagaltzis

Ask Bjoern Hansen

Audrey Tang

Breno G. de Oliveira

Burak Gursoy

Ch Lamprecht

Christian Hansen

David Davis

Dmitry Konstantinov

Gisle Aas

Glen Hinkle

Graham Barr

James Duncan

Jaroslav Muhin

Jesse Vincent

Kazuhiro Shibuya

Kevin Old

Lars Balker Rasmussen

Leon Brocard

Maik Fischer

Marcus Ramberg

Mark Stosberg

Matthew Lineen

Maksym Komar

Maxim Vuets

Mirko Westermeier

Pascal Gaudette

Pedro Melo

Pierre-Yves Ritschard

Rafal Pocztarski

Randal Schwartz

Robert Hicks

Ryan Jendoubi

Sascha Kiefer

Sergey Zasenko

Shu Cho

Stanis Trendelenburg

Tatsuhiko Miyagawa

Uwe Voelker

Viacheslav Tykhanovskyi

Yaroslav Korshak

Yuki Kimoto

COPYRIGHT AND LICENSE

Copyright (C) 2008-2010, Sebastian Riedel.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.