How Do I Push a Path to Unix Path Environment Variable in Perl?
In the UNIX operating system, each running process has a collection of environment variables to which it can read and write. One of those variables is "PATH," corresponding to a colon-separated list of folders where the process will look for the executables of other programs it may need to run. If, in particular, your UNIX process consists of a running Perl application, you can set the variable of PATH from within your Perl code.
Instructions
-
-
1
Store the value you want to assign to the PATH environment variable into a Perl variable, as in the following sample code:
$desiredPath = "/usr/bin:/usr/local/bin"
Replace "/usr/bin:/usr/local/bin" with the value you want to assign to the PATH variable.
-
2
Create a Perl string that contains the whole command that will set the PATH variable:
$systemCommand = "setenv PATH=".$desiredPATH
The "setenv" primitive will be executed by the UNIX command shell when Perl transfers control to it.
-
-
3
Transfer control to the UNIX shell by using Perl's built-in "system" function:
system($systemCommand)
After executing this line, the PATH environment variable will have the value selected in Step 1.
-
1