File Coverage

blib/lib/Shell/Autobox.pm
Criterion Covered Total %
statement 40 40 100.0
branch 10 10 100.0
condition 6 9 66.6
subroutine 9 9 100.0
pod n/a
total 65 68 95.5


line stmt bran cond sub pod time code
1             package Shell::Autobox;
2              
3 2     2   143395 use strict;
  2         12  
  2         60  
4 2     2   10 use warnings;
  2         4  
  2         57  
5              
6 2     2   9 use base qw(autobox);
  2         4  
  2         1192  
7              
8 2     2   22058 use Carp qw(confess);
  2         6  
  2         101  
9 2     2   1123 use IPC::Run3 qw(run3);
  2         70088  
  2         127  
10              
11             # XXX this declaration must be on a single line
12             # https://metacpan.org/pod/version#How-to-declare()-a-dotted-decimal-version
13 2     2   18 use version; our $VERSION = version->declare('v2.0.1');
  2         4  
  2         15  
14              
15             sub import {
16 2     2   20 my $class = shift;
17 2         7 my $caller = (caller)[0];
18              
19 2         12 for my $program (@_) {
20             my $sub = sub {
21 19     19   6319 my ($input, @args) = @_;
22 19         239 my @command = ($program, @args);
23 19         153 my $command = join(' ', @command);
24 19 100 66     307 my $stdin = (defined($input) && ref($input) eq '') ? \$input : $input;
25              
26 19         174 run3(\@command, $stdin, \my $stdout, \my $stderr, {
27             return_if_system_error => 1, # don't die on error
28             });
29              
30 19 100 66     124580 my $error = (defined($stderr) && $stderr =~ /\S/) ? ": $stderr" : '';
31 19         243 my $message = "$command$error";
32              
33 19 100       483 if ($?) {
    100          
34 4         2448 confess "can't exec $message";
35             } elsif ($error) {
36 4         136 warn "error running $message";
37             }
38              
39 15 100 66     5851 return (defined($stdout) && length($stdout)) ? $stdout : '';
40 1         6 };
41              
42             {
43 2     2   591 no strict 'refs';
  2         5  
  2         208  
  1         3  
44 1         2 *{"$caller\::$program"} = $sub;
  1         5  
45             }
46             }
47              
48 2         20 $class->SUPER::import(SCALAR => $caller, ARRAY => $caller);
49             }
50              
51             1;
52              
53             __END__