0

Cleaning up some old code on server ... getting an error on line 228 ... pulling out snippet ... the while ($i < $num_cols) is the line with the error AH01215 "Use of uninitialized value $num_cols in numeric lt(<) at"

I added in the my and attempted checking for not defined, the code works, but I would like to get the code working without web server error log messages.

I have several of these "Use of uninitialized" errors, hoping seeing how to fix this will help me with the rest.

            $sth = $h->prepare($sel);
            if ($sth == 0) {
                    print "<XMLRSSQLERROR>ERROR: $DBI::errstr</XMLRSSQLERROR>\n";
                    exit;
            }
            if (!$sth->execute) {
                    print "<XMLRSSQLERROR>ERROR: $DBI::errstr</XMLRSSQLERROR>\n";
                    exit;
            }

            my $num_cols = $sth->{NUM_OF_FIELDS};

            #Start the XML output
            #Start the RS section and add the table name
            print "<RS>\n";
            print "$table\n";

            #Start the SCHEMA section
            print "<SCHEMA>\n";

            my @columns = @{$sth->{NAME}};
            my @type = @{$sth->{TYPE}};

            my $i = 0;
            while ($i < $num_cols) {
                    print "<$columns[$i]>";
                    if (($type[$i] == 1)  or ($type[$i] == 12) or ($type[$i] == -1)) {
                            print "char";
                    } elsif (($type[$i] == 4) or ($type[$i] == 5) or ($type[$i] == -6)) {
                            print "int";
                    } elsif (($type[$i] == 2) or ($type[$i] == 6) or ($type[$i] == 7) or ($type[$i] == 8)) {
                            print "float";
                    } elsif (($type[$i] == 11) or ($type[$i] == 10) or ($type[$i] == 9)) {
                            print "datetime";
                    } else {
                            print "$type[$i]"
                    }
                    print "</$columns[$i]>\n";
                    $i += 1;
            }   

            #End the SCHEMA section
            print "</SCHEMA>\n";
3
  • Thanks this looks to be the area ... I cast it to an int and the warning/error went away. Commented Mar 17, 2020 at 17:33
  • 1
    You cast what to an int? The undefined value? Simply casting something when you don't even know what the problem is, is not the proper approach to take. An undefined value in Perl when cast to an int will result in a value of 0. Commented Mar 17, 2020 at 17:40
  • in this case when asking for the number of columns from a SQL result if I cast it to an int and it is undefined then yes I would want it to be 0. I could check if it is defined before casting however the result would be the same. Commented Mar 17, 2020 at 17:58

2 Answers 2

1

The defined-or can set a default value:

 my $num_cols = $sth->{NUM_OF_FIELDS} // 0;

There are various other tricks, but most of those are different ways to checking values are what you expect before you use them.

But, without seeing your SQL statements or knowing which DBI driver you are using, there's not much we can do to guess. I'd definitely want to know why that value has undef.

Sign up to request clarification or add additional context in comments.

Comments

0

As others have said, it seems weird that $sth->{NUM_OF_FIELDS} is undefined. I'd definitely want to find out what's going on there.

But it looks to me like you don't need that at all. It's only there because you're using a while loop that should probably be written as a foreach loop. You could remove:

my $num_cols = $sth->{NUM_OF_FIELDS};

And replace:

while ($i < $num_cols) {

with:

foreach my $i (0 .. $#columns) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.