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