Click here to get back home

re: use of DBI; I am getting multiple error messages mixed in with the correct output.

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
re: use of DBI; I am getting multiple error messages mixed in with the correct output. Ted 04-23-2008
Get Chitika Premium
Posted by Peter J. Holzer on April 26, 2008, 5:53 am
Please log in for more thread options
> But null values in SQL are not the same thing as undefined or
> uninitialized values in any other language I know.

Neither are perl undef values the same thing as uninitialized values in
any other language I know.

> In any RDBMS I have worked with, null is a legitimate value that can
> be given to a specific column in a specific row, and it has a very
> specific meaning in a database.

In Perl, undef is a legitimate value that can be given to a scalar, and
it has a very specific meaning in Perl.

> Any SQL programmer knows how to handle nulls, what can and can not be
> done with them, &c.

Any Perl programmer knows how to handle undefs, what can and can not be
done with them, &c.

> That is very different from programming errors where a variable has
> been defined and then used before it has been given a value.

That is very different from programming errors where a variable has been
defined and then used before it has been given a value.

There are no uninitialized variables in Perl. If the programmer doesn't
explicitely initialize a variable, it is implicitely initialized to
undef. This is similar to C implicitely initializing all variables of
static duration to 0 (of the appropriate type). C doesn't initialize
automatic variables, but Perl does.

(The warning about an "uninitialized value" is misleading, IMHO. It
should say "undefined value", or simply "undef". But it is unlikely to
change)


> So I maintain this IS a DBI issue, and there ought to be functionality
> provided within DBI to handle the processing of recordsets, including
> printing them, that can handle null values in a rational manner
> without generating spurious warnings.

There is no unambiguous way to convert a null value to a string. For
every string you choose, it would also be a valid string value.
Therefore it is better to leave that to the programmer.

> At a minimum the DBI documentation ought to say somethng
> about users needing to implement their own processing to handle null
> values when that is not available.

It does. There's a whole section entitled "NULL Values" in perldoc DBI.

> The real error here is that, if I am to believe Ted, that nulls are
> treated as undefined in the DBI translation, and this is plain wrong.
> On the daatabase side, nulls have a specific meaning (and as you well
> said, further on, that meaning is different from that of an empty
> string),

undef also has a specific meaning and that is different from an empty
string. The meaning is not exactly the same as that of SQL null, but I
doubt you'll find two languages where "null" or "nil" or "undef" or
whatever it is called has exactly the same meaning. Most languages have
the concept of "a missing, unknown or undefined value", but the details
differ.

> and DBI ought to at least provide a means of handling them
> correctly from the perspective of a database programmer.

The means are there. You get a specific value which can be distinguished
from any non-null value. How you handle it is up to you.

> The fact is that if I wanted to represent, in a C++ or Java program,
> the idea represented by null in SQL, I could write a class that
> represents that idea and give me the kinds of behaviours I'd require
> of it; something far richer than anything possible with the null
> keywords in either C++ or java.

You can do the same thing in Perl.

> Therefore, I could do precisely the same thing in Perl, if need be.

Ah, yes. So you realized that yourself. What's stopping you?

> I repeat yet again, it is wrong to think of this as a perl issue,
> rather than an issue of a deficiency in the DBI library.

I see that as a feature, not a deficiency. Why should the DBI force a
slow, clunky interface on me if a lean, fast one suffices in 99% of all
cases? DBI is slow enough as it is. It doesn't have to be slowed down
artificially.

> The mistake here is to assume this is a perl issue. Perl, like Java
> and C++ and FORTRAN and a host of other programming languages, is
> Turing complete, so anything that can be done in any one of them can
> in principle be done in any of the others. Surely you know that JDBC
> is to Java as DBI is to Perl.

Yes. And there's the Java way and the there's the Perl way (Ok, there
is more than one Perl way, but you know what I mean). JDBC presents an
interface that is natural to a Java programmer and DBI presents an
interface that is natural to a Perl programmer. You shouldn't expect
them to be identical.

        hp


Posted by Joost Diepenmaat on April 23, 2008, 6:31 pm
Please log in for more thread options

[ on the conversion of NULL fields to <undefined> ]

> This would be a useful tidbit to add to the documentation. I hadn't
> expected a mature library like DBI to behave like this.

Well, it is the most DWIM way of representing NULL values in perl.

> I do not, for example, have to go through any such extra hoops when
> using JDBC with JSF. If a particular record set contains nulls in one
> or more columns in one or more records, JSF automagically displays it
> as an empty string; something I have modified on occassion to display
> the string "N/A".

Perl does the same thing, but it will issue a warning if you have
warnings enabled (warnings are output to STDERR, so if you're capturing
STDOUT only it shouldn't be a problem - I haven't read all of the
thread). *If* you don't want to deal with that, either don't "use
warnings" and don't use the -w flag, or do use them in general but
disable warnings in the offending code block.

In my opinon warnings are very useful and should be enabled as much as
possible, but in the particular cases/constructs where it they cause
problems, you shouldn't be afraid to disable them (using "no
warnings"). See perldoc warnings and perldoc no.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by Joost Diepenmaat on April 23, 2008, 6:34 pm
Please log in for more thread options

> In my opinon warnings are very useful and should be enabled as much as
> possible, but in the particular cases/constructs where it they cause
^^ disregard that
> problems, you shouldn't be afraid to disable them (using "no
> warnings"). See perldoc warnings and perldoc no.
^^ oops, there that
doens't exist. See
perldoc -f use

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by Ted on April 23, 2008, 9:24 pm
Please log in for more thread options
>
> =A0[ on the conversion of NULL fields to <undefined> ]
>
> > This would be a useful tidbit to add to the documentation. =A0I hadn't
> > expected a mature library like DBI to behave like this.
>
> Well, it is the most DWIM way of representing NULL values in perl.
>
As I explained to Xho, it is wrong. My understanding of undefined in
Perl was to relate it to uninitialized values in Java or C++. You
declare a variable, but then attempt to use it before you define it
(i.e. give it a value). The idea of NULL in SQL is very different.
It IS a valid value that can be given to variables or to fields in
specific rows, where the table definition allows it. NULLs in SQL
carry a very specific meaning, and require very specific behaviours
when handling them. To translate this into a language like Java or C+
+, I would write a class that provides for the same meaning and
behaviours that would be familiar to a SQL programmer. Had I known
that the DBI was broken in terms of translating database nulls into
<undefined>, I'd have considered developeing an extension to DBI; one
that included a NULL class that would serve the needs of database
programmers.

> > =A0I do not, for example, have to go through any such extra hoops when
> > using JDBC with JSF. =A0If a particular record set contains nulls in one=

> > or more columns in one or more records, JSF automagically displays it
> > as an empty string; something I have modified on occassion to display
> > the string "N/A".
>
> Perl does the same thing, but it will issue a warning if you have
> warnings enabled (warnings are output to STDERR, so if you're capturing
> STDOUT only it shouldn't be a problem - I haven't read all of the
> thread). *If* you don't want to deal with that, either don't "use
> warnings" and don't use the -w flag, or do use them in general but
> disable warnings in the offending code block.
>
I routinely treat warnings as errors, and pass my C++ code through
different compilers, since each vendor has different strengths and
weaknesses. This helps me identift potential problems and make my
code more robust. I disable warnings ONLY if I can prove that it is
wrong or I learn that it is produced by a documented bug in the
compiler.

So, it appears that the little scriptlet I cobbled together is
behaving correctly apart from generating spurious warnings. Knowing
about this behaviour, I can easily avoid triggering it.


The mixing of stdout and stderr is, in this case, an emacs issue. I
do not know how to get Emacs to split the window it is displaying into
three (one for the program, one for stdoutt and a third for stderr)
when I tell it to run the script I'm working on. :-(

Thanks

Ted

Posted by Martien Verbruggen on April 23, 2008, 11:10 pm
Please log in for more thread options
On Wed, 23 Apr 2008 18:24:31 -0700 (PDT),
>>
>>  [ on the conversion of NULL fields to <undefined> ]
>>
>> > This would be a useful tidbit to add to the documentation.  I hadn't
>> > expected a mature library like DBI to behave like this.
>>
>> Well, it is the most DWIM way of representing NULL values in perl.
>>
> As I explained to Xho, it is wrong. My understanding of undefined in
> Perl was to relate it to uninitialized values in Java or C++. You

Your whole argument seems to be based on this assumption. I'm pretty
sure that that assumption is wrong. Could you tell us what that
assumption is based on? And if that was the only reason for the undef
value to exist, why does the undef function exist?

Comparing it to null in Java or NULL in C or C++ is probably a good
analogy. In all those cases the value is a special one that carry a
meaning that is close to the null value in SQL: A value that is not part
of the normal range of values. The fact that nulls in SQL have special
'handling' does not change that. If Jave or C has set operations, then
they would need to deal with null in very much the same way, and if Perl
has them, it would probably do similar things to undef.

SQL NULL maps, IMO, very well to Perl undef.

If you still think the DBI is broken, instead of you having the wrong
idea of what undef means, you should probably have this argument with
the DBI developers, who will probably be much better be able to explain
to you why it is not broken.

Martien
--
|
Martien Verbruggen |
| "Mr Kaplan. Paging Mr Kaplan..."
|

Similar ThreadsPosted
Multiple Inheritance: mixed base class refs (hash, array) April 29, 2005, 2:29 am
FAQ 9.3: How can I get better error messages from a CGI program? December 5, 2004, 6:03 pm
FAQ 9.3: How can I get better error messages from a CGI program? December 29, 2004, 12:03 pm
FAQ 9.3 How can I get better error messages from a CGI program? March 25, 2005, 12:03 pm
FAQ 9.3 How can I get better error messages from a CGI program? June 4, 2005, 5:03 am
FAQ 9.3 How can I get better error messages from a CGI program? September 23, 2005, 4:03 pm
FAQ 9.3 How can I get better error messages from a CGI program? November 29, 2005, 5:03 am
FAQ 9.3 How can I get better error messages from a CGI program? September 17, 2006, 9:03 pm
FAQ 9.3 How can I get better error messages from a CGI program? March 21, 2007, 3:03 am
FAQ 9.3 How can I get better error messages from a CGI program? November 11, 2007, 9:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap