Try using it like this
-----------------------------
use strict;
my $cache = {};
&test("hello0",$cache);
&test("hello1",$cache);
&test("hello0",$cache);
&test("hello3",$cache);
sub test {
my $param = shift;
my $cache = shift;
my $cache_key = "param=$param";
if (exists $cache->{$cache_key}) {
print "Exists\n";
return $cache->{$cache_key};
}
sleep 1;
$cache->{$cache_key} = $param . "done"; # save the value
print "does not exists\n";
}
------------------------------------------------------
-----Original Message-----
From: Rob Anderson [mailto:[EMAIL PROTECTED]
Sent: Friday, March 28, 2003 10:36 AM
To: [EMAIL PROTECTED]
Subject: Re: Variable scoping, static variable
Thanks but I don't see how this could work. I've tried using it, but to no
avail
"Janek Schleicher" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> 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]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]