I'm trying to make a script using Perl and the
Chart::Gnuplot
module for Linux to represent a series of data. It is something like this:
use strict;
use warnings;
use Chart::Gnuplot;
die "Linux check" if ( $^O ne 'linux' );
my $chart = Chart::Gnuplot->new(
terminal => 'x11',
title => {
text => "GRAPH",
font => "Arial, 20"
},
xlabel => {
text => "X AXIS",
font => "arial, 20",
offset =>"0,-1"
},
ylabel => {
text => "Y AXIS",
font => "arial, 20",
offset =>"-6,0"
},
);
my $dataSet = Chart::Gnuplot::DataSet->new(
xdata => \@x_data,
ydata => \@y_data,
style => "points",
);
$chart->plot2d($dataSet);
exit;
When I run this from Eclipse, the script works OK and the graph shows correctly, but the process won't terminate (closing the Gnuplot window won't work). Something similar happens when I run it at the terminal.
The problematic line is $chart->plot2d($dataSet)
without which it can terminate correctly. What could I do to end it right after closing the window?
Chart::Gnuplot
runs gnuplot
with the same stdin and other file descriptors open. After drawing the picture, gnuplot
reads stdin waiting for more commands.
One solution is to simply redirect stdin to /dev/null
in your perl program before doing the plot. This will also make the image immediately appear and disappear, so you should also add the persist
option. Here are the 2 lines to change:
my $chart = Chart::Gnuplot->new(
terminal => 'x11 persist',
...
open(STDIN, "<", "/dev/null") or die "Can't open /dev/null: $!";
$chart->plot2d($dataSet);