3 package Devel::REPL::Plugin::OutputCache;
4 # ABSTRACT: Remember past results, _ is most recent
6 our $VERSION = '1.003027';
8 use Devel::REPL::Plugin;
9 use namespace::autoclean;
14 default => sub { [] },
18 has warned_about_underscore => (
25 around 'eval' => sub {
27 my ($self, $line) = @_;
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);
35 # if _ is removed, then we should warn about it again if it comes back
36 $self->warned_about_underscore(0);
39 # this needs to be a postfix conditional for 'local' to work
40 local *_ = sub () { $self->output_cache->[-1] } unless $has_underscore;
44 @ret = $self->$orig($line);
47 $ret[0] = $self->$orig($line);
50 push @{ $self->output_cache }, @ret > 1 ? \@ret : $ret[0];
51 return wantarray ? @ret : $ret[0];
73 Re-using results is very useful when working in a REPL. With C<OutputCache> you
74 get C<_>, which holds the past result. The benefit is that you can build up
75 your result instead of having to type it in all at once, or store it in
76 intermediate variables. C<OutputCache> also provides
77 C<< $_REPL->output_cache >>, an array reference of all results in this session.
79 L<Devel::REPL> already has a similar plugin, L<Devel::REPL::Plugin::History>.
80 There are some key differences though:
86 C<History> remembers input. C<OutputCache> remembers output.
88 =item Munging vs Pure Perl
90 C<History> performs regular expressions on your input. C<OutputCache> provides
91 the C<_> sub as a hook to get the most recent result, and
92 C<< $_REPL->output_cache >> for any other results.
94 =item Principle of Least Surprise
96 C<History> will replace exclamation points in any part of the input. This is
97 problematic if you accidentally include one in a string, or in a C<not>
98 expression. C<OutputCache> uses a regular (if oddly named) subroutine so Perl
99 does the parsing -- no surprises.
105 The C<_> sub is shared across all packages. This means that if a module is
106 using the C<_> sub, then there is a conflict and you should not use this
107 plugin. For example, L<Jifty> uses the C<_> sub for localization. L<Jifty> is the
112 C<Devel::REPL>, C<Devel::REPL::Plugin::History>
116 Shawn M Moore, C<< <sartak at gmail dot com> >>
118 =head1 COPYRIGHT AND LICENSE
120 Copyright (C) 2007 by Shawn M Moore
122 This library is free software; you can redistribute it and/or modify
123 it under the same terms as Perl itself.