File Coverage

blib/lib/Devel/REPL/Plugin/OutputCache.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1 2     2   2773 use strict;
  2         3  
  2         69  
2 2     2   8 use warnings;
  2         3  
  2         118  
3             package Devel::REPL::Plugin::OutputCache;
4             # ABSTRACT: Remember past results, _ is most recent
5              
6             our $VERSION = '1.003027';
7              
8 2     2   9 use Devel::REPL::Plugin;
  2         4  
  2         15  
9 2     2   10473 use namespace::autoclean;
  2         5  
  2         23  
10              
11             has output_cache => (
12             is => 'rw',
13             isa => 'ArrayRef',
14             default => sub { [] },
15             lazy => 1,
16             );
17              
18             has warned_about_underscore => (
19             is => 'rw',
20             isa => 'Bool',
21             default => 0,
22             lazy => 1,
23             );
24              
25             around 'eval' => sub {
26             my $orig = shift;
27             my ($self, $line) = @_;
28              
29             my $has_underscore = *_{CODE};
30             if ($has_underscore && !$self->warned_about_underscore) {
31             warn "OutputCache: Sub _ already defined.";
32             $self->warned_about_underscore(1);
33             }
34             else {
35             # if _ is removed, then we should warn about it again if it comes back
36             $self->warned_about_underscore(0);
37             }
38              
39             # this needs to be a postfix conditional for 'local' to work
40             local *_ = sub () { $self->output_cache->[-1] } unless $has_underscore;
41              
42             my @ret;
43             if (wantarray) {
44             @ret = $self->$orig($line);
45             }
46             else {
47             $ret[0] = $self->$orig($line);
48             }
49              
50             push @{ $self->output_cache }, @ret > 1 ? \@ret : $ret[0];
51             return wantarray ? @ret : $ret[0];
52             };
53              
54             1;
55              
56             __END__
57              
58             =pod
59              
60             =encoding UTF-8
61              
62             =head1 NAME
63              
64             Devel::REPL::Plugin::OutputCache - Remember past results, _ is most recent
65              
66             =head1 VERSION
67              
68             version 1.003027
69              
70             =head1 SYNOPSIS
71              
72             > 21 / 7
73             3
74             > _ * _
75             9
76             > sub { die "later" }
77             sub { die "later" }
78             > _->()
79             Runtime error: later
80              
81             =head1 DESCRIPTION
82              
83             Re-using results is very useful when working in a REPL. With C<OutputCache> you
84             get C<_>, which holds the past result. The benefit is that you can build up
85             your result instead of having to type it in all at once, or store it in
86             intermediate variables. C<OutputCache> also provides
87             C<< $_REPL->output_cache >>, an array reference of all results in this session.
88              
89             L<Devel::REPL> already has a similar plugin, L<Devel::REPL::Plugin::History>.
90             There are some key differences though:
91              
92             =over 4
93              
94             =item Input vs Output
95              
96             C<History> remembers input. C<OutputCache> remembers output.
97              
98             =item Munging vs Pure Perl
99              
100             C<History> performs regular expressions on your input. C<OutputCache> provides
101             the C<_> sub as a hook to get the most recent result, and
102             C<< $_REPL->output_cache >> for any other results.
103              
104             =item Principle of Least Surprise
105              
106             C<History> will replace exclamation points in any part of the input. This is
107             problematic if you accidentally include one in a string, or in a C<not>
108             expression. C<OutputCache> uses a regular (if oddly named) subroutine so Perl
109             does the parsing -- no surprises.
110              
111             =back
112              
113             =head1 CAVEATS
114              
115             The C<_> sub is shared across all packages. This means that if a module is
116             using the C<_> sub, then there is a conflict and you should not use this
117             plugin. For example, L<Jifty> uses the C<_> sub for localization. L<Jifty> is the
118             only known user.
119              
120             =head1 SEE ALSO
121              
122             C<Devel::REPL>, C<Devel::REPL::Plugin::History>
123              
124             =head1 AUTHOR
125              
126             Shawn M Moore, C<< <sartak at gmail dot com> >>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             Copyright (C) 2007 by Shawn M Moore
131              
132             This library is free software; you can redistribute it and/or modify
133             it under the same terms as Perl itself.
134              
135             =cut