Hello everyone,
I am looking for a way to open an external program, feed a set of data to it and capturing the output in a variable, much like using
@var = `extprog blah blaha` - extprog returns a set of values each time it gets input.
The problem is that I have a lot of data (several million sets of coordinates) and it takes a bit of time to open and initialize the external program each time, hence I would like to open the external program once and feed the data through a pipe into the program, I was imagining something like this: (note that the code might have errors, just showing for illustration)
open EXT,"|extprog";
while (<>){
@data = conv_data($_);
print EXT "$data[0] $data[1]\n";
}
close EXT;
Now, my external program will get this data and put it's data on stdout. The question is how do I get hold of/redirect the output, preferrably without resorting to temporary files.
rgds,
Ole C.
Update
I got this sorted now, thanks for the replies. I also got a couple of requests to share my final code and what I ended up doing in the end, so I will post it here.
The output from my external program was buffered, so I could not use open3 as the read/write deadlocked. Hence I ended up using brother merlyns suggestion which at first seemed like pure magic but which made more sense after reading up on -|
My final code for achieving what I wanted then looked like this:
$run =
"mapproject -F --D_FORMAT=\%lf -I --ELLIPSOID=HELMERT1906 -Jt31/30/1:
+1 -R31/40/30/39 --MAP_SCALE_FACTOR=1.0000 ";
open FH, "<depths.asc.to" or die "could not open asc file\n";
if ( $pid = open RESULTS, "-|" ) {
# I am the original process, so read from <RESULTS> below
}
else {
# I am the kid
if ( $pid1 = open STDIN, "-|" ) {
# I am still the kid, but my STDIN is now piped from the grand
+kid
exec "$run";
}
else { # I am the grandkid: be a data pump
while (<FH>) {
print "$_\n";
}
exit 0;
}
}
while (<RESULTS>) {
print $_;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.