Capture::Tiny - Capture STDOUT and STDERR from Perl, XS or external programs
This documentation describes version 0.02.
use Capture::Tiny qw/capture tee/; ($stdout, $stderr) = capture { # your code here }; ($stdout, $stderr) = tee { # your code here };
Capture::Tiny provides a simple, portable way to capture anything sent to STDOUT or STDERR, regardless of whether it comes from Perl, from XS code or from an external program. Optionally, output can be teed so that it is captured while being passed through to the original handles. Yes, it even works on Windows. Stop guessing which of a dozen capturing modules to use in any particular situation and just use this one.
This module was heavily inspired by IO::CaptureOutput, which provides similar functionality without the ability to tee output and with more complicated code and API.
The following functions are available. None are exported by default.
($stdout, $stderr) = capture \&code; $stdout = capture \&code;
The capture function takes a code reference and returns what is sent to STDOUT and STDERR. In scalar context, it returns only STDOUT. If no output was received, returns an empty string. Regardless of context, all output is captured -- nothing is passed to the existing handles.
capture
It is prototyped to take a subroutine reference as an argument. Thus, it can be called in block form:
($stdout, $stderr) = capture { # your code here ... };
($stdout, $stderr) = tee \&code; $stdout = tee \&code;
The tee function works just like capture, except that output is captured as well as passed on to the original STDOUT and STDERR. As with capture it may be called in block form.
tee
Portability is a goal, not a guarantee. tee requires fork, except on Windows where system(1, @cmd) is used instead. Not tested on any esoteric platforms yet. Minimal test suite so far.
system(1, @cmd)
No support for merging STDERR with STDOUT. This may be added in the future.
Please report any bugs or feature using the CPAN Request Tracker. Bugs can be submitted through the web interface at http://rt.cpan.org/Dist/Display.html?Queue=Capture-Tiny
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
This is a selection of CPAN modules that provide some sort of output capture, albeit with various limitations that make them appropriate only in particular circumstances. I'm probably missing some. The long list is provided to show why I felt Capture::Tiny was necessary.
IO::Capture
IO::Capture::Extended
IO::CaptureOutput
IPC::Capture
IPC::Cmd
IPC::Open2
IPC::Open3
IPC::Open3::Simple
IPC::Open3::Utils
IPC::Run
IPC::Run::SafeHandles
IPC::Run::Simple
IPC::Run3
IPC::System::Simple
Tee
IO::Tee
File::Tee
Filter::Handle
Tie::STDERR
Tie::STDOUT
Test::Output
David A. Golden (DAGOLDEN)
Copyright (c) 2009 by David A. Golden. All rights reserved.
Licensed under Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License was distributed with this file or you may obtain a copy of the License from http://www.apache.org/licenses/LICENSE-2.0
Files produced as output though the use of this software, shall not be considered Derivative Works, but shall be considered the original work of the Licensor.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
To install Capture::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Capture::Tiny
CPAN shell
perl -MCPAN -e shell install Capture::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.