# Generated by Pod::WikiDoc version 0.18 =pod =head1 NAME IO::CaptureOutput - capture STDOUT and STDERR from Perl code, subprocesses or XS =head1 VERSION This documentation describes version 1.06. =head1 SYNOPSIS use IO::CaptureOutput qw(capture capture_exec); my ($stdout, $stderr); sub noisy { warn "this sub prints to stdout and stderr!"; print "arguments: @_"; } capture sub {noisy(@args)}, \$stdout, \$stderr; ($stdout, $stderr) = capture_exec( 'perl', '-e', 'print "Hello"; print STDERR "World!"'); =head1 DESCRIPTION This module provides routines for capturing STDOUT and STDERR from perl subroutines, forked system calls (e.g. C<<< system() >>>, C<<< fork() >>>) and from XS or C modules. =head1 FUNCTIONS The following functions will be exported on demand. =head2 capture() capture(\&subroutine, \$stdout, \$stderr); Captures everything printed to C<<< STDOUT >>> and C<<< STDERR >>> for the duration of C<<< &subroutine >>>. C<<< $stdout >>> and C<<< $stderr >>> are optional scalars that will contain C<<< STDOUT >>> and C<<< STDERR >>> respectively. Returns the return value(s) of C<<< &subroutine >>>. The sub is called in the same context as C<<< capture() >>> was called e.g.: @rv = capture(sub {wantarray}); # returns true $rv = capture(sub {wantarray}); # returns defined, but not true capture(sub {wantarray}); # void, returns undef C<<< capture() >>> is able to capture output from subprocesses and C code, which traditional C<<< tie() >>> methods of output capture are unable to do. If the two scalar references refer to the same scalar, then C<<< STDERR >>> will be merged to C<<< STDOUT >>> before capturing and the scalar will hold the combined output of both. capture(\&subroutine, \$combined, \$combined); B C<<< capture() >>> will only capture output that has been written or flushed to the filehandle. =head2 capture_exec() ($stdout, $stderr) = capture_exec(@args); Captures and returns the output from C<<< system(@args) >>>. In scalar context, C<<< capture_exec() >>> will return what was printed to C<<< STDOUT >>>. In list context, it returns what was printed to C<<< STDOUT >>> and C<<< STDERR >>> $stdout = capture_exec('perl', '-e', 'print "hello world"'); ($stdout, $stderr) = capture_exec('perl', '-e', 'warn "Test"'); C<<< capture_exec >>> passes its arguments to C<<< system() >>> and on MSWin32 will protect arguments with shell quotes if necessary. This makes it a handy and slightly more portable alternative to backticks, piped C<<< open() >>> and C<<< IPC::Open3 >>>. You can check the exit status of the C<<< system() >>> call with the C<<< $? >>> variable. See L for more information. =head2 capture_exec_combined() $combined = capture_exec_combined( 'perl', '-e', 'print "hello\n"', 'warn "Test\n" ); This is just like C<<< capture_exec() >>>, except that it merges C<<< STDERR >>> with C<<< STDOUT >>> before capturing output and returns a single scalar. B there is no guarantee that text printed to C<<< STDOUT >>> and C<<< STDERR >>> in the subprocess will be appear in order. The actual order will depend on how IO buffering is handled in the subprocess. =head2 qxx() This is an alias for C<<< capture_exec() >>>. =head2 qxy() This is an alias for C<<< capture_exec_combined() >>>. =head1 SEE ALSO =over =item * L =item * L =item * L =back =head1 AUTHORS =over =item * Simon Flack Esimonflk _AT_ cpan.orgE (original author) =item * David Golden Edagolden _AT_ cpan.orgE (co-maintainer since version 1.04) =back =head1 COPYRIGHT AND LICENSE Portions copyright 2004, 2005 Simon Flack. Portions copyright 2007 David Golden. All rights reserved. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.