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

Hi Monks, I read somewhere on a website about Hash Dump. I know of Data::Dumper. I was wondering if there was a better efficient way of storing hashes in files and retrieving them (packages)? I know in the past I saw something like Data::Dumper but it dump the file into binary and then took it back. I need the file to be as small as possible and also fast in dump and reloading the file back.

Replies are listed 'Best First'.
Re: Perl : Hash Dump Packages?
by Tanktalus (Canon) on Jan 28, 2005 at 16:12 UTC

    Have you looked at Storable or FreezeThaw? Often these alternatives have the disadvantage that upgrading to a new version of the module may break compatability, meaning you'll no longer be able to retrieve the old files.

    Otherwise, you'll have to pack and unpack the entries you care about yourself.

      thanks, I guess i will have to experiment
Re: Perl : Hash Dump Packages?
by moot (Chaplain) on Jan 28, 2005 at 16:09 UTC
    Storable


    Er, less tersely, 'Storable' is the package you are thinking of.

Re: Perl : Hash Dump Packages?
by brian_d_foy (Abbot) on Jan 28, 2005 at 17:10 UTC

    There are various ways to keep persistent data. DBM::Deep is a recent and popular module, but are various other DBM sorts of modules out there that deal with hashes, like MLDBM, DB_File, or GDBM_File. Storable is another option.

    --
    brian d foy <bdfoy@cpan.org>
Re: Perl : Hash Dump Packages?
by Aristotle (Chancellor) on Jan 28, 2005 at 21:18 UTC

    If you haven't stored references as hash values, you can use my $box = pack "(Z*)*", %hash; to deflate the hash and my %hash = unpack "(Z*)*", $box; to inflate it again.

    If your data contains nul bytes or you need to avoid them altogether you'll want a pack format with a counter, such as (A8/A*)* maybe, which will consume more space and impose a limit on the maximum length of keys and values.

    Both of those will turn undef values into empty strings.

    If you have nested data structures, implementing your own gets complex quick; see aforementioned posts for suggestions.

    It all depends on your needs.

    Makeshifts last the longest.