"R. Joseph Newton" wrote:

> Sorry, the function I proposed [see bottom] did not properly enclose the 
> declaration, and thus would not have provided the protection I referred to..  Should 
> have been:
> test_cache();
> sub test_cache {
>   my $cache = {};
>
>   &test("hello0",$cache);
>   &test("hello1",$cache);
>   &test("hello0",$cache);
>   screw_things_up();
>   &test("hello3",$cache);
> }

> use strict;
>
> my $cache = {};
>
>  &test("hello0",$cache);         #  Note:  better written as test("hello0",$cache);
>  &test("hello1",$cache);         #     ampersand adds nothing
>  &test("hello0",$cache);
>  screw_things_up();
>  &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";
>  }
>
>  sub screw_things_up {
>       $cache->{'param=hello3'} = "param=hello3" .
>  }
>
> The reality is that anything declared in the global namespace, i.e. outside of a 
> block, is vulnerable to external corruption.Even use-ing strict, the screw_things_up 
> function can silently move in and corrupt you data.  I would suggest going with the 
> closure, or better yet, declaring important variables inside a function definition:  
> This has the added benefit of providing a name to describe the major functionaility 
> of the overall program.
>
> use strict;
>
> test_cache {
>  &test("hello0",$cache);         #  Note:  better written as test("hello0",$cache);
>  &test("hello1",$cache);         #     ampersand adds nothing
>  &test("hello0",$cache);
>  screw_things_up();
>  &test("hello3",$cache);
> }
>
> This will raise an error when $cache is addressed inside screw_things_up.  No 
> function declared in the main package can access $cache unless it the reference 
> specifically presented to it as an argument.  The only way to get at $cache then is 
> through the front door.
>
> Joseph
>
> --
> 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]

Reply via email to