开发者

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

开发者 https://www.devze.com 2023-02-20 06:04 出处:网络
I have a Perl script which takes both command line arguments and STDIN #!/usr/bin/perl -w use s开发者_开发技巧trict;

I have a Perl script which takes both command line arguments and STDIN

#!/usr/bin/perl -w
use s开发者_开发技巧trict;
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. 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消