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