{
    "mode": "perldoc",
    "parameter": "IPC::Open2",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/IPC%3A%3AOpen2/json",
    "generated": "2026-05-30T06:07:36Z",
    "synopsis": "use IPC::Open2;\nmy $pid = open2(my $chldout, my $chldin,\n'some', 'cmd', 'and', 'args');\n# or passing the command through the shell\nmy $pid = open2(my $chldout, my $chldin, 'some cmd and args');\n# read from parent STDIN and write to already open handle\nopen my $outfile, '>', 'outfile.txt' or die \"open failed: $!\";\nmy $pid = open2($outfile, '<&STDIN', 'some', 'cmd', 'and', 'args');\n# read from already open handle and write to parent STDOUT\nopen my $infile, '<', 'infile.txt' or die \"open failed: $!\";\nmy $pid = open2('>&STDOUT', $infile, 'some', 'cmd', 'and', 'args');\n# reap zombie and retrieve exit status\nwaitpid( $pid, 0 );\nmy $childexitstatus = $? >> 8;",
    "sections": {
        "NAME": {
            "content": "IPC::Open2 - open a process for both reading and writing using open2()\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use IPC::Open2;\n\nmy $pid = open2(my $chldout, my $chldin,\n'some', 'cmd', 'and', 'args');\n# or passing the command through the shell\nmy $pid = open2(my $chldout, my $chldin, 'some cmd and args');\n\n# read from parent STDIN and write to already open handle\nopen my $outfile, '>', 'outfile.txt' or die \"open failed: $!\";\nmy $pid = open2($outfile, '<&STDIN', 'some', 'cmd', 'and', 'args');\n\n# read from already open handle and write to parent STDOUT\nopen my $infile, '<', 'infile.txt' or die \"open failed: $!\";\nmy $pid = open2('>&STDOUT', $infile, 'some', 'cmd', 'and', 'args');\n\n# reap zombie and retrieve exit status\nwaitpid( $pid, 0 );\nmy $childexitstatus = $? >> 8;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The open2() function runs the given command and connects $chldout for\nreading and $chldin for writing. It's what you think should work when\nyou try\n\nmy $pid = open(my $fh, \"|cmd args|\");\n\nThe $chldin filehandle will have autoflush turned on.\n\nIf $chldout is a string (that is, a bareword filehandle rather than a\nglob or a reference) and it begins with \">&\", then the child will send\noutput directly to that file handle. If $chldin is a string that begins\nwith \"<&\", then $chldin will be closed in the parent, and the child\nwill read from it directly. In both cases, there will be a dup(2)\ninstead of a pipe(2) made.\n\nIf either reader or writer is the empty string or undefined, this will\nbe replaced by an autogenerated filehandle. If so, you must pass a valid\nlvalue in the parameter slot so it can be overwritten in the caller, or\nan exception will be raised.\n\nopen2() returns the process ID of the child process. It doesn't return\non failure: it just raises an exception matching \"/^open2:/\". However,\n\"exec\" failures in the child are not detected. You'll have to trap\nSIGPIPE yourself.\n\nopen2() does not wait for and reap the child process after it exits.\nExcept for short programs where it's acceptable to let the operating\nsystem take care of this, you need to do this yourself. This is normally\nas simple as calling \"waitpid $pid, 0\" when you're done with the\nprocess. Failing to do this can result in an accumulation of defunct or\n\"zombie\" processes. See \"waitpid\" in perlfunc for more information.\n\nThis whole affair is quite dangerous, as you may block forever. It\nassumes it's going to talk to something like bc(1), both writing to it\nand reading from it. This is presumably safe because you \"know\" that\ncommands like bc(1) will read a line at a time and output a line at a\ntime. Programs like sort(1) that read their entire input stream first,\nhowever, are quite apt to cause deadlock.\n\nThe big problem with this approach is that if you don't have control\nover source code being run in the child process, you can't control what\nit does with pipe buffering. Thus you can't just open a pipe to \"cat -v\"\nand continually read and write a line from it.\n\nThe IO::Pty and Expect modules from CPAN can help with this, as they\nprovide a real tty (well, a pseudo-tty, actually), which gets you back\nto line buffering in the invoked command again.\n",
            "subsections": []
        },
        "WARNING": {
            "content": "The order of arguments differs from that of open3().\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "See IPC::Open3 for an alternative that handles STDERR as well. This\nfunction is really just a wrapper around open3().\n",
            "subsections": []
        }
    },
    "summary": "IPC::Open2 - open a process for both reading and writing using open2()",
    "flags": [],
    "examples": [],
    "see_also": []
}