小编Deb*_*nix的帖子

在perl @ARGV中查找管道和重定向

编写传统的Unix/Linux程序时,perl提供了菱形运算符<>.我试图理解如何测试是否没有传递任何参数以避免perl脚本在STDIN的等待循环中不应该.

#!/usr/bin/perl
# Reading @ARGV when pipe or redirect on the command line
use warnings;
use strict;

while ( defined (my $line = <ARGV>)) { 
    print "$ARGV: $. $line" if ($line =~ /eof/) ;  # an example
    close(ARGV) if eof;
}

sub usage {
    print  << "END_USAGE" ;
    Usage:
        $0 file
        $0 < file
        cat file | $0    
END_USAGE
    exit();
}
Run Code Online (Sandbox Code Playgroud)

一些输出运行显示<>工作,但没有参数我们等待STDIN输入,这不是我想要的.

$ cat grab.pl | ./grab.pl
-: 7     print "$ARGV: $. $line" if ($line =~ /eof/) ;  # an …
Run Code Online (Sandbox Code Playgroud)

perl pipe diamond-operator argv

3
推荐指数
1
解决办法
1485
查看次数

标签 统计

argv ×1

diamond-operator ×1

perl ×1

pipe ×1