0

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");
}
2
  • 1
    I can't see any (attempted) use of command line arguments in the code. Commented Apr 14, 2014 at 8:01
  • 2
    "it's giving an error" isn't very useful information to help us diagnose the problem. You should always show the exact error message in your question. You must always use strict as well as use warnings at the top of your programs. Commented Apr 14, 2014 at 8:53

2 Answers 2

2

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;.

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

Comments

-1

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.

Comments

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.