4 ms·
I think Bash is the only common-use language that uses dynamic scoping. Whenever you get into more than two functions everything can quickly become unmanageable
by gjulianm 8y ago
I think Bash is the only common-use language that uses dynamic scoping. Whenever you get into more than two functions everything can quickly become unmanageable if you don't prefix all your declarations with 'local'. Forget one of those, and the moment you reuse a variable name you'll find yourself with some annoying weird bugs.
- slobotron 8y agoYou 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...
- deleted 8y ago[deleted]