A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Mojolicious basics

How to do common stuff.

Run an app:

MOJO_USERAGENT_DEBUG=1 perl -I lib ~/path/to/morbo --verbose --watch lib --watch local bin/app.pl

View existing routes:

perl -I lib bin/app.pl routes

Find the code that defines the routes:

grep -r '$r->get' .
grep -r '$r->post' .

Install database:

perl -I lib bin/app.pl dbic_migration --action=install

Write a script that uses the app config, etc:

See Mojolicious::Command

Debug Test::Mojo:

print $t->tx->res->body; # See also guide to debugging

Catch unexpected exceptions:

use Mojo::Base 'Mojolicious';
use Try::Tiny;
$self->hook(
around_action => sub {
my ( $next, $c, $action, $last ) = @_;
my $request = $c->req;
return try {
return $next->();
}
catch {
my $error = $_;
if ( $error->$_isa('App::KnownException') ) {
$self->handle($error); # I meant to do that
}
else {
my $message = defined $error ? "$error" : "Unknown error";
$self->log->error($error);
return $c->reply->exception($message);
}
}
}
);
view raw exception.pl hosted with ❤ by GitHub
Dump out 2nd-level routes:
my @routes = sort map { $_->to_string } map { @{ $_->children } }
    grep { $_->name eq 'distro' || $_->name eq 'candidates' }
    @{ $t->app->routes->children };

Best Data::Dumper configuration for debugging Perl

This:

  local $Data::Dumper::Indent=0;
  local $Data::Dumper::Varname='';
  local $Data::Dumper::Terse=1;
  local $Data::Dumper::Pair='=>';
  local $Data::Dumper::Sortkeys=1;
  local $Data::Dumper::Quotekeys=0;

(source)

Or:

use Mojo::Util 'dumper';