line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sys::Cmd::Template; |
2
|
1
|
|
|
1
|
|
57946
|
use strict; |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
27
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
4
|
1
|
|
|
1
|
|
18
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
5
|
1
|
|
|
1
|
|
4
|
use Carp qw/croak confess/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
52
|
|
6
|
1
|
|
|
1
|
|
864
|
use Exporter::Tidy default => [qw/cmd_template/]; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
6
|
|
7
|
1
|
|
|
1
|
|
383
|
use File::Spec::Functions qw/splitdir/; |
|
1
|
|
|
|
|
684
|
|
|
1
|
|
|
|
|
51
|
|
8
|
1
|
|
|
1
|
|
348
|
use File::Which; |
|
1
|
|
|
|
|
788
|
|
|
1
|
|
|
|
|
47
|
|
9
|
1
|
|
|
1
|
|
384
|
use Sys::Cmd; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
10
|
1
|
|
|
1
|
|
44
|
use Sys::Cmd::Mo qw/is default/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
12
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.99.0_2'; |
13
|
|
|
|
|
|
|
our $CONFESS; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub cmd_template { |
16
|
1
|
|
|
1
|
1
|
68
|
my @cmd = grep { ref $_ ne 'HASH' } @_; |
|
1
|
|
|
|
|
4
|
|
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
|
|
2
|
my $bin = $cmd[0]; |
19
|
1
|
50
|
|
|
|
26
|
defined $bin || confess '$cmd must be defined'; |
20
|
|
|
|
|
|
|
|
21
|
1
|
50
|
33
|
|
|
35
|
if ( !-f $bin and splitdir($bin) < 2 ) { |
22
|
1
|
|
|
|
|
13
|
$cmd[0] = which($bin); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
272
|
my $opts = ( grep { ref $_ eq 'HASH' } @_ )[0]; |
|
1
|
|
|
|
|
4
|
|
26
|
|
|
|
|
|
|
|
27
|
1
|
50
|
|
|
|
3
|
my %args = $opts ? %$opts : (); |
28
|
1
|
|
|
|
|
3
|
$args{cmd} = \@cmd; |
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
|
|
16
|
return Sys::Cmd::Template->new(%args); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has 'cmd' => ( |
34
|
|
|
|
|
|
|
is => 'rw', |
35
|
|
|
|
|
|
|
isa => sub { ref $_[0] eq 'ARRAY' || confess "cmd must be ARRAYREF" }, |
36
|
|
|
|
|
|
|
default => sub { [] }, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has 'dir' => ( |
40
|
|
|
|
|
|
|
is => 'rw', |
41
|
|
|
|
|
|
|
predicate => 'have_dir', |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
has 'encoding' => ( |
45
|
|
|
|
|
|
|
is => 'rw', |
46
|
|
|
|
|
|
|
predicate => 'have_encoding', |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
has 'env' => ( |
50
|
|
|
|
|
|
|
is => 'rw', |
51
|
|
|
|
|
|
|
isa => sub { ref $_[0] eq 'HASH' || confess "env must be HASHREF" }, |
52
|
|
|
|
|
|
|
predicate => 'have_env', |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
has 'input' => ( |
56
|
|
|
|
|
|
|
is => 'rw', |
57
|
|
|
|
|
|
|
predicate => 'have_input', |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub run { |
61
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
62
|
0
|
|
|
|
|
|
my $proc = $self->spawn(@_); |
63
|
0
|
|
|
|
|
|
my @out = $proc->stdout->getlines; |
64
|
0
|
|
|
|
|
|
my @err = $proc->stderr->getlines; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
$proc->close; |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
if ( $proc->exit != 0 ) { |
69
|
0
|
0
|
|
|
|
|
confess( join( '', @err ) . 'Command exited with value ' . $proc->exit ) |
70
|
|
|
|
|
|
|
if $CONFESS; |
71
|
0
|
|
|
|
|
|
croak( join( '', @err ) . 'Command exited with value ' . $proc->exit ); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
if (wantarray) { |
75
|
0
|
|
|
|
|
|
return @out; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
0
|
|
|
|
|
|
return join( '', @out ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub runx { |
83
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
84
|
0
|
|
|
|
|
|
my $proc = $self->spawn(@_); |
85
|
0
|
|
|
|
|
|
my @out = $proc->stdout->getlines; |
86
|
0
|
|
|
|
|
|
my @err = $proc->stderr->getlines; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
$proc->close; |
89
|
|
|
|
|
|
|
|
90
|
0
|
0
|
|
|
|
|
if ( $proc->exit != 0 ) { |
91
|
0
|
0
|
|
|
|
|
confess( join( '', @err ) . 'Command exited with value ' . $proc->exit ) |
92
|
|
|
|
|
|
|
if $CONFESS; |
93
|
0
|
|
|
|
|
|
croak( join( '', @err ) . 'Command exited with value ' . $proc->exit ); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
0
|
0
|
|
|
|
|
if (wantarray) { |
97
|
0
|
|
|
|
|
|
return @out, @err; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
0
|
|
|
|
|
|
return join( '', @out, @err ); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub spawn { |
105
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
106
|
0
|
|
|
|
|
|
my %args = ( cmd => [ @{ $self->cmd }, grep { ref $_ ne 'HASH' } @_ ], ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
0
|
|
0
|
|
|
|
my $opts = ( grep { ref $_ eq 'HASH' } @_ )[0] || {}; |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if ( exists $opts->{dir} ) { |
|
|
0
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
$args{dir} = $opts->{dir}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
elsif ( $self->have_dir ) { |
114
|
0
|
|
|
|
|
|
$args{dir} = $self->dir; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
0
|
0
|
|
|
|
|
if ( exists $opts->{encoding} ) { |
|
|
0
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
$args{encoding} = $opts->{encoding}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
elsif ( $self->have_encoding ) { |
121
|
0
|
|
|
|
|
|
$args{encoding} = $self->encoding; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
|
|
|
|
if ( $self->have_env ) { |
125
|
0
|
|
|
|
|
|
$args{env} = { %{ $self->env } }; |
|
0
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
if ( exists $opts->{env} ) { |
129
|
0
|
|
|
|
|
|
while ( my ( $key, $val ) = each %{ $opts->{env} } ) { |
|
0
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
$args{env}->{$key} = $val; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
if ( exists $opts->{input} ) { |
|
|
0
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
$args{input} = $opts->{input}; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
elsif ( $self->have_input ) { |
138
|
0
|
|
|
|
|
|
$args{input} = $self->input; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
return Sys::Cmd->new(%args); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
__END__ |