NAME
POSIX::Run::Capture - run command and capture its output
SYNOPSIS
use POSIX::Run::Capture;
$obj = new POSIX::Run::Capture(argv => [ $command, @args ],
program => $prog,
env => [ @environment ],
stdin => $fh_or_string,
stdout => $ref_or_string,
stderr => $ref_or_string,
timeout => $n);
$obj->run;
$num = $obj->errno;
$num = $obj->status;
$num = $obj->length($chan);
$num = $obj->nlines($chan);
$str = $obj->next_line($chan);
$aref = $obj->get_lines($chan);
$obj->rewind($chan)
$obj->set_argv(@argv);
$obj->set_program($prog);
$obj->set_env(@argv);
$obj->set_timeout($n);
$obj->set_input($fh_or_string);
$aref = $obj->argv;
$str = $obj->program
$aref = $obj->env;
$num = $obj->timeout;
DESCRIPTION
Runs an external command and captures its output. Both standard error and output can be captured. Standard input can be supplied as either a filehandle or a string. Upon exit, the captured streams can be accessed line by line or in one chunk. Callback routines can be supplied that will be called for each complete line of output read, providing a way for synchronous processing.
This module is for those who value performance and effectiveness over portability. As its name suggests, it can be used only on POSIX systems.
new POSIX::Run::Capture
Creates a new capture object. There are three possible invocation modes.
new POSIX::Run::Capture(argv => [ $command, @args ],
program => $prog,
env => [ @environment ],
stdin => $fh_or_string,
stdout => $ref_or_string,
stderr => $ref_or_string,
timeout => $n)
When named arguments are used, the following keywords are allowed:
- argv
-
Defines the command (C argv[0]) and its arguments. In the absense of program argument, $argv[0] will be run.
- program
-
Sets the pathname of binary file to run.
- env
-
Defines execution environment. By default, environment variables are inherited from the calling process.
- stdin or input
-
Supplies standard input for the command. The argument can be a string or a file handle.
- stdout => $coderef
-
Sets the line monitor function for standard output. Line monitor is invoked each time a complete line is read, or the EOF is hit on the standard output. The acquired line is passed to the monitor as its argument. The following example monitor function prints its argument to STDOUT:
sub stdout_monitor { my $line = shift; print $line; }
Notice that the last line read can lack the teminating newline character.
- stdout => FH
-
Redirect standard output to file handle FH. Obviously, the handle should be writable.
- stdout => NAME
-
Capture standard output and write it to the file NAME. If the file exists, it will be truncated. Otherwise, it will be created with permissions of 0666 modified by the process' "umask" value.
- stderr => $arg
-
Sets the line monitor function for standard error or redirects it to the file handle or file, depending on the type of $arg (CODE reference, GLOB or scalar string). For details, see the description of stdout above.
- timeout
-
Sets execution timeout, in seconds. If the program takes longer than $n seconds to terminate, it will be forcibly terminated (by sending the SIGKILL signal).
new POSIX::Run::Capture([ $command, @args ]);
A simplified way of creating the object, equivalent to
new POSIX::Run::Capture(argv => [ $command, @args ]);
new POSIX::Run::Capture;
Crates an empty capture object.
Whatever constructor is used, the necessary parameters can be set or changed later, using set_argv, set_program, set_env, set_input, and set_timeout.
Monitors and redirections can be defined only when creating the object.
Modifying the object.
The following methods modify the object:
$obj->set_argv(@argv)
Set arguments array.
$obj->set_program($prog)
Sets the pathname of the command to run.
$obj->set_env(@env)
Set environment variables.
$obj->set_timeout($n)
Sets runtime timeout, in seconds.
$obj->set_input($fh_or_string)
Sets standard input for the program. Argument must be either a filehandle open for reading or a string. The filehandle will be repositioned to its beginning prior to use.
Accessors
The following accessors return parameters associated with the object:
$obj->argv
Returns a reference to the argv array associated with the object.
$obj->program
Returns the pathname of the executable program.
$obj->env
Returns a reference to the array of environment variables.
$obj->timeout
Returns the runtime timeout or 0 if no timeout is set.
Running the command
$obj->run
Runs the program. Returns 1 on successful termination, 0 otherwise.
$obj->errno
If the last call to run returned false, this method returns the value of the system error number (the C errno variable).
Upon successful return from $obj->run, the following accessors can be used:
$obj->status
Returns the termination status of the program. Use the macros from POSIX :sys_wait_h to analyze it. E.g.:
use POSIX qw(:sys_wait_h);
if ($obj->run()) {
if (WIFEXITED($obj->status)) {
print "program "
. $obj->program
. " terminated with code "
. WEXITSTATUS($obj->status);
} elsif (WIFSIGNALED($self->status)) {
print "program "
. $obj->program
. " terminated on signal "
. WTERMSIG($obj->status);
} else {
print "program "
. $obj->program
. " terminated with unrecogized code "
. $obj->status;
}
}
$obj->nlines($chan)
Returns number of lines saved in output channel $chan (1 for stdout or 2 for stderr). You can also use symbolic constants SD_STDOUT and SD_STDERR, if you require the module as
use POSIX::Run::Capture qw(:std);
$obj->length($chan)
Returns total length in bytes of data captured in output channel $chan.
$obj->next_line($chan)
Returns next line from the captured channel $chan.
$obj->get_lines($chan)
Returns a reference to an array of lines captured from channel $chan.
$obj->rewind($chan)
Rewinds the captured channel $chan to its beginning.
EXPORT
None by default. Use :std or :all to export the constants SD_STDOUT and SD_STDERR, which correspond to the numbers of standard output and error channels.
AUTHOR
Sergey Poznyakoff, <gray@gnu.org>
COPYRIGHT AND LICENSE
Copyright (C) 2017-2024 by Sergey Poznyakoff
This library 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.
It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this library. If not, see <http://www.gnu.org/licenses/>.