Re: [OT] Warnings from code when using perl-dbi



On 2007-08-16 16:38:02 +0800, Ow Mun Heng wrote:
Sorry, this isn't really a perl-dbi issue per-se. but I don't know where
else to post this issue. I can't find a "Perl" mail-list. There's loads
of list, but no perl-general.

The newsgroup comp.lang.perl.misc is probably the best place to discuss
general perl questions.


Anyway.. pulling data from SQL-server using perl-dbi to be formatted as
a CSV file.

I had to do some tweaking to the output to maintain the format which
postgressql likes.

eg:
"A","B","C",,"D","E"

(note the ,, which means a NULL value)

My current code is below however, I enabled warnings and strict and get
this.

Use of uninitialized value in length at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
line 195 (#1)
[...]
Use of uninitialized value in concatenation (.) or string at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
line 197 (#1)
"A","B",,"C"
Use of uninitialized value in length at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
line 188 (#1)
Use of uninitialized value in print at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.pl
line 190 (#1)
[...]
##
while ( @first = $sth->fetchrow_array )
{
# my $first = 0;
# my $counter = 0;
my $count = @first;
my $first = @first;

for ($counter = 0; $counter < $count; $counter = $counter + 1)
{
if ($counter == $count-1)
{
if (length($first[$counter]) == 0) <==line 188

You really want to know whether $first[$counter] is undef, not whether
it has a length == 0 here, so you should test for that:

if (!defined($first[$counter]))

{
print $first[$counter]; <== Line 190

Also you shouldn't try to print undefined values. Just omit the print
here ...

} else {
print "\"$first[$counter]\"";
}
} else {
if (length($first[$counter]) == 0)
{
print $first[$counter].","; <==LIne 197

and only the comma here.

} else {
print "\"$first[$counter]\",";
}
}
}
print ("\n");
}
}

an alternative is to use

no warnings 'undefined';

in the smallest enclosing block. But that is only useful if you want to
treat undefined values exactly like empty strings, which isn't the case
here.

hp

--
_ | Peter J. Holzer | If I wanted to be "academically correct",
|_|_) | Sysadmin WSR | I'd be programming in Java.
| | | hjp@xxxxxxxxx | I don't, and I'm not.
__/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users

Attachment: pgpoymd6q9iVu.pgp
Description: PGP signature



Relevant Pages