Odd Perl bitwise-AND & MySQL problem?

Discussion in 'Perl' started by dohnut, Oct 20, 2003.

  1. dohnut

    dohnut Guest

    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?
    dohnut, Oct 20, 2003
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dohnut
    Sam Holden
    Oct 21, 2003
  2. dohnut
    Oct 21, 2003
  3. Serve Laurijssen

    odd/even bitwise and

    Serve Laurijssen, Apr 6, 2004, in forum: C Programming
    Michael Wojcik
    Apr 13, 2004
  4. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Richard Heathfield
    Feb 18, 2007
  5. dohnut
    Tad McClellan
    Oct 21, 2003

Share This Page