Commit | Line | Data |
cf51843c |
1 | package Devel::REPL::Plugin::OutputCache; |
2 | |
3 | use Moose::Role; |
4 | use namespace::clean -except => [ 'meta' ]; |
5 | |
6 | has output_cache => ( |
7 | is => 'rw', |
8 | isa => 'ArrayRef', |
9 | default => sub { [] }, |
10 | lazy => 1, |
11 | ); |
12 | |
13 | around 'eval' => sub { |
14 | my $orig = shift; |
15 | my ($self, $line) = @_; |
16 | |
17 | local *_ = sub () { $self->output_cache->[-1] }; |
18 | |
19 | my @ret; |
20 | if (wantarray) { |
21 | @ret = $self->$orig($line); |
22 | } |
23 | else { |
24 | $ret[0] = $self->$orig($line); |
25 | } |
26 | |
27 | push @{ $self->output_cache }, @ret > 1 ? \@ret : $ret[0]; |
28 | return wantarray ? @ret : $ret[0]; |
29 | }; |
30 | |
31 | 1; |
32 | |
33 | __END__ |
34 | |
35 | =head1 NAME |
36 | |
37 | Devel::REPL::Plugin::OutputCache - remember past results, _ is most recent |
38 | |
39 | =head1 SYNOPSIS |
40 | |
41 | > 21 / 7 |
42 | 3 |
43 | > _ * _ |
44 | 9 |
45 | > sub { die "later" } |
46 | sub { die "later" } |
47 | > _->() |
48 | Runtime error: later |
49 | |
50 | =head1 DESCRIPTION |
51 | |
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. |
57 | |
58 | Devel::REPL already has a similar plugin, L<Devel::REPL::Plugin::History>. |
59 | There are some key differences though: |
60 | |
61 | =over 4 |
62 | |
63 | =item Input vs Output |
64 | |
65 | C<History> remembers input. C<OutputCache> remembers output. |
66 | |
67 | =item Munging vs Pure Perl |
68 | |
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. |
72 | |
73 | =item Principle of Least Surprise |
74 | |
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. |
79 | |
80 | =back |
81 | |
82 | =head1 CAVEATS |
83 | |
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 |
87 | only known user. |
88 | |
89 | =head1 SEE ALSO |
90 | |
91 | C<Devel::REPL>, C<Devel::REPL::Plugin::History> |
92 | |
93 | =head1 AUTHOR |
94 | |
95 | Shawn M Moore, C<< <sartak at gmail dot com> >> |
96 | |
97 | =head1 COPYRIGHT AND LICENSE |
98 | |
99 | Copyright (C) 2007 by Shawn M Moore |
100 | |
101 | This library is free software; you can redistribute it and/or modify |
102 | it under the same terms as Perl itself. |
103 | |
104 | =cut |