如何从 Perl 设置 Windows PATH 变量?
我需要在 Perl 中设置环境变量。 理想情况下,我需要查询一个变量,然后在不需要时更改它。 具体来说,我要更改的是 PATH 变量。
我如何获取和设置这些变量?
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.
How do I get and set these variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要全局且永久地更改环境变量,就像在控制面板中设置它一样,那么您必须 用注册表搞砸(更新:现在有模块可以做到这一点,Win32 ::Env 和 Win32::Env::Path) 。 请注意,更改注册表中的变量并“广播”更改不会更改某些当前进程中的环境变量,特别是 perl.exe 和 cmd.exe。
如果您只想更改当前进程(以及随后生成的子进程),那么全局 %ENV 哈希变量就是您想要的(例如 $ENV{PATH})。 请参阅 perldoc perlvar。
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.
$ENV{路径}?
但是请记住,环境变量仅影响子进程。 您无法运行 Perl 程序,更改 %ENV,然后在父进程中看到该更改 - 环境不是这样工作的。
$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.
您可以使用
%ENV
哈希来做到这一点You can do that using the
%ENV
hash