Forum Moderators: coopster & phranque

Message Too Old, No Replies

Use of uninitialized value in concatenation (.) or string

         

PHP_Chimp

2:48 pm on Dec 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im new to perl, as you can probably guess.
I have the following error message -
Use of uninitialized value in concatenation (.) or string at ./get_final_energy.perl line 24, <FH> line 18868.

Line 24 of my code is the start of a here doc print block. The uninitialized value message goes away if I use print <<'ERG'...but obviously none of the variables are interpreted.

#!/usr/bin/perl -wT
use strict;
my ($final_nrg, $final_a, $final_alpha, $final_b, $final_beta, $final_c, $final_gamma);
my @castep_files = @ARGV;
foreach my $file (@castep_files) {
open(FH, $file) or die("Can't open $file.\n");
print "Location -> $file\n";
while (my $line = <FH>) {
if ($line =~ m%^Final energy =\s+(-[\d\.]+)%) {
$final_nrg = "'Final Energy', $1,";
}
elsif ($line =~ m%^a =\s+([\d\.]+)\s%) { #+alpha =\s+([\d\.]+)
$final_a = "'a =', $1,";
#$final_alpha = "'Alpha =', " . $2 . ",";
}
elsif ($line =~ m%^b =\s+([\d\.]+)%) {
$final_b = "'b =', $1,";
}
elsif ($line =~ m%^c =[^\n]+%) {
$final_c = $&;
}
}
print<<ERG;
print variables in here, all the variables that are declared in the first my ($variables) list. Removed to save a bit of space.
ERG
}

If I remove everything other than $final_nrg from the heredoc it all works fine. So my assumption is that there is no problem with the heredoc, but the error is within the while loop...not that I can see the problem :(

The Final energy if loop works fine, just cant get the others to play nice.

I know that there are no $final_alpha, $final_beta or $final_gamma getting set, but I got rid of those in an effort to track down my error in the other variables. The final elsif loop is left in from version 1 of the script (the version that worked, but wasnt comma separated), just encase you are wandering about the change in style.

PHP_Chimp

3:44 pm on Dec 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Found my problem.
Seeing as the regexes were not working the $final_* variables were not getting values passed through to them.

For anyone else that finds this post this may help -

example
my $string; # $string is declared but not initialized!

So when you are not passing any value through...they dont work.

perl_diver

7:00 pm on Dec 31, 2007 (gmt 0)

10+ Year Member



As a side-note, what you have is a warning, not an error. Warnings alert you of situations that can lead to possible problems that you should be aware of, but they do not terminate perl programs like errors do.

The -w flag on the shebang line turns on warnings. But you should not use it and use the warnings pragma instead:

use warnings;

Because sometimes it is impossible to avoid getting warnings but you still want to see other warnings. So you can use the "no warnings" command in blocks of code if you use the warnings pragma:


use warnings;

sub foo{
no warnings;
...
}

that way sub foo will not issue any warnings but the rest of the program will.

Another good idea is to have a fall-through condition at the end of your if/elsif block, an 'else' condition:


if () {
}
elsif () {
}
elsif () {
}
else {
fall through condition
}