I am running a query 'describe table' and it returns value of 'null' for the 'default' column. However, when I try to print the value from database to an HTML table it is not printing 'null'. It is just always blank.
This is how I am storing the data from database:
@nulls = ();
while (($null) = $sth1->fetchrow_array)
{
push (@nulls, $null);
}
When I print the contents of array @nulls
it never prints the literal value of 'null'. It is always blank. Is there a way to overcome this problem?
As Chris J said, the null values are returned as undefined values.
If you had warnings enabled, you would have received an "undefined value in print" warning when you printed the values. Using the strict
and warnings
pragmata can save a lot of time debugging. The diagnostics
pragma adds additional explanatory text to the standard warnings and fatal errors.
It's pretty easy to trap and replace the NULL values as they come from the database:
use strict;
use warnings;
my @nulls = ();
while ((my $null) = $sth1->fetchrow_array)
{
# before perl 5.10: use the ternary operator.
push @nulls, defined $null ? $null : 'NULL';
# perl 5.10 adds the defined-or operator: //
push @nulls, $null // 'NULL';
}
Or you could build your @nulls
array the same way you show above, and then alter the nulls at display time.
my @pre_5_10 = map { defined $_ ? $_ : 'NULL' } @nulls;
my @perl_5_10 = map { $_ // 'NULL' } @nulls;