1IO::CaptureOutput(3) User Contributed Perl Documentation IO::CaptureOutput(3)
2
3
4
6 IO::CaptureOutput - capture STDOUT/STDERR from subprocesses and XS/C
7 modules
8
10 use IO::CaptureOutput qw(capture capture_exec qxx);
11
12 my ($stdout, $stderr);
13 capture sub {noisy(@args)}, \$stdout, \$stderr;
14 sub noisy {
15 my @args = @_;
16 warn "this sub prints to stdout and stderr!";
17 ...
18 print "finished";
19 }
20
21 ($stdout, $stderr) = capture_exec('perl', '-e', 'print "Hello "; print STDERR "World!"');
22
24 This module provides routines for capturing STDOUT and STDERR from
25 forked system calls (e.g. "system()", "fork()") and from XS/C modules.
26
28 The following functions are be exported on demand.
29
30 capture(\&subroutine, \$output, \$error)
31 Captures everything printed to "STDOUT" and "STDERR" for the dura‐
32 tion of &subroutine. $output and $error are optional scalar refer‐
33 ences that will contain "STDOUT" and "STDERR" respectively.
34
35 Returns the return value(s) of &subroutine. The sub is called in
36 the same context as "capture()" was called e.g.:
37
38 @rv = capture(sub {wantarray}); # returns true
39 $rv = capture(sub {wantarray}); # returns defined, but not true
40 capture(sub {wantarray}); # void, returns undef
41
42 "capture()" is able to trap output from subprocesses and C code,
43 which traditional "tie()" methods are unable to capture.
44
45 Note: "capture()" will only capture output that has been written or
46 flushed to the filehandle.
47
48 capture_exec(@args)
49 Captures and returns the output from "system(@args)". In scalar
50 context, "capture_exec()" will return what was printed to "STDOUT".
51 In list context, it returns what was printed to "STDOUT" and
52 "STDERR"
53
54 my $output = capture_exec('perl', '-e', 'print "hello world"');
55
56 my ($output, $error) = capture_exec('perl', '-e', 'warn "Test"');
57
58 "capture_exec" passes its arguments to "CORE::system" it can take
59 advantage of the shell quoting, which makes it a handy and slightly
60 more portable alternative to backticks, piped "open()" and
61 "IPC::Open3".
62
63 You can check the exit status of the "system()" call with the $?
64 variable. See perlvar for more information.
65
66 qxx(@args)
67 This is an alias of "capture_exec"
68
70 IPC::Open3
71
72 IO::Capture
73
74 IO::Utils
75
77 Simon Flack <simonflk _AT_ cpan.org>
78
80 Copyright 2004, 2005 Simon Flack <simonflk _AT_ cpan.org>. All rights
81 reserved
82
83 You may distribute under the terms of either the GNU General Public
84 License or the Artistic License, as specified in the Perl README file.
85
86
87
88perl v5.8.8 2005-03-25 IO::CaptureOutput(3)