Sandeep Chayapathi <something or the other>

A REPL for your Mojolicious app

Read-Eval-Print loop (repl) is a lifesaver, you can quickly hack a script with one. However a repl with full access to your application is vastly more powerful.

Mojolicious is a next generation web framework for the perl programming language. There are many things to love about Mojolicious , ease of use is one of them.

Perl always came with a simple repl but Devel::REPL took it to the next level. Devel::REPL allows you to tailor the environemt and the following code snippet shows you how to add a interactive shell to your Mojolicious app quickly and simply.

Note: You will have to install Devel::REPL and Term::ReadLine packages from cpan

I used the Ado project as the base to show off how you can a interactive shell to a mojo app.

package Ado::Command::repl;
use Devel::REPL;
use Mojo::Base 'Mojolicious::Command';
use feature qw(say);

has description => 'REPL for your Mojo app';
has usage       => "Usage: APPLICATION repl\n";


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


    my $repl = Devel::REPL->new;
    $repl->load_plugin($_) for qw(History LexEnv Colors DDS Completion);

    my $app = $self->app;

    $repl->lexical_environment->set_context('_' => {'$app' => $app});

    $repl->run;
}

1;

This adds a new command to your mojolicious app. When you invoke the repl you are dropped into a interactive shell with full access to your mojolicious app.

Here’s a screen recording of few simple commands I ran in the repl