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

I'm really new to Perl, so please forgive the dumb question. I need to work (set, read values, etc.) with system environment variables on an Win 2000 system. Can anyone tell me how to go about this? Thanks!
  • Comment on How to access system environment variables?

Replies are listed 'Best First'.
Re: How to access system environment variables?
by Fastolfe (Vicar) on Oct 06, 2000 at 00:58 UTC
    %ENV E.g.:
    print "Path = ", $ENV{PATH}, "\n"; $ENV{PATH} .= ";c:\temp"; # add c:\temp to it
    You may be interested in the perlvar documentation.

    Not sure how it works under Windows, but under Unix these changes are not global and only survive for the life of your script and its children.

      Yes, that's also true under Windows (I always assumed it was, but then I got to wondering, so I just tested it).

RE: How to access system environment variables?
by Adam (Vicar) on Oct 06, 2000 at 01:20 UTC
    Fastolfe pretty much covered %ENV, but seemed a little unsure on the details, so I'll reiterate with a little more information. :-) (++ for Fastolfe though!)

    When Perl starts up it creates the %ENV associated array, which contains all your environment variables of the calling process (usually the shell). To catch a quick glimpse of the contents, do something like this:

    perl -MData::Dumper -we "print Data::Dumper->Dump([\%ENV], ['ENV']);"
    Which should make things pretty clear. You can edit any of these elements as you would any hash. Whenever you create a child process (or a system call or what-have-you) Perl passes this %ENV hash to that process to use. Thus everything below you becomes effected by your changes. However, this hash is not passed UP to the calling process, only down to the children.

    To make permenant changes on an MSWin32 system you need to muck with the registry. A great way to do that is Win32::TieRegistry. Do so, however, at your own risk.

    Update:I just remembered that this is a FAQ!, check out "How can I change the environment from within my Perl script?." I remembered because my post seemed familliar, and then I remembered answering this in Q&A.

(tye)Re: How to access system environment variables?
by tye (Sage) on Oct 06, 2000 at 10:02 UTC

    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")
RE: How to access system environment variables?
by myocom (Deacon) on Oct 06, 2000 at 00:58 UTC

    All you need to do is:

    use Env qw(PATH SYSTEMROOT); # or whatever...

    All the info can be found here.

    You could also use the built-in pseudohash %ENV like so:

    perl -e"print $ENV{PATH}"

    UPDATE: Some day I will snatch the pebble from Fastolfe's hand... ++ to him for beating me to it.