简体   繁体   English

传递命令行参数以及来自STDIN的Perl脚本输入?

[英]Pass command line arguments as well as input from STDIN for Perl script?

I have a Perl script which takes both command line arguments and STDIN 我有一个Perl脚本,它接受命令行参数和STDIN

#!/usr/bin/perl -w
use strict;
use warnings;

my $logpath = $ARGV[0];
print "logpath : $logpath\n";

print "Name : ";
my $name = <>;
chomp($name);
print "my name is $name\n";

It does not stop at stdin input. 它不会在stdin输入处停止。 Works fine for any one of command line or standard input but not for both. 适用于任何一个命令行或标准输入,但不适用于两者。

Any Reason? 任何原因?

Change 更改

my $name = <>;

to

my $name = <STDIN>;

If @ARGV has no elements, then the diamond operator will read from STDIN but in your case since you are passing arguments though command line, @ARGV will not be empty. 如果@ARGV没有元素,那么菱形运算符将从STDIN读取,但在您的情况下,因为您通过命令行传递参数,@ @ARGV将不为空。 So when you use the diamond operator <> to read the name, the first line from the file whose name is specified on the command line will be read. 因此,当您使用菱形运算符<>来读取名称时,将读取名称在命令行中指定的文件的第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM