1Capture::Tiny(3)      User Contributed Perl Documentation     Capture::Tiny(3)
2
3
4

NAME

6       Capture::Tiny - Capture STDOUT and STDERR from Perl, XS or external
7       programs
8

VERSION

10       version 0.24
11

SYNOPSIS

13          use Capture::Tiny ':all';
14
15          # capture from external command
16
17          ($stdout, $stderr, $exit) = capture {
18            system( $cmd, @args );
19          };
20
21          # capture from arbitrary code (Perl or external)
22
23          ($stdout, $stderr, @result) = capture {
24            # your code here
25          };
26
27          # capture partial or merged output
28
29          $stdout = capture_stdout { ... };
30          $stderr = capture_stderr { ... };
31          $merged = capture_merged { ... };
32
33          # tee output
34
35          ($stdout, $stderr) = tee {
36            # your code here
37          };
38
39          $stdout = tee_stdout { ... };
40          $stderr = tee_stderr { ... };
41          $merged = tee_merged { ... };
42

DESCRIPTION

44       Capture::Tiny provides a simple, portable way to capture almost
45       anything sent to STDOUT or STDERR, regardless of whether it comes from
46       Perl, from XS code or from an external program.  Optionally, output can
47       be teed so that it is captured while being passed through to the
48       original filehandles.  Yes, it even works on Windows (usually).  Stop
49       guessing which of a dozen capturing modules to use in any particular
50       situation and just use this one.
51

USAGE

53       The following functions are available.  None are exported by default.
54
55   capture
56          ($stdout, $stderr, @result) = capture \&code;
57          $stdout = capture \&code;
58
59       The "capture" function takes a code reference and returns what is sent
60       to STDOUT and STDERR as well as any return values from the code
61       reference.  In scalar context, it returns only STDOUT.  If no output
62       was received for a filehandle, it returns an empty string for that
63       filehandle.  Regardless of calling context, all output is captured --
64       nothing is passed to the existing filehandles.
65
66       It is prototyped to take a subroutine reference as an argument. Thus,
67       it can be called in block form:
68
69          ($stdout, $stderr) = capture {
70            # your code here ...
71          };
72
73       Note that the coderef is evaluated in list context.  If you wish to
74       force scalar context on the return value, you must use the "scalar"
75       keyword.
76
77          ($stdout, $stderr, $count) = capture {
78            my @list = qw/one two three/;
79            return scalar @list; # $count will be 3
80          };
81
82       Also note that within the coderef, the @_ variable will be empty.  So
83       don't use arguments from a surrounding subroutine without copying them
84       to an array first:
85
86          sub wont_work {
87            my ($stdout, $stderr) = capture { do_stuff( @_ ) };    # WRONG
88            ...
89          }
90
91          sub will_work {
92            my @args = @_;
93            my ($stdout, $stderr) = capture { do_stuff( @args ) }; # RIGHT
94            ...
95          }
96
97       Captures are normally done to an anonymous temporary filehandle.  To
98       capture via a named file (e.g. to externally monitor a long-running
99       capture), provide custom filehandles as a trailing list of option
100       pairs:
101
102          my $out_fh = IO::File->new("out.txt", "w+");
103          my $err_fh = IO::File->new("out.txt", "w+");
104          capture { ... } stdout => $out_fh, stderr => $err_fh;
105
106       The filehandles must be read/write and seekable.  Modifying the files
107       or filehandles during a capture operation will give unpredictable
108       results.  Existing IO layers on them may be changed by the capture.
109
110       When called in void context, "capture" saves memory and time by not
111       reading back from the capture handles.
112
113   capture_stdout
114          ($stdout, @result) = capture_stdout \&code;
115          $stdout = capture_stdout \&code;
116
117       The "capture_stdout" function works just like "capture" except only
118       STDOUT is captured.  STDERR is not captured.
119
120   capture_stderr
121          ($stderr, @result) = capture_stderr \&code;
122          $stderr = capture_stderr \&code;
123
124       The "capture_stderr" function works just like "capture" except only
125       STDERR is captured.  STDOUT is not captured.
126
127   capture_merged
128          ($merged, @result) = capture_merged \&code;
129          $merged = capture_merged \&code;
130
131       The "capture_merged" function works just like "capture" except STDOUT
132       and STDERR are merged. (Technically, STDERR is redirected to the same
133       capturing handle as STDOUT before executing the function.)
134
135       Caution: STDOUT and STDERR output in the merged result are not
136       guaranteed to be properly ordered due to buffering.
137
138   tee
139          ($stdout, $stderr, @result) = tee \&code;
140          $stdout = tee \&code;
141
142       The "tee" function works just like "capture", except that output is
143       captured as well as passed on to the original STDOUT and STDERR.
144
145       When called in void context, "tee" saves memory and time by not reading
146       back from the capture handles, except when the original STDOUT OR
147       STDERR were tied or opened to a scalar handle.
148
149   tee_stdout
150          ($stdout, @result) = tee_stdout \&code;
151          $stdout = tee_stdout \&code;
152
153       The "tee_stdout" function works just like "tee" except only STDOUT is
154       teed.  STDERR is not teed (output goes to STDERR as usual).
155
156   tee_stderr
157          ($stderr, @result) = tee_stderr \&code;
158          $stderr = tee_stderr \&code;
159
160       The "tee_stderr" function works just like "tee" except only STDERR is
161       teed.  STDOUT is not teed (output goes to STDOUT as usual).
162
163   tee_merged
164          ($merged, @result) = tee_merged \&code;
165          $merged = tee_merged \&code;
166
167       The "tee_merged" function works just like "capture_merged" except that
168       output is captured as well as passed on to STDOUT.
169
170       Caution: STDOUT and STDERR output in the merged result are not
171       guaranteed to be properly ordered due to buffering.
172

LIMITATIONS

174   Portability
175       Portability is a goal, not a guarantee.  "tee" requires fork, except on
176       Windows where "system(1, @cmd)" is used instead.  Not tested on any
177       particularly esoteric platforms yet.  See the CPAN Testers Matrix
178       <http://matrix.cpantesters.org/?dist=Capture-Tiny> for test result by
179       platform.
180
181   PerlIO layers
182       Capture::Tiny does it's best to preserve PerlIO layers such as ':utf8'
183       or ':crlf' when capturing (only for Perl 5.8.1+) .  Layers should be
184       applied to STDOUT or STDERR before the call to "capture" or "tee".
185       This may not work for tied filehandles (see below).
186
187   Modifying filehandles before capturing
188       Generally speaking, you should do little or no manipulation of the
189       standard IO filehandles prior to using Capture::Tiny.  In particular,
190       closing, reopening, localizing or tying standard filehandles prior to
191       capture may cause a variety of unexpected, undesirable and/or
192       unreliable behaviors, as described below.  Capture::Tiny does its best
193       to compensate for these situations, but the results may not be what you
194       desire.
195
196       Closed filehandles
197
198       Capture::Tiny will work even if STDIN, STDOUT or STDERR have been
199       previously closed.  However, since they will be reopened to capture or
200       tee output, any code within the captured block that depends on finding
201       them closed will, of course, not find them to be closed.  If they
202       started closed, Capture::Tiny will close them again when the capture
203       block finishes.
204
205       Note that this reopening will happen even for STDIN or a filehandle not
206       being captured to ensure that the filehandle used for capture is not
207       opened to file descriptor 0, as this causes problems on various
208       platforms.
209
210       Prior to Perl 5.12, closed STDIN combined with PERL_UNICODE=D leaks
211       filehandles and also breaks tee() for undiagnosed reasons.  So don't do
212       that.
213
214       Localized filehandles
215
216       If code localizes any of Perl's standard filehandles before capturing,
217       the capture will affect the localized filehandles and not the original
218       ones.  External system calls are not affected by localizing a
219       filehandle in Perl and will continue to send output to the original
220       filehandles (which will thus not be captured).
221
222       Scalar filehandles
223
224       If STDOUT or STDERR are reopened to scalar filehandles prior to the
225       call to "capture" or "tee", then Capture::Tiny will override the output
226       filehandle for the duration of the "capture" or "tee" call and then,
227       for "tee", send captured output to the output filehandle after the
228       capture is complete.  (Requires Perl 5.8)
229
230       Capture::Tiny attempts to preserve the semantics of STDIN opened to a
231       scalar reference, but note that external processes will not be able to
232       read from such a handle.  Capture::Tiny tries to ensure that external
233       processes will read from the null device instead, but this is not
234       guaranteed.
235
236       Tied output filehandles
237
238       If STDOUT or STDERR are tied prior to the call to "capture" or "tee",
239       then Capture::Tiny will attempt to override the tie for the duration of
240       the "capture" or "tee" call and then send captured output to the tied
241       filehandle after the capture is complete.  (Requires Perl 5.8)
242
243       Capture::Tiny may not succeed resending UTF-8 encoded data to a tied
244       STDOUT or STDERR filehandle.  Characters may appear as bytes.  If the
245       tied filehandle is based on Tie::StdHandle, then Capture::Tiny will
246       attempt to determine appropriate layers like ":utf8" from the
247       underlying filehandle and do the right thing.
248
249       Tied input filehandle
250
251       Capture::Tiny attempts to preserve the semantics of tied STDIN, but
252       this requires Perl 5.8 and is not entirely predictable.  External
253       processes will not be able to read from such a handle.
254
255       Unless having STDIN tied is crucial, it may be safest to localize STDIN
256       when capturing:
257
258          my ($out, $err) = do { local *STDIN; capture { ... } };
259
260   Modifying filehandles during a capture
261       Attempting to modify STDIN, STDOUT or STDERR during "capture" or "tee"
262       is almost certainly going to cause problems.  Don't do that.
263
264   No support for Perl 5.8.0
265       It's just too buggy when it comes to layers and UTF-8.  Perl 5.8.1 or
266       later is recommended.
267
268   Limited support for Perl 5.6
269       Perl 5.6 predates PerlIO.  UTF-8 data may not be captured correctly.
270

ENVIRONMENT

272   PERL_CAPTURE_TINY_TIMEOUT
273       Capture::Tiny uses subprocesses for "tee".  By default, Capture::Tiny
274       will timeout with an error if the subprocesses are not ready to receive
275       data within 30 seconds (or whatever is the value of
276       $Capture::Tiny::TIMEOUT).  An alternate timeout may be specified by
277       setting the "PERL_CAPTURE_TINY_TIMEOUT" environment variable.  Setting
278       it to zero will disable timeouts.
279

SEE ALSO

281       This module was, inspired by IO::CaptureOutput, which provides similar
282       functionality without the ability to tee output and with more
283       complicated code and API.  IO::CaptureOutput does not handle layers or
284       most of the unusual cases described in the "Limitations" section and I
285       no longer recommend it.
286
287       There are many other CPAN modules that provide some sort of output
288       capture, albeit with various limitations that make them appropriate
289       only in particular circumstances.  I'm probably missing some.  The long
290       list is provided to show why I felt Capture::Tiny was necessary.
291
292       ·   IO::Capture
293
294       ·   IO::Capture::Extended
295
296       ·   IO::CaptureOutput
297
298       ·   IPC::Capture
299
300       ·   IPC::Cmd
301
302       ·   IPC::Open2
303
304       ·   IPC::Open3
305
306       ·   IPC::Open3::Simple
307
308       ·   IPC::Open3::Utils
309
310       ·   IPC::Run
311
312       ·   IPC::Run::SafeHandles
313
314       ·   IPC::Run::Simple
315
316       ·   IPC::Run3
317
318       ·   IPC::System::Simple
319
320       ·   Tee
321
322       ·   IO::Tee
323
324       ·   File::Tee
325
326       ·   Filter::Handle
327
328       ·   Tie::STDERR
329
330       ·   Tie::STDOUT
331
332       ·   Test::Output
333

SUPPORT

335   Bugs / Feature Requests
336       Please report any bugs or feature requests through the issue tracker at
337       <https://github.com/dagolden/Capture-Tiny/issues>.  You will be
338       notified automatically of any progress on your issue.
339
340   Source Code
341       This is open source software.  The code repository is available for
342       public review and contribution under the terms of the license.
343
344       <https://github.com/dagolden/Capture-Tiny>
345
346         git clone https://github.com/dagolden/Capture-Tiny.git
347

AUTHOR

349       David Golden <dagolden@cpan.org>
350

CONTRIBUTOR

352       Dagfinn Ilmari Mannsaaker <ilmari@ilmari.org>
353
355       This software is Copyright (c) 2009 by David Golden.
356
357       This is free software, licensed under:
358
359         The Apache License, Version 2.0, January 2004
360
361
362
363perl v5.16.3                      2014-02-06                  Capture::Tiny(3)
Impressum