【问题标题】:Piping heredoc from Perl to Gnuplot从 Perl 到 Gnuplot 的管道 heredoc
【发布时间】:2013-08-07 15:06:50
【问题描述】:

很快我就遇到了问题,无法从 Perl 脚本通过管道传输到 Gnuplot 本身的 Gnuplot 命令墙。我的主要起始参考是perlmonks。然后子函数看起来像:

sub binPics {

my $inFileName = shift;
my $outFileName = shift;
my $outputFormatPics = shift;

open(GP, "| gnuplot") or die "Error while piping to Gnuplot: $! \n";
    print GP << "   GNU_EOF"

    plot "$inFileName" u 2 lw 2.5 lc 1 , "" u 3 lw 2.5 lc 2
    set terminal $outputFormatPics
    set output '$outFileName.$outputFormatPics'
    replot

    GNU_EOF
}

在此之后,第二个子函数使用类似的语法但不同的 Gnuplot 命令定义。我按照我定义它们的顺序调用这些子函数。稍后在脚本中生成的图片将被进一步使用。这会产生错误。

那么这里的问题是什么?可能的运行脚本会是什么样子?

我将为这个问题提供我的固定脚本,但无法解释整个 heredoc 语法。随意这样做或提供其他建议。

/编辑

缩进不再以 SO 样式显示。 heredoc 中的行将制表符作为第一个命令(用于构建代码)。

【问题讨论】:

    标签: perl pipe gnuplot


    【解决方案1】:

    主要问题是缺少关闭文件句柄GP。如果没有关闭,第一个子函数不会引起任何问题(或者更具体地说:应该在脚本中稍后使用的生成图片在这里不会产生错误),因为第二个子函数因为第二个 @987654322 而关闭了调用@- 陈述。但是在第二个子功能中,管道没有关闭,因此可能会导致错误。

    关闭管道时,还需要在print 语句处添加分号。我不知道为什么没有close(GP) 的脚本没有缺少分号的问题,我也不知道制表符缩进是否有问题。

    尽管如此,这对我有用,也许有人也有兴趣:

    sub binPics {
    
    my $inFileName = shift;
    my $outFileName = shift;
    my $outputFormatPics = shift;
    
    open(GP, "| gnuplot") or die "Error while piping to Gnuplot: $! \n";
    print GP << "GNU_EOF";
    
    plot "$inFileName" u 2 lw 2.5 lc 1 , "" u 3 lw 2.5 lc 2
    set terminal $outputFormatPics
    set output '$outFileName.$outputFormatPics'
    replot
    
    GNU_EOF
    close(GP);
    }
    

    【讨论】:

    • 还要记住,裸字文件句柄实际上是全局变量的一种形式。如果你定义了一个词法文件句柄,如:open(my $gp, "| gnuplot"),它会在块末尾超出范围时自动关闭。
    • 啊,现在说得通了,谢谢!顺便提一句。似乎缺少的close 并不总是脚本的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2013-07-06
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    相关资源
    最近更新 更多