1 package Devel::REPL::Plugin::OutputCache;
3 use Devel::REPL::Plugin;
5 use MooX::Types::MooseLike::Base qw(ArrayRef Bool);
10 default => sub { [] },
14 has warned_about_underscore => (
21 around 'eval' => sub {
23 my ($self, $line) = @_;
25 my $has_underscore = *_{CODE};
26 if ($has_underscore && !$self->warned_about_underscore) {
27 warn "OutputCache: Sub _ already defined.";
28 $self->warned_about_underscore(1);
31 # if _ is removed, then we should warn about it again if it comes back
32 $self->warned_about_underscore(0);
35 # this needs to be a postfix conditional for 'local' to work
36 local *_ = sub () { $self->output_cache->[-1] } unless $has_underscore;
40 @ret = $self->$orig($line);
43 $ret[0] = $self->$orig($line);
46 push @{ $self->output_cache }, @ret > 1 ? \@ret : $ret[0];
47 return wantarray ? @ret : $ret[0];
56 Devel::REPL::Plugin::OutputCache - remember past results, _ is most recent
71 Re-using results is very useful when working in a REPL. With C<OutputCache> you
72 get C<_>, which holds the past result. The benefit is that you can build up
73 your result instead of having to type it in all at once, or store it in
74 intermediate variables. C<OutputCache> also provides
75 C<< $_REPL->output_cache >>, an array reference of all results in this session.
77 Devel::REPL already has a similar plugin, L<Devel::REPL::Plugin::History>.
78 There are some key differences though:
84 C<History> remembers input. C<OutputCache> remembers output.
86 =item Munging vs Pure Perl
88 C<History> performs regular expressions on your input. C<OutputCache> provides
89 the C<_> sub as a hook to get the most recent result, and
90 C<< $_REPL->output_cache >> for any other results.
92 =item Principle of Least Surprise
94 C<History> will replace exclamation points in any part of the input. This is
95 problematic if you accidentally include one in a string, or in a C<not>
96 expression. C<OutputCache> uses a regular (if oddly named) subroutine so Perl
97 does the parsing -- no surprises.
103 The C<_> sub is shared across all packages. This means that if a module is
104 using the C<_> sub, then there is a conflict and you should not use this
105 plugin. For example, L<Jifty> uses the C<_> sub for localization. Jifty is the
110 C<Devel::REPL>, C<Devel::REPL::Plugin::History>
114 Shawn M Moore, C<< <sartak at gmail dot com> >>
116 =head1 COPYRIGHT AND LICENSE
118 Copyright (C) 2007 by Shawn M Moore
120 This library is free software; you can redistribute it and/or modify
121 it under the same terms as Perl itself.