How to print the literal "null" for undefined values ​​in Perl?

I run a "describe table" query and return a "null" value for the "default" column. However, when I try to print a value from the database to an HTML table, it does not print "null". It is always empty.

This is how I store data from a database:

@nulls = ();

while (($null) = $sth1->fetchrow_array)
    {
    push (@nulls, $null);
    }

When I print the contents of an array @nulls, it never prints the literal value "null". It is always empty. Is there any way to overcome this problem?

+3
source share
4 answers

As Chris J said, null values ​​are returned as undefined values.

, "undefined " . strict warnings pragmata . diagnostics .

NULL, :

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';
    }

@nulls , , .

my @pre_5_10  = map { defined $_ ? $_ : 'NULL' } @nulls;
my @perl_5_10 = map { $_ // 'NULL' } @nulls;
+13
+7

Perl, , , null. , "null".

+2
source

You do not say which database you are using, but you can do it at the SQL level if you are not against a non-portable solution, for example, in Oracle:

select NVL(some_column, 'NULL')
from some_table
+1
source

Source: https://habr.com/ru/post/1703662/


All Articles