<?xml version="1.0" encoding="windows-1252"?>
<node id="143961" title="Always Follow Up on Warnings" created="2002-02-07 15:05:59" updated="2005-08-15 09:59:58">
<type id="120">
perlmeditation</type>
<author id="63135">
buckaduck</author>
<data>
<field name="doctext">
Once again I am struck by how true it is, that a good programmer should always follow up on warnings.
&lt;p&gt;
I'm writing a DBI program with a CGI interface. The program reads information from the database, performs some transformations, and puts the result into an HTML table. Easy.
&lt;p&gt;
But my database has a number of NULL values. This didn't cause any problems in the HTML output, it just made Perl generate a hundreds of annoying warnings about the "use of an uninitialized value". (When using "warnings", of course.) I'll admit that I always used to hate this warning -- after all, I didn't really care if the values were defined or not, as long as the output looked okay.
&lt;READMORE&gt;
&lt;p&gt;
I initially intended to turn off the warnings, at least in a small block around my string-handling code. But there was this little nagging voice in my head that said, "Make your program run clean under 'use warnings' and 'use strict'". 
I'm pretty sure that I got this voice from Perl Monks.
&lt;p&gt;
And so I spent thirty minutes finding all of the places where the data could possibly be NULL, and I made sure they were defined, like this:
&lt;code&gt;
$string = "" unless defined($string);
&lt;/code&gt;
This got rid of most of the warnings, but there were more. Whoops, I missed a few NULLs. Thirty minutes later I was sure that I had tracked them all down, so I ran the program again. Now, my hundreds of warnings were down to about five.
&lt;p&gt;
So I spent another thirty minutes trying to find a place in my code where I could still be getting undefined values. And what did I find? A very real problem with my database. There were NULLS in fields where there shouldn't have been -- where I was absolutely positive that there weren't any. I hadn't even thought to double-check it before.
&lt;p&gt;
I've fixed my database, and the program runs clean now. And I am very glad that I didn't turn off warnings, even for one seemingly innocent line of code.
&lt;p&gt;
buckaduck</field>
</data>
</node>
