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.
The output:#!/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";
And what is odd is that if I modify the final print statment of the program to this: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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |