summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/ResultCache.pm
blob: 0407a684053d245c158e510596e9cdeb6316a802 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main;
use strict;
use warnings;
# ABSTRACT: retain previous results to be able to refer to them later

use mop;

=head1 SYNOPSIS

  ; .replyrc
  [ResultCache]
  variable = r

=head1 DESCRIPTION

This plugin caches the results of successful evaluations, and provides them in
a lexical array (by default C<@res>, although this can be changed via the
C<variable> option). This means that you can, for instance, access the value
returned by the previous line with C<$res[-1]>. It also modifies the output to
include an indication of where the value is stored, for later reference.

=cut

class Reply::Plugin::ResultCache extends Reply::Plugin {
    has $results = [];
    has $variable = 'res';

    method execute ($next, @args) {
        my @res = $next->(@args);
        if (@res == 1) {
            push @$results, $res[0];
        }
        elsif (@res > 1) {
            push @$results, \@res;
        }

        return @res;
    }

    method mangle_result ($result) {
        return unless defined $result;
        return '$' . $variable . '[' . $#$results . '] = ' . $result;
    }

    method lexical_environment {
        return { "\@$variable" => [ @$results ] };
    }
}

1;