jlm17 has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Doubled print with eval and open3
by almut (Canon) on Mar 06, 2007 at 00:04 UTC | |
by ikegami (Patriarch) on Mar 06, 2007 at 01:07 UTC | |
by almut (Canon) on Mar 06, 2007 at 01:31 UTC | |
by ikegami (Patriarch) on Mar 06, 2007 at 01:49 UTC | |
by OfficeLinebacker (Chaplain) on Mar 06, 2007 at 04:10 UTC | |
|
Re: Doubled print with eval and open3
by GrandFather (Saint) on Mar 05, 2007 at 22:41 UTC | |
by ikegami (Patriarch) on Mar 06, 2007 at 00:03 UTC | |
by GrandFather (Saint) on Mar 06, 2007 at 00:33 UTC | |
by ikegami (Patriarch) on Mar 06, 2007 at 00:52 UTC | |
|
Re: Doubled print with eval and open3
by jlm17 (Initiate) on Mar 06, 2007 at 18:06 UTC | |
by ikegami (Patriarch) on Mar 06, 2007 at 18:24 UTC |