How can I set the Windows PATH variable from Perl?

ぐ巨炮叔叔 提交于 2019-12-03 17:03:14

问题


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?


回答1:


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.




回答2:


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




回答3:


You can do that using the %ENV hash

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


来源:https://stackoverflow.com/questions/512613/how-can-i-set-the-windows-path-variable-from-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!