2
votes

I have the following function to plot data using perl + gnuplot. Input data files are quite numerous and about half of them produce invalid ranges and I want to detect and delete those bad files. The function only processes a single file and I could not get it to read any gnuplot output, it just blocks. I tried reading GPR/GPE in vain.

sub plot_file {
  my $filename = shift;
  if ($filename =~ m![^\/]+/([^.]+)\.([^.]+)\.([^.]+)!) {
    my ($node, $box, $metric) = ($1, $2, $3);
    my $plot_file = "plots/$node.$box.$metric.eps";
    if ($metric !~ /Sqr/) {
      open3(\*GPW, \*GPR, \*GPE, "$gnuplot") or die "no gnuplot";
      print GPW "set terminal postscript 'Consola' 12\n";
      print GPW "NODE      = '$node'\n";
      print GPW "BOX       = '$box'\n";
      print GPW "METRIC    = '$metric'\n";
      print GPW "DATA_FILE = '$filename'\n";
      print GPW "OUT_FILE  = '$plot_file'\n";
      print GPW "set size 1.0,0.5\n";
      print GPW "set title BOX.' - '.NODE.' - '.METRIC\n";
      print GPW "set output OUT_FILE\n";
      print GPW "plot DATA_FILE using 1:2 with lines notitle\n";
      my $skip;
      while (<GPE>) {
        if (/Warning/) {
          system ("rm $plot_file");
          $skip = 1;
          last;
        }
      }
      if (not $skip) {
        system ("epstopdf $plot_file");
      }
    }
  }
}
1

1 Answers

1
votes

The external command will probably not produce any output until you close the input stream to that command to tell the command that no more input is expected. Call

close GPW;

after your last print GPW ... statement.