user3003367
user3003367

Reputation: 31

Command line arguments are not working

I am using Perl for the first time.

I am writing two scripts, and one of those is being called from the other.

While I am passing arguments from user input it's giving an error, but if I hard code the values it works fine.

Please advise how to solve.

Code:

script.pl

use warnings;

my ($choice);

print("Hello!\n");
print("If you want to Generate Add, enter 1.\n");
print("If you want to exit,enter 2.\n");

$choice = <>;
chomp($choice);

if ($choice eq "1") {
  print "Please enter 1st argument:";
  $inputFile = <STDIN>;
  print "Please enter 2nd argument:";
  $outputFile = <STDIN>;

  system($^X, "generateLdifAdd.pl", $inputFile, $outputFile);
}
elsif ($choice eq "2") {
  exit();
}
else {
  print("$choice is an invalid response.\n");
}

Upvotes: 0

Views: 224

Answers (2)

Sobrique
Sobrique

Reputation: 53478

Someone's already mentioned needing to chomp your reads from STDIN. Would I be right in thinking you've done a print on the values you got, and they're all looking good? Can I suggest the next port of call is to check what command line you're passing to your second script? I would suggest as simple as:

print "$^X generateLdifAdd.pl $inputFile $outputFile\n"; 

Check that looks right to you - gotchas might be that your 'other' script isn't in the path. Or that it's not correctly parsing your command line arguments. (You don't give an example, so it's hard to say). This would have also highlighted the problem with not using chomp - that your args contain linefeeds.

Upvotes: -1

Miller
Miller

Reputation: 35198

You probably need to chomp your input:

chomp($inputFile = <STDIN>);

chomp($outputFile = <STDIN>);

Also, don't forget to include use strict; at the top of every script along with use warnings;.

Upvotes: 2

Related Questions