in reply to How to use packed binary data

Use the Storable module - it's a bit like Data::Dumper but stores the data in a compact binary form.

Dave.

Replies are listed 'Best First'.
Re^2: How to use packed binary data
by merrymonk (Hermit) on Jan 24, 2005 at 12:06 UTC
    I have now tested one example in the web address I gave in my last reply and all was fine.
    I then tried to alter this so that the hash was defined as
    $megalith{name} = 'Stonehenge';
    and so on.

    I found that I had to alter the freeze to
    my $storedValues = freeze( \%megalith );
    However, I then got the error
    Reference found where even-sized list expected at 37
    this line was the thaw line that I had put as
    %megalith = thaw( $storedValues );
    I have tried various things but so far failed to find an answer
    can anyone explain how to correct this?
    the full code is below

    use strict "vars";
    use Storable qw( freeze thaw );
    ### Create some values in a hash
    my %megalith;
    $megalith{name} = 'Stonehenge';
    $megalith{mapref} = 'SU 123 400';
    $megalith{location} = 'Wiltshire';
    ### Print them out
    print "Initial Values: megalith = $megalith{name}\n" .
    " mapref = $megalith{mapref}\n" .
    " location = $megalith{location}\n\n";
    ### Store the values to a string
    my $storedValues = freeze( \%megalith );
    ### Reset the variables to rubbish values
    $megalith{name} = 'Flibble Flabble';
    $megalith{mapref} = 'XX 000 000';
    $megalith{location} = 'Saturn';
    ### Print out the rubbish values
    print "Rubbish Values: megalith = $megalith{name}\n" .
    " mapref = $megalith{mapref}\n" .
    " location = $megalith{location}\n\n";
    ### Retrieve the values from the string
    %megalith = thaw( $storedValues );
    ### Display the re-loaded values
    print "Re-loaded Values: megalith = $megalith{name}\n" .
    " mapref = $megalith{mapref}\n" .
    " location = $megalith{location}\n\n";
    exit;
      thaw returns a reference to a hash, so you need to dereference it if you want to assign it to a hash variable:
      %megalith = %{thaw( $storedValues )};

      Dave.

        Many thanks - all now works well. One day I will understand such things - or will I??
Re^2: How to use packed binary data
by merrymonk (Hermit) on Jan 24, 2005 at 11:18 UTC
    Just what I wanted - thanks!
    And for anyone else, this web site seems to explain how to use it (but I have not tried it yet) http://www.xxx.xxx/xxx_xxx/xxxxx/xxx/xxxx_xx.htm

    Edited by davido to obscure link to copyrighted material, per consideration.