Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: 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:39 UTC ( #762643=note: print w/replies, xml ) Need Help??


in reply to Re: 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

Well actually I am using strict. But thanks for the help :)I will see if that helps
  • Comment on Re^2: Use of uninitialized value in concatenation (.) or string error after adding new arguement on command line

Replies are listed 'Best First'.
Re^3: Use of uninitialized value in concatenation (.) or string error after adding new arguement on command line
by kennethk (Abbot) on May 07, 2009 at 15:43 UTC
    My update was poorly worded (fixed). The supported conclusion is that your blocks are not structured like you expect. Try it with the posted code.

    I find it surprising that you are using strict, given that there are no my statements in your snippet. It is a bad idea to use loop variables that are not limited to that loop in scope. This is particularly risky with C-style loops.

      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?

        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://762643]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2023-02-01 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (11 votes). Check out past polls.

    Notices?