Move to Moo for fast bootstrapping.
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Interrupt.pm
CommitLineData
54d0f9a0 1package Devel::REPL::Plugin::Interrupt;
2
6a5409bc 3use Devel::REPL::Plugin;
54fea9b0 4use Sys::SigAction qw(set_sig_handler);
e2d0b019 5use namespace::sweep;
54d0f9a0 6
54fea9b0 7around 'run' => sub {
8 my ($orig, $self) = (shift, shift);
54d0f9a0 9
54fea9b0 10 local $SIG{INT} = 'IGNORE';
11
12 return $self->$orig(@_);
13};
14
15around '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 {
54d0f9a0 22 die "Interrupted.\n";
23 };
24
54fea9b0 25 return $self->$orig(@_);
26};
27
28around '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 }
54d0f9a0 42};
43
441;
45
cfd1094b 46__END__
47
48=head1 NAME
49
50Devel::REPL::Plugin::Interrupt - traps SIGINT to kill long-running lines
51
54fea9b0 52=head1 DESCRIPTION
53
54By default L<Devel::REPL> exits on SIGINT (usually Ctrl-C). If you load this
55module, 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
57can still use Ctrl-D to exit.)
58
30b459d4 59=head1 AUTHOR
60
61Shawn M Moore, C<< <sartak at gmail dot com> >>
62
54fea9b0 63=cut