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