Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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:16 UTC ( [id://762629]=perlquestion: print w/replies, xml ) Need Help??

Angharad has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently modifying some code and wanted to add an additional argument to the code on the command line. The argument I added is the last one on the list.
#_ Command line options & setup filenames 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]; } print "test $csa_file\n"; #_ Check for minimum number of command line argument variables if($#ARGV < 6) { Usage(); print "NOT ENOUGH COMMAND LINE ARGUMENTS\n"; exit 0; } $fh_csa = new FileHandle($csa_file, "r") || die "Cannot open CSA f +ile: $csa_file ($!)";
The code worked before I added the -c argument - i'm very confused as to why it doesn't work now. It doesn't seem obvious to me. Ay suggestions much appreciated! I know the error means that the $csa_file variable isn't initialized but I fail to see how. I am of course remembering to input said argument at the prompt ;)
  • Comment on Use of uninitialized value in concatenation (.) or string error after adding new arguement on command line
  • Download Code

Replies are listed 'Best First'.
Re: Use of uninitialized value in concatenation (.) or string error after adding new arguement on command line
by ikegami (Patriarch) on May 07, 2009 at 16:11 UTC
    I'd recommend Getopt::Long. It's easy to use, and easier to extend.
    use Getopt::Long qw( GetOptions ); use File::Basename qw( basename ); sub usage { my ($msg) = @_; print STDERR $msg if defined($msg); my $fn = basename($0); print STDERR "usage: $fn ...\n"; # XXX Fill in the "..." exit(1); } GetOptions( 'a=s' => my $alignment_file, 's=s' => my $scorecons_file, 'i=s' => my $image_file, 'm=s' => my $numbered_model, 'c=s' => my $csa_file, ) or usage(); defined($alignment_file) or usage("Missing alignment file\n"); defined($scorecons_file) or usage("Missing score file\n"); defined($image_file ) or usage("Missing image file\n"); defined($numbered_model) or usage("Missing numbered model file\n"); defined($csa_file ) or usage("Missing CSA file\n");
Re: 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:34 UTC
    First, you have unbalanced curly brackets in your posted code. This means I am guessing at how your code is actually structured. In the future, please post code that a monk can just download and run to replicate your issue - see How (Not) To Ask A Question.

    Assuming that the cause of your error in present within your code snippet, the warning is being thrown by your print statement. Specifically, variable interpolation within double quotes is implicitly done with the concatenation operator, thus when you attempt to print without first initializing $csa_file, warnings notes you are concatenating with an undefined value. See String interpolation. I suspect this error is the result from your misplaced curly bracket. Perhaps you mean something like this:

    #_ Command line options & setup filenames 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]; } } print "test $csa_file\n"; #_ Check for minimum number of command line argument variables if($#ARGV < 6) { Usage(); print "NOT ENOUGH COMMAND LINE ARGUMENTS\n"; exit 0; } $fh_csa = new FileHandle($csa_file, "r") || die "Cannot open CSA file: + $csa_file ($!)";

    Some side notes on style:

    1. Generally, the lower precedence or is used in testing in place of the high precedence || you have used to test your file open. It doesn't matter here, but it will matter in similar constructs, such as if you omit parentheses on an argument list.
    2. Foreach constructs tend to be more resilient than C-style for loops. Consider that replacing your for loop with:

      foreach my $i (0 .. $#ARGV)

    3. You are obviously using warnings given then message you report, but are not using strictures. You can avoid a lot of headaches if you use strict.

    Update: Your update supports the conclusion that you have a brackets probelm. You are only evaluating the first term of your argument list opening your file and continuing.

      Well actually I am using strict. But thanks for the help :)I will see if that helps
        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.

      yep, that sorted it. Thanks
Re: 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:32 UTC
    On further inspection, it seems that the only variable from the command line that's presently being initialized is the first one.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-19 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found