Capture::Tiny (3)
Leading comments
Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) Standard preamble: ========================================================================
NAME
Capture::Tiny - Capture STDOUT and STDERR from Perl, XS or external programsVERSION
version 0.46SYNOPSIS
use Capture::Tiny ':all'; # capture from external command ($stdout, $stderr, $exit) = capture { system( $cmd, @args ); }; # capture from arbitrary code (Perl or external) ($stdout, $stderr, @result) = capture { # your code here }; # capture partial or merged output $stdout = capture_stdout { ... }; $stderr = capture_stderr { ... }; $merged = capture_merged { ... }; # tee output ($stdout, $stderr) = tee { # your code here }; $stdout = tee_stdout { ... }; $stderr = tee_stderr { ... }; $merged = tee_merged { ... };
DESCRIPTION
Capture::Tiny provides a simple, portable way to capture almost anything sent toUSAGE
The following functions are available. None are exported by default.capture
($stdout, $stderr, @result) = capture \&code; $stdout = capture \&code;
The "capture" function takes a code reference and returns what is sent to
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 ... };
Note that the coderef is evaluated in list context. If you wish to force scalar context on the return value, you must use the "scalar" keyword.
($stdout, $stderr, $count) = capture { my @list = qw/one two three/; return scalar @list; # $count will be 3 };
Also note that within the coderef, the @_ variable will be empty. So don't use arguments from a surrounding subroutine without copying them to an array first:
sub wont_work { my ($stdout, $stderr) = capture { do_stuff( @_ ) }; # WRONG ... } sub will_work { my @args = @_; my ($stdout, $stderr) = capture { do_stuff( @args ) }; # RIGHT ... }
Captures are normally done to an anonymous temporary filehandle. To capture via a named file (e.g. to externally monitor a long-running capture), provide custom filehandles as a trailing list of option pairs:
my $out_fh = IO::File->new("out.txt", "w+"); my $err_fh = IO::File->new("out.txt", "w+"); capture { ... } stdout => $out_fh, stderr => $err_fh;
The filehandles must be read/write and seekable. Modifying the files or filehandles during a capture operation will give unpredictable results. Existing
When called in void context, "capture" saves memory and time by not reading back from the capture handles.
capture_stdout
($stdout, @result) = capture_stdout \&code; $stdout = capture_stdout \&code;
The "capture_stdout" function works just like "capture" except only
capture_stderr
($stderr, @result) = capture_stderr \&code; $stderr = capture_stderr \&code;
The "capture_stderr" function works just like "capture" except only
capture_merged
($merged, @result) = capture_merged \&code; $merged = capture_merged \&code;
The "capture_merged" function works just like "capture" except
Caution:
tee
($stdout, $stderr, @result) = 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
When called in void context, "tee" saves memory and time by not reading back from the capture handles, except when the original
tee_stdout
($stdout, @result) = tee_stdout \&code; $stdout = tee_stdout \&code;
The "tee_stdout" function works just like "tee" except only
tee_stderr
($stderr, @result) = tee_stderr \&code; $stderr = tee_stderr \&code;
The "tee_stderr" function works just like "tee" except only
tee_merged
($merged, @result) = tee_merged \&code; $merged = tee_merged \&code;
The "tee_merged" function works just like "capture_merged" except that output is captured as well as passed on to
Caution:
LIMITATIONS
Portability
Portability is a goal, not a guarantee. "tee" requires fork, except on Windows where "system(1, @cmd)" is used instead. Not tested on any particularly esoteric platforms yet. See thePerlIO layers
Capture::Tiny does its best to preserve PerlIO layers such as ':utf8' or ':crlf' when capturing (only for Perl 5.8.1+) . Layers should be applied toModifying filehandles before capturing
Generally speaking, you should do little or no manipulation of the standardClosed filehandles
Capture::Tiny will work even if
Note that this reopening will happen even for
Prior to Perl 5.12, closed
Localized filehandles
If code localizes any of Perl's standard filehandles before capturing, the capture will affect the localized filehandles and not the original ones. External system calls are not affected by localizing a filehandle in Perl and will continue to send output to the original filehandles (which will thus not be captured).
Scalar filehandles
If
Capture::Tiny attempts to preserve the semantics of
Tied output filehandles
If
Capture::Tiny may not succeed resending
Tied input filehandle
Capture::Tiny attempts to preserve the semantics of tied
Unless having
my ($out, $err) = do { local *STDIN; capture { ... } };
Modifying filehandles during a capture
Attempting to modifyForking inside a capture
Forks aren't portable. The behavior of filehandles during a fork is even less so. If Capture::Tiny detects that a fork has occurred within a capture, it will shortcut in the child process and return empty strings for captures. Other problems may occur in the child or parent, as well. Forking in a capture block is not recommended.
Using threads
Filehandles are global. Mixing up I/O and captures in different threads without coordination is going to cause problems. Besides, threads are officially discouraged.
Dropping privileges during a capture
If you drop privileges during a capture, temporary files created to facilitate the capture may not be cleaned up afterwards.
support for Perl 5.8.0
It's just too buggy when it comes to layers andLimited support for Perl 5.6
Perl 5.6 predates PerlIO.ENVIRONMENT
PERL_CAPTURE_TINY_TIMEOUT
Capture::Tiny uses subprocesses internally for "tee". By default,
Capture::Tiny will timeout with an error if such subprocesses are not ready to
receive data within 30 seconds (or whatever is the value of
$Capture::Tiny::TIMEOUT). An alternate timeout may be specified by setting
the "PERL_CAPTURE_TINY_TIMEOUT" environment variable. Setting it to zero will
disable timeouts. SEE ALSO
This module was inspired by IO::CaptureOutput, which provides similar functionality without the ability to tee output and with more complicated code andThere are many other
- *
- 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
SUPPORT
Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker at <github.com/dagolden/Capture-Tiny/issues>. You will be notified automatically of any progress on your issue.Source Code
This is open source software. The code repository is available for public review and contribution under the terms of the license.<github.com/dagolden/Capture-Tiny>
git clone github.com/dagolden/Capture-Tiny.git
AUTHOR
David Golden <dagolden@cpan.org>CONTRIBUTORS
- *
- Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
- *
- David E. Wheeler <david@justatheory.com>
- *
- fecundf <not.com+github@gmail.com>
- *
- Graham Knop <haarg@haarg.org>
- *
- Peter Rabbitson <ribasushi@cpan.org>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2009 by David Golden.This is free software, licensed under:
The Apache License, Version 2.0, January 2004