Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by Angharad (Pilgrim)
on May 07, 2009 at 15:52 UTC ( #762647=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://762647]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2023-02-06 00:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (33 votes). Check out past polls.

    Notices?