in reply to Design Question

Basically you want to create a temporary data structure and cache it. You should be able to do that by just creating a package global variable containing the large array. Something like:
use vars qw($big_array); sub get_big_array { if (!$big_array) { $big_array = [ ... construct big array... ]; } return $big_array; } sub clear_big_array { undef $big_array; } sub processA { common_stuff($_); # special stuff } sub processB { loop (1..1000) { common_stuff($_); # special stuff } } sub common_stuff { my $ba = get_big_array(); ... }
Knowing when to call clear_big_array is harder.

Replies are listed 'Best First'.
Re: Re: Design Question
by Anonymous Monk on Aug 07, 2003 at 02:44 UTC

    Hi, thanks for your help!

    Knowing when to call clear_big_array is harder.


    So if I take this approach, I will need to have a "convention" that all sub-routines in this package call clear_big_array when they are done with this array, or before they start using it. I can see that being a bit harder to maintain -- are there any other disadvantages to that?

      I can't think of any other disadvantages. Even if you never call clear_big_array, it might not matter, the memory will just stay allocated. Generally the program won't return memory to the OS until it exits, and if the OS needs the memory for something else it will just page it out.

      If you give more details about exactly what you're doing, you might be able to get more specific advice.