如何从 Perl 设置 Windows PATH 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/512613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:01:29  来源:igfitidea点击:

How can I set the Windows PATH variable from Perl?

windowsperlenvironment

提问by Xetius

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 variable I want to change.

我需要在 Perl 中设置一个环境变量。理想情况下,我需要查询一个变量,然后在不需要时更改它。具体来说,它是我想要更改的 PATH 变量。

How do I get and set these variables?

如何获取和设置这些变量?

回答by runrig

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::Envand 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.

如果您需要全局且永久地更改环境变量,就像在控制面板中设置它一样,那么您必须修改注册表(更新:现在有模块可以做到这一点,Win32::EnvWin32::Env ::路径)。请注意,更改注册表中的变量并“广播”更改不会更改某些当前进程中的环境变量,尤其是 perl.exe 和 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.

如果您只想更改当前进程(以及随后产生的子进程),那么全局 %ENV 哈希变量就是您想要的(例如 $ENV{PATH})。请参阅perldoc perlvar

回答by jrockway

$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.

但是请记住,环境变量仅影响子流程。您不能运行 Perl 程序,更改 %ENV,然后在父进程中看到该更改——环境不是那样工作的。

回答by Leon Timmermans

You can do that using the %ENVhash

你可以使用%ENV哈希来做到这一点

$ENV{PATH} = 'C:\Windows\;D:\Programs';