Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Setting Env Variable question

by blackadder (Hermit)
on Jul 17, 2002 at 16:10 UTC ( #182483=perlquestion: print w/replies, xml ) Need Help??

blackadder has asked for the wisdom of the Perl Monks concerning the following question:

Hi All
This is a simple problem, for which I thought that the answer would be straight forward, however it wasn't (well for me anyway). The problem goes like this :
in M$ Windows, I go to the ‘start button’ select run and type in cmd, then on the cmd window I type the command 'set' and I get a display of the environment values. Also, when I do 'set var = 2000' and press enter and type set again I get the same display with the value I have just inputted showing in there as expected. However I tried to automate this process and I wrote the following script to do just that. But it did not work.
Also if I type command instead of cmd, will this make any difference.
use strict; system (set); #this bit work s my $var = Shift @ARGV; my $value = 3000; system (set $var=$value);#this bit doesn't

I thought this code should be sufficient to do this simple job, I do not know if I have missed something simple or what. I have tried a combination of back ticks double and single quotes to no avail. Can someone please tell what the heck I am doing wrong?
Thanks in advance Guys

Replies are listed 'Best First'.
Re: Setting Env Variable question
by dws (Chancellor) on Jul 17, 2002 at 16:59 UTC
    I tried to automate this process and I wrote the following script to do just that. But it did not work.

    If the effect you're trying to achieve is to set an environment variable to make it available to a different script, then Win32 makes you jump through some hoops that Unix doesn't.

    Here's how I approached this problem (from memory, so the details are at best mostly right): Script1 emits a batch (.bat or .cmd) file, which includes the SET commands to set environment variables, and the command to invoke script2.

    @ECHO OFF SET DEBUG=1 perl
    Script 1 then exec()'s the batch file, via   exec($batchfile); which sets up the environment and starts script2. It's an ugly hack, but it gets the job done.

Re: Setting Env Variable question
by Rex(Wrecks) (Curate) on Jul 17, 2002 at 17:52 UTC
    Tye gave me a solution for this very problem. It's not as easy as one thinks :)


    Enjoy, it seems to work.

    Update: One other thing, Win32::AdminMisc has several methods to get and set variables. These however will not be available to the existing shell, but the WILL be available to any shell after that. So if you are looking to set vars to use later try this approach. You can automate it so that it runs as a logon or startup script and those vars will always be there.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: Setting Env Variable question
by hiseldl (Priest) on Jul 17, 2002 at 16:37 UTC
    You can also use: $ENV{$var} = $value; to set the environment variables. This will only last for the scope of your script. E.g. once you exit your script and go back to your cmd.exe shell, $var will be what it was before you ran your script.


Re: Setting Env Variable question
by amphiplex (Monk) on Jul 17, 2002 at 16:27 UTC
    In order to not get a syntax error, you will have to put the argument to system inside double-quotes:
    system ("set $var=$value");
    But although I have never used perl on a windows machine, I am pretty sure that this will not do what you want.
    I think that you expect to see the variable you set in the script after you have run it. This will propably not happen as the script is running inside a forked shell.
    I don't know if there is any way to set the environment of the calling shell.

    ---- amphiplex
Re: Setting Env Variable question
by flounder99 (Friar) on Jul 17, 2002 at 16:40 UTC
    Try using %ENV
    $ENV{myvar}="myvalue"; system("set");
    Unfortunately myvar will disapear when the program ends. (It will be there for programs you call using system within the same program.) If you want to change your environment you will either have to change autoexec.bat on win9X or by changing the environment under System in the Control Panel on winNT/2k.



      Thanks to you all indeed.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://182483]
Approved by kvale
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2023-12-09 09:46 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (37 votes). Check out past polls.