Add [Read|Write]History for Term::ReadLine::Perl
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Interrupt.pm
1 package Devel::REPL::Plugin::Interrupt;
2
3 use Devel::REPL::Plugin;
4 use Sys::SigAction qw(set_sig_handler);
5 use namespace::clean -except => [ 'meta' ];
6
7 around 'run' => sub {
8     my ($orig, $self) = (shift, shift);
9
10     local $SIG{INT} = 'IGNORE';
11
12     return $self->$orig(@_);
13 };
14
15 around 'run_once' => sub {
16     my ($orig, $self) = (shift, shift);
17
18     # We have to use Sys::SigAction: Perl 5.8+ has safe signal handling by
19     # default, and Term::ReadLine::Gnu restarts the interrupted system calls.
20     # The result is that the signal handler is not fired until you hit Enter.
21     my $sig_action = set_sig_handler INT => sub {
22         die "Interrupted.\n";
23     };
24
25     return $self->$orig(@_);
26 };
27
28 around 'read' => sub {
29     my ($orig, $self) = (shift, shift);
30
31     # here SIGINT is caught and only kills the line being edited
32     while (1) {
33         my $line = eval { $self->$orig(@_) };
34         return $line unless $@;
35
36         die unless $@ =~ /^Interrupted\./;
37
38         # (Term::ReadLine::Gnu kills the line by default, but needs a LF -
39         # maybe I missed something?)
40         print "\n";
41     }
42 };
43
44 1;
45
46 __END__
47
48 =head1 NAME
49
50 Devel::REPL::Plugin::Interrupt - traps SIGINT to kill long-running lines
51
52 =head1 DESCRIPTION
53
54 By default L<Devel::REPL> exits on SIGINT (usually Ctrl-C). If you load this
55 module, SIGINT will be trapped and used to kill long-running commands
56 (statements) and also to kill the line being edited (like eg. BASH do). (You
57 can still use Ctrl-D to exit.)
58
59 =head1 AUTHOR
60
61 Shawn M Moore, C<< <sartak at gmail dot com> >>
62
63 =cut