如何从Perl设置Windows PATH变量?

时间:2022-06-08 12:06:30

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?

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

3 个解决方案

#1


16  

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.

如果你需要全局和永久地更改环境变量,就好像你在控制面板中设置它,那么你必须使用注册表(更新:现在有模块来执行此操作,Win32 :: Env和Win32 :: 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。

#2


7  

$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,然后在父进程中看到该更改 - 环境不能以这种方式工作。

#3


4  

You can do that using the %ENV hash

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

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

#1


16  

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.

如果你需要全局和永久地更改环境变量,就好像你在控制面板中设置它,那么你必须使用注册表(更新:现在有模块来执行此操作,Win32 :: Env和Win32 :: 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。

#2


7  

$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,然后在父进程中看到该更改 - 环境不能以这种方式工作。

#3


4  

You can do that using the %ENV hash

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

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