in reply to Changing ENV values locally

Your snippet wouldn't work because %ENV is a global variable, so when you change it in the inner scoper (inside the curlies) you're changing it outside, too. To localize the change, you can maybe do local %ENV; inside the inner block.

It's also possible You may also just want to save the value and reset it later..
my $save_term = $ENV{TERM}; $ENV{TERM} = "foo"; print `echo a \$TERM`; $ENV{TERM} = $save_term;
Update: rephrased to lessen the emphasis on the second (non-local) solution which is just an example of another possibility and not an advocation of the 'right' (or even 'good') way.

Replies are listed 'Best First'.
Re^2: Changing ENV values locally
by halley (Prior) on Jul 02, 2005 at 19:48 UTC
    Your code example is an excellent demonstration of exactly what the local keyword does. Except you don't have to pick a name like save_term for the old version of the value.

    --
    [ e d @ h a l l e y . c c ]

      I know you know this, but it's worth mentioning another big advantage of local over the hand-coded equivalents exampled by your respondee.

      The programmer (or another at some later time) may modify the code such that it returns without restoring the original value.

      With local, this won't happen. The programmer might forget, but the compiler/interpreter will not.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.