I'd just like to emphasize that under WinNT and later, "a system environment variable" means much more than just "an environment variable". WinNT/Win2K environment variables can be "system", "user", or "temporary". "system" ones are set for all processes. "user" ones are set when that particular user logs in. "temporary" ones are set with "SET X=Y" or $ENV{X}="Y".

Getting and setting "system" and "user" environment variables is quite easy using Win32::TieRegistry. There are a couple of things to worry about.

First, don't read a variable via $ENV{PATH} if you want to modify it permanently. One of our best programmers did that and it was a huge mess (more on this later). Second, be sure to enable ArrayValues so you don't change the type of the registry value.

For example, say the "system" "PATH" variable is just the standard %SystemRoot%\System32;%SystemRoot% and your "user" "PATH" is C:\Perl\bin. You log in one day, happen to do SET PATH=%PATH%;C:\doom before you run your neat little script. Then $ENV{PATH} will return "C:\\WinNT\\System32;C:\\WinNT;C:\\Perl\\bin;C:\\doom". Then you update the "system" "PATH" and the next time you log in your PATH is C:\WinNT\System32;C:\WinNT;C:\Perl\bin;C:\doom;C:\Perl\bin. Note the duplicate and how your temporary change has now become permanent.

Update: I have posted a nice little script I have that demonstrates how to do this correctly and also can fix the above mistakes automatically for you.

Note that changes to "system" environment variables via the registry do not take full effect until you reboot. You can make them take almost full effect by properly diddling (that is the technical term for it) with the "Environment" tab of the "System" control panel applet and logging out and back in again.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: How to access system environment variables? by tye
in thread How to access system environment variables? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.