1Capture::Tiny(3) User Contributed Perl Documentation Capture::Tiny(3)
2
3
4
6 Capture::Tiny - Capture STDOUT and STDERR from Perl, XS or external
7 programs
8
10 version 0.48
11
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
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
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
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 its 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 Forking inside a capture
265
266 Forks aren't portable. The behavior of filehandles during a fork is
267 even less so. If Capture::Tiny detects that a fork has occurred within
268 a capture, it will shortcut in the child process and return empty
269 strings for captures. Other problems may occur in the child or parent,
270 as well. Forking in a capture block is not recommended.
271
272 Using threads
273
274 Filehandles are global. Mixing up I/O and captures in different
275 threads without coordination is going to cause problems. Besides,
276 threads are officially discouraged.
277
278 Dropping privileges during a capture
279
280 If you drop privileges during a capture, temporary files created to
281 facilitate the capture may not be cleaned up afterwards.
282
283 No support for Perl 5.8.0
284 It's just too buggy when it comes to layers and UTF-8. Perl 5.8.1 or
285 later is recommended.
286
287 Limited support for Perl 5.6
288 Perl 5.6 predates PerlIO. UTF-8 data may not be captured correctly.
289
291 PERL_CAPTURE_TINY_TIMEOUT
292 Capture::Tiny uses subprocesses internally for "tee". By default,
293 Capture::Tiny will timeout with an error if such subprocesses are not
294 ready to receive data within 30 seconds (or whatever is the value of
295 $Capture::Tiny::TIMEOUT). An alternate timeout may be specified by
296 setting the "PERL_CAPTURE_TINY_TIMEOUT" environment variable. Setting
297 it to zero will disable timeouts. NOTE, this does not timeout the code
298 reference being captured -- this only prevents Capture::Tiny itself
299 from hanging your process waiting for its child processes to be ready
300 to proceed.
301
303 This module was inspired by IO::CaptureOutput, which provides similar
304 functionality without the ability to tee output and with more
305 complicated code and API. IO::CaptureOutput does not handle layers or
306 most of the unusual cases described in the "Limitations" section and I
307 no longer recommend it.
308
309 There are many other CPAN modules that provide some sort of output
310 capture, albeit with various limitations that make them appropriate
311 only in particular circumstances. I'm probably missing some. The long
312 list is provided to show why I felt Capture::Tiny was necessary.
313
314 · IO::Capture
315
316 · IO::Capture::Extended
317
318 · IO::CaptureOutput
319
320 · IPC::Capture
321
322 · IPC::Cmd
323
324 · IPC::Open2
325
326 · IPC::Open3
327
328 · IPC::Open3::Simple
329
330 · IPC::Open3::Utils
331
332 · IPC::Run
333
334 · IPC::Run::SafeHandles
335
336 · IPC::Run::Simple
337
338 · IPC::Run3
339
340 · IPC::System::Simple
341
342 · Tee
343
344 · IO::Tee
345
346 · File::Tee
347
348 · Filter::Handle
349
350 · Tie::STDERR
351
352 · Tie::STDOUT
353
354 · Test::Output
355
357 Bugs / Feature Requests
358 Please report any bugs or feature requests through the issue tracker at
359 <https://github.com/dagolden/Capture-Tiny/issues>. You will be
360 notified automatically of any progress on your issue.
361
362 Source Code
363 This is open source software. The code repository is available for
364 public review and contribution under the terms of the license.
365
366 <https://github.com/dagolden/Capture-Tiny>
367
368 git clone https://github.com/dagolden/Capture-Tiny.git
369
371 David Golden <dagolden@cpan.org>
372
374 · Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
375
376 · David E. Wheeler <david@justatheory.com>
377
378 · fecundf <not.com+github@gmail.com>
379
380 · Graham Knop <haarg@haarg.org>
381
382 · Peter Rabbitson <ribasushi@cpan.org>
383
385 This software is Copyright (c) 2009 by David Golden.
386
387 This is free software, licensed under:
388
389 The Apache License, Version 2.0, January 2004
390
391
392
393perl v5.30.0 2019-07-26 Capture::Tiny(3)