Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

How can I create a new ENV in Perl

by anaconda_wly (Scribe)
on Apr 01, 2013 at 09:11 UTC ( #1026446=perlquestion: print w/replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

I want to add a new Windows system ENV variable by Perl script, saying %MyEnv%=abc. How can I do in a simplst way? When the script finished, the %MyEnv% should be still valid to all users and processes. Thanks for any suggestion!

Replies are listed 'Best First'.
Re: How can I create a new ENV in Perl
by Corion (Pope) on Apr 01, 2013 at 09:16 UTC

    You will have to use the Windows API, and even then, the new environment variable will likely only be seen by processes created after that change. See the Windows documentation on Environment Variables.

    The likely easiest way to set the variables through the registry is Win32::TieRegistry.

      Thanks for the quick/useful reply. I'll have a try. Thanks if any other replies.

      I doubt that it would help to set the variable directly in the registry. You need to change the registry the same way that the control panel does when it creates new variable. Good luck finding that.


        In the MSDN article I conveniently linked, there is the following paragraph:

        Calling SetEnvironmentVariable has no effect on the system environment variables. To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.

        From that, I would assume that no luck is needed in finding the appropriate place.

Re: How can I create a new ENV in Perl
by Anonymous Monk on Apr 01, 2013 at 09:54 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026446]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-06-18 02:41 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.