News for hackers. Stuff that matters.
Re:History of T (2001) by gjulianm on Friday August 17, 2018 @03:39PM (#17785186)
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.
Re:History of T (2001) by slobotron on Friday August 17, 2018 @04:08PM (#17785320)
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...