IPC::Run3(3) User Contributed Perl Documentation IPC::Run3(3) NAME IPC::Run3 - run a subprocess with input/output redirection VERSION version 0.049 SYNOPSIS use IPC::Run3; # Exports run3() by default run3 \@cmd, \$in, \$out, \$err; DESCRIPTION This module allows you to run a subprocess and redirect stdin, stdout, and/or stderr to files and perl data structures. It aims to satisfy 99% of the need for using "system", "qx", and "open3" with a simple, extremely Perlish API. Speed, simplicity, and portability are paramount. (That's speed of Perl code; which is often much slower than the kind of buffered I/O that this module uses to spool input to and output from the child command.) "run3($cmd, $stdin, $stdout, $stderr, \%options)" All parameters after $cmd are optional. The parameters $stdin, $stdout and $stderr indicate how the child's corresponding filehandle ("STDIN", "STDOUT" and "STDERR", resp.) will be redirected. Because the redirects come last, this allows "STDOUT" and "STDERR" to default to the parent's by just not specifying them -- a common use case. "run3" throws an exception if the wrapped "system" call returned -1 or anything went wrong with "run3"'s processing of filehandles. Otherwise it returns true. It leaves $? intact for inspection of exit and wait status. Note that a true return value from "run3" doesn't mean that the command had a successful exit code. Hence you should always check $?. See "%options" for an option to handle the case of "system" returning -1 yourself. $cmd Usually $cmd will be an ARRAY reference and the child is invoked via system @$cmd; But $cmd may also be a string in which case the child is invoked via system $cmd; (cf. "system" in perlfunc for the difference and the pitfalls of using the latter form). $stdin, $stdout, $stderr The parameters $stdin, $stdout and $stderr can take one of the following forms: "undef" (or not specified at all) The child inherits the corresponding filehandle from the parent. run3 \@cmd, $stdin; # child writes to same STDOUT and STDERR as parent run3 \@cmd, undef, $stdout, $stderr; # child reads from same STDIN as parent "\undef" The child's filehandle is redirected from or to the local equivalent of "/dev/null" (as returned by "File::Spec->devnull()"). run3 \@cmd, \undef, $stdout, $stderr; # child reads from /dev/null a simple scalar The parameter is taken to be the name of a file to read from or write to. In the latter case, the file will be opened via open FH, ">", ... i.e. it is created if it doesn't exist and truncated otherwise. Note that the file is opened by the parent which will croak in case of failure. run3 \@cmd, \undef, "out.txt"; # child writes to file "out.txt" a filehandle (either a reference to a GLOB or an "IO::Handle") The filehandle is inherited by the child. open my $fh, ">", "out.txt"; print $fh "prologue\n"; ... run3 \@cmd, \undef, $fh; # child writes to $fh ... print $fh "epilogue\n"; close $fh; a SCALAR reference The referenced scalar is treated as a string to be read from or written to. In the latter case, the previous content of the string is overwritten. my $out; run3 \@cmd, \undef, \$out; # child writes into string run3 \@cmd, \< Ricardo SIGNES <"rjbs@cpan.org"> performed routine maintenance since 2010, thanks to help from the following ticket and/or patch submitters: Jody Belka, Roderich Schupp, David Morel, Jeff Lavallee, and anonymous others. perl v5.38.1 2024-01-28 IPC::Run3(3)