Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Perl中组合来自STDIN和文件的输入_Perl_File_File Handling - Fatal编程技术网

在Perl中组合来自STDIN和文件的输入

在Perl中组合来自STDIN和文件的输入,perl,file,file-handling,Perl,File,File Handling,我希望我的Perl脚本能够做到这一点 ./code1 param1 param2 param3 | perl myperlscript.pl param1 param2 因此Perl脚本应该包含3个输入: 参数1 参数2 /code 1参数1参数2参数3的输出 我的Perl代码中有这一点,但它出错了: my $param1 = $ARGV[0] || "control.fst"; my $param2 = $ARGV[1] || "target.fst"; my @control_head

我希望我的Perl脚本能够做到这一点

./code1 param1 param2 param3 | perl myperlscript.pl param1 param2
因此Perl脚本应该包含3个输入:

  • 参数1
  • 参数2
  • /code 1参数1参数2参数3的输出
  • 我的Perl代码中有这一点,但它出错了:

    my $param1 = $ARGV[0] || "control.fst";
    my $param2  = $ARGV[1] || "target.fst";
    
    my @control_header = `grep ">" $param1`;
    my @target_header = `grep ">" $param2`;
    # Later to do something with those arrays
    
    while (<>) {
     # do something again
    }
    
    my$param1=$ARGV[0]| |“control.fst”;
    my$param2=$ARGV[1]| |“target.fst”;
    我的@control_头=`grep'>“$param1`”;
    my@target_header=`grep'>“$param2`;
    #稍后,我们将对这些数组执行一些操作
    而(){
    #再做一次
    }
    
    我知道我可以为
    /code1
    进行临时输出。 但由于输出量很大,我更喜欢用管道输送。
    请告知正确的方法。

    您有两种选择

    1) 阅读标准

    while (<STDIN>) {
    chomp();  
    # do something again
    
    }
    
    while(){
    chomp();
    #再做一次
    }
    
    2) 从Perl脚本调用第一个程序并处理其输出。看

    open(PRG,“./code参数1参数2参数3 |”)或die$!;
    而(){
    chomp();
    #再做一次
    }
    
    您有两种选择

    1) 阅读标准

    while (<STDIN>) {
    chomp();  
    # do something again
    
    }
    
    while(){
    chomp();
    #再做一次
    }
    
    2) 从Perl脚本调用第一个程序并处理其输出。看

    open(PRG,“./code参数1参数2参数3 |”)或die$!;
    而(){
    chomp();
    #再做一次
    }
    
    @ARGV
    不为空时,
    操作员将从指定的文件中读取。一旦
    @ARGV
    耗尽,将继续从
    STDIN
    读取

    如果在读取
    @control\u header
    @target\u header
    数组后显式清空
    @ARGV
    ,则程序应按预期工作。或者,您可以使用
    STDIN
    显式读取

    但我认为最好使用Perl从文件参数中读取所需的信息。这个节目展示了这个想法

    use strict;
    use warnings;
    
    my $param1 = shift || 'control.fst';
    my @control_header = do {
      open my $fh, '<', $param1 or die qq(Unable to open file "$param1": $!);
      grep />/, <$fh>;
    };
    
    my $param2 = shift || 'target.fst';
    my @target_header = do {
      open my $fh, '<', $param2 or die qq(Unable to open file "$param2": $!);
      grep />/, <$fh>;
    };
    
    while (<STDIN>) {
      # do something again
    }
    
    使用严格;
    使用警告;
    my$param1=shift | |'control.fst';
    我的@control\u header=do{
    
    @ARGV
    不为空时,打开我的$fh,,
    操作员将从此处指定的文件中读取。一旦
    @ARGV
    耗尽,读取将从
    STDIN继续

    如果在读取
    @control\u header
    @target\u header
    数组后显式清空
    @ARGV
    ,则程序应按预期工作。或者,您可以使用
    STDIN
    显式读取

    但是我认为最好使用Perl从文件参数中读取所需的信息

    use strict;
    use warnings;
    
    my $param1 = shift || 'control.fst';
    my @control_header = do {
      open my $fh, '<', $param1 or die qq(Unable to open file "$param1": $!);
      grep />/, <$fh>;
    };
    
    my $param2 = shift || 'target.fst';
    my @target_header = do {
      open my $fh, '<', $param2 or die qq(Unable to open file "$param2": $!);
      grep />/, <$fh>;
    };
    
    while (<STDIN>) {
      # do something again
    }
    
    使用严格;
    使用警告;
    my$param1=shift | |'control.fst';
    我的@control\u header=do{
    打开我的$fh,更多的perlish代码是:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $param1 = $ARGV[0] || "control.fst";
    my $param2 = $ARGV[1] || "target.fst";
    
    my @control_header;
    my @target_header;
    
    open FH, "<$param1" or die;
    map { push @control_header, $1 if /^(.*?>.*?)[\n\r]*$/ } (<FH>);
    close FH;
    
    open FH, "<$param2" or die;
    map { push @target_header,  $1 if /^(.*?>.*?)[\n\r]*$/ } (<FH>);
    close FH;
    
    ...                                                                              
    
    while (<STDIN>) {                                                                
      ...                                                                            
    }                                                                                
    
    !/usr/bin/perl
    严格使用;
    使用警告;
    my$param1=$ARGV[0]| |“control.fst”;
    my$param2=$ARGV[1]| |“target.fst”;
    我的@control_头;
    我的@target_标题;
    打开FH,“Moreperlish代码为:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $param1 = $ARGV[0] || "control.fst";
    my $param2 = $ARGV[1] || "target.fst";
    
    my @control_header;
    my @target_header;
    
    open FH, "<$param1" or die;
    map { push @control_header, $1 if /^(.*?>.*?)[\n\r]*$/ } (<FH>);
    close FH;
    
    open FH, "<$param2" or die;
    map { push @target_header,  $1 if /^(.*?>.*?)[\n\r]*$/ } (<FH>);
    close FH;
    
    ...                                                                              
    
    while (<STDIN>) {                                                                
      ...                                                                            
    }                                                                                
    
    !/usr/bin/perl
    严格使用;
    使用警告;
    my$param1=$ARGV[0]| |“control.fst”;
    my$param2=$ARGV[1]| |“target.fst”;
    我的@control_头;
    我的@target_标题;
    
    打开FH,“在您的情况下,从
    @ARGV
    中删除
    param1
    param2

    my $param1 = shift;
    my $param2 = shift;
    
    现在,您可以使用

    while (<>) {
      ...;
    }
    

    将愉快地处理。

    在您的情况下,从
    @ARGV
    中删除
    param1
    param2

    my $param1 = shift;
    my $param2 = shift;
    
    现在,您可以使用

    while (<>) {
      ...;
    }
    

    将愉快地处理。

    请解释您所说的出错是什么意思。您的问题是什么?请解释您所说的出错是什么意思。您的问题是什么?您可以选择扩展
    die
    输出并替换
    die;
    die“无法打开文件$param1”;
    或类似内容…void上下文中的映射一点也不容易消失。你真的不应该鼓励使用typeglob文件句柄或
    open
    的两参数形式。另外
    die
    字符串应该包含
    $!
    信息。你可以选择扩展
    die
    输出并用
    di替换
    die;
    e“无法打开文件$param1”;
    或类似内容…在void上下文中映射一点也不容易损坏。您真的不应该鼓励使用typeglob文件句柄或双参数形式的
    open
    die
    字符串应该包含
    $!
    以获取信息。