in reply to Is this a bug in perl or a bug in me?

I don't understand why your code displays the strange results it does. Here are some observations.

If I change the last loop to read:

while(1){ shmread($key,$message,0,99); ++$t; my $t_copy = $t; my $message_copy = $message; print "$t_copy - $message_copy is ", $t_copy - $message_copy, +"\n"; sleep 1; }

it all works out.

Adding use Devel::Peek and Dump [$t, $message, $result]; to the original loop shows some other interesting results:

SV = IV(0x1a65bf8) at 0x1a65c08 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1a65cb0 SV = PVAV(0x1a54f50) at 0x1a65cb0 REFCNT = 1 FLAGS = () ARRAY = 0x19b75a0 FILL = 2 MAX = 2 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = PVNV(0x1a10e10) at 0x1a65d58 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 8 NV = 0 PV = 0 Elt No. 1 SV = PVNV(0x1a5eda0) at 0x19a6a60 REFCNT = 1 FLAGS = (POK,pIOK,pNOK,pPOK) IV = 1 NV = 1 PV = 0x1a2c2b0 "2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ +0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 +\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ +0\0\0\0\0\0"\0 CUR = 99 LEN = 104 Elt No. 2 SV = PVNV(0x1a5edc0) at 0x1a65770 REFCNT = 1 FLAGS = (NOK,POK,pNOK,pPOK) IV = 0 NV = 6 PV = 0x1a37f80 "6"\0 CUR = 1 LEN = 16 8 - 2 is 7

So, lots of trailing \0's in $message. Not sure how that leads to the disconnect between IV/NV and PV though.

Update: removing the \0's seems to fix it too:

$message =~ s/\0+//g;

Replies are listed 'Best First'.
Re^2: Is this a bug in perl or a bug in me?
by Corion (Patriarch) on Sep 05, 2011 at 19:15 UTC

    Looking at memread in http://cpansearch.perl.org/src/MHX/IPC-SysV-2.03/SysV.xs, my guess is that the IV field of the variable remains untouched when reading/writing the string portion of the shared memory:

    ... SvPV_force_nolen(sv); dst = SvGROW(sv, (STRLEN) size + 1); Copy(caddr + pos, dst, size, char); SvCUR_set(sv, size); ...

    (assuming that none of the macros actually reset the IV slot). I think this could be construed as a bug.

      This is interesting stuff but I have to admit my knowledge of the perl source extends to more /0īs than shmwrite is padding my $message with :). I had intended to try and copy the variables while I was thinking about this on the train home but then forgot about it :)

      I very much appreciate your input on this though. Having not been using perl or unix for a while I was a bit apprehensive about submitting a bug report on this but I think now, I will.

      Thanks again

        “Zed, we have a bug!"
        -- Men In Black

      Not touching the IV slot should be fine (and the right thing to do). But it should turn off the pOK and pIOK flags. I'm not familiar enough with the SV* macros called in memread to know whether it does or not.