POSIX::Run::Capture - run command and capture its output


  use POSIX::Run::Capture;

  $obj = new POSIX::Run::Capture(argv => [ $command, @args ],
                                 program => $prog,
                                 stdin => $fh_or_string,
                                 stdout => $ref_or_string,
                                 stderr => $ref_or_string,
                                 timeout => $n);

  $num = $obj->errno;
  $num = $obj->status;
  $num = $obj->length($chan);
  $num = $obj->nlines($chan);

  $str = $obj->next_line($chan);
  $aref = $obj->get_lines($chan);


  $aref = $obj->argv;
  $str = $obj->program
  $num = $obj->timeout;


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,
                          stdin => $fh_or_string,
                          stdout => $ref_or_string,
                          stderr => $ref_or_string,
                          timeout => $n)

When named arguments are used, the following keywords are allowed:


Defines the command (C argv[0]) and its arguments. In the absense of program argument, $argv[0] will be run.


Sets the pathname of binary file to run.

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.


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_input, and set_timeout.

Monitors and redirections can be defined only when creating the object.

Modifying the object.

The following methods modify the object:


Sets the pathname of the command to run.


Sets runtime timeout, in seconds.


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.


The following accessors return parameters associated with the object:


Returns a reference to the argv array associated with the object.


Returns the pathname of the executable program.


Returns the runtime timeout or 0 if no timeout is set.

Running the command


Runs the program. Returns 1 on successful termination, 0 otherwise.


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:


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;


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);


Returns total length in bytes of data captured in output channel $chan.


Returns next line from the captured channel $chan.


Returns a reference to an array of lines captured from channel $chan.


Rewinds the captured channel $chan to its beginning.


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.


Sergey Poznyakoff, <>


Copyright (C) 2017-2020 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 <>.