如何从Perl调用一个Gnuplot脚本。

3

我有一个gnu.gp文件:

# grphist.conf
set terminal canvas
#Terminal type set to 'canvas'
#Options are ' solid butt size 600,400 fsize 10 lw 1 fontscale 1 standalone'
set output 'output.html'  

set grid
set xtic rotate by 90
set style data histograms
set style fill solid 1.00 border -1
#$ cat grphist.conf | gnuplot
plot "c_time"  using 2:xtic(1) title "time to number of UIDs"

但是,我必须将其与 Perl 脚本集成。

2个回答

10

您可以打开一个管道连接到gnuplot:

use autodie qw(:all);
open my $GP, '|-', 'gnuplot';

print {$GP} <<'__GNUPLOT__';
    set xrange [-5:5];
    plot sin(x);
__GNUPLOT__

close $GP;

或者,您可以访问 CPAN 上的 Chart::Gnuplot


当我尝试运行你的代码时,出现了一个错误 Can't find string terminator "__GNUPLOT__" anywhere before EOF at scripts/temp.pl。你有什么想法吗?谢谢。 - SSilk
@SSilk:奇怪,我可以清楚地看到第7行的__GNUPLOT__ - choroba
谢谢您的快速响应!我已经解决了这个问题。我把您的代码片段放在缩进的代码块中了。它不喜欢第7行上的 __GNUPLOT__ 被缩进。当它缩进时,我应该注意到它后面的语法高亮出现了错误。 - SSilk
@SSilk: 没错。虽然在即将发布的 Perl 5 版本中可能会出现缩进文档。 - choroba
谢谢。我还有一个问题。当我现在运行你的代码时,gnuplot的结果会打印在屏幕上(我正在使用term dumb)。我能否将它们捕获到一个变量中,例如my $temp以便稍后打印?我无法弄清楚,但我对这个管道语法还不太熟悉。 - SSilk
@SSilk:通常,您会将提示设置为“png”或“pdfcairo”或其他内容,并使用“set output“文件名””。 - choroba

2
`gnuplot <your file>`; #linux
`wgnuplot.exe <your file>`; #win

或者

system("gnuplot <your file>"); #linux
system("wgnuplot.exe <your file>"); #win

或者

exec("gnuplot <yout file>"); #linux
exec("wgnuplot.exe <your file>"); #win

你选择哪种方式取决于:

Perl中反引号、system和exec的区别是什么?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接