You are suppose to input three parameters on the command line when you run your program. However, there is no check to see if you do so in your program. Thus, if there is no first parameter, $ARGV[0]
is null, and so is $user
. When you chomp
user, you get an error. Here's a slightly modified version of your program:
use strict;
use warnings;
# Let's give some directions
my $usage <<USAGE;
Usage: <program> <user> <model> <application>
All parameters are required!
USAGE
if (scalar @ARGV != 3) {
die "$usage";
}
my $user = shift;
my $modelName = shift;
my $Application = shift;
# No need for this
chomp $user;
chomp $modelName;
chomp $Application;
Notice a few things:
- I have
use strict
and use warnings
pragmas. Always use these in all of your programs. These help spot about 90% of errors that people make in programs.
- I check whether the arguments are there before I do anything else. I use
scalar
to force @ARGV
into giving me its scalar value (which it would do anyway in this context), and see if there are actually three and only three values in it. If there aren't three values, I give the usage text.
- You don't need to chomp the command line parameters. Chomping is only really needed on file input because each file line is read in with its EOL character or characters. This is mainly done to ensure that the
<...>
doesn't return a zero if the line is merely blank. If the line is blank, you'll get <...>
returning at least a single NL character.
One more thing...
I like to use a $usage
text rather than a subroutine. I set the usage text right on the very top of my program where someone who looks at my code can see it, and read exactly how the program is used. I can also put my usage text in a die
statement.
Plus, it's bad form to print anything out in a subroutine. Subroutines should return values which the calling program can operate on.