3 package Devel::REPL::Plugin::Interrupt;
4 # ABSTRACT: Traps SIGINT to kill long-running lines
6 our $VERSION = '1.003030';
8 use Devel::REPL::Plugin;
9 use Sys::SigAction qw(set_sig_handler);
10 use namespace::autoclean;
13 my ($orig, $self) = (shift, shift);
15 local $SIG{INT} = 'IGNORE';
17 return $self->$orig(@_);
20 around 'run_once' => sub {
21 my ($orig, $self) = (shift, shift);
23 # We have to use Sys::SigAction: Perl 5.8+ has safe signal handling by
24 # default, and Term::ReadLine::Gnu restarts the interrupted system calls.
25 # The result is that the signal handler is not fired until you hit Enter.
26 my $sig_action = set_sig_handler INT => sub {
30 return $self->$orig(@_);
33 around 'read' => sub {
34 my ($orig, $self) = (shift, shift);
36 # here SIGINT is caught and only kills the line being edited
38 my $line = eval { $self->$orig(@_) };
39 return $line unless $@;
41 die unless $@ =~ /^Interrupted\./;
43 # (Term::ReadLine::Gnu kills the line by default, but needs a LF -
44 # maybe I missed something?)
57 By default L<Devel::REPL> exits on SIGINT (usually Ctrl-C). If you load this
58 module, SIGINT will be trapped and used to kill long-running commands
59 (statements) and also to kill the line being edited (like eg. BASH do). (You
60 can still use Ctrl-D to exit.)
64 Shawn M Moore, C<< <sartak at gmail dot com> >>