Uninitialized value $row[0] warning emitted after upgrading from 1.643 to 1.644
Closed this issue · 6 comments
After upgrading from 1.643 to 1.644 I've noticed a new warning occurring
Use of uninitialized value $row[0] in hash element at /foo/bar/baz/lib/perl5/x86_64-linux/DBI.pm line 2121.
The lines mentioned above:
Lines 2117 to 2121 in bd7bdba
If @row
only ever contained undef
it seems like it should have been emitting this warning in previous versions:
$ perl -e 'use warnings; my @row = (undef) x 5; my $x = {}; my $z = $x->{$row[0]}'
Use of uninitialized value $row[0] in hash element at -e line 1.
Looking at the change log the only thing that stuck out to me was 860c8b8 but it isn't obvious to me how moving the use warnings
in that way would cause this (and so I might be totally wrong).
I think I misunderstood the cause of the warning I was seeing so I'm going to close this
I'm seeing this too (in 1.645), but I don't understand the cause of the warning. What was your solution @ugexe?
I had a query like:
SELECT DISTINCT foo AS bar FROM baz;
which I updated to:
SELECT DISTINCT foo AS bar FROM baz WHERE foo IS NOT NULL;
If $row[$_]
is undefined for whatever reason, it should be skipped completely in that loop.
That code stems from b750928 (2005-03-14
) "Initial commit of enhanced fetchall_hashref"
Thar part effectively changed
my $key_value;
$sth->bind_col ($index, \$key_value) or return;
my %rows;
while (my $row = $sth->fetchrow_hashref ($hash_key_name)) {
$rows{$key_value} = $row;
}
return \%rows;
to
my $rows = {};
my $NAME = $sth->FETCH ($hash_key_name);
my @row;
push @row => undef for 1..$num_of_fields;
$sth->bind_columns (\(@row));
while ($sth->fetch) {
my $ref = $rows;
$ref = $ref->{$row[$_]} ||= {} for @key_indexes;
@{$ref}{@$NAME} = @row;
}
return $rows;
You're quite right - I was also effectively doing a fetchall_hashref
with a key field that was sometimes NULL. Altering the query and key to use id
as the key as it should have been and all is well.