1 package Devel::REPL::Plugin::OutputCache;
4 use namespace::clean -except => [ 'meta' ];
13 around 'eval' => sub {
15 my ($self, $line) = @_;
17 local *_ = sub () { $self->output_cache->[-1] };
21 @ret = $self->$orig($line);
24 $ret[0] = $self->$orig($line);
27 push @{ $self->output_cache }, @ret > 1 ? \@ret : $ret[0];
28 return wantarray ? @ret : $ret[0];
37 Devel::REPL::Plugin::OutputCache - remember past results, _ is most recent
52 Re-using results is very useful when working in a REPL. With C<OutputCache> you
53 get C<_>, which holds the past result. The benefit is that you can build up
54 your result instead of having to type it in all at once, or store it in
55 intermediate variables. C<OutputCache> also provides
56 C<< $_REPL->output_cache >>, an array reference of all results in this session.
58 Devel::REPL already has a similar plugin, L<Devel::REPL::Plugin::History>.
59 There are some key differences though:
65 C<History> remembers input. C<OutputCache> remembers output.
67 =item Munging vs Pure Perl
69 C<History> performs regular expressions on your input. C<OutputCache> provides
70 the C<_> sub as a hook to get the most recent result, and
71 C<< $_REPL->output_cache >> for any other results.
73 =item Principle of Least Surprise
75 C<History> will replace exclamation points in any part of the input. This is
76 problematic if you accidentally include one in a string, or in a C<not>
77 expression. C<OutputCache> uses a regular (if oddly named) subroutine so Perl
78 does the parsing -- no surprises.
84 The C<_> sub is shared across all packages. This means that if a module is
85 using the C<_> sub, then there is a conflict and you should not use this
86 plugin. For example, L<Jifty> uses the C<_> sub for localization. Jifty is the
91 C<Devel::REPL>, C<Devel::REPL::Plugin::History>
95 Shawn M Moore, C<< <sartak at gmail dot com> >>
97 =head1 COPYRIGHT AND LICENSE
99 Copyright (C) 2007 by Shawn M Moore
101 This library is free software; you can redistribute it and/or modify
102 it under the same terms as Perl itself.