12

I have a Perl script which takes both command line arguments and 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. Works fine for any one of command line or standard input but not for both.

Any Reason?

1
  • I'm sorry sir, but what do you want? Commented Mar 25, 2011 at 10:11

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It is a problem because <> reads from files in @ARGV. If you want it to read from stdin instead, then you need to ensure that @ARGV is empty. An alternative way to fix your program is to continue to use <>, but change to: my $logpath = shift; so that @ARGV will be empty.
Using shift worked for me, whereas changing <> to <STDIN> did not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.