File Coverage

lib/LiBot/Handler/PerldocJP.pm
Criterion Covered Total %
statement 21 65 32.3
branch 0 14 0.0
condition 0 6 0.0
subroutine 7 9 77.7
pod 0 1 0.0
total 28 95 29.4


line stmt bran cond sub pod time code
1             package LiBot::Handler::PerldocJP;
2 1     1   1562 use strict;
  1         3  
  1         35  
3 1     1   4 use warnings;
  1         3  
  1         31  
4 1     1   4 use utf8;
  1         3  
  1         6  
5 1     1   30 use Encode qw(decode_utf8 encode_utf8);
  1         1  
  1         53  
6 1     1   7355 use Text::Shorten qw(shorten_scalar);
  1         7600  
  1         156  
7              
8 1     1   15 use Mouse;
  1         4  
  1         15  
9              
10             has limit => (
11             is => 'ro',
12             default => sub { 120 },
13             );
14              
15 1     1   844 no Mouse;
  1         4  
  1         8  
16              
17             sub init {
18 0     0 0   my ($self, $bot) = @_;
19              
20             $bot->register(
21             qr/^perldoc\s+(.*)/ => sub {
22 0     0     my ( $cb, $event, $arg ) = @_;
23              
24 0           pipe( my $rh, my $wh );
25              
26 0           my $pid = fork();
27 0   0       $pid // do {
28 0           close $rh;
29 0           close $wh;
30 0           die $!;
31             };
32              
33 0 0         if ($pid) {
34              
35             # parent
36 0           close $wh;
37              
38 0           my $ret = '';
39 0           my $sweep;
40             my $timer = AE::timer(
41             10, 0,
42             sub {
43 0           kill 9, $pid;
44             }
45 0           );
46 0           my $child;
47             $child = AE::child(
48             $pid,
49             sub {
50 0           undef $timer;
51 0           $ret =~ s/NAME\n//;
52 0           $ret =~ s/\nDESCRIPTION\n/\n/;
53 0           $ret = shorten_scalar( decode_utf8($ret), $self->limit );
54 0 0         if ( $arg =~ /\A[\$\@\%]/ ) {
    0          
55 0           $ret .= "\n\nhttp://perldoc.jp/perlvar";
56             }
57             elsif ( $arg =~ /\A-[a-z]\s+(.+)/ ) {
58 0           $ret .= "\n\nhttp://perldoc.jp/$1";
59             }
60             else {
61 0           $ret .= "\n\nhttp://perldoc.jp/$arg";
62             }
63 0           $cb->($ret);
64 0           undef $sweep;
65 0           undef $child;
66             }
67 0           );
68             $sweep = AE::io(
69             $rh, 0,
70             sub {
71 0           $ret .= scalar(<$rh>);
72             }
73 0           );
74             }
75             else {
76             # child
77 0           close $rh;
78              
79 0 0         open STDERR, '>&', $wh
80             or die "failed to redirect STDERR to logfile";
81 0 0         open STDOUT, '>&', $wh
82             or die "failed to redirect STDOUT to logfile";
83              
84 0           eval {
85 0           require Pod::PerldocJp;
86 0           local @ARGV = split /\s+/, $arg;
87 0 0 0       if ( @ARGV == 1 && $ARGV[0] =~ /^[\$\@\%]/ ) {
88 0           unshift @ARGV, '-v';
89             }
90 0           unshift @ARGV, '-J';
91 0           @ARGV = map { encode_utf8($_) } @ARGV;
  0            
92 0           Pod::PerldocJp->run();
93             };
94 0 0         warn $@ if $@;
95              
96 0           exit 0;
97             }
98             }
99 0           );
100             }
101              
102             1;
103             __END__