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