in reply to setenv in perl
When you use system or backticks or qx{}, the command is executed in a newly spawned shell, so setting an environment variable using one of these methods will not reflect to the calling process. Consider it to be equivalent to (I used $$ to show that it is the prompt of the sub-shell)
$ export A=1 $ echo $A 1 $ bash $$ echo $A 1 $$ export A=2 $$ echo $A 2 $$ exit $ echo $A 1
So, those three are out.
The case of %ENV is way more interesting, as it involves scope and timing. Lets start with simple examples:
$ echo $A;perl -wE'say $ENV{A};$ENV{A}=2;say$ENV{A}';echo $A 1 1 2 1 $ perl -wE'say $ENV{A};qx{echo \$A};$ENV{A}="B";say $ENV{A };qx{echo \$A};' 1 B
You can observe that the proces spawned with the first qx uses the original value stored in $A, and the second invocation uses the changed value, as you expect (if I read your question correctly), so no surprises there.
What will complicate matters is if those %ENV values are used in the startup phase of a module that you use. This implies that the value is used before you change its value. In that case, you should do something like
BEGIN { $ENV{test} = "B"; } use My::Module; # Which initializes with $ENV{test}
Now the environment is set before it is seen by the module.
HTH
|
|---|