NAME

Mojolicious::Guides::Cookbook - Cookbook

OVERVIEW

This document contains many fun recipes for cooking with Mojolicious.

DEPLOYMENT

Getting Mojolicious and Mojolicious::Lite applications running on different platforms. Note that many real-time web features are based on the Mojo::IOLoop event loop, and therefore require one of the built-in web servers to be able to use them to their full potential.

Built-in web server

Mojolicious contains a very portable non-blocking I/O HTTP and WebSocket server with Mojo::Server::Daemon. It is usually used during development and in the construction of more advanced web servers, but is solid and fast enough for small to mid sized applications.

  $ ./script/myapp daemon
  Server available at http://127.0.0.1:3000.

It has many configuration options and is known to work on every platform Perl works on.

  $ ./script/myapp daemon -h
  ...List of available options...

Another huge advantage is that it supports TLS and WebSockets out of the box.

  $ ./script/myapp daemon -l https://*:3000
  Server available at https://127.0.0.1:3000.

A development certificate for testing purposes is built right in, so it just works.

Morbo

After reading the Mojolicious::Lite tutorial, you should already be familiar with Mojo::Server::Morbo.

  Mojo::Server::Morbo
  +- Mojo::Server::Daemon

It is basically a restarter that forks a new Mojo::Server::Daemon web server whenever a file in your project changes, and should therefore only be used during development.

  $ morbo script/myapp
  Server available at http://127.0.0.1:3000.

Hypnotoad

For bigger applications Mojolicious contains the UNIX optimized preforking web server Mojo::Server::Hypnotoad that will allow you to take advantage of multiple CPU cores and copy-on-write.

  Mojo::Server::Hypnotoad
  |- Mojo::Server::Daemon [1]
  |- Mojo::Server::Daemon [2]
  |- Mojo::Server::Daemon [3]
  +- Mojo::Server::Daemon [4]

It is also based on the Mojo::Server::Daemon web server, but optimized specifically for production environments out of the box.

  $ hypnotoad script/myapp
  Server available at http://127.0.0.1:8080.

You can tweak many configuration settings right from within your application, for a full list see "SETTINGS" in Mojo::Server::Hypnotoad.

  use Mojolicious::Lite;

  app->config(hypnotoad => {listen => ['http://*:80']});

  get '/' => {text => 'Hello Wor...ALL GLORY TO THE HYPNOTOAD!'};

  app->start;

Or just add a hypnotoad section to your Mojolicious::Plugin::Config or Mojolicious::Plugin::JSONConfig configuration file.

  # myapp.conf
  {
    hypnotoad => {
      listen  => ['https://*:443?cert=/etc/server.crt&key=/etc/server.key'],
      workers => 10
    }
  };

But one of its biggest advantages is the support for effortless zero downtime software upgrades. That means you can upgrade Mojolicious, Perl or even system libraries at runtime without ever stopping the server or losing a single incoming connection, just by running the command above again.

  $ hypnotoad script/myapp
  Starting hot deployment for Hypnotoad server 31841.

You might also want to enable proxy support if you're using Hypnotoad behind a reverse proxy. This allows Mojolicious to automatically pick up the X-Forwarded-For and X-Forwarded-HTTPS headers.

  # myapp.conf
  {hypnotoad => {proxy => 1}};

Your application is preloaded in the manager process during startup, to run code whenever a new worker process has been forked you can use Mojo::IOLoop timers.

  use Mojolicious::Lite;

  Mojo::IOLoop->timer(0 => sub {
    app->log->info("Worker $$ star...ALL GLORY TO THE HYPNOTOAD!");
  });

  get '/' => {text => 'Hello Wor...ALL GLORY TO THE HYPNOTOAD!'};

  app->start;

Nginx

One of the most popular setups these days is the built-in web server behind a Nginx reverse proxy.

  upstream myapp {
    server 127.0.0.1:8080;
  }
  server {
    listen 80;
    server_name localhost;
    location / {
      proxy_read_timeout 300;
      proxy_pass http://myapp;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-HTTPS 0;
    }
  }

Apache/mod_proxy

Another good reverse proxy is Apache with mod_proxy, the configuration looks very similar to the Nginx one above.

  <VirtualHost *:80>
    ServerName localhost
    <Proxy *>
      Order deny,allow
      Allow from all
    </Proxy>
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass / http://localhost:8080/ keepalive=On
    ProxyPassReverse / http://localhost:8080/
    RequestHeader set X-Forwarded-HTTPS "0"
  </VirtualHost>

Apache/CGI

CGI is supported out of the box and your Mojolicious application will automatically detect that it is executed as a CGI script.

  ScriptAlias / /home/sri/myapp/script/myapp/

PSGI/Plack

PSGI is an interface between Perl web frameworks and web servers, and Plack is a Perl module and toolkit that contains PSGI middleware, helpers and adapters to web servers. PSGI and Plack are inspired by Python's WSGI and Ruby's Rack. Mojolicious applications are ridiculously simple to deploy with Plack.

  $ plackup ./script/myapp
  HTTP::Server::PSGI: Accepting connections at http://0:5000/

Plack provides many server and protocol adapters for you to choose from, such as FCGI, SCGI and mod_perl.

  $ plackup ./script/myapp -s FCGI -l /tmp/myapp.sock

If an older server adapter is not be able to correctly detect the application home directory, you can simply use the MOJO_HOME environment variable.

  $ MOJO_HOME=/home/sri/myapp plackup ./script/myapp
  HTTP::Server::PSGI: Accepting connections at http://0:5000/

There is no need for a .psgi file, just point the server adapter at your application script, it will automatically act like one if it detects the presence of a PLACK_ENV environment variable.

Plack middleware

Wrapper scripts like myapp.fcgi are a great way to separate deployment and application logic.

  #!/usr/bin/env plackup -s FCGI
  use Plack::Builder;

  builder {
    enable 'Deflater';
    require 'myapp.pl';
  };

But you could even use middleware right in your application.

  use Mojolicious::Lite;
  use Plack::Builder;

  get '/welcome' => sub {
    my $self = shift;
    $self->render(text => 'Hello Mojo!');
  };

  builder {
    enable 'Deflater';
    app->start;
  };

Rewriting

Sometimes you might have to deploy your application in a blackbox environment where you can't just change the server configuration or behind a reverse proxy that passes along additional information with X-* headers. In such cases you can use a before_dispatch hook to rewrite incoming requests.

  # Change scheme if "X-Forwarded-Protocol" header is set to "https"
  app->hook(before_dispatch => sub {
    my $self = shift;
    $self->req->url->base->scheme('https')
      if $self->req->headers->header('X-Forwarded-Protocol') eq 'https';
  });

Since reverse proxies generally don't pass along information about path prefixes your application might be deployed under, rewriting the base path of incoming requests is also quite common.

  # Move first part from path to base path in production mode
  app->hook(before_dispatch => sub {
    my $self = shift;
    push @{$self->req->url->base->path->parts},
      shift @{$self->req->url->path->parts};
  }) if app->mode eq 'production';

Application embedding

From time to time you might want to reuse parts of Mojolicious applications like configuration files, database connection or helpers for other scripts, with this little mock server you can just embed them.

  use Mojo::Server;

  # Load application with mock server
  my $server = Mojo::Server->new;
  my $app = $server->load_app('./myapp.pl');

  # Access fully initialized application
  say for @{$app->static->paths};
  say $app->config->{secret_identity};
  say $app->dumper({just => 'a helper test'});

Web server embedding

You can also use the built-in web server to embed Mojolicious applications into alien environments like foreign event loops.

  use Mojolicious::Lite;
  use Mojo::IOLoop;
  use Mojo::Server::Daemon;

  # Normal action
  get '/' => {text => 'Hello World!'};

  # Connect application with web server and start accepting connections
  my $daemon
    = Mojo::Server::Daemon->new(app => app, listen => ['http://*:8080']);
  $daemon->start;

  # Call "one_tick" repeatedly from the alien environment
  Mojo::IOLoop->one_tick while 1;

REAL-TIME WEB

The real-time web is a collection of technologies that include Comet (long-polling), EventSource and WebSockets, which allow content to be pushed to consumers with long-lived connections as soon as it is generated, instead of relying on the more traditional pull model. All built-in web servers use non-blocking I/O and are based on the Mojo::IOLoop event loop, which provides many very powerful features that allow real-time web applications to scale up to thousands of clients.

Backend web services

Since Mojo::UserAgent is also based on the Mojo::IOLoop event loop, it won't block the built-in web servers when used non-blocking, even for high latency backend web services.

  use Mojolicious::Lite;

  # Search Twitter for "perl"
  get '/' => sub {
    my $self = shift;
    $self->ua->get('http://search.twitter.com/search.json?q=perl' => sub {
      my ($ua, $tx) = @_;
      $self->render('twitter', results => $tx->res->json->{results});
    });
  };

  app->start;
  __DATA__

  @@ twitter.html.ep
  <!DOCTYPE html>
  <html>
    <head><title>Twitter results for "perl"</title></head>
    <body>
      % for my $result (@$results) {
        <p><%= $result->{text} %></p>
      % }
    </body>
  </html>

Multiple events such as parallel requests can be easily synchronized with a Mojo::IOLoop delay.

  use Mojolicious::Lite;
  use Mojo::IOLoop;
  use Mojo::URL;

  # Search Twitter for "perl" and "python"
  get '/' => sub {
    my $self = shift;

    # Prepare response in two steps
    Mojo::IOLoop->delay(

      # Parallel requests
      sub {
        my $delay = shift;
        my $url   = Mojo::URL->new('http://search.twitter.com/search.json');
        $self->ua->get($url->clone->query({q => 'perl'})   => $delay->begin);
        $self->ua->get($url->clone->query({q => 'python'}) => $delay->begin);
      },

      # Delayed rendering
      sub {
        my ($delay, $perl, $python) = @_;
        $self->render(json => {
          perl   => $perl->res->json('/results/0/text'),
          python => $python->res->json('/results/0/text')
        });
      }
    );
  };

  app->start;

Timers

Another primary feature of the Mojo::IOLoop event loop are timers, which can for example be used to delay rendering of a response, and unlike sleep, won't block any other requests that might be processed in parallel.

  use Mojolicious::Lite;
  use Mojo::IOLoop;

  # Wait 3 seconds before rendering a response
  get '/' => sub {
    my $self = shift;
    Mojo::IOLoop->timer(3 => sub {
      $self->render(text => 'Delayed by 3 seconds!');
    });
  };

  app->start;

Recurring timers are slightly more powerful, but need to be stopped manually, or they would just keep getting emitted.

  use Mojolicious::Lite;
  use Mojo::IOLoop;

  # Count to 5 in 1 second steps
  get '/' => sub {
    my $self = shift;

    # Start recurring timer
    my $i = 1;
    my $id = Mojo::IOLoop->recurring(1 => sub {
      $self->write_chunk($i);
      $self->finish if $i++ == 5;
    });

    # Stop recurring timer
    $self->on(finish => sub { Mojo::IOLoop->remove($id) });
  };

  app->start;

Timers are not tied to a specific request or connection, and can even be created at startup time.

  use Mojolicious::Lite;
  use Mojo::IOLoop;

  # Count seconds since startup
  my $i = 0;
  Mojo::IOLoop->recurring(1 => sub { $i++ });

  # Show counter
  get '/' => sub {
    my $self = shift;
    $self->render(text => "About $i seconds running!");
  };

  app->start;

Since timers and other low level event watchers are also independent from applications, errors can't get logged automatically, you can change that by subscribing to the event "error" in Mojo::Reactor.

  # Forward error messages to the application log
  Mojo::IOLoop->singleton->reactor->on(error => sub {
    my ($reactor, $err) = @_;
    app->log->error($err);
  });

Just remember that all events are processed cooperatively, so your callbacks shouldn't block for too long.

WebSocket web service

The WebSocket protocol offers full bi-directional low-latency communication channels between clients and servers. Receiving messages is as easy as subscribing to the event "message" in Mojo::Transaction::WebSocket with the method "on" in Mojolicious::Controller.

  use Mojolicious::Lite;
  use Mojo::IOLoop;

  # Template with browser-side code
  get '/' => 'index';

  # WebSocket echo service
  websocket '/echo' => sub {
    my $self = shift;

    # Connected
    $self->app->log->debug('WebSocket connected.');

    # Increase inactivity timeout for connection a bit
    Mojo::IOLoop->stream($self->tx->connection)->timeout(300);

    # Incoming message
    $self->on(message => sub {
      my ($self, $msg) = @_;
      $self->send("echo: $msg");
    });

    # Disconnected
    $self->on(finish => sub {
      my $self = shift;
      $self->app->log->debug('WebSocket disconnected.');
    });
  };

  app->start;
  __DATA__

  @@ index.html.ep
  <!DOCTYPE html>
  <html>
    <head><title>Echo</title></head>
    <body>
      <script>
        var ws = new WebSocket('<%= url_for('echo')->to_abs %>');

        // Incoming messages
        ws.onmessage = function(event) {
          document.body.innerHTML += event.data + '<br/>';
        };

        // Outgoing messages
        window.setInterval(function() {
          ws.send('Hello Mojo!');
        }, 1000);
      </script>
    </body>
  </html>

The event "finish" in Mojo::Transaction::WebSocket will be emitted right after the WebSocket connection has been closed.

Testing WebSocket web services

While the message flow on WebSocket connections can be rather dynamic, it more often than not is quite predictable, which allows this rather pleasant Test::Mojo API to be used.

  use Test::More;
  use Test::Mojo;

  # Include application
  use FindBin;
  require "$FindBin::Bin/../echo.pl";

  # Test echo web service
  my $t = Test::Mojo->new;
  $t->websocket_ok('/echo')
    ->send_ok('Hello Mojo!')
    ->message_is('echo: Hello Mojo!')
    ->finish_ok;

  done_testing();

EventSource web service

EventSource is a special form of long-polling where you can directly send DOM events from servers to clients. It is uni-directional, that means you will have to use Ajax requests for sending data from clients to servers, the advantage however is low infrastructure requirements, since it reuses the HTTP protocol for transport.

  use Mojolicious::Lite;
  use Mojo::IOLoop;

  # Template with browser-side code
  get '/' => 'index';

  # EventSource for log messages
  get '/events' => sub {
    my $self = shift;

    # Increase inactivity timeout for connection a bit
    Mojo::IOLoop->stream($self->tx->connection)->timeout(300);

    # Change content type
    $self->res->headers->content_type('text/event-stream');

    # Subscribe to "message" event and forward "log" events to browser
    my $cb = $self->app->log->on(message => sub {
      my ($log, $level, @lines) = @_;
      $self->write("event:log\ndata: [$level] @lines\n\n");
    });

    # Unsubscribe from "message" event again once we are done
    $self->on(finish => sub {
      my $self = shift;
      $self->app->log->unsubscribe(message => $cb);
    });
  };

  app->start;
  __DATA__

  @@ index.html.ep
  <!DOCTYPE html>
  <html>
    <head><title>LiveLog</title></head>
    <body>
      <script>
        var events = new EventSource('<%= url_for 'events' %>');

        // Subscribe to "log" event
        events.addEventListener('log', function(event) {
          document.body.innerHTML += event.data + '<br/>';
        }, false);
      </script>
    </body>
  </html>

The event "message" in Mojo::Log will be emitted for every new log message and the event "finish" in Mojo::Transaction right after the transaction has been finished.

Streaming multipart uploads

Mojolicious contains a very sophisticated event system based on Mojo::EventEmitter, with ready-to-use events on almost all layers, and which can be combined to solve some of hardest problems in web development.

  use Mojolicious::Lite;
  use Scalar::Util 'weaken';

  # Emit "request" event early for requests that get upgraded to multipart
  hook after_build_tx => sub {
    my $tx = shift;
    weaken $tx;
    $tx->req->content->on(upgrade => sub { $tx->emit('request') });
  };

  # Upload form in DATA section
  get '/' => 'index';

  # Streaming multipart upload (invoked twice, due to early "request" event)
  post '/upload' => sub {
    my $self = shift;

    # First invocation, subscribe to "part" event to find the right one
    return $self->req->content->on(part => sub {
      my ($multi, $single) = @_;

      # Subscribe to "body" event of part to make sure we have all headers
      $single->on(body => sub {
        my $single = shift;

        # Make sure we have the right part and replace "read" event
        return unless $single->headers->content_disposition =~ /example/;
        $single->unsubscribe('read')->on(read => sub {
          my ($single, $chunk) = @_;

          # Log size of every chunk we receive
          $self->app->log->debug(length($chunk) . ' bytes uploaded.');
        });
      });
    }) unless $self->req->is_finished;

    # Second invocation, render response
    $self->render(text => 'Upload was successful.');
  };

  app->start;
  __DATA__

  @@ index.html.ep
  <!DOCTYPE html>
  <html>
    <head><title>Streaming multipart upload</title></head>
    <body>
      %= form_for upload => (enctype => 'multipart/form-data') => begin
        %= file_field 'example'
        %= submit_button 'Upload'
      % end
    </body>
  </html>

Event loops

Internally the Mojo::IOLoop event loop can use multiple reactor backends, EV for example will be automatically used if installed. Which in turn allows other event loops like IO::Async to just work.

  use Mojolicious::Lite;
  use EV;
  use IO::Async::Loop::EV;
  use IO::Async::Timer::Absolute;

  my $loop = IO::Async::Loop::EV->new;

  # Wait 3 seconds before rendering a response
  get '/' => sub {
    my $self = shift;
    $loop->add(IO::Async::Timer::Absolute->new(
      time      => time + 3,
      on_expire => sub { $self->render(text => 'Delayed by 3 seconds!') }
    ));
  };

  app->start;

Same for AnyEvent.

  use Mojolicious::Lite;
  use EV;
  use AnyEvent;

  # Wait 3 seconds before rendering a response
  get '/' => sub {
    my $self = shift;
    my $w;
    $w = AE::timer 3, 0, sub {
      $self->render(text => 'Delayed by 3 seconds!');
      undef $w;
    };
  };

  app->start;

Who actually controls the event loop backend is not important.

  use Mojo::UserAgent;
  use EV;
  use AnyEvent;

  # Search Twitter for "perl"
  my $cv = AE::cv;
  my $ua = Mojo::UserAgent->new;
  $ua->get('http://search.twitter.com/search.json?q=perl' => sub {
    my ($ua, $tx) = @_;
    $cv->send($tx->res->json('/results/0/text'));
  });
  say $cv->recv;

You could for example just embed the built-in web server into an AnyEvent application.

  use Mojolicious::Lite;
  use Mojo::Server::Daemon;
  use EV;
  use AnyEvent;

  # Normal action
  get '/' => {text => 'Hello World!'};

  # Connect application with web server and start accepting connections
  my $daemon
    = Mojo::Server::Daemon->new(app => app, listen => ['http://*:8080']);
  $daemon->start;

  # Let AnyEvent take control
  AE::cv->recv;

USER AGENT

When we say Mojolicious is a web framework we actually mean it.

Web scraping

Scraping information from web sites has never been this much fun before. The built-in HTML/XML parser Mojo::DOM supports all CSS selectors that make sense for a standalone parser.

  use Mojo::UserAgent;

  # Fetch web site
  my $ua = Mojo::UserAgent->new;
  my $tx = $ua->get('mojolicio.us/perldoc');

  # Extract title
  say 'Title: ', $tx->res->dom->at('head > title')->text;

  # Extract headings
  $tx->res->dom('h1, h2, h3')->each(sub { say 'Heading: ', shift->all_text });

  # Visit all elements recursively to extract more than just text
  for my $e ($tx->res->dom('*')->each) {

    # Text before this element
    print $e->text_before(0);

    # Also include alternate text for images
    print $e->{alt} if $e->type eq 'img';

    # Text for elements without children
    print $e->text(0) unless $e->children->size;

    # Text after last element
    print $e->text_after(0) unless $e->next;
  }

Especially for unit testing your Mojolicious applications this can be a very powerful tool.

JSON web services

Most web services these days are based on the JSON data-interchange format. That's why Mojolicious comes with the possibly fastest pure-Perl implementation Mojo::JSON built right in.

  use Mojo::UserAgent;
  use Mojo::Util 'encode';

  # Fresh user agent
  my $ua = Mojo::UserAgent->new;

  # Fetch the latest news about Mojolicious from Twitter
  my $search = 'http://search.twitter.com/search.json?q=Mojolicious';
  for $tweet (@{$ua->get($search)->res->json->{results}}) {

    # Tweet text
    my $text = $tweet->{text};

    # Twitter user
    my $user = $tweet->{from_user};

    # Show both
    say encode('UTF-8', "$text --$user");
  }

Basic authentication

You can just add username and password to the URL.

  use Mojo::UserAgent;

  my $ua = Mojo::UserAgent->new;
  say $ua->get('https://sri:secret@mojolicio.us/hideout')->res->body;

Decorating followup requests

Mojo::UserAgent can automatically follow redirects, the event "start" in Mojo::UserAgent allows you direct access to each transaction right after they have been initialized and before a connection gets associated with them.

  use Mojo::UserAgent;

  # User agent following up to 10 redirects
  my $ua = Mojo::UserAgent->new(max_redirects => 10);

  # Add a witty header to every request
  $ua->on(start => sub {
    my ($ua, $tx) = @_;
    $tx->req->headers->header('X-Bender' => 'Bite my shiny metal ass!');
    say 'Request: ', $tx->req->url->clone->to_abs;
  });

  # Request that will most likely get redirected
  say 'Title: ', $ua->get('google.com')->res->dom->at('head > title')->text;

This even works for proxy CONNECT requests.

Streaming response

Receiving a streaming response can be really tricky in most HTTP clients, but Mojo::UserAgent makes it actually easy.

  use Mojo::UserAgent;

  # Build a normal transaction
  my $ua = Mojo::UserAgent->new;
  my $tx = $ua->build_tx(GET => 'http://mojolicio.us');

  # Replace "read" events to disable default content parser
  $tx->res->content->unsubscribe('read')->on(read => sub {
    my ($content, $chunk) = @_;
    say "Streaming: $chunk";
  });

  # Process transaction
  $ua->start($tx);

The event "read" in Mojo::Content will be emitted for every chunk of data that is received, even chunked encoding will be handled transparently if necessary.

Streaming request

Sending a streaming request is almost just as easy.

  use Mojo::UserAgent;

  # Build a normal transaction
  my $ua = Mojo::UserAgent->new;
  my $tx = $ua->build_tx(GET => 'http://mojolicio.us');

  # Prepare content
  my $content = 'Hello world!';
  $tx->req->headers->content_length(length $content);

  # Start writing directly with a drain callback
  my $drain;
  $drain = sub {
    my $req   = shift;
    my $chunk = substr $content, 0, 1, '';
    $drain    = undef unless length $content;
    $req->write($chunk, $drain);
  };
  $tx->req->$drain;

  # Process transaction
  $ua->start($tx);

The drain callback passed to "write" in Mojo::Message will be invoked whenever the entire previous chunk has actually been written.

Large file downloads

When downloading large files with Mojo::UserAgent you don't have to worry about memory usage at all, because it will automatically stream everything above 250KB into a temporary file.

  use Mojo::UserAgent;

  # Lets fetch the latest Mojolicious tarball
  my $ua = Mojo::UserAgent->new(max_redirects => 5);
  my $tx = $ua->get('latest.mojolicio.us');
  $tx->res->content->asset->move_to('mojo.tar.gz');

To protect you from excessively large files there is also a limit of 5MB by default, which you can tweak with the MOJO_MAX_MESSAGE_SIZE environment variable.

  # Increase limit to 1GB
  $ENV{MOJO_MAX_MESSAGE_SIZE} = 1073741824;

Large file upload

Uploading a large file is even easier.

  use Mojo::UserAgent;

  # Upload file via POST and "multipart/form-data"
  my $ua = Mojo::UserAgent->new;
  $ua->post_form('mojolicio.us/upload',
    {image => {file => '/home/sri/hello.png'}});

And once again you don't have to worry about memory usage, all data will be streamed directly from the file.

  use Mojo::UserAgent;

  # Upload file via PUT
  my $ua     = Mojo::UserAgent->new;
  my $asset  = Mojo::Asset::File->new(path => '/home/sri/hello.png');
  my $tx     = $ua->build_tx(PUT => 'mojolicio.us/upload');
  $tx->req->content->asset($asset);
  $ua->start($tx);

Non-blocking

Mojo::UserAgent has been designed from the ground up to be non-blocking, the whole blocking API is just a simple convenience wrapper. Especially for high latency tasks like web crawling this can be extremely useful, because you can keep many parallel connections active at the same time.

  use Mojo::UserAgent;
  use Mojo::IOLoop;

  # Parallel non-blocking requests
  my $ua = Mojo::UserAgent->new;
  $ua->get('http://mojolicio.us' => sub {
    my ($ua, $tx) = @_;
    ...
  });
  $ua->get('http://mojolicio.us/perldoc' => sub {
    my ($ua, $tx) = @_;
    ...
  });

  # Start event loop if necessary
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

You can take full control of the Mojo::IOLoop event loop.

Parallel blocking requests

You can emulate blocking behavior by using a Mojo::IOLoop delay to synchronize multiple non-blocking requests. Just be aware that the resulting transactions will be in random order.

  use Mojo::UserAgent;
  use Mojo::IOLoop;

  # Synchronize non-blocking requests and capture result
  my $ua    = Mojo::UserAgent->new;
  my $delay = Mojo::IOLoop->delay;
  $ua->get('http://mojolicio.us'         => $delay->begin);
  $ua->get('http://mojolicio.us/perldoc' => $delay->begin);
  my ($tx, $tx2) = $delay->wait;

The event "finish" in Mojo::IOLoop::Delay can be used for code that needs to be able to work standalone as well as inside Mojolicious applications.

  use Mojo::UserAgent;
  use Mojo::IOLoop;

  # Synchronize non-blocking requests portably
  my $ua    = Mojo::UserAgent->new;
  my $delay = Mojo::IOLoop->delay(sub {
    my ($delay, $tx, $tx2) = @_;
    ...
  });
  $ua->get('http://mojolicio.us'         => $delay->begin);
  $ua->get('http://mojolicio.us/perldoc' => $delay->begin);
  $delay->wait unless Mojo::IOLoop->is_running;

Command line

Don't you hate checking huge HTML files from the command line? Thanks to the mojo get command that is about to change. You can just pick the parts that actually matter with the CSS selectors from Mojo::DOM and JSON Pointers from Mojo::JSON::Pointer.

  $ mojo get http://mojolicio.us 'head > title'

How about a list of all id attributes?

  $ mojo get http://mojolicio.us '*' attr id

Or the text content of all heading tags?

  $ mojo get http://mojolicio.us 'h1, h2, h3' text

Maybe just the text of the third heading?

  $ mojo get http://mojolicio.us 'h1, h2, h3' 3 text

You can also extract all text from nested child elements.

  $ mojo get http://mojolicio.us '#mojobar' all

The request can be customized as well.

  $ mojo get -M POST -c 'Hello!' http://mojolicio.us
  $ mojo get -H 'X-Bender: Bite my shiny metal ass!' http://google.com

You can follow redirects and view the headers for all messages.

  $ mojo get -r -v http://reddit.com 'head > title'

Extract just the information you really need from JSON data structures.

  $ mojo get http://search.twitter.com/search.json /error

This can be an invaluable tool for testing your applications.

  $ ./myapp.pl get /welcome 'head > title'

Oneliners

For quick hacks and especially testing, ojo oneliners are also a great choice.

  $ perl -Mojo -E 'say g("mojolicio.us")->dom->html->head->title->text'

HACKS

Fun hacks you might not use very often but that might come in handy some day.

Adding commands to Mojolicious

By now you've propably used many of the built-in commands described in Mojolicious::Commands, but did you know that you can just add new ones and that they will be picked up automatically by the command line interface?

  package Mojolicious::Command::spy;
  use Mojo::Base 'Mojolicious::Command';

  has description => "Spy on application.\n";
  has usage       => "usage: $0 spy [TARGET]\n";

  sub run {
    my ($self, $target) = @_;

    # Leak secret passphrase
    if ($target eq 'secret') {
      my $secret = $self->app->secret;
      say qq{The secret of this application is "$secret".};
    }
  }

  1;

There are many more useful methods and attributes in Mojolicious::Command that you can use or overload.

  $ mojo spy secret
  The secret of this application is "Mojolicious::Lite".

  $ ./myapp.pl spy secret
  The secret of this application is "secr3t".

And to make your commands application specific, just put them in a different namespace.

  # Application
  package MyApp;
  use Mojo::Base 'Mojolicious';

  sub startup {
    my $self = shift;

    # Add another namespace to load commands from
    push @{$self->commands->namespaces}, 'MyApp::Command';
  }

  1;

Running code against your application

Ever thought about running a quick oneliner against your Mojolicious application to test something? Thanks to the eval command you can do just that, the application object itself can be accessed via app.

  $ mojo generate lite_app
  $ ./myapp.pl eval 'say for @{app->static->paths}'

The verbose option will automatically print the return value to STDOUT.

  $ ./myapp.pl eval -v 'app->static->paths->[0]'

Making your application installable

Ever thought about releasing your Mojolicious application to CPAN? It's actually much easier than you might think.

  $ mojo generate app
  $ cd my_mojolicious_app
  $ mv public lib/MyMojoliciousApp/
  $ mv templates lib/MyMojoliciousApp/

The trick is to move the public and templates directories so they can get automatically installed with the modules.

  # Application
  package MyMojoliciousApp;
  use Mojo::Base 'Mojolicious';

  use File::Basename 'dirname';
  use File::Spec::Functions 'catdir';

  # Every CPAN module needs a version
  our $VERSION = '1.0';

  sub startup {
    my $self = shift;

    # Switch to installable home directory
    $self->home->parse(catdir(dirname(__FILE__), 'MyMojoliciousApp'));

    # Switch to installable "public" directory
    $self->static->paths->[0] = $self->home->rel_dir('public');

    # Switch to installable "templates" directory
    $self->renderer->paths->[0] = $self->home->rel_dir('templates');

    $self->plugin('PODRenderer');

    my $r = $self->routes;
    $r->get('/welcome')->to('example#welcome');
  }

  1;

That's really everything, now you can package your application like any other CPAN module.

  $ ./script/my_mojolicious_app generate makefile
  $ perl Makefile.PL
  $ make test
  $ make manifest
  $ make dist

And if you have a PAUSE account (which can be requested at http://pause.perl.org) even upload it.

  $ mojo cpanify -u USER -p PASS MyMojoliciousApp-0.01.tar.gz

Hello World

If every byte matters this is the smallest Hello World application you can write with Mojolicious::Lite.

  use Mojolicious::Lite;
  any {text => 'Hello World!'};
  app->start;

It works because all routes without a pattern default to / and automatic rendering kicks in even if no actual code gets executed by the router. The renderer just picks up the text value from the stash and generates a response.

Hello World oneliners

The Hello World example above can get even a little bit shorter in an ojo oneliner.

  $ perl -Mojo -E 'a({text => "Hello World!"})->start' daemon

And you can use all the commands from Mojolicious::Commands.

  $ perl -Mojo -E 'a({text => "Hello World!"})->start' get -v /

MORE

You can continue with Mojolicious::Guides now or take a look at the Mojolicious wiki, which contains a lot more documentation and examples by many different authors.