Perl: Environment Variables

Source: Internet
Author: User
Tags perl script

How to Use the Perl script to operate system environment variables?

Clever 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,

So the following section is simple:CodeYou can: 

  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 through the above Code, it is best to restart the machine ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.