Click here to get back home

unsetenv in the shell

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
unsetenv in the shell Si 03-02-2008
Get Chitika Premium
Posted by Si on March 2, 2008, 4:00 am
Please log in for more thread options

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");


can someone tell me what the correct syntax should be for this?

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

Posted by Klaus on March 2, 2008, 4:36 am
Please log in for more thread options
> 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.
>
> $var1 = $ENV;
> $var2 = $ENV;
> $var3 = $ENV;
>
> system( "unsetenv VAR1");
> system( "unsetenv VAR2");
> system( "unsetenv VAR3");
>
> run_the_external_app;

Environment variables will be restored to their original state after a
system call, therefore whatever system("unsetenv...") (or any other
system() call for that matter) does to the environment variables has
no effect whatsoever on the environment variables of the running perl
program, this includes the effect on any subsequent actions in the
running perl program, such as "run_the_external_app;"

However, if you are running under Unix, perlfaq 8 might be helpful:

+++++++++++++++++++++++++++++++++++++++++++
++ perlfaq 8:
++ ----------
++ "I {changed directory, modified my environment} in a
++ perl script. How come the change disappeared when I
++ exited the script? How do I get my changes to be
++ visible?"
++
++ Unix: In the strictest sense, it can't be done--the script
++ executes as a different process from the shell it was
++ started from. Changes to a process are not reflected in
++ its parent--only in any children created after the change.
++ There is shell magic that may allow you to fake it by
++ eval()ing the script's output in your shell; check out the
++ comp.unix.questions FAQ for details.
+++++++++++++++++++++++++++++++++++++++++++

--
Klaus

Posted by Peter J. Holzer on March 2, 2008, 6:32 am
Please log in for more thread options
>> 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.
>>
>> $var1 = $ENV;
>> $var2 = $ENV;
>> $var3 = $ENV;
>>
>> system( "unsetenv VAR1");
>> system( "unsetenv VAR2");
>> system( "unsetenv VAR3");
>>
>> run_the_external_app;
>
> Environment variables will be restored to their original state after a
> system call,

That's wrong. The variables will not be restored after the call, they
never change.

system( "unsetenv VAR1");
starts a second process which then tries to execute the "unsetenv"
program, which probably doesn't exist ("unsetenv" is a builtin command
of the csh). But even if program was a shell which could unset
environment variables (try system("unset VAR1;");) it would only unset
it's own copy of the variable, never the variable of the perl process.

        hp

Posted by Jürgen Exner on March 2, 2008, 7:06 am
Please log in for more thread options
>> system( "unsetenv VAR1");
>
>Environment variables will be restored to their original state after a
>system call,

Actually they are never modified for the parent process in the first place.

jue

Similar ThreadsPosted
execute a shell script in a shell script December 3, 2007, 8:30 am
Looking for a better shell for windows 2K July 21, 2004, 6:00 pm
FAQ: Is there a Perl shell? October 21, 2004, 5:10 pm
FAQ 3.3: Is there a Perl shell? November 5, 2004, 6:03 am
FAQ 3.3 Is there a Perl shell? January 17, 2005, 6:03 am
FAQ 3.3 Is there a Perl shell? March 29, 2005, 6:03 pm
FAQ 3.3 Is there a Perl shell? June 14, 2005, 11:03 am
FAQ 3.3 Is there a Perl shell? August 11, 2005, 10:03 am
Shell script September 27, 2005, 3:10 pm
FAQ 3.3 Is there a Perl shell? October 26, 2005, 10:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap