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

Answer: How can I change the environment from within my Perl script? (Adam: edit registry)

( #22120=categorized answer: print w/ replies, xml ) Need Help??

Q&A > programs and processes > How can I change the environment from within my Perl script? contributed by Adam

system() passes the process environment to the child shell process. This environment is stored in the %ENV assoc array. If you need only to change the path for a couple system() calls, just alter $ENV{path}. If you are trying to make a permanent change, it can be done... You have to hack the registry, via the Win32 perl api.

If I remember to, I will post some sample code explaining how to do this, but I don't remember the exact syntaxes off hand. I suppose I also aught to point out that you hack the Win32 registry at your own (rather probable) risk. Do not tread there lightly. That said, you just change the key in the registry that stores the path. I'll have to look up the key name for you too.

UPDATE: As promised, I have pursued this further for you. Struggling to remember how I did it, I remembered that I didn't... I used a subroutine that a co-worker of mine had written. So I asked him how he did it, and his response was, "Well the best way to do it is with the Win32::TieRegistry module that I wrote" (paraphrase). He was even kind enough to show me how to use it; here is the code that he gave me (disclaimer: muck with Win32 registry at own risk!)

#!/usr/bin/perl -w use strict; my $Registry; use Win32::TieRegistry ( TiedRef=>\$Registry, Delimiter=>"/", ArrayVal +ues=>1 ); my $env= $Registry->{"LMachine/System/CurrentControlSet/Control/". "Session Manager/Environment/"} or die "Can't open CCS/SM/Env: $^E\n"; my $path= $env->{"/PATH"} or die "Can't get PATH: $^E\n" $path->[0]= "C:\Perl\bin;$path->[0]"; $env->{"/PATH"}= $path or die "Can't set PATH: $^E\n"
Alas, I can not claim credit for that one. That goes to Tye. My next goal is to get Tye to join Perl Monks.

Comment on Answer: How can I change the environment from within my Perl script? (Adam: edit registry)
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-04-21 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls