Amar Daadaa
Amar Daadaa

Reputation: 73

How to read from a redirected file instead of taking command line parameters

I am writing a program where if no command line arguments are supplied i.e @ARGV == 0, the program takes in three inputs. But, the program has the feature to read any files given as arguments, thus

calculate input1 input2

runs the formula on the numbers found in file1 and file2.

The problem I am running into is when I run

calculate < input1

@ARGV returns 0, thus it runs the code for user input.

How do I get around this so that the program can read input1 and use the values inside for calculations?

Upvotes: 1

Views: 585

Answers (3)

tchrist
tchrist

Reputation: 80404

That should not be a problem. If you read reading from <> (which is really <ARGV>), then there is no difference.

You must be doing something wrong if redirection changes things. Are you actually opening files yourself???

Upvotes: 2

Jim Brandt
Jim Brandt

Reputation: 601

You might consider using a module like Getopt::Euclid or Getopt::Long to make the argument passing more explicit. That might make the program easier to understand for other users too.

Upvotes: 0

Quentin
Quentin

Reputation: 943579

calculate < input1 is equivalent to cat input1 | calculate.

You need to read from <STDIN> and not look for command line arguments.

Upvotes: 4

Related Questions