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