[英]How can I set the Windows PATH variable from Perl?
I need to set the an environment variable from within Perl. 我需要在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. 具体来说,它是我想要改变的PATH变量。
How do I get and set these variables? 如何获取和设置这些变量?
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 ). 如果你需要全局和永久地更改环境变量,就好像你在控制面板中设置它一样,那么你必须使用注册表 (更新:现在有模块来执行此操作, Win32 :: Env和Win32 :: Env ::路径 )。 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. 请注意,更改注册表中的变量并“广播”更改不会更改某些当前进程中的环境变量,尤其是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 (eg $ENV{PATH}). 如果您只想更改当前进程(以及随后生成的子进程),那么全局%ENV哈希变量就是您想要的(例如$ ENV {PATH})。 See perldoc perlvar . 见perldoc perlvar 。
$ENV{PATH}? $ 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,然后在父进程中看到该更改 - 环境不能以这种方式工作。
你可以使用%ENV
哈希来做到这一点
$ENV{PATH} = 'C:\\Windows\;D:\\Programs';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.