3 package Devel::REPL::Plugin::OutputCache;
5 our $VERSION = '1.003027';
7 use Devel::REPL::Plugin;
8 use namespace::autoclean;
13 default => sub { [] },
17 has warned_about_underscore => (
24 around 'eval' => sub {
26 my ($self, $line) = @_;
28 my $has_underscore = *_{CODE};
29 if ($has_underscore && !$self->warned_about_underscore) {
30 warn "OutputCache: Sub _ already defined.";
31 $self->warned_about_underscore(1);
34 # if _ is removed, then we should warn about it again if it comes back
35 $self->warned_about_underscore(0);
38 # this needs to be a postfix conditional for 'local' to work
39 local *_ = sub () { $self->output_cache->[-1] } unless $has_underscore;
43 @ret = $self->$orig($line);
46 $ret[0] = $self->$orig($line);
49 push @{ $self->output_cache }, @ret > 1 ? \@ret : $ret[0];
50 return wantarray ? @ret : $ret[0];
59 Devel::REPL::Plugin::OutputCache - remember past results, _ is most recent
74 Re-using results is very useful when working in a REPL. With C<OutputCache> you
75 get C<_>, which holds the past result. The benefit is that you can build up
76 your result instead of having to type it in all at once, or store it in
77 intermediate variables. C<OutputCache> also provides
78 C<< $_REPL->output_cache >>, an array reference of all results in this session.
80 L<Devel::REPL> already has a similar plugin, L<Devel::REPL::Plugin::History>.
81 There are some key differences though:
87 C<History> remembers input. C<OutputCache> remembers output.
89 =item Munging vs Pure Perl
91 C<History> performs regular expressions on your input. C<OutputCache> provides
92 the C<_> sub as a hook to get the most recent result, and
93 C<< $_REPL->output_cache >> for any other results.
95 =item Principle of Least Surprise
97 C<History> will replace exclamation points in any part of the input. This is
98 problematic if you accidentally include one in a string, or in a C<not>
99 expression. C<OutputCache> uses a regular (if oddly named) subroutine so Perl
100 does the parsing -- no surprises.
106 The C<_> sub is shared across all packages. This means that if a module is
107 using the C<_> sub, then there is a conflict and you should not use this
108 plugin. For example, L<Jifty> uses the C<_> sub for localization. L<Jifty> is the
113 C<Devel::REPL>, C<Devel::REPL::Plugin::History>
117 Shawn M Moore, C<< <sartak at gmail dot com> >>
119 =head1 COPYRIGHT AND LICENSE
121 Copyright (C) 2007 by Shawn M Moore
123 This library is free software; you can redistribute it and/or modify
124 it under the same terms as Perl itself.