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')
$plotString = "\"<(echo -e 'plot \\\"-\\\"\\n";
$plotString .= "1 1\\n2 3\\n3 1.7\\n4.5 5\\ne')\"";
system('gnuplot -p ' . $plotString);
-e plot "-"
^
"<(echo -e 'plot "-"\n1 1\n2 3\n3 1.7\n4.5 5\ne')", line 1: invalid command
system()
$plotString
system()
with lines
system($shell_cmd)
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
e
__EOS__
open(my $pipe, '|-', "gnuplot", "-p")
print($pipe $program);
close($pipe);