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

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.

Replies are listed 'Best First'.
Re^4: can't thaw a hash: Magic number error.
by balt (Novice) on Nov 03, 2015 at 23:43 UTC
    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!