<p>In the middle of a Perl script, there is a system command I want to execute. I have a string that contains the data that needs to be fed into stdin (the command only accepts input from stdin), and I need to capture the output written to stdout. I've looked at the various methods of executing system commands in Perl, and the <code>open</code> function seems to be what I need, except that it looks like I can only capture stdin or stdout, not both.</p>

<p>At the moment, it seems like my best solution is to use <code>open</code>, redirect stdout into a temporary file, and read from the file after the command finishes. Is there a better solution?</p>

[{'answer_id': 78102, 'author': 'mopoke', 'author_id': 14054, 'author_profile': 'https://Stackoverflow.com/users/14054', 'pm_score': 2, 'selected': False, 'text': '<p>IPC::Open3 would probably do what you want. It can capture STDERR and STDOUT.</p>\n\n<p><a href="http://metacpan.org/pod/IPC::Open3" rel="nofollow noreferrer">http://metacpan.org/pod/IPC::Open3</a></p>\n'}, {'answer_id': 78109, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 2, 'selected': True, 'text': '<p>I think you want to take a look at <a href="http://search.cpan.org/~rgarcia/perl-5.10.0/lib/IPC/Open2.pm" rel="nofollow noreferrer">IPC::Open2</a></p>\n'}, {'answer_id': 78154, 'author': 'X-Istence', 'author_id': 13986, 'author_profile': 'https://Stackoverflow.com/users/13986', 'pm_score': 1, 'selected': False, 'text': '<p>There is a special perl command for it</p>\n\n<pre><code>open2()\n</code></pre>\n\n<p>More info can be found on: <a href="http://sunsite.ualberta.ca/Documentation/Misc/perl-5.6.1/lib/IPC/Open2.html" rel="nofollow noreferrer">http://sunsite.ualberta.ca/Documentation/Misc/perl-5.6.1/lib/IPC/Open2.html</a></p>\n'}, {'answer_id': 78201, 'author': 'benzado', 'author_id': 10947, 'author_profile': 'https://Stackoverflow.com/users/10947', 'pm_score': 2, 'selected': False, 'text': "<p>Somewhere at the top of your script, include the line</p>\n\n<pre><code>use IPC::Open2;\n</code></pre>\n\n<p>That will include the necessary module, usually installed with most Perl distributions by default.  (If you don't have it, you could install it using CPAN.)  Then, instead of open, call:</p>\n\n<pre><code>$pid = open2($cmd_out, $cmd_in, 'some cmd and args');\n</code></pre>\n\n<p>You can send data to your command by sending it to $cmd_in and then read your command's output by reading from $cmd_out.</p>\n\n<p>If you also want to be able to read the command's stderr stream, you can use the IPC::Open3 module instead.</p>\n"}, {'answer_id': 78323, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>I always do it this way if I'm only expecting a single line of output or want to split the result on something other than a newline:</p>\n\n<pre><code>my $result = qx( command args 2&gt;&amp;1 );  \nmy $rc=$?;  \n# $rc &gt;&gt; 8 is the exit code of the called program.\n\nif ($rc != 0 ) {  \n    error();  \n}  \n</code></pre>\n\n<p>If you want to deal with a multi-line response, get the result as an array:</p>\n\n<pre><code>my @lines = qx( command args 2&gt;&amp;1 );  \n\nforeach ( my $line ) (@lines) {  \n    if ( $line =~ /some pattern/ ) {  \n        do_something();  \n    }  \n}  \n</code></pre>\n"}, {'answer_id': 79308, 'author': 'brian d foy', 'author_id': 2766176, 'author_profile': 'https://Stackoverflow.com/users/2766176', 'pm_score': 2, 'selected': False, 'text': '<p>The <a href="http://perldoc.perl.org/perlipc.html" rel="nofollow noreferrer">perlipc documentation</A> covers many ways that you can do this, including IPC::Open2 and IPC::Open3.</p>\n'}, {'answer_id': 79723, 'author': 'xdg', 'author_id': 11800, 'author_profile': 'https://Stackoverflow.com/users/11800', 'pm_score': 3, 'selected': False, 'text': '<p>IPC::Open2/3 are fine, but I\'ve found that usually all I really need is <a href="http://metacpan.org/pod/IPC::Run3" rel="nofollow noreferrer">IPC::Run3</a>, which handles the simple cases really well with minimal complexity:</p>\n\n<pre><code>use IPC::Run3;    # Exports run3() by default\n\nrun3( \\@cmd, \\$in, \\$out, \\$err );\n</code></pre>\n\n<p>The documentation compares IPC::Run3 to other alternatives.  It\'s worth a read even if you don\'t decide to use it.</p>\n'}, {'answer_id': 81251, 'author': 'stephanea', 'author_id': 8776, 'author_profile': 'https://Stackoverflow.com/users/8776', 'pm_score': 0, 'selected': False, 'text': '<p>If you do not want to include extra packages, you can just do</p>\n\n<pre><code>open(TMP,"&gt;tmpfile");\nprint TMP  $tmpdata ;\nopen(RES,"$yourcommand|");\n$res = "" ;\nwhile(&lt;RES&gt;){\n$res .= $_ ;\n}\n</code></pre>\n\n<p>which is the contrary of what you suggested, but should work also.</p>\n'}, {'answer_id': 82219, 'author': 'Aristotle Pagaltzis', 'author_id': 9410, 'author_profile': 'https://Stackoverflow.com/users/9410', 'pm_score': 2, 'selected': False, 'text': '<p>A very easy way to do this that I recently found is <strong>the <a href="http://p3rl.org/IPC::Filter" rel="nofollow noreferrer">IPC::Filter</a> module</strong>. It lets you do the job extremely intuitively:</p>\n\n<pre><code>$output = filter $input, \'somecmd\', \'--with\', \'various=args\', \'--etc\';\n</code></pre>\n\n<p>Note how it invokes your command without going through the shell if you pass it a list. It also does a reasonable job of handling errors for common utilities. (On failure, it <code>die</code>s, using the text from STDERR as its error message; on success, STDERR is just discarded.)</p>\n\n<p>Of course, it’s not suitable for huge amounts of data since it provides no way of doing any streaming processing; also, the error handling might not be granular enough for your needs. But it makes the many simple cases really <em>really</em> simple.</p>\n'}]
['https://Stackoverflow.com/questions/78091', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']