3 package Devel::REPL::Plugin::History;
4 # ABSTRACT: Keep track of all input, provide shortcuts !1, !-1
6 our $VERSION = '1.003029';
8 use Devel::REPL::Plugin;
9 use namespace::autoclean;
12 isa => 'ArrayRef', is => 'rw',
17 # lazy so ReadLineHistory Plugin can set this
18 has 'have_readline_history' => (
25 my ($self, $line) = @_;
26 # Push history is not needed if we have Term::ReadLine
27 # support. We put the test inside push_history() in case
28 # someone has modified it in their code.
29 if ($self->have_readline_history) {
30 # update history to keep consistent with Term::ReadLine
31 $self->history( [ $self->term->GetHistory ] );
33 # not used with Term::ReadLine history support
34 push(@{$self->history}, $line);
38 around 'read' => sub {
40 my ($self, @args) = @_;
41 my $line = $self->$orig(@args);
43 if ($line =~ m/^!(.*)$/) {
45 $line = $self->history_call($call);
47 $self->print($line."\n");
49 return "'Unable to find ${call} in history'";
53 $self->push_history($line);
60 my ($self, $call) = @_;
61 if ($call =~ m/^(-?\d+)$/) { # handle !1 or !-1
63 $idx-- if ($idx > 0); # !1 gets history element 0
64 my $line = $self->history->[$idx];
67 my $re = qr/^\Q${call}\E/;
68 foreach my $line (reverse @{$self->history}) {
69 return $line if ($line =~ $re);