Introduction to global variables and non-global environments in Lua _lua

Source: Internet
Author: User
Tags lua

Today we'll talk about two topics-global variables and non-global environments.

As we all feel at the moment, the content of global variables is very simple, but not the global context of the content of a little exercise brain cells.

1. The prototype of the global variable

In Lua, it's easy to declare a global variable, that is, when you define a variable, don't precede it with a local.

This mysterious global variable, in essence, is also a table, which saves the global variables we create into a table.

And the name of this table is: _g

Let's take a look at the code:

Copy Code code as follows:

--Define a global variable
Gname = "Ouch, very lame";

--Output The value of a variable in three ways
Print (gname);
Print (_g["gname"]);
Print (_g.gname);

The output results are as follows:

Copy Code code as follows:

[Lua-print] Ouch, it's so lame.
[Lua-print] Ouch, it's so lame.
[Lua-print] Ouch, it's so lame.

We defined a global variable gname, so this gname became a field of _g.
How, very simple.

2. Non-Global environment

For global variables, regardless of where, in which language, people will always warn: "Do not abuse, the consequences of the ego."
Perhaps because of this, LUA has a rather special mechanism: a non-global environment.
I call it a "global variable that does not cause global impact."

3. Change the function of the global variable environment--setfenv function

First look at the following code:

Copy Code code as follows:

--Define a global variable
Gname = "Ouch, very lame";

--Reset the current global environment to a new table
Setfenv (1, {});

--Output value
Print (gname);

If you run the code now, the output will be like this:
Copy Code code as follows:

[Lua-print] LUA ERROR: [string "Src/main.lua"]:107:attempt to call global ' print ' (a nil value)

Why? It's an unexpected face print function that can't be found?

This is because we have changed the global variable environment in the scope of the current function, the global variable is saved in _g by default, and now the global variable is in a new table.

The table is currently empty, so there are no global variables.

setfenv function is used to change the scope of a function of the global environment, in layman's terms, is a function within the scope of the _g to be lost.

The SETFENV function has two parameters, respectively, representing:

1. The first parameter can be a function that is about to change the environment, or it can be a number. The number 1 represents the current function, and the number 2 represents the function that calls the current function, followed by the following.

2. The second parameter, the new Global environment table.

4. Keep the original _g

Now even the print function can not be used, for testing is very inconvenient, we can do a small action, the original _g reserve.

The following code:

Copy Code code as follows:

--Define a global variable
Gname = "Ouch, very lame";

--Reset the current global environment to a new table
Setfenv (1, {g = _g});

--Output value
G.print (Gname);

--Define a global variable again
Gname = "Ouch, a little wrong Oh";

--Output value again
G.print (Gname);

--Output the original value
G.print (G.gname);

As long as you define a new environment, put _g as a field in the new table, you can call the original global variable.

So the output is as follows:

Copy Code code as follows:

[Lua-print] Nil
[Lua-print] Ouch, something's wrong.
[Lua-print] Ouch, it's so lame.

The output of the three call g.print function is not the same:

A. For the first time, the global environment has just been reset, when the current function has only one global variable, and that is G, so the Gname value is nil.

B. The second time, we are assigning the gname again, at this time, already in the new environment, so the output of the next Gname value is present.

C. Third, this output is the value of G.gname, the Gname value by G is the original global environment value, so the Gname value is still the original "Ouch, very lame."

In fact, what is the use of it? It's better to use local variables directly.

Indeed, there is no particular place in this case.

In the book for the introduction of knowledge are deep, so there is no more in-depth introduction, see the later content, I continue to share with you.

5. Use the __index element method to preserve the original _g

Here's another tip to share, just for example, to keep _g, but when you call print and other functions, you need to be in the form of G.print, which is a bit of a hindrance.

We can use __index to solve this problem, the following code:

Copy Code code as follows:

--Define a global variable
Gname = "Ouch, very lame";

--a table that is about to become a new environment
Local NEWG = {};
Setmetatable (NEWG, {__index = _g});

--Reset the current global environment to a new table
Setfenv (1, NEWG);

Gname = "No more ouch, very annoying!" ";

--Output value
Print (gname);
Print (_g.gname);

We set a meta table for the new table, and the __index method of the meta table is _g.

As a result, when the new environment can not find the print field, it will go to the _g search.

The output results are as follows:

Copy Code code as follows:

[Lua-print] No more ouch, very annoying!
[Lua-print] Ouch, it's so lame.

The first output is the new environment of the Gname value, the second output is the original environment in the Gname value, do not affect each other.

6. End

Well, about global variables and non-global environments, that's all for a while.

Although temporarily still do not feel what effect, it does not matter, there will be later on this part of the content.

Just like __index, it is the foundation, which may be mentioned often later.

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.