[OT] Warnings from code when using perl-dbi

[OT] Warnings from code when using perl-dbi

am 16.08.2007 10:38:02 von Ow.Mun.Heng

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.

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.p l
line 195 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a
mistake. To suppress this warning assign a defined value to your
variables.
To help you figure out what was undefined, perl tells you what
operation you used the undefined value in. Note, however, that perl
optimizes your program and the operation displayed in the warning may
not necessarily appear literally in your program. For example, "that
$foo" is usually optimized into "that " . $foo, and the warning will
refer to the concatenation (.) operator, even though there is no . in
your program.

Use of uninitialized value in concatenation (.) or string at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.p l
line 197 (#1)
"A","B",,"C"
Use of uninitialized value in length at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.p l
line 188 (#1)
Use of uninitialized value in print at
/home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.p l
line 190 (#1)


Code Block:
==============

##
## We do the fetching of the returned values and then we perform some
formatting prior
## to output. We need to make sure that if we are at the last value, we
must omit the
## trailing comma (,) to make it of form "value1","value2",,"value4"
## We also need to check if the current value is a NULL (0 length), if
so, we just
## do not put a quote mark around it.
##
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
{
print $first[$counter]; <== Line 190
} else {
print "\"$first[$counter]\"";
}
} else {
if (length($first[$counter]) == 0)
{
print $first[$counter].","; <==LIne 197
} else {
print "\"$first[$counter]\",";
}
}
}
print ("\n");
}
}


Thanks in advance..

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

am 16.08.2007 11:38:58 von Gary Stainburn

Hi

Firstly, for an excelent general perl list, try beginners@perl.org. It's not
just for beginners.

Secondly, it looks like you're trying to access a field that isn't defined,
possibly by running off the end of the row.

I've re-written (but not tested) the routine slightly more perlified.

See what you think.

while (my @row = $sth->fetchrow_array )
{
foreach my $field (shift @row) {
if (! defined ($field) || (length($field) == 0))
{
# don't know why you do this cos
# you're printing nothing here
print $field;
} else {
print "\"$field\"";
}
if (@row) # fields left, stick in a comma
{
print ",";
}
}
print ("\n");
}
--
Gary Stainburn

This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000

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

am 16.08.2007 11:50:16 von hjp

--PPYy/fEw/8QCHSq3
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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.
>=20
> I had to do some tweaking to the output to maintain the format which
> postgressql likes.
>=20
> eg:
> "A","B","C",,"D","E"
>=20
> (note the ,, which means a NULL value)
>=20
> My current code is below however, I enabled warnings and strict and get
> this.
>=20
> Use of uninitialized value in length at
> /home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.p l
> line 195 (#1)
[...]
> Use of uninitialized value in concatenation (.) or string at
> /home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.p l
> line 197 (#1)
> "A","B",,"C"
> Use of uninitialized value in length at
> /home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.p l
> line 188 (#1)
> Use of uninitialized value in print at
> /home/gentoo/Desktop/postgres_loading/mssql_2_postgres_cvs.p l
> line 190 (#1)
[...]
> ##
> while ( @first =3D $sth->fetchrow_array )
> {
> # my $first =3D 0;
> # my $counter =3D 0;
> my $count =3D @first;
> my $first =3D @first;
>=20
> for ($counter =3D 0; $counter < $count; $counter =3D $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

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

--PPYy/fEw/8QCHSq3
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGxB3YMdFfQa64PCwRAkY2AJ0ShXnrmdlKgEEz3lpbDT/6ngeeEACg nuHq
GmTuFzx1/6HcqwunjkwD4nE=
=qj41
-----END PGP SIGNATURE-----

--PPYy/fEw/8QCHSq3--

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

am 16.08.2007 12:30:52 von Ow.Mun.Heng

On Thu, 2007-08-16 at 11:50 +0200, Peter J. Holzer wrote:
> 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.

No nntp for me to use. best would be mail list

> 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]))

That worked.. I was thinking of checking the length because if there the
data is NULL, then length should be 0 and not undef.


> 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.

NULL != Empty string.. Right.

Here's the altered one, for brewity..

my @first;
while ( @first = $sth->fetchrow_array )
{
my $count = @first;
my $first = @first;

for ($counter = 0; $counter < $count; $counter = $counter + 1)
{
if ($counter == $count-1)
{
if (!defined($first[$counter]))
{
# WHAT DO I PUT HERE?
} else {
print "\"$first[$counter]\"";
}
} else {
if (!defined($first[$counter]))
{
print ",";
} else {
print "\"$first[$counter]\",";
}
}
}
print ("\n");
}
}

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

am 16.08.2007 12:40:33 von Ow Mun Heng

On Thu, 2007-08-16 at 10:38 +0100, Gary Stainburn wrote:
> Hi
>
> Firstly, for an excelent general perl list, try beginners@perl.org. It's not
> just for beginners.

Ah.. Cool.. I'll subscribe there.

>
> Secondly, it looks like you're trying to access a field that isn't defined,
> possibly by running off the end of the row.
>
> I've re-written (but not tested) the routine slightly more perlified.
>
> See what you think.
>
> while (my @row = $sth->fetchrow_array )
> {
> foreach my $field (shift @row) {
> if (! defined ($field) || (length($field) == 0))
> {
> # don't know why you do this cos
> # you're printing nothing here
> print $field;
> } else {
> print "\"$field\"";
> }
> if (@row) # fields left, stick in a comma
> {
> print ",";
> }
> }
> print ("\n");
> }

Thanks for the solution, however it doesn't really run. Not sure why.
There's a supposed missing brace/curly brace somewhere but I can't find
it yet. Your solution is more elegent. I will definitely try to figure
out where the error is..

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

am 16.08.2007 13:35:51 von hjp

--Sw7tCqrGA+HQ0/zt
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On 2007-08-16 18:30:52 +0800, Ow Mun Heng wrote:
> On Thu, 2007-08-16 at 11:50 +0200, Peter J. Holzer wrote:
> > You really want to know whether $first[$counter] is undef, not whether
> > it has a length == 0 here, so you should test for that:
> >=20
> > if (!defined($first[$counter]))
>=20
> That worked.. I was thinking of checking the length because if there the
> data is NULL, then length should be 0 and not undef.=20

In SQL, length(NULL) is NULL, not 0. In Perl length(undef) is 0, but you
get a warning.


> Here's the altered one, for brewity..
>=20
> my @first;
> while ( @first =3D $sth->fetchrow_array )
> {
> my $count =3D @first;
> my $first =3D @first;
>=20
> for ($counter =3D 0; $counter < $count; $counter =3D $counter + 1)
> {
> if ($counter == $count-1)
> {
> if (!defined($first[$counter]))
> {
> # WHAT DO I PUT HERE?

Nothing. There is nothing to do in this case.

> } else {
> print "\"$first[$counter]\"";
> }

You could invert the test to avoid the empty if:

if (defined($first[$counter]))
{
print "\"$first[$counter]\"";
}

[...]

Of course the whole loop can be written quite a bit shorter (and more
"perlish"):

print join(q{,}, map { defined $_ ? qq{"$_"} : q{} } @first), "\n";

hp

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

--Sw7tCqrGA+HQ0/zt
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGxDaXMdFfQa64PCwRAoK0AKCFHo0Q8uSrc4X9sDEeWChLFouJzACg lFAH
5zGRoo8hPqFvE/4NuJBQ9PU=
=9vtk
-----END PGP SIGNATURE-----

--Sw7tCqrGA+HQ0/zt--