Collecting my attempts to improve at tech, art, and life

replyrc And Mojo

Tags: perl reply mojolicious tools

Series: The Reply Perl REPL


It has been a few days since I started playing with Reply, and I still enjoy it. Command history features from the ReadLine plugin became available once I installed GNU ReadLine and Term::ReadLine::Gnu.

There are still customizations that I would like to make. For example, I write most of my personal code using Perl 5.20’s experimental subroutine signature and postderef features.

Use Shiny Perl 5.20 Features Automatically

A default $HOME/.replyrc is created the first time you run Reply - unless you already have one, of course. It includes a selection of plugins that will be loaded and a collection of script_line entries that are evaluated automatically for any new Reply session. I fiddled with mine so that I had a Perl with syntactical sugar in place.

script_line1 = use 5.20.0
script_line2 = use warnings
script_line3 = use feature qw(signatures postderef)
script_line4 = no warnings 'experimental'

Each line gets its own numbered script_line entry because of the INI format and the way that input is parsed by Reply.

With this base level of behavior defined, I can run reply and rewrite my greet subroutine.

0> sub greet ($name) { "Hello $name!" }
1> my $me = "Brian"
$res[0] = 'Brian'

2> greet $me
$res[1] = 'Hello Brian!'

That’s better.

Enabling The Editor

You need Proc::InvokeEditor in order to activate the Editor plugin. I suppose that makes sense. Proc::InvokeEditor is a module that makes it easy to launch your default text editor on behalf of the application, sending the editor buffer back to your application as user input.

$ cpanm Proc::InvokeEditor

Add the Editor plugin entry to your .replyrc.


And just like that, here is a new #e command.

0> #e

It can probably work with EmacsClient, but I have been lazy lately and fallen back to Vim as my $EDITOR default.

Specifying a Module at Start

I have been exploring Mojolicious, which is a surprisingly full-featured framework considering its small size. The ojo library is a Mojolicious command line tool focused on making your one-liners even more useful. Since Reply is sort of an extended one-liner environment - okay, you can call it a “shell” - ojo and Reply can go together perfectly.

There is no need to add ojo to my .replyrc, because I will not be needing its functionality every single time I load Reply. Instead I will just tell Reply to load the library when starting those particular sessions.

Let’s keep with the example from the earlier post. I start reply with the -M flag to load a specific module on startup.

$ reply -Mojo
0> sort map { $_->{name} } j( g( '' )->body )->@*
$res[0] = [
  'Big Data',
  'DC Metro Region',
  'Python (Ru)',
  'Yoga + Meditation'

I know. This is confusing if you are unfamiliar with ojo and the experimental Perl 5.20 postderef feature. We can look at it in smaller pieces.

# Using ojo::g
g( '' )->body

# is roughly the same as this
$ua->get( '' )->res->body

g is a shortcut for the get method of Mojo::UserAgent. There are shortcuts for numerous HTTP verbs in ojo.

# using ojo::j and ojo::g
j( g( '' )->body )

# is rougly the same as this
decode_json( $ua->get( '' )->res->body )

j is a convenience function from Mojo::JSON for encoding and decoding JSON. My experience so far has been that it does what I mean when I use it.

# using j, g, and postderef
j( g( '' )->body )->@*

# is rougly the same as this
@{ decode_json( $ua->get( '' )->res->body) }

$ref->@* is an experimental new syntax for accessing the contents of an array reference. It is equivalent to @{ $ref } or @$ref. The postderef syntax is a little easier for me to read, but your experience may be different.

All Done

That is enough for now. With the ReadLine plugin and Editor plugin enabled, a nice 2014-ish Perl setup in my .replyrc, and ojo available when I want it, Reply is downright useful for me.

Got a comment? A question? More of a comment than a question?

Talk to me about this page on:

Added to vault 2024-01-15. Updated on 2024-02-02