Rob Anderson wrote at Fri, 28 Mar 2003 14:45:07 +0000:

> ------ module sub routine --------
> 
> sub test {
>     my $param = shift;
>     my $cache_key = "param=$param";
>     if (exists $cache{$cache_key}) {
>         return $cache{$cache_key};
>     }
>     sleep 1;
>     $cache{$cache_key} = $param . "done"; # save the value
> }
> 
> 
> My problem with this is that I can't use strict, because I'm not declaring
> %cache. If I do use strict, I'm forced to declare %cache, and when the sub
> ends, the hash goes out of scope. So, my question is, can I create a
> 'static' hash for this fuctions that'll work with warnings and strict? I
> know that there are modules for caching functions, but I don't have much
> control of the environment and would rather not install extra modules.

Try a closure:

{
  my %cache;
  sub test {
      my $param = shift;
      my $cache_key = "param=$param";
      if (exists $cache{$cache_key}) {
          return $cache{$cache_key};
      }
      sleep 1;
      $cache{$cache_key} = $param . "done"; # save the value
  }
}


Greetings,
Janek

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to