3 package Devel::REPL::Plugin::Interrupt;
5 use Devel::REPL::Plugin;
6 use Sys::SigAction qw(set_sig_handler);
7 use namespace::autoclean;
10 my ($orig, $self) = (shift, shift);
12 local $SIG{INT} = 'IGNORE';
14 return $self->$orig(@_);
17 around 'run_once' => sub {
18 my ($orig, $self) = (shift, shift);
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 {
27 return $self->$orig(@_);
30 around 'read' => sub {
31 my ($orig, $self) = (shift, shift);
33 # here SIGINT is caught and only kills the line being edited
35 my $line = eval { $self->$orig(@_) };
36 return $line unless $@;
38 die unless $@ =~ /^Interrupted\./;
40 # (Term::ReadLine::Gnu kills the line by default, but needs a LF -
41 # maybe I missed something?)
52 Devel::REPL::Plugin::Interrupt - traps SIGINT to kill long-running lines
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.)
63 Shawn M Moore, C<< <sartak at gmail dot com> >>