Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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

by Anonymous Monk
on Jul 12, 2000 at 01:42 UTC ( #22117=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on Jul 12, 2000 at 01:42 UTC
Q&A  > 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.

Answer: How can I change the environment from within my Perl script? (Adam: edit registry)
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.
Answer: How can I change the environment from within my Perl script?
contributed by jcwren

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.


Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (3)
    As of 2020-10-23 00:04 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (232 votes). Check out past polls.