I'm trying to write a generalized function that I can use to execute a program and capture it's STDOUT, STDERR, and exit code. In the process of developing this function I have found a problem that I can't understand.

When I attempt to execute a program which doesn't exist, I simply want to trap that and return an error from the function to the user stating that. In the following program the final print statement appears to be printing twice. Once to print my value of $error, and once to print the value that open3 dies with.

#!/usr/bin/perl -w use IPC::Open3; use IO::Select; use POSIX ":sys_wait_h"; use Symbol; sub run { my ($WRITE, $READ, $ERROR); $ERROR=gensym(); my $command="not_a_command"; my $error=""; my $output=""; eval { $pid=open3($WRITE, $READ, $ERROR, "$command"); }; if($@) { $error="Error, Could not execute $command: $!"; } else { close($WRITE); my $selector=IO::Select->new(); $selector->add($READ, $ERROR); while(@ready=$selector->can_read) { foreach (@ready) { if(fileno($_)==fileno($READ)) { $bytes=sysread($READ, $text, 1024); if($bytes==0) { $selector->remove($_); } else { $output.=$text; } } elsif(fileno($_)==fileno($ERROR)) { $bytes=sysread($ERROR, $text, 1024); if($bytes==0) { $selector->remove($_); } else { $error.=$text; } } } } waitpid($pid, 0); } return($output, $error); } my ($output, $error); ($output, $error)=run(); print "$output, $error\n";
The output:
jlm17 5660> ./test_eval.pl , Error, Could not execute not_a_command: No such file or directory , Can't exec "not_a_command": No such file or directory at /usr/share/ +perl/5.8/IPC/Open3.pm line 168.
And what is odd is that if I modify the final print statment of the program to this:

print "$error\n";

Then it only prints one line. It prints two lines when I have it printing the $output also, even though there is nothing to print out.

What is happening here, and more importantly, how do I make it stop?


In reply to Doubled print with eval and open3 by jlm17

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.