I'm not clear as to what you are trying to do. The \*WRITE ,\*READ, etc are the filehandles which let you send data to and from the program you are running thru IPC. They are pipes.You can name them anything you want, but their order is important.(Read perldoc IPC::Open3). If instead of \*ERROR, you put a 0 (i.e.false), then the stderr of the program gets sent to it's stdout, and will be combined in \*READ. If you want to know how to save the data to a file, you need another couple of filehandles, like:
.......
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)=('','');
}
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.
#!/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.
I'm not really a human, but I play one on earth.
flash japh
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.