http://qs1969.pair.com?node_id=762647


in reply to Re^3: Use of uninitialized value in concatenation (.) or string error after adding new arguement on command line
in thread Use of uninitialized value in concatenation (.) or string error after adding new arguement on command line

Oh I see. Well, I declared all the variables at the beginning of the script - so are you saying I should have declared them elsewhere?
  • Comment on Re^4: Use of uninitialized value in concatenation (.) or string error after adding new arguement on command line

Replies are listed 'Best First'.
Re^5: Use of uninitialized value in concatenation (.) or string error after adding new arguement on command line
by kennethk (Abbot) on May 07, 2009 at 16:14 UTC

    This is of course all style - the best programs are those that actually run. You certainly shouldn't worry about refactoring working code, and if any of these ideas go against what has worked for you, take them with a grain of salt.

    By limiting the scope of variables to the smallest range possible, you reduce the possibility of misusing those variables. Following what you posted, I would have written it as:

    #_ Check for minimum number of command line argument variables if($#ARGV < 6) { Usage(); print "NOT ENOUGH COMMAND LINE ARGUMENTS\n"; exit 0; } #_ Command line options & setup filenames my($alignment_file, $scorecons_file, $image_file, $numbered_model, $cs +a_file); foreach my $i (0 .. $#ARGV) #for ($i=0; $i<=$#ARGV; $i++) { if($ARGV[$i] eq "-a") { $alignment_file = $ARGV[$i+1]; } if($ARGV[$i] eq "-s") { $scorecons_file = $ARGV[$i+1]; } if($ARGV[$i] eq "-i") { $image_file = $ARGV[$i+1]; } if($ARGV[$i] eq "-m") { $numbered_model = $ARGV[$i+1]; } if($ARGV[$i] eq "-c") { $csa_file = $ARGV[$i+1]; } } if (not defined $csa_file) { Usage(); print "No CSA file defined\n"; exit 0; } print "test $csa_file\n"; my $fh_csa = new FileHandle($csa_file, "r") or die "Cannot open CSA file: $csa_file ($!)";

    I've made a few changes here. First, I moved your argument count to before you attempt to parse your argument list since that is a more basic test. Second, I swapped to a foreach loop, and declared $i to be scoped to that loop. Since an iterator is localized to a loop anyway, it is essentially a separate variable already - giving it a unique name makes this more explicit. If your _file variables all behave like script globals, I agree that they should be declared as early as possible in your script. If they only have meaning in the context of the command line arguments and some file handles, then it is a good idea to declare them near to where they get their values, so no one attempts to use them before they are defined. This is also why I added a my to your file open.