You can get dynamic scoping in perl by prefixing things with `local`.
$ENV{BLAH} = 'hi';
$Some::Package::Var = 'there';
{
local $ENV{BLAH} = 'hello';
local $Some::Package::Var = 'world';
# this call will see 'hello' and 'world'
some_method()
}
# this call will see 'hi' and 'there'
some_method()
All the caveats with playing with globals above notwithstanding, it's more useful in practice as a way to get a 'slightly different copy of an object':
my $a = { hello => 'world', other => 'stuff'};
{
delete local $a->{other};
print "$a->{hello} $a->{other}\n";
}
print "$a->{hello} $a->{other}\n";
this gives
world
world stuff
Great way for setting up log contexts etc that automatically get removed after the scope exits...