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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.