in reply to
Detect a localized variable
Seems, you're doing some kind of lazy cache clearing?
Perhaps the situation can be relieved a bit
- by restricting access to the flag (i.e. $My::Cache::FORCE_REFRESH), and
- by giving the user an environment that operates on a cleared cache?
Maybe just:
with_cache_cleared_do { some_sub_requesting_the_data; };
Example:
use strict;
use warnings;
{ #--- confine flag in lexical scope
my $refresh_request_flag;
#--- use closures to access flag
sub refresh_requested { return !! $refresh_request_flag }
sub with_cache_cleared_do (&) {
$refresh_request_flag = 1;
my @result = shift->(); # run block
$refresh_request_flag = 0;
return unless defined wantarray;
return wantarray ? @result : $result[0];
}
}
# usage: -run: with_cache_cleared_do { some block };
# -check: refresh_requested()
#--- that's it - now the tests ...
use Test::Simple tests => 6;
ok( ! refresh_requested() , 'before: flag is cleared' );
with_cache_cleared_do { ok( refresh_requested() , 'flag set within bl
+ock' ); };
ok( ! refresh_requested() , 'after: flag is cleared agai
+n' );
sub nested {
ok( ( $_[0] == 42 and refresh_requested() ),
'flag set within nested calls with parameters')
}
with_cache_cleared_do { nested( 42 ) };
my $x = with_cache_cleared_do { return 'foo'; };
ok ( $x eq 'foo' , 'result in scalar context' );
my @x = with_cache_cleared_do { return ( 'one', refresh_requested() )
+; };
ok( ( $x[0] eq 'one' and $x[1] == 1 ) , 'result in list context' );
HTH