File: ResultCache.pm

package info (click to toggle)
libreply-perl 0.35-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 276 kB
  • ctags: 130
  • sloc: perl: 1,425; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 1,880 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package Reply::Plugin::ResultCache;
BEGIN {
  $Reply::Plugin::ResultCache::AUTHORITY = 'cpan:DOY';
}
$Reply::Plugin::ResultCache::VERSION = '0.35';
use strict;
use warnings;
# ABSTRACT: retain previous results to be able to refer to them later

use base 'Reply::Plugin';


sub new {
    my $class = shift;
    my %opts = @_;

    my $self = $class->SUPER::new(@_);
    $self->{results} = [];
    $self->{result_name} = $opts{variable} || 'res';

    return $self;
}

sub execute {
    my $self = shift;
    my ($next, @args) = @_;

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

    return @res;
}

sub mangle_result {
    my $self = shift;
    my ($result) = @_;

    return unless defined $result;
    return '$' . $self->{result_name} . '[' . $#{ $self->{results} } . '] = '
         . $result;
}

sub lexical_environment {
    my $self = shift;
    return { "\@$self->{result_name}" => [ @{ $self->{results} } ] };
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Reply::Plugin::ResultCache - retain previous results to be able to refer to them later

=head1 VERSION

version 0.35

=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.

=head1 AUTHOR

Jesse Luehrs <doy@tozt.net>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2014 by Jesse Luehrs.

This is free software, licensed under:

  The MIT (X11) License

=cut