keep $VERSION right in the repo
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Interrupt.pm
CommitLineData
1716b200 1use strict;
2use warnings;
54d0f9a0 3package Devel::REPL::Plugin::Interrupt;
4
54beb05d 5our $VERSION = '1.003027';
6
6a5409bc 7use Devel::REPL::Plugin;
54fea9b0 8use Sys::SigAction qw(set_sig_handler);
aa8b7647 9use namespace::autoclean;
54d0f9a0 10
54fea9b0 11around 'run' => sub {
12 my ($orig, $self) = (shift, shift);
54d0f9a0 13
54fea9b0 14 local $SIG{INT} = 'IGNORE';
15
16 return $self->$orig(@_);
17};
18
19around '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 {
54d0f9a0 26 die "Interrupted.\n";
27 };
28
54fea9b0 29 return $self->$orig(@_);
30};
31
32around '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 }
54d0f9a0 46};
47
481;
49
cfd1094b 50__END__
51
52=head1 NAME
53
54Devel::REPL::Plugin::Interrupt - traps SIGINT to kill long-running lines
55
54fea9b0 56=head1 DESCRIPTION
57
58By default L<Devel::REPL> exits on SIGINT (usually Ctrl-C). If you load this
59module, 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
61can still use Ctrl-D to exit.)
62
30b459d4 63=head1 AUTHOR
64
65Shawn M Moore, C<< <sartak at gmail dot com> >>
66
54fea9b0 67=cut