"Use of uninitialized value in scalar chomp" in Perl

pauler picture pauler · Aug 6, 2012 · Viewed 10.6k times · Source

I get the below error when i run the script: Could someone help me on this

Use of uninitialized value $user in scalar chomp at ./temp.pl line 38, <DATA> line 558.
Use of uninitialized value $modelName in scalar chomp at ./temp.pl line 39, <DATA> line 558.
Use of uninitialized value $Application in scalar chomp at ./temp.pl line 40, <DATA> line 558.
Use of uninitialized value $user in string eq at ./temp.pl line 42, <DATA> line 558.

The code is as below, could some-one help me on this:

my ($user) = $ARGV[0];
my ($modelName) = $ARGV[1];
my ($Application) = $ARGV[2];

chomp($user);
chomp($modelName);
chomp($Application);

if ( ($user eq "") || ($modelName eq "") || ($Application eq "")) {
  &usage;
}

sub usage {
  print "\tUsage : $0 User ModelName Application\n";
  exit (1);
}

Answer

Borodin picture Borodin · Aug 6, 2012

The program is expecting parameters - user, model name, and application - on the command line and you have provided none

There is no need to chomp a value passed from the command line as it will never end in a new line

Your code is better written like this

usage() unless @ARGV == 3;

my ($user, $modelName, $Application) = @ARGV;