in reply to Re^2: Executing command from perl script with input/output
in thread Executing command from perl script with input/output
Try this:
sub ExecCmd { my $cmd = shift; my $pid = open3(\*WRITE,\*READ,\*ERROR,$cmd); unless (defined $pid) { LogError("Failed to execute $cmd"); return undef; } LogInfo("PID is $pid"); select(undef,undef,undef, 1); a little delay you can try #print WRITE "0\n"; #try it here too my $selread = new IO::Select(); my $selerror = new IO::Select(); $selread->add(\*READ); $selerror->add(\*ERROR); # may not be best use of IO::Select my($error,$answer)=('',''); #see which filehandles have output if($selread->can_read(0)){print "ready->read\n"} if($selerror->can_read(0)){print "ready->error\n"} #get any output sysread(ERROR,$error,4096) if $selerror->can_read(0); if($error){print "ERROR-> $error\n"} sysread(READ,$answer,4096) if $selread->can_read(0); if($answer){print "Response = $answer\n"} ($error,$answer)=('',''); print WRITE "0\n";
Also, if you don't care what the initial output of the program is, you can avoid IPC::Open3 altogether, and use a piped open. Like:
my $pid = open( FH, "| $cmd") or warn "$!\n"; print FH "0\n"; #maybe need syswrite here
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Executing command from perl script with input/output
by linuxfan (Beadle) on Aug 09, 2005 at 19:48 UTC | |
by zentara (Cardinal) on Aug 10, 2005 at 11:30 UTC | |
by linuxfan (Beadle) on Aug 10, 2005 at 18:26 UTC | |
by zentara (Cardinal) on Aug 11, 2005 at 10:27 UTC | |
by linuxfan (Beadle) on Aug 12, 2005 at 21:15 UTC |