in reply to Re: can't thaw a hash: Magic number error.
in thread can't thaw a hash: Magic number error.

That is a good question... a leftover from the normal operation of packing ints for copying via shared memory. I was under the assumption that the pack function is the only way to get the data written as a byte sequence into the shared memory. Are you suggesting I can write that into the shared memory block as is? Trying to read it back from the shared memory block doesn't bode well: %somehash = %{thaw($shm_somehash->read(0, 1024))}; causes: Byte order is not compatible at /System/Library/Perl/5.18/darwin-thread-multi-2level/Storable.pm line 417, at ./test.pl line 47
  • Comment on Re^2: can't thaw a hash: Magic number error.

Replies are listed 'Best First'.
Re^3: can't thaw a hash: Magic number error.
by BrowserUk (Patriarch) on Nov 03, 2015 at 11:44 UTC

    I suspect the problem is that, in either case, the length of the value returned from shared memory is a full 1024 bytes despite that the length of the original frozen hash is less that 100 bytes. It is probably those extra bytes -- that might be space or null padded; or just random garbage -- that are upsetting the thaw process.

    Without having tested it -- IPC::SysV doesn't work on my system -- what I think you should be doing is something like this:

    my $serialized = pack 'L/a', freeze \%somehash; $shm_somehash->write( $serialized, 0, 1024); ... my $unpacked = unpack "L/a", $shm_somehash->read( 0, 1024); my %hash = %{ thaw $unpacked };

    The idea being that the 'L/a' template packs the string with a prefix of the number of characters that are packed. Thus when you unpack it, despite that you read back 1024 bytes from the shared memory, only the same number of bytes as where in the original freeze'd string will be returned, devoid of any packing, and (hopefully) when you pass that to thaw, it should be exactly what it is expecting.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks, that however produced the same magic number error. I then added some code to pass back the length of the packed string, so that I won't be reading the engire 1024 bytes every time and read in garbage. That then showed me that on the first call, I'm trying to thaw what hasn't been frozen yet... which of course results in the above error regardless of how you're trying to thaw garbage... So thanks for helping me point in the right direction!