How do I use the Perl script operating system environment variables?
Presumably a lot of friends will soon think of this particular hash variable that $env in Perl, and, yes, you can get some system variables in $env. How do I get it? For example, to know the contents of the system environment variable of Path, we can get it by $env{' Path '. If you want to set Path for this environment variable, ha, use $env{' Path '}= ' C:\Windows\System32 ' to set it. To get all the list of environment variables is also very simple, hash traversal can not be
In fact, the real question in this article is that you have set an environment variable through the above method, right click on the "My Computer" properties, select the middle-and-middle tab to see the value of the path variable really changed it? In fact, the careful you will find, completely unchanged! But do not complain that Perl deceives you, it does not deceive you, but you do not really understand. In fact, the path variable can be set by setting $env{' Path ', but the change caused by this setting is only the session level, which is your program level. If you do not understand, you can imagine that after opening a CMD, enter the effect of set path= "C:\\royen", that is, the PATH environment variable is "C:\\royen" in this window, And you open a new window after cmd the path variable will find it is completely not "C:\\royen";
So to really add or change a system environment variable, we need to turn to the registry to help. Just find the location where the environment variable is stored in the registry.
General system variables can be found under registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.
Realize:
Copy Code code as follows:
My $loc = "System\\currentcontrolset\\control\\session manager\\environment";
My $regkey;
$HKEY _local_machine->open ($loc, $regkey) | | Die "Open Registry fail,information:$!\n";
$regkey->setvalueex ("Perllib", 0,REG_SZ, "c:\\staf\\bin;c:\\staf\\bin\\perl58");
After you add an environment variable, remember to restart the machine for it to take effect.