Use of uninitialized value in concatenation (.) or string at ./get_final_energy.perl line 24, <FH> line 18868.
#!/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
}
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.
For anyone else that finds this post this may help -
example
my $string; # $string is declared but not initialized!
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
}