Odd Perl bitwise-AND & MySQL problem?

From: dohnut (cmjensen_at_dohnut.org)
Date: 10/20/03


Date: 20 Oct 2003 14:26:29 -0700

Here's one for some bored problem solver :)

I ran across this earlier today and fixed it, but don't exactly know
why. (that usually only happens in C :)

I'm using Perl version 5.8.0 btw.

Ok, let's start here: I end up with an array that comes from a
fetchrow_array() call to MySQL. I return the array to a function.

Ok, then the function does this (roughly):

sub func()
{
   my @dat = shift;

   my $val = $dat[3] & $dat[4] & $dat[5] & 0x7fff;

   print $val;
}

In the MySQL database, element 0 = int, 1 = varchar(32), 2 =
varchar(255), 3 = int, 4 = int, 5 = int, 6 = int, etc..

Ok, so I call the function. Let's assume $dat[3] = 57 (0x39), $dat[4]
= 2045 (0x7fd), $dat[5] = 2047 (0x7ff).

Guess what I get for output? --> "00" (I should get 57 (0x39) btw)

Not just a zero "0" but _2_ zeros. So, clearly it thinks something
stringy is there. But why? I guess that is my question.

Now, if I populate @dat myself (i.e. don't get the values from MySQL
calls) it works fine.

To my knowledge only numbers are in the 3 variables and I've tested
this. If I print the values individually, they print correctly. I
also do a 'length' on them and I get 2, 4, and 4 respectively -- which
is correct.

How did I fix it?

my $val = int($dat[3]) & int($dat[4]) & int($dat[5]) & 0x7fff;

I added int()s around each value. So apparently Perl thought there
was something before the numbers in the variables?? Nothing should be
there but numbers, the values come from table elements that are ints
and are read right into a Perl array. Aside from a bug, I can't see
how this could get corrupted or how I could be missing something.

I've done quite a bit of MySQL/Perl programming and I've never had
this happen before, in fact, this specific problem doesn't happen all
the time even in this code. Only with when returning data from
specific rows (in my case), though it is consistent and reproducable
-- it's not flaky. I can note that $dat[2] (which comes from a
varchar(255)) contains some text and some "/r/n" characters. The ones
that don't fail don't contain "/r/n" characters in $dat[2]. I don't
know if this is related, I only have 3 rows of data (at the moment)
and 1 causes the problem.

I'm thinking this is a problem in the Perl MySQL DBI module? I
realize I can do more troubleshooting to narrow it down a bit better
and I probably will tonight, but just wanted to get it out there and
see what people thought. Maybe this has been seen before?



Relevant Pages

  • Odd Perl bitwise-AND & MySQL problem?
    ... I'm using Perl version 5.8.0 btw. ... fetchrow_arraycall to MySQL. ... I return the array to a function. ... if I populate @dat myself (i.e. don't get the values from MySQL ...
    (comp.lang.perl.misc)
  • Re: Learning Perl
    ... it should be an array, ... Then they'd be completely inaccessible to beginners. ... that should be my $var. ... so why is it redundant to point out that Perl is different from C here? ...
    (comp.lang.perl.misc)
  • Re: perl vs Unix grep
    ... variable indexCount on array and reintialized evry time. ... Perl is langauge to make things work at any cost. ... > grep but the shell scripts that use ... As far as I can tell from reading and research ...
    (comp.lang.perl)
  • Re: DELETING records using MYSQL Database
    ... you don't have the rights for a local infile operation for that mysql ... ask the Perl Guys to run the infile op as a perl script with your file - ... mySql / VFP / MS-SQL ... > remote table - DELETE FROM, I want to repopulate it with data from ...
    (microsoft.public.fox.vfp.queries-sql)
  • script help to update member profiles
    ... form is all stored in a MySql Database (First Name, Last Name, Username, ... my partner that did all the perl scripts was injured ... Compare entered username and password to database to confirm the user is ...
    (comp.lang.perl.misc)