Add the OutputCache plugin, which stores the most recent result in _
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / OutputCache.pm
CommitLineData
cf51843c 1package Devel::REPL::Plugin::OutputCache;
2
3use Moose::Role;
4use namespace::clean -except => [ 'meta' ];
5
6has output_cache => (
7 is => 'rw',
8 isa => 'ArrayRef',
9 default => sub { [] },
10 lazy => 1,
11);
12
13around 'eval' => sub {
14 my $orig = shift;
15 my ($self, $line) = @_;
16
17 local *_ = sub () { $self->output_cache->[-1] };
18
19 my @ret;
20 if (wantarray) {
21 @ret = $self->$orig($line);
22 }
23 else {
24 $ret[0] = $self->$orig($line);
25 }
26
27 push @{ $self->output_cache }, @ret > 1 ? \@ret : $ret[0];
28 return wantarray ? @ret : $ret[0];
29};
30
311;
32
33__END__
34
35=head1 NAME
36
37Devel::REPL::Plugin::OutputCache - remember past results, _ is most recent
38
39=head1 SYNOPSIS
40
41 > 21 / 7
42 3
43 > _ * _
44 9
45 > sub { die "later" }
46 sub { die "later" }
47 > _->()
48 Runtime error: later
49
50=head1 DESCRIPTION
51
52Re-using results is very useful when working in a REPL. With C<OutputCache> you
53get C<_>, which holds the past result. The benefit is that you can build up
54your result instead of having to type it in all at once, or store it in
55intermediate variables. C<OutputCache> also provides
56C<< $_REPL->output_cache >>, an array reference of all results in this session.
57
58Devel::REPL already has a similar plugin, L<Devel::REPL::Plugin::History>.
59There are some key differences though:
60
61=over 4
62
63=item Input vs Output
64
65C<History> remembers input. C<OutputCache> remembers output.
66
67=item Munging vs Pure Perl
68
69C<History> performs regular expressions on your input. C<OutputCache> provides
70the C<_> sub as a hook to get the most recent result, and
71C<< $_REPL->output_cache >> for any other results.
72
73=item Principle of Least Surprise
74
75C<History> will replace exclamation points in any part of the input. This is
76problematic if you accidentally include one in a string, or in a C<not>
77expression. C<OutputCache> uses a regular (if oddly named) subroutine so Perl
78does the parsing -- no surprises.
79
80=back
81
82=head1 CAVEATS
83
84The C<_> sub is shared across all packages. This means that if a module is
85using the C<_> sub, then there is a conflict and you should not use this
86plugin. For example, L<Jifty> uses the C<_> sub for localization. Jifty is the
87only known user.
88
89=head1 SEE ALSO
90
91C<Devel::REPL>, C<Devel::REPL::Plugin::History>
92
93=head1 AUTHOR
94
95Shawn M Moore, C<< <sartak at gmail dot com> >>
96
97=head1 COPYRIGHT AND LICENSE
98
99Copyright (C) 2007 by Shawn M Moore
100
101This library is free software; you can redistribute it and/or modify
102it under the same terms as Perl itself.
103
104=cut