How can I set the Windows PATH variable from Perl?

后端 未结 3 982
时光取名叫无心
时光取名叫无心 2021-02-08 11:59

I need to set the an environment variable from within Perl. Ideally, I need to query a variable and then change it if it is not what is required. Specifically it is the PATH var

相关标签:
3条回答
  • 2021-02-08 12:09

    If you need to change environment variables globally and permanently, as if you set it in the control panel, then you have to muck with the registry (update: and now there are modules to do this, Win32::Env and Win32::Env::Path). Note that changing variables in the registry and "broadcasting" the change will not change the environment variables in some current processes, notably perl.exe and cmd.exe.

    If you just want to change the current process (and subsequently spawned child processes), then the global %ENV hash variable is what you want (e.g. $ENV{PATH}). See perldoc perlvar.

    0 讨论(0)
  • 2021-02-08 12:18

    $ENV{PATH}?

    Keep in mind that environment variables only affect subprocesses, however. You can't run a Perl program, change %ENV, and then see that change in the parent process -- the environment does not work that way.

    0 讨论(0)
  • 2021-02-08 12:21

    You can do that using the %ENV hash

    $ENV{PATH} = 'C:\\Windows\;D:\\Programs';
    
    0 讨论(0)
提交回复
热议问题