.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "IPC::Open2 3" .TH IPC::Open2 3 2025-06-24 "perl v5.42.0" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME IPC::Open2 \- open a process for both reading and writing using open2() .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use IPC::Open2; \& \& my $pid = open2(my $chld_out, my $chld_in, \& \*(Aqsome\*(Aq, \*(Aqcmd\*(Aq, \*(Aqand\*(Aq, \*(Aqargs\*(Aq); \& # or passing the command through the shell \& my $pid = open2(my $chld_out, my $chld_in, \*(Aqsome cmd and args\*(Aq); \& \& # read from parent STDIN and write to already open handle \& open my $outfile, \*(Aq>\*(Aq, \*(Aqoutfile.txt\*(Aq or die "open failed: $!"; \& my $pid = open2([\*(Aq&\*(Aq, $outfile], [\*(Aq&\*(Aq, *STDIN], \& \*(Aqsome\*(Aq, \*(Aqcmd\*(Aq, \*(Aqand\*(Aq, \*(Aqargs\*(Aq); \& \& # read from already open handle and write to parent STDOUT \& open my $infile, \*(Aq<\*(Aq, \*(Aqinfile.txt\*(Aq or die "open failed: $!"; \& my $pid = open2([\*(Aq&\*(Aq, *STDOUT], [\*(Aq&\*(Aq, $infile], \& \*(Aqsome\*(Aq, \*(Aqcmd\*(Aq, \*(Aqand\*(Aq, \*(Aqargs\*(Aq); \& \& # reap zombie and retrieve exit status \& waitpid( $pid, 0 ); \& my $child_exit_status = $? >> 8; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" The \f(CWopen2()\fR function runs the given command and connects \f(CW$chld_out\fR for reading and \f(CW$chld_in\fR for writing. It\*(Aqs what you think should work when you try .PP .Vb 1 \& my $pid = open(my $fh, "|cmd args|"); # ERROR .Ve .PP but you have to write it as: .PP .Vb 1 \& my $pid = open2($chld_out, $chld_in, @command_and_args); .Ve .PP The \f(CW$chld_in\fR filehandle will have autoflush turned on. .PP By default, the filehandles you pass in are used as output parameters. \&\f(CW\*(C`open2\*(C'\fR internally creates two pipes. The write end of the first pipe and the read end of the second pipe are connected to the command\*(Aqs standard output and input, respectively. The corresponding read and write ends are placed in the first and second argument to \f(CW\*(C`open2\*(C'\fR. .PP The filehandle arguments can take the following forms: .IP \(bu 4 An uninitialized variable (technically, either \f(CW\*(C`undef\*(C'\fR or the empty string will work): \f(CW\*(C`open2\*(C'\fR generates a fresh filehandle and assigns it to the argument, which must be a modifiable variable for this work (otherwise an exception will be raised). .IP \(bu 4 An existing handle in the form of a typeglob like \f(CW*STDIN\fR or \f(CW*FOO\fR or a reference to such: \f(CW\*(C`open2\*(C'\fR places the filehandle in the \f(CW\*(C`IO\*(C'\fR slot of the typeglob, which means the corresponding bareword filehandle (like \f(CW\*(C`STDIN\*(C'\fR or \&\f(CW\*(C`FOO\*(C'\fR) can be used for I/O from/to the child process. (If the handle is already open, it is automatically closed first.) .IP \(bu 4 A string containing the name of a bareword handle (like \f(CW\*(AqSTDIN\*(Aq\fR or \&\f(CW\*(AqFOO\*(Aq\fR): Such strings are resolved to typeglobs at runtime and then act like the case described above. .PP However, it is possible to make \f(CW\*(C`open2\*(C'\fR use an existing handle directly (as an input argument) and skip the creation of a pipe. To do this, the filehandle argument must have one of the following two forms: .IP \(bu 4 An array reference like \f(CW\*(C`[\*(Aq&\*(Aq, $fh]\*(C'\fR, i.e. the first element is the string \&\f(CW\*(Aq&\*(Aq\fR and the second element is the existing handle to use in the child process. .IP \(bu 4 A string of the form \f(CW\*(Aq<&FOO\*(Aq\fR or \f(CW\*(Aq>&FOO\*(Aq\fR, i.e. a string starting with the two characters \f(CW\*(C`<&\*(C'\fR (for input) or \f(CW\*(C`>&\*(C'\fR (for output), followed by the name of a bareword filehandle. (The string form cannot be used with handles stored in variables.) .PP If you use this form for \f(CW$chld_in\fR, the filehandle will be closed in the parent process. .PP \&\f(CW\*(C`open2\*(C'\fR returns the process ID of the child process. It doesn\*(Aqt return on failure: it just raises an exception matching \f(CW\*(C`/^open2:/\*(C'\fR. However, \&\f(CW\*(C`exec\*(C'\fR failures in the child are not detected. You\*(Aqll have to trap SIGPIPE yourself. .PP \&\f(CW\*(C`open2\*(C'\fR does not wait for and reap the child process after it exits. Except for short programs where it\*(Aqs acceptable to let the operating system take care of this, you need to do this yourself. This is normally as simple as calling \f(CW\*(C`waitpid $pid, 0\*(C'\fR when you\*(Aqre done with the process. Failing to do this can result in an accumulation of defunct or "zombie" processes. See "waitpid" in perlfunc for more information. .PP This whole affair is quite dangerous, as you may block forever. It assumes it\*(Aqs going to talk to something like \fBbc\fR\|(1), both writing to it and reading from it. This is presumably safe because you "know" that commands like \fBbc\fR\|(1) will read a line at a time and output a line at a time. Programs like \fBsort\fR\|(1) that read their entire input stream first, however, are quite apt to cause deadlock. .PP The big problem with this approach is that if you don\*(Aqt have control over source code being run in the child process, you can\*(Aqt control what it does with pipe buffering. Thus you can\*(Aqt just open a pipe to \&\f(CW\*(C`cat \-v\*(C'\fR and continually read and write a line from it. .PP The IO::Pty and Expect modules from CPAN can help with this, as they provide a real tty (well, a pseudo\-tty, actually), which gets you back to line buffering in the invoked command again. .SH WARNING .IX Header "WARNING" The order of arguments differs from that of \f(CW\*(C`open3\*(C'\fR from IPC::Open3. .SH "SEE ALSO" .IX Header "SEE ALSO" See IPC::Open3 for an alternative that handles \f(CW\*(C`STDERR\*(C'\fR as well. This function is really just a wrapper around \f(CW\*(C`open3\*(C'\fR.