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

Hi monks, I am newbie ..Can anyone give me code or way how to write script to set windows environment variable, now i am setting it usign,Mycomputer-->right click-->system properties window-->Advanced tab-->select and click the environmental variables button and click new and add varibale name and path in text box and click ok to confirm
  • Comment on newbie question about setting environment variables

Replies are listed 'Best First'.
Re: newbie question about setting environment variables
by syphilis (Archbishop) on Aug 01, 2009 at 14:36 UTC
    From within a perl script:
    $ENV{MY_NEW_ENV_VARIABLE} = 'C:\some\path';
    That will set the environment variable 'MY_NEW_ENV_VARIABLE' to 'C:\some\path' for the life of the script only.

    From within a batch (.bat) file:
    set MY_NEW_ENV_VARIABLE=C:\some\path
    That sets the environment variable for the life of the shell in which you execute the batch file.

    Are either of those options good enough, or do you want to set the environment variable permanently ?

    Cheers,
    Rob
Re: newbie question about setting environment variables
by jbt (Chaplain) on Aug 01, 2009 at 14:58 UTC
    I don't use Windows :-) , but you may need a windows specific module like Win32::AdminMisc to set environment variables as a system property.
Re: newbie question about setting environment variables
by cdarke (Prior) on Aug 02, 2009 at 12:51 UTC
    To set them permanently you need to alter the registry: HKEY_CURRENT_USER\Environment. I suggest you use Win32::TieRegistry. Remember that this will only alter the environment for new processes, running processes will not be affected.