0

I normally use a code like following to pipe data from a file to gnuplot and create a picture during the Perl script:

#!/usr/bin/perl
use warnings;
use strict;

my $in="file.dat";

open(GP, "| gnuplot") or die "$!\n";
print GP << "GNU_EOF";

set terminal png size 1920,1080 font 'Verdana,15' dashed
set output 'out.png'
plot "$in"

GNU_EOF

close(GP);

I have to define "GNU_EOF" instead of 'GNU_EOF' so I can use variables like $in.

Now I want to use data which isn't read from a file directly. My code looks like:

#!/usr/bin/perl
use warnings;
use strict;

open(GP, "| gnuplot") or die "$!\n";
print GP << 'GNU_EOF';

set terminal png size 1920,1080 font 'Verdana,15' dashed
set output 'out.png'
plot '-'

GNU_EOF

open(INFILE,"< stuff.dat") or die "$!\n";
while (my $line = <INFILE>) {

for my $i (1..10){
    # do some stuff to calculate my data points stored in $x and $y
    print GP "$x $y\n";
}
print GP "EOF\n";
}

close(INFILE);
close(GP);

If I try this using "GNU_EOF" to be able to define variables in the heredoc, I am getting errors like:

gnuplot> 187 0.05
         ^
         line 1: invalid command

I don't know

  • why I have to use "" for the heredoc to get the desired variable expansion and

  • why I get errors for the second example.

Help is highly appreciated.

4

1 回答 1

0

我解决了。抱歉问题不完整,我想举一个最小的例子以避免混淆。不幸的是,我的问题错过了重要的部分。就像评论中所说的那样,我使用几个循环来生成数据并将其通过管道传输到 gnuplot:

#!/usr/bin/perl
use warnings;
use strict;

open(GP, "| gnuplot") or die "$!\n";
print GP << 'GNU_EOF';

set terminal png size 1920,1080 font 'Verdana,15' dashed
set output 'out.png'
plot    '-' t "one", \
        '-' t "two"

GNU_EOF

open(INFILE,"< stuff.dat") or die "$!\n";
while (my $line = <INFILE>) {

for my $i (1..10){
    # do some stuff to calculate data points stored in $x and $y
    print GP "$x $y\n";
}
print GP "EOF\n";
}

for my $i (1..50){
    # do some other stuff to calculate data points stored in $x and $y
    print GP "$x $y\n";
}
print GP "EOF\n";
}

close(INFILE);
close(GP);

我不知道为什么,但是使用'GNU_EOF'(没有变量扩展)我可以plot用 line 刹车命令定义几个\

使用"GNU_EOF"我必须在一行中定义它:

plot '-' t "one", '-' t "two"

很抱歉这场斗争,但也许这对其他人也有帮助(也许你可以向我解释这种行为)。

于 2013-12-06T15:36:12.597 回答