Copy Code code as follows:
function foo ()
Print (g or "No g defined!")
End
Foo ()
Setfenv (foo, {g = m, print = print})--set Foo's environment as table {g=100, ...}
Foo ()
Print (g or "No g defined!")
--no g defined!
--100
--no g defined!
definition: A function environment is a collection of global variables that are seen by the function at execution time, and is hosted in a table.
Description: Each function can have its own environment, can be displayed by setfenv to specify a function of the environment. If you do not display the specified, the function's environment defaults to the environment that defines the function's function. In the previous code, the variable G was not defined in the default environment of function Foo, so the first execution of Foo, G for nil, expression G or "no g defined!" The value is "No g defined!". Subsequently, Foo was assigned an environment of {g = m, print = print}. This environment defines the (global) variable g, and prints the print function, so the value of the second execution foo,g is 100. But in the context of the function that defines the function Foo, G is still an undefined variable.
application: The function environment has a lot of functions, it can realize the "safe sandbox" of function execution, and the implementation of the LUA package also relies on it.
Package
Copy Code code as follows:
--mypack.lua:
Module (..., package.seeall)--Define Package
ver = "0.1 Alpha"
function Afuninmypack ()
Print ("hello!")
End
_g.afuncfrommypack = Afuninmypack
Copy Code code as follows:
--testp.lua:
Pack = require "Mypack"--Import Package
Print (ver or "No ver defined!")
Print (Pack.ver)
Print (Afuninmypack or "No afuninmypack defined!")
Pack.afuninmypack ()
Print (Afuncfrommypack or "No afuncfrommypack defined!")
Afuncfrommypack ()
Copy Code code as follows:
--Execute Testp.lua results
No ver defined!
0.1 Alpha
No Afuninmypack defined!
Hello!
Function:0068cb50
Hello!
definition: A package is a way to organize your code.
implementation: Typically, a package is defined as a module function within a LUA file. The module also defines a function environment for a new package so that the global variables defined in this package are in this environment, not in the context of the function that uses the package. It is critical to understand this. Take the preceding code as an example, "module (..., package.seeall)" means to define a package with the same name as the file that defines the package (except for the filename suffix, in the previous code, "Mypack"), And in the function environment of the package, you can access the function environment that uses the package (for example, the implementation of the package uses print, which is not defined in the package, but is defined in the external environment where the package is used).
How to use: generally use the Require function to import a package, the package to be imported must be placed on the package path (PackagePath). Package paths can be set by Package.path or by environment variables. In general, the current work path is always in the package path.