in reply to Using tie to initialize large datastructures

You could tie a hash just as you described. This will give your structure a hash-like interface, which is either a pro or a con.

Another way would be to use Dominus' excellent Memoize module. I wrote a review of the module: read the review of the Memoize module. Using it could be as simple as writing a function get_data_structure_from_database, saying memoize 'get_data_structure_from_database', and continuing to use that function!

Replies are listed 'Best First'.
Re: Re: Using tie to initialize large datastructures
by da (Friar) on Aug 08, 2001 at 20:54 UTC
    I would like to add a second vote for use Memoize; your lookup functions are expensive and read-only, exactly what memoization is good for.

    In addition to the behavior ariels mentions above, I'd like to point out it is easy to persistantly store the memoized data using tie, no muss, no fuss.

    use DB_File; tie my %cache => 'DB_File', $filename, O_RDWR|O_CREAT, 0666; memoize 'function', SCALAR_CACHE => [HASH => \%cache];
    In addition, Memoize makes a good profiler-- details are available here in Dominus's article.

    ___ -DA > perl -MPOSIX -e'$ENV{TZ}="US/Eastern";print ctime(10**9)' Sat Sep 8 21:46:40 2001