Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Detect a localized variable

by Perlbotics (Chancellor)
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938976]
help
Chatterbox?
[ambrus]: "Corion Meh. My bank removed (part of) their CSV download facilities." => ah great. interface changes.
[ambrus]: Is this for work?
[Eily]: Discipulus try looking at $@
[Eily]: or count the parentheses :)
[Discipulus]: stupid me! I had to stay at home and do not code with the flu..
[holli]: bacause it's a syntax error

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2017-10-24 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (287 votes). Check out past polls.

    Notices?