How to Use Perl scripts to operate system environment variables?
Presumably many of my friends will immediately think of the special hash variable $ ENV provided in Perl. Right, some system variables can be obtained in $ ENV. How can this problem be obtained? For example, if you want to know the content of the system environment variable path, you can get it through $ ENV {'path. If you want to set the PATH environment variable, use $ ENV {'path'} = 'C: \ windows \ system32. It's also easy to get a list of all environment variables. It's not enough to do hash traversal ~
In fact, what I really want to ask in this article is, after you set an environment variable through the above method, right-click the attribute of "My Computer, has the PATH variable value changed after the Advanced tab is selected? In fact, you will find that it has never changed! But do not complain that Perl has cheated you. In fact, it has not cheated you, but you have not really understood it. In fact, you can set the PATH variable by setting $ ENV {'path'}, but the change caused by this setting is only at the session level, that is, youProgramLevel. If you do not understand it, you can think of the effect caused by entering set Path = "C: \ royen" after opening a cmd, in this window, the PATH environment variable is "C: \ royen ", the PATH variable of the new window after you open a cmd will find that it is not "C :\\ royen";
To add or change a system environment variable, we need to turn to the Registry for help. You only need to find the location where the environment variables are stored in the registry.
General System variables can be found in the Registry HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ environment.
Implementation:
CopyCode The Code is 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 adding environment variables, remember to restart the machine to make it take effect.