-
Notifications
You must be signed in to change notification settings - Fork 2
/
README
235 lines (177 loc) · 8.24 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
NAME
Sys::Cmd - run a system command or spawn a system processes
VERSION
0.99.1_1 (yyyy-mm-dd)
SYNOPSIS
use Sys::Cmd qw/run spawn/;
# Get command output, raise exception on failure:
$output = run(@cmd);
# Feed command some input, get output as lines,
# raise exception on failure:
@output = run( @cmd, { input => 'feedme' } );
# Spawn and interact with a process somewhere else:
$proc = spawn(
@cmd,
{
dir => '/',
encoding => 'encoding(iso-8859-3)'
},
);
while ( my $line = $proc->stdout->getline ) {
$proc->stdin->print("thanks\n");
}
my @errors = $proc->stderr->getlines;
$proc->close(); # Finished talking to file handles
$proc->wait_child(); # Cleanup
# read exit information
$proc->exit(); # exit status
$proc->signal(); # signal
$proc->core(); # core dumped? (boolean)
DESCRIPTION
Sys::Cmd lets you run system commands and capture their output, or
spawn and interact with a system process through its "STDIN",
"STDOUT", and "STDERR" file handles. The following functions are
exported on demand by this module:
run( @cmd, [\%opt] ) => $output | @output
Execute @cmd and return what the command sent to its "STDOUT",
raising an exception in the event of error. In array context
returns a list instead of a plain string.
The first element of @cmd determines what/how things are run:
* If it is a relative file name it is executed directly using
Proc::Spawn.
* If it is a CODE reference (subroutine) Sys::Cmd forks before
running it in the child process. This is not supported on
Win32.
* Everything else is looked up using File::Which and then
executed with Proc::Spawn.
The command input and environment can be modified with an
optional hashref containing the following key/values:
dir The working directory the command will be run in.
encoding
An string value identifying the encoding of the input/output
file-handles. Defaults to 'utf8'.
env A hashref containing key/values to be added to the current
environment at run-time. If a key has an undefined value
then the key is removed from the environment altogether.
input
A string which is fed to the command via its standard input,
which is then closed.
runx( @cmd, [\%opt] ) => $outerrput | @outerrput
The same as the "run" function but with the command's "STDERR"
output appended to the "STDOUT" output.
spawn( @cmd, [\%opt] ) => Sys::Cmd
Return a Sys::Cmd object (documented below) representing the
process running @cmd, with attributes set according to the
optional \%opt hashref. The first element of @cmd determines the
execution method just like the "run()" function.
Sys::Cmd objects can of course be created using the standard "new"
constructor if you prefer that to the "spawn" function:
$proc = Sys::Cmd->new(
cmd => \@cmd,
dir => '/',
env => { SOME => 'VALUE' },
enc => 'iso-8859-3',
input => 'feedme',
on_exit => sub {
my $proc = shift;
print $proc->pid .' exited with '. $proc->exit;
},
);
Note that Sys::Cmd objects created this way will not lookup the
command using File::Which the way the "run", "runx" and "spawn"
functions do. CODE references in $cmd[0] are however still
recognized and forked off.
Sys::Cmd uses Log::Any "debug" calls for logging purposes. An easy
way to see the output is to add "use Log::Any::Adapter 'Stdout'" in
your program.
CONSTRUCTOR
new(%args) => Sys::Cmd
Spawns a process based on %args. %args must contain at least a
"cmd" value, and optionally "encoding", "env", "dir" and "input"
values as defined as attributes below.
If an "on_exit" subref argument is provided it will be called by
the "wait_child" method, which can either be called manually or
will be automatically called when the object is destroyed.
ATTRIBUTES
All attributes are read-only.
cmd An array ref containing the command or CODE reference (UNIX
only) and its arguments.
dir The working directory the command will be run in.
encoding
An string value identifying the encoding of the input/output
file-handles. Defaults to 'utf8'.
env A hashref containing key/values to be added to the current
environment at run-time. If a key has an undefined value then
the key is removed from the environment altogether.
input
A string which is fed to the command via its standard input,
which is then closed. This is a shortcut for printing to, and
closing the command's *stdin* file-handle. An empty string will
close the command's standard input without writing to it. On
some systems, some commands may close standard input on startup,
which will cause a SIGPIPE when trying to write to it for which
Sys::Cmd will warn.
pid The command's process ID.
stdin
The command's *STDIN* file handle, based on IO::Handle so you
can call print() etc methods on it. Autoflush is automatically
enabled on this handle.
stdout
The command's *STDOUT* file handle, based on IO::Handle so you
can call getline() etc methods on it.
stderr
The command's *STDERR* file handle, based on IO::Handle so you
can call getline() etc methods on it.
exit
The command's exit value, shifted by 8 (see "perldoc -f
system"). Set by "wait_child()".
signal
The signal number (if any) that terminated the command,
bitwise-added with 127 (see "perldoc -f system"). Set by
"wait_child()".
core
A boolean indicating the process core was dumped. Set by
"wait_child()".
METHODS
cmdline => @list | $str
In array context returns a list of the command and its
arguments. In scalar context returns a string of the command and
its arguments joined together by spaces.
close()
Close all filehandles to the child process. Note that file
handles will automaticaly be closed when the Sys::Cmd object is
destroyed. Annoyingly, this means that in the following example
$fh will be closed when you tried to use it:
my $fh = Sys::Cmd->new( %args )->stdout;
So you have to keep track of the Sys::Cmd object manually.
wait_child() -> $exit_value
Wait for the child to exit using waitpid
<http://perldoc.perl.org/functions/waitpid.html>, collect the
exit status and return it. This method sets the *exit*, *signal*
and *core* attributes and will also be called automatically when
the Sys::Cmd object is destroyed.
SEE ALSO
Sys::Cmd::Template
ALTERNATIVES
AnyEvent::Run, AnyEvent::Util, Argv, Capture::Tiny, Child,
Forks::Super, IO::Pipe, IPC::Capture, IPC::Cmd,
IPC::Command::Multiplex, IPC::Exe, IPC::Open3, IPC::Open3::Simple,
IPC::Run, IPC::Run3, IPC::RunSession::Simple, IPC::ShellCmd,
IPC::System::Simple, POE::Pipe::TwoWay, Proc::Background,
Proc::Fork, Proc::Spawn, Spawn::Safe, System::Command
SUPPORT
This distribution is managed via github:
https://github.com/mlawren/sys-cmd/tree/devel
This distribution follows the semantic versioning model:
http://semver.org/
Code is tidied up on Git commit using githook-perltidy:
http://github.com/mlawren/githook-perltidy
AUTHOR
Mark Lawrence <[email protected]>, based heavily on
Git::Repository::Command by Philippe Bruhat (BooK).
COPYRIGHT AND LICENSE
Copyright 2011-2021 Mark Lawrence <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.