Perl Module(s) Of The Week - 2019 Week 04 - Reply

Is there a good read–eval–print loop (REPL) or language shell exists for Perl? Yeah, there are several and the Perl module of the week is going to look at Reply module, a REPL that can be customized with several plugins for additional features.

Installation and quick run of the program. The `0>` is the console prompt at the counter of number of lines.
$ cpanm Reply
$ reply
0>

To install and use different plugins, for example, Reply::Plugin::Editor. Just install and update the default configuration file, `~/.replyrc`. To launch the editor when running the REPL, just use `#e` at the console.
$ cpanm Reply::Plugin::Editor
$ cat ~/.replyrc
[Editor]
editor = vi

Now for some code reading of the module. Surprisingly quite a small program compares to my initial assumption. To execute the program as source code, instantiate the class to load the configuration file and `run` it.
use Reply;
 
Reply->new(config => "$ENV{HOME}/.replyrc")->run;

First, code was written in old school Perl's OO as shown below. Codes are self-explanatory with clear variable names.
sub new {
    my $class = shift;
    my %opts = @_;
 
    my $self = bless {}, $class;
    ....
    return $self;
}

Next, run and loop the execution of the console input step by step until there is a termination.
sub run {
    my $self = shift;
 
    while (1) {
        my $continue = $self->step;
        last unless $continue;
    }
    print "\n";
}

Plugins are loaded dynamically based on the configuration files using the Module::Runtime module.
    if (!blessed($plugin)) {
        $plugin = compose_module_name("Reply::Plugin", $plugin);
        require_module($plugin);
        die "$plugin is not a valid plugin"
            unless $plugin->isa("Reply::Plugin");
 
        my $weakself = $self;
        weaken($weakself);
 
        $plugin = $plugin->new(
            %$opts,
            publisher => sub { $weakself->_publish(@_) },
       }

No comments:

Post a Comment