Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the Windows PATH variable from Perl?

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?

like image 506
Xetius Avatar asked Feb 04 '09 18:02

Xetius


People also ask

How do I permanently set PATH variable in Windows?

In System Properties, tap on the Advanced tab and click the Environment Variables button at the bottom. Next, click on the Path entry from either section in the Environment Variables window—depending on whether you set the PATH temporarily or permanently—and hit the Edit button.

How do I get an environment variable in Perl?

In Perl, the environment variables are available via the special %ENV hash; each key in this hash represents one environment variable. At the start of your program's execution, %ENV holds values it has inherited from its parent process (generally the shell).

How do I set an environment variable in pointing?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.


2 Answers

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.

like image 92
8 revs Avatar answered Sep 19 '22 15:09

8 revs


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

like image 43
jrockway Avatar answered Sep 17 '22 15:09

jrockway