1 # Generated by Pod::WikiDoc version 0.18
7 Capture::Tiny - Capture STDOUT and STDERR from Perl, XS or external programs
11 This documentation describes version 0.06.
15 use Capture::Tiny qw/capture tee capture_merged tee_merged/;
17 ($stdout, $stderr) = capture {
21 ($stdout, $stderr) = tee {
25 $merged = capture_merged {
29 $merged = tee_merged {
35 Capture::Tiny provides a simple, portable way to capture anything sent to
36 STDOUT or STDERR, regardless of whether it comes from Perl, from XS code or
37 from an external program. Optionally, output can be teed so that it is
38 captured while being passed through to the original handles. Yes, it even
39 works on Windows. Stop guessing which of a dozen capturing modules to use in
40 any particular situation and just use this one.
42 This module was heavily inspired by L<IO::CaptureOutput>, which provides
43 similar functionality without the ability to tee output and with more
44 complicated code and API.
48 The following functions are available. None are exported by default.
52 ($stdout, $stderr) = capture \&code;
53 $stdout = capture \&code;
55 The C<<< capture >>> function takes a code reference and returns what is sent to
56 STDOUT and STDERR. In scalar context, it returns only STDOUT. If no output
57 was received, returns an empty string. Regardless of context, all output is
58 captured -- nothing is passed to the existing handles.
60 It is prototyped to take a subroutine reference as an argument. Thus, it
61 can be called in block form:
63 ($stdout, $stderr) = capture {
69 $merged = capture_merged \&code;
71 The C<<< capture_merged >>> function works just like C<<< capture >>> except STDOUT and
72 STDERR are merged. (Technically, STDERR is redirected to STDOUT before
73 executing the function.) If no output was received, returns an empty string.
74 As with C<<< capture >>> it may be called in block form.
76 Caution: STDOUT and STDERR output in the merged result are not guaranteed to be
77 properly ordered due to buffering.
81 ($stdout, $stderr) = tee \&code;
84 The C<<< tee >>> function works just like C<<< capture >>>, except that output is captured
85 as well as passed on to the original STDOUT and STDERR. As with C<<< capture >>> it
86 may be called in block form.
90 $merged = tee_merged \&code;
92 The C<<< tee_merged >>> function works just like C<<< capture_merged >>> except that output
93 is captured as well as passed on to STDOUT. As with C<<< capture >>> it may be called
96 Caution: STDOUT and STDERR output in the merged result are not guaranteed to be
97 properly ordered due to buffering.
103 Portability is a goal, not a guarantee. C<<< tee >>> requires fork, except on
104 Windows where C<<< system(1, @cmd) >>> is used instead. Not tested on any
105 particularly esoteric platforms yet.
109 Capture::Tiny does it's best to preserve PerlIO layers such as ':utf8' or
110 ':crlf' when capturing. Layers should be applied to STDOUT or STDERR I<before>
111 the call to C<<< capture >>> or C<<< tee >>>.
113 =head2 Closed STDIN, STDOUT or STDERR
115 Capture::Tiny will work even if STDIN, STDOUT or STDERR have been previously
116 closed. However, since they may be reopened to capture or tee output, any code
117 within the captured block that depends on finding them closed will, of course,
118 not find them to be closed. If they started closed, Capture::Tiny will reclose
119 them again when the capture block finishes.
121 =head2 Scalar filehandles and STDIN, STDOUT or STDERR
123 If STDOUT or STDERR are reopened to scalar filehandles prior to the call to
124 C<<< capture >>> or C<<< tee >>>, then Capture::Tiny will override the output handle for the
125 duration of the C<<< capture >>> or C<<< tee >>> call and then send captured output to the
126 output handle after the capture is complete. (Requires Perl 5.8)
128 Capture::Tiny attempts to preserve the semantics of STDIN opened to a scalar
131 =head2 Tied STDIN, STDOUT or STDERR
133 If STDOUT or STDERR are tied prior to the call to C<<< capture >>> or C<<< tee >>>, then
134 Capture::Tiny will attempt to override the tie for the duration of the
135 C<<< capture >>> or C<<< tee >>> call and then send captured output to the tied handle after
136 the capture is complete. (Requires Perl 5.8)
138 Capture::Tiny does not (yet) support resending utf8 encoded data to a tied
139 STDOUT or STDERR handle. Characters will appear as bytes.
141 Capture::Tiny attempts to preserve the semantics of tied STDIN, but capturing
142 or teeing when STDIN is tied is currently broken on Windows.
144 =head2 Modifiying STDIN, STDOUT or STDERR during a capture
146 Attempting to modify STDIN, STDOUT or STDERR I<during> C<<< capture >>> or C<<< tee >>> is
147 almost certainly going to cause problems. Don't do that.
151 Please report any bugs or feature requests using the CPAN Request Tracker.
152 Bugs can be submitted through the web interface at
153 L<http://rt.cpan.org/Dist/Display.html?Queue=Capture-Tiny>
155 When submitting a bug or request, please include a test-file or a patch to an
156 existing test-file that illustrates the bug or desired feature.
160 This is a selection of CPAN modules that provide some sort of output capture,
161 albeit with various limitations that make them appropriate only in particular
162 circumstances. I'm probably missing some. The long list is provided to show
163 why I felt Capture::Tiny was necessary.
173 L<IO::Capture::Extended>
197 L<IPC::Open3::Simple>
209 L<IPC::Run::SafeHandles>
221 L<IPC::System::Simple>
255 David A. Golden (DAGOLDEN)
257 =head1 COPYRIGHT AND LICENSE
259 Copyright (c) 2009 by David A. Golden. All rights reserved.
261 Licensed under Apache License, Version 2.0 (the "License"). You may not use
262 this file except in compliance with the License. A copy of the License was
263 distributed with this file or you may obtain a copy of the License from
264 http:E<sol>E<sol>www.apache.orgE<sol>licensesE<sol>LICENSE-2.0
266 Files produced as output though the use of this software, shall not be
267 considered Derivative Works, but shall be considered the original work of the
270 Unless required by applicable law or agreed to in writing, software
271 distributed under the License is distributed on an "AS IS" BASIS,
272 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
273 See the License for the specific language governing permissions and
274 limitations under the License.