Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Detect a localized variable

by Perlbotics (Canon)
on Nov 19, 2011 at 14:25 UTC ( #938976=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Detect a localized variable
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (16)
As of 2015-07-07 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls