merrymonk has asked for the wisdom of the Perl Monks concerning the following question:

Many thanks for the helpful replies that included the use of wperl as an alternate to launching a Perl application.
I need to file and read in data stored in hashes. The Programming Perl manual (Third edition page 287) shows how to use Data::Dumper but also says that ‘packed binary’ is very fast. I have been looking for examples of how to use ‘packed binary’ (presumably you convert the hash to a packed binary structure then file it). However, my search has failed. Therefore this novice would appreciate some help in how to pack and file and read in and unpack a hash.

Replies are listed 'Best First'.
Re: How to use packed binary data
by dave_the_m (Monsignor) on Jan 24, 2005 at 10:27 UTC
    Use the Storable module - it's a bit like Data::Dumper but stores the data in a compact binary form.

    Dave.

      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.

      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.