Asheep has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm using FreeTDS to connect to MSSQL with great success, so far... But when I output a row that contains a field with a sql <NULL> value I get "Use of uninitialized value".. Is it common to write a function to compare all each field fetched to a undef and replace the literal value with "NULL" or such.. Before you print out a value? Isn't this a waste to run a function on every fetch? Hey monks, what is the better way?

Replies are listed 'Best First'.
Re: Perl DBI and NULL vales from MSSQL
by sachmet (Scribe) on Apr 30, 2001 at 21:06 UTC
    Yes. You get an undef whenever the field is null, because you might actually want to store the value 'NULL' in a field for some reason.

    If you care to print out a NULL in the case of an undef, you can always do:
    while (my $row = $sth->fetchrow_arrayref) { foreach my $field (@$row) { $field = '(NULL)' unless defined($field); } ... (the rest of your code) ... }
    and that should take care of your problem.
Re: Perl DBI and NULL vales from MSSQL
by thabenksta (Pilgrim) on Apr 30, 2001 at 21:07 UTC

    Is it common to write a function to compare all each field fetched to a undef and replace the literal value with "NULL" or such.. Before you print out a value?

    You shouldn't have to do that. It should just return NULL automatically.

    I might be able to help more if you showed me the code you are working with.

    -thabenksta
    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';