Re: What is the difference between NULL and "undef"

From: "Michael A(dot) Mayo" <mmayo(at)mcauleybrooklyn(dot)org>
To: "Rudolph, Michael" <Michael(dot)Rudolph(at)telekom(dot)de>, "'Alessio Bragadini'" <alessio(at)albourne(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: What is the difference between NULL and "undef"
Date: 2000-05-16 17:18:46
Message-ID: 001001bfbf5a$cde10020$2182b798@362197428
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

---- Original Message -----
From: "Rudolph, Michael" <Michael(dot)Rudolph(at)telekom(dot)de>
> I do that statement from Perl via DBI without placeholders.
> According to the DBI-Documentation, it should be possible:
> "Undefined values or undef can be used to indicate null values",
> this sentence is an excerpt from the doc. My application reads in
> input text-fields from a web-formular, so do I always have to
> do a check, if the field is empty and than put it explicit to NULL?

I believe you are confused about the meaning of "undef." In general,
variables are classified as undefined in one of 2 ways:
1) The variable is not declared, or is declared but no value is ever
assigned to it
2) The variable is assigned the return value of some function, and the
function fails, returning undef.

A valid value is not "undef." Things like empty string or the number 0 are
valid values for a variable to have, and therefore are not "undef." Undef
should be interpreted as "something is catastrophically wrong with this
variable."

CGI.pm returns an empty string for text form fields that are not filled out.
Therefore, it is neccecary to test for the empty string and translate that
to
NULL or undef if you want an empty form field to work out to NULL.

----------------------------------------------------------------------------
On my system, the following mini-program inserts a NULL value:
my $test_string; #note: no value assigned to test_string - it's undefined
my $database = DBI->connect("dbi:Pg:dbname=test");

$test_string = $database->quote($test_string);
$database->do("
INSERT INTO employees(name)
VALUES($test_string)
");

----------------------------------------------------------------------------
The following mini-program inserts an empty string:
my $test_string = ""; #empty string assigned to test_string
my $database = DBI->connect("dbi:Pg:dbname=test");

$test_string = $database->quote($test_string);
$sql_statement = $database->do("
INSERT INTO employees(name)
VALUES($test_string)
");
-Mike

Browse pgsql-sql by date

  From Date Subject
Next Message mig 2000-05-16 18:00:21 Re: Index not used in functions in 7.0?
Previous Message Clayton Cottingham aka DrFrog 2000-05-16 17:18:05 doc links broken