简体   繁体   English

如何从Perl设置Windows PATH变量?

[英]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 :: EnvWin32 :: 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.

相关问题 如何在Windows上的Java应用程序中设置/更新PATH变量? - How can I set/update PATH variable from within java application on Windows? 如何在Windows中使用Perl获取没有文件名的路径? - How can I get the path without the filename in Windows using Perl? 如何在Windows上的环境变量中设置Jenkins ant构建中的build.xml文件的路径 - How do I set the path to the build.xml file in a Jenkins ant build from an environment variable on Windows 从PowerShell设置时如何处理Windows PATH变量扩展? - How do I handle Windows PATH variable expansion when set from PowerShell? 如何在 Windows 8 的 PATH 环境变量中添加 Octave? - How Can I add Octave in PATH environment variable in Windows 8? 在 Windows 上使用 Perl,如何确保我在 chdir 后以正确的大小写获得路径? - Using Perl on Windows, how can I ensure I get the path in the correct case following a chdir? 如何将变量设置为Windows上父目录的路径? - How to SET a variable to the path of parent directory on windows? 如何从 PATH 环境变量中提取完整路径? - How can I extract a full path from the PATH environment variable? 如何在Windows的Perl中调用Winrar - How can I call winrar in perl on windows 如何在 windows 中将凭据设置为环境变量? - How can i set up credentials as an environmental variable in windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM