in reply to Re^2: example for ipc::open3
in thread example for ipc::open3
If you want to read input from a file, you need to open that file, and read it line by line, and print each line to the WRITE handle, then wait for output. It sounds like you need to experiment with it a little bit. Here is a simpler version, without a separate stderr........ open(OUT,">file_out) or warn "$!\n"; # then where you read the output print it to # the OUT filehandle #get the answer from bc sysread(READ,$answer,4096) if $selread->can_read(0); if($answer){print OUT "$query = $answer\n"} ($error,$answer)=('',''); }
#!/usr/bin/perl use warnings; use strict; use IPC::Open3; #interface to "bc" calculator #my $pid = open3(\*WRITE, \*READ, \*ERROR,"bc"); my $pid = open3(\*WRITE, \*READ,0,"bc"); #if \*ERROR is false, STDERR is sent to STDOUT while(1){ print "Enter expression for bc, i.e. 2 + 2\n"; chomp(my $query = <STDIN>); #send query to bc print WRITE "$query\n"; select(undef,undef,undef,2); #get the answer from bc chomp(my $answer = <READ>); print "$query = $answer\n"; } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: example for ipc::open3
by Anonymous Monk on Jan 07, 2005 at 23:17 UTC | |
by zentara (Cardinal) on Jan 08, 2005 at 12:11 UTC |