in reply to Memory Optimization

For a complex hash you'll probably want to use Storable qw(nstore);. nstore is more portable than store and you can still use the same retrieve function. You might need to experiment but below are snippets from code that I currently use. The resulting file is roughly 2.2 times larger than a fixed length text file of the same data. Excerpts from The Perl Cookbook gives some explanation. To save the hash structure to disk:
use strict; use Storable qw(nstore); my %hash = my_build_hash_sub(); nstore (\%hash, "hash.nstore.dat");
To reload hash from disk:
use strict; use Storable; my %hash = %{ retrieve("hash.nstore.dat") };

--Jim

Replies are listed 'Best First'.
Re: Re: Memory Optimization
by Juerd (Abbot) on Dec 30, 2001 at 06:02 UTC
    Speaking of memory optimizations, my %hash = my_build_hash_sub(); will toss around a list and eat memory for that. Returning a reference to a hash would in many cases be more efficient than returning a list of keys/values.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      You're absolutely right. My concern when these snippets were written was not memory optimization so much as decreasing IO read/writes times.

      Thanks for the clarification. I'll look into rewriting the hash generation subs to use references.

      --Jim