Capture::SystemIO - system() capture stderr, stdout, and signals.
use strict; use warnings; use Capture::SystemIO qw(cs_system); # Run a command; chuck the output. cs_system("dd if=/dev/zero of=/dev/sda"); # Run a command and capture STDIN and STDOUT; do something with it. my ($stdin, $stdout) = cs_system("ls -l /bin"); print "ls said: $$stdout \n\n ls also mentioned: $$stderr"; # Run a command and check for errors. eval { cs_system("sudo rm -rf /"); }; if (my $e = Exception::Class->Caught("Capture::SystemIO::Interrupt")) { print "Keyboard interrupt. Signal: " $e->signal(); exit(); } elsif (my $e = Exception::Class->Caught("Capture::SystemIO::Error")) { print "Stderr: ". $e->stderr() ."Stdout: ". $e->stdout() ."Return code: ". $return_code ."Command: ". $command } elsif (my $e = Exception::Class->Caught()) { print "Some other error". $e->stderr(); $e->rethrow(); }
Runs a system command from within Perl and captures both STDOUT and STDERR;
provides exception based interface to SIGINT and SIGQUIT;
provides exceptions for non-zero return values.
none
cs_system
This is a wrapper for system() that uses Capture::Tiny::Capture() along with bits and pieces of IO::CaptureOutput, to capture both STDOUT and STDERR. It also checks the return value of system() and throws an exception if the command terminated unexpectedly because of a signal or the command exited with a non-zero result to indicate failure. In which case, the captured STDOUT and STDERR are contained within the exception object.
When used in list context, references to the captured STDOUT and STDERR are returned. In scalar context, however, only numeric exit code for the command is returned.
my ($$stdout,$stderr) = cs_system("true"); my ($return) = cs_system("true");
See perfunc system for details
The return value depends on the context in which cs_system was called
The return code from the call to system
References to the captured stderr and stdout
Thrown if the subprocess terminated as a result of either SIGINT or SIGQUIT
Thrown if the subprocess terminated as a result of another signal
Thrown if the return value of the subprocess is non-zero
my $stderr = $e->stderr();
my $stdout = $e->stdout();
my $return_code = $e->return_code();
my $command = $e->command();
my $signal = $e->signal();
The name signal that caused the subprocess to terminate
my $signal_number = $e->signal_no();
The numerical signal that caused the subprocess to terminate
Standard output captured before termination
Returns the Standard error output captured before termination of the subprocess
The name of the signal that caused the subprocess to terminate, if known.
move pre-condition checks from test suite to Makefile.PL
write more tests
test on more systems/platforms
Capture::Tiny, Exception::Class, IO::CaptureOutput, "system" in perlfunc, signal(7), "SIGNAL" in Posix
Rudolf Lippan <rlippan@kolkhoz.org>
You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
Copyright (c) 2008 - 2010, Remote Linux, Inc. All rights reserved.
Portions Copyright (c) 2009-2010, Public Karma, Inc.
Portions lifted from IO::CaptureOutput; copyright belongs to the respective authors.
To install Capture::SystemIO, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Capture::SystemIO
CPAN shell
perl -MCPAN -e shell install Capture::SystemIO
For more information on module installation, please visit the detailed CPAN module installation guide.