File Coverage

blib/lib/Devel/REPL/Plugin/Interrupt.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1 2     2   13878 use strict;
  2         7  
  2         60  
2 2     2   10 use warnings;
  2         4  
  2         113  
3             # ABSTRACT: Traps SIGINT to kill long-running lines
4              
5             our $VERSION = '1.003029';
6              
7             use Devel::REPL::Plugin;
8 2     2   10 use Sys::SigAction qw(set_sig_handler);
  2         4  
  2         14  
9 2     2   9513 use namespace::autoclean;
  2         14266  
  2         127  
10 2     2   15  
  2         5  
  2         19  
11             around 'run' => sub {
12             my ($orig, $self) = (shift, shift);
13              
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 {
26             die "Interrupted.\n";
27             };
28              
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             }
46             };
47              
48             1;
49              
50              
51             =pod
52              
53             =encoding UTF-8
54              
55             =head1 NAME
56              
57             Devel::REPL::Plugin::Interrupt - Traps SIGINT to kill long-running lines
58              
59             =head1 VERSION
60              
61             version 1.003029
62              
63             =head1 DESCRIPTION
64              
65             By default L<Devel::REPL> exits on SIGINT (usually Ctrl-C). If you load this
66             module, SIGINT will be trapped and used to kill long-running commands
67             (statements) and also to kill the line being edited (like eg. BASH do). (You
68             can still use Ctrl-D to exit.)
69              
70             =head1 SUPPORT
71              
72             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Devel-REPL>
73             (or L<bug-Devel-REPL@rt.cpan.org|mailto:bug-Devel-REPL@rt.cpan.org>).
74              
75             There is also an irc channel available for users of this distribution, at
76             L<C<#devel> on C<irc.perl.org>|irc://irc.perl.org/#devel-repl>.
77              
78             =head1 AUTHOR
79              
80             Shawn M Moore, C<< <sartak at gmail dot com> >>
81              
82             =head1 COPYRIGHT AND LICENCE
83              
84             This software is copyright (c) 2007 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>).
85              
86             This is free software; you can redistribute it and/or modify it under
87             the same terms as the Perl 5 programming language system itself.
88              
89             =cut