代码之家  ›  专栏  ›  技术社区  ›  claf

为什么我的Perl系统调用会因dot而失败?

  •  2
  • claf  · 技术社区  · 14 年前

    # First command is :
    # dot -Tpng $dottmpfile > $pngfile
    # Second command is :
    # rm $dottmpfile
    
    if (!($pngfile eq "")) {
      my @args = ("dot", "-Tpng", $dottmpfile, " > ", $pngfile);
      system (join (' ' , @args ))
        or die "system @args failed : $!";
    
      unlink $dottmpfile;
    }
    

    system dot -Tpng toto.dot  >  toto.png failed : Inappropriate ioctl for device at /home/claferri/bin/fractal.pl line 79.
    

    我用过 system 产生这段代码。

    4 回复  |  直到 14 年前
        1
  •  3
  •   Sinan Ünür    14 年前

    看着 perldoc -f system ,注:

    如果LIST中有多个参数,或者LIST是一个具有多个值的数组,则用列表中其他元素给出的参数启动列表中第一个元素给出的程序。如果只有一个标量参数,则检查该参数是否有shell元字符,如果有,则将整个参数传递给系统的命令shell进行解析

    你正在调用 system LIST > 最后被传给 dot 而不是由shell来解释。

    我建议你继续使用 系统列表 -o ,那就这么做吧。

    如果你真的想打点你的 T s(双关语不是有意的),那么您可以使用:

    if ( defined $pngfile and $pngfile ne '') {
        my @args = (dot => '-Tpng', $dottmpfile, "-o$pngfile");
        if ( system @args ) {
            warn "'system @args' failed\n";
            my $reason = $?;
            if ( $reason == -1 ) {
                die "Failed to execute: $!";
            }
            elsif ( $reason & 0x7f ) {
                die sprintf(
                    'child died with signal %d, %s coredump',
                    ($reason & 0x7f),  
                    ($reason & 0x80) ? 'with' : 'without'
                );
            }
            else {
                die sprintf('child exited with value %d', $reason >> 8);
            }
        }
        warn "'system @args' executed successfully\n";
        unlink $dottmpfile;
    }
    
        2
  •  3
  •   Sinan Ünür    14 年前

    > system LIST ,您正在绕过shell。因此,您可以使用:

    system ( join (' ' , @args ) ); 
    

    system "@args";
    
        3
  •  1
  •   mob    14 年前

    system 系统

    system($command) and warn "system $command: failed $?\n";   # and not or
    

    if (system($command) != 0) { ... handle error ... }
    
        4
  •  -1
  •   Sinan Ünür    14 年前

    PATH

    据我所知,这似乎是正确的 perldoc -f system