Georg Georg - 3 months ago 48
Perl Question

How to call a gnuplot one-liner from Perl using system()?

I would like a Perl script to display a simple gnuplot graph. I don't want to store any data in a file, I want to use a gnuplot one-liner such as:

gnuplot -p <(echo -e 'plot "-"\n1 1\n2 3\n3 1.7\n4.5 5\ne')

That displays the points (1, 1), (2, 3), (3, 1.7) and (4.5, 5).

In a Perl script, I tried things like

$plotString = "\"<(echo -e 'plot \\\"-\\\"\\n";
$plotString .= "1 1\\n2 3\\n3 1.7\\n4.5 5\\ne')\"";
system('gnuplot -p ' . $plotString);

but I get the following error:

-e plot "-"
"<(echo -e 'plot "-"\n1 1\n2 3\n3 1.7\n4.5 5\ne')", line 1: invalid command

This error surprises me because the string passed to
, as quoted in the error message, is apparently correct.

Any idea how to modify
would correctly interpret the gnuplot command?

Secondary question: how to draw the graph
with lines
? (I couldn't get the gnuplot one-liner to do it, even outside Perl.)

Edit: My OS is Ubuntu 16.04.

Answer Source

is short for

system('/bin/sh', '-c', $shell_cmd)

But what you have there is not a valid sh command, but a bash command. That means you'll need to invoke bash instead.

my $cmd = q{gnuplot -p <(echo -e 'plot "-"\\n1 1\\n2 3\\n3 1.7\\n4.5 5\\ne')};
system('bash', '-c', $cmd)

It appears you could also use the following to avoid creating two shells:

my $program = <<'__EOS__';
plot "-"
1 1
2 3
3 1.7
4.5 5

open(my $pipe, '|-', "gnuplot", "-p")
print($pipe $program);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download