|
Posted by Christian Winter on March 2, 2008, 4:21 am
Please log in for more thread options
Si wrote:
> hi, i have a perl program that calls another application. this second
> application uses the value of certain environment variables during its
> operation. i'm having trouble with unsetting those variables during
> the course of the perl script.
>
> my (wrong) pseudo code:
>
> $var1 = $ENV;
> $var2 = $ENV;
> $var3 = $ENV;
>
> system( "unsetenv VAR1");
> system( "unsetenv VAR2");
> system( "unsetenv VAR3");
>
> run_the_external_app;
>
> system( "setenv VAR1 $var1");
> system( "setenv VAR2 $var2");
> system( "setenv VAR3 $var3");
Calling setenv/unsetenv via system doesn't work, because it
first forks of a child shell. Modifications there don't,
of course, propagate up to the parent environment.
Luckily, you can directly modify the %ENV hash, as
"perldoc perlvar" explains:
----------------------------- snip ----------------------------
%ENV
$ENV
The hash %ENV contains your current environment. Setting a value
in "ENV" changes the environment for any child processes you
subsequently fork() off.
----------------------------- snap ----------------------------
So doing "my $var1 = delete $ENV;" before calling the
external app and assigning "$ENV = $var1;" afterwards
will do the trick.
-Chris
|