Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > unsetenv in the shell

Reply
Thread Tools

unsetenv in the shell

 
 
Si
Guest
Posts: n/a
 
      03-02-2008

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{'VAR1};
$var2 = $ENV{'VAR2'};
$var3 = $ENV{'VAR3'};

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?
 
Reply With Quote
 
 
 
 
Klaus
Guest
Posts: n/a
 
      03-02-2008
On Mar 2, 10:00 am, Si <(E-Mail Removed)> 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.
>
> $var1 = $ENV{'VAR1};
> $var2 = $ENV{'VAR2'};
> $var3 = $ENV{'VAR3'};
>
> 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
 
Reply With Quote
 
 
 
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-02-2008
On 2008-03-02 09:36, Klaus <(E-Mail Removed)> wrote:
> On Mar 2, 10:00 am, Si <(E-Mail Removed)> 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.
>>
>> $var1 = $ENV{'VAR1};
>> $var2 = $ENV{'VAR2'};
>> $var3 = $ENV{'VAR3'};
>>
>> 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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-02-2008
Si <(E-Mail Removed)> 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{'VAR1};
>$var2 = $ENV{'VAR2'};
>$var3 = $ENV{'VAR3'};
>
>system( "unsetenv VAR1");
>system( "unsetenv VAR2");
>system( "unsetenv VAR3");


"Unsetting" an environment variable is in no way different from setting it.
Your case is a variation of the FAQs:
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?

Just do those changes in the script itself instead of in a child process
(perldoc -f delete)

jue
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-02-2008
Klaus <(E-Mail Removed)> wrote:
>On Mar 2, 10:00 am, Si <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-03-2008
Si <(E-Mail Removed)> 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{'VAR1};
> $var2 = $ENV{'VAR2'};
> $var3 = $ENV{'VAR3'};
>
> 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?


Others have explained why this doesn't work. By adding an extra scope
and a local, you can avoid having to explicitly store the old values.

{
my @del=qw/VAR1 VAR2 VAR3/;
local %ENV{@del};
delete %ENV{@del};

run_the_external_app;

};

Once the outer scope exits, the variables will automatically be reset.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-03-2008
>>>>> "x" == xhoster <(E-Mail Removed)> writes:

x> Others have explained why this doesn't work. By adding an extra scope
x> and a local, you can avoid having to explicitly store the old values.

x> {
x> my @del=qw/VAR1 VAR2 VAR3/;
x> local %ENV{@del};
x> delete %ENV{@del};


technically those should be @ENV{@del} as they are hash slices. but i
think perl allows % there.

x> Once the outer scope exits, the variables will automatically be reset.

definitely the best way to do this.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
szr
Guest
Posts: n/a
 
      03-03-2008
Uri Guttman wrote:
>>>>>> "x" == xhoster <(E-Mail Removed)> writes:

>
> x> Others have explained why this doesn't work. By adding an extra
> scope x> and a local, you can avoid having to explicitly store the
> old values.
>
> x> {
> x> my @del=qw/VAR1 VAR2 VAR3/;
> x> local %ENV{@del};
> x> delete %ENV{@del};
>
>
> technically those should be @ENV{@del} as they are hash slices. but i
> think perl allows % there.


I get:

$ perl -v | head -n 2 | tail -n 1
This is perl, v5.8.8 built for i686-linux-64int-ld


$ ./test_clpm_env_0001.pl
syntax error at ./test_clpm_env_0001.pl line 9, near "%ENV{"
syntax error at ./test_clpm_env_0001.pl line 10, near "%ENV{"
Execution of ./test_clpm_env_0001.pl aborted due to compilation errors.
You have new mail in /var/spool/mail/root


$ nl -b a -n rn -w 3 -s ' ' test_clpm_env_0001.pl | head -n 10 | tail -n
2
9 local %ENV{@var_list};
10 delete %ENV{@var_list};


If I changed those lines to have @ENV{... instead of %ENV{... it works
as expected.

* * *

One more thing; the "delete" statement on line 10 is not needed:

$ nl -b a -n rn -w 3 -s ' ' test_clpm_env_0001.pl
1 #!/usr/local/bin/perl
2
3 use strict;
4
5 my @var_list = qw/VAR1 VAR2 VAR3/;
6 @ENV{@var_list} = qw/X1 Y2 Z3/;
7
8 {
9 local @ENV{@var_list};
10 #delete @ENV{@var_list};
11
12 #run_the_external_app;
13 print "[", join(', ', @ENV{@var_list}), "]\n";
14 };
15 print "[", join(', ', @ENV{@var_list}), "]\n";


$ ./test_clpm_env_0001.pl
[, , ]
[X1, Y2, Z3]


Reemmber that the "local" statement gives a temporary copy of %ENV which
is in a clean state, making the "delete" unnecessary

--
szr (mmmmm.., Perl)


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      03-03-2008

Quoth "szr" <(E-Mail Removed)>:
>

[ local $ENV{FOO}; delete $ENV{FOO} ]
>
> One more thing; the "delete" statement on line 10 is not needed:

<snip>
>
> Reemmber that the "local" statement gives a temporary copy of %ENV which
> is in a clean state, making the "delete" unnecessary


This is not the case. local sets the value to undef, which in the case
of %ENV is translated to the empty string. An empty environment variable
is very different from one which isn't there:

~% perl -le'$ENV{FOO} = 1; local $ENV{FOO}; system "env"' | grep FOO
FOO=

Ben

 
Reply With Quote
 
szr
Guest
Posts: n/a
 
      03-03-2008
Ben Morrow wrote:
> Quoth "szr" <(E-Mail Removed)>:
>>

> [ local $ENV{FOO}; delete $ENV{FOO} ]
>>
>> One more thing; the "delete" statement on line 10 is not needed:

> <snip>
>>
>> Reemmber that the "local" statement gives a temporary copy of %ENV
>> which is in a clean state, making the "delete" unnecessary

>
> This is not the case. local sets the value to undef, which in the case
> of %ENV is translated to the empty string. An empty environment
> variable is very different from one which isn't there:
>
> ~% perl -le'$ENV{FOO} = 1; local $ENV{FOO}; system "env"' | grep
> FOO FOO=


Excellent point. I view %ENV as a tied hash of sorts.

--
szr


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using subprocess module to launch a shell shell script that itselfforks a process Samuel A. Falvo II Python 0 10-08-2008 12:43 AM
Re: How to pass shell variable to shell script from python D'Arcy J.M. Cain Python 0 02-27-2008 01:56 PM
Re: How to pass shell variable to shell script from python Gerardo Herzig Python 1 02-27-2008 12:19 PM
Re: How to pass shell variable to shell script from python Christian Heimes Python 0 02-27-2008 10:53 AM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM



Advertisments