Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

2-way communication

I need to do the following because I am using mod_perl and I do not want to write then read from a temp file:

1. My script pipes info to gnuplot beta 3.6.
2. Gnuplot reads config info from standard in, creates plot in GIF format and outputs it on standard out.
3. My script captures gnuplot output to a varaible.
4. My script outputs varaible to web page.

I tried IPC::Open2 but could not capture all the output.
0
hartlieb
Asked:
hartlieb
  • 2
  • 2
1 Solution
 
n3mtrCommented:
You need to use a child process for this one.
Try this:
pipe(PARENT,CHILD);
if(! fork()){
   #this is executed by the child
   close(PARENT);
   open(STDOUT,">&CHILD"); #redirect stdout to child pipe
   open(PLOT,"|gnuplot");
   #print your output to gnuplot here
   close(PLOT);
   close(CHILD);
   close(STDOUT);
   exit; #end child process
} else {
   #this is executed by the parent
   close(CHILD);
   #gnuplot output is available with the PARENT filehandle
   close(PARENT);
}
If you have any questions about how it works, just ask.
0
 
hartliebAuthor Commented:
This works but I need to clarify how to get all the output
from gnuplot.

I tried the following and It seems to work.

while (<PARENT>) {
   $Gif .= $_;
}

Is there a better way as the output from gnuplot is binary?

Thanks for the quick response,


0
 
n3mtrCommented:
do this:
undef($/); #make sure this is a forward slash
$Gif = <PARENT>;

undefining the $/ makes it read the whole file(pipe) at once.
You may want to change it back to $/="\n"; if you read any files after this one.

0
 
hartliebAuthor Commented:
Thanks for the quick response. It was a nagging problem.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now