.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "System::Command 3pm" .TH System::Command 3pm "2023-02-10" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" System::Command \- Object for running system commands .SH "VERSION" .IX Header "VERSION" version 1.122 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use System::Command; \& \& # invoke an external command, and return an object \& $cmd = System::Command\->new( @cmd ); \& \& # options can be passed as a hashref \& $cmd = System::Command\->new( @cmd, \e%option ); \& \& # $cmd is basically a hash, with keys / accessors \& $cmd\->stdin(); # filehandle to the process stdin (write) \& $cmd\->stdout(); # filehandle to the process stdout (read) \& $cmd\->stderr(); # filehandle to the process stdout (read) \& $cmd\->pid(); # pid of the child process \& \& # find out if the child process died \& if ( $cmd\->is_terminated() ) { \& # the handles are not closed yet \& # but $cmd\->exit() et al. are available if it\*(Aqs dead \& } \& \& # done! \& $cmd\->close(); \& \& # exit information \& $cmd\->exit(); # exit status \& $cmd\->signal(); # signal \& $cmd\->core(); # core dumped? (boolean) \& \& # cut to the chase \& my ( $pid, $in, $out, $err ) = System::Command\->spawn(@cmd); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" System::Command is a class that launches external system commands and return an object representing them, allowing to interact with them through their \f(CW\*(C`STDIN\*(C'\fR, \f(CW\*(C`STDOUT\*(C'\fR and \f(CW\*(C`STDERR\*(C'\fR handles. .SH "METHODS" .IX Header "METHODS" System::Command supports the following methods: .SS "new" .IX Subsection "new" .Vb 1 \& my $cmd = System::Command\->new( @cmd ) .Ve .PP Runs an external command using the list in \f(CW@cmd\fR. .PP If \f(CW@cmd\fR contains a hash reference, it is taken as an \fIoption\fR hash. .PP If several option hashes are passed to \f(CW\*(C`new()\*(C'\fR, they will be merged together with individual values being overridden by those (with the same key) from hashes that appear later in the list. .PP To allow subclasses to support their own set of options, unrecognized options are silently ignored. .PP The recognized keys are: .ie n .IP """cwd""" 4 .el .IP "\f(CWcwd\fR" 4 .IX Item "cwd" The \fIcurrent working directory\fR in which the command will be run. .ie n .IP """env""" 4 .el .IP "\f(CWenv\fR" 4 .IX Item "env" A hashref containing key / values to add to the command environment. .Sp If several option hashes define the \f(CW\*(C`env\*(C'\fR key, the hashes they point to will be merged into one (instead of the last one taking precedence). .Sp If a value is \f(CW\*(C`undef\*(C'\fR, the variable corresponding to the key will be \fIremoved\fR from the environment. .ie n .IP """input""" 4 .el .IP "\f(CWinput\fR" 4 .IX Item "input" A string that is send to the command's standard input, which is then closed. .Sp Using the empty string as \f(CW\*(C`input\*(C'\fR will close the command's standard input without writing to it. .Sp Using \f(CW\*(C`undef\*(C'\fR as \f(CW\*(C`input\*(C'\fR will not do anything. This behaviour provides a way to modify previous options populated by some other part of the program. .Sp On some systems, some commands may close standard input on startup, which will cause a \s-1SIGPIPE\s0 when trying to write to it. This will raise an exception. .ie n .IP """interactive""" 4 .el .IP "\f(CWinteractive\fR" 4 .IX Item "interactive" If true, the command will actually be run using the \*(L"system\*(R" in perlfunc builtin. If \f(CW\*(C`STDIN\*(C'\fR is not a terminal, the constructor will die. .Sp Not reaper object will be created, and the \f(CW\*(C`stdin\*(C'\fR, \f(CW\*(C`stdout\*(C'\fR and \&\f(CW\*(C`stderr\*(C'\fR filehandles will point to dummy closed handles. The \f(CW\*(C`exit\*(C'\fR, \&\f(CW\*(C`signal\*(C'\fR and \f(CW\*(C`core\*(C'\fR attributes will be correctly set. .Sp (Added in version 1.114.) .ie n .IP """setpgrp""" 4 .el .IP "\f(CWsetpgrp\fR" 4 .IX Item "setpgrp" By default, the spawned process is made the leader of its own process group using \f(CW\*(C`setpgrp( 0, 0 )\*(C'\fR (if possible). This enables sending a signal to the command and all its child processes at once: .Sp .Vb 2 \& # negative signal is sent to the process group \& kill \-SIGKILL, $cmd\->pid; .Ve .Sp Setting the \f(CW\*(C`setpgrp\*(C'\fR option to a false value disables this behaviour. .Sp (Added in version 1.110.) .ie n .IP """trace""" 4 .el .IP "\f(CWtrace\fR" 4 .IX Item "trace" The \f(CW\*(C`trace\*(C'\fR option defines the trace settings for System::Command. The \f(CW\*(C`SYSTEM_COMMAND_TRACE\*(C'\fR environment variable can be used to specify a global trace setting at startup. The environment variable overrides individual \f(CW\*(C`trace\*(C'\fR options. .Sp If \f(CW\*(C`trace\*(C'\fR or \f(CW\*(C`SYSTEM_COMMAND_TRACE\*(C'\fR contains an \f(CW\*(C`=\*(C'\fR character then what follows it is used as the name of the file to append the trace to. When using the \f(CW\*(C`trace\*(C'\fR option, it is recommended to use an absolute path for the trace file, in case the main program \f(CW\*(C`chdir()\*(C'\fR before calling System::Command. .Sp At trace level 1, only the command line is shown: .Sp .Vb 1 \& System::Command cmd[12834]: /usr/bin/git commit \-m "Test option hash in new()" .Ve .Sp Note: Command-line parameters containing whitespace will be properly quoted. .Sp At trace level 2, the options values are shown: .Sp .Vb 3 \& System::Command opt[12834]: cwd => "/tmp/kHkPUBIVWd" \& System::Command opt[12834]: fatal => {128 => 1,129 => 1} \& System::Command opt[12834]: git => "/usr/bin/git" .Ve .Sp Note: The \f(CW\*(C`fatal\*(C'\fR and \f(CW\*(C`git\*(C'\fR options in the example above are actually used by Git::Repository to determine the command to be run, and ignored by System::Command. References are dumped using Data::Dumper. .Sp At trace level 3, the content of the \f(CW\*(C`env\*(C'\fR option is also listed: .Sp .Vb 2 \& System::Command env[12834]: GIT_AUTHOR_EMAIL => "author\e@example.com" \& System::Command env[12834]: GIT_AUTHOR_NAME => "Example author" .Ve .Sp If the command cannot be spawned, the trace will show \f(CW\*(C`!\*(C'\fR instead of the pid: .Sp .Vb 1 \& System::Command cmd[!]: does\-not\-exist .Ve .Sp (Added in version 1.108.) .IP "exit" 4 .IX Item "exit" .PD 0 .IP "core" 4 .IX Item "core" .IP "signal" 4 .IX Item "signal" .PD The above three options can be set to point to a reference to a scalar, which will be automatically updated when the command is terminated. See the \*(L"Accessors\*(R" section for details about what the attributes of the same name mean. .Sp (Added in version 1.114.) .PP The System::Command object returned by \f(CW\*(C`new()\*(C'\fR has a number of attributes defined (see below). .SS "close" .IX Subsection "close" .Vb 1 \& $cmd\->close; .Ve .PP Close all pipes to the child process, collects exit status, etc. and defines a number of attributes (see below). .PP Returns the invocant, so one can do things like: .PP .Vb 1 \& my $exit = $cmd\->close\->exit; .Ve .SS "is_terminated" .IX Subsection "is_terminated" .Vb 1 \& if ( $cmd\->is_terminated ) {...} .Ve .PP Returns a true value if the underlying process was terminated. .PP If the process was indeed terminated, collects exit status, etc. and defines the same attributes as \f(CW\*(C`close()\*(C'\fR, but does \fBnot\fR close all pipes to the child process. .SS "spawn" .IX Subsection "spawn" .Vb 1 \& my ( $pid, $in, $out, $err ) = System::Command\->spawn(@cmd); .Ve .PP This shortcut method calls \f(CW\*(C`new()\*(C'\fR (and so accepts options in the same manner) and directly returns the \f(CW\*(C`pid\*(C'\fR, \f(CW\*(C`stdin\*(C'\fR, \f(CW\*(C`stdout\*(C'\fR and \f(CW\*(C`stderr\*(C'\fR attributes, in that order. .PP (Added in version 1.01.) .SS "loop_on" .IX Subsection "loop_on" .Vb 4 \& $cmd\->loop_on( \& stdout => sub { ... }, \& stderr => sub { ... }, \& ); .Ve .PP This method calls the corresponding code references with each line produced on the standard output and errput of the command. .PP If the \f(CW\*(C`stdout\*(C'\fR or \f(CW\*(C`stderr\*(C'\fR argument is not given, the default is to silently drop the data for \f(CW\*(C`stdout\*(C'\fR, and to pass through (to \s-1STDERR\s0) the data for \f(CW\*(C`stderr\*(C'\fR. To prevent any processing, pass a false value to the parameter. .PP For example, the following line will silently run the command to completion: .PP .Vb 1 \& $cmd\->loop_on( stderr => \*(Aq\*(Aq ); .Ve .PP The method blocks until the command is completed (or rather, until its output and errput handles have been closed), or until one of the callbacks returns a false value. .PP Data is read using readline, which depends on \f(CW$/\fR for its definition of a \*(L"line\*(R". To that effect, the method takes a third optional argument, \f(CW\*(C`input_record_separator\*(C'\fR, which sets the value for \&\f(CW$/\fR for the duration of the call. .PP \&\fICaveat Emptor\fR: since \f(CW\*(C`loop_on\*(C'\fR is line-based, it may \fBblock\fR if either output or errput sends incomplete lines (e.g. if the command is some sort of interactive shell with a prompt). .PP The return value is true if the command exited with status 0, and false otherwise (i.e. the Unix traditional definition of success). .PP (Added in version 1.117.) .SS "Accessors" .IX Subsection "Accessors" The attributes of a System::Command object are also accessible through a number of accessors. .PP The object returned by \f(CW\*(C`new()\*(C'\fR will have the following attributes defined: .IP "cmdline" 4 .IX Item "cmdline" Return the command-line actually executed, as a list of strings. .IP "options" 4 .IX Item "options" The merged list of options used to run the command. .IP "pid" 4 .IX Item "pid" The \s-1PID\s0 of the underlying command. .IP "stdin" 4 .IX Item "stdin" A filehandle opened in write mode to the child process' standard input. .IP "stdout" 4 .IX Item "stdout" A filehandle opened in read mode to the child process' standard output. .IP "stderr" 4 .IX Item "stderr" A filehandle opened in read mode to the child process' standard error output. .PP Regarding the handles to the child process, note that in the following code: .PP .Vb 1 \& my $fh = System::Command\->new( @cmd )\->stdout; .Ve .PP \&\f(CW$fh\fR is opened and points to the output handle of the child process, while the anonymous System::Command object has been destroyed. Once \&\f(CW$fh\fR is destroyed, the subprocess will be reaped, thus avoiding zombies. (System::Command::Reaper undertakes this process.) .PP After the call to \f(CW\*(C`close()\*(C'\fR or after \f(CW\*(C`is_terminated()\*(C'\fR returns true, the following attributes will be defined (note that the accessors always run \f(CW\*(C`is_terminated()\*(C'\fR, to improve their chance of getting a value if the process just finished): .IP "exit" 4 .IX Item "exit" The exit status of the underlying command. .IP "signal" 4 .IX Item "signal" The signal, if any, that killed the command. .IP "core" 4 .IX Item "core" A boolean value indicating if the command dumped core. .PP Even when not having a reference to the System::Command object any more, it's still possible to get the \f(CW\*(C`exit\*(C'\fR, \f(CW\*(C`core\*(C'\fR or \f(CW\*(C`signal\*(C'\fR values, using the options of the same name: .PP .Vb 1 \& my $fh = System::Command\->new( @cmd, { exit => \emy $exit } )\->stdout; .Ve .PP Once the command is terminated, the \f(CW$exit\fR variable will contain the value that would have been returned by the \f(CW\*(C`exit()\*(C'\fR method. .SH "CAVEAT EMPTOR" .IX Header "CAVEAT EMPTOR" Note that System::Command uses \f(CW\*(C`waitpid()\*(C'\fR to catch the status information of the child processes it starts. This means that if your code (or any module you \f(CW\*(C`use\*(C'\fR) does something like the following: .PP .Vb 1 \& local $SIG{CHLD} = \*(AqIGNORE\*(Aq; # reap child processes .Ve .PP System::Command will not be able to capture the \f(CW\*(C`exit\*(C'\fR, \f(CW\*(C`signal\*(C'\fR and \f(CW\*(C`core\*(C'\fR attributes. It will instead set all of them to the impossible value \f(CW\*(C`\-1\*(C'\fR, and display the warning \&\f(CW\*(C`Child process already reaped, check for a SIGCHLD handler\*(C'\fR. .PP To silence this warning (and accept the impossible status information), load System::Command with: .PP .Vb 1 \& use System::Command \-quiet; .Ve .PP It is also possible to more finely control the warning by setting the \f(CW$System::Command::QUIET\fR variable (the warning is not emitted if the variable is set to a true value). .PP If the subprocess started by System::Command has a short life expectancy, and no other child process is expected to die during that time, you could even disable the handler locally (use at your own risks): .PP .Vb 5 \& { \& local $SIG{CHLD}; \& my $cmd = System::Command\->new(@cmd); \& ... \& } .Ve .SH "AUTHOR" .IX Header "AUTHOR" Philippe Bruhat (BooK), \f(CW\*(C`\*(C'\fR .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" Thanks to Alexis Sukrieh (\s-1SUKRIA\s0) who, when he saw the description of Git::Repository::Command during my talk at \s-1OSDC\s0.fr 2010, asked why it was not an independent module. This module was started by taking out of Git::Repository::Command 1.08 the parts that weren't related to Git. .PP Thanks to Christian Walde (\s-1MITHALDU\s0) for his help in making this module work better under Win32. .PP The System::Command::Reaper class was added after the addition of Git::Repository::Command::Reaper in Git::Repository::Command 1.11. It was later removed from System::Command version 1.03, and brought back from the dead to deal with the zombie apocalypse in version 1.106. The idea of a reaper class comes from Vincent Pit. .PP Thanks to Tim Bunce for using Git::Repository and making many suggestions based on his use and needs. Most of them turned into improvement for System::Command instead, once we figured out that the more general feature idea really belonged there. .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests to \f(CW\*(C`bug\-system\-command at rt.cpan.org\*(C'\fR, or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .SH "SUPPORT" .IX Header "SUPPORT" You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc System::Command .Ve .PP You can also look for information at: .IP "\(bu" 4 \&\s-1RT: CPAN\s0's request tracker .Sp .IP "\(bu" 4 AnnoCPAN: Annotated \s-1CPAN\s0 documentation .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Ratings .Sp .IP "\(bu" 4 Search \s-1CPAN\s0 .Sp .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2010\-2016 Philippe Bruhat (BooK). .SH "LICENSE" .IX Header "LICENSE" This program is free software; you can redistribute it and/or modify it under the terms of either: the \s-1GNU\s0 General Public License as published by the Free Software Foundation; or the Artistic License. .PP See for more information.