编写传统的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)