Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How can I change the environment from within my Perl script?

by Anonymous Monk
on Jul 12, 2000 at 01:42 UTC ( [id://22117]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (programs and processes)

In WIN NT4.0 or WIN98 workstation, how do we change path?
system("set path=%path%;c:\xx;c:\xx\sss;")
does not change my path.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I change the environment from within my Perl script? (Adam: edit registry)
by Adam (Vicar) on Jul 12, 2000 at 01:56 UTC
    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.
Re: How can I change the environment from within my Perl script?
by jcwren (Prior) on Jul 12, 2000 at 01:51 UTC
    Nor should it. system() will pass the current environment to the program being invoked, but changes made to that environment are not passed to the parent. So, basically, you're changing the path for the duration of the system() call, which is just long enough for the command to exit.

    If you're trying to change the path permanently, this can't be done. There's no way to update the path of the parent process.

    If you'd like to change the path for the duration of the Perl script, see the perldoc ::Cwd documentation. This will change the path environment of the current process, which is your Perl script. Any subsequent system() or exec() calls will honour the new path. However, once the Perl program setting the path exits, your path will revert to whatever it was set to by the calling program.

    --Chris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://22117]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found